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

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

Committer:
jksoft
Date:
Wed Jun 14 07:48:45 2017 +0000
Revision:
8:e2f15b1b4f70
Parent:
1:8e4149b53a8a
?????

Who changed what in which revision?

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