USE YHTANG AWS ACCOUNT TO TEST MQTT

Dependencies:   MQTTPacket FP

Dependents:   NuMaker-mbed-AWS-IoT-example

Committer:
icraggs
Date:
Fri Aug 01 13:13:35 2014 +0000
Revision:
35:063dc3b472d5
Parent:
33:8bbc3a992326
Child:
36:2f1ada427e56
Change boolean flag size in MQTTPacket to one byte (unsigned char)

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