An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
icraggs
Date:
Mon Apr 28 16:07:51 2014 +0000
Revision:
20:cad3d54d7ecf
Parent:
19:57f6f976e878
Child:
21:e918525e529d
Split APIs into two.  Add unsubscribe and disconnect processing

Who changed what in which revision?

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