attendance system MQTT lib

Dependencies:   FP MQTTPacket

Fork of MQTT by James Bruce

Committer:
icraggs
Date:
Tue Apr 29 16:04:55 2014 +0000
Revision:
21:e918525e529d
Parent:
20:cad3d54d7ecf
Child:
22:aadb79d29330
Add more structure to the Async client

Who changed what in which revision?

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