Fork of MQTT library

Dependencies:   FP

Dependents:   WNCProximityMqtt

Committer:
elmkom
Date:
Tue Sep 27 15:47:27 2016 +0000
Revision:
48:326380990a95
Parent:
47:21d6fba046df
Add proximity sensor; increase mqtt buffer size to 512

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