cc

Dependencies:   MQTTPacket FP

Committer:
icraggs
Date:
Tue May 06 09:44:23 2014 +0000
Revision:
23:05fc7de97d4a
Parent:
22:aadb79d29330
Child:
25:d13a6c558164
Child:
26:2658bb87c53d
Allocate arrays from automatic storage

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