ddd

Dependencies:   FP MQTTPacket

Fork of MQTTforLecture by Bohyun Bang

Committer:
icraggs
Date:
Tue May 20 15:07:11 2014 +0000
Revision:
30:a4e3a97dabe3
Parent:
29:833386b16f3e
Parent:
28:8b2abe9bd814
Child:
31:a51dd239b78e
Merge after refactoring header locations

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