Better timing

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
Ian Craggs
Date:
Mon Apr 14 21:58:58 2014 +0100
Revision:
19:57f6f976e878
Parent:
16:91c2f9a144d4
Child:
20:cad3d54d7ecf
Fix and test subscribe

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