Had to fork with a different name, because of some incompatibility issues.
Diff: DeviceClient.cpp
- Revision:
- 25:f4727705353b
- Parent:
- 23:1523bdaba8c8
- Child:
- 27:3806829a0247
--- a/DeviceClient.cpp Tue Mar 10 23:22:53 2020 -0400 +++ b/DeviceClient.cpp Sat Mar 21 15:36:30 2020 -0400 @@ -16,13 +16,9 @@ * Lokesh K Haralakatta - Port to mbed OS 5 support * Lokesh K Haralakatta - Added SSL/TLS Support *******************************************************************************/ - -#include "mbed.h" #include "MQTTClient.h" #include "DeviceClient.h" -//Addedd the use of mutexes and semaphores - // 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) @@ -59,7 +55,7 @@ { LOG("Constructor#2 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org, (deviceType==NULL)?"NULL":deviceType, (deviceId==NULL)?"NULL":deviceId); - + if(strcmp(this->org, QUICKSTART) != 0) { WARN("Registered flow must provide valid token\r\n"); } @@ -75,7 +71,7 @@ // Don't print token for security reasons LOG("Constructor#3 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org, (deviceType==NULL)?"NULL":deviceType, (deviceId==NULL)?"NULL":deviceId); - + mqttNetwork = new MQTTNetwork(); mqttClient = new MQTT::Client<MQTTNetwork, Countdown>(*mqttNetwork); } @@ -107,8 +103,8 @@ sprintf(hostname, "%s%s", organizationName, IBM_IOT_MESSAGING); //NetworkInterface* net = mqttNetwork->getEth(); - //NetworkInterface* net = mqttNetwork->network; - EthernetInterface* net=mqttNetwork->net; + NetworkInterface* net = mqttNetwork->network; + //EthernetInterface net = mqttNetwork->network; const char* ip = net->get_ip_address(); @@ -144,6 +140,12 @@ logData(net, hostname, clientId); +SocketAddress addr; +mqttNetwork->network->gethostbyname("www.google.com", &addr); + + LOG("%d: %s\r\n",mqttNetwork->network->set_dhcp(true), + addr.get_ip_address()); + if(ip){ rc = tryConnect(hostname, data); // By default subscribe to commands if we are in registered flow @@ -154,12 +156,12 @@ if(rc == true) { connected = true; - LOG("Device Client Connected to %s:%d\r\n",hostname,port); + LOG("Device Client Connected to %s:%d\r\n",hostname,port); } } else LOG("No IP Assigned to Network Interface...\r\n"); - + return rc; } @@ -172,7 +174,6 @@ bool DeviceClient::reConnect() { LOG("DeviceClient::reConnect() entry and connected = %s\r\n",(connected == true)?"true":"false"); - if(connected == true) { disconnect(); @@ -181,8 +182,9 @@ if(linkStatus()) { //NetworkInterface* net = mqttNetwork->getEth(); - //NetworkInterface* net = mqttNetwork->network; - EthernetInterface* net = mqttNetwork->net; + NetworkInterface* net = mqttNetwork->network; + //EthernetInterface net = mqttNetwork->network; + if(net->connect() == 0) { bool status = connect(); @@ -201,14 +203,15 @@ int rc = -1; int retryAttempt = 0; do { - rc = mqttNetwork->connect(hostname, port); + LOG("%d\r\n",rc = mqttNetwork->connect(hostname, port)); if (rc != 0) { WARN("mqttNetwork connect returned: %d\r\n", rc); } // MQTT connect - if (rc == 0 && (rc = mqttClient->connect(data)) != 0) + LOG("%d\r\n",rc = mqttClient->connect(data)); + if (rc == 0 && (rc) != 0) { WARN("MQTT connect returned %d\r\n", rc); if (rc == MQTT_NOT_AUTHORIZED || rc == MQTT_BAD_USERNAME_OR_PASSWORD) @@ -228,13 +231,13 @@ NVIC_SystemReset(); return false; } else { - thread_sleep_for(timeout); + wait(timeout); } } while(true); } -//void DeviceClient::logData(NetworkInterface* net, char *hostname, char *clientId) -void DeviceClient::logData(EthernetInterface* net, char *hostname, char *clientId) +void DeviceClient::logData(NetworkInterface* net, char *hostname, char *clientId) +//void DeviceClient::logData(EthernetInterface net, char *hostname, char *clientId) { // Network debug statements LOG("=====================================\r\n"); @@ -246,7 +249,7 @@ LOG("Server Hostname: %s\r\n", hostname); LOG("Server Port: %d\r\n", port); LOG("Client ID: %s\r\n", clientId); - LOG("=====================================\r\n"); + LOG("=====================================\r\n"); } int DeviceClient::getConnTimeout(int attemptNumber) @@ -295,7 +298,6 @@ message.payloadlen = strlen(data); LOG("Publishing %s\r\n", data); - int rc = mqttClient->publish(topic, message); return rc == 0; } @@ -335,7 +337,7 @@ 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); - + // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ char* start = strstr(topic, "/cmd/") + 5; int len = strstr(topic, "/fmt/") - start; @@ -369,8 +371,9 @@ } //NetworkInterface* net = mqttNetwork->getEth(); - //NetworkInterface* net = mqttNetwork->network; - EthernetInterface* net = mqttNetwork->net; + NetworkInterface* net = mqttNetwork->network; + //EthernetInterface net = mqttNetwork->network; + mqttNetwork->disconnect(); net->disconnect(); connected = false; @@ -400,8 +403,9 @@ char* DeviceClient::getMac(char* buf, int buflen) { //NetworkInterface* net = mqttNetwork->getEth(); - //NetworkInterface* net = mqttNetwork->network; - EthernetInterface* net = mqttNetwork->net; + NetworkInterface* net = mqttNetwork->network; + //EthernetInterface net = mqttNetwork->network; + strncpy(buf, net->get_mac_address(), buflen); char* pos; // Remove colons from mac address @@ -414,9 +418,10 @@ //char iplocal[25]; //NetworkInterface* net = mqttNetwork->getEth(); - //NetworkInterface* net = mqttNetwork->network; - EthernetInterface* net = mqttNetwork->net; - //const char* ip = net->get_ip_address(); + NetworkInterface* net = mqttNetwork->network; + //EthernetInterface net = mqttNetwork->network; + + const char* ip = net->get_ip_address(); //strcpy(iplocal,ip); //return iplocal;