Had to fork with a different name, because of some incompatibility issues.

Dependencies:   MQTT

Revision:
29:40cc05c6c14b
Parent:
27:3806829a0247
--- a/DeviceClient.cpp	Wed Jun 24 23:12:19 2020 +0000
+++ b/DeviceClient.cpp	Mon Sep 14 18:32:15 2020 -0400
@@ -19,6 +19,8 @@
 #include "MQTTClient.h"
 #include "DeviceClient.h"
 
+Semaphore   semaIoT(3);
+
 // need a wrapper since K64F and LPC1768 wont have the same name for mii read methods
 #if defined(TARGET_UBLOX_C027) || defined(TARGET_K64F) || defined(TARGET_DISCO_F746NG)
 
@@ -47,17 +49,23 @@
 DeviceClient::DeviceClient():org(NULL),deviceType(NULL),deviceId(NULL),
         authMethod(NULL),authToken(NULL),mqttNetwork(NULL),mqttClient(NULL),connected(false),port(0)
 {
-    LOG("Constructor#1 called::\r\n");
+    semaIoT.try_acquire_for(500);
+    debug("Constructor#1 called::\r\n");
+    semaIoT.release();
 }
 
 DeviceClient::DeviceClient(char *orgId, char *typeId, char *id, int port):org(orgId),deviceType(typeId),
         deviceId(id),authMethod(NULL),authToken(NULL),connected(false), port(port)
 {
-    LOG("Constructor#2 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org,
+    semaIoT.try_acquire_for(500);
+    debug("Constructor#2 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org,
                     (deviceType==NULL)?"NULL":deviceType, (deviceId==NULL)?"NULL":deviceId);
+    semaIoT.release();
 
     if(strcmp(this->org, QUICKSTART) != 0) {
-        WARN("Registered flow must provide valid token\r\n");
+        semaIoT.try_acquire_for(500);
+        debug("Registered flow must provide valid token\r\n");
+        semaIoT.release();
     }
 
     mqttNetwork = new MQTTNetwork();
@@ -69,8 +77,10 @@
         deviceType(typeId),deviceId(id),authMethod(method),authToken(token),connected(false), port(port)
 {
     // Don't print token for security reasons
-    LOG("Constructor#3 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org,
+    semaIoT.try_acquire_for(500);
+    debug("Constructor#3 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org,
                     (deviceType==NULL)?"NULL":deviceType, (deviceId==NULL)?"NULL":deviceId);
+    semaIoT.release();
 
     mqttNetwork = new MQTTNetwork();
     mqttClient = new MQTT::Client<MQTTNetwork, Countdown>(*mqttNetwork);
@@ -150,11 +160,15 @@
        if(rc == true)
        {
           connected = true;
-          LOG("Device Client Connected to %s:%d\r\n",hostname,port);
+          semaIoT.try_acquire_for(500);
+          debug("Device Client Connected to %s:%d\r\n",hostname,port);
+          semaIoT.release();
         }
     }
     else
-       LOG("No IP Assigned to Network Interface...\r\n");
+       semaIoT.try_acquire_for(500);
+       debug("No IP Assigned to Network Interface...\r\n");
+       semaIoT.release();
 
     return rc;
 }
@@ -167,7 +181,10 @@
  */
 bool DeviceClient::reConnect()
 {
-    LOG("DeviceClient::reConnect() entry and connected = %s\r\n",(connected == true)?"true":"false");
+    semaIoT.try_acquire_for(500);
+    debug("DeviceClient::reConnect() entry and connected = %s\r\n",(connected == true)?"true":"false");
+    semaIoT.release();
+
     if(connected == true)
     {
         disconnect();
@@ -197,17 +214,28 @@
     int rc = -1;
     int retryAttempt = 0;
     do {
-        LOG("%d\r\n",rc = mqttNetwork->connect(hostname, port));
+        semaIoT.try_acquire_for(500);
+        debug("%d\r\n",rc = mqttNetwork->connect(hostname, port));
+        semaIoT.release();
+
         if (rc != 0)
         {
-            WARN("mqttNetwork connect returned: %d\r\n", rc);
+            semaIoT.try_acquire_for(500);
+            debug("mqttNetwork connect returned: %d\r\n", rc);
+            semaIoT.release();
         }
 
         // MQTT connect
-        LOG("%d\r\n",rc = mqttClient->connect(data));
+        semaIoT.try_acquire_for(500);
+        debug("%d\r\n",rc = mqttClient->connect(data));
+        semaIoT.release();
+
         if (rc == 0 && (rc) != 0)
         {
-            WARN("MQTT connect returned %d\r\n", rc);
+            semaIoT.try_acquire_for(500);
+            debug("MQTT connect returned %d\r\n", rc);
+            semaIoT.release();
+
             if (rc == MQTT_NOT_AUTHORIZED || rc == MQTT_BAD_USERNAME_OR_PASSWORD)
                 return false; // don't reattempt to connect if credentials are wrong
         } else if (rc == MQTT_CONNECTION_ACCEPTED) {
@@ -216,7 +244,9 @@
 
         int timeout = getConnTimeout(++retryAttempt);
 
-        WARN("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout);
+        semaIoT.try_acquire_for(500);
+        debug("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout);
+        semaIoT.release();
 
         // enough retry is done - return to application
         if (retryAttempt == 5){
@@ -234,16 +264,18 @@
 //void DeviceClient::logData(EthernetInterface net, char *hostname, char *clientId)
 {
     // Network debug statements
-    LOG("=====================================\r\n");
-    LOG("Connection Config Details:\r\n");
-    LOG("IP ADDRESS: %s\r\n", net->get_ip_address());
-    LOG("MAC ADDRESS: %s\r\n", net->get_mac_address());
-    LOG("Gateway: %s\r\n", net->get_gateway());
-    LOG("Network Mask: %s\r\n", net->get_netmask());
-    LOG("Server Hostname: %s\r\n", hostname);
-    LOG("Server Port: %d\r\n", port);
-    LOG("Client ID: %s\r\n", clientId);
-    LOG("=====================================\r\n");
+    semaIoT.try_acquire_for(500);
+    debug("=====================================\r\n");
+    debug("Connection Config Details:\r\n");
+    debug("IP ADDRESS: %s\r\n", net->get_ip_address());
+    debug("MAC ADDRESS: %s\r\n", net->get_mac_address());
+    debug("Gateway: %s\r\n", net->get_gateway());
+    debug("Network Mask: %s\r\n", net->get_netmask());
+    debug("Server Hostname: %s\r\n", hostname);
+    debug("Server Port: %d\r\n", port);
+    debug("Client ID: %s\r\n", clientId);
+    debug("=====================================\r\n");
+    semaIoT.release();
 }
 
 int DeviceClient::getConnTimeout(int attemptNumber)
@@ -269,7 +301,10 @@
 {
     if(!mqttClient->isConnected())
     {
-        WARN("Client is not connected \r\n");
+        semaIoT.try_acquire_for(500);
+        debug("Client is not connected \r\n");
+        semaIoT.release();
+
         return false;
     }
 
@@ -291,7 +326,10 @@
     message.payload = (void*)data;
     message.payloadlen = strlen(data);
 
-    LOG("Publishing %s\r\n", data);
+    semaIoT.try_acquire_for(500);
+    debug("Publishing %s\r\n", data);
+    semaIoT.release();
+
     int rc = mqttClient->publish(topic, message);
     return rc == 0;
 }
@@ -309,7 +347,9 @@
     int rc = 0;
     // iot-2/cmd/+/fmt/+
     if ((rc = mqttClient->subscribe("iot-2/cmd/+/fmt/+", MQTT::QOS2, msgArrived)) != 0)
-        WARN("rc from MQTT subscribe is %d\r\n", rc);
+        semaIoT.try_acquire_for(500);
+        debug("rc from MQTT subscribe is %d\r\n", rc);
+        semaIoT.release();
     return rc;
 }
 
@@ -330,7 +370,9 @@
 
     sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
 
-    LOG("Message arrived on topic %s: Length: %ul. Payload: %s\r\n",  topic, message.payloadlen, message.payload);
+    semaIoT.try_acquire_for(500);
+    debug("Message arrived on topic %s: Length: %ul. Payload: %s\r\n",  topic, message.payloadlen, message.payload);
+    semaIoT.release();
 
     // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
     char* start = strstr(topic, "/cmd/") + 5;