Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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