MQTTClient

Dependents:   IoTGateway_Basic test

Files at this revision

API Documentation at this revision

Comitter:
SomeRandomBloke
Date:
Thu Apr 05 07:50:30 2012 +0000
Parent:
8:f7afec229461
Commit message:
quick fix to a length

Changed in this revision

MQTTClient.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f7afec229461 -r a0e39cea763a MQTTClient.cpp
--- a/MQTTClient.cpp	Wed Apr 04 22:00:28 2012 +0000
+++ b/MQTTClient.cpp	Thu Apr 05 07:50:30 2012 +0000
@@ -36,8 +36,8 @@
 MQTTClient::~MQTTClient() {}
 
 /** Alternative Constructor with parameters
- * 
- * Allow object to be constructed with minimum parameters. 
+ *
+ * Allow object to be constructed with minimum parameters.
  *
  * @param server The IP address of the server to connect to
  * @param port   The TCP/IP port on the server to connect to
@@ -47,6 +47,8 @@
     this->port = port;
     callback_server = callback;
     serverIp = server;
+    this->userName = NULL;
+    this->password = NULL;
     connected = false;
     sessionOpened = false;
     timer.start();
@@ -54,7 +56,7 @@
 
 
 /** MQTT initialisation method
- * 
+ *
  * Used when default constructor used and need to specify parameters at runtime
  *
  * @param server The IP address of the server to connect to
@@ -73,7 +75,7 @@
 }
 
 /** A brief description of the function foo
- * 
+ *
  * More details about the function goes here
  * and here
  *
@@ -95,7 +97,7 @@
 }
 
 /** Send a message of specified size
- * 
+ *
  * @param msg  message string
  * @param size Size of the message to send
  * @returns value to indicate message sent successfully or not, -1 for error, 1 for success.
@@ -115,9 +117,9 @@
 }
 
 /** Start a MQTT session, build CONNECT packet
- * 
+ *
  * @param id The client name shown on MQTT server.
- * @returns -1 for error, 1 for success   
+ * @returns -1 for error, 1 for success
  */
 int MQTTClient::open_session(char* id) {
     /*variable header*/
@@ -131,7 +133,7 @@
     var_header[9] |= (passwordLen > 0 ? 0x40 : 0x00 );
 
 //    printf("fixed %d, var %d id %d, username %d, password %d\n",sizeof(fixed_header), sizeof(var_header), sizeof(id), usernameLen, passwordLen );
-    char packet[sizeof(fixed_header) + sizeof(var_header) + sizeof(id)  + 6 + usernameLen + (usernameLen > 0 ? 2 : 0) + passwordLen + (passwordLen > 0 ? 2 : 0) ];
+    char packet[sizeof(fixed_header) + sizeof(var_header) + sizeof(id) + 6 + usernameLen + (usernameLen > 0 ? 2 : 0) + passwordLen + (passwordLen > 0 ? 2 : 0) ];
 
     memset(packet,0,sizeof(packet));
     memcpy(packet,fixed_header,sizeof(fixed_header));
@@ -164,9 +166,9 @@
 }
 
 /** Open TCP port, connect to server on given IP address.
- * 
+ *
  * @param id The client name shown on MQTT server.
- * @returns -1: If connect to server failed. -2: Failed to open session on server.  1: Connection accessed.  
+ * @returns -1: If connect to server failed. -2: Failed to open session on server.  1: Connection accessed.
  */
 int MQTTClient::connect(char* id) {
     clientId = id;
@@ -188,7 +190,7 @@
         printf("Error connecting to host [%d]\r\n", (int) err);
         return -1;
     }
-    printf("Connect to host sucessed..\r\n\r\n");
+    printf("Connect to host..\r\n\r\n");
 
     /*Wait TCP connection with server to be established*/
     int i = 0;
@@ -210,10 +212,10 @@
     while (!sessionOpened) {
         Net::poll();
         wait(1);
-        if (!connected || ++i >30) {
+        if (!connected || ++i >40) {
             break;
         }
-        printf("Wait for session..\r\n");
+        printf("Wait for session %d..\r\n", i);
     }
     if (!connected) {
         return -2;
@@ -226,7 +228,7 @@
  *
  * @param pub_topic  The topic name the massage will be publish on.
  * @param msg        The massage to be published.
- * @returns  -1: Failed to publish message. 1: Publish sucessed.  
+ * @returns  -1: Failed to publish message. 1: Publish sucessed.
  */
 int MQTTClient::publish(char* pub_topic, char* msg) {
     uint8_t var_header_pub[strlen(pub_topic)+3];
@@ -259,7 +261,7 @@
 }
 
 /** Read data from receive packet
- * Determine what needs to be done with packet. 
+ * Determine what needs to be done with packet.
  */
 void MQTTClient::read_data() {
     char buffer[1024];
@@ -287,15 +289,15 @@
         if (callback_server) {
             short index = 1;
             short multiplier = 1;
-            short value = 0; 
+            short value = 0;
             uint8_t  digit;
             do {
-                digit = buffer[index++]; 
-                value += (digit & 127) * multiplier; 
+                digit = buffer[index++];
+                value += (digit & 127) * multiplier;
                 multiplier *= 128;
             } while ((digit & 128) != 0);
             printf( "variable length %d, index %d\n", value, index );
-            
+
 //            uint8_t tl = (buffer[2]<<3)+buffer[3];
             uint8_t tl = (buffer[index]<<3)+buffer[index+1];
             printf("Topic len %d\n",tl);
@@ -332,9 +334,9 @@
 }
 
 /** Subscribe to a topic
- * 
+ *
  * @param topic The topic name to be subscribed.
- * @returns  -1: Failed to subscribe to topic. 1: Subscribe sucessed.  
+ * @returns  -1: Failed to subscribe to topic. 1: Subscribe sucessed.
  */
 int MQTTClient::subscribe(char* topic) {
 
@@ -365,7 +367,7 @@
 }
 
 /** Send heartbeat/keep-alive message
- * 
+ *
  */
 void MQTTClient::live() {
     if (connected) {