MQTT client to test the ENC28J60-EMAC on NUCLEO-L152RE

Dependencies:   ENC28J60-EMAC

Committer:
jack1930
Date:
Fri Aug 20 11:02:55 2021 +0000
Revision:
6:c123d9b8e6f4
Parent:
0:238f0d0c0ba3
Angepasst fuer GSOE

Who changed what in which revision?

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