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
Revision:
5:389ccac5a50c
Parent:
4:4ef00243708e
Child:
6:4d312a49200b
--- a/MQTTClient.cpp	Mon Apr 07 12:24:36 2014 +0000
+++ b/MQTTClient.cpp	Mon Apr 07 23:52:57 2014 +0100
@@ -26,10 +26,11 @@
 template<class Network, class Thread> MQTT::Client<Network, Thread>::Client(Network* network, const int buffer_size, const int command_timeout)
 {
     
-   buf = new char[buffer_size];
+   buf = new char[buffer_size];
+   readbuf = new char[buffer_size];
    this->ipstack = ipstack;
    this->command_timeout = command_timeout;
-   this->thread = new Thread(0); // only need a background thread for non-blocking mode
+   //this->thread = new Thread(0); // only need a background thread for non-blocking mode
    this->ipstack = network;
 }
 
@@ -93,7 +94,7 @@
     if ((rc = ipstack->read(readbuf + len, rem_len, timeout)) != rem_len)
         goto exit;
 
-    header.byte = buf[0];
+    header.byte = readbuf[0];
     rc = header.bits.type;
 exit:
     return rc;
@@ -106,11 +107,14 @@
     /* get one piece of work off the wire and one pass through */
     
     // 1. read the socket, see what work is due. 
-    int packet_type = readPacket(-1);
+    int packet_type = readPacket(-1);
+
+	printf("packet type %d\n", packet_type);
     
     switch (packet_type)
     {
-        case CONNACK:
+        case CONNACK:
+			printf("connack received\n");
             break;
         case PUBACK:
             break;
@@ -129,24 +133,36 @@
 
 template<class Network, class Thread> int MQTT::Client<Network, Thread>::connect(MQTTPacket_connectData* options, FP<void, MQTT::Result*> *resultHandler)
 {
-    /* 1. connect to the server with the desired transport */
-    /*if (!ipstack->connect())
-        return -99;*/
-    
-    /* 2. if the connect was successful, send the MQTT connect packet */        
-    int len = MQTTSerialize_connect(buf, buflen, options);
-    sendPacket(len); // send the connect packet
+	int len = 0;
+	int rc = -99;
+
+    /* 2. if the connect was successful, send the MQTT connect packet */   
+	if (options == 0)
+	{
+		MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
+		default_options.clientID.cstring = "me";
+		len = MQTTSerialize_connect(buf, buflen, &default_options);
+	}
+	else
+		len = MQTTSerialize_connect(buf, buflen, options);
+    rc = sendPacket(len); // send the connect packet
+	printf("rc from send is %d\n", rc);
     
     /* 3. wait until the connack is received */
     if (resultHandler == 0)
     {
-        // this will be a blocking call, wait for the connack
-        //waitfor(CONNACK);    
+        // this will be a blocking call, wait for the connack
+		if (cycle() == CONNACK)
+		{
+			int connack_rc = -1;
+			if (MQTTDeserialize_connack(&connack_rc, readbuf, readbuflen) == 1)
+				rc = connack_rc;
+		}
     }
     else
     {
         // set connect response callback function
     }
     
-    return len;
+    return rc;
 }