cc

Dependencies:   MQTTPacket FP

Committer:
icraggs
Date:
Thu Apr 10 22:47:17 2014 +0000
Revision:
11:db15da110a37
Parent:
9:01b8cc7d94cc
Child:
13:fd82db992024
Change thread function definitions to allow it to work

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;
icraggs 2:dcfdd2abfe71 36 unsigned short msgid;
icraggs 2:dcfdd2abfe71 37 void *payload;
icraggs 2:dcfdd2abfe71 38 size_t payloadlen;
sam_grove 0:fe461e4d7afe 39 };
sam_grove 0:fe461e4d7afe 40
icraggs 6:4d312a49200b 41 template<class Network, class Timer, class Thread> class Client;
icraggs 4:4ef00243708e 42
icraggs 4:4ef00243708e 43 class Result
icraggs 4:4ef00243708e 44 {
icraggs 4:4ef00243708e 45 /* success or failure result data */
icraggs 6:4d312a49200b 46 Client<class Network, class Timer, class Thread>* client;
icraggs 4:4ef00243708e 47 };
icraggs 4:4ef00243708e 48
icraggs 9:01b8cc7d94cc 49
icraggs 9:01b8cc7d94cc 50 class PacketId
icraggs 9:01b8cc7d94cc 51 {
icraggs 9:01b8cc7d94cc 52 public:
icraggs 9:01b8cc7d94cc 53 PacketId();
icraggs 9:01b8cc7d94cc 54
icraggs 9:01b8cc7d94cc 55 int getNext();
icraggs 9:01b8cc7d94cc 56
icraggs 9:01b8cc7d94cc 57 private:
icraggs 9:01b8cc7d94cc 58 static const int MAX_PACKET_ID = 65535;
icraggs 9:01b8cc7d94cc 59 int next;
icraggs 9:01b8cc7d94cc 60 };
icraggs 9:01b8cc7d94cc 61
icraggs 9:01b8cc7d94cc 62 typedef void (*resultHandler)(Result*);
icraggs 9:01b8cc7d94cc 63 typedef void (*messageHandler)(Message*);
icraggs 2:dcfdd2abfe71 64
icraggs 6:4d312a49200b 65 template<class Network, class Timer, class Thread> class Client
icraggs 2:dcfdd2abfe71 66 {
icraggs 2:dcfdd2abfe71 67
sam_grove 0:fe461e4d7afe 68 public:
icraggs 4:4ef00243708e 69
icraggs 9:01b8cc7d94cc 70 Client(Network* network, Timer* timer, const int buffer_size = 100, const int command_timeout = 30);
icraggs 2:dcfdd2abfe71 71
icraggs 9:01b8cc7d94cc 72 int connect(MQTTPacket_connectData* options = 0, resultHandler fn = 0);
icraggs 2:dcfdd2abfe71 73
icraggs 9:01b8cc7d94cc 74 template<class T>
icraggs 9:01b8cc7d94cc 75 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 76
icraggs 9:01b8cc7d94cc 77 int publish(const char* topic, Message* message, resultHandler rh = 0);
sam_grove 0:fe461e4d7afe 78
icraggs 9:01b8cc7d94cc 79 int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh, resultHandler rh = 0);
icraggs 2:dcfdd2abfe71 80
icraggs 9:01b8cc7d94cc 81 int unsubscribe(char* topicFilter, resultHandler rh = 0);
icraggs 9:01b8cc7d94cc 82
icraggs 9:01b8cc7d94cc 83 int disconnect(int timeout, resultHandler rh = 0);
sam_grove 0:fe461e4d7afe 84
icraggs 8:c46930bd6c82 85 void run(void const *argument);
icraggs 8:c46930bd6c82 86
icraggs 2:dcfdd2abfe71 87 private:
icraggs 2:dcfdd2abfe71 88
icraggs 4:4ef00243708e 89 int cycle();
icraggs 2:dcfdd2abfe71 90
icraggs 3:dbff6b768d28 91 int decodePacket(int* value, int timeout);
icraggs 4:4ef00243708e 92 int readPacket(int timeout = -1);
icraggs 6:4d312a49200b 93 int sendPacket(int length, int timeout = -1);
icraggs 3:dbff6b768d28 94
icraggs 4:4ef00243708e 95 Thread* thread;
icraggs 4:4ef00243708e 96 Network* ipstack;
icraggs 6:4d312a49200b 97 Timer* timer;
icraggs 4:4ef00243708e 98
icraggs 9:01b8cc7d94cc 99 char* buf;
icraggs 2:dcfdd2abfe71 100 int buflen;
icraggs 2:dcfdd2abfe71 101
icraggs 4:4ef00243708e 102 char* readbuf;
icraggs 4:4ef00243708e 103 int readbuflen;
icraggs 4:4ef00243708e 104
icraggs 6:4d312a49200b 105 int command_timeout; // max time to wait for any MQTT command to complete, in seconds
icraggs 6:4d312a49200b 106 int keepalive;
icraggs 9:01b8cc7d94cc 107 PacketId packetid;
icraggs 9:01b8cc7d94cc 108
icraggs 9:01b8cc7d94cc 109 typedef FP<void, Result*> resultHandlerFP;
icraggs 9:01b8cc7d94cc 110 // how many concurrent operations should we allow? Each one will require a function pointer
icraggs 9:01b8cc7d94cc 111 resultHandlerFP connectHandler;
icraggs 9:01b8cc7d94cc 112
icraggs 9:01b8cc7d94cc 113 #define MAX_MESSAGE_HANDLERS 5
icraggs 9:01b8cc7d94cc 114 typedef FP<void, Message*> messageHandlerFP;
icraggs 9:01b8cc7d94cc 115 messageHandlerFP messageHandlers[MAX_MESSAGE_HANDLERS]; // Linked list, or constructor parameter to limit array size?
icraggs 4:4ef00243708e 116
icraggs 11:db15da110a37 117 static void threadfn(void* arg);
icraggs 11:db15da110a37 118
sam_grove 0:fe461e4d7afe 119 };
sam_grove 0:fe461e4d7afe 120
icraggs 8:c46930bd6c82 121
icraggs 8:c46930bd6c82 122 }
icraggs 8:c46930bd6c82 123
icraggs 11:db15da110a37 124 template<class Network, class Timer, class Thread> void MQTT::Client<Network, Timer, Thread>::threadfn(void* arg)
icraggs 11:db15da110a37 125 {
icraggs 11:db15da110a37 126 ((Client<Network, Timer, Thread>*) arg)->run(NULL);
icraggs 11:db15da110a37 127 }
icraggs 11:db15da110a37 128
icraggs 11:db15da110a37 129
icraggs 9:01b8cc7d94cc 130 template<class Network, class Timer, class Thread> MQTT::Client<Network, Timer, Thread>::Client(Network* network, Timer* timer, const int buffer_size, const int command_timeout) : packetid()
icraggs 8:c46930bd6c82 131 {
icraggs 8:c46930bd6c82 132
icraggs 8:c46930bd6c82 133 buf = new char[buffer_size];
icraggs 8:c46930bd6c82 134 readbuf = new char[buffer_size];
icraggs 8:c46930bd6c82 135 buflen = readbuflen = buffer_size;
icraggs 8:c46930bd6c82 136 this->command_timeout = command_timeout;
icraggs 8:c46930bd6c82 137 this->thread = 0;
icraggs 8:c46930bd6c82 138 this->ipstack = network;
icraggs 8:c46930bd6c82 139 this->timer = timer;
icraggs 8:c46930bd6c82 140 }
icraggs 8:c46930bd6c82 141
icraggs 8:c46930bd6c82 142
icraggs 8:c46930bd6c82 143 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::sendPacket(int length, int timeout)
icraggs 8:c46930bd6c82 144 {
icraggs 8:c46930bd6c82 145 int sent = 0;
icraggs 8:c46930bd6c82 146
icraggs 8:c46930bd6c82 147 while (sent < length)
icraggs 8:c46930bd6c82 148 sent += ipstack->write(&buf[sent], length, -1);
icraggs 8:c46930bd6c82 149
icraggs 8:c46930bd6c82 150 return sent;
icraggs 8:c46930bd6c82 151 }
icraggs 8:c46930bd6c82 152
icraggs 8:c46930bd6c82 153
icraggs 8:c46930bd6c82 154 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::decodePacket(int* value, int timeout)
icraggs 8:c46930bd6c82 155 {
icraggs 8:c46930bd6c82 156 char c;
icraggs 8:c46930bd6c82 157 int multiplier = 1;
icraggs 8:c46930bd6c82 158 int len = 0;
icraggs 8:c46930bd6c82 159 #define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
icraggs 8:c46930bd6c82 160
icraggs 8:c46930bd6c82 161 *value = 0;
icraggs 8:c46930bd6c82 162 do
icraggs 8:c46930bd6c82 163 {
icraggs 8:c46930bd6c82 164 int rc = MQTTPACKET_READ_ERROR;
icraggs 8:c46930bd6c82 165
icraggs 8:c46930bd6c82 166 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
icraggs 8:c46930bd6c82 167 {
icraggs 8:c46930bd6c82 168 rc = MQTTPACKET_READ_ERROR; /* bad data */
icraggs 8:c46930bd6c82 169 goto exit;
icraggs 8:c46930bd6c82 170 }
icraggs 8:c46930bd6c82 171 rc = ipstack->read(&c, 1, timeout);
icraggs 8:c46930bd6c82 172 if (rc != 1)
icraggs 8:c46930bd6c82 173 goto exit;
icraggs 8:c46930bd6c82 174 *value += (c & 127) * multiplier;
icraggs 8:c46930bd6c82 175 multiplier *= 128;
icraggs 8:c46930bd6c82 176 } while ((c & 128) != 0);
icraggs 8:c46930bd6c82 177 exit:
icraggs 8:c46930bd6c82 178 return len;
icraggs 8:c46930bd6c82 179 }
icraggs 8:c46930bd6c82 180
icraggs 8:c46930bd6c82 181
icraggs 8:c46930bd6c82 182 /**
icraggs 8:c46930bd6c82 183 * If any read fails in this method, then we should disconnect from the network, as on reconnect
icraggs 8:c46930bd6c82 184 * the packets can be retried.
icraggs 8:c46930bd6c82 185 * @param timeout the max time to wait for the packet read to complete, in milliseconds
icraggs 8:c46930bd6c82 186 * @return the MQTT packet type, or -1 if none
icraggs 8:c46930bd6c82 187 */
icraggs 8:c46930bd6c82 188 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::readPacket(int timeout)
icraggs 8:c46930bd6c82 189 {
icraggs 8:c46930bd6c82 190 int rc = -1;
icraggs 8:c46930bd6c82 191 MQTTHeader header = {0};
icraggs 8:c46930bd6c82 192 int len = 0;
icraggs 8:c46930bd6c82 193 int rem_len = 0;
icraggs 8:c46930bd6c82 194
icraggs 8:c46930bd6c82 195 /* 1. read the header byte. This has the packet type in it */
icraggs 8:c46930bd6c82 196 if (ipstack->read(readbuf, 1, timeout) != 1)
icraggs 8:c46930bd6c82 197 goto exit;
icraggs 8:c46930bd6c82 198
icraggs 8:c46930bd6c82 199 len = 1;
icraggs 8:c46930bd6c82 200 /* 2. read the remaining length. This is variable in itself */
icraggs 8:c46930bd6c82 201 decodePacket(&rem_len, timeout);
icraggs 8:c46930bd6c82 202 len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
icraggs 8:c46930bd6c82 203
icraggs 8:c46930bd6c82 204 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
icraggs 8:c46930bd6c82 205 if (ipstack->read(readbuf + len, rem_len, timeout) != rem_len)
icraggs 8:c46930bd6c82 206 goto exit;
icraggs 8:c46930bd6c82 207
icraggs 8:c46930bd6c82 208 header.byte = readbuf[0];
icraggs 8:c46930bd6c82 209 rc = header.bits.type;
icraggs 8:c46930bd6c82 210 exit:
icraggs 8:c46930bd6c82 211 return rc;
icraggs 3:dbff6b768d28 212 }
icraggs 3:dbff6b768d28 213
icraggs 8:c46930bd6c82 214
icraggs 8:c46930bd6c82 215 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::cycle()
icraggs 8:c46930bd6c82 216 {
icraggs 9:01b8cc7d94cc 217 int timeout = -1;
icraggs 8:c46930bd6c82 218 /* get one piece of work off the wire and one pass through */
icraggs 8:c46930bd6c82 219
icraggs 8:c46930bd6c82 220 // 1. read the socket, see what work is due.
icraggs 9:01b8cc7d94cc 221 int packet_type = readPacket(timeout);
icraggs 8:c46930bd6c82 222
icraggs 8:c46930bd6c82 223 printf("packet type %d\n", packet_type);
icraggs 8:c46930bd6c82 224
icraggs 8:c46930bd6c82 225 switch (packet_type)
icraggs 8:c46930bd6c82 226 {
icraggs 8:c46930bd6c82 227 case CONNACK:
icraggs 8:c46930bd6c82 228 printf("connack received\n");
icraggs 8:c46930bd6c82 229 break;
icraggs 8:c46930bd6c82 230 case PUBLISH:
icraggs 8:c46930bd6c82 231 break;
icraggs 8:c46930bd6c82 232 case PUBACK:
icraggs 8:c46930bd6c82 233 break;
icraggs 8:c46930bd6c82 234 case SUBACK:
icraggs 8:c46930bd6c82 235 break;
icraggs 8:c46930bd6c82 236 case PUBREC:
icraggs 8:c46930bd6c82 237 break;
icraggs 8:c46930bd6c82 238 case PUBCOMP:
icraggs 8:c46930bd6c82 239 break;
icraggs 8:c46930bd6c82 240 case PINGRESP:
icraggs 8:c46930bd6c82 241 break;
icraggs 8:c46930bd6c82 242 case -1:
icraggs 8:c46930bd6c82 243 break;
icraggs 8:c46930bd6c82 244 }
icraggs 8:c46930bd6c82 245 return packet_type;
icraggs 8:c46930bd6c82 246 }
icraggs 8:c46930bd6c82 247
icraggs 8:c46930bd6c82 248
icraggs 8:c46930bd6c82 249 template<class Network, class Timer, class Thread> void MQTT::Client<Network, Timer, Thread>::run(void const *argument)
icraggs 8:c46930bd6c82 250 {
icraggs 11:db15da110a37 251 while (true)
icraggs 11:db15da110a37 252 {
icraggs 11:db15da110a37 253 cycle();
icraggs 11:db15da110a37 254 }
icraggs 8:c46930bd6c82 255 }
icraggs 8:c46930bd6c82 256
icraggs 8:c46930bd6c82 257
icraggs 9:01b8cc7d94cc 258 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::connect(MQTTPacket_connectData* options, resultHandler resultHandler)
icraggs 8:c46930bd6c82 259 {
icraggs 8:c46930bd6c82 260 int len = 0;
icraggs 8:c46930bd6c82 261 int rc = -99;
icraggs 8:c46930bd6c82 262 MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
icraggs 8:c46930bd6c82 263
icraggs 8:c46930bd6c82 264 /* 2. if the connect was successful, send the MQTT connect packet */
icraggs 8:c46930bd6c82 265 if (options == 0)
icraggs 8:c46930bd6c82 266 {
icraggs 8:c46930bd6c82 267 default_options.clientID.cstring = "me";
icraggs 8:c46930bd6c82 268 options = &default_options;
icraggs 8:c46930bd6c82 269 }
icraggs 8:c46930bd6c82 270
icraggs 8:c46930bd6c82 271 this->keepalive = options->keepAliveInterval;
icraggs 8:c46930bd6c82 272 len = MQTTSerialize_connect(buf, buflen, options);
icraggs 8:c46930bd6c82 273 printf("len from send is %d %d\n", len, buflen);
icraggs 8:c46930bd6c82 274 rc = sendPacket(len); // send the connect packet
icraggs 8:c46930bd6c82 275 printf("rc from send is %d\n", rc);
icraggs 8:c46930bd6c82 276
icraggs 8:c46930bd6c82 277 /* 3. wait until the connack is received */
icraggs 8:c46930bd6c82 278 if (resultHandler == 0)
icraggs 8:c46930bd6c82 279 {
icraggs 8:c46930bd6c82 280 // this will be a blocking call, wait for the connack
icraggs 8:c46930bd6c82 281 if (cycle() == CONNACK)
icraggs 8:c46930bd6c82 282 {
icraggs 8:c46930bd6c82 283 int connack_rc = -1;
icraggs 8:c46930bd6c82 284 if (MQTTDeserialize_connack(&connack_rc, readbuf, readbuflen) == 1)
icraggs 8:c46930bd6c82 285 rc = connack_rc;
icraggs 8:c46930bd6c82 286 }
icraggs 8:c46930bd6c82 287 }
icraggs 8:c46930bd6c82 288 else
icraggs 8:c46930bd6c82 289 {
icraggs 8:c46930bd6c82 290 // set connect response callback function
icraggs 9:01b8cc7d94cc 291 connectHandler.attach(resultHandler);
icraggs 8:c46930bd6c82 292
icraggs 8:c46930bd6c82 293 // start background thread
icraggs 11:db15da110a37 294 this->thread = new Thread((void (*)(void const *argument))&MQTT::Client<Network, Timer, Thread>::threadfn, (void*)this);
icraggs 8:c46930bd6c82 295 }
icraggs 8:c46930bd6c82 296
icraggs 8:c46930bd6c82 297 return rc;
icraggs 8:c46930bd6c82 298 }
icraggs 8:c46930bd6c82 299
icraggs 8:c46930bd6c82 300
icraggs 9:01b8cc7d94cc 301 template<class Network, class Timer, class Thread> int MQTT::Client<Network, Timer, Thread>::subscribe(const char* topicFilter, enum QoS qos,
icraggs 9:01b8cc7d94cc 302 messageHandler messageHandler, resultHandler resultHandler)
icraggs 8:c46930bd6c82 303 {
icraggs 8:c46930bd6c82 304 int rc = -1,
icraggs 8:c46930bd6c82 305 len = 0;
icraggs 8:c46930bd6c82 306 MQTTString topic = {(char*)topicFilter, 0, 0};
icraggs 8:c46930bd6c82 307
icraggs 9:01b8cc7d94cc 308 len = MQTTSerialize_subscribe(buf, buflen, 0, packetid.getNext(), 1, &topic, (int*)&qos);
icraggs 8:c46930bd6c82 309 rc = sendPacket(len); // send the subscribe packet
icraggs 8:c46930bd6c82 310
icraggs 8:c46930bd6c82 311 /* wait for suback */
icraggs 8:c46930bd6c82 312 if (resultHandler == 0)
icraggs 8:c46930bd6c82 313 {
icraggs 8:c46930bd6c82 314 // this will block
icraggs 8:c46930bd6c82 315 if (cycle() == SUBACK)
icraggs 8:c46930bd6c82 316 {
icraggs 9:01b8cc7d94cc 317 int count = 0, grantedQoS = -1, mypacketid;
icraggs 9:01b8cc7d94cc 318 if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, readbuflen) == 1)
icraggs 8:c46930bd6c82 319 rc = grantedQoS; // 0, 1, 2 or 0x80
icraggs 8:c46930bd6c82 320 }
icraggs 8:c46930bd6c82 321 }
icraggs 8:c46930bd6c82 322 else
icraggs 8:c46930bd6c82 323 {
icraggs 8:c46930bd6c82 324 // set subscribe response callback function
icraggs 8:c46930bd6c82 325
icraggs 8:c46930bd6c82 326 }
icraggs 8:c46930bd6c82 327
icraggs 8:c46930bd6c82 328 return rc;
icraggs 8:c46930bd6c82 329 }
icraggs 8:c46930bd6c82 330
icraggs 8:c46930bd6c82 331
sam_grove 0:fe461e4d7afe 332 #endif