An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   Cellular_HelloMQTT IoTStarterKit GSwifiInterface_HelloMQTT IBMIoTClientEthernetExample ... more

This library is part of the EclipseTM Paho project; specifically the embedded client.

The goals of this API are:

  1. to be independent of any system library: hence templates parameters for networking, timer and threading classes
  2. not to rely on heap storage, only automatic (I think this is a good thing)
  3. to limit memory use, for instance by defining the size of the buffers and arrays used at object creation time

Files at this revision

API Documentation at this revision

Comitter:
icraggs
Date:
Fri Aug 01 14:45:25 2014 +0000
Parent:
35:063dc3b472d5
Child:
37:e3d64f9b986c
Commit message:
Update types to match MQTTPacket changes

Changed in this revision

MQTTClient.h Show annotated file Show diff for this revision Revisions of this file
MQTTPacket.lib Show annotated file Show diff for this revision Revisions of this file
MQTTSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/MQTTClient.h	Fri Aug 01 13:13:35 2014 +0000
+++ b/MQTTClient.h	Fri Aug 01 14:45:25 2014 +0000
@@ -180,8 +180,8 @@
     Network& ipstack;
     unsigned int command_timeout_ms;
     
-    char buf[MAX_MQTT_PACKET_SIZE];  
-    char readbuf[MAX_MQTT_PACKET_SIZE];  
+    unsigned char buf[MAX_MQTT_PACKET_SIZE];  
+    unsigned char readbuf[MAX_MQTT_PACKET_SIZE];  
 
     Timer ping_timer;
     unsigned int keepAliveInterval;
@@ -252,7 +252,7 @@
 template<class Network, class Timer, int a, int b> 
 int MQTT::Client<Network, Timer, a, b>::decodePacket(int* value, int timeout)
 {
-    char c;
+    unsigned char c;
     int multiplier = 1;
     int len = 0;
     const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
@@ -403,7 +403,7 @@
     /* get one piece of work off the wire and one pass through */
 
     // read the socket, see what work is due
-    int packet_type = readPacket(timer);
+    unsigned short packet_type = readPacket(timer);
     
     int len = 0,
         rc = SUCCESS;
@@ -417,8 +417,8 @@
         case PUBLISH:
             MQTTString topicName;
             Message msg;
-            if (MQTTDeserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (int*)&msg.id, &topicName,
-                                 (char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
+            if (MQTTDeserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
+                                 (unsigned char**)&msg.payload, (int*)&msg.payloadlen, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
                 goto exit;
 //          if (msg.qos != QOS2) 
                 deliverMessage(topicName, msg);
@@ -444,8 +444,8 @@
             }
             break;
         case PUBREC:
-            int type, mypacketid;
-            unsigned char dup;
+            unsigned short mypacketid;
+            unsigned char dup, type;
             if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
                 rc = FAILURE;
             else if ((len = MQTTSerialize_ack(buf, MAX_MQTT_PACKET_SIZE, PUBREL, 0, mypacketid)) <= 0)
@@ -537,7 +537,7 @@
     // this will be a blocking call, wait for the connack
     if (waitfor(CONNACK, connect_timer) == CONNACK)
     {
-        int connack_rc = -1;
+        unsigned char connack_rc = 255;
         if (MQTTDeserialize_connack(&connack_rc, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
             rc = connack_rc;
         else
@@ -572,7 +572,8 @@
     
     if (waitfor(SUBACK, timer) == SUBACK)      // wait for suback 
     {
-        int count = 0, grantedQoS = -1, mypacketid;
+        int count = 0, grantedQoS = -1;
+        unsigned short mypacketid;
         if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
             rc = grantedQoS; // 0, 1, 2 or 0x80 
         if (rc != 0x80)
@@ -615,7 +616,7 @@
     
     if (waitfor(UNSUBACK, timer) == UNSUBACK)
     {
-        int mypacketid;  // should be the same as the packetid above
+        unsigned short mypacketid;  // should be the same as the packetid above
         if (MQTTDeserialize_unsuback(&mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) == 1)
             rc = 0; 
     }
@@ -643,7 +644,7 @@
         message->id = packetid.getNext();
     
     len = MQTTSerialize_publish(buf, MAX_MQTT_PACKET_SIZE, 0, message->qos, message->retained, message->id, 
-              topicString, (char*)message->payload, message->payloadlen);
+              topicString, (unsigned char*)message->payload, message->payloadlen);
     if (len <= 0)
         goto exit;
     if ((rc = sendPacket(len, timer)) != SUCCESS) // send the subscribe packet
@@ -653,8 +654,8 @@
     {
         if (waitfor(PUBACK, timer) == PUBACK)
         {
-            int type, mypacketid;
-            unsigned char dup;
+            unsigned short mypacketid;
+            unsigned char dup, type;
             if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
                 rc = FAILURE;
         }
@@ -665,8 +666,8 @@
     {
         if (waitfor(PUBCOMP, timer) == PUBCOMP)
         {
-            int type, mypacketid;
-            unsigned char dup;
+            unsigned short mypacketid;
+            unsigned char dup, type;
             if (MQTTDeserialize_ack(&type, &dup, &mypacketid, readbuf, MAX_MQTT_PACKET_SIZE) != 1)
                 rc = FAILURE;
         }
--- a/MQTTPacket.lib	Fri Aug 01 13:13:35 2014 +0000
+++ b/MQTTPacket.lib	Fri Aug 01 14:45:25 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/mqtt/code/MQTTPacket/#5e60cd1a52e7
+http://mbed.org/teams/mqtt/code/MQTTPacket/#c2052aee81de
--- a/MQTTSocket.h	Fri Aug 01 13:13:35 2014 +0000
+++ b/MQTTSocket.h	Fri Aug 01 14:45:25 2014 +0000
@@ -13,16 +13,16 @@
         return mysock.connect(hostname, port);
     }
 
-    int read(char* buffer, int len, int timeout)
+    int read(unsigned char* buffer, int len, int timeout)
     {
         mysock.set_blocking(false, timeout);  
-        return mysock.receive(buffer, len);
+        return mysock.receive((char*)buffer, len);
     }
     
-    int write(char* buffer, int len, int timeout)
+    int write(unsigned char* buffer, int len, int timeout)
     {
         mysock.set_blocking(false, timeout);  
-        return mysock.send(buffer, len);
+        return mysock.send((char*)buffer, len);
     }
     
     int disconnect()