Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 29:40cc05c6c14b, committed 2020-09-14
- Comitter:
- lamell
- Date:
- Mon Sep 14 18:32:15 2020 -0400
- Parent:
- 28:d146fa8001b7
- Commit message:
- No changes
Changed in this revision
DeviceClient.cpp | Show annotated file Show diff for this revision Revisions of this file |
MQTTNetwork.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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;
--- a/MQTTNetwork.h Wed Jun 24 23:12:19 2020 +0000 +++ b/MQTTNetwork.h Mon Sep 14 18:32:15 2020 -0400 @@ -67,10 +67,15 @@ //Instantiate new ethernet interface //network = new EthernetInterface(); //Connect to ethernet interface - network = NetworkInterface::get_default_instance(); + network = NetworkInterface::get_default_instance(); + nsapi_error_t neterr = network->connect(); - LOG("%d\r\n",network->connect()); - LOG("%d\r\n",socket.open(network)); + if (neterr != NSAPI_ERROR_OK) { + debug("Net disconn %d\r\n",network->disconnect()); + debug("%d\r\n",network->connect()); + } + + debug("%d\r\n",socket.open(network)); //Instantiate mbedTLS structures _entropy = new mbedtls_entropy_context();