kh7ioiuyg56yjiuy

Dependencies:   FP MQTTPacket

Fork of MQTT by Irayya Mathad

Committer:
biswajit007
Date:
Wed Jun 20 06:32:25 2018 +0000
Revision:
49:52d58ea16e09
Parent:
48:c11d59168c0a
pkjhgtyj

Who changed what in which revision?

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