An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   IBMIoTClientEthernetExample_W5500 IBMIoTClientEthernetExample_W5200

Fork of MQTT by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTClient.h Source File

MQTTClient.h

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016  
00017  /*
00018  
00019  TODO: 
00020  
00021  ensure publish packets are retried on reconnect
00022  
00023  */
00024 
00025 #if !defined(MQTTCLIENT_H)
00026 #define MQTTCLIENT_H
00027 
00028 #include "FP.h"
00029 #include "MQTTPacket.h"
00030 #include "stdio.h"
00031 #include "MQTT_logging.h"
00032 
00033 namespace MQTT
00034 {
00035 
00036 
00037 enum QoS { QOS0, QOS1, QOS2 };
00038 
00039 // all failure return codes must be negative
00040 enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
00041 
00042 
00043 struct Message
00044 {
00045     enum QoS qos;
00046     bool retained;
00047     bool dup;
00048     unsigned short id;
00049     void *payload;
00050     size_t payloadlen;
00051 };
00052 
00053 
00054 struct MessageData
00055 {
00056     MessageData(MQTTString &aTopicName, struct Message &aMessage)  : message(aMessage), topicName(aTopicName)
00057     { }
00058     
00059     struct Message &message;
00060     MQTTString &topicName;
00061 };
00062 
00063 
00064 class PacketId
00065 {
00066 public:
00067     PacketId()
00068     {
00069         next = 0;
00070     }
00071     
00072     int getNext()
00073     {
00074         return next = (next == MAX_PACKET_ID) ? 1 : ++next;
00075     }
00076    
00077 private:
00078     static const int MAX_PACKET_ID = 65535;
00079     int next;
00080 };
00081 
00082 
00083 class QoS2
00084 {
00085 public:
00086 
00087     
00088 private:
00089 
00090 
00091 };
00092   
00093   
00094 /**
00095  * @class Client
00096  * @brief blocking, non-threaded MQTT client API
00097  * 
00098  * This version of the API blocks on all method calls, until they are complete.  This means that only one
00099  * MQTT request can be in process at any one time.  
00100  * @param Network a network class which supports send, receive
00101  * @param Timer a timer class with the methods: 
00102  */ 
00103 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5>
00104 class Client
00105 {
00106     
00107 public:
00108    
00109     typedef void (*messageHandler)(MessageData&);
00110 
00111     /** Construct the client
00112      *  @param network - pointer to an instance of the Network class - must be connected to the endpoint
00113      *      before calling MQTT connect
00114      *  @param limits an instance of the Limit class - to alter limits as required
00115      */
00116     Client(Network& network, unsigned int command_timeout_ms = 30000); 
00117     
00118     /** Set the default message handling callback - used for any message which does not match a subscription message handler
00119      *  @param mh - pointer to the callback function
00120      */
00121     void setDefaultMessageHandler(messageHandler mh)
00122     {
00123         defaultMessageHandler.attach(mh);
00124     }
00125     
00126     void setLogHandler()
00127     {
00128        // logHandler.attach(lh);
00129     }
00130 
00131     
00132     /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
00133      *  The nework object must be connected to the network endpoint before calling this 
00134      *  @param options - connect options
00135      *  @return success code -  
00136      */       
00137     int connect(MQTTPacket_connectData* options = 0);
00138       
00139     /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
00140      *  @param topic - the topic to publish to
00141      *  @param message - the message to send
00142      *  @return success code -  
00143      */      
00144     int publish(const char* topicName, Message* message);
00145    
00146     /** MQTT Subscribe - send an MQTT subscribe packet and wait for the suback
00147      *  @param topicFilter - a topic pattern which can include wildcards
00148      *  @param qos - the MQTT QoS to subscribe at
00149      *  @param mh - the callback function to be invoked when a message is received for this subscription
00150      *  @return success code -  
00151      */   
00152     int subscribe(const char* topicFilter, enum QoS qos, messageHandler mh);
00153     
00154     /** MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback
00155      *  @param topicFilter - a topic pattern which can include wildcards
00156      *  @return success code -  
00157      */   
00158     int unsubscribe(const char* topicFilter);
00159     
00160     /** MQTT Disconnect - send an MQTT disconnect packet, and clean up any state
00161      *  @return success code -  
00162      */
00163     int disconnect();
00164     
00165     /** A call to this API must be made within the keepAlive interval to keep the MQTT connection alive
00166      *  yield can be called if no other MQTT operation is needed.  This will also allow messages to be 
00167      *  received.
00168      *  @param timeout_ms the time to wait, in milliseconds
00169      *  @return success code - on failure, this means the client has disconnected
00170      */
00171     int yield(int timeout_ms = 1000);
00172     
00173 private:
00174 
00175     int cycle(Timer& timer);
00176     int waitfor(int packet_type, Timer& timer);
00177     int keepalive();
00178 
00179     int decodePacket(int* value, int timeout);
00180     int readPacket(Timer& timer);
00181     int sendPacket(int length, Timer& timer);
00182     int deliverMessage(MQTTString& topicName, Message& message);
00183     bool isTopicMatched(char* topicFilter, MQTTString& topicName);
00184     
00185     Network& ipstack;
00186     unsigned int command_timeout_ms;
00187     
00188     unsigned char buf[MAX_MQTT_PACKET_SIZE];  
00189     unsigned char readbuf[MAX_MQTT_PACKET_SIZE];  
00190 
00191     Timer ping_timer;
00192     unsigned int keepAliveInterval;
00193     bool ping_outstanding;
00194     
00195     PacketId packetid;
00196     
00197     struct MessageHandlers
00198     {
00199         const char* topicFilter;
00200         FP<void, MessageData&> fp;
00201     } messageHandlers[MAX_MESSAGE_HANDLERS];      // Message handlers are indexed by subscription topic
00202     
00203     FP<void, MessageData&> defaultMessageHandler;
00204      
00205     bool isconnected;
00206     
00207 #if 0
00208     struct
00209     {
00210       bool used;
00211       int id;  
00212     } QoS2messages[MAX_QOS2_MESSAGES];
00213     
00214 #endif
00215 
00216 };
00217 
00218 }
00219 
00220 
00221 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> 
00222 MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::Client(Network& network, unsigned int command_timeout_ms)  : ipstack(network), packetid()
00223 {
00224     ping_timer = Timer();
00225     ping_outstanding = 0;
00226     for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
00227         messageHandlers[i].topicFilter = 0;
00228     this->command_timeout_ms = command_timeout_ms; 
00229     isconnected = false;
00230 }
00231 
00232 
00233 template<class Network, class Timer, int a, int b> 
00234 int MQTT::Client<Network, Timer, a, b>::sendPacket(int length, Timer& timer)
00235 {
00236     int rc = FAILURE, 
00237         sent = 0;
00238     
00239     while (sent < length && !timer.expired())
00240     {
00241         rc = ipstack.write(&buf[sent], length, timer.left_ms());
00242         if (rc < 0)  // there was an error writing the data
00243             break;
00244         sent += rc;
00245     }
00246     if (sent == length)
00247     {
00248         ping_timer.countdown(this->keepAliveInterval); // record the fact that we have successfully sent the packet    
00249         rc = SUCCESS;
00250         //if (debug)
00251         // Log (packet)
00252     }
00253     else
00254         rc = FAILURE;
00255     return rc;
00256 }
00257 
00258 
00259 template<class Network, class Timer, int a, int b> 
00260 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
00261 {
00262     unsigned char c;
00263     int multiplier = 1;
00264     int len = 0;
00265     const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
00266 
00267     *value = 0;
00268     do
00269     {
00270         int rc = MQTTPACKET_READ_ERROR;
00271 
00272         if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
00273         {
00274             rc = MQTTPACKET_READ_ERROR; /* bad data */
00275             goto exit;
00276         }
00277         rc = ipstack.read(&c, 1, timeout);
00278         if (rc != 1)
00279             goto exit;
00280         *value += (c & 127) * multiplier;
00281         multiplier *= 128;
00282     } while ((c & 128) != 0);
00283 exit:
00284     return len;
00285 }
00286 
00287 
00288 /**
00289  * If any read fails in this method, then we should disconnect from the network, as on reconnect
00290  * the packets can be retried. 
00291  * @param timeout the max time to wait for the packet read to complete, in milliseconds
00292  * @return the MQTT packet type, or -1 if none
00293  */
00294 template<class Network, class Timer, int a, int b> 
00295 int MQTT::Client<Network, Timer, a, b>::readPacket(Timer& timer) 
00296 {
00297     int rc = FAILURE;
00298     MQTTHeader header = {0};
00299     int len = 0;
00300     int rem_len = 0;
00301 
00302     /* 1. read the header byte.  This has the packet type in it */
00303     if (ipstack.read(readbuf, 1, timer.left_ms()) != 1)
00304         goto exit;
00305 
00306     len = 1;
00307     /* 2. read the remaining length.  This is variable in itself */
00308     decodePacket(&rem_len, timer.left_ms());
00309     len += MQTTPacket_encode(readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
00310 
00311     /* 3. read the rest of the buffer using a callback to supply the rest of the data */
00312     if (ipstack.read(readbuf + len, rem_len, timer.left_ms()) != rem_len)
00313         goto exit;
00314 
00315     header.byte = readbuf[0];
00316     rc = header.bits.type;
00317 exit:
00318     return rc;
00319 }
00320 
00321 
00322 // assume topic filter and name is in correct format
00323 // # can only be at end
00324 // + and # can only be next to separator
00325 template<class Network, class Timer, int a, int b> 
00326 bool MQTT::Client<Network, Timer, a, b>::isTopicMatched(char* topicFilter, MQTTString& topicName)
00327 {
00328     char* curf = topicFilter;
00329     char* curn = topicName.lenstring.data;
00330     char* curn_end = curn + topicName.lenstring.len;
00331     
00332     while (*curf && curn < curn_end)
00333     {
00334         if (*curn == '/' && *curf != '/')
00335             break;
00336         if (*curf != '+' && *curf != '#' && *curf != *curn)
00337             break;
00338         if (*curf == '+')
00339         {   // skip until we meet the next separator, or end of string
00340             char* nextpos = curn + 1;
00341             while (nextpos < curn_end && *nextpos != '/')
00342                 nextpos = ++curn + 1;
00343         }
00344         else if (*curf == '#')
00345             curn = curn_end - 1;    // skip until end of string
00346         curf++;
00347         curn++;
00348     };
00349     
00350     return (curn == curn_end) && (*curf == '\0');
00351 }
00352 
00353 
00354 
00355 template<class Network, class Timer, int a, int MAX_MESSAGE_HANDLERS> 
00356 int MQTT::Client<Network, Timer, a, MAX_MESSAGE_HANDLERS>::deliverMessage(MQTTString& topicName, Message& message)
00357 {
00358     int rc = FAILURE;
00359 
00360     // we have to find the right message handler - indexed by topic
00361     for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
00362     {
00363         if (messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(&topicName, (char*)messageHandlers[i].topicFilter) ||
00364                 isTopicMatched((char*)messageHandlers[i].topicFilter, topicName)))
00365         {
00366             if (messageHandlers[i].fp.attached())
00367             {
00368                 MessageData md(topicName, message);
00369                 messageHandlers[i].fp(md);
00370                 rc = SUCCESS;
00371             }
00372         }
00373     }
00374     
00375     if (rc == FAILURE && defaultMessageHandler.attached()) 
00376     {
00377         MessageData md(topicName, message);
00378         defaultMessageHandler(md);
00379         rc = SUCCESS;
00380     }   
00381     
00382     return rc;
00383 }
00384 
00385 
00386 
00387 template<class Network, class Timer, int a, int b> 
00388 int MQTT::Client<Network, Timer, a, b>::yield(int timeout_ms)
00389 {
00390     int rc = SUCCESS;
00391     Timer timer = Timer();
00392     
00393     timer.countdown_ms(timeout_ms);
00394     while (!timer.expired())
00395     {
00396         if (cycle(timer) == FAILURE)
00397         {
00398             rc = FAILURE;
00399             break;
00400         }
00401     }
00402         
00403     return rc;
00404 }
00405 
00406 
00407 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> 
00408 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::cycle(Timer& timer)
00409 {
00410     /* get one piece of work off the wire and one pass through */
00411 
00412     // read the socket, see what work is due
00413     unsigned short packet_type = readPacket(timer);
00414     
00415     int len = 0,
00416         rc = SUCCESS;
00417 
00418     switch (packet_type)
00419     {
00420         case CONNACK:
00421         case PUBACK:
00422         case SUBACK:
00423             break;
00424         case PUBLISH:
00425             MQTTString topicName;
00426             Message msg;
00427             if (MQTTDeserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
00428                                  (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
00429                 goto exit;
00430 //          if (msg.qos != QOS2) 
00431                 deliverMessage(topicName, msg);
00432 #if 0
00433             else if (isQoS2msgidFree(msg.id))
00434             {
00435                 UseQoS2msgid(msg.id);
00436                 deliverMessage(topicName, msg);
00437             }
00438 #endif
00439             if (msg.qos != QOS0)
00440             {
00441                 if (msg.qos == QOS1)
00442                     len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBACK, 0, msg.id);
00443                 else if (msg.qos == QOS2)
00444                     len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREC, 0, msg.id);
00445                 if (len <= 0)
00446                     rc = FAILURE;
00447                 else
00448                     rc = sendPacket(len, timer);
00449                 if (rc == FAILURE)
00450                     goto exit; // there was a problem
00451             }
00452             break;
00453         case PUBREC:
00454             unsigned short mypacketid;
00455             unsigned char dup, type;
00456             if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
00457                 rc = FAILURE;
00458             else if ((len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, mypacketid)) <= 0)
00459                 rc = FAILURE;
00460             else if ((rc = sendPacket(len, timer)) != SUCCESS) // send the PUBREL packet
00461                 rc = FAILURE; // there was a problem
00462             if (rc == FAILURE)
00463                 goto exit; // there was a problem
00464             break;
00465         case PUBCOMP:
00466             break;
00467         case PINGRESP:
00468             ping_outstanding = false;
00469             break;
00470     }
00471     keepalive();
00472 exit:
00473     if (rc == SUCCESS)
00474         rc = packet_type;
00475     return rc;
00476 }
00477 
00478 
00479 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b>
00480 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::keepalive()
00481 {
00482     int rc = FAILURE;
00483 
00484     if (keepAliveInterval == 0)
00485     {
00486         rc = SUCCESS;
00487         goto exit;
00488     }
00489 
00490     if (ping_timer.expired())
00491     {
00492         if (!ping_outstanding)
00493         {
00494             Timer timer = Timer(1000);
00495             int len = MQTTSerialize_pingreq(buf, MAX_MQTT_PACKET_SIZE);
00496             if (len > 0 && (rc = sendPacket(len, timer)) == SUCCESS) // send the ping packet
00497                 ping_outstanding = true;
00498         }
00499     }
00500 
00501 exit:
00502     return rc;
00503 }
00504 
00505 
00506 // only used in single-threaded mode where one command at a time is in process
00507 template<class Network, class Timer, int a, int b> 
00508 int MQTT::Client<Network, Timer, a, b>::waitfor(int packet_type, Timer& timer)
00509 {
00510     int rc = FAILURE;
00511     
00512     do
00513     {
00514         if (timer.expired()) 
00515             break; // we timed out
00516     }
00517     while ((rc = cycle(timer)) != packet_type);  
00518     
00519     return rc;
00520 }
00521 
00522 
00523 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> 
00524 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::connect(MQTTPacket_connectData* options)
00525 {
00526     Timer connect_timer = Timer(command_timeout_ms);
00527     int rc = FAILURE;
00528     MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
00529     int len = 0;
00530     
00531     if (isconnected) // don't send connect packet again if we are already connected
00532         goto exit;
00533 
00534     if (options == 0)
00535         options = &default_options; // set default options if none were supplied
00536 
00537     this->keepAliveInterval = options->keepAliveInterval;
00538     ping_timer.countdown(this->keepAliveInterval);
00539     if ((len = MQTTSerialize_connect(buf, MAX_MQTT_PACKET_SIZE, options)) <= 0)
00540         goto exit;
00541     if ((rc = sendPacket(len, connect_timer)) != SUCCESS)  // send the connect packet
00542         goto exit; // there was a problem
00543     
00544     // this will be a blocking call, wait for the connack
00545     if (waitfor(CONNACK, connect_timer) == CONNACK)
00546     {
00547         unsigned char connack_rc = 255;
00548         bool sessionPresent = false;
00549         if (MQTTDeserialize_connack((unsigned char*)&sessionPresent, &connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
00550             rc = connack_rc;
00551         else
00552             rc = FAILURE;
00553     }
00554     else
00555         rc = FAILURE;
00556     
00557 exit:
00558     if (rc == SUCCESS)
00559         isconnected = true;
00560     return rc;
00561 }
00562 
00563 
00564 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> 
00565 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::subscribe(const char* topicFilter, enum QoS qos, messageHandler messageHandler)
00566 { 
00567     int rc = FAILURE;  
00568     Timer timer = Timer(command_timeout_ms);
00569     int len = 0;
00570     MQTTString topic = {(char*)topicFilter, 0, 0};
00571     
00572     if (!isconnected)
00573         goto exit;
00574     
00575     len = MQTTSerialize_subscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic, (int*)&qos);
00576     if (len <= 0)
00577         goto exit;
00578     if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
00579         goto exit;             // there was a problem
00580     
00581     if (waitfor(SUBACK, timer) == SUBACK)      // wait for suback 
00582     {
00583         int count = 0, grantedQoS = -1;
00584         unsigned short mypacketid;
00585         if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
00586             rc = grantedQoS; // 0, 1, 2 or 0x80 
00587         if (rc != 0x80)
00588         {
00589             for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
00590             {
00591                 if (messageHandlers[i].topicFilter == 0)
00592                 {
00593                     messageHandlers[i].topicFilter = topicFilter;
00594                     messageHandlers[i].fp.attach(messageHandler);
00595                     rc = 0;
00596                     break;
00597                 }
00598             }
00599         }
00600     }
00601     else 
00602         rc = FAILURE;
00603         
00604 exit:
00605     return rc;
00606 }
00607 
00608 
00609 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int MAX_MESSAGE_HANDLERS> 
00610 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS>::unsubscribe(const char* topicFilter)
00611 {   
00612     int rc = FAILURE;
00613     Timer timer = Timer(command_timeout_ms);    
00614     MQTTString topic = {(char*)topicFilter, 0, 0};
00615     int len = 0;
00616     
00617     if (!isconnected)
00618         goto exit;
00619     
00620     if ((len = MQTTSerialize_unsubscribe(buf, MAX_MQTT_PACKET_SIZE, 0, packetid.getNext(), 1, &topic)) <= 0)
00621         goto exit;
00622     if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
00623         goto exit; // there was a problem
00624     
00625     if (waitfor(UNSUBACK, timer) == UNSUBACK)
00626     {
00627         unsigned short mypacketid;  // should be the same as the packetid above
00628         if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
00629             rc = 0; 
00630     }
00631     else
00632         rc = FAILURE;
00633     
00634 exit:
00635     return rc;
00636 }
00637 
00638 
00639    
00640 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> 
00641 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::publish(const char* topicName, Message* message)
00642 {
00643     int rc = FAILURE;
00644     Timer timer = Timer(command_timeout_ms);   
00645     MQTTString topicString = {(char*)topicName, 0, 0};
00646     int len = 0;
00647     
00648     if (!isconnected)
00649         goto exit;
00650 
00651     if (message->qos == QOS1 || message->qos == QOS2)
00652         message->id = packetid.getNext();
00653     
00654     len = MQTTSerialize_publish(buf, MAX_MQTT_PACKET_SIZE, 0, message->qos, message->retained, message->id, 
00655               topicString, (unsigned char*)message->payload, message->payloadlen);
00656     if (len <= 0)
00657         goto exit;
00658     if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
00659         goto exit; // there was a problem
00660     
00661     if (message->qos == QOS1)
00662     {
00663         if (waitfor(PUBACK, timer) == PUBACK)
00664         {
00665             unsigned short mypacketid;
00666             unsigned char dup, type;
00667             if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
00668                 rc = FAILURE;
00669         }
00670         else
00671             rc = FAILURE;
00672     }
00673     else if (message->qos == QOS2)
00674     {
00675         if (waitfor(PUBCOMP, timer) == PUBCOMP)
00676         {
00677             unsigned short mypacketid;
00678             unsigned char dup, type;
00679             if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
00680                 rc = FAILURE;
00681         }
00682         else
00683             rc = FAILURE;
00684     }
00685     
00686 exit:
00687     return rc;
00688 }
00689 
00690 
00691 template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE, int b> 
00692 int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::disconnect()
00693 {  
00694     int rc = FAILURE;
00695     Timer timer = Timer(command_timeout_ms);     // we might wait for incomplete incoming publishes to complete
00696     int len = MQTTSerialize_disconnect(buf, MAX_MQTT_PACKET_SIZE);
00697     if (len > 0)
00698         rc = sendPacket(len, timer);            // send the disconnect packet
00699         
00700     isconnected = false;
00701     return rc;
00702 }
00703 
00704 
00705 #endif