MQtt to publish the data

Dependencies:   FP MQTTPacket

Fork of MQTT by James Bruce

Committer:
icraggs
Date:
Sun May 11 18:19:07 2014 +0000
Revision:
26:2658bb87c53d
Parent:
23:05fc7de97d4a
Child:
28:8b2abe9bd814
Wildcard subscription support.  Limits as template parameters.

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 *******************************************************************************/
icraggs 21:e918525e529d 16
icraggs 21:e918525e529d 17 /*
icraggs 21:e918525e529d 18
icraggs 21:e918525e529d 19 TODO:
icraggs 21:e918525e529d 20
icraggs 26:2658bb87c53d 21 ensure publish packets are retried on reconnect
icraggs 23:05fc7de97d4a 22
icraggs 21:e918525e529d 23 */
sam_grove 0:fe461e4d7afe 24
icraggs 2:dcfdd2abfe71 25 #if !defined(MQTTCLIENT_H)
icraggs 2:dcfdd2abfe71 26 #define MQTTCLIENT_H
icraggs 2:dcfdd2abfe71 27
icraggs 2:dcfdd2abfe71 28 #include "FP.h"
icraggs 3:dbff6b768d28 29 #include "MQTTPacket.h"
icraggs 6:4d312a49200b 30 #include "stdio.h"
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 23:05fc7de97d4a 38 enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
icraggs 23:05fc7de97d4a 39
icraggs 2:dcfdd2abfe71 40
icraggs 3:dbff6b768d28 41 struct Message
icraggs 2:dcfdd2abfe71 42 {
icraggs 2:dcfdd2abfe71 43 enum QoS qos;
icraggs 2:dcfdd2abfe71 44 bool retained;
icraggs 2:dcfdd2abfe71 45 bool dup;
Ian Craggs 12:cc7f2d62a393 46 unsigned short id;
icraggs 2:dcfdd2abfe71 47 void *payload;
icraggs 2:dcfdd2abfe71 48 size_t payloadlen;
sam_grove 0:fe461e4d7afe 49 };
sam_grove 0:fe461e4d7afe 50
icraggs 4:4ef00243708e 51
icraggs 20:cad3d54d7ecf 52 struct MessageData
icraggs 20:cad3d54d7ecf 53 {
icraggs 20:cad3d54d7ecf 54 struct Message message;
icraggs 20:cad3d54d7ecf 55 char* topicName;
icraggs 20:cad3d54d7ecf 56 };
icraggs 20:cad3d54d7ecf 57
icraggs 20:cad3d54d7ecf 58
icraggs 9:01b8cc7d94cc 59 class PacketId
icraggs 9:01b8cc7d94cc 60 {
icraggs 9:01b8cc7d94cc 61 public:
icraggs 23:05fc7de97d4a 62 PacketId()
icraggs 23:05fc7de97d4a 63 {
icraggs 23:05fc7de97d4a 64 next = 0;
icraggs 23:05fc7de97d4a 65 }
icraggs 9:01b8cc7d94cc 66
icraggs 23:05fc7de97d4a 67 int getNext()
icraggs 23:05fc7de97d4a 68 {
icraggs 23:05fc7de97d4a 69 return next = (next == MAX_PACKET_ID) ? 1 : ++next;
icraggs 23:05fc7de97d4a 70 }
Ian Craggs 12:cc7f2d62a393 71
icraggs 9:01b8cc7d94cc 72 private:
icraggs 9:01b8cc7d94cc 73 static const int MAX_PACKET_ID = 65535;
icraggs 9:01b8cc7d94cc 74 int next;
icraggs 9:01b8cc7d94cc 75 };
icraggs 2:dcfdd2abfe71 76
icraggs 16:91c2f9a144d4 77
icraggs 21:e918525e529d 78 /**
icraggs 21:e918525e529d 79 * @class Client
icraggs 22:aadb79d29330 80 * @brief blocking, non-threaded MQTT client API
icraggs 23:05fc7de97d4a 81 *
icraggs 23:05fc7de97d4a 82 * This version of the API blocks on all method calls, until they are complete. This means that only one
icraggs 23:05fc7de97d4a 83 * MQTT request can be in process at any one time.
icraggs 21:e918525e529d 84 * @param Network a network class which supports send, receive
icraggs 21:e918525e529d 85 * @param Timer a timer class with the methods:
icraggs 21:e918525e529d 86 */
icraggs 23:05fc7de97d4a 87 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5> class Client
icraggs 2:dcfdd2abfe71 88 {
icraggs 2:dcfdd2abfe71 89
icraggs 22:aadb79d29330 90 public:
icraggs 26:2658bb87c53d 91
icraggs 23:05fc7de97d4a 92 typedef void (*messageHandler)(Message*);
icraggs 23:05fc7de97d4a 93
icraggs 23:05fc7de97d4a 94 /** Construct the client
icraggs 23:05fc7de97d4a 95 * @param network - pointer to an instance of the Network class - must be connected to the endpoint
icraggs 23:05fc7de97d4a 96 * before calling MQTT connect
icraggs 23:05fc7de97d4a 97 * @param limits an instance of the Limit class - to alter limits as required
icraggs 23:05fc7de97d4a 98 */
icraggs 23:05fc7de97d4a 99 Client(Network& network, unsigned int command_timeout_ms = 30000);
icraggs 20:cad3d54d7ecf 100
icraggs 20:cad3d54d7ecf 101 /** Set the default message handling callback - used for any message which does not match a subscription message handler
icraggs 20:cad3d54d7ecf 102 * @param mh - pointer to the callback function
icraggs 20:cad3d54d7ecf 103 */
icraggs 20:cad3d54d7ecf 104 void setDefaultMessageHandler(messageHandler mh)
icraggs 20:cad3d54d7ecf 105 {
icraggs 20:cad3d54d7ecf 106 defaultMessageHandler.attach(mh);
icraggs 20:cad3d54d7ecf 107 }
icraggs 2:dcfdd2abfe71 108
icraggs 20:cad3d54d7ecf 109 /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
icraggs 20:cad3d54d7ecf 110 * The nework object must be connected to the network endpoint before calling this
icraggs 20:cad3d54d7ecf 111 * @param options - connect options
icraggs 20:cad3d54d7ecf 112 * @return success code -
icraggs 20:cad3d54d7ecf 113 */
icraggs 20:cad3d54d7ecf 114 int connect(MQTTPacket_connectData* options = 0);
icraggs 20:cad3d54d7ecf 115
icraggs 20:cad3d54d7ecf 116 /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
icraggs 20:cad3d54d7ecf 117 * @param topic - the topic to publish to
icraggs 20:cad3d54d7ecf 118 * @param message - the message to send
icraggs 20:cad3d54d7ecf 119 * @return success code -
icraggs 20:cad3d54d7ecf 120 */
icraggs 20:cad3d54d7ecf 121 int publish(const char* topicName, Message* message);
icraggs 20:cad3d54d7ecf 122
icraggs 20:cad3d54d7ecf 123 /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
icraggs 20:cad3d54d7ecf 124 * @param topicFilter - a topic pattern which can include wildcards
icraggs 20:cad3d54d7ecf 125 * @param qos - the MQTT QoS to subscribe at
icraggs 20:cad3d54d7ecf 126 * @param mh - the callback function to be invoked when a message is received for this subscription
icraggs 20:cad3d54d7ecf 127 * @return success code -
icraggs 20:cad3d54d7ecf 128 */
icraggs 20:cad3d54d7ecf 129 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh);
icraggs 9:01b8cc7d94cc 130
icraggs 20:cad3d54d7ecf 131 /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
icraggs 20:cad3d54d7ecf 132 * @param topicFilter - a topic pattern which can include wildcards
icraggs 20:cad3d54d7ecf 133 * @return success code -
icraggs 20:cad3d54d7ecf 134 */
icraggs 20:cad3d54d7ecf 135 int unsubscribe(const char* topicFilter);
icraggs 20:cad3d54d7ecf 136
icraggs 20:cad3d54d7ecf 137 /** MQTT Disconnect - send an MQTT disconnect packet
icraggs 20:cad3d54d7ecf 138 * @return success code -
icraggs 20:cad3d54d7ecf 139 */
icraggs 20:cad3d54d7ecf 140 int disconnect();
icraggs 8:c46930bd6c82 141
icraggs 20:cad3d54d7ecf 142 /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive
icraggs 20:cad3d54d7ecf 143 * yield can be called if no other MQTT operation is needed. This will also allow messages to be
icraggs 20:cad3d54d7ecf 144 * received.
icraggs 23:05fc7de97d4a 145 * @param timeout_ms the time to wait, in milliseconds
icraggs 26:2658bb87c53d 146 * @return success code - on failure, this means the client has disconnected
icraggs 20:cad3d54d7ecf 147 */
icraggs 26:2658bb87c53d 148 int yield(int timeout_ms = 1000);
icraggs 20:cad3d54d7ecf 149
icraggs 20:cad3d54d7ecf 150 private:
icraggs 20:cad3d54d7ecf 151
icraggs 20:cad3d54d7ecf 152 int cycle(Timer& timer);
icraggs 20:cad3d54d7ecf 153 int waitfor(int packet_type, Timer& timer);
icraggs 20:cad3d54d7ecf 154 int keepalive();
icraggs 2:dcfdd2abfe71 155
icraggs 3:dbff6b768d28 156 int decodePacket(int* value, int timeout);
icraggs 20:cad3d54d7ecf 157 int readPacket(Timer& timer);
icraggs 20:cad3d54d7ecf 158 int sendPacket(int length, Timer& timer);
icraggs 26:2658bb87c53d 159 int deliverMessage(MQTTString& topicName, Message& message);
icraggs 26:2658bb87c53d 160 bool isTopicMatched(char* topicFilter, MQTTString& topicName);
icraggs 3:dbff6b768d28 161
icraggs 23:05fc7de97d4a 162 Network& ipstack;
icraggs 23:05fc7de97d4a 163 unsigned int command_timeout_ms;
icraggs 16:91c2f9a144d4 164
icraggs 23:05fc7de97d4a 165 char buf[MAX_MQTT_PACKET_SIZE];
icraggs 23:05fc7de97d4a 166 char readbuf[MAX_MQTT_PACKET_SIZE];
icraggs 15:64a57183aa03 167
icraggs 20:cad3d54d7ecf 168 Timer ping_timer;
icraggs 15:64a57183aa03 169 unsigned int keepAliveInterval;
icraggs 20:cad3d54d7ecf 170 bool ping_outstanding;
icraggs 4:4ef00243708e 171
icraggs 9:01b8cc7d94cc 172 PacketId packetid;
icraggs 9:01b8cc7d94cc 173
icraggs 9:01b8cc7d94cc 174 typedef FP<void, Message*> messageHandlerFP;
icraggs 16:91c2f9a144d4 175 struct MessageHandlers
icraggs 15:64a57183aa03 176 {
icraggs 26:2658bb87c53d 177 const char* topicFilter;
icraggs 20:cad3d54d7ecf 178 messageHandlerFP fp;
icraggs 23:05fc7de97d4a 179 } messageHandlers[MAX_MESSAGE_HANDLERS]; // Message handlers are indexed by subscription topic
icraggs 15:64a57183aa03 180
icraggs 20:cad3d54d7ecf 181 messageHandlerFP defaultMessageHandler;
icraggs 20:cad3d54d7ecf 182
icraggs 26:2658bb87c53d 183 bool isconnected;
sam_grove 0:fe461e4d7afe 184 };
sam_grove 0:fe461e4d7afe 185
icraggs 15:64a57183aa03 186 }
icraggs 15:64a57183aa03 187
icraggs 15:64a57183aa03 188
icraggs 23:05fc7de97d4a 189 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 190 MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms) : ipstack(network), packetid()
icraggs 15:64a57183aa03 191 {
icraggs 23:05fc7de97d4a 192 ping_timer = Timer();
icraggs 23:05fc7de97d4a 193 ping_outstanding = 0;
icraggs 23:05fc7de97d4a 194 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 26:2658bb87c53d 195 messageHandlers[i].topicFilter = 0;
icraggs 26:2658bb87c53d 196 this->command_timeout_ms = command_timeout_ms;
icraggs 26:2658bb87c53d 197 isconnected = false;
icraggs 11:db15da110a37 198 }
icraggs 11:db15da110a37 199
icraggs 11:db15da110a37 200
icraggs 23:05fc7de97d4a 201 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 202 int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer)
icraggs 8:c46930bd6c82 203 {
icraggs 23:05fc7de97d4a 204 int rc = FAILURE,
icraggs 23:05fc7de97d4a 205 sent = 0;
icraggs 8:c46930bd6c82 206
icraggs 23:05fc7de97d4a 207 while (sent < length && !timer.expired())
icraggs 23:05fc7de97d4a 208 {
icraggs 23:05fc7de97d4a 209 rc = ipstack.write(&buf[sent], length, timer.left_ms());
icraggs 26:2658bb87c53d 210 if (rc < 0) // there was an error writing the data
icraggs 26:2658bb87c53d 211 break;
icraggs 26:2658bb87c53d 212 sent += rc;
icraggs 23:05fc7de97d4a 213 }
icraggs 20:cad3d54d7ecf 214 if (sent == length)
icraggs 23:05fc7de97d4a 215 {
icraggs 20:cad3d54d7ecf 216 ping_timer.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet
icraggs 23:05fc7de97d4a 217 rc = SUCCESS;
icraggs 23:05fc7de97d4a 218 }
icraggs 23:05fc7de97d4a 219 else
icraggs 23:05fc7de97d4a 220 rc = FAILURE;
icraggs 23:05fc7de97d4a 221 return rc;
icraggs 8:c46930bd6c82 222 }
icraggs 8:c46930bd6c82 223
icraggs 8:c46930bd6c82 224
icraggs 26:2658bb87c53d 225 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 226 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
icraggs 8:c46930bd6c82 227 {
icraggs 8:c46930bd6c82 228 char c;
icraggs 8:c46930bd6c82 229 int multiplier = 1;
icraggs 8:c46930bd6c82 230 int len = 0;
icraggs 20:cad3d54d7ecf 231 const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
icraggs 8:c46930bd6c82 232
icraggs 8:c46930bd6c82 233 *value = 0;
icraggs 8:c46930bd6c82 234 do
icraggs 8:c46930bd6c82 235 {
icraggs 8:c46930bd6c82 236 int rc = MQTTPACKET_READ_ERROR;
icraggs 8:c46930bd6c82 237
icraggs 8:c46930bd6c82 238 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
icraggs 8:c46930bd6c82 239 {
icraggs 8:c46930bd6c82 240 rc = MQTTPACKET_READ_ERROR; /* bad data */
icraggs 8:c46930bd6c82 241 goto exit;
icraggs 8:c46930bd6c82 242 }
icraggs 23:05fc7de97d4a 243 rc = ipstack.read(&c, 1, timeout);
icraggs 8:c46930bd6c82 244 if (rc != 1)
icraggs 8:c46930bd6c82 245 goto exit;
icraggs 8:c46930bd6c82 246 *value += (c & 127) * multiplier;
icraggs 8:c46930bd6c82 247 multiplier *= 128;
icraggs 8:c46930bd6c82 248 } while ((c & 128) != 0);
icraggs 8:c46930bd6c82 249 exit:
icraggs 8:c46930bd6c82 250 return len;
icraggs 8:c46930bd6c82 251 }
icraggs 8:c46930bd6c82 252
icraggs 8:c46930bd6c82 253
icraggs 8:c46930bd6c82 254 /**
icraggs 8:c46930bd6c82 255 * If any read fails in this method, then we should disconnect from the network, as on reconnect
icraggs 8:c46930bd6c82 256 * the packets can be retried.
icraggs 8:c46930bd6c82 257 * @param timeout the max time to wait for the packet read to complete, in milliseconds
icraggs 8:c46930bd6c82 258 * @return the MQTT packet type, or -1 if none
icraggs 8:c46930bd6c82 259 */
icraggs 23:05fc7de97d4a 260 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 261 int MQTT::Client<Network, Timer, a, b>::readPacket(Timer& timer)
icraggs 8:c46930bd6c82 262 {
icraggs 26:2658bb87c53d 263 int rc = FAILURE;
icraggs 8:c46930bd6c82 264 MQTTHeader header = {0};
icraggs 8:c46930bd6c82 265 int len = 0;
icraggs 8:c46930bd6c82 266 int rem_len = 0;
icraggs 8:c46930bd6c82 267
icraggs 8:c46930bd6c82 268 /* 1. read the header byte. This has the packet type in it */
icraggs 23:05fc7de97d4a 269 if (ipstack.read(readbuf, 1, timer.left_ms()) != 1)
icraggs 8:c46930bd6c82 270 goto exit;
icraggs 8:c46930bd6c82 271
icraggs 8:c46930bd6c82 272 len = 1;
icraggs 8:c46930bd6c82 273 /* 2. read the remaining length. This is variable in itself */
icraggs 20:cad3d54d7ecf 274 decodePacket(&rem_len, timer.left_ms());
icraggs 8:c46930bd6c82 275 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
icraggs 8:c46930bd6c82 276
icraggs 8:c46930bd6c82 277 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
icraggs 23:05fc7de97d4a 278 if (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len)
icraggs 8:c46930bd6c82 279 goto exit;
icraggs 8:c46930bd6c82 280
icraggs 8:c46930bd6c82 281 header.byte = readbuf[0];
icraggs 8:c46930bd6c82 282 rc = header.bits.type;
icraggs 8:c46930bd6c82 283 exit:
icraggs 8:c46930bd6c82 284 return rc;
icraggs 3:dbff6b768d28 285 }
icraggs 3:dbff6b768d28 286
icraggs 8:c46930bd6c82 287
icraggs 26:2658bb87c53d 288 // assume topic filter and name is in correct format
icraggs 26:2658bb87c53d 289 // # can only be at end
icraggs 26:2658bb87c53d 290 // + and # can only be next to separator
icraggs 26:2658bb87c53d 291 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 292 bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName)
icraggs 26:2658bb87c53d 293 {
icraggs 26:2658bb87c53d 294 char* curf = topicFilter;
icraggs 26:2658bb87c53d 295 char* curn = topicName.lenstring.data;
icraggs 26:2658bb87c53d 296 char* curn_end = curn + topicName.lenstring.len;
icraggs 26:2658bb87c53d 297
icraggs 26:2658bb87c53d 298 while (*curf && curn < curn_end)
icraggs 26:2658bb87c53d 299 {
icraggs 26:2658bb87c53d 300 if (*curn == '/' && *curf != '/')
icraggs 26:2658bb87c53d 301 break;
icraggs 26:2658bb87c53d 302 if (*curf != '+' && *curf != '#' && *curf != *curn)
icraggs 26:2658bb87c53d 303 break;
icraggs 26:2658bb87c53d 304 if (*curf == '+')
icraggs 26:2658bb87c53d 305 { // skip until we meet the next separator, or end of string
icraggs 26:2658bb87c53d 306 char* nextpos = curn + 1;
icraggs 26:2658bb87c53d 307 while (nextpos < curn_end && *nextpos != '/')
icraggs 26:2658bb87c53d 308 nextpos = ++curn + 1;
icraggs 26:2658bb87c53d 309 }
icraggs 26:2658bb87c53d 310 else if (*curf == '#')
icraggs 26:2658bb87c53d 311 curn = curn_end - 1; // skip until end of string
icraggs 26:2658bb87c53d 312 curf++;
icraggs 26:2658bb87c53d 313 curn++;
icraggs 26:2658bb87c53d 314 };
icraggs 26:2658bb87c53d 315
icraggs 26:2658bb87c53d 316 return (curn == curn_end) && (*curf == '\0');
icraggs 26:2658bb87c53d 317 }
icraggs 26:2658bb87c53d 318
icraggs 26:2658bb87c53d 319
icraggs 26:2658bb87c53d 320
icraggs 23:05fc7de97d4a 321 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS>
icraggs 26:2658bb87c53d 322 int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message)
icraggs 15:64a57183aa03 323 {
icraggs 26:2658bb87c53d 324 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 325
icraggs 20:cad3d54d7ecf 326 // we have to find the right message handler - indexed by topic
icraggs 23:05fc7de97d4a 327 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 20:cad3d54d7ecf 328 {
icraggs 26:2658bb87c53d 329 if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) ||
icraggs 26:2658bb87c53d 330 isTopicMatched((char*)messageHandlers[i].topicFilter, topicName)))
icraggs 20:cad3d54d7ecf 331 {
icraggs 26:2658bb87c53d 332 if (messageHandlers[i].fp.attached())
icraggs 26:2658bb87c53d 333 {
icraggs 26:2658bb87c53d 334 messageHandlers[i].fp(&message);
icraggs 26:2658bb87c53d 335 rc = SUCCESS;
icraggs 26:2658bb87c53d 336 }
icraggs 20:cad3d54d7ecf 337 }
icraggs 20:cad3d54d7ecf 338 }
icraggs 26:2658bb87c53d 339
icraggs 26:2658bb87c53d 340 if (rc == FAILURE && defaultMessageHandler.attached())
icraggs 26:2658bb87c53d 341 {
icraggs 26:2658bb87c53d 342 defaultMessageHandler(&message);
icraggs 26:2658bb87c53d 343 rc = SUCCESS;
icraggs 26:2658bb87c53d 344 }
icraggs 20:cad3d54d7ecf 345
icraggs 20:cad3d54d7ecf 346 return rc;
icraggs 15:64a57183aa03 347 }
icraggs 15:64a57183aa03 348
icraggs 15:64a57183aa03 349
icraggs 20:cad3d54d7ecf 350
icraggs 23:05fc7de97d4a 351 template<class Network, class Timer, int a, int b>
icraggs 26:2658bb87c53d 352 int MQTT::Client<Network, Timer, a, b>::yield(int timeout_ms)
icraggs 20:cad3d54d7ecf 353 {
icraggs 26:2658bb87c53d 354 int rc = SUCCESS;
icraggs 20:cad3d54d7ecf 355 Timer timer = Timer();
icraggs 20:cad3d54d7ecf 356
icraggs 23:05fc7de97d4a 357 timer.countdown_ms(timeout_ms);
icraggs 20:cad3d54d7ecf 358 while (!timer.expired())
icraggs 26:2658bb87c53d 359 {
icraggs 26:2658bb87c53d 360 if (cycle(timer) == FAILURE)
icraggs 26:2658bb87c53d 361 {
icraggs 26:2658bb87c53d 362 rc = FAILURE;
icraggs 26:2658bb87c53d 363 break;
icraggs 26:2658bb87c53d 364 }
icraggs 26:2658bb87c53d 365 }
icraggs 26:2658bb87c53d 366
icraggs 26:2658bb87c53d 367 return rc;
icraggs 20:cad3d54d7ecf 368 }
icraggs 20:cad3d54d7ecf 369
icraggs 20:cad3d54d7ecf 370
icraggs 23:05fc7de97d4a 371 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 372 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
icraggs 8:c46930bd6c82 373 {
icraggs 8:c46930bd6c82 374 /* get one piece of work off the wire and one pass through */
icraggs 20:cad3d54d7ecf 375
Ian Craggs 12:cc7f2d62a393 376 // read the socket, see what work is due
icraggs 20:cad3d54d7ecf 377 int packet_type = readPacket(timer);
icraggs 15:64a57183aa03 378
icraggs 26:2658bb87c53d 379 int len = 0,
icraggs 26:2658bb87c53d 380 rc = SUCCESS;
icraggs 8:c46930bd6c82 381 switch (packet_type)
icraggs 8:c46930bd6c82 382 {
icraggs 15:64a57183aa03 383 case CONNACK:
icraggs 8:c46930bd6c82 384 case PUBACK:
icraggs 8:c46930bd6c82 385 case SUBACK:
icraggs 8:c46930bd6c82 386 break;
icraggs 15:64a57183aa03 387 case PUBLISH:
icraggs 20:cad3d54d7ecf 388 MQTTString topicName;
icraggs 20:cad3d54d7ecf 389 Message msg;
icraggs 26:2658bb87c53d 390 if (MQTTDeserialize_publish((int*)&msg.dup, (int*)&msg.qos, (int*)&msg.retained, (int*)&msg.id, &topicName,
icraggs 26:2658bb87c53d 391 (char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 392 goto exit;
icraggs 26:2658bb87c53d 393 deliverMessage(topicName, msg);
icraggs 20:cad3d54d7ecf 394 if (msg.qos != QOS0)
icraggs 20:cad3d54d7ecf 395 {
icraggs 20:cad3d54d7ecf 396 if (msg.qos == QOS1)
icraggs 23:05fc7de97d4a 397 len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id);
icraggs 20:cad3d54d7ecf 398 else if (msg.qos == QOS2)
icraggs 23:05fc7de97d4a 399 len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id);
icraggs 26:2658bb87c53d 400 if (len <= 0)
icraggs 26:2658bb87c53d 401 rc = FAILURE;
icraggs 26:2658bb87c53d 402 else
icraggs 26:2658bb87c53d 403 rc = sendPacket(len, timer);
icraggs 26:2658bb87c53d 404 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 405 goto exit; // there was a problem
icraggs 20:cad3d54d7ecf 406 }
Ian Craggs 12:cc7f2d62a393 407 break;
icraggs 15:64a57183aa03 408 case PUBREC:
icraggs 20:cad3d54d7ecf 409 int type, dup, mypacketid;
icraggs 26:2658bb87c53d 410 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 411 rc = FAILURE;
icraggs 26:2658bb87c53d 412 else if ((len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, mypacketid)) <= 0)
icraggs 26:2658bb87c53d 413 rc = FAILURE;
icraggs 26:2658bb87c53d 414 else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet
icraggs 26:2658bb87c53d 415 rc = FAILURE; // there was a problem
icraggs 26:2658bb87c53d 416 if (rc == FAILURE)
icraggs 20:cad3d54d7ecf 417 goto exit; // there was a problem
icraggs 8:c46930bd6c82 418 break;
icraggs 8:c46930bd6c82 419 case PUBCOMP:
icraggs 8:c46930bd6c82 420 break;
icraggs 15:64a57183aa03 421 case PINGRESP:
icraggs 20:cad3d54d7ecf 422 ping_outstanding = false;
icraggs 8:c46930bd6c82 423 break;
icraggs 15:64a57183aa03 424 }
icraggs 20:cad3d54d7ecf 425 keepalive();
Ian Craggs 12:cc7f2d62a393 426 exit:
icraggs 26:2658bb87c53d 427 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 428 rc = packet_type;
icraggs 26:2658bb87c53d 429 return rc;
icraggs 15:64a57183aa03 430 }
icraggs 15:64a57183aa03 431
icraggs 15:64a57183aa03 432
icraggs 23:05fc7de97d4a 433 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 434 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive()
icraggs 15:64a57183aa03 435 {
icraggs 26:2658bb87c53d 436 int rc = FAILURE;
icraggs 15:64a57183aa03 437
icraggs 20:cad3d54d7ecf 438 if (keepAliveInterval == 0)
icraggs 26:2658bb87c53d 439 {
icraggs 26:2658bb87c53d 440 rc = SUCCESS;
icraggs 20:cad3d54d7ecf 441 goto exit;
icraggs 26:2658bb87c53d 442 }
icraggs 15:64a57183aa03 443
icraggs 20:cad3d54d7ecf 444 if (ping_timer.expired())
icraggs 20:cad3d54d7ecf 445 {
icraggs 26:2658bb87c53d 446 if (!ping_outstanding)
icraggs 20:cad3d54d7ecf 447 {
icraggs 20:cad3d54d7ecf 448 Timer timer = Timer(1000);
icraggs 23:05fc7de97d4a 449 int len = MQTTSerialize_pingreq(buf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 450 if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet
icraggs 20:cad3d54d7ecf 451 ping_outstanding = true;
icraggs 20:cad3d54d7ecf 452 }
icraggs 20:cad3d54d7ecf 453 }
icraggs 15:64a57183aa03 454
icraggs 15:64a57183aa03 455 exit:
icraggs 20:cad3d54d7ecf 456 return rc;
icraggs 8:c46930bd6c82 457 }
icraggs 8:c46930bd6c82 458
icraggs 8:c46930bd6c82 459
icraggs 16:91c2f9a144d4 460 // only used in single-threaded mode where one command at a time is in process
icraggs 23:05fc7de97d4a 461 template<class Network, class Timer, int a, int b>
icraggs 23:05fc7de97d4a 462 int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer)
icraggs 15:64a57183aa03 463 {
icraggs 26:2658bb87c53d 464 int rc = FAILURE;
icraggs 20:cad3d54d7ecf 465
icraggs 20:cad3d54d7ecf 466 do
icraggs 16:91c2f9a144d4 467 {
icraggs 20:cad3d54d7ecf 468 if (timer.expired())
icraggs 20:cad3d54d7ecf 469 break; // we timed out
icraggs 20:cad3d54d7ecf 470 }
icraggs 20:cad3d54d7ecf 471 while ((rc = cycle(timer)) != packet_type);
icraggs 20:cad3d54d7ecf 472
icraggs 20:cad3d54d7ecf 473 return rc;
icraggs 16:91c2f9a144d4 474 }
icraggs 16:91c2f9a144d4 475
icraggs 16:91c2f9a144d4 476
icraggs 23:05fc7de97d4a 477 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 478 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData* options)
icraggs 16:91c2f9a144d4 479 {
icraggs 23:05fc7de97d4a 480 Timer connect_timer = Timer(command_timeout_ms);
icraggs 26:2658bb87c53d 481 int rc = FAILURE;
icraggs 8:c46930bd6c82 482
Ian Craggs 12:cc7f2d62a393 483 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
icraggs 8:c46930bd6c82 484 if (options == 0)
Ian Craggs 12:cc7f2d62a393 485 options = &default_options; // set default options if none were supplied
icraggs 8:c46930bd6c82 486
icraggs 15:64a57183aa03 487 this->keepAliveInterval = options->keepAliveInterval;
icraggs 20:cad3d54d7ecf 488 ping_timer.countdown(this->keepAliveInterval);
icraggs 23:05fc7de97d4a 489 int len = MQTTSerialize_connect(buf, MAX_MQTT_PACKET_SIZE, options);
icraggs 26:2658bb87c53d 490 if (len <= 0)
icraggs 26:2658bb87c53d 491 goto exit;
icraggs 26:2658bb87c53d 492 if ((rc = sendPacket(len, connect_timer)) != SUCCESS) // send the connect packet
icraggs 20:cad3d54d7ecf 493 goto exit; // there was a problem
icraggs 8:c46930bd6c82 494
icraggs 20:cad3d54d7ecf 495 // this will be a blocking call, wait for the connack
icraggs 20:cad3d54d7ecf 496 if (waitfor(CONNACK, connect_timer) == CONNACK)
icraggs 15:64a57183aa03 497 {
icraggs 20:cad3d54d7ecf 498 int connack_rc = -1;
icraggs 23:05fc7de97d4a 499 if (MQTTDeserialize_connack(&connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 500 rc = connack_rc;
icraggs 26:2658bb87c53d 501 else
icraggs 26:2658bb87c53d 502 rc = FAILURE;
icraggs 8:c46930bd6c82 503 }
icraggs 26:2658bb87c53d 504 else
icraggs 26:2658bb87c53d 505 rc = FAILURE;
icraggs 15:64a57183aa03 506
icraggs 15:64a57183aa03 507 exit:
icraggs 26:2658bb87c53d 508 if (rc == SUCCESS)
icraggs 26:2658bb87c53d 509 isconnected = true;
icraggs 8:c46930bd6c82 510 return rc;
icraggs 8:c46930bd6c82 511 }
icraggs 8:c46930bd6c82 512
icraggs 8:c46930bd6c82 513
icraggs 23:05fc7de97d4a 514 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 515 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler)
icraggs 20:cad3d54d7ecf 516 {
icraggs 26:2658bb87c53d 517 int rc = FAILURE;
icraggs 23:05fc7de97d4a 518 Timer timer = Timer(command_timeout_ms);
icraggs 26:2658bb87c53d 519 int len = 0;
icraggs 20:cad3d54d7ecf 520
icraggs 8:c46930bd6c82 521 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 26:2658bb87c53d 522 if (!isconnected)
icraggs 20:cad3d54d7ecf 523 goto exit;
icraggs 26:2658bb87c53d 524
icraggs 26:2658bb87c53d 525 len = MQTTSerialize_subscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos);
icraggs 26:2658bb87c53d 526 if (len <= 0)
icraggs 26:2658bb87c53d 527 goto exit;
icraggs 23:05fc7de97d4a 528 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 26:2658bb87c53d 529 goto exit; // there was a problem
icraggs 8:c46930bd6c82 530
icraggs 20:cad3d54d7ecf 531 if (waitfor(SUBACK, timer) == SUBACK) // wait for suback
icraggs 8:c46930bd6c82 532 {
icraggs 20:cad3d54d7ecf 533 int count = 0, grantedQoS = -1, mypacketid;
icraggs 23:05fc7de97d4a 534 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 535 rc = grantedQoS; // 0, 1, 2 or 0x80
icraggs 20:cad3d54d7ecf 536 if (rc != 0x80)
icraggs 8:c46930bd6c82 537 {
icraggs 23:05fc7de97d4a 538 for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
icraggs 16:91c2f9a144d4 539 {
icraggs 26:2658bb87c53d 540 if (messageHandlers[i].topicFilter == 0)
icraggs 20:cad3d54d7ecf 541 {
icraggs 26:2658bb87c53d 542 messageHandlers[i].topicFilter = topicFilter;
icraggs 20:cad3d54d7ecf 543 messageHandlers[i].fp.attach(messageHandler);
icraggs 20:cad3d54d7ecf 544 rc = 0;
icraggs 20:cad3d54d7ecf 545 break;
icraggs 20:cad3d54d7ecf 546 }
icraggs 16:91c2f9a144d4 547 }
icraggs 8:c46930bd6c82 548 }
icraggs 8:c46930bd6c82 549 }
icraggs 26:2658bb87c53d 550 else
icraggs 26:2658bb87c53d 551 rc = FAILURE;
icraggs 26:2658bb87c53d 552
icraggs 15:64a57183aa03 553 exit:
icraggs 26:2658bb87c53d 554 //if (rc == FAILURE)
icraggs 26:2658bb87c53d 555 // closesession();
Ian Craggs 12:cc7f2d62a393 556 return rc;
icraggs 15:64a57183aa03 557 }
icraggs 15:64a57183aa03 558
icraggs 15:64a57183aa03 559
icraggs 23:05fc7de97d4a 560 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS>
icraggs 23:05fc7de97d4a 561 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter)
icraggs 20:cad3d54d7ecf 562 {
icraggs 26:2658bb87c53d 563 int rc = FAILURE;
icraggs 23:05fc7de97d4a 564 Timer timer = Timer(command_timeout_ms);
icraggs 20:cad3d54d7ecf 565
Ian Craggs 12:cc7f2d62a393 566 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 8:c46930bd6c82 567
icraggs 26:2658bb87c53d 568 int len = MQTTSerialize_unsubscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic);
icraggs 26:2658bb87c53d 569 if (len <= 0)
icraggs 20:cad3d54d7ecf 570 goto exit;
icraggs 23:05fc7de97d4a 571 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 20:cad3d54d7ecf 572 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 573
icraggs 20:cad3d54d7ecf 574 if (waitfor(UNSUBACK, timer) == UNSUBACK)
Ian Craggs 12:cc7f2d62a393 575 {
icraggs 20:cad3d54d7ecf 576 int mypacketid; // should be the same as the packetid above
icraggs 23:05fc7de97d4a 577 if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
icraggs 20:cad3d54d7ecf 578 rc = 0;
Ian Craggs 12:cc7f2d62a393 579 }
icraggs 26:2658bb87c53d 580 else
icraggs 26:2658bb87c53d 581 rc = FAILURE;
icraggs 15:64a57183aa03 582
icraggs 15:64a57183aa03 583 exit:
Ian Craggs 12:cc7f2d62a393 584 return rc;
icraggs 15:64a57183aa03 585 }
icraggs 15:64a57183aa03 586
icraggs 15:64a57183aa03 587
icraggs 15:64a57183aa03 588
icraggs 23:05fc7de97d4a 589 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 590 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message* message)
icraggs 20:cad3d54d7ecf 591 {
icraggs 26:2658bb87c53d 592 int rc = FAILURE;
icraggs 23:05fc7de97d4a 593 Timer timer = Timer(command_timeout_ms);
icraggs 20:cad3d54d7ecf 594
icraggs 20:cad3d54d7ecf 595 MQTTString topicString = {(char*)topicName, 0, 0};
icraggs 15:64a57183aa03 596
icraggs 20:cad3d54d7ecf 597 if (message->qos == QOS1 || message->qos == QOS2)
icraggs 20:cad3d54d7ecf 598 message->id = packetid.getNext();
icraggs 15:64a57183aa03 599
icraggs 23:05fc7de97d4a 600 int len = MQTTSerialize_publish(buf, MAX_MQTT_PACKET_SIZE, 0, message->qos, message->retained, message->id,
icraggs 20:cad3d54d7ecf 601 topicString, (char*)message->payload, message->payloadlen);
icraggs 26:2658bb87c53d 602 if (len <= 0)
icraggs 26:2658bb87c53d 603 goto exit;
icraggs 26:2658bb87c53d 604 if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
icraggs 20:cad3d54d7ecf 605 goto exit; // there was a problem
Ian Craggs 12:cc7f2d62a393 606
icraggs 20:cad3d54d7ecf 607 if (message->qos == QOS1)
Ian Craggs 12:cc7f2d62a393 608 {
icraggs 20:cad3d54d7ecf 609 if (waitfor(PUBACK, timer) == PUBACK)
icraggs 20:cad3d54d7ecf 610 {
icraggs 20:cad3d54d7ecf 611 int type, dup, mypacketid;
icraggs 26:2658bb87c53d 612 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 613 rc = FAILURE;
icraggs 20:cad3d54d7ecf 614 }
icraggs 26:2658bb87c53d 615 else
icraggs 26:2658bb87c53d 616 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 617 }
icraggs 20:cad3d54d7ecf 618 else if (message->qos == QOS2)
Ian Craggs 12:cc7f2d62a393 619 {
icraggs 20:cad3d54d7ecf 620 if (waitfor(PUBCOMP, timer) == PUBCOMP)
icraggs 20:cad3d54d7ecf 621 {
icraggs 20:cad3d54d7ecf 622 int type, dup, mypacketid;
icraggs 26:2658bb87c53d 623 if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
icraggs 26:2658bb87c53d 624 rc = FAILURE;
icraggs 20:cad3d54d7ecf 625 }
icraggs 26:2658bb87c53d 626 else
icraggs 26:2658bb87c53d 627 rc = FAILURE;
Ian Craggs 12:cc7f2d62a393 628 }
icraggs 15:64a57183aa03 629
icraggs 15:64a57183aa03 630 exit:
icraggs 8:c46930bd6c82 631 return rc;
icraggs 8:c46930bd6c82 632 }
icraggs 8:c46930bd6c82 633
icraggs 8:c46930bd6c82 634
icraggs 23:05fc7de97d4a 635 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
icraggs 23:05fc7de97d4a 636 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
icraggs 20:cad3d54d7ecf 637 {
icraggs 26:2658bb87c53d 638 int rc = FAILURE;
icraggs 23:05fc7de97d4a 639 Timer timer = Timer(command_timeout_ms); // we might wait for incomplete incoming publishes to complete
icraggs 23:05fc7de97d4a 640 int len = MQTTSerialize_disconnect(buf, MAX_MQTT_PACKET_SIZE);
icraggs 26:2658bb87c53d 641 if (len > 0)
icraggs 26:2658bb87c53d 642 rc = sendPacket(len, timer); // send the disconnect packet
icraggs 26:2658bb87c53d 643
icraggs 23:05fc7de97d4a 644 return rc;
icraggs 20:cad3d54d7ecf 645 }
icraggs 20:cad3d54d7ecf 646
icraggs 20:cad3d54d7ecf 647
icraggs 20:cad3d54d7ecf 648 #endif