An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   Cellular_HelloMQTT IoTStarterKit GSwifiInterface_HelloMQTT IBMIoTClientEthernetExample ... more

This library is part of the EclipseTM Paho project; specifically the embedded client.

The goals of this API are:

  1. to be independent of any system library: hence templates parameters for networking, timer and threading classes
  2. not to rely on heap storage, only automatic (I think this is a good thing)
  3. to limit memory use, for instance by defining the size of the buffers and arrays used at object creation time
Committer:
Ian Craggs
Date:
Thu Nov 02 12:12:41 2017 +0000
Revision:
59:9cff7b6bbd01
Parent:
57:3513ee54ebb4
MQTTPacket library merge

Who changed what in which revision?

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