Duy tran / MQTT_Test

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
DuyLionTran
Date:
Tue Nov 28 07:38:51 2017 +0000
Revision:
61:36e76acf2166
Parent:
56:30f7b745eaaa
Child:
62:0934f0842804
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 6:4d312a49200b 1 /*******************************************************************************
icraggs 46:e335fcc1a663 2 * Copyright (c) 2014, 2015 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 46:e335fcc1a663 15 * Ian Craggs - fix for bug 458512 - QoS 2 messages
icraggs 46:e335fcc1a663 16 * Ian Craggs - fix for bug 460389 - send loop uses wrong length
icraggs 46:e335fcc1a663 17 * Ian Craggs - fix for bug 464169 - clearing subscriptions
icraggs 46:e335fcc1a663 18 * Ian Craggs - fix for bug 464551 - enums and ints can be different size
icraggs 6:4d312a49200b 19 *******************************************************************************/
sam_grove 0:fe461e4d7afe 20
icraggs 2:dcfdd2abfe71 21 #if !defined(MQTTCLIENT_H)
icraggs 2:dcfdd2abfe71 22 #define MQTTCLIENT_H
icraggs 2:dcfdd2abfe71 23
icraggs 34:e18a166198df 24 #include "FP.h"
icraggs 3:dbff6b768d28 25 #include "MQTTPacket.h"
icraggs 6:4d312a49200b 26 #include "stdio.h"
icraggs 43:21da1f744243 27 #include "MQTTLogging.h"
icraggs 43:21da1f744243 28
icraggs 43:21da1f744243 29 #if !defined(MQTTCLIENT_QOS1)
icraggs 43:21da1f744243 30 #define MQTTCLIENT_QOS1 1
icraggs 43:21da1f744243 31 #endif
icraggs 43:21da1f744243 32 #if !defined(MQTTCLIENT_QOS2)
icraggs 43:21da1f744243 33 #define MQTTCLIENT_QOS2 0
icraggs 43:21da1f744243 34 #endif
icraggs 2:dcfdd2abfe71 35
icraggs 3:dbff6b768d28 36 namespace MQTT
icraggs 3:dbff6b768d28 37 {
icraggs 3:dbff6b768d28 38
icraggs 2:dcfdd2abfe71 39
icraggs 2:dcfdd2abfe71 40 enum QoS { QOS0, QOS1, QOS2 };
sam_grove 0:fe461e4d7afe 41
icraggs 31:a51dd239b78e 42 // all failure return codes must be negative
icraggs 23:05fc7de97d4a 43 enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
icraggs 23:05fc7de97d4a 44
icraggs 2:dcfdd2abfe71 45
icraggs 3:dbff6b768d28 46 struct Message
icraggs 2:dcfdd2abfe71 47 {
icraggs 2:dcfdd2abfe71 48 enum QoS qos;
icraggs 2:dcfdd2abfe71 49 bool retained;
icraggs 2:dcfdd2abfe71 50 bool dup;
Ian Craggs 12:cc7f2d62a393 51 unsigned short id;
icraggs 2:dcfdd2abfe71 52 void *payload;
icraggs 2:dcfdd2abfe71 53 size_t payloadlen;
sam_grove 0:fe461e4d7afe 54 };
sam_grove 0:fe461e4d7afe 55
icraggs 4:4ef00243708e 56
icraggs 20:cad3d54d7ecf 57 struct MessageData
icraggs 20:cad3d54d7ecf 58 {
icraggs 31:a51dd239b78e 59 MessageData(MQTTString &aTopicName, struct Message &aMessage) : message(aMessage), topicName(aTopicName)
icraggs 37:e3d64f9b986c 60 { }
icraggs 43:21da1f744243 61
icraggs 31:a51dd239b78e 62 struct Message &message;
icraggs 31:a51dd239b78e 63 MQTTString &topicName;
icraggs 20:cad3d54d7ecf 64 };
icraggs 20:cad3d54d7ecf 65
icraggs 20:cad3d54d7ecf 66
icraggs 9:01b8cc7d94cc 67 class PacketId
icraggs 9:01b8cc7d94cc 68 {
icraggs 9:01b8cc7d94cc 69 public:
icraggs 23:05fc7de97d4a 70 PacketId()
icraggs 23:05fc7de97d4a 71 {
icraggs 23:05fc7de97d4a 72 next = 0;
icraggs 23:05fc7de97d4a 73 }
icraggs 43:21da1f744243 74
icraggs 23:05fc7de97d4a 75 int getNext()
icraggs 23:05fc7de97d4a 76 {
icraggs 23:05fc7de97d4a 77 return next = (next == MAX_PACKET_ID) ? 1 : ++next;
icraggs 23:05fc7de97d4a 78 }
icraggs 43:21da1f744243 79
icraggs 9:01b8cc7d94cc 80 private:
icraggs 9:01b8cc7d94cc 81 static const int MAX_PACKET_ID = 65535;
icraggs 9:01b8cc7d94cc 82 int next;
icraggs 9:01b8cc7d94cc 83 };
icraggs 31:a51dd239b78e 84
icraggs 31:a51dd239b78e 85
icraggs 21:e918525e529d 86 /**
icraggs 21:e918525e529d 87 * @class Client
icraggs 22:aadb79d29330 88 * @brief blocking, non-threaded MQTT client API
icraggs 43:21da1f744243 89 *
icraggs 23:05fc7de97d4a 90 * This version of the API blocks on all method calls, until they are complete. This means that only one
icraggs 43:21da1f744243 91 * MQTT request can be in process at any one time.
icraggs 21:e918525e529d 92 * @param Network a network class which supports send, receive
icraggs 43:21da1f744243 93 * @param Timer a timer class with the methods:
icraggs 43:21da1f744243 94 */
icraggs 31:a51dd239b78e 95 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5>
icraggs 31:a51dd239b78e 96 class Client
icraggs 2:dcfdd2abfe71 97 {
icraggs 43:21da1f744243 98
icraggs 22:aadb79d29330 99 public:
icraggs 43:21da1f744243 100
icraggs 31:a51dd239b78e 101 typedef void (*messageHandler)(MessageData&);
icraggs 23:05fc7de97d4a 102
icraggs 23:05fc7de97d4a 103 /** Construct the client
icraggs 23:05fc7de97d4a 104 * @param network - pointer to an instance of the Network class - must be connected to the endpoint
icraggs 23:05fc7de97d4a 105 * before calling MQTT connect
icraggs 23:05fc7de97d4a 106 * @param limits an instance of the Limit class - to alter limits as required
icraggs 23:05fc7de97d4a 107 */
icraggs 43:21da1f744243 108 Client(Network& network, unsigned int command_timeout_ms = 30000);
icraggs 43:21da1f744243 109
icraggs 20:cad3d54d7ecf 110 /** Set the default message handling callback - used for any message which does not match a subscription message handler
icraggs 20:cad3d54d7ecf 111 * @param mh - pointer to the callback function
icraggs 20:cad3d54d7ecf 112 */
icraggs 20:cad3d54d7ecf 113 void setDefaultMessageHandler(messageHandler mh)
icraggs 20:cad3d54d7ecf 114 {
icraggs 20:cad3d54d7ecf 115 defaultMessageHandler.attach(mh);
icraggs 20:cad3d54d7ecf 116 }
icraggs 42:f5beda831651 117
icraggs 43:21da1f744243 118 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
icraggs 43:21da1f744243 119 * The nework object must be connected to the network endpoint before calling this
icraggs 43:21da1f744243 120 * Default connect options are used
icraggs 43:21da1f744243 121 * @return success code -
icraggs 43:21da1f744243 122 */
icraggs 43:21da1f744243 123 int connect();
icraggs 42:f5beda831651 124
icraggs 46:e335fcc1a663 125 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
icraggs 43:21da1f744243 126 * The nework object must be connected to the network endpoint before calling this
icraggs 20:cad3d54d7ecf 127 * @param options - connect options
icraggs 43:21da1f744243 128 * @return success code -
icraggs 43:21da1f744243 129 */
icraggs 43:21da1f744243 130 int connect(MQTTPacket_connectData& options);
icraggs 43:21da1f744243 131
icraggs 20:cad3d54d7ecf 132 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
icraggs 20:cad3d54d7ecf 133 * @param topic - the topic to publish to
icraggs 20:cad3d54d7ecf 134 * @param message - the message to send
icraggs 43:21da1f744243 135 * @return success code -
icraggs 43:21da1f744243 136 */
icraggs 43:21da1f744243 137 int publish(const char* topicName, Message& message);
icraggs 43:21da1f744243 138
icraggs 43:21da1f744243 139 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
icraggs 43:21da1f744243 140 * @param topic - the topic to publish to
icraggs 43:21da1f744243 141 * @param payload - the data to send
icraggs 43:21da1f744243 142 * @param payloadlen - the length of the data
icraggs 43:21da1f744243 143 * @param qos - the QoS to send the publish at
icraggs 43:21da1f744243 144 * @param retained - whether the message should be retained
icraggs 43:21da1f744243 145 * @return success code -
icraggs 43:21da1f744243 146 */
icraggs 43:21da1f744243 147 int publish(const char* topicName, void* payload, size_t payloadlen, enum QoS qos = QOS0, bool retained = false);
icraggs 43:21da1f744243 148
icraggs 43:21da1f744243 149 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
icraggs 43:21da1f744243 150 * @param topic - the topic to publish to
icraggs 43:21da1f744243 151 * @param payload - the data to send
icraggs 43:21da1f744243 152 * @param payloadlen - the length of the data
icraggs 43:21da1f744243 153 * @param id - the packet id used - returned
icraggs 43:21da1f744243 154 * @param qos - the QoS to send the publish at
icraggs 43:21da1f744243 155 * @param retained - whether the message should be retained
icraggs 43:21da1f744243 156 * @return success code -
icraggs 43:21da1f744243 157 */
icraggs 43:21da1f744243 158 int publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum QoS qos = QOS1, bool retained = false);
icraggs 43:21da1f744243 159
icraggs 20:cad3d54d7ecf 160 /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
icraggs 20:cad3d54d7ecf 161 * @param topicFilter - a topic pattern which can include wildcards
icraggs 20:cad3d54d7ecf 162 * @param qos - the MQTT QoS to subscribe at
icraggs 20:cad3d54d7ecf 163 * @param mh - the callback function to be invoked when a message is received for this subscription
icraggs 43:21da1f744243 164 * @return success code -
icraggs 43:21da1f744243 165 */
icraggs 20:cad3d54d7ecf 166 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh);
icraggs 43:21da1f744243 167
icraggs 20:cad3d54d7ecf 168 /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
icraggs 20:cad3d54d7ecf 169 * @param topicFilter - a topic pattern which can include wildcards
icraggs 43:21da1f744243 170 * @return success code -
icraggs 43:21da1f744243 171 */
icraggs 20:cad3d54d7ecf 172 int unsubscribe(const char* topicFilter);
icraggs 43:21da1f744243 173
icraggs 31:a51dd239b78e 174 /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state
icraggs 43:21da1f744243 175 * @return success code -
icraggs 20:cad3d54d7ecf 176 */
icraggs 20:cad3d54d7ecf 177 int disconnect();
icraggs 43:21da1f744243 178
icraggs 20:cad3d54d7ecf 179 /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive
icraggs 43:21da1f744243 180 * yield can be called if no other MQTT operation is needed. This will also allow messages to be
icraggs 20:cad3d54d7ecf 181 * received.
icraggs 23:05fc7de97d4a 182 * @param timeout_ms the time to wait, in milliseconds
icraggs 26:2658bb87c53d 183 * @return success code - on failure, this means the client has disconnected
icraggs 20:cad3d54d7ecf 184 */
icraggs 43:21da1f744243 185 int yield(unsigned long timeout_ms = 1000L);
icraggs 43:21da1f744243 186
icraggs 43:21da1f744243 187 /** Is the client connected?
icraggs 43:21da1f744243 188 * @return flag - is the client connected or not?
icraggs 43:21da1f744243 189 */
icraggs 43:21da1f744243 190 bool isConnected()
icraggs 43:21da1f744243 191 {
icraggs 43:21da1f744243 192 return isconnected;
icraggs 43:21da1f744243 193 }
icraggs 43:21da1f744243 194
icraggs 20:cad3d54d7ecf 195 private:
icraggs 20:cad3d54d7ecf 196
icraggs 46:e335fcc1a663 197 void cleanSession();
icraggs 20:cad3d54d7ecf 198 int cycle(Timer& timer);
icraggs 20:cad3d54d7ecf 199 int waitfor(int packet_type, Timer& timer);
icraggs 20:cad3d54d7ecf 200 int keepalive();
icraggs 43:21da1f744243 201 int publish(int len, Timer& timer, enum QoS qos);
icraggs 2:dcfdd2abfe71 202
icraggs 3:dbff6b768d28 203 int decodePacket(int* value, int timeout);
icraggs 20:cad3d54d7ecf 204 int readPacket(Timer& timer);
icraggs 20:cad3d54d7ecf 205 int sendPacket(int length, Timer& timer);
icraggs 26:2658bb87c53d 206 int deliverMessage(MQTTString& topicName, Message& message);
icraggs 26:2658bb87c53d 207 bool isTopicMatched(char* topicFilter, MQTTString& topicName);
icraggs 43:21da1f744243 208
icraggs 23:05fc7de97d4a 209 Network& ipstack;
icraggs 43:21da1f744243 210 unsigned long command_timeout_ms;
icraggs 15:64a57183aa03 211
icraggs 43:21da1f744243 212 unsigned char sendbuf[MAX_MQTT_PACKET_SIZE];
icraggs 43:21da1f744243 213 unsigned char readbuf[MAX_MQTT_PACKET_SIZE];
icraggs 43:21da1f744243 214
icraggs 43:21da1f744243 215 Timer last_sent, last_received;
icraggs 15:64a57183aa03 216 unsigned int keepAliveInterval;
icraggs 20:cad3d54d7ecf 217 bool ping_outstanding;
icraggs 43:21da1f744243 218 bool cleansession;
icraggs 43:21da1f744243 219
icraggs 9:01b8cc7d94cc 220 PacketId packetid;
icraggs 43:21da1f744243 221
icraggs 16:91c2f9a144d4 222 struct MessageHandlers
icraggs 15:64a57183aa03 223 {
icraggs 26:2658bb87c53d 224 const char* topicFilter;
icraggs 31:a51dd239b78e 225 FP<void, MessageData&> fp;
icraggs 23:05fc7de97d4a 226 } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic
icraggs 43:21da1f744243 227
icraggs 31:a51dd239b78e 228 FP<void, MessageData&> defaultMessageHandler;
icraggs 43:21da1f744243 229
icraggs 26:2658bb87c53d 230 bool isconnected;
icraggs 43:21da1f744243 231
icraggs 43:21da1f744243 232 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 233 unsigned char pubbuf[MAX_MQTT_PACKET_SIZE]; // store the last publish for sending on reconnect
icraggs 43:21da1f744243 234 int inflightLen;
icraggs 43:21da1f744243 235 unsigned short inflightMsgid;
icraggs 43:21da1f744243 236 enum QoS inflightQoS;
icraggs 43:21da1f744243 237 #endif
icraggs 43:21da1f744243 238
icraggs 43:21da1f744243 239 #if MQTTCLIENT_QOS2
icraggs 43:21da1f744243 240 bool pubrel;
icraggs 43:21da1f744243 241 #if !defined(MAX_INCOMING_QOS2_MESSAGES)
icraggs 43:21da1f744243 242 #define MAX_INCOMING_QOS2_MESSAGES 10
icraggs 43:21da1f744243 243 #endif
icraggs 43:21da1f744243 244 unsigned short incomingQoS2messages[MAX_INCOMING_QOS2_MESSAGES];
icraggs 43:21da1f744243 245 bool isQoS2msgidFree(unsigned short id);
icraggs 43:21da1f744243 246 bool useQoS2msgid(unsigned short id);
icraggs 46:e335fcc1a663 247 void freeQoS2msgid(unsigned short id);
icraggs 31:a51dd239b78e 248 #endif
icraggs 28:8b2abe9bd814 249
sam_grove 0:fe461e4d7afe 250 };
sam_grove 0:fe461e4d7afe 251
icraggs 15:64a57183aa03 252 }
icraggs 15:64a57183aa03 253
icraggs 15:64a57183aa03 254
icraggs 43:21da1f744243 255 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 46:e335fcc1a663 256 void MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::cleanSession()
icraggs 46:e335fcc1a663 257 {
icraggs 46:e335fcc1a663 258 ping_outstanding = false;
icraggs 46:e335fcc1a663 259 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 46:e335fcc1a663 260 messageHandlers[i].topicFilter = 0;
icraggs 46:e335fcc1a663 261 isconnected = false;
icraggs 46:e335fcc1a663 262
icraggs 46:e335fcc1a663 263 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 46:e335fcc1a663 264 inflightMsgid = 0;
icraggs 46:e335fcc1a663 265 inflightQoS = QOS0;
icraggs 46:e335fcc1a663 266 #endif
icraggs 46:e335fcc1a663 267
icraggs 46:e335fcc1a663 268 #if MQTTCLIENT_QOS2
icraggs 46:e335fcc1a663 269 pubrel = false;
icraggs 46:e335fcc1a663 270 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
icraggs 46:e335fcc1a663 271 incomingQoS2messages[i] = 0;
icraggs 46:e335fcc1a663 272 #endif
icraggs 46:e335fcc1a663 273 }
icraggs 46:e335fcc1a663 274
icraggs 46:e335fcc1a663 275
icraggs 46:e335fcc1a663 276 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 277 MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid()
icraggs 15:64a57183aa03 278 {
icraggs 43:21da1f744243 279 last_sent = Timer();
icraggs 43:21da1f744243 280 last_received = Timer();
icraggs 43:21da1f744243 281 this->command_timeout_ms = command_timeout_ms;
icraggs 46:e335fcc1a663 282 cleanSession();
icraggs 46:e335fcc1a663 283 }
icraggs 43:21da1f744243 284
icraggs 43:21da1f744243 285
icraggs 43:21da1f744243 286 #if MQTTCLIENT_QOS2
icraggs 43:21da1f744243 287 template<class Network, class Timer, int a, int b>
icraggs 43:21da1f744243 288 bool MQTT::Client<Network, Timer, a, b>::isQoS2msgidFree(unsigned short id)
icraggs 43:21da1f744243 289 {
icraggs 43:21da1f744243 290 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
icraggs 43:21da1f744243 291 {
icraggs 43:21da1f744243 292 if (incomingQoS2messages[i] == id)
icraggs 43:21da1f744243 293 return false;
icraggs 43:21da1f744243 294 }
icraggs 43:21da1f744243 295 return true;
icraggs 11:db15da110a37 296 }
icraggs 11:db15da110a37 297
icraggs 11:db15da110a37 298
icraggs 43:21da1f744243 299 template<class Network, class Timer, int a, int b>
icraggs 43:21da1f744243 300 bool MQTT::Client<Network, Timer, a, b>::useQoS2msgid(unsigned short id)
icraggs 43:21da1f744243 301 {
icraggs 43:21da1f744243 302 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
icraggs 43:21da1f744243 303 {
icraggs 43:21da1f744243 304 if (incomingQoS2messages[i] == 0)
icraggs 43:21da1f744243 305 {
icraggs 43:21da1f744243 306 incomingQoS2messages[i] = id;
icraggs 43:21da1f744243 307 return true;
icraggs 43:21da1f744243 308 }
icraggs 43:21da1f744243 309 }
icraggs 43:21da1f744243 310 return false;
icraggs 43:21da1f744243 311 }
icraggs 46:e335fcc1a663 312
icraggs 46:e335fcc1a663 313
icraggs 46:e335fcc1a663 314 template<class Network, class Timer, int a, int b>
icraggs 46:e335fcc1a663 315 void MQTT::Client<Network, Timer, a, b>::freeQoS2msgid(unsigned short id)
icraggs 46:e335fcc1a663 316 {
icraggs 46:e335fcc1a663 317 for (int i = 0; i < MAX_INCOMING_QOS2_MESSAGES; ++i)
icraggs 46:e335fcc1a663 318 {
icraggs 46:e335fcc1a663 319 if (incomingQoS2messages[i] == id)
icraggs 46:e335fcc1a663 320 {
icraggs 46:e335fcc1a663 321 incomingQoS2messages[i] = 0;
icraggs 46:e335fcc1a663 322 return;
icraggs 46:e335fcc1a663 323 }
icraggs 46:e335fcc1a663 324 }
icraggs 46:e335fcc1a663 325 }
icraggs 43:21da1f744243 326 #endif
icraggs 43:21da1f744243 327
icraggs 43:21da1f744243 328
icraggs 43:21da1f744243 329 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 330 int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer)
icraggs 8:c46930bd6c82 331 {
icraggs 43:21da1f744243 332 int rc = FAILURE,
icraggs 23:05fc7de97d4a 333 sent = 0;
icraggs 43:21da1f744243 334
icraggs 23:05fc7de97d4a 335 while (sent < length && !timer.expired())
icraggs 23:05fc7de97d4a 336 {
icraggs 46:e335fcc1a663 337 rc = ipstack.write(&sendbuf[sent], length - sent, timer.left_ms());
icraggs 26:2658bb87c53d 338 if (rc < 0) // there was an error writing the data
DuyLionTran 56:30f7b745eaaa 339 {
DuyLionTran 56:30f7b745eaaa 340 printf("sendPacket fail %d\n", rc);
icraggs 26:2658bb87c53d 341 break;
DuyLionTran 56:30f7b745eaaa 342 }
icraggs 26:2658bb87c53d 343 sent += rc;
icraggs 23:05fc7de97d4a 344 }
icraggs 20:cad3d54d7ecf 345 if (sent == length)
icraggs 23:05fc7de97d4a 346 {
icraggs 43:21da1f744243 347 if (this->keepAliveInterval > 0)
icraggs 43:21da1f744243 348 last_sent.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet
icraggs 23:05fc7de97d4a 349 rc = SUCCESS;
icraggs 23:05fc7de97d4a 350 }
icraggs 23:05fc7de97d4a 351 else
icraggs 23:05fc7de97d4a 352 rc = FAILURE;
icraggs 43:21da1f744243 353
icraggs 43:21da1f744243 354 #if defined(MQTT_DEBUG)
icraggs 46:e335fcc1a663 355 char printbuf[150];
icraggs 46:e335fcc1a663 356 DEBUG("Rc %d from sending packet %s\n", rc, MQTTFormat_toServerString(printbuf, sizeof(printbuf), sendbuf, length));
icraggs 43:21da1f744243 357 #endif
icraggs 23:05fc7de97d4a 358 return rc;
icraggs 8:c46930bd6c82 359 }
icraggs 8:c46930bd6c82 360
icraggs 8:c46930bd6c82 361
icraggs 43:21da1f744243 362 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 363 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
icraggs 8:c46930bd6c82 364 {
icraggs 36:2f1ada427e56 365 unsigned char c;
icraggs 8:c46930bd6c82 366 int multiplier = 1;
icraggs 8:c46930bd6c82 367 int len = 0;
icraggs 20:cad3d54d7ecf 368 const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
icraggs 8:c46930bd6c82 369
icraggs 8:c46930bd6c82 370 *value = 0;
icraggs 8:c46930bd6c82 371 do
icraggs 8:c46930bd6c82 372 {
icraggs 8:c46930bd6c82 373 int rc = MQTTPACKET_READ_ERROR;
icraggs 8:c46930bd6c82 374
icraggs 8:c46930bd6c82 375 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
icraggs 8:c46930bd6c82 376 {
icraggs 8:c46930bd6c82 377 rc = MQTTPACKET_READ_ERROR; /* bad data */
icraggs 8:c46930bd6c82 378 goto exit;
icraggs 8:c46930bd6c82 379 }
icraggs 23:05fc7de97d4a 380 rc = ipstack.read(&c, 1, timeout);
icraggs 8:c46930bd6c82 381 if (rc != 1)
icraggs 8:c46930bd6c82 382 goto exit;
icraggs 8:c46930bd6c82 383 *value += (c & 127) * multiplier;
icraggs 8:c46930bd6c82 384 multiplier *= 128;
icraggs 8:c46930bd6c82 385 } while ((c & 128) != 0);
icraggs 8:c46930bd6c82 386 exit:
icraggs 8:c46930bd6c82 387 return len;
icraggs 8:c46930bd6c82 388 }
icraggs 8:c46930bd6c82 389
icraggs 8:c46930bd6c82 390
icraggs 8:c46930bd6c82 391 /**
icraggs 8:c46930bd6c82 392 * If any read fails in this method, then we should disconnect from the network, as on reconnect
icraggs 43:21da1f744243 393 * the packets can be retried.
icraggs 8:c46930bd6c82 394 * @param timeout the max time to wait for the packet read to complete, in milliseconds
icraggs 8:c46930bd6c82 395 * @return the MQTT packet type, or -1 if none
icraggs 8:c46930bd6c82 396 */
icraggs 46:e335fcc1a663 397 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 46:e335fcc1a663 398 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::readPacket(Timer& timer)
icraggs 8:c46930bd6c82 399 {
icraggs 26:2658bb87c53d 400 int rc = FAILURE;
icraggs 8:c46930bd6c82 401 MQTTHeader header = {0};
icraggs 8:c46930bd6c82 402 int len = 0;
icraggs 8:c46930bd6c82 403 int rem_len = 0;
icraggs 8:c46930bd6c82 404
icraggs 8:c46930bd6c82 405 /* 1. read the header byte. This has the packet type in it */
icraggs 23:05fc7de97d4a 406 if (ipstack.read(readbuf, 1, timer.left_ms()) != 1)
icraggs 8:c46930bd6c82 407 goto exit;
icraggs 8:c46930bd6c82 408
icraggs 8:c46930bd6c82 409 len = 1;
icraggs 8:c46930bd6c82 410 /* 2. read the remaining length. This is variable in itself */
icraggs 20:cad3d54d7ecf 411 decodePacket(&rem_len, timer.left_ms());
icraggs 43:21da1f744243 412 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length into the buffer */
icraggs 8:c46930bd6c82 413
icraggs 46:e335fcc1a663 414 if (rem_len > (MAX_MQTT_PACKET_SIZE - len))
icraggs 46:e335fcc1a663 415 {
icraggs 46:e335fcc1a663 416 rc = BUFFER_OVERFLOW;
icraggs 46:e335fcc1a663 417 goto exit;
icraggs 46:e335fcc1a663 418 }
icraggs 46:e335fcc1a663 419
icraggs 8:c46930bd6c82 420 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
icraggs 43:21da1f744243 421 if (rem_len > 0 && (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len))
icraggs 8:c46930bd6c82 422 goto exit;
icraggs 8:c46930bd6c82 423
icraggs 8:c46930bd6c82 424 header.byte = readbuf[0];
icraggs 8:c46930bd6c82 425 rc = header.bits.type;
icraggs 43:21da1f744243 426 if (this->keepAliveInterval > 0)
icraggs 43:21da1f744243 427 last_received.countdown(this->keepAliveInterval); // record the fact that we have successfully received a packet
icraggs 8:c46930bd6c82 428 exit:
icraggs 43:21da1f744243 429
icraggs 43:21da1f744243 430 #if defined(MQTT_DEBUG)
icraggs 46:e335fcc1a663 431 if (rc >= 0)
icraggs 46:e335fcc1a663 432 {
icraggs 46:e335fcc1a663 433 char printbuf[50];
icraggs 46:e335fcc1a663 434 DEBUG("Rc %d from receiving packet %s\n", rc, MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len));
icraggs 46:e335fcc1a663 435 }
icraggs 43:21da1f744243 436 #endif
icraggs 8:c46930bd6c82 437 return rc;
icraggs 3:dbff6b768d28 438 }
icraggs 3:dbff6b768d28 439
icraggs 8:c46930bd6c82 440
icraggs 26:2658bb87c53d 441 // assume topic filter and name is in correct format
icraggs 26:2658bb87c53d 442 // # can only be at end
icraggs 26:2658bb87c53d 443 // + and # can only be next to separator
icraggs 43:21da1f744243 444 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 445 bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName)
icraggs 26:2658bb87c53d 446 {
icraggs 26:2658bb87c53d 447 char* curf = topicFilter;
icraggs 26:2658bb87c53d 448 char* curn = topicName.lenstring.data;
icraggs 26:2658bb87c53d 449 char* curn_end = curn + topicName.lenstring.len;
icraggs 43:21da1f744243 450
icraggs 26:2658bb87c53d 451 while (*curf && curn < curn_end)
icraggs 26:2658bb87c53d 452 {
icraggs 26:2658bb87c53d 453 if (*curn == '/' && *curf != '/')
icraggs 26:2658bb87c53d 454 break;
icraggs 26:2658bb87c53d 455 if (*curf != '+' && *curf != '#' && *curf != *curn)
icraggs 26:2658bb87c53d 456 break;
icraggs 26:2658bb87c53d 457 if (*curf == '+')
icraggs 26:2658bb87c53d 458 { // skip until we meet the next separator, or end of string
icraggs 26:2658bb87c53d 459 char* nextpos = curn + 1;
icraggs 26:2658bb87c53d 460 while (nextpos < curn_end && *nextpos != '/')
icraggs 26:2658bb87c53d 461 nextpos = ++curn + 1;
icraggs 26:2658bb87c53d 462 }
icraggs 26:2658bb87c53d 463 else if (*curf == '#')
icraggs 26:2658bb87c53d 464 curn = curn_end - 1; // skip until end of string
icraggs 26:2658bb87c53d 465 curf++;
icraggs 26:2658bb87c53d 466 curn++;
icraggs 26:2658bb87c53d 467 };
icraggs 43:21da1f744243 468
icraggs 26:2658bb87c53d 469 return (curn == curn_end) && (*curf == '\0');
icraggs 26:2658bb87c53d 470 }
icraggs 26:2658bb87c53d 471
icraggs 26:2658bb87c53d 472
icraggs 26:2658bb87c53d 473
icraggs 43:21da1f744243 474 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 26:2658bb87c53d 475 int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message)
icraggs 15:64a57183aa03 476 {
icraggs 26:2658bb87c53d 477 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 478
icraggs 20:cad3d54d7ecf 479 // we have to find the right message handler - indexed by topic
icraggs 23:05fc7de97d4a 480 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 20:cad3d54d7ecf 481 {
icraggs 26:2658bb87c53d 482 if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) ||
icraggs 26:2658bb87c53d 483 isTopicMatched((char*)messageHandlers[i].topicFilter, topicName)))
icraggs 20:cad3d54d7ecf 484 {
icraggs 26:2658bb87c53d 485 if (messageHandlers[i].fp.attached())
icraggs 26:2658bb87c53d 486 {
icraggs 31:a51dd239b78e 487 MessageData md(topicName, message);
icraggs 31:a51dd239b78e 488 messageHandlers[i].fp(md);
icraggs 26:2658bb87c53d 489 rc = SUCCESS;
icraggs 26:2658bb87c53d 490 }
icraggs 20:cad3d54d7ecf 491 }
icraggs 20:cad3d54d7ecf 492 }
icraggs 43:21da1f744243 493
icraggs 43:21da1f744243 494 if (rc == FAILURE && defaultMessageHandler.attached())
icraggs 26:2658bb87c53d 495 {
icraggs 31:a51dd239b78e 496 MessageData md(topicName, message);
icraggs 31:a51dd239b78e 497 defaultMessageHandler(md);
icraggs 26:2658bb87c53d 498 rc = SUCCESS;
icraggs 43:21da1f744243 499 }
icraggs 43:21da1f744243 500
icraggs 20:cad3d54d7ecf 501 return rc;
icraggs 15:64a57183aa03 502 }
icraggs 15:64a57183aa03 503
icraggs 15:64a57183aa03 504
icraggs 20:cad3d54d7ecf 505
icraggs 43:21da1f744243 506 template<class Network, class Timer, int a, int b>
icraggs 43:21da1f744243 507 int MQTT::Client<Network, Timer, a, b>::yield(unsigned long timeout_ms)
icraggs 20:cad3d54d7ecf 508 {
icraggs 26:2658bb87c53d 509 int rc = SUCCESS;
DuyLionTran 56:30f7b745eaaa 510 Timer timer;
icraggs 43:21da1f744243 511
icraggs 23:05fc7de97d4a 512 timer.countdown_ms(timeout_ms);
icraggs 20:cad3d54d7ecf 513 while (!timer.expired())
icraggs 26:2658bb87c53d 514 {
icraggs 46:e335fcc1a663 515 if (cycle(timer) < 0)
icraggs 26:2658bb87c53d 516 {
icraggs 26:2658bb87c53d 517 rc = FAILURE;
icraggs 26:2658bb87c53d 518 break;
icraggs 26:2658bb87c53d 519 }
icraggs 26:2658bb87c53d 520 }
icraggs 43:21da1f744243 521
icraggs 26:2658bb87c53d 522 return rc;
icraggs 20:cad3d54d7ecf 523 }
icraggs 20:cad3d54d7ecf 524
icraggs 20:cad3d54d7ecf 525
icraggs 43:21da1f744243 526 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 527 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
icraggs 8:c46930bd6c82 528 {
icraggs 8:c46930bd6c82 529 /* get one piece of work off the wire and one pass through */
DuyLionTran 61:36e76acf2166 530 // printf("In cycle\r\n");
Ian Craggs 12:cc7f2d62a393 531 // read the socket, see what work is due
icraggs 46:e335fcc1a663 532 int packet_type = readPacket(timer);
icraggs 43:21da1f744243 533
icraggs 26:2658bb87c53d 534 int len = 0,
icraggs 26:2658bb87c53d 535 rc = SUCCESS;
icraggs 8:c46930bd6c82 536 switch (packet_type)
icraggs 8:c46930bd6c82 537 {
icraggs 46:e335fcc1a663 538 case FAILURE:
icraggs 46:e335fcc1a663 539 case BUFFER_OVERFLOW:
icraggs 46:e335fcc1a663 540 rc = packet_type;
icraggs 46:e335fcc1a663 541 break;
icraggs 15:64a57183aa03 542 case CONNACK:
icraggs 8:c46930bd6c82 543 case PUBACK:
icraggs 8:c46930bd6c82 544 case SUBACK:
icraggs 8:c46930bd6c82 545 break;
icraggs 15:64a57183aa03 546 case PUBLISH:
icraggs 46:e335fcc1a663 547 {
icraggs 46:e335fcc1a663 548 MQTTString topicName = MQTTString_initializer;
icraggs 20:cad3d54d7ecf 549 Message msg;
icraggs 46:e335fcc1a663 550 int intQoS;
icraggs 46:e335fcc1a663 551 if (MQTTDeserialize_publish((unsigned char*)&msg.dup, &intQoS, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
icraggs 36:2f1ada427e56 552 (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 553 goto exit;
icraggs 46:e335fcc1a663 554 msg.qos = (enum QoS)intQoS;
icraggs 43:21da1f744243 555 #if MQTTCLIENT_QOS2
icraggs 43:21da1f744243 556 if (msg.qos != QOS2)
icraggs 43:21da1f744243 557 #endif
icraggs 31:a51dd239b78e 558 deliverMessage(topicName, msg);
icraggs 43:21da1f744243 559 #if MQTTCLIENT_QOS2
icraggs 31:a51dd239b78e 560 else if (isQoS2msgidFree(msg.id))
icraggs 31:a51dd239b78e 561 {
icraggs 43:21da1f744243 562 if (useQoS2msgid(msg.id))
icraggs 43:21da1f744243 563 deliverMessage(topicName, msg);
icraggs 43:21da1f744243 564 else
icraggs 43:21da1f744243 565 WARN("Maximum number of incoming QoS2 messages exceeded");
icraggs 46:e335fcc1a663 566 }
icraggs 31:a51dd239b78e 567 #endif
icraggs 43:21da1f744243 568 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 20:cad3d54d7ecf 569 if (msg.qos != QOS0)
icraggs 20:cad3d54d7ecf 570 {
icraggs 20:cad3d54d7ecf 571 if (msg.qos == QOS1)
icraggs 43:21da1f744243 572 len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id);
icraggs 20:cad3d54d7ecf 573 else if (msg.qos == QOS2)
icraggs 43:21da1f744243 574 len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id);
icraggs 26:2658bb87c53d 575 if (len <= 0)
icraggs 26:2658bb87c53d 576 rc = FAILURE;
icraggs 26:2658bb87c53d 577 else
icraggs 26:2658bb87c53d 578 rc = sendPacket(len, timer);
icraggs 26:2658bb87c53d 579 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 580 goto exit; // there was a problem
icraggs 20:cad3d54d7ecf 581 }
Ian Craggs 12:cc7f2d62a393 582 break;
icraggs 43:21da1f744243 583 #endif
icraggs 46:e335fcc1a663 584 }
icraggs 43:21da1f744243 585 #if MQTTCLIENT_QOS2
icraggs 15:64a57183aa03 586 case PUBREC:
icraggs 46:e335fcc1a663 587 case PUBREL:
icraggs 36:2f1ada427e56 588 unsigned short mypacketid;
icraggs 36:2f1ada427e56 589 unsigned char dup, type;
icraggs 26:2658bb87c53d 590 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 591 rc = FAILURE;
icraggs 46:e335fcc1a663 592 else if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE,
icraggs 46:e335fcc1a663 593 (packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0)
icraggs 26:2658bb87c53d 594 rc = FAILURE;
icraggs 26:2658bb87c53d 595 else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet
icraggs 26:2658bb87c53d 596 rc = FAILURE; // there was a problem
icraggs 26:2658bb87c53d 597 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 598 goto exit; // there was a problem
icraggs 46:e335fcc1a663 599 if (packet_type == PUBREL)
icraggs 46:e335fcc1a663 600 freeQoS2msgid(mypacketid);
icraggs 8:c46930bd6c82 601 break;
icraggs 46:e335fcc1a663 602
icraggs 8:c46930bd6c82 603 case PUBCOMP:
icraggs 8:c46930bd6c82 604 break;
icraggs 43:21da1f744243 605 #endif
icraggs 15:64a57183aa03 606 case PINGRESP:
icraggs 20:cad3d54d7ecf 607 ping_outstanding = false;
icraggs 8:c46930bd6c82 608 break;
icraggs 15:64a57183aa03 609 }
icraggs 20:cad3d54d7ecf 610 keepalive();
Ian Craggs 12:cc7f2d62a393 611 exit:
DuyLionTran 61:36e76acf2166 612 // printf("Out cycle\r\n");
icraggs 26:2658bb87c53d 613 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 614 rc = packet_type;
DuyLionTran 56:30f7b745eaaa 615
icraggs 26:2658bb87c53d 616 return rc;
icraggs 15:64a57183aa03 617 }
icraggs 15:64a57183aa03 618
icraggs 15:64a57183aa03 619
icraggs 23:05fc7de97d4a 620 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 621 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive()
icraggs 15:64a57183aa03 622 {
icraggs 26:2658bb87c53d 623 int rc = FAILURE;
icraggs 15:64a57183aa03 624
icraggs 20:cad3d54d7ecf 625 if (keepAliveInterval == 0)
icraggs 26:2658bb87c53d 626 {
icraggs 26:2658bb87c53d 627 rc = SUCCESS;
icraggs 20:cad3d54d7ecf 628 goto exit;
icraggs 26:2658bb87c53d 629 }
icraggs 15:64a57183aa03 630
icraggs 43:21da1f744243 631 if (last_sent.expired() || last_received.expired())
icraggs 20:cad3d54d7ecf 632 {
icraggs 26:2658bb87c53d 633 if (!ping_outstanding)
icraggs 20:cad3d54d7ecf 634 {
icraggs 46:e335fcc1a663 635 Timer timer(1000);
icraggs 43:21da1f744243 636 int len = MQTTSerialize_pingreq(sendbuf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 637 if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet
icraggs 20:cad3d54d7ecf 638 ping_outstanding = true;
icraggs 20:cad3d54d7ecf 639 }
icraggs 20:cad3d54d7ecf 640 }
icraggs 15:64a57183aa03 641
icraggs 15:64a57183aa03 642 exit:
icraggs 20:cad3d54d7ecf 643 return rc;
icraggs 8:c46930bd6c82 644 }
icraggs 8:c46930bd6c82 645
icraggs 8:c46930bd6c82 646
icraggs 16:91c2f9a144d4 647 // only used in single-threaded mode where one command at a time is in process
icraggs 43:21da1f744243 648 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 649 int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer)
icraggs 15:64a57183aa03 650 {
icraggs 26:2658bb87c53d 651 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 652 do
DuyLionTran 56:30f7b745eaaa 653 {
icraggs 43:21da1f744243 654 if (timer.expired())
icraggs 20:cad3d54d7ecf 655 break; // we timed out
icraggs 20:cad3d54d7ecf 656 }
icraggs 43:21da1f744243 657 while ((rc = cycle(timer)) != packet_type);
icraggs 43:21da1f744243 658
icraggs 20:cad3d54d7ecf 659 return rc;
icraggs 16:91c2f9a144d4 660 }
icraggs 16:91c2f9a144d4 661
icraggs 16:91c2f9a144d4 662
icraggs 43:21da1f744243 663 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 664 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData& options)
icraggs 16:91c2f9a144d4 665 {
icraggs 46:e335fcc1a663 666 Timer connect_timer(command_timeout_ms);
icraggs 26:2658bb87c53d 667 int rc = FAILURE;
icraggs 31:a51dd239b78e 668 int len = 0;
icraggs 43:21da1f744243 669
icraggs 31:a51dd239b78e 670 if (isconnected) // don't send connect packet again if we are already connected
icraggs 31:a51dd239b78e 671 goto exit;
icraggs 8:c46930bd6c82 672
DuyLionTran 61:36e76acf2166 673
icraggs 43:21da1f744243 674 this->keepAliveInterval = options.keepAliveInterval;
icraggs 43:21da1f744243 675 this->cleansession = options.cleansession;
icraggs 43:21da1f744243 676 if ((len = MQTTSerialize_connect(sendbuf, MAX_MQTT_PACKET_SIZE, &options)) <= 0)
icraggs 26:2658bb87c53d 677 goto exit;
icraggs 26:2658bb87c53d 678 if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet
icraggs 20:cad3d54d7ecf 679 goto exit; // there was a problem
icraggs 43:21da1f744243 680
icraggs 43:21da1f744243 681 if (this->keepAliveInterval > 0)
icraggs 43:21da1f744243 682 last_received.countdown(this->keepAliveInterval);
icraggs 20:cad3d54d7ecf 683 // this will be a blocking call, wait for the connack
icraggs 20:cad3d54d7ecf 684 if (waitfor(CONNACK, connect_timer) == CONNACK)
icraggs 15:64a57183aa03 685 {
icraggs 36:2f1ada427e56 686 unsigned char connack_rc = 255;
icraggs 37:e3d64f9b986c 687 bool sessionPresent = false;
icraggs 37:e3d64f9b986c 688 if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 689 rc = connack_rc;
icraggs 26:2658bb87c53d 690 else
icraggs 26:2658bb87c53d 691 rc = FAILURE;
icraggs 8:c46930bd6c82 692 }
icraggs 26:2658bb87c53d 693 else
icraggs 26:2658bb87c53d 694 rc = FAILURE;
icraggs 46:e335fcc1a663 695
icraggs 43:21da1f744243 696 #if MQTTCLIENT_QOS2
icraggs 46:e335fcc1a663 697 // resend any inflight publish
icraggs 46:e335fcc1a663 698 if (inflightMsgid > 0 && inflightQoS == QOS2 && pubrel)
icraggs 43:21da1f744243 699 {
icraggs 43:21da1f744243 700 if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, inflightMsgid)) <= 0)
icraggs 43:21da1f744243 701 rc = FAILURE;
icraggs 43:21da1f744243 702 else
icraggs 43:21da1f744243 703 rc = publish(len, connect_timer, inflightQoS);
icraggs 43:21da1f744243 704 }
icraggs 43:21da1f744243 705 else
icraggs 43:21da1f744243 706 #endif
icraggs 43:21da1f744243 707 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 708 if (inflightMsgid > 0)
icraggs 43:21da1f744243 709 {
icraggs 43:21da1f744243 710 memcpy(sendbuf, pubbuf, MAX_MQTT_PACKET_SIZE);
icraggs 43:21da1f744243 711 rc = publish(inflightLen, connect_timer, inflightQoS);
icraggs 43:21da1f744243 712 }
icraggs 43:21da1f744243 713 #endif
icraggs 43:21da1f744243 714
icraggs 15:64a57183aa03 715 exit:
DuyLionTran 56:30f7b745eaaa 716 if (rc == SUCCESS) {
icraggs 26:2658bb87c53d 717 isconnected = true;
DuyLionTran 56:30f7b745eaaa 718 }
icraggs 8:c46930bd6c82 719 return rc;
icraggs 8:c46930bd6c82 720 }
icraggs 8:c46930bd6c82 721
icraggs 8:c46930bd6c82 722
icraggs 43:21da1f744243 723 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 724 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect()
icraggs 43:21da1f744243 725 {
icraggs 43:21da1f744243 726 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
icraggs 43:21da1f744243 727 return connect(default_options);
icraggs 43:21da1f744243 728 }
icraggs 43:21da1f744243 729
icraggs 43:21da1f744243 730
icraggs 43:21da1f744243 731 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 732 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler)
icraggs 43:21da1f744243 733 {
icraggs 43:21da1f744243 734 int rc = FAILURE;
icraggs 46:e335fcc1a663 735 Timer timer(command_timeout_ms);
icraggs 26:2658bb87c53d 736 int len = 0;
icraggs 46:e335fcc1a663 737 MQTTString topic = {(char*)topicFilter, {0, 0}};
icraggs 43:21da1f744243 738
icraggs 26:2658bb87c53d 739 if (!isconnected)
DuyLionTran 56:30f7b745eaaa 740 {
DuyLionTran 56:30f7b745eaaa 741 printf("Not connected\r\n");
icraggs 20:cad3d54d7ecf 742 goto exit;
DuyLionTran 56:30f7b745eaaa 743 }
icraggs 43:21da1f744243 744
DuyLionTran 56:30f7b745eaaa 745 len = MQTTSerialize_subscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int)qos);
icraggs 26:2658bb87c53d 746 if (len <= 0)
DuyLionTran 56:30f7b745eaaa 747 {
icraggs 26:2658bb87c53d 748 goto exit;
DuyLionTran 56:30f7b745eaaa 749 }
DuyLionTran 56:30f7b745eaaa 750
icraggs 23:05fc7de97d4a 751 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
DuyLionTran 56:30f7b745eaaa 752 {
icraggs 26:2658bb87c53d 753 goto exit; // there was a problem
DuyLionTran 56:30f7b745eaaa 754 }
icraggs 43:21da1f744243 755
icraggs 43:21da1f744243 756 if (waitfor(SUBACK, timer) == SUBACK) // wait for suback
icraggs 8:c46930bd6c82 757 {
icraggs 36:2f1ada427e56 758 int count = 0, grantedQoS = -1;
icraggs 36:2f1ada427e56 759 unsigned short mypacketid;
icraggs 23:05fc7de97d4a 760 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 43:21da1f744243 761 rc = grantedQoS; // 0, 1, 2 or 0x80
icraggs 20:cad3d54d7ecf 762 if (rc != 0x80)
icraggs 8:c46930bd6c82 763 {
icraggs 23:05fc7de97d4a 764 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 16:91c2f9a144d4 765 {
icraggs 26:2658bb87c53d 766 if (messageHandlers[i].topicFilter == 0)
icraggs 20:cad3d54d7ecf 767 {
icraggs 26:2658bb87c53d 768 messageHandlers[i].topicFilter = topicFilter;
icraggs 20:cad3d54d7ecf 769 messageHandlers[i].fp.attach(messageHandler);
icraggs 20:cad3d54d7ecf 770 rc = 0;
icraggs 20:cad3d54d7ecf 771 break;
icraggs 20:cad3d54d7ecf 772 }
icraggs 16:91c2f9a144d4 773 }
icraggs 8:c46930bd6c82 774 }
icraggs 8:c46930bd6c82 775 }
icraggs 43:21da1f744243 776 else
DuyLionTran 56:30f7b745eaaa 777 {
icraggs 26:2658bb87c53d 778 rc = FAILURE;
DuyLionTran 56:30f7b745eaaa 779 }
icraggs 43:21da1f744243 780
icraggs 15:64a57183aa03 781 exit:
icraggs 43:21da1f744243 782 if (rc != SUCCESS)
icraggs 46:e335fcc1a663 783 cleanSession();
Ian Craggs 12:cc7f2d62a393 784 return rc;
icraggs 15:64a57183aa03 785 }
icraggs 15:64a57183aa03 786
icraggs 15:64a57183aa03 787
icraggs 43:21da1f744243 788 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 789 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter)
icraggs 43:21da1f744243 790 {
icraggs 26:2658bb87c53d 791 int rc = FAILURE;
icraggs 46:e335fcc1a663 792 Timer timer(command_timeout_ms);
icraggs 46:e335fcc1a663 793 MQTTString topic = {(char*)topicFilter, {0, 0}};
icraggs 31:a51dd239b78e 794 int len = 0;
icraggs 43:21da1f744243 795
DuyLionTran 56:30f7b745eaaa 796 if (!isconnected) {
DuyLionTran 56:30f7b745eaaa 797
DuyLionTran 56:30f7b745eaaa 798 goto exit;
DuyLionTran 56:30f7b745eaaa 799 }
icraggs 43:21da1f744243 800
icraggs 43:21da1f744243 801 if ((len = MQTTSerialize_unsubscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0)
icraggs 20:cad3d54d7ecf 802 goto exit;
icraggs 43:21da1f744243 803 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the unsubscribe packet
icraggs 20:cad3d54d7ecf 804 goto exit; // there was a problem
icraggs 43:21da1f744243 805
icraggs 20:cad3d54d7ecf 806 if (waitfor(UNSUBACK, timer) == UNSUBACK)
Ian Craggs 12:cc7f2d62a393 807 {
icraggs 36:2f1ada427e56 808 unsigned short mypacketid; // should be the same as the packetid above
icraggs 23:05fc7de97d4a 809 if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 46:e335fcc1a663 810 {
icraggs 43:21da1f744243 811 rc = 0;
icraggs 46:e335fcc1a663 812
icraggs 46:e335fcc1a663 813 // remove the subscription message handler associated with this topic, if there is one
icraggs 46:e335fcc1a663 814 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 46:e335fcc1a663 815 {
icraggs 46:e335fcc1a663 816 if (messageHandlers[i].topicFilter && strcmp(messageHandlers[i].topicFilter, topicFilter) == 0)
icraggs 46:e335fcc1a663 817 {
icraggs 46:e335fcc1a663 818 messageHandlers[i].topicFilter = 0;
icraggs 46:e335fcc1a663 819 break;
icraggs 46:e335fcc1a663 820 }
icraggs 46:e335fcc1a663 821 }
icraggs 46:e335fcc1a663 822 }
Ian Craggs 12:cc7f2d62a393 823 }
icraggs 26:2658bb87c53d 824 else
icraggs 26:2658bb87c53d 825 rc = FAILURE;
icraggs 43:21da1f744243 826
icraggs 15:64a57183aa03 827 exit:
icraggs 43:21da1f744243 828 if (rc != SUCCESS)
icraggs 46:e335fcc1a663 829 cleanSession();
Ian Craggs 12:cc7f2d62a393 830 return rc;
icraggs 15:64a57183aa03 831 }
icraggs 15:64a57183aa03 832
icraggs 15:64a57183aa03 833
icraggs 43:21da1f744243 834 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 835 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Timer& timer, enum QoS qos)
icraggs 20:cad3d54d7ecf 836 {
icraggs 43:21da1f744243 837 int rc;
icraggs 46:e335fcc1a663 838
icraggs 43:21da1f744243 839 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the publish packet
icraggs 43:21da1f744243 840 goto exit; // there was a problem
icraggs 15:64a57183aa03 841
icraggs 46:e335fcc1a663 842 #if MQTTCLIENT_QOS1
icraggs 43:21da1f744243 843 if (qos == QOS1)
Ian Craggs 12:cc7f2d62a393 844 {
icraggs 20:cad3d54d7ecf 845 if (waitfor(PUBACK, timer) == PUBACK)
icraggs 20:cad3d54d7ecf 846 {
icraggs 36:2f1ada427e56 847 unsigned short mypacketid;
icraggs 36:2f1ada427e56 848 unsigned char dup, type;
icraggs 26:2658bb87c53d 849 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 850 rc = FAILURE;
icraggs 43:21da1f744243 851 else if (inflightMsgid == mypacketid)
icraggs 43:21da1f744243 852 inflightMsgid = 0;
icraggs 20:cad3d54d7ecf 853 }
icraggs 26:2658bb87c53d 854 else
icraggs 26:2658bb87c53d 855 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 856 }
icraggs 43:21da1f744243 857 #elif MQTTCLIENT_QOS2
icraggs 43:21da1f744243 858 else if (qos == QOS2)
Ian Craggs 12:cc7f2d62a393 859 {
icraggs 20:cad3d54d7ecf 860 if (waitfor(PUBCOMP, timer) == PUBCOMP)
icraggs 20:cad3d54d7ecf 861 {
icraggs 36:2f1ada427e56 862 unsigned short mypacketid;
icraggs 36:2f1ada427e56 863 unsigned char dup, type;
icraggs 26:2658bb87c53d 864 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 865 rc = FAILURE;
icraggs 43:21da1f744243 866 else if (inflightMsgid == mypacketid)
icraggs 43:21da1f744243 867 inflightMsgid = 0;
icraggs 20:cad3d54d7ecf 868 }
icraggs 26:2658bb87c53d 869 else
icraggs 26:2658bb87c53d 870 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 871 }
icraggs 43:21da1f744243 872 #endif
icraggs 43:21da1f744243 873
icraggs 43:21da1f744243 874 exit:
icraggs 43:21da1f744243 875 if (rc != SUCCESS)
icraggs 46:e335fcc1a663 876 cleanSession();
icraggs 43:21da1f744243 877 return rc;
icraggs 43:21da1f744243 878 }
icraggs 43:21da1f744243 879
icraggs 43:21da1f744243 880
icraggs 43:21da1f744243 881
icraggs 43:21da1f744243 882 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 883 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, void* payload, size_t payloadlen, unsigned short& id, enum QoS qos, bool retained)
icraggs 43:21da1f744243 884 {
icraggs 43:21da1f744243 885 int rc = FAILURE;
icraggs 46:e335fcc1a663 886 Timer timer(command_timeout_ms);
icraggs 43:21da1f744243 887 MQTTString topicString = MQTTString_initializer;
icraggs 43:21da1f744243 888 int len = 0;
icraggs 43:21da1f744243 889
DuyLionTran 56:30f7b745eaaa 890 if (!isconnected) {
icraggs 43:21da1f744243 891 goto exit;
DuyLionTran 56:30f7b745eaaa 892 }
icraggs 43:21da1f744243 893 topicString.cstring = (char*)topicName;
icraggs 43:21da1f744243 894
icraggs 43:21da1f744243 895 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 896 if (qos == QOS1 || qos == QOS2)
icraggs 43:21da1f744243 897 id = packetid.getNext();
icraggs 43:21da1f744243 898 #endif
icraggs 43:21da1f744243 899 len = MQTTSerialize_publish(sendbuf, MAX_MQTT_PACKET_SIZE, 0, qos, retained, id,
icraggs 43:21da1f744243 900 topicString, (unsigned char*)payload, payloadlen);
icraggs 43:21da1f744243 901 if (len <= 0)
icraggs 43:21da1f744243 902 goto exit;
icraggs 46:e335fcc1a663 903
icraggs 43:21da1f744243 904 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 905 if (!cleansession)
icraggs 43:21da1f744243 906 {
icraggs 43:21da1f744243 907 memcpy(pubbuf, sendbuf, len);
icraggs 43:21da1f744243 908 inflightMsgid = id;
icraggs 43:21da1f744243 909 inflightLen = len;
icraggs 43:21da1f744243 910 inflightQoS = qos;
icraggs 43:21da1f744243 911 #if MQTTCLIENT_QOS2
icraggs 43:21da1f744243 912 pubrel = false;
icraggs 43:21da1f744243 913 #endif
icraggs 43:21da1f744243 914 }
icraggs 43:21da1f744243 915 #endif
icraggs 46:e335fcc1a663 916
icraggs 43:21da1f744243 917 rc = publish(len, timer, qos);
icraggs 15:64a57183aa03 918 exit:
icraggs 8:c46930bd6c82 919 return rc;
icraggs 8:c46930bd6c82 920 }
icraggs 8:c46930bd6c82 921
icraggs 8:c46930bd6c82 922
icraggs 43:21da1f744243 923 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 924 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, void* payload, size_t payloadlen, enum QoS qos, bool retained)
icraggs 43:21da1f744243 925 {
icraggs 43:21da1f744243 926 unsigned short id = 0; // dummy - not used for anything
icraggs 43:21da1f744243 927 return publish(topicName, payload, payloadlen, id, qos, retained);
icraggs 43:21da1f744243 928 }
icraggs 43:21da1f744243 929
icraggs 43:21da1f744243 930
icraggs 43:21da1f744243 931 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 932 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message& message)
icraggs 43:21da1f744243 933 {
icraggs 43:21da1f744243 934 return publish(topicName, message.payload, message.payloadlen, message.qos, message.retained);
icraggs 43:21da1f744243 935 }
icraggs 43:21da1f744243 936
icraggs 43:21da1f744243 937
icraggs 43:21da1f744243 938 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 939 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
icraggs 43:21da1f744243 940 {
icraggs 26:2658bb87c53d 941 int rc = FAILURE;
icraggs 46:e335fcc1a663 942 Timer timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete
icraggs 44:c299463ae853 943 int len = MQTTSerialize_disconnect(sendbuf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 944 if (len > 0)
icraggs 26:2658bb87c53d 945 rc = sendPacket(len, timer); // send the disconnect packet
icraggs 43:21da1f744243 946
icraggs 46:e335fcc1a663 947 if (cleansession)
icraggs 46:e335fcc1a663 948 cleanSession();
icraggs 46:e335fcc1a663 949 else
icraggs 46:e335fcc1a663 950 isconnected = false;
icraggs 23:05fc7de97d4a 951 return rc;
icraggs 20:cad3d54d7ecf 952 }
icraggs 20:cad3d54d7ecf 953
icraggs 20:cad3d54d7ecf 954
icraggs 46:e335fcc1a663 955 #endif