An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
icraggs
Date:
Mon Apr 14 18:51:52 2014 +0000
Revision:
16:91c2f9a144d4
Parent:
15:64a57183aa03
Child:
19:57f6f976e878
Add subscriptions

Who changed what in which revision?

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