MQTT library (clone of https://os.mbed.com/teams/mqtt/code/MQTT/)

Dependencies:   FP MQTTPacket

Dependents:   NuMaker-mbed-AWS-IoT-example NuMaker-mbed-OS-6-AWS-IoT-example

Committer:
icraggs
Date:
Tue Aug 18 09:57:19 2015 +0000
Revision:
46:e335fcc1a663
Parent:
44:c299463ae853
Child:
53:15b5a280d22d
Latest update from Paho

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
icraggs 26:2658bb87c53d 339 break;
icraggs 26:2658bb87c53d 340 sent += rc;
icraggs 23:05fc7de97d4a 341 }
icraggs 20:cad3d54d7ecf 342 if (sent == length)
icraggs 23:05fc7de97d4a 343 {
icraggs 43:21da1f744243 344 if (this->keepAliveInterval > 0)
icraggs 43:21da1f744243 345 last_sent.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet
icraggs 23:05fc7de97d4a 346 rc = SUCCESS;
icraggs 23:05fc7de97d4a 347 }
icraggs 23:05fc7de97d4a 348 else
icraggs 23:05fc7de97d4a 349 rc = FAILURE;
icraggs 43:21da1f744243 350
icraggs 43:21da1f744243 351 #if defined(MQTT_DEBUG)
icraggs 46:e335fcc1a663 352 char printbuf[150];
icraggs 46:e335fcc1a663 353 DEBUG("Rc %d from sending packet %s\n", rc, MQTTFormat_toServerString(printbuf, sizeof(printbuf), sendbuf, length));
icraggs 43:21da1f744243 354 #endif
icraggs 23:05fc7de97d4a 355 return rc;
icraggs 8:c46930bd6c82 356 }
icraggs 8:c46930bd6c82 357
icraggs 8:c46930bd6c82 358
icraggs 43:21da1f744243 359 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 360 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
icraggs 8:c46930bd6c82 361 {
icraggs 36:2f1ada427e56 362 unsigned char c;
icraggs 8:c46930bd6c82 363 int multiplier = 1;
icraggs 8:c46930bd6c82 364 int len = 0;
icraggs 20:cad3d54d7ecf 365 const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
icraggs 8:c46930bd6c82 366
icraggs 8:c46930bd6c82 367 *value = 0;
icraggs 8:c46930bd6c82 368 do
icraggs 8:c46930bd6c82 369 {
icraggs 8:c46930bd6c82 370 int rc = MQTTPACKET_READ_ERROR;
icraggs 8:c46930bd6c82 371
icraggs 8:c46930bd6c82 372 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
icraggs 8:c46930bd6c82 373 {
icraggs 8:c46930bd6c82 374 rc = MQTTPACKET_READ_ERROR; /* bad data */
icraggs 8:c46930bd6c82 375 goto exit;
icraggs 8:c46930bd6c82 376 }
icraggs 23:05fc7de97d4a 377 rc = ipstack.read(&c, 1, timeout);
icraggs 8:c46930bd6c82 378 if (rc != 1)
icraggs 8:c46930bd6c82 379 goto exit;
icraggs 8:c46930bd6c82 380 *value += (c & 127) * multiplier;
icraggs 8:c46930bd6c82 381 multiplier *= 128;
icraggs 8:c46930bd6c82 382 } while ((c & 128) != 0);
icraggs 8:c46930bd6c82 383 exit:
icraggs 8:c46930bd6c82 384 return len;
icraggs 8:c46930bd6c82 385 }
icraggs 8:c46930bd6c82 386
icraggs 8:c46930bd6c82 387
icraggs 8:c46930bd6c82 388 /**
icraggs 8:c46930bd6c82 389 * If any read fails in this method, then we should disconnect from the network, as on reconnect
icraggs 43:21da1f744243 390 * the packets can be retried.
icraggs 8:c46930bd6c82 391 * @param timeout the max time to wait for the packet read to complete, in milliseconds
icraggs 8:c46930bd6c82 392 * @return the MQTT packet type, or -1 if none
icraggs 8:c46930bd6c82 393 */
icraggs 46:e335fcc1a663 394 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 46:e335fcc1a663 395 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::readPacket(Timer& timer)
icraggs 8:c46930bd6c82 396 {
icraggs 26:2658bb87c53d 397 int rc = FAILURE;
icraggs 8:c46930bd6c82 398 MQTTHeader header = {0};
icraggs 8:c46930bd6c82 399 int len = 0;
icraggs 8:c46930bd6c82 400 int rem_len = 0;
icraggs 8:c46930bd6c82 401
icraggs 8:c46930bd6c82 402 /* 1. read the header byte. This has the packet type in it */
icraggs 23:05fc7de97d4a 403 if (ipstack.read(readbuf, 1, timer.left_ms()) != 1)
icraggs 8:c46930bd6c82 404 goto exit;
icraggs 8:c46930bd6c82 405
icraggs 8:c46930bd6c82 406 len = 1;
icraggs 8:c46930bd6c82 407 /* 2. read the remaining length. This is variable in itself */
icraggs 20:cad3d54d7ecf 408 decodePacket(&rem_len, timer.left_ms());
icraggs 43:21da1f744243 409 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length into the buffer */
icraggs 8:c46930bd6c82 410
icraggs 46:e335fcc1a663 411 if (rem_len > (MAX_MQTT_PACKET_SIZE - len))
icraggs 46:e335fcc1a663 412 {
icraggs 46:e335fcc1a663 413 rc = BUFFER_OVERFLOW;
icraggs 46:e335fcc1a663 414 goto exit;
icraggs 46:e335fcc1a663 415 }
icraggs 46:e335fcc1a663 416
icraggs 8:c46930bd6c82 417 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
icraggs 43:21da1f744243 418 if (rem_len > 0 && (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len))
icraggs 8:c46930bd6c82 419 goto exit;
icraggs 8:c46930bd6c82 420
icraggs 8:c46930bd6c82 421 header.byte = readbuf[0];
icraggs 8:c46930bd6c82 422 rc = header.bits.type;
icraggs 43:21da1f744243 423 if (this->keepAliveInterval > 0)
icraggs 43:21da1f744243 424 last_received.countdown(this->keepAliveInterval); // record the fact that we have successfully received a packet
icraggs 8:c46930bd6c82 425 exit:
icraggs 43:21da1f744243 426
icraggs 43:21da1f744243 427 #if defined(MQTT_DEBUG)
icraggs 46:e335fcc1a663 428 if (rc >= 0)
icraggs 46:e335fcc1a663 429 {
icraggs 46:e335fcc1a663 430 char printbuf[50];
icraggs 46:e335fcc1a663 431 DEBUG("Rc %d from receiving packet %s\n", rc, MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len));
icraggs 46:e335fcc1a663 432 }
icraggs 43:21da1f744243 433 #endif
icraggs 8:c46930bd6c82 434 return rc;
icraggs 3:dbff6b768d28 435 }
icraggs 3:dbff6b768d28 436
icraggs 8:c46930bd6c82 437
icraggs 26:2658bb87c53d 438 // assume topic filter and name is in correct format
icraggs 26:2658bb87c53d 439 // # can only be at end
icraggs 26:2658bb87c53d 440 // + and # can only be next to separator
icraggs 43:21da1f744243 441 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 442 bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName)
icraggs 26:2658bb87c53d 443 {
icraggs 26:2658bb87c53d 444 char* curf = topicFilter;
icraggs 26:2658bb87c53d 445 char* curn = topicName.lenstring.data;
icraggs 26:2658bb87c53d 446 char* curn_end = curn + topicName.lenstring.len;
icraggs 43:21da1f744243 447
icraggs 26:2658bb87c53d 448 while (*curf && curn < curn_end)
icraggs 26:2658bb87c53d 449 {
icraggs 26:2658bb87c53d 450 if (*curn == '/' && *curf != '/')
icraggs 26:2658bb87c53d 451 break;
icraggs 26:2658bb87c53d 452 if (*curf != '+' && *curf != '#' && *curf != *curn)
icraggs 26:2658bb87c53d 453 break;
icraggs 26:2658bb87c53d 454 if (*curf == '+')
icraggs 26:2658bb87c53d 455 { // skip until we meet the next separator, or end of string
icraggs 26:2658bb87c53d 456 char* nextpos = curn + 1;
icraggs 26:2658bb87c53d 457 while (nextpos < curn_end && *nextpos != '/')
icraggs 26:2658bb87c53d 458 nextpos = ++curn + 1;
icraggs 26:2658bb87c53d 459 }
icraggs 26:2658bb87c53d 460 else if (*curf == '#')
icraggs 26:2658bb87c53d 461 curn = curn_end - 1; // skip until end of string
icraggs 26:2658bb87c53d 462 curf++;
icraggs 26:2658bb87c53d 463 curn++;
icraggs 26:2658bb87c53d 464 };
icraggs 43:21da1f744243 465
icraggs 26:2658bb87c53d 466 return (curn == curn_end) && (*curf == '\0');
icraggs 26:2658bb87c53d 467 }
icraggs 26:2658bb87c53d 468
icraggs 26:2658bb87c53d 469
icraggs 26:2658bb87c53d 470
icraggs 43:21da1f744243 471 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 26:2658bb87c53d 472 int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message)
icraggs 15:64a57183aa03 473 {
icraggs 26:2658bb87c53d 474 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 475
icraggs 20:cad3d54d7ecf 476 // we have to find the right message handler - indexed by topic
icraggs 23:05fc7de97d4a 477 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 20:cad3d54d7ecf 478 {
icraggs 26:2658bb87c53d 479 if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) ||
icraggs 26:2658bb87c53d 480 isTopicMatched((char*)messageHandlers[i].topicFilter, topicName)))
icraggs 20:cad3d54d7ecf 481 {
icraggs 26:2658bb87c53d 482 if (messageHandlers[i].fp.attached())
icraggs 26:2658bb87c53d 483 {
icraggs 31:a51dd239b78e 484 MessageData md(topicName, message);
icraggs 31:a51dd239b78e 485 messageHandlers[i].fp(md);
icraggs 26:2658bb87c53d 486 rc = SUCCESS;
icraggs 26:2658bb87c53d 487 }
icraggs 20:cad3d54d7ecf 488 }
icraggs 20:cad3d54d7ecf 489 }
icraggs 43:21da1f744243 490
icraggs 43:21da1f744243 491 if (rc == FAILURE && defaultMessageHandler.attached())
icraggs 26:2658bb87c53d 492 {
icraggs 31:a51dd239b78e 493 MessageData md(topicName, message);
icraggs 31:a51dd239b78e 494 defaultMessageHandler(md);
icraggs 26:2658bb87c53d 495 rc = SUCCESS;
icraggs 43:21da1f744243 496 }
icraggs 43:21da1f744243 497
icraggs 20:cad3d54d7ecf 498 return rc;
icraggs 15:64a57183aa03 499 }
icraggs 15:64a57183aa03 500
icraggs 15:64a57183aa03 501
icraggs 20:cad3d54d7ecf 502
icraggs 43:21da1f744243 503 template<class Network, class Timer, int a, int b>
icraggs 43:21da1f744243 504 int MQTT::Client<Network, Timer, a, b>::yield(unsigned long timeout_ms)
icraggs 20:cad3d54d7ecf 505 {
icraggs 26:2658bb87c53d 506 int rc = SUCCESS;
icraggs 20:cad3d54d7ecf 507 Timer timer = Timer();
icraggs 43:21da1f744243 508
icraggs 23:05fc7de97d4a 509 timer.countdown_ms(timeout_ms);
icraggs 20:cad3d54d7ecf 510 while (!timer.expired())
icraggs 26:2658bb87c53d 511 {
icraggs 46:e335fcc1a663 512 if (cycle(timer) < 0)
icraggs 26:2658bb87c53d 513 {
icraggs 26:2658bb87c53d 514 rc = FAILURE;
icraggs 26:2658bb87c53d 515 break;
icraggs 26:2658bb87c53d 516 }
icraggs 26:2658bb87c53d 517 }
icraggs 43:21da1f744243 518
icraggs 26:2658bb87c53d 519 return rc;
icraggs 20:cad3d54d7ecf 520 }
icraggs 20:cad3d54d7ecf 521
icraggs 20:cad3d54d7ecf 522
icraggs 43:21da1f744243 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>::cycle(Timer& timer)
icraggs 8:c46930bd6c82 525 {
icraggs 8:c46930bd6c82 526 /* get one piece of work off the wire and one pass through */
icraggs 20:cad3d54d7ecf 527
Ian Craggs 12:cc7f2d62a393 528 // read the socket, see what work is due
icraggs 46:e335fcc1a663 529 int packet_type = readPacket(timer);
icraggs 43:21da1f744243 530
icraggs 26:2658bb87c53d 531 int len = 0,
icraggs 26:2658bb87c53d 532 rc = SUCCESS;
icraggs 28:8b2abe9bd814 533
icraggs 8:c46930bd6c82 534 switch (packet_type)
icraggs 8:c46930bd6c82 535 {
icraggs 46:e335fcc1a663 536 case FAILURE:
icraggs 46:e335fcc1a663 537 case BUFFER_OVERFLOW:
icraggs 46:e335fcc1a663 538 rc = packet_type;
icraggs 46:e335fcc1a663 539 break;
icraggs 15:64a57183aa03 540 case CONNACK:
icraggs 8:c46930bd6c82 541 case PUBACK:
icraggs 8:c46930bd6c82 542 case SUBACK:
icraggs 8:c46930bd6c82 543 break;
icraggs 15:64a57183aa03 544 case PUBLISH:
icraggs 46:e335fcc1a663 545 {
icraggs 46:e335fcc1a663 546 MQTTString topicName = MQTTString_initializer;
icraggs 20:cad3d54d7ecf 547 Message msg;
icraggs 46:e335fcc1a663 548 int intQoS;
icraggs 46:e335fcc1a663 549 if (MQTTDeserialize_publish((unsigned char*)&msg.dup, &intQoS, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
icraggs 36:2f1ada427e56 550 (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 551 goto exit;
icraggs 46:e335fcc1a663 552 msg.qos = (enum QoS)intQoS;
icraggs 43:21da1f744243 553 #if MQTTCLIENT_QOS2
icraggs 43:21da1f744243 554 if (msg.qos != QOS2)
icraggs 43:21da1f744243 555 #endif
icraggs 31:a51dd239b78e 556 deliverMessage(topicName, msg);
icraggs 43:21da1f744243 557 #if MQTTCLIENT_QOS2
icraggs 31:a51dd239b78e 558 else if (isQoS2msgidFree(msg.id))
icraggs 31:a51dd239b78e 559 {
icraggs 43:21da1f744243 560 if (useQoS2msgid(msg.id))
icraggs 43:21da1f744243 561 deliverMessage(topicName, msg);
icraggs 43:21da1f744243 562 else
icraggs 43:21da1f744243 563 WARN("Maximum number of incoming QoS2 messages exceeded");
icraggs 46:e335fcc1a663 564 }
icraggs 31:a51dd239b78e 565 #endif
icraggs 43:21da1f744243 566 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 20:cad3d54d7ecf 567 if (msg.qos != QOS0)
icraggs 20:cad3d54d7ecf 568 {
icraggs 20:cad3d54d7ecf 569 if (msg.qos == QOS1)
icraggs 43:21da1f744243 570 len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id);
icraggs 20:cad3d54d7ecf 571 else if (msg.qos == QOS2)
icraggs 43:21da1f744243 572 len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id);
icraggs 26:2658bb87c53d 573 if (len <= 0)
icraggs 26:2658bb87c53d 574 rc = FAILURE;
icraggs 26:2658bb87c53d 575 else
icraggs 26:2658bb87c53d 576 rc = sendPacket(len, timer);
icraggs 26:2658bb87c53d 577 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 578 goto exit; // there was a problem
icraggs 20:cad3d54d7ecf 579 }
Ian Craggs 12:cc7f2d62a393 580 break;
icraggs 43:21da1f744243 581 #endif
icraggs 46:e335fcc1a663 582 }
icraggs 43:21da1f744243 583 #if MQTTCLIENT_QOS2
icraggs 15:64a57183aa03 584 case PUBREC:
icraggs 46:e335fcc1a663 585 case PUBREL:
icraggs 36:2f1ada427e56 586 unsigned short mypacketid;
icraggs 36:2f1ada427e56 587 unsigned char dup, type;
icraggs 26:2658bb87c53d 588 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 589 rc = FAILURE;
icraggs 46:e335fcc1a663 590 else if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE,
icraggs 46:e335fcc1a663 591 (packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0)
icraggs 26:2658bb87c53d 592 rc = FAILURE;
icraggs 26:2658bb87c53d 593 else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet
icraggs 26:2658bb87c53d 594 rc = FAILURE; // there was a problem
icraggs 26:2658bb87c53d 595 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 596 goto exit; // there was a problem
icraggs 46:e335fcc1a663 597 if (packet_type == PUBREL)
icraggs 46:e335fcc1a663 598 freeQoS2msgid(mypacketid);
icraggs 8:c46930bd6c82 599 break;
icraggs 46:e335fcc1a663 600
icraggs 8:c46930bd6c82 601 case PUBCOMP:
icraggs 8:c46930bd6c82 602 break;
icraggs 43:21da1f744243 603 #endif
icraggs 15:64a57183aa03 604 case PINGRESP:
icraggs 20:cad3d54d7ecf 605 ping_outstanding = false;
icraggs 8:c46930bd6c82 606 break;
icraggs 15:64a57183aa03 607 }
icraggs 20:cad3d54d7ecf 608 keepalive();
Ian Craggs 12:cc7f2d62a393 609 exit:
icraggs 26:2658bb87c53d 610 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 611 rc = packet_type;
icraggs 26:2658bb87c53d 612 return rc;
icraggs 15:64a57183aa03 613 }
icraggs 15:64a57183aa03 614
icraggs 15:64a57183aa03 615
icraggs 23:05fc7de97d4a 616 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 617 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive()
icraggs 15:64a57183aa03 618 {
icraggs 26:2658bb87c53d 619 int rc = FAILURE;
icraggs 15:64a57183aa03 620
icraggs 20:cad3d54d7ecf 621 if (keepAliveInterval == 0)
icraggs 26:2658bb87c53d 622 {
icraggs 26:2658bb87c53d 623 rc = SUCCESS;
icraggs 20:cad3d54d7ecf 624 goto exit;
icraggs 26:2658bb87c53d 625 }
icraggs 15:64a57183aa03 626
icraggs 43:21da1f744243 627 if (last_sent.expired() || last_received.expired())
icraggs 20:cad3d54d7ecf 628 {
icraggs 26:2658bb87c53d 629 if (!ping_outstanding)
icraggs 20:cad3d54d7ecf 630 {
icraggs 46:e335fcc1a663 631 Timer timer(1000);
icraggs 43:21da1f744243 632 int len = MQTTSerialize_pingreq(sendbuf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 633 if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet
icraggs 20:cad3d54d7ecf 634 ping_outstanding = true;
icraggs 20:cad3d54d7ecf 635 }
icraggs 20:cad3d54d7ecf 636 }
icraggs 15:64a57183aa03 637
icraggs 15:64a57183aa03 638 exit:
icraggs 20:cad3d54d7ecf 639 return rc;
icraggs 8:c46930bd6c82 640 }
icraggs 8:c46930bd6c82 641
icraggs 8:c46930bd6c82 642
icraggs 16:91c2f9a144d4 643 // only used in single-threaded mode where one command at a time is in process
icraggs 43:21da1f744243 644 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 645 int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer)
icraggs 15:64a57183aa03 646 {
icraggs 26:2658bb87c53d 647 int rc = FAILURE;
icraggs 43:21da1f744243 648
icraggs 20:cad3d54d7ecf 649 do
icraggs 16:91c2f9a144d4 650 {
icraggs 43:21da1f744243 651 if (timer.expired())
icraggs 20:cad3d54d7ecf 652 break; // we timed out
icraggs 20:cad3d54d7ecf 653 }
icraggs 43:21da1f744243 654 while ((rc = cycle(timer)) != packet_type);
icraggs 43:21da1f744243 655
icraggs 20:cad3d54d7ecf 656 return rc;
icraggs 16:91c2f9a144d4 657 }
icraggs 16:91c2f9a144d4 658
icraggs 16:91c2f9a144d4 659
icraggs 43:21da1f744243 660 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 661 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData& options)
icraggs 16:91c2f9a144d4 662 {
icraggs 46:e335fcc1a663 663 Timer connect_timer(command_timeout_ms);
icraggs 26:2658bb87c53d 664 int rc = FAILURE;
icraggs 31:a51dd239b78e 665 int len = 0;
icraggs 43:21da1f744243 666
icraggs 31:a51dd239b78e 667 if (isconnected) // don't send connect packet again if we are already connected
icraggs 31:a51dd239b78e 668 goto exit;
icraggs 8:c46930bd6c82 669
icraggs 43:21da1f744243 670 this->keepAliveInterval = options.keepAliveInterval;
icraggs 43:21da1f744243 671 this->cleansession = options.cleansession;
icraggs 43:21da1f744243 672 if ((len = MQTTSerialize_connect(sendbuf, MAX_MQTT_PACKET_SIZE, &options)) <= 0)
icraggs 26:2658bb87c53d 673 goto exit;
icraggs 26:2658bb87c53d 674 if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet
icraggs 20:cad3d54d7ecf 675 goto exit; // there was a problem
icraggs 43:21da1f744243 676
icraggs 43:21da1f744243 677 if (this->keepAliveInterval > 0)
icraggs 43:21da1f744243 678 last_received.countdown(this->keepAliveInterval);
icraggs 20:cad3d54d7ecf 679 // this will be a blocking call, wait for the connack
icraggs 20:cad3d54d7ecf 680 if (waitfor(CONNACK, connect_timer) == CONNACK)
icraggs 15:64a57183aa03 681 {
icraggs 36:2f1ada427e56 682 unsigned char connack_rc = 255;
icraggs 37:e3d64f9b986c 683 bool sessionPresent = false;
icraggs 37:e3d64f9b986c 684 if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 685 rc = connack_rc;
icraggs 26:2658bb87c53d 686 else
icraggs 26:2658bb87c53d 687 rc = FAILURE;
icraggs 8:c46930bd6c82 688 }
icraggs 26:2658bb87c53d 689 else
icraggs 26:2658bb87c53d 690 rc = FAILURE;
icraggs 46:e335fcc1a663 691
icraggs 43:21da1f744243 692 #if MQTTCLIENT_QOS2
icraggs 46:e335fcc1a663 693 // resend any inflight publish
icraggs 46:e335fcc1a663 694 if (inflightMsgid > 0 && inflightQoS == QOS2 && pubrel)
icraggs 43:21da1f744243 695 {
icraggs 43:21da1f744243 696 if ((len = MQTTSerialize_ack(sendbuf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, inflightMsgid)) <= 0)
icraggs 43:21da1f744243 697 rc = FAILURE;
icraggs 43:21da1f744243 698 else
icraggs 43:21da1f744243 699 rc = publish(len, connect_timer, inflightQoS);
icraggs 43:21da1f744243 700 }
icraggs 43:21da1f744243 701 else
icraggs 43:21da1f744243 702 #endif
icraggs 43:21da1f744243 703 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 704 if (inflightMsgid > 0)
icraggs 43:21da1f744243 705 {
icraggs 43:21da1f744243 706 memcpy(sendbuf, pubbuf, MAX_MQTT_PACKET_SIZE);
icraggs 43:21da1f744243 707 rc = publish(inflightLen, connect_timer, inflightQoS);
icraggs 43:21da1f744243 708 }
icraggs 43:21da1f744243 709 #endif
icraggs 43:21da1f744243 710
icraggs 15:64a57183aa03 711 exit:
icraggs 26:2658bb87c53d 712 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 713 isconnected = true;
icraggs 8:c46930bd6c82 714 return rc;
icraggs 8:c46930bd6c82 715 }
icraggs 8:c46930bd6c82 716
icraggs 8:c46930bd6c82 717
icraggs 43:21da1f744243 718 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 719 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect()
icraggs 43:21da1f744243 720 {
icraggs 43:21da1f744243 721 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
icraggs 43:21da1f744243 722 return connect(default_options);
icraggs 43:21da1f744243 723 }
icraggs 43:21da1f744243 724
icraggs 43:21da1f744243 725
icraggs 43:21da1f744243 726 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 727 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler)
icraggs 43:21da1f744243 728 {
icraggs 43:21da1f744243 729 int rc = FAILURE;
icraggs 46:e335fcc1a663 730 Timer timer(command_timeout_ms);
icraggs 26:2658bb87c53d 731 int len = 0;
icraggs 46:e335fcc1a663 732 MQTTString topic = {(char*)topicFilter, {0, 0}};
icraggs 43:21da1f744243 733
icraggs 26:2658bb87c53d 734 if (!isconnected)
icraggs 20:cad3d54d7ecf 735 goto exit;
icraggs 43:21da1f744243 736
icraggs 43:21da1f744243 737 len = MQTTSerialize_subscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos);
icraggs 26:2658bb87c53d 738 if (len <= 0)
icraggs 26:2658bb87c53d 739 goto exit;
icraggs 23:05fc7de97d4a 740 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 26:2658bb87c53d 741 goto exit; // there was a problem
icraggs 43:21da1f744243 742
icraggs 43:21da1f744243 743 if (waitfor(SUBACK, timer) == SUBACK) // wait for suback
icraggs 8:c46930bd6c82 744 {
icraggs 36:2f1ada427e56 745 int count = 0, grantedQoS = -1;
icraggs 36:2f1ada427e56 746 unsigned short mypacketid;
icraggs 23:05fc7de97d4a 747 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 43:21da1f744243 748 rc = grantedQoS; // 0, 1, 2 or 0x80
icraggs 20:cad3d54d7ecf 749 if (rc != 0x80)
icraggs 8:c46930bd6c82 750 {
icraggs 23:05fc7de97d4a 751 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 16:91c2f9a144d4 752 {
icraggs 26:2658bb87c53d 753 if (messageHandlers[i].topicFilter == 0)
icraggs 20:cad3d54d7ecf 754 {
icraggs 26:2658bb87c53d 755 messageHandlers[i].topicFilter = topicFilter;
icraggs 20:cad3d54d7ecf 756 messageHandlers[i].fp.attach(messageHandler);
icraggs 20:cad3d54d7ecf 757 rc = 0;
icraggs 20:cad3d54d7ecf 758 break;
icraggs 20:cad3d54d7ecf 759 }
icraggs 16:91c2f9a144d4 760 }
icraggs 8:c46930bd6c82 761 }
icraggs 8:c46930bd6c82 762 }
icraggs 43:21da1f744243 763 else
icraggs 26:2658bb87c53d 764 rc = FAILURE;
icraggs 43:21da1f744243 765
icraggs 15:64a57183aa03 766 exit:
icraggs 43:21da1f744243 767 if (rc != SUCCESS)
icraggs 46:e335fcc1a663 768 cleanSession();
Ian Craggs 12:cc7f2d62a393 769 return rc;
icraggs 15:64a57183aa03 770 }
icraggs 15:64a57183aa03 771
icraggs 15:64a57183aa03 772
icraggs 43:21da1f744243 773 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 774 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter)
icraggs 43:21da1f744243 775 {
icraggs 26:2658bb87c53d 776 int rc = FAILURE;
icraggs 46:e335fcc1a663 777 Timer timer(command_timeout_ms);
icraggs 46:e335fcc1a663 778 MQTTString topic = {(char*)topicFilter, {0, 0}};
icraggs 31:a51dd239b78e 779 int len = 0;
icraggs 43:21da1f744243 780
icraggs 31:a51dd239b78e 781 if (!isconnected)
icraggs 31:a51dd239b78e 782 goto exit;
icraggs 43:21da1f744243 783
icraggs 43:21da1f744243 784 if ((len = MQTTSerialize_unsubscribe(sendbuf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0)
icraggs 20:cad3d54d7ecf 785 goto exit;
icraggs 43:21da1f744243 786 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the unsubscribe packet
icraggs 20:cad3d54d7ecf 787 goto exit; // there was a problem
icraggs 43:21da1f744243 788
icraggs 20:cad3d54d7ecf 789 if (waitfor(UNSUBACK, timer) == UNSUBACK)
Ian Craggs 12:cc7f2d62a393 790 {
icraggs 36:2f1ada427e56 791 unsigned short mypacketid; // should be the same as the packetid above
icraggs 23:05fc7de97d4a 792 if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 46:e335fcc1a663 793 {
icraggs 43:21da1f744243 794 rc = 0;
icraggs 46:e335fcc1a663 795
icraggs 46:e335fcc1a663 796 // remove the subscription message handler associated with this topic, if there is one
icraggs 46:e335fcc1a663 797 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 46:e335fcc1a663 798 {
icraggs 46:e335fcc1a663 799 if (messageHandlers[i].topicFilter && strcmp(messageHandlers[i].topicFilter, topicFilter) == 0)
icraggs 46:e335fcc1a663 800 {
icraggs 46:e335fcc1a663 801 messageHandlers[i].topicFilter = 0;
icraggs 46:e335fcc1a663 802 break;
icraggs 46:e335fcc1a663 803 }
icraggs 46:e335fcc1a663 804 }
icraggs 46:e335fcc1a663 805 }
Ian Craggs 12:cc7f2d62a393 806 }
icraggs 26:2658bb87c53d 807 else
icraggs 26:2658bb87c53d 808 rc = FAILURE;
icraggs 43:21da1f744243 809
icraggs 15:64a57183aa03 810 exit:
icraggs 43:21da1f744243 811 if (rc != SUCCESS)
icraggs 46:e335fcc1a663 812 cleanSession();
Ian Craggs 12:cc7f2d62a393 813 return rc;
icraggs 15:64a57183aa03 814 }
icraggs 15:64a57183aa03 815
icraggs 15:64a57183aa03 816
icraggs 43:21da1f744243 817 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 818 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(int len, Timer& timer, enum QoS qos)
icraggs 20:cad3d54d7ecf 819 {
icraggs 43:21da1f744243 820 int rc;
icraggs 46:e335fcc1a663 821
icraggs 43:21da1f744243 822 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the publish packet
icraggs 43:21da1f744243 823 goto exit; // there was a problem
icraggs 15:64a57183aa03 824
icraggs 46:e335fcc1a663 825 #if MQTTCLIENT_QOS1
icraggs 43:21da1f744243 826 if (qos == QOS1)
Ian Craggs 12:cc7f2d62a393 827 {
icraggs 20:cad3d54d7ecf 828 if (waitfor(PUBACK, timer) == PUBACK)
icraggs 20:cad3d54d7ecf 829 {
icraggs 36:2f1ada427e56 830 unsigned short mypacketid;
icraggs 36:2f1ada427e56 831 unsigned char dup, type;
icraggs 26:2658bb87c53d 832 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 833 rc = FAILURE;
icraggs 43:21da1f744243 834 else if (inflightMsgid == mypacketid)
icraggs 43:21da1f744243 835 inflightMsgid = 0;
icraggs 20:cad3d54d7ecf 836 }
icraggs 26:2658bb87c53d 837 else
icraggs 26:2658bb87c53d 838 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 839 }
icraggs 43:21da1f744243 840 #elif MQTTCLIENT_QOS2
icraggs 43:21da1f744243 841 else if (qos == QOS2)
Ian Craggs 12:cc7f2d62a393 842 {
icraggs 20:cad3d54d7ecf 843 if (waitfor(PUBCOMP, timer) == PUBCOMP)
icraggs 20:cad3d54d7ecf 844 {
icraggs 36:2f1ada427e56 845 unsigned short mypacketid;
icraggs 36:2f1ada427e56 846 unsigned char dup, type;
icraggs 26:2658bb87c53d 847 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 848 rc = FAILURE;
icraggs 43:21da1f744243 849 else if (inflightMsgid == mypacketid)
icraggs 43:21da1f744243 850 inflightMsgid = 0;
icraggs 20:cad3d54d7ecf 851 }
icraggs 26:2658bb87c53d 852 else
icraggs 26:2658bb87c53d 853 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 854 }
icraggs 43:21da1f744243 855 #endif
icraggs 43:21da1f744243 856
icraggs 43:21da1f744243 857 exit:
icraggs 43:21da1f744243 858 if (rc != SUCCESS)
icraggs 46:e335fcc1a663 859 cleanSession();
icraggs 43:21da1f744243 860 return rc;
icraggs 43:21da1f744243 861 }
icraggs 43:21da1f744243 862
icraggs 43:21da1f744243 863
icraggs 43:21da1f744243 864
icraggs 43:21da1f744243 865 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 866 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 867 {
icraggs 43:21da1f744243 868 int rc = FAILURE;
icraggs 46:e335fcc1a663 869 Timer timer(command_timeout_ms);
icraggs 43:21da1f744243 870 MQTTString topicString = MQTTString_initializer;
icraggs 43:21da1f744243 871 int len = 0;
icraggs 43:21da1f744243 872
icraggs 43:21da1f744243 873 if (!isconnected)
icraggs 43:21da1f744243 874 goto exit;
icraggs 46:e335fcc1a663 875
icraggs 43:21da1f744243 876 topicString.cstring = (char*)topicName;
icraggs 43:21da1f744243 877
icraggs 43:21da1f744243 878 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 879 if (qos == QOS1 || qos == QOS2)
icraggs 43:21da1f744243 880 id = packetid.getNext();
icraggs 43:21da1f744243 881 #endif
icraggs 43:21da1f744243 882
icraggs 43:21da1f744243 883 len = MQTTSerialize_publish(sendbuf, MAX_MQTT_PACKET_SIZE, 0, qos, retained, id,
icraggs 43:21da1f744243 884 topicString, (unsigned char*)payload, payloadlen);
icraggs 43:21da1f744243 885 if (len <= 0)
icraggs 43:21da1f744243 886 goto exit;
icraggs 46:e335fcc1a663 887
icraggs 43:21da1f744243 888 #if MQTTCLIENT_QOS1 || MQTTCLIENT_QOS2
icraggs 43:21da1f744243 889 if (!cleansession)
icraggs 43:21da1f744243 890 {
icraggs 43:21da1f744243 891 memcpy(pubbuf, sendbuf, len);
icraggs 43:21da1f744243 892 inflightMsgid = id;
icraggs 43:21da1f744243 893 inflightLen = len;
icraggs 43:21da1f744243 894 inflightQoS = qos;
icraggs 43:21da1f744243 895 #if MQTTCLIENT_QOS2
icraggs 43:21da1f744243 896 pubrel = false;
icraggs 43:21da1f744243 897 #endif
icraggs 43:21da1f744243 898 }
icraggs 43:21da1f744243 899 #endif
icraggs 46:e335fcc1a663 900
icraggs 43:21da1f744243 901 rc = publish(len, timer, qos);
icraggs 15:64a57183aa03 902 exit:
icraggs 8:c46930bd6c82 903 return rc;
icraggs 8:c46930bd6c82 904 }
icraggs 8:c46930bd6c82 905
icraggs 8:c46930bd6c82 906
icraggs 43:21da1f744243 907 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 908 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 909 {
icraggs 43:21da1f744243 910 unsigned short id = 0; // dummy - not used for anything
icraggs 43:21da1f744243 911 return publish(topicName, payload, payloadlen, id, qos, retained);
icraggs 43:21da1f744243 912 }
icraggs 43:21da1f744243 913
icraggs 43:21da1f744243 914
icraggs 43:21da1f744243 915 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 43:21da1f744243 916 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message& message)
icraggs 43:21da1f744243 917 {
icraggs 43:21da1f744243 918 return publish(topicName, message.payload, message.payloadlen, message.qos, message.retained);
icraggs 43:21da1f744243 919 }
icraggs 43:21da1f744243 920
icraggs 43:21da1f744243 921
icraggs 43:21da1f744243 922 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 923 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
icraggs 43:21da1f744243 924 {
icraggs 26:2658bb87c53d 925 int rc = FAILURE;
icraggs 46:e335fcc1a663 926 Timer timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete
icraggs 44:c299463ae853 927 int len = MQTTSerialize_disconnect(sendbuf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 928 if (len > 0)
icraggs 26:2658bb87c53d 929 rc = sendPacket(len, timer); // send the disconnect packet
icraggs 43:21da1f744243 930
icraggs 46:e335fcc1a663 931 if (cleansession)
icraggs 46:e335fcc1a663 932 cleanSession();
icraggs 46:e335fcc1a663 933 else
icraggs 46:e335fcc1a663 934 isconnected = false;
icraggs 23:05fc7de97d4a 935 return rc;
icraggs 20:cad3d54d7ecf 936 }
icraggs 20:cad3d54d7ecf 937
icraggs 20:cad3d54d7ecf 938
icraggs 46:e335fcc1a663 939 #endif