hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Wed May 05 14:48:01 2021 +0000
Revision:
25:ca1b1098c77f
TEST

Who changed what in which revision?

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