iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Committer:
DuyLionTran
Date:
Thu Dec 07 17:07:44 2017 +0000
Revision:
11:3802c82a5ae9
version 1.5 MQTT works oke

Who changed what in which revision?

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