An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   Cellular_HelloMQTT IoTStarterKit GSwifiInterface_HelloMQTT IBMIoTClientEthernetExample ... more

This library is part of the EclipseTM Paho project; specifically the embedded client.

The goals of this API are:

  1. to be independent of any system library: hence templates parameters for networking, timer and threading classes
  2. not to rely on heap storage, only automatic (I think this is a good thing)
  3. to limit memory use, for instance by defining the size of the buffers and arrays used at object creation time
Committer:
icraggs
Date:
Thu Aug 21 12:40:43 2014 +0000
Revision:
42:f5beda831651
Parent:
41:b7e86fa6dbb8
Child:
43:21da1f744243
latest updates

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 *******************************************************************************/
icraggs 21:e918525e529d 16
icraggs 21:e918525e529d 17 /*
icraggs 21:e918525e529d 18
icraggs 21:e918525e529d 19 TODO:
icraggs 21:e918525e529d 20
icraggs 26:2658bb87c53d 21 ensure publish packets are retried on reconnect
icraggs 23:05fc7de97d4a 22
icraggs 21:e918525e529d 23 */
sam_grove 0:fe461e4d7afe 24
icraggs 2:dcfdd2abfe71 25 #if !defined(MQTTCLIENT_H)
icraggs 2:dcfdd2abfe71 26 #define MQTTCLIENT_H
icraggs 2:dcfdd2abfe71 27
icraggs 34:e18a166198df 28 #include "FP.h"
icraggs 3:dbff6b768d28 29 #include "MQTTPacket.h"
icraggs 6:4d312a49200b 30 #include "stdio.h"
icraggs 41:b7e86fa6dbb8 31 #include "MQTT_logging.h"
icraggs 2:dcfdd2abfe71 32
icraggs 3:dbff6b768d28 33 namespace MQTT
icraggs 3:dbff6b768d28 34 {
icraggs 3:dbff6b768d28 35
icraggs 2:dcfdd2abfe71 36
icraggs 2:dcfdd2abfe71 37 enum QoS { QOS0, QOS1, QOS2 };
sam_grove 0:fe461e4d7afe 38
icraggs 31:a51dd239b78e 39 // all failure return codes must be negative
icraggs 23:05fc7de97d4a 40 enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
icraggs 23:05fc7de97d4a 41
icraggs 2:dcfdd2abfe71 42
icraggs 3:dbff6b768d28 43 struct Message
icraggs 2:dcfdd2abfe71 44 {
icraggs 2:dcfdd2abfe71 45 enum QoS qos;
icraggs 2:dcfdd2abfe71 46 bool retained;
icraggs 2:dcfdd2abfe71 47 bool dup;
Ian Craggs 12:cc7f2d62a393 48 unsigned short id;
icraggs 2:dcfdd2abfe71 49 void *payload;
icraggs 2:dcfdd2abfe71 50 size_t payloadlen;
sam_grove 0:fe461e4d7afe 51 };
sam_grove 0:fe461e4d7afe 52
icraggs 4:4ef00243708e 53
icraggs 20:cad3d54d7ecf 54 struct MessageData
icraggs 20:cad3d54d7ecf 55 {
icraggs 31:a51dd239b78e 56 MessageData(MQTTString &aTopicName, struct Message &aMessage) : message(aMessage), topicName(aTopicName)
icraggs 37:e3d64f9b986c 57 { }
icraggs 31:a51dd239b78e 58
icraggs 31:a51dd239b78e 59 struct Message &message;
icraggs 31:a51dd239b78e 60 MQTTString &topicName;
icraggs 20:cad3d54d7ecf 61 };
icraggs 20:cad3d54d7ecf 62
icraggs 20:cad3d54d7ecf 63
icraggs 9:01b8cc7d94cc 64 class PacketId
icraggs 9:01b8cc7d94cc 65 {
icraggs 9:01b8cc7d94cc 66 public:
icraggs 23:05fc7de97d4a 67 PacketId()
icraggs 23:05fc7de97d4a 68 {
icraggs 23:05fc7de97d4a 69 next = 0;
icraggs 23:05fc7de97d4a 70 }
icraggs 9:01b8cc7d94cc 71
icraggs 23:05fc7de97d4a 72 int getNext()
icraggs 23:05fc7de97d4a 73 {
icraggs 23:05fc7de97d4a 74 return next = (next == MAX_PACKET_ID) ? 1 : ++next;
icraggs 23:05fc7de97d4a 75 }
Ian Craggs 12:cc7f2d62a393 76
icraggs 9:01b8cc7d94cc 77 private:
icraggs 9:01b8cc7d94cc 78 static const int MAX_PACKET_ID = 65535;
icraggs 9:01b8cc7d94cc 79 int next;
icraggs 9:01b8cc7d94cc 80 };
icraggs 31:a51dd239b78e 81
icraggs 31:a51dd239b78e 82
icraggs 31:a51dd239b78e 83 class QoS2
icraggs 31:a51dd239b78e 84 {
icraggs 31:a51dd239b78e 85 public:
icraggs 31:a51dd239b78e 86
icraggs 31:a51dd239b78e 87
icraggs 31:a51dd239b78e 88 private:
icraggs 31:a51dd239b78e 89
icraggs 31:a51dd239b78e 90
icraggs 31:a51dd239b78e 91 };
icraggs 2:dcfdd2abfe71 92
icraggs 16:91c2f9a144d4 93
icraggs 21:e918525e529d 94 /**
icraggs 21:e918525e529d 95 * @class Client
icraggs 22:aadb79d29330 96 * @brief blocking, non-threaded MQTT client API
icraggs 23:05fc7de97d4a 97 *
icraggs 23:05fc7de97d4a 98 * This version of the API blocks on all method calls, until they are complete. This means that only one
icraggs 23:05fc7de97d4a 99 * MQTT request can be in process at any one time.
icraggs 21:e918525e529d 100 * @param Network a network class which supports send, receive
icraggs 21:e918525e529d 101 * @param Timer a timer class with the methods:
icraggs 21:e918525e529d 102 */
icraggs 31:a51dd239b78e 103 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5>
icraggs 31:a51dd239b78e 104 class Client
icraggs 2:dcfdd2abfe71 105 {
icraggs 2:dcfdd2abfe71 106
icraggs 22:aadb79d29330 107 public:
icraggs 26:2658bb87c53d 108
icraggs 31:a51dd239b78e 109 typedef void (*messageHandler)(MessageData&);
icraggs 23:05fc7de97d4a 110
icraggs 23:05fc7de97d4a 111 /** Construct the client
icraggs 23:05fc7de97d4a 112 * @param network - pointer to an instance of the Network class - must be connected to the endpoint
icraggs 23:05fc7de97d4a 113 * before calling MQTT connect
icraggs 23:05fc7de97d4a 114 * @param limits an instance of the Limit class - to alter limits as required
icraggs 23:05fc7de97d4a 115 */
icraggs 23:05fc7de97d4a 116 Client(Network& network, unsigned int command_timeout_ms = 30000);
icraggs 20:cad3d54d7ecf 117
icraggs 20:cad3d54d7ecf 118 /** Set the default message handling callback - used for any message which does not match a subscription message handler
icraggs 20:cad3d54d7ecf 119 * @param mh - pointer to the callback function
icraggs 20:cad3d54d7ecf 120 */
icraggs 20:cad3d54d7ecf 121 void setDefaultMessageHandler(messageHandler mh)
icraggs 20:cad3d54d7ecf 122 {
icraggs 20:cad3d54d7ecf 123 defaultMessageHandler.attach(mh);
icraggs 20:cad3d54d7ecf 124 }
icraggs 2:dcfdd2abfe71 125
icraggs 42:f5beda831651 126 void setLogHandler()
icraggs 42:f5beda831651 127 {
icraggs 42:f5beda831651 128 logHandler.attach(lh);
icraggs 42:f5beda831651 129 }
icraggs 42:f5beda831651 130
icraggs 42:f5beda831651 131
icraggs 20:cad3d54d7ecf 132 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
icraggs 20:cad3d54d7ecf 133 * The nework object must be connected to the network endpoint before calling this
icraggs 20:cad3d54d7ecf 134 * @param options - connect options
icraggs 20:cad3d54d7ecf 135 * @return success code -
icraggs 20:cad3d54d7ecf 136 */
icraggs 20:cad3d54d7ecf 137 int connect(MQTTPacket_connectData* options = 0);
icraggs 20:cad3d54d7ecf 138
icraggs 20:cad3d54d7ecf 139 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
icraggs 20:cad3d54d7ecf 140 * @param topic - the topic to publish to
icraggs 20:cad3d54d7ecf 141 * @param message - the message to send
icraggs 20:cad3d54d7ecf 142 * @return success code -
icraggs 20:cad3d54d7ecf 143 */
icraggs 20:cad3d54d7ecf 144 int publish(const char* topicName, Message* message);
icraggs 20:cad3d54d7ecf 145
icraggs 20:cad3d54d7ecf 146 /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
icraggs 20:cad3d54d7ecf 147 * @param topicFilter - a topic pattern which can include wildcards
icraggs 20:cad3d54d7ecf 148 * @param qos - the MQTT QoS to subscribe at
icraggs 20:cad3d54d7ecf 149 * @param mh - the callback function to be invoked when a message is received for this subscription
icraggs 20:cad3d54d7ecf 150 * @return success code -
icraggs 20:cad3d54d7ecf 151 */
icraggs 20:cad3d54d7ecf 152 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh);
icraggs 9:01b8cc7d94cc 153
icraggs 20:cad3d54d7ecf 154 /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
icraggs 20:cad3d54d7ecf 155 * @param topicFilter - a topic pattern which can include wildcards
icraggs 20:cad3d54d7ecf 156 * @return success code -
icraggs 20:cad3d54d7ecf 157 */
icraggs 20:cad3d54d7ecf 158 int unsubscribe(const char* topicFilter);
icraggs 20:cad3d54d7ecf 159
icraggs 31:a51dd239b78e 160 /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state
icraggs 20:cad3d54d7ecf 161 * @return success code -
icraggs 20:cad3d54d7ecf 162 */
icraggs 20:cad3d54d7ecf 163 int disconnect();
icraggs 8:c46930bd6c82 164
icraggs 20:cad3d54d7ecf 165 /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive
icraggs 20:cad3d54d7ecf 166 * yield can be called if no other MQTT operation is needed. This will also allow messages to be
icraggs 20:cad3d54d7ecf 167 * received.
icraggs 23:05fc7de97d4a 168 * @param timeout_ms the time to wait, in milliseconds
icraggs 26:2658bb87c53d 169 * @return success code - on failure, this means the client has disconnected
icraggs 20:cad3d54d7ecf 170 */
icraggs 26:2658bb87c53d 171 int yield(int timeout_ms = 1000);
icraggs 20:cad3d54d7ecf 172
icraggs 20:cad3d54d7ecf 173 private:
icraggs 20:cad3d54d7ecf 174
icraggs 20:cad3d54d7ecf 175 int cycle(Timer& timer);
icraggs 20:cad3d54d7ecf 176 int waitfor(int packet_type, Timer& timer);
icraggs 20:cad3d54d7ecf 177 int keepalive();
icraggs 2:dcfdd2abfe71 178
icraggs 3:dbff6b768d28 179 int decodePacket(int* value, int timeout);
icraggs 20:cad3d54d7ecf 180 int readPacket(Timer& timer);
icraggs 20:cad3d54d7ecf 181 int sendPacket(int length, Timer& timer);
icraggs 26:2658bb87c53d 182 int deliverMessage(MQTTString& topicName, Message& message);
icraggs 26:2658bb87c53d 183 bool isTopicMatched(char* topicFilter, MQTTString& topicName);
icraggs 3:dbff6b768d28 184
icraggs 23:05fc7de97d4a 185 Network& ipstack;
icraggs 23:05fc7de97d4a 186 unsigned int command_timeout_ms;
icraggs 16:91c2f9a144d4 187
icraggs 36:2f1ada427e56 188 unsigned char buf[MAX_MQTT_PACKET_SIZE];
icraggs 36:2f1ada427e56 189 unsigned char readbuf[MAX_MQTT_PACKET_SIZE];
icraggs 15:64a57183aa03 190
icraggs 20:cad3d54d7ecf 191 Timer ping_timer;
icraggs 15:64a57183aa03 192 unsigned int keepAliveInterval;
icraggs 20:cad3d54d7ecf 193 bool ping_outstanding;
icraggs 4:4ef00243708e 194
icraggs 9:01b8cc7d94cc 195 PacketId packetid;
icraggs 9:01b8cc7d94cc 196
icraggs 16:91c2f9a144d4 197 struct MessageHandlers
icraggs 15:64a57183aa03 198 {
icraggs 26:2658bb87c53d 199 const char* topicFilter;
icraggs 31:a51dd239b78e 200 FP<void, MessageData&> fp;
icraggs 23:05fc7de97d4a 201 } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic
icraggs 15:64a57183aa03 202
icraggs 31:a51dd239b78e 203 FP<void, MessageData&> defaultMessageHandler;
icraggs 28:8b2abe9bd814 204
icraggs 26:2658bb87c53d 205 bool isconnected;
icraggs 31:a51dd239b78e 206
icraggs 31:a51dd239b78e 207 #if 0
icraggs 31:a51dd239b78e 208 struct
icraggs 31:a51dd239b78e 209 {
icraggs 31:a51dd239b78e 210 bool used;
icraggs 31:a51dd239b78e 211 int id;
icraggs 31:a51dd239b78e 212 } QoS2messages[MAX_QOS2_MESSAGES];
icraggs 31:a51dd239b78e 213
icraggs 31:a51dd239b78e 214 #endif
icraggs 28:8b2abe9bd814 215
sam_grove 0:fe461e4d7afe 216 };
sam_grove 0:fe461e4d7afe 217
icraggs 15:64a57183aa03 218 }
icraggs 15:64a57183aa03 219
icraggs 15:64a57183aa03 220
icraggs 23:05fc7de97d4a 221 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 222 MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid()
icraggs 15:64a57183aa03 223 {
icraggs 23:05fc7de97d4a 224 ping_timer = Timer();
icraggs 23:05fc7de97d4a 225 ping_outstanding = 0;
icraggs 23:05fc7de97d4a 226 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 26:2658bb87c53d 227 messageHandlers[i].topicFilter = 0;
icraggs 26:2658bb87c53d 228 this->command_timeout_ms = command_timeout_ms;
icraggs 26:2658bb87c53d 229 isconnected = false;
icraggs 11:db15da110a37 230 }
icraggs 11:db15da110a37 231
icraggs 11:db15da110a37 232
icraggs 23:05fc7de97d4a 233 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 234 int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer)
icraggs 8:c46930bd6c82 235 {
icraggs 23:05fc7de97d4a 236 int rc = FAILURE,
icraggs 23:05fc7de97d4a 237 sent = 0;
icraggs 8:c46930bd6c82 238
icraggs 23:05fc7de97d4a 239 while (sent < length && !timer.expired())
icraggs 23:05fc7de97d4a 240 {
icraggs 23:05fc7de97d4a 241 rc = ipstack.write(&buf[sent], length, timer.left_ms());
icraggs 26:2658bb87c53d 242 if (rc < 0) // there was an error writing the data
icraggs 26:2658bb87c53d 243 break;
icraggs 26:2658bb87c53d 244 sent += rc;
icraggs 23:05fc7de97d4a 245 }
icraggs 20:cad3d54d7ecf 246 if (sent == length)
icraggs 23:05fc7de97d4a 247 {
icraggs 20:cad3d54d7ecf 248 ping_timer.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet
icraggs 23:05fc7de97d4a 249 rc = SUCCESS;
icraggs 42:f5beda831651 250 //if (debug)
icraggs 42:f5beda831651 251 // Log (packet)
icraggs 23:05fc7de97d4a 252 }
icraggs 23:05fc7de97d4a 253 else
icraggs 23:05fc7de97d4a 254 rc = FAILURE;
icraggs 23:05fc7de97d4a 255 return rc;
icraggs 8:c46930bd6c82 256 }
icraggs 8:c46930bd6c82 257
icraggs 8:c46930bd6c82 258
icraggs 26:2658bb87c53d 259 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 260 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
icraggs 8:c46930bd6c82 261 {
icraggs 36:2f1ada427e56 262 unsigned char c;
icraggs 8:c46930bd6c82 263 int multiplier = 1;
icraggs 8:c46930bd6c82 264 int len = 0;
icraggs 20:cad3d54d7ecf 265 const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
icraggs 8:c46930bd6c82 266
icraggs 8:c46930bd6c82 267 *value = 0;
icraggs 8:c46930bd6c82 268 do
icraggs 8:c46930bd6c82 269 {
icraggs 8:c46930bd6c82 270 int rc = MQTTPACKET_READ_ERROR;
icraggs 8:c46930bd6c82 271
icraggs 8:c46930bd6c82 272 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
icraggs 8:c46930bd6c82 273 {
icraggs 8:c46930bd6c82 274 rc = MQTTPACKET_READ_ERROR; /* bad data */
icraggs 8:c46930bd6c82 275 goto exit;
icraggs 8:c46930bd6c82 276 }
icraggs 23:05fc7de97d4a 277 rc = ipstack.read(&c, 1, timeout);
icraggs 8:c46930bd6c82 278 if (rc != 1)
icraggs 8:c46930bd6c82 279 goto exit;
icraggs 8:c46930bd6c82 280 *value += (c & 127) * multiplier;
icraggs 8:c46930bd6c82 281 multiplier *= 128;
icraggs 8:c46930bd6c82 282 } while ((c & 128) != 0);
icraggs 8:c46930bd6c82 283 exit:
icraggs 8:c46930bd6c82 284 return len;
icraggs 8:c46930bd6c82 285 }
icraggs 8:c46930bd6c82 286
icraggs 8:c46930bd6c82 287
icraggs 8:c46930bd6c82 288 /**
icraggs 8:c46930bd6c82 289 * If any read fails in this method, then we should disconnect from the network, as on reconnect
icraggs 8:c46930bd6c82 290 * the packets can be retried.
icraggs 8:c46930bd6c82 291 * @param timeout the max time to wait for the packet read to complete, in milliseconds
icraggs 8:c46930bd6c82 292 * @return the MQTT packet type, or -1 if none
icraggs 8:c46930bd6c82 293 */
icraggs 23:05fc7de97d4a 294 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 295 int MQTT::Client<Network, Timer, a, b>::readPacket(Timer& timer)
icraggs 8:c46930bd6c82 296 {
icraggs 26:2658bb87c53d 297 int rc = FAILURE;
icraggs 8:c46930bd6c82 298 MQTTHeader header = {0};
icraggs 8:c46930bd6c82 299 int len = 0;
icraggs 8:c46930bd6c82 300 int rem_len = 0;
icraggs 8:c46930bd6c82 301
icraggs 8:c46930bd6c82 302 /* 1. read the header byte. This has the packet type in it */
icraggs 23:05fc7de97d4a 303 if (ipstack.read(readbuf, 1, timer.left_ms()) != 1)
icraggs 8:c46930bd6c82 304 goto exit;
icraggs 8:c46930bd6c82 305
icraggs 8:c46930bd6c82 306 len = 1;
icraggs 8:c46930bd6c82 307 /* 2. read the remaining length. This is variable in itself */
icraggs 20:cad3d54d7ecf 308 decodePacket(&rem_len, timer.left_ms());
icraggs 8:c46930bd6c82 309 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
icraggs 8:c46930bd6c82 310
icraggs 8:c46930bd6c82 311 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
icraggs 23:05fc7de97d4a 312 if (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len)
icraggs 8:c46930bd6c82 313 goto exit;
icraggs 8:c46930bd6c82 314
icraggs 8:c46930bd6c82 315 header.byte = readbuf[0];
icraggs 8:c46930bd6c82 316 rc = header.bits.type;
icraggs 8:c46930bd6c82 317 exit:
icraggs 8:c46930bd6c82 318 return rc;
icraggs 3:dbff6b768d28 319 }
icraggs 3:dbff6b768d28 320
icraggs 8:c46930bd6c82 321
icraggs 26:2658bb87c53d 322 // assume topic filter and name is in correct format
icraggs 26:2658bb87c53d 323 // # can only be at end
icraggs 26:2658bb87c53d 324 // + and # can only be next to separator
icraggs 26:2658bb87c53d 325 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 326 bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName)
icraggs 26:2658bb87c53d 327 {
icraggs 26:2658bb87c53d 328 char* curf = topicFilter;
icraggs 26:2658bb87c53d 329 char* curn = topicName.lenstring.data;
icraggs 26:2658bb87c53d 330 char* curn_end = curn + topicName.lenstring.len;
icraggs 26:2658bb87c53d 331
icraggs 26:2658bb87c53d 332 while (*curf && curn < curn_end)
icraggs 26:2658bb87c53d 333 {
icraggs 26:2658bb87c53d 334 if (*curn == '/' && *curf != '/')
icraggs 26:2658bb87c53d 335 break;
icraggs 26:2658bb87c53d 336 if (*curf != '+' && *curf != '#' && *curf != *curn)
icraggs 26:2658bb87c53d 337 break;
icraggs 26:2658bb87c53d 338 if (*curf == '+')
icraggs 26:2658bb87c53d 339 { // skip until we meet the next separator, or end of string
icraggs 26:2658bb87c53d 340 char* nextpos = curn + 1;
icraggs 26:2658bb87c53d 341 while (nextpos < curn_end && *nextpos != '/')
icraggs 26:2658bb87c53d 342 nextpos = ++curn + 1;
icraggs 26:2658bb87c53d 343 }
icraggs 26:2658bb87c53d 344 else if (*curf == '#')
icraggs 26:2658bb87c53d 345 curn = curn_end - 1; // skip until end of string
icraggs 26:2658bb87c53d 346 curf++;
icraggs 26:2658bb87c53d 347 curn++;
icraggs 26:2658bb87c53d 348 };
icraggs 26:2658bb87c53d 349
icraggs 26:2658bb87c53d 350 return (curn == curn_end) && (*curf == '\0');
icraggs 26:2658bb87c53d 351 }
icraggs 26:2658bb87c53d 352
icraggs 26:2658bb87c53d 353
icraggs 26:2658bb87c53d 354
icraggs 23:05fc7de97d4a 355 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 26:2658bb87c53d 356 int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message)
icraggs 15:64a57183aa03 357 {
icraggs 26:2658bb87c53d 358 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 359
icraggs 20:cad3d54d7ecf 360 // we have to find the right message handler - indexed by topic
icraggs 23:05fc7de97d4a 361 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 20:cad3d54d7ecf 362 {
icraggs 26:2658bb87c53d 363 if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) ||
icraggs 26:2658bb87c53d 364 isTopicMatched((char*)messageHandlers[i].topicFilter, topicName)))
icraggs 20:cad3d54d7ecf 365 {
icraggs 26:2658bb87c53d 366 if (messageHandlers[i].fp.attached())
icraggs 26:2658bb87c53d 367 {
icraggs 31:a51dd239b78e 368 MessageData md(topicName, message);
icraggs 31:a51dd239b78e 369 messageHandlers[i].fp(md);
icraggs 26:2658bb87c53d 370 rc = SUCCESS;
icraggs 26:2658bb87c53d 371 }
icraggs 20:cad3d54d7ecf 372 }
icraggs 20:cad3d54d7ecf 373 }
icraggs 26:2658bb87c53d 374
icraggs 26:2658bb87c53d 375 if (rc == FAILURE && defaultMessageHandler.attached())
icraggs 26:2658bb87c53d 376 {
icraggs 31:a51dd239b78e 377 MessageData md(topicName, message);
icraggs 31:a51dd239b78e 378 defaultMessageHandler(md);
icraggs 26:2658bb87c53d 379 rc = SUCCESS;
icraggs 26:2658bb87c53d 380 }
icraggs 20:cad3d54d7ecf 381
icraggs 20:cad3d54d7ecf 382 return rc;
icraggs 15:64a57183aa03 383 }
icraggs 15:64a57183aa03 384
icraggs 15:64a57183aa03 385
icraggs 20:cad3d54d7ecf 386
icraggs 23:05fc7de97d4a 387 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 388 int MQTT::Client<Network, Timer, a, b>::yield(int timeout_ms)
icraggs 20:cad3d54d7ecf 389 {
icraggs 26:2658bb87c53d 390 int rc = SUCCESS;
icraggs 20:cad3d54d7ecf 391 Timer timer = Timer();
icraggs 20:cad3d54d7ecf 392
icraggs 23:05fc7de97d4a 393 timer.countdown_ms(timeout_ms);
icraggs 20:cad3d54d7ecf 394 while (!timer.expired())
icraggs 26:2658bb87c53d 395 {
icraggs 26:2658bb87c53d 396 if (cycle(timer) == FAILURE)
icraggs 26:2658bb87c53d 397 {
icraggs 26:2658bb87c53d 398 rc = FAILURE;
icraggs 26:2658bb87c53d 399 break;
icraggs 26:2658bb87c53d 400 }
icraggs 26:2658bb87c53d 401 }
icraggs 26:2658bb87c53d 402
icraggs 26:2658bb87c53d 403 return rc;
icraggs 20:cad3d54d7ecf 404 }
icraggs 20:cad3d54d7ecf 405
icraggs 20:cad3d54d7ecf 406
icraggs 23:05fc7de97d4a 407 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 408 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
icraggs 8:c46930bd6c82 409 {
icraggs 8:c46930bd6c82 410 /* get one piece of work off the wire and one pass through */
icraggs 20:cad3d54d7ecf 411
Ian Craggs 12:cc7f2d62a393 412 // read the socket, see what work is due
icraggs 36:2f1ada427e56 413 unsigned short packet_type = readPacket(timer);
icraggs 15:64a57183aa03 414
icraggs 26:2658bb87c53d 415 int len = 0,
icraggs 26:2658bb87c53d 416 rc = SUCCESS;
icraggs 28:8b2abe9bd814 417
icraggs 8:c46930bd6c82 418 switch (packet_type)
icraggs 8:c46930bd6c82 419 {
icraggs 15:64a57183aa03 420 case CONNACK:
icraggs 8:c46930bd6c82 421 case PUBACK:
icraggs 8:c46930bd6c82 422 case SUBACK:
icraggs 8:c46930bd6c82 423 break;
icraggs 15:64a57183aa03 424 case PUBLISH:
icraggs 20:cad3d54d7ecf 425 MQTTString topicName;
icraggs 20:cad3d54d7ecf 426 Message msg;
icraggs 36:2f1ada427e56 427 if (MQTTDeserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
icraggs 36:2f1ada427e56 428 (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 429 goto exit;
icraggs 31:a51dd239b78e 430 // if (msg.qos != QOS2)
icraggs 31:a51dd239b78e 431 deliverMessage(topicName, msg);
icraggs 31:a51dd239b78e 432 #if 0
icraggs 31:a51dd239b78e 433 else if (isQoS2msgidFree(msg.id))
icraggs 31:a51dd239b78e 434 {
icraggs 31:a51dd239b78e 435 UseQoS2msgid(msg.id);
icraggs 31:a51dd239b78e 436 deliverMessage(topicName, msg);
icraggs 31:a51dd239b78e 437 }
icraggs 31:a51dd239b78e 438 #endif
icraggs 20:cad3d54d7ecf 439 if (msg.qos != QOS0)
icraggs 20:cad3d54d7ecf 440 {
icraggs 20:cad3d54d7ecf 441 if (msg.qos == QOS1)
icraggs 23:05fc7de97d4a 442 len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id);
icraggs 20:cad3d54d7ecf 443 else if (msg.qos == QOS2)
icraggs 23:05fc7de97d4a 444 len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id);
icraggs 26:2658bb87c53d 445 if (len <= 0)
icraggs 26:2658bb87c53d 446 rc = FAILURE;
icraggs 26:2658bb87c53d 447 else
icraggs 26:2658bb87c53d 448 rc = sendPacket(len, timer);
icraggs 26:2658bb87c53d 449 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 450 goto exit; // there was a problem
icraggs 20:cad3d54d7ecf 451 }
Ian Craggs 12:cc7f2d62a393 452 break;
icraggs 15:64a57183aa03 453 case PUBREC:
icraggs 36:2f1ada427e56 454 unsigned short mypacketid;
icraggs 36:2f1ada427e56 455 unsigned char dup, type;
icraggs 26:2658bb87c53d 456 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 457 rc = FAILURE;
icraggs 26:2658bb87c53d 458 else if ((len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, mypacketid)) <= 0)
icraggs 26:2658bb87c53d 459 rc = FAILURE;
icraggs 26:2658bb87c53d 460 else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet
icraggs 26:2658bb87c53d 461 rc = FAILURE; // there was a problem
icraggs 26:2658bb87c53d 462 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 463 goto exit; // there was a problem
icraggs 8:c46930bd6c82 464 break;
icraggs 8:c46930bd6c82 465 case PUBCOMP:
icraggs 8:c46930bd6c82 466 break;
icraggs 15:64a57183aa03 467 case PINGRESP:
icraggs 20:cad3d54d7ecf 468 ping_outstanding = false;
icraggs 8:c46930bd6c82 469 break;
icraggs 15:64a57183aa03 470 }
icraggs 20:cad3d54d7ecf 471 keepalive();
Ian Craggs 12:cc7f2d62a393 472 exit:
icraggs 26:2658bb87c53d 473 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 474 rc = packet_type;
icraggs 26:2658bb87c53d 475 return rc;
icraggs 15:64a57183aa03 476 }
icraggs 15:64a57183aa03 477
icraggs 15:64a57183aa03 478
icraggs 23:05fc7de97d4a 479 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 480 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive()
icraggs 15:64a57183aa03 481 {
icraggs 26:2658bb87c53d 482 int rc = FAILURE;
icraggs 15:64a57183aa03 483
icraggs 20:cad3d54d7ecf 484 if (keepAliveInterval == 0)
icraggs 26:2658bb87c53d 485 {
icraggs 26:2658bb87c53d 486 rc = SUCCESS;
icraggs 20:cad3d54d7ecf 487 goto exit;
icraggs 26:2658bb87c53d 488 }
icraggs 15:64a57183aa03 489
icraggs 20:cad3d54d7ecf 490 if (ping_timer.expired())
icraggs 20:cad3d54d7ecf 491 {
icraggs 26:2658bb87c53d 492 if (!ping_outstanding)
icraggs 20:cad3d54d7ecf 493 {
icraggs 20:cad3d54d7ecf 494 Timer timer = Timer(1000);
icraggs 23:05fc7de97d4a 495 int len = MQTTSerialize_pingreq(buf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 496 if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet
icraggs 20:cad3d54d7ecf 497 ping_outstanding = true;
icraggs 20:cad3d54d7ecf 498 }
icraggs 20:cad3d54d7ecf 499 }
icraggs 15:64a57183aa03 500
icraggs 15:64a57183aa03 501 exit:
icraggs 20:cad3d54d7ecf 502 return rc;
icraggs 8:c46930bd6c82 503 }
icraggs 8:c46930bd6c82 504
icraggs 8:c46930bd6c82 505
icraggs 16:91c2f9a144d4 506 // only used in single-threaded mode where one command at a time is in process
icraggs 23:05fc7de97d4a 507 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 508 int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer)
icraggs 15:64a57183aa03 509 {
icraggs 26:2658bb87c53d 510 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 511
icraggs 20:cad3d54d7ecf 512 do
icraggs 16:91c2f9a144d4 513 {
icraggs 20:cad3d54d7ecf 514 if (timer.expired())
icraggs 20:cad3d54d7ecf 515 break; // we timed out
icraggs 20:cad3d54d7ecf 516 }
icraggs 20:cad3d54d7ecf 517 while ((rc = cycle(timer)) != packet_type);
icraggs 20:cad3d54d7ecf 518
icraggs 20:cad3d54d7ecf 519 return rc;
icraggs 16:91c2f9a144d4 520 }
icraggs 16:91c2f9a144d4 521
icraggs 16:91c2f9a144d4 522
icraggs 23:05fc7de97d4a 523 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 524 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData* options)
icraggs 16:91c2f9a144d4 525 {
icraggs 23:05fc7de97d4a 526 Timer connect_timer = Timer(command_timeout_ms);
icraggs 26:2658bb87c53d 527 int rc = FAILURE;
icraggs 31:a51dd239b78e 528 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
icraggs 31:a51dd239b78e 529 int len = 0;
icraggs 31:a51dd239b78e 530
icraggs 31:a51dd239b78e 531 if (isconnected) // don't send connect packet again if we are already connected
icraggs 31:a51dd239b78e 532 goto exit;
icraggs 8:c46930bd6c82 533
icraggs 8:c46930bd6c82 534 if (options == 0)
Ian Craggs 12:cc7f2d62a393 535 options = &default_options; // set default options if none were supplied
icraggs 8:c46930bd6c82 536
icraggs 15:64a57183aa03 537 this->keepAliveInterval = options->keepAliveInterval;
icraggs 20:cad3d54d7ecf 538 ping_timer.countdown(this->keepAliveInterval);
icraggs 31:a51dd239b78e 539 if ((len = MQTTSerialize_connect(buf, MAX_MQTT_PACKET_SIZE, options)) <= 0)
icraggs 26:2658bb87c53d 540 goto exit;
icraggs 26:2658bb87c53d 541 if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet
icraggs 20:cad3d54d7ecf 542 goto exit; // there was a problem
icraggs 8:c46930bd6c82 543
icraggs 20:cad3d54d7ecf 544 // this will be a blocking call, wait for the connack
icraggs 20:cad3d54d7ecf 545 if (waitfor(CONNACK, connect_timer) == CONNACK)
icraggs 15:64a57183aa03 546 {
icraggs 36:2f1ada427e56 547 unsigned char connack_rc = 255;
icraggs 37:e3d64f9b986c 548 bool sessionPresent = false;
icraggs 37:e3d64f9b986c 549 if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 550 rc = connack_rc;
icraggs 26:2658bb87c53d 551 else
icraggs 26:2658bb87c53d 552 rc = FAILURE;
icraggs 8:c46930bd6c82 553 }
icraggs 26:2658bb87c53d 554 else
icraggs 26:2658bb87c53d 555 rc = FAILURE;
icraggs 15:64a57183aa03 556
icraggs 15:64a57183aa03 557 exit:
icraggs 26:2658bb87c53d 558 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 559 isconnected = true;
icraggs 8:c46930bd6c82 560 return rc;
icraggs 8:c46930bd6c82 561 }
icraggs 8:c46930bd6c82 562
icraggs 8:c46930bd6c82 563
icraggs 23:05fc7de97d4a 564 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 565 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler)
icraggs 20:cad3d54d7ecf 566 {
icraggs 31:a51dd239b78e 567 int rc = FAILURE;
icraggs 23:05fc7de97d4a 568 Timer timer = Timer(command_timeout_ms);
icraggs 26:2658bb87c53d 569 int len = 0;
icraggs 31:a51dd239b78e 570 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 20:cad3d54d7ecf 571
icraggs 26:2658bb87c53d 572 if (!isconnected)
icraggs 20:cad3d54d7ecf 573 goto exit;
icraggs 26:2658bb87c53d 574
icraggs 26:2658bb87c53d 575 len = MQTTSerialize_subscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos);
icraggs 26:2658bb87c53d 576 if (len <= 0)
icraggs 26:2658bb87c53d 577 goto exit;
icraggs 23:05fc7de97d4a 578 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 26:2658bb87c53d 579 goto exit; // there was a problem
icraggs 8:c46930bd6c82 580
icraggs 20:cad3d54d7ecf 581 if (waitfor(SUBACK, timer) == SUBACK) // wait for suback
icraggs 8:c46930bd6c82 582 {
icraggs 36:2f1ada427e56 583 int count = 0, grantedQoS = -1;
icraggs 36:2f1ada427e56 584 unsigned short mypacketid;
icraggs 23:05fc7de97d4a 585 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 586 rc = grantedQoS; // 0, 1, 2 or 0x80
icraggs 20:cad3d54d7ecf 587 if (rc != 0x80)
icraggs 8:c46930bd6c82 588 {
icraggs 23:05fc7de97d4a 589 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 16:91c2f9a144d4 590 {
icraggs 26:2658bb87c53d 591 if (messageHandlers[i].topicFilter == 0)
icraggs 20:cad3d54d7ecf 592 {
icraggs 26:2658bb87c53d 593 messageHandlers[i].topicFilter = topicFilter;
icraggs 20:cad3d54d7ecf 594 messageHandlers[i].fp.attach(messageHandler);
icraggs 20:cad3d54d7ecf 595 rc = 0;
icraggs 20:cad3d54d7ecf 596 break;
icraggs 20:cad3d54d7ecf 597 }
icraggs 16:91c2f9a144d4 598 }
icraggs 8:c46930bd6c82 599 }
icraggs 8:c46930bd6c82 600 }
icraggs 26:2658bb87c53d 601 else
icraggs 26:2658bb87c53d 602 rc = FAILURE;
icraggs 26:2658bb87c53d 603
icraggs 15:64a57183aa03 604 exit:
Ian Craggs 12:cc7f2d62a393 605 return rc;
icraggs 15:64a57183aa03 606 }
icraggs 15:64a57183aa03 607
icraggs 15:64a57183aa03 608
icraggs 23:05fc7de97d4a 609 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 610 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter)
icraggs 20:cad3d54d7ecf 611 {
icraggs 26:2658bb87c53d 612 int rc = FAILURE;
icraggs 31:a51dd239b78e 613 Timer timer = Timer(command_timeout_ms);
Ian Craggs 12:cc7f2d62a393 614 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 31:a51dd239b78e 615 int len = 0;
icraggs 8:c46930bd6c82 616
icraggs 31:a51dd239b78e 617 if (!isconnected)
icraggs 31:a51dd239b78e 618 goto exit;
icraggs 31:a51dd239b78e 619
icraggs 31:a51dd239b78e 620 if ((len = MQTTSerialize_unsubscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0)
icraggs 20:cad3d54d7ecf 621 goto exit;
icraggs 23:05fc7de97d4a 622 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 20:cad3d54d7ecf 623 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 624
icraggs 20:cad3d54d7ecf 625 if (waitfor(UNSUBACK, timer) == UNSUBACK)
Ian Craggs 12:cc7f2d62a393 626 {
icraggs 36:2f1ada427e56 627 unsigned short mypacketid; // should be the same as the packetid above
icraggs 23:05fc7de97d4a 628 if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 629 rc = 0;
Ian Craggs 12:cc7f2d62a393 630 }
icraggs 26:2658bb87c53d 631 else
icraggs 26:2658bb87c53d 632 rc = FAILURE;
icraggs 15:64a57183aa03 633
icraggs 15:64a57183aa03 634 exit:
Ian Craggs 12:cc7f2d62a393 635 return rc;
icraggs 15:64a57183aa03 636 }
icraggs 15:64a57183aa03 637
icraggs 15:64a57183aa03 638
icraggs 15:64a57183aa03 639
icraggs 23:05fc7de97d4a 640 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 641 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message* message)
icraggs 20:cad3d54d7ecf 642 {
icraggs 26:2658bb87c53d 643 int rc = FAILURE;
icraggs 31:a51dd239b78e 644 Timer timer = Timer(command_timeout_ms);
icraggs 31:a51dd239b78e 645 MQTTString topicString = {(char*)topicName, 0, 0};
icraggs 31:a51dd239b78e 646 int len = 0;
icraggs 20:cad3d54d7ecf 647
icraggs 31:a51dd239b78e 648 if (!isconnected)
icraggs 31:a51dd239b78e 649 goto exit;
icraggs 15:64a57183aa03 650
icraggs 20:cad3d54d7ecf 651 if (message->qos == QOS1 || message->qos == QOS2)
icraggs 20:cad3d54d7ecf 652 message->id = packetid.getNext();
icraggs 15:64a57183aa03 653
icraggs 31:a51dd239b78e 654 len = MQTTSerialize_publish(buf, MAX_MQTT_PACKET_SIZE, 0, message->qos, message->retained, message->id,
icraggs 36:2f1ada427e56 655 topicString, (unsigned char*)message->payload, message->payloadlen);
icraggs 26:2658bb87c53d 656 if (len <= 0)
icraggs 26:2658bb87c53d 657 goto exit;
icraggs 26:2658bb87c53d 658 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 20:cad3d54d7ecf 659 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 660
icraggs 20:cad3d54d7ecf 661 if (message->qos == QOS1)
Ian Craggs 12:cc7f2d62a393 662 {
icraggs 20:cad3d54d7ecf 663 if (waitfor(PUBACK, timer) == PUBACK)
icraggs 20:cad3d54d7ecf 664 {
icraggs 36:2f1ada427e56 665 unsigned short mypacketid;
icraggs 36:2f1ada427e56 666 unsigned char dup, type;
icraggs 26:2658bb87c53d 667 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 668 rc = FAILURE;
icraggs 20:cad3d54d7ecf 669 }
icraggs 26:2658bb87c53d 670 else
icraggs 26:2658bb87c53d 671 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 672 }
icraggs 20:cad3d54d7ecf 673 else if (message->qos == QOS2)
Ian Craggs 12:cc7f2d62a393 674 {
icraggs 20:cad3d54d7ecf 675 if (waitfor(PUBCOMP, timer) == PUBCOMP)
icraggs 20:cad3d54d7ecf 676 {
icraggs 36:2f1ada427e56 677 unsigned short mypacketid;
icraggs 36:2f1ada427e56 678 unsigned char dup, type;
icraggs 26:2658bb87c53d 679 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 680 rc = FAILURE;
icraggs 20:cad3d54d7ecf 681 }
icraggs 26:2658bb87c53d 682 else
icraggs 26:2658bb87c53d 683 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 684 }
icraggs 15:64a57183aa03 685
icraggs 15:64a57183aa03 686 exit:
icraggs 8:c46930bd6c82 687 return rc;
icraggs 8:c46930bd6c82 688 }
icraggs 8:c46930bd6c82 689
icraggs 8:c46930bd6c82 690
icraggs 23:05fc7de97d4a 691 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 692 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
icraggs 20:cad3d54d7ecf 693 {
icraggs 26:2658bb87c53d 694 int rc = FAILURE;
icraggs 23:05fc7de97d4a 695 Timer timer = Timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete
icraggs 23:05fc7de97d4a 696 int len = MQTTSerialize_disconnect(buf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 697 if (len > 0)
icraggs 26:2658bb87c53d 698 rc = sendPacket(len, timer); // send the disconnect packet
icraggs 31:a51dd239b78e 699
icraggs 31:a51dd239b78e 700 isconnected = false;
icraggs 23:05fc7de97d4a 701 return rc;
icraggs 20:cad3d54d7ecf 702 }
icraggs 20:cad3d54d7ecf 703
icraggs 20:cad3d54d7ecf 704
icraggs 20:cad3d54d7ecf 705 #endif