Fork of Hello MQTT, using mbed TLS for secure mqtt transport

Dependencies:   MQTT

Fork of HelloMQTT by MQTT

Files at this revision

API Documentation at this revision

Comitter:
vpcola
Date:
Sat Mar 18 01:54:50 2017 +0000
Parent:
21:4534812bb94f
Commit message:
Output to serial only

Changed in this revision

C12832.lib Show diff for this revision Revisions of this file
MQTTSNetwork.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/C12832.lib	Fri Mar 17 08:42:29 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/teams/components/code/C12832/#03069e3deaa4
--- a/MQTTSNetwork.h	Fri Mar 17 08:42:29 2017 +0000
+++ b/MQTTSNetwork.h	Sat Mar 18 01:54:50 2017 +0000
@@ -121,18 +121,13 @@
         printf("mbedtls_ssl_read returned %d\r\n", ret);
         if (ret < 0) {
             if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) 
-            {
-                // Some other error we can not recover from
                 print_mbedtls_error("mbedtls_ssl_read", ret);
-                onError(tcpsocket, -1 );
-            }
-            else {
-                // timeout occurred ...
+            else 
                 printf("Timed out? ...\r\n");
-                _error = ret;
-            }
+
+            _error = ret;            
             printf("MQTTS client read returns with error!!!...\r\n");
-            return -1;
+            return ret;
         }
         printf("MQTS client read successfully!! ...\r\n");
         return ret ;
@@ -149,12 +144,12 @@
         if (ret < 0) {
             if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
                 print_mbedtls_error("mbedtls_ssl_write", ret);
-                onError(tcpsocket, -1 );
+                //onError(tcpsocket, -1 );
             }
             else {
                 _error = ret;
             }
-            return -1;
+            return ret;
         }
         
         return ret;        
@@ -172,7 +167,10 @@
         // Save the hostname and port on first connect
         // Create the socket 
         if (tcpsocket == NULL)
+        {
+            printf("Creating TCP socket ...\r\n");
             tcpsocket = new TCPSocket(network);
+        }
             
         if (tcpsocket == NULL)
             ret = -1;
--- a/main.cpp	Fri Mar 17 08:42:29 2017 +0000
+++ b/main.cpp	Sat Mar 18 01:54:50 2017 +0000
@@ -103,6 +103,7 @@
     "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n"
     "-----END CERTIFICATE-----";
 
+Serial pc(USBTX, USBRX, 115200);
 int arrivedcount = 0;
 Thread thdMQTT;
 
@@ -118,8 +119,8 @@
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
-    printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
-    printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
+    pc.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
+    pc.printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
     ++arrivedcount;
 }
 
@@ -130,33 +131,33 @@
 {
     int rc;
     
-    printf("Connecting to %s:%d\r\n", hostname, port);
+    pc.printf("Connecting to %s:%d\r\n", hostname, port);
     rc = mqttNetwork.connect(hostname, port);
     if (rc != 0)
     {
-        printf("rc from TCP connect is %d\r\n", rc);
+        pc.printf("rc from TCP connect is %d\r\n", rc);
         return -1;
     }
     else
-        printf("RC passed!\r\n");
+        pc.printf("RC passed!\r\n");
  
-    printf("Creating data connection ...\r\n");
+    pc.printf("Creating data connection ...\r\n");
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
     data.MQTTVersion = 3;
     data.clientID.cstring = (char *) clientID;
     data.username.cstring = (char *) username;
     data.password.cstring = (char *) password;
-    printf("Connecting client ...\r\n");
+    pc.printf("Connecting client ...\r\n");
     if ((rc = client.connect(data)) != 0)
     {
         printf("rc from MQTT connect is %d\r\n", rc);
         return -1;
     }
  
-    printf("Subscribing to topic ...\r\n");
+    pc.printf("Subscribing to topic ...\r\n");
     if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0)
     {
-        printf("rc from MQTT subscribe is %d\r\n", rc);
+        pc.printf("rc from MQTT subscribe is %d\r\n", rc);
         return -1;
     }
 
@@ -167,9 +168,9 @@
 {
     int rc;
     
-    printf("Subscribing to topic ...\r\n");
+    pc.printf("Subscribing to topic ...\r\n");
     if ((rc = client.subscribe(topic, os, handler)) != 0)
-        printf("rc from MQTT subscribe is %d\r\n", rc);
+        pc.printf("rc from MQTT subscribe is %d\r\n", rc);
 
     return rc;        
 }
@@ -183,42 +184,42 @@
     *message = msg;
     
     // Push the data to the consumer thread
-    printf("Pushing data to MQTTS Listener thread ...\r\n");
+    pc.printf("Pushing data to MQTTS Listener thread ...\r\n");
     queue.put(message);       
 }
 
 void mqttListener(void)
 {
-    printf("MQTT listener thread started ...\r\n");
+    pc.printf("MQTT listener thread started ...\r\n");
     while(true)
     {
         // Wait for data in the queue, timeout at 10ms
         osEvent evt = queue.get(10);
         if (evt.status == osEventMessage) {
-            printf("Message arrived from main thread ...\r\n");
+            pc.printf("Message arrived from main thread ...\r\n");
             // Unpack the message
             MQTT::Message * message = (MQTT::Message *)evt.value.p;
             
-            printf("Publishing message to MQTT ...\r\n");
+            pc.printf("Publishing message to MQTT ...\r\n");
             // Push to mqtt
             int rc = client.publish(topic, *message);
             if (rc < 0)
-                printf("Error sending mqtt message \r\n");
+                pc.printf("Error sending mqtt message \r\n");
             else
-                printf("Message published ...\r\n");
+                pc.printf("Message published ...\r\n");
                 
             // Don't forget this!
             pool.free(message);
         }        
         
-        printf("MQTT client yeild ...\r\n");
+        pc.printf("MQTT client yeild ...\r\n");
         if (client.yield(100) != 0)
         {
             client.disconnect();
             // TODO: reconnect TLS session.
             return;
         }
-        printf("MQTT client yeild successful ...\r\n");
+        pc.printf("MQTT client yeild successful ...\r\n");
     }
 }
 
@@ -228,29 +229,29 @@
 
     int i = 0;
 
-    printf("HelloMQTT: version is %.2f\r\n", version);
+    pc.printf("HelloMQTT: version is %.2f\r\n", version);
 
     NetworkInterface* network = easy_connect(true);
     if (!network) {
         return -1;
     }
-
+    
     if ( mqttNetwork.setupTLS(network, SSL_CA_PEM) != 0 )
     {
-        printf("Failed initializing sercure MQTTS...\r\n");
+        pc.printf("Failed initializing sercure MQTTS...\r\n");
         return -1;
     }
     
     if ( mqttsConnect("mqtt.mbedhacks.com", 
                         8883,"mbedtest_01","tinong","tatay") != 0 )
     {
-        printf("Failed connecting to mqtt.mbedhacks.com:8883 \r\n");
+        pc.printf("Failed connecting to mqtt.mbedhacks.com:8883 \r\n");
         return -1;
     }
     
     if ( mqttsSubscribe(topic, MQTT::QOS2, messageArrived) != 0 )
     {
-        printf("Failed to subscribe to a topic!\r\n");
+        pc.printf("Failed to subscribe to a topic!\r\n");
         return -1;
     }
     
@@ -281,5 +282,5 @@
 
     //printf("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
 
-    return 0;
+    //return 0;
 }