mbed OS5に対応したMilkcocoaライブラリのテストバージョンです。

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
jksoft
Date:
Tue Jan 24 13:41:36 2017 +0000
Revision:
24:6ba1245bf049
??????????

Who changed what in which revision?

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