【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/

Dependents:   mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn

Committer:
jksoft
Date:
Mon Mar 26 04:49:20 2018 +0000
Revision:
13:61e0cc093180
Parent:
11:278ba4de2e99
???????????

Who changed what in which revision?

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