Better timing

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
icraggs
Date:
Mon Oct 06 11:41:05 2014 +0000
Revision:
44:c299463ae853
Parent:
43:21da1f744243
Child:
46:e335fcc1a663
Fix buf reference in disconnect

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