An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
Ian Craggs
Date:
Fri Apr 11 22:31:55 2014 +0100
Revision:
12:cc7f2d62a393
Parent:
9:01b8cc7d94cc
Child:
13:fd82db992024
Unsubscribe, publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 6:4d312a49200b 1 /*******************************************************************************
icraggs 6:4d312a49200b 2 * Copyright (c) 2014 IBM Corp.
sam_grove 0:fe461e4d7afe 3 *
icraggs 6:4d312a49200b 4 * All rights reserved. This program and the accompanying materials
icraggs 6:4d312a49200b 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 6:4d312a49200b 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
sam_grove 0:fe461e4d7afe 7 *
icraggs 6:4d312a49200b 8 * The Eclipse Public License is available at
icraggs 6:4d312a49200b 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 6:4d312a49200b 10 * and the Eclipse Distribution License is available at
icraggs 6:4d312a49200b 11 * http://www.eclipse.org/org/documents/edl-v10.php.
sam_grove 0:fe461e4d7afe 12 *
icraggs 6:4d312a49200b 13 * Contributors:
icraggs 6:4d312a49200b 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 6:4d312a49200b 15 *******************************************************************************/
sam_grove 0:fe461e4d7afe 16
icraggs 2:dcfdd2abfe71 17 #if !defined(MQTTCLIENT_H)
icraggs 2:dcfdd2abfe71 18 #define MQTTCLIENT_H
icraggs 2:dcfdd2abfe71 19
icraggs 2:dcfdd2abfe71 20 #include "FP.h"
icraggs 3:dbff6b768d28 21 #include "MQTTPacket.h"
icraggs 6:4d312a49200b 22 #include "stdio.h"
icraggs 2:dcfdd2abfe71 23
icraggs 3:dbff6b768d28 24 namespace MQTT
icraggs 3:dbff6b768d28 25 {
icraggs 3:dbff6b768d28 26
icraggs 2:dcfdd2abfe71 27
icraggs 2:dcfdd2abfe71 28 enum QoS { QOS0, QOS1, QOS2 };
sam_grove 0:fe461e4d7afe 29
icraggs 2:dcfdd2abfe71 30
icraggs 3:dbff6b768d28 31 struct Message
icraggs 2:dcfdd2abfe71 32 {
icraggs 2:dcfdd2abfe71 33 enum QoS qos;
icraggs 2:dcfdd2abfe71 34 bool retained;
icraggs 2:dcfdd2abfe71 35 bool dup;
Ian Craggs 12:cc7f2d62a393 36 unsigned short id;
icraggs 2:dcfdd2abfe71 37 void *payload;
icraggs 2:dcfdd2abfe71 38 size_t payloadlen;
sam_grove 0:fe461e4d7afe 39 };
sam_grove 0:fe461e4d7afe 40
icraggs 6:4d312a49200b 41 template<class Network, class Timer, class Thread> class Client;
icraggs 4:4ef00243708e 42
icraggs 9:01b8cc7d94cc 43 class PacketId
icraggs 9:01b8cc7d94cc 44 {
icraggs 9:01b8cc7d94cc 45 public:
icraggs 9:01b8cc7d94cc 46 PacketId();
icraggs 9:01b8cc7d94cc 47
Ian Craggs 12:cc7f2d62a393 48 int getNext();
Ian Craggs 12:cc7f2d62a393 49
icraggs 9:01b8cc7d94cc 50 private:
icraggs 9:01b8cc7d94cc 51 static const int MAX_PACKET_ID = 65535;
icraggs 9:01b8cc7d94cc 52 int next;
icraggs 9:01b8cc7d94cc 53 };
icraggs 9:01b8cc7d94cc 54
icraggs 9:01b8cc7d94cc 55 typedef void (*messageHandler)(Message*);
icraggs 2:dcfdd2abfe71 56
icraggs 6:4d312a49200b 57 template<class Network, class Timer, class Thread> class Client
icraggs 2:dcfdd2abfe71 58 {
icraggs 2:dcfdd2abfe71 59
Ian Craggs 12:cc7f2d62a393 60 public:
Ian Craggs 12:cc7f2d62a393 61
Ian Craggs 12:cc7f2d62a393 62 struct Result
Ian Craggs 12:cc7f2d62a393 63 {
Ian Craggs 12:cc7f2d62a393 64 /* success or failure result data */
Ian Craggs 12:cc7f2d62a393 65 Client<Network, Timer, Thread>* client;
Ian Craggs 12:cc7f2d62a393 66 int connack_rc;
Ian Craggs 12:cc7f2d62a393 67 };
Ian Craggs 12:cc7f2d62a393 68
Ian Craggs 12:cc7f2d62a393 69 typedef void (*resultHandler)(Result*);
icraggs 4:4ef00243708e 70
Ian Craggs 12:cc7f2d62a393 71 Client(Network* network, const int MAX_MQTT_PACKET_SIZE = 100, const int command_timeout = 30);
icraggs 2:dcfdd2abfe71 72
icraggs 9:01b8cc7d94cc 73 int connect(MQTTPacket_connectData* options = 0, resultHandler fn = 0);
icraggs 2:dcfdd2abfe71 74
icraggs 9:01b8cc7d94cc 75 template<class T>
icraggs 9:01b8cc7d94cc 76 int connect(MQTTPacket_connectData* options = 0, T *item = 0, void(T::*method)(Result *) = 0); // alternative to pass in pointer to member function
icraggs 9:01b8cc7d94cc 77
icraggs 9:01b8cc7d94cc 78 int publish(const char* topic, Message* message, resultHandler rh = 0);
sam_grove 0:fe461e4d7afe 79
icraggs 9:01b8cc7d94cc 80 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh, resultHandler rh = 0);
icraggs 2:dcfdd2abfe71 81
Ian Craggs 12:cc7f2d62a393 82 int unsubscribe(const char* topicFilter, resultHandler rh = 0);
icraggs 9:01b8cc7d94cc 83
icraggs 9:01b8cc7d94cc 84 int disconnect(int timeout, resultHandler rh = 0);
sam_grove 0:fe461e4d7afe 85
icraggs 8:c46930bd6c82 86 void run(void const *argument);
icraggs 8:c46930bd6c82 87
icraggs 2:dcfdd2abfe71 88 private:
icraggs 2:dcfdd2abfe71 89
Ian Craggs 12:cc7f2d62a393 90 int cycle(int timeout);
Ian Craggs 12:cc7f2d62a393 91 int keepalive();
icraggs 2:dcfdd2abfe71 92
icraggs 3:dbff6b768d28 93 int decodePacket(int* value, int timeout);
icraggs 4:4ef00243708e 94 int readPacket(int timeout = -1);
icraggs 6:4d312a49200b 95 int sendPacket(int length, int timeout = -1);
icraggs 3:dbff6b768d28 96
icraggs 4:4ef00243708e 97 Thread* thread;
icraggs 4:4ef00243708e 98 Network* ipstack;
Ian Craggs 12:cc7f2d62a393 99 Timer command_timer, ping_timer;
icraggs 4:4ef00243708e 100
icraggs 9:01b8cc7d94cc 101 char* buf;
icraggs 2:dcfdd2abfe71 102 int buflen;
icraggs 2:dcfdd2abfe71 103
icraggs 4:4ef00243708e 104 char* readbuf;
Ian Craggs 12:cc7f2d62a393 105 int readbuflen;
Ian Craggs 12:cc7f2d62a393 106
Ian Craggs 12:cc7f2d62a393 107 unsigned int keepAliveInterval;
Ian Craggs 12:cc7f2d62a393 108 bool ping_outstanding;
icraggs 4:4ef00243708e 109
icraggs 6:4d312a49200b 110 int command_timeout; // max time to wait for any MQTT command to complete, in seconds
icraggs 9:01b8cc7d94cc 111 PacketId packetid;
icraggs 9:01b8cc7d94cc 112
icraggs 9:01b8cc7d94cc 113 typedef FP<void, Result*> resultHandlerFP;
icraggs 9:01b8cc7d94cc 114 // how many concurrent operations should we allow? Each one will require a function pointer
icraggs 9:01b8cc7d94cc 115 resultHandlerFP connectHandler;
icraggs 9:01b8cc7d94cc 116
icraggs 9:01b8cc7d94cc 117 #define MAX_MESSAGE_HANDLERS 5
icraggs 9:01b8cc7d94cc 118 typedef FP<void, Message*> messageHandlerFP;
Ian Craggs 12:cc7f2d62a393 119 messageHandlerFP messageHandlers[MAX_MESSAGE_HANDLERS]; // Linked list, or constructor parameter to limit array size?
Ian Craggs 12:cc7f2d62a393 120
Ian Craggs 12:cc7f2d62a393 121 static void threadfn(void* arg);
icraggs 4:4ef00243708e 122
sam_grove 0:fe461e4d7afe 123 };
sam_grove 0:fe461e4d7afe 124
Ian Craggs 12:cc7f2d62a393 125 }
Ian Craggs 12:cc7f2d62a393 126
Ian Craggs 12:cc7f2d62a393 127
Ian Craggs 12:cc7f2d62a393 128 template<class Network, class Timer, class Thread> void MQTT::Client<Network, Timer, Thread>::threadfn(void* arg)
Ian Craggs 12:cc7f2d62a393 129 {
Ian Craggs 12:cc7f2d62a393 130 ((Client<Network, Timer, Thread>*) arg)->run(NULL);
Ian Craggs 12:cc7f2d62a393 131 }
icraggs 8:c46930bd6c82 132
icraggs 8:c46930bd6c82 133
Ian Craggs 12:cc7f2d62a393 134 template<class Network, class Timer, class Thread> MQTT::Client<Network, Timer, Thread>::Client(Network* network, const int MAX_MQTT_PACKET_SIZE, const int command_timeout) : packetid()
icraggs 8:c46930bd6c82 135 {
icraggs 8:c46930bd6c82 136
Ian Craggs 12:cc7f2d62a393 137 buf = new char[MAX_MQTT_PACKET_SIZE];
Ian Craggs 12:cc7f2d62a393 138 readbuf = new char[MAX_MQTT_PACKET_SIZE];
Ian Craggs 12:cc7f2d62a393 139 buflen = readbuflen = MAX_MQTT_PACKET_SIZE;
icraggs 8:c46930bd6c82 140 this->command_timeout = command_timeout;
icraggs 8:c46930bd6c82 141 this->thread = 0;
icraggs 8:c46930bd6c82 142 this->ipstack = network;
Ian Craggs 12:cc7f2d62a393 143 this->command_timer = Timer();
Ian Craggs 12:cc7f2d62a393 144 this->ping_timer = Timer();
Ian Craggs 12:cc7f2d62a393 145 this->ping_outstanding = 0;
icraggs 8:c46930bd6c82 146 }
icraggs 8:c46930bd6c82 147
icraggs 8:c46930bd6c82 148
icraggs 8:c46930bd6c82 149 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::sendPacket(int length, int timeout)
icraggs 8:c46930bd6c82 150 {
icraggs 8:c46930bd6c82 151 int sent = 0;
icraggs 8:c46930bd6c82 152
icraggs 8:c46930bd6c82 153 while (sent < length)
Ian Craggs 12:cc7f2d62a393 154 sent += ipstack->write(&buf[sent], length, -1);
Ian Craggs 12:cc7f2d62a393 155 if (sent == length)
Ian Craggs 12:cc7f2d62a393 156 ping_timer.reset(); // record the fact that we have successfully sent the packet
icraggs 8:c46930bd6c82 157 return sent;
icraggs 8:c46930bd6c82 158 }
icraggs 8:c46930bd6c82 159
icraggs 8:c46930bd6c82 160
icraggs 8:c46930bd6c82 161 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::decodePacket(int* value, int timeout)
icraggs 8:c46930bd6c82 162 {
icraggs 8:c46930bd6c82 163 char c;
icraggs 8:c46930bd6c82 164 int multiplier = 1;
icraggs 8:c46930bd6c82 165 int len = 0;
icraggs 8:c46930bd6c82 166 #define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
icraggs 8:c46930bd6c82 167
icraggs 8:c46930bd6c82 168 *value = 0;
icraggs 8:c46930bd6c82 169 do
icraggs 8:c46930bd6c82 170 {
icraggs 8:c46930bd6c82 171 int rc = MQTTPACKET_READ_ERROR;
icraggs 8:c46930bd6c82 172
icraggs 8:c46930bd6c82 173 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
icraggs 8:c46930bd6c82 174 {
icraggs 8:c46930bd6c82 175 rc = MQTTPACKET_READ_ERROR; /* bad data */
icraggs 8:c46930bd6c82 176 goto exit;
icraggs 8:c46930bd6c82 177 }
icraggs 8:c46930bd6c82 178 rc = ipstack->read(&c, 1, timeout);
icraggs 8:c46930bd6c82 179 if (rc != 1)
icraggs 8:c46930bd6c82 180 goto exit;
icraggs 8:c46930bd6c82 181 *value += (c & 127) * multiplier;
icraggs 8:c46930bd6c82 182 multiplier *= 128;
icraggs 8:c46930bd6c82 183 } while ((c & 128) != 0);
icraggs 8:c46930bd6c82 184 exit:
icraggs 8:c46930bd6c82 185 return len;
icraggs 8:c46930bd6c82 186 }
icraggs 8:c46930bd6c82 187
icraggs 8:c46930bd6c82 188
icraggs 8:c46930bd6c82 189 /**
icraggs 8:c46930bd6c82 190 * If any read fails in this method, then we should disconnect from the network, as on reconnect
icraggs 8:c46930bd6c82 191 * the packets can be retried.
icraggs 8:c46930bd6c82 192 * @param timeout the max time to wait for the packet read to complete, in milliseconds
icraggs 8:c46930bd6c82 193 * @return the MQTT packet type, or -1 if none
icraggs 8:c46930bd6c82 194 */
icraggs 8:c46930bd6c82 195 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::readPacket(int timeout)
icraggs 8:c46930bd6c82 196 {
icraggs 8:c46930bd6c82 197 int rc = -1;
icraggs 8:c46930bd6c82 198 MQTTHeader header = {0};
icraggs 8:c46930bd6c82 199 int len = 0;
icraggs 8:c46930bd6c82 200 int rem_len = 0;
icraggs 8:c46930bd6c82 201
icraggs 8:c46930bd6c82 202 /* 1. read the header byte. This has the packet type in it */
icraggs 8:c46930bd6c82 203 if (ipstack->read(readbuf, 1, timeout) != 1)
icraggs 8:c46930bd6c82 204 goto exit;
icraggs 8:c46930bd6c82 205
icraggs 8:c46930bd6c82 206 len = 1;
icraggs 8:c46930bd6c82 207 /* 2. read the remaining length. This is variable in itself */
icraggs 8:c46930bd6c82 208 decodePacket(&rem_len, timeout);
icraggs 8:c46930bd6c82 209 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
icraggs 8:c46930bd6c82 210
icraggs 8:c46930bd6c82 211 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
icraggs 8:c46930bd6c82 212 if (ipstack->read(readbuf + len, rem_len, timeout) != rem_len)
icraggs 8:c46930bd6c82 213 goto exit;
icraggs 8:c46930bd6c82 214
icraggs 8:c46930bd6c82 215 header.byte = readbuf[0];
icraggs 8:c46930bd6c82 216 rc = header.bits.type;
icraggs 8:c46930bd6c82 217 exit:
icraggs 8:c46930bd6c82 218 return rc;
icraggs 3:dbff6b768d28 219 }
icraggs 3:dbff6b768d28 220
icraggs 8:c46930bd6c82 221
Ian Craggs 12:cc7f2d62a393 222 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::cycle(int timeout)
icraggs 8:c46930bd6c82 223 {
icraggs 8:c46930bd6c82 224 /* get one piece of work off the wire and one pass through */
icraggs 8:c46930bd6c82 225
Ian Craggs 12:cc7f2d62a393 226 // read the socket, see what work is due
icraggs 9:01b8cc7d94cc 227 int packet_type = readPacket(timeout);
icraggs 8:c46930bd6c82 228
icraggs 8:c46930bd6c82 229 printf("packet type %d\n", packet_type);
Ian Craggs 12:cc7f2d62a393 230
Ian Craggs 12:cc7f2d62a393 231 int len, rc;
icraggs 8:c46930bd6c82 232 switch (packet_type)
icraggs 8:c46930bd6c82 233 {
Ian Craggs 12:cc7f2d62a393 234 case CONNACK:
Ian Craggs 12:cc7f2d62a393 235 if (this->thread)
Ian Craggs 12:cc7f2d62a393 236 {
Ian Craggs 12:cc7f2d62a393 237 Result res = {this, 0};
Ian Craggs 12:cc7f2d62a393 238 int connack_rc = -1;
Ian Craggs 12:cc7f2d62a393 239 if (MQTTDeserialize_connack(&res.connack_rc, readbuf, readbuflen) == 1)
Ian Craggs 12:cc7f2d62a393 240 ;
Ian Craggs 12:cc7f2d62a393 241 connectHandler(&res);
Ian Craggs 12:cc7f2d62a393 242 }
icraggs 8:c46930bd6c82 243 case PUBACK:
icraggs 8:c46930bd6c82 244 case SUBACK:
icraggs 8:c46930bd6c82 245 break;
Ian Craggs 12:cc7f2d62a393 246 case PUBLISH:
Ian Craggs 12:cc7f2d62a393 247 MQTTString topicName;
Ian Craggs 12:cc7f2d62a393 248 Message msg;
Ian Craggs 12:cc7f2d62a393 249 rc = MQTTDeserialize_publish((int*)&msg.dup, (int*)&msg.qos, (int*)&msg.retained, (int*)&msg.id, &topicName,
Ian Craggs 12:cc7f2d62a393 250 (char**)&msg.payload, (int*)&msg.payloadlen, readbuf, readbuflen);
Ian Craggs 12:cc7f2d62a393 251 if (msg.qos == QOS0)
Ian Craggs 12:cc7f2d62a393 252 messageHandlers[0](&msg);
Ian Craggs 12:cc7f2d62a393 253 break;
Ian Craggs 12:cc7f2d62a393 254 case PUBREC:
Ian Craggs 12:cc7f2d62a393 255 int type, dup, mypacketid;
Ian Craggs 12:cc7f2d62a393 256 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, readbuflen) == 1)
Ian Craggs 12:cc7f2d62a393 257 ;
Ian Craggs 12:cc7f2d62a393 258 len = MQTTSerialize_ack(buf, buflen, PUBREL, 0, mypacketid);
Ian Craggs 12:cc7f2d62a393 259 rc = sendPacket(len); // send the subscribe packet
Ian Craggs 12:cc7f2d62a393 260 if (rc != len)
Ian Craggs 12:cc7f2d62a393 261 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 262
icraggs 8:c46930bd6c82 263 break;
icraggs 8:c46930bd6c82 264 case PUBCOMP:
icraggs 8:c46930bd6c82 265 break;
Ian Craggs 12:cc7f2d62a393 266 case PINGRESP:
Ian Craggs 12:cc7f2d62a393 267 if (ping_outstanding)
Ian Craggs 12:cc7f2d62a393 268 ping_outstanding = false;
Ian Craggs 12:cc7f2d62a393 269 //else disconnect();
icraggs 8:c46930bd6c82 270 break;
icraggs 8:c46930bd6c82 271 case -1:
icraggs 8:c46930bd6c82 272 break;
Ian Craggs 12:cc7f2d62a393 273 }
Ian Craggs 12:cc7f2d62a393 274 keepalive();
Ian Craggs 12:cc7f2d62a393 275 exit:
icraggs 8:c46930bd6c82 276 return packet_type;
Ian Craggs 12:cc7f2d62a393 277 }
Ian Craggs 12:cc7f2d62a393 278
Ian Craggs 12:cc7f2d62a393 279
Ian Craggs 12:cc7f2d62a393 280 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::keepalive()
Ian Craggs 12:cc7f2d62a393 281 {
Ian Craggs 12:cc7f2d62a393 282 int rc = 0;
Ian Craggs 12:cc7f2d62a393 283
Ian Craggs 12:cc7f2d62a393 284 if (keepAliveInterval == 0)
Ian Craggs 12:cc7f2d62a393 285 goto exit;
Ian Craggs 12:cc7f2d62a393 286
Ian Craggs 12:cc7f2d62a393 287 if (ping_timer.read_ms() >= (keepAliveInterval * 1000))
Ian Craggs 12:cc7f2d62a393 288 {
Ian Craggs 12:cc7f2d62a393 289 if (ping_outstanding)
Ian Craggs 12:cc7f2d62a393 290 rc = -1;
Ian Craggs 12:cc7f2d62a393 291 else
Ian Craggs 12:cc7f2d62a393 292 {
Ian Craggs 12:cc7f2d62a393 293 int len = MQTTSerialize_pingreq(buf, buflen);
Ian Craggs 12:cc7f2d62a393 294 rc = sendPacket(len); // send the connect packet
Ian Craggs 12:cc7f2d62a393 295 if (rc != len)
Ian Craggs 12:cc7f2d62a393 296 rc = -1; // indicate there's a problem
Ian Craggs 12:cc7f2d62a393 297 else
Ian Craggs 12:cc7f2d62a393 298 ping_outstanding = true;
Ian Craggs 12:cc7f2d62a393 299 }
Ian Craggs 12:cc7f2d62a393 300 }
Ian Craggs 12:cc7f2d62a393 301
Ian Craggs 12:cc7f2d62a393 302 exit:
Ian Craggs 12:cc7f2d62a393 303 return rc;
icraggs 8:c46930bd6c82 304 }
icraggs 8:c46930bd6c82 305
icraggs 8:c46930bd6c82 306
icraggs 8:c46930bd6c82 307 template<class Network, class Timer, class Thread> void MQTT::Client<Network, Timer, Thread>::run(void const *argument)
Ian Craggs 12:cc7f2d62a393 308 {
Ian Craggs 12:cc7f2d62a393 309 while (true)
Ian Craggs 12:cc7f2d62a393 310 cycle((keepAliveInterval * 1000) - ping_timer.read_ms());
icraggs 8:c46930bd6c82 311 }
icraggs 8:c46930bd6c82 312
icraggs 8:c46930bd6c82 313
icraggs 9:01b8cc7d94cc 314 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::connect(MQTTPacket_connectData* options, resultHandler resultHandler)
Ian Craggs 12:cc7f2d62a393 315 {
Ian Craggs 12:cc7f2d62a393 316 command_timer.start();
icraggs 8:c46930bd6c82 317
Ian Craggs 12:cc7f2d62a393 318 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
icraggs 8:c46930bd6c82 319 if (options == 0)
Ian Craggs 12:cc7f2d62a393 320 options = &default_options; // set default options if none were supplied
icraggs 8:c46930bd6c82 321
Ian Craggs 12:cc7f2d62a393 322 this->keepAliveInterval = options->keepAliveInterval;
Ian Craggs 12:cc7f2d62a393 323 ping_timer.start();
Ian Craggs 12:cc7f2d62a393 324 int len = MQTTSerialize_connect(buf, buflen, options);
Ian Craggs 12:cc7f2d62a393 325 int rc = sendPacket(len); // send the connect packet
Ian Craggs 12:cc7f2d62a393 326 if (rc != len)
Ian Craggs 12:cc7f2d62a393 327 goto exit; // there was a problem
icraggs 8:c46930bd6c82 328
Ian Craggs 12:cc7f2d62a393 329 if (resultHandler == 0) // wait until the connack is received
Ian Craggs 12:cc7f2d62a393 330 {
Ian Craggs 12:cc7f2d62a393 331 if (command_timer.read_ms() > (command_timeout * 1000))
Ian Craggs 12:cc7f2d62a393 332 goto exit; // we timed out
icraggs 8:c46930bd6c82 333 // this will be a blocking call, wait for the connack
Ian Craggs 12:cc7f2d62a393 334 if (cycle(command_timeout - command_timer.read_ms()) == CONNACK)
icraggs 8:c46930bd6c82 335 {
icraggs 8:c46930bd6c82 336 int connack_rc = -1;
icraggs 8:c46930bd6c82 337 if (MQTTDeserialize_connack(&connack_rc, readbuf, readbuflen) == 1)
icraggs 8:c46930bd6c82 338 rc = connack_rc;
icraggs 8:c46930bd6c82 339 }
icraggs 8:c46930bd6c82 340 }
icraggs 8:c46930bd6c82 341 else
icraggs 8:c46930bd6c82 342 {
icraggs 8:c46930bd6c82 343 // set connect response callback function
icraggs 9:01b8cc7d94cc 344 connectHandler.attach(resultHandler);
icraggs 8:c46930bd6c82 345
Ian Craggs 12:cc7f2d62a393 346 // start background thread
Ian Craggs 12:cc7f2d62a393 347 this->thread = new Thread((void (*)(void const *argument))&MQTT::Client<Network, Timer, Thread>::threadfn, (void*)this);
icraggs 8:c46930bd6c82 348 }
Ian Craggs 12:cc7f2d62a393 349
Ian Craggs 12:cc7f2d62a393 350 exit:
Ian Craggs 12:cc7f2d62a393 351 command_timer.stop();
Ian Craggs 12:cc7f2d62a393 352 command_timer.reset();
icraggs 8:c46930bd6c82 353 return rc;
icraggs 8:c46930bd6c82 354 }
icraggs 8:c46930bd6c82 355
icraggs 8:c46930bd6c82 356
Ian Craggs 12:cc7f2d62a393 357 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler, resultHandler resultHandler)
Ian Craggs 12:cc7f2d62a393 358 {
Ian Craggs 12:cc7f2d62a393 359 command_timer.start();
Ian Craggs 12:cc7f2d62a393 360
icraggs 8:c46930bd6c82 361 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 8:c46930bd6c82 362
Ian Craggs 12:cc7f2d62a393 363 int len = MQTTSerialize_subscribe(buf, buflen, 0, packetid.getNext(), 1, &topic, (int*)&qos);
Ian Craggs 12:cc7f2d62a393 364 int rc = sendPacket(len); // send the subscribe packet
Ian Craggs 12:cc7f2d62a393 365 if (rc != len)
Ian Craggs 12:cc7f2d62a393 366 goto exit; // there was a problem
icraggs 8:c46930bd6c82 367
icraggs 8:c46930bd6c82 368 /* wait for suback */
icraggs 8:c46930bd6c82 369 if (resultHandler == 0)
icraggs 8:c46930bd6c82 370 {
icraggs 8:c46930bd6c82 371 // this will block
Ian Craggs 12:cc7f2d62a393 372 if (cycle(command_timeout - command_timer.read_ms()) == SUBACK)
icraggs 8:c46930bd6c82 373 {
icraggs 9:01b8cc7d94cc 374 int count = 0, grantedQoS = -1, mypacketid;
icraggs 9:01b8cc7d94cc 375 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, readbuflen) == 1)
icraggs 8:c46930bd6c82 376 rc = grantedQoS; // 0, 1, 2 or 0x80
icraggs 8:c46930bd6c82 377 }
icraggs 8:c46930bd6c82 378 }
icraggs 8:c46930bd6c82 379 else
icraggs 8:c46930bd6c82 380 {
icraggs 8:c46930bd6c82 381 // set subscribe response callback function
icraggs 8:c46930bd6c82 382
icraggs 8:c46930bd6c82 383 }
Ian Craggs 12:cc7f2d62a393 384
Ian Craggs 12:cc7f2d62a393 385 exit:
Ian Craggs 12:cc7f2d62a393 386 command_timer.stop();
Ian Craggs 12:cc7f2d62a393 387 command_timer.reset();
Ian Craggs 12:cc7f2d62a393 388 return rc;
Ian Craggs 12:cc7f2d62a393 389 }
Ian Craggs 12:cc7f2d62a393 390
Ian Craggs 12:cc7f2d62a393 391
Ian Craggs 12:cc7f2d62a393 392 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::unsubscribe(const char* topicFilter, resultHandler resultHandler)
Ian Craggs 12:cc7f2d62a393 393 {
Ian Craggs 12:cc7f2d62a393 394 command_timer.start();
Ian Craggs 12:cc7f2d62a393 395
Ian Craggs 12:cc7f2d62a393 396 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 8:c46930bd6c82 397
Ian Craggs 12:cc7f2d62a393 398 int len = MQTTSerialize_unsubscribe(buf, buflen, 0, packetid.getNext(), 1, &topic);
Ian Craggs 12:cc7f2d62a393 399 int rc = sendPacket(len); // send the subscribe packet
Ian Craggs 12:cc7f2d62a393 400 if (rc != len)
Ian Craggs 12:cc7f2d62a393 401 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 402
Ian Craggs 12:cc7f2d62a393 403 /* wait for suback */
Ian Craggs 12:cc7f2d62a393 404 if (resultHandler == 0)
Ian Craggs 12:cc7f2d62a393 405 {
Ian Craggs 12:cc7f2d62a393 406 // this will block
Ian Craggs 12:cc7f2d62a393 407 if (cycle(command_timeout - command_timer.read_ms()) == UNSUBACK)
Ian Craggs 12:cc7f2d62a393 408 {
Ian Craggs 12:cc7f2d62a393 409 int mypacketid;
Ian Craggs 12:cc7f2d62a393 410 if (MQTTDeserialize_unsuback(&mypacketid, readbuf, readbuflen) == 1)
Ian Craggs 12:cc7f2d62a393 411 rc = 0;
Ian Craggs 12:cc7f2d62a393 412 }
Ian Craggs 12:cc7f2d62a393 413 }
Ian Craggs 12:cc7f2d62a393 414 else
Ian Craggs 12:cc7f2d62a393 415 {
Ian Craggs 12:cc7f2d62a393 416 // set unsubscribe response callback function
Ian Craggs 12:cc7f2d62a393 417
Ian Craggs 12:cc7f2d62a393 418 }
Ian Craggs 12:cc7f2d62a393 419
Ian Craggs 12:cc7f2d62a393 420 exit:
Ian Craggs 12:cc7f2d62a393 421 command_timer.stop();
Ian Craggs 12:cc7f2d62a393 422 command_timer.reset();
Ian Craggs 12:cc7f2d62a393 423 return rc;
Ian Craggs 12:cc7f2d62a393 424 }
Ian Craggs 12:cc7f2d62a393 425
Ian Craggs 12:cc7f2d62a393 426
Ian Craggs 12:cc7f2d62a393 427
Ian Craggs 12:cc7f2d62a393 428 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::publish(const char* topicName, Message* message, resultHandler resultHandler)
Ian Craggs 12:cc7f2d62a393 429 {
Ian Craggs 12:cc7f2d62a393 430 command_timer.start();
Ian Craggs 12:cc7f2d62a393 431
Ian Craggs 12:cc7f2d62a393 432 MQTTString topic = {(char*)topicName, 0, 0};
Ian Craggs 12:cc7f2d62a393 433
Ian Craggs 12:cc7f2d62a393 434 message->id = packetid.getNext();
Ian Craggs 12:cc7f2d62a393 435
Ian Craggs 12:cc7f2d62a393 436 int len = MQTTSerialize_publish(buf, buflen, 0, message->qos, message->retained, message->id, topic, message->payload, message->payloadlen);
Ian Craggs 12:cc7f2d62a393 437 int rc = sendPacket(len); // send the subscribe packet
Ian Craggs 12:cc7f2d62a393 438 if (rc != len)
Ian Craggs 12:cc7f2d62a393 439 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 440
Ian Craggs 12:cc7f2d62a393 441 /* wait for acks */
Ian Craggs 12:cc7f2d62a393 442 if (resultHandler == 0)
Ian Craggs 12:cc7f2d62a393 443 {
Ian Craggs 12:cc7f2d62a393 444 if (message->qos == QOS1)
Ian Craggs 12:cc7f2d62a393 445 {
Ian Craggs 12:cc7f2d62a393 446 if (cycle(command_timeout - command_timer.read_ms()) == PUBACK)
Ian Craggs 12:cc7f2d62a393 447 {
Ian Craggs 12:cc7f2d62a393 448 int type, dup, mypacketid;
Ian Craggs 12:cc7f2d62a393 449 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, readbuflen) == 1)
Ian Craggs 12:cc7f2d62a393 450 rc = 0;
Ian Craggs 12:cc7f2d62a393 451 }
Ian Craggs 12:cc7f2d62a393 452 }
Ian Craggs 12:cc7f2d62a393 453 else if (message->qos == QOS2)
Ian Craggs 12:cc7f2d62a393 454 {
Ian Craggs 12:cc7f2d62a393 455 if (cycle(command_timeout - command_timer.read_ms()) == PUBREC)
Ian Craggs 12:cc7f2d62a393 456 {
Ian Craggs 12:cc7f2d62a393 457 int type, dup, mypacketid;
Ian Craggs 12:cc7f2d62a393 458 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, readbuflen) == 1)
Ian Craggs 12:cc7f2d62a393 459 rc = 0;
Ian Craggs 12:cc7f2d62a393 460 len = MQTTSerialize_ack(buf, buflen, PUBREL, 0, message->id);
Ian Craggs 12:cc7f2d62a393 461 rc = sendPacket(len); // send the subscribe packet
Ian Craggs 12:cc7f2d62a393 462 if (rc != len)
Ian Craggs 12:cc7f2d62a393 463 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 464 if (cycle(command_timeout - command_timer.read_ms()) == PUBCOMP)
Ian Craggs 12:cc7f2d62a393 465 {
Ian Craggs 12:cc7f2d62a393 466 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, readbuflen) == 1)
Ian Craggs 12:cc7f2d62a393 467 rc = 0;
Ian Craggs 12:cc7f2d62a393 468 }
Ian Craggs 12:cc7f2d62a393 469 }
Ian Craggs 12:cc7f2d62a393 470 }
Ian Craggs 12:cc7f2d62a393 471 }
Ian Craggs 12:cc7f2d62a393 472 else
Ian Craggs 12:cc7f2d62a393 473 {
Ian Craggs 12:cc7f2d62a393 474 // set publish response callback function
Ian Craggs 12:cc7f2d62a393 475
Ian Craggs 12:cc7f2d62a393 476 }
Ian Craggs 12:cc7f2d62a393 477
Ian Craggs 12:cc7f2d62a393 478 exit:
Ian Craggs 12:cc7f2d62a393 479 command_timer.stop();
Ian Craggs 12:cc7f2d62a393 480 command_timer.reset();
icraggs 8:c46930bd6c82 481 return rc;
icraggs 8:c46930bd6c82 482 }
icraggs 8:c46930bd6c82 483
icraggs 8:c46930bd6c82 484
sam_grove 0:fe461e4d7afe 485 #endif