USE YHTANG AWS ACCOUNT TO TEST MQTT

Dependencies:   MQTTPacket FP

Dependents:   NuMaker-mbed-AWS-IoT-example

Committer:
icraggs
Date:
Mon Apr 28 16:07:51 2014 +0000
Revision:
20:cad3d54d7ecf
Child:
21:e918525e529d
Split APIs into two.  Add unsubscribe and disconnect processing

Who changed what in which revision?

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