MQTT cellular example

Dependencies:   C027 C027_Support C12832 mbed-rtos mbed FP MQTTPacket

Fork of HelloMQTT by MQTT

Committer:
mazgch
Date:
Mon May 12 10:19:47 2014 +0000
Revision:
9:996c8c7aa1f9
Parent:
8:b32c94be6522
latest lib

Who changed what in which revision?

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