this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Revision:
99:05398b3184f8
Parent:
98:65c2333a38b6
Child:
100:80ef4bc31b7a
--- a/source/WiFiManager.cpp	Sun Apr 21 13:06:41 2019 +0000
+++ b/source/WiFiManager.cpp	Mon Apr 22 11:11:41 2019 +0000
@@ -70,7 +70,7 @@
 
 void WiFiManager::runMain(){
     nsapi_error_t error;
-    bool httpReqResult;
+    bool result;
     printf("\r\n [WIFI MAN]  Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
     while(true){
         dequeueWiFiCommands();
@@ -123,8 +123,15 @@
                 queueATresponse(AT_CONFIG_RESP);
                 break;
             case WIFI_CMD_INTERNET_CONFIG:
-                set_internet_config();
-                queueATresponse(AT_INTERNET_CONFIG_RESP);
+                result = set_internet_config();
+                if(result == true)
+                {
+                    queueATresponse(AT_INTERNET_CONFIG_RESP);
+                }
+                else
+                {
+                    sendATresponseString(AT_COMMAND_FAILED);
+                }
                 wifiCmd = WIFI_CMD_NONE;
                 break;
             case WIFI_CMD_NETWORK_STATUS:
@@ -140,8 +147,8 @@
             case WIFI_CMD_SEND_HTTPS_REQ:
                 printf("before call to send http request \n");
                 print_memory_info();
-                httpReqResult = createHttpsRequest();
-                if(httpReqResult == false)
+                result = createHttpsRequest();
+                if(result == false)
                 {
                     sendATresponseString(AT_COMMAND_FAILED);
                 }
@@ -164,12 +171,11 @@
 {
     int strLen = strlen(responseString) + 1;
     at_data_resp = new at_data_msg_t;
-    // create message pointer for response header generation
-    char * msgPtr = (char *)at_data_resp->buffer;
     // set string length 
     at_data_resp->dataLen = strLen;
     memcpy(at_data_resp->buffer, responseString, strLen);
     free(responseString);
+    responseString = NULL;
     // package and send on wifi data queue    
     at_data_resp->at_resp = at_cmd;
     bool queueResult = true;
@@ -184,8 +190,38 @@
         queueResult = queueWiFiDataResponse(*at_data_resp);
     }while(queueResult == false);
     delete at_data_resp;
+    at_data_resp = NULL;
 }                                            
 
+
+void  WiFiManager::sendATresponseBytes(at_cmd_resp_t at_cmd, int len)
+{
+    at_data_resp = new at_data_msg_t;
+    // set string length 
+    at_data_resp->dataLen = len;
+    memcpy(at_data_resp->buffer, responseBytes, len);
+    free(responseBytes);
+    responseBytes = NULL;
+    // package and send on wifi data queue    
+    at_data_resp->at_resp = at_cmd;
+    bool queueResult = true;
+    int wait_count = 0;
+    do
+    {
+        if(!queueResult){
+            wait_count++;
+            wait_ms(10);
+            printf("ATCMD Queue full waited %d ms so far...\n", wait_count*10);
+        }
+        queueResult = queueWiFiDataResponse(*at_data_resp);
+    }while(queueResult == false);
+    delete at_data_resp;
+    at_data_resp = NULL;
+    printf("[WIFI-MAN] sendATresponseBytes completed successfully\r\n");
+}                                            
+
+
+
 bool  WiFiManager::dequeueWiFiCommands(){
     if(wifiCmd != WIFI_CMD_NONE) return false; // busy
     osEvent evt = _aT2WiFiCmdQueue->get(0);
@@ -329,7 +365,7 @@
 
 
 
-void WiFiManager::set_internet_config()
+bool WiFiManager::set_internet_config()
 {
     internet_config_t *internet_cfg  = (internet_config_t *) data_msg->buffer;
     internet_config.peer_id          = internet_cfg->peer_id;
@@ -340,8 +376,30 @@
     printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, 
                                                       internet_config.url, 
                                                       internet_config.connectionScheme);
-    https_connection_active = false; // reset whenever any of the security credentials change
-    delete socket;
+    if(https_connection_active)
+    {
+        https_connection_active = false; // reset whenever any of the security credentials change
+        socket->close();    // close socket before deleting memory    
+        delete socket;
+        socket = NULL;
+    }
+    nsapi_value_or_error_t value_or_error;
+    value_or_error = network->gethostbyname_async(internet_config.url, 
+                                         callback(this, &WiFiManager::gethostbyname_callback), 
+                                         NSAPI_UNSPEC);
+    if(value_or_error >= NSAPI_ERROR_OK) // success
+    {
+        printf("[WIFI-MAN] hostname translation successful value_or_error = %d\r\n", value_or_error);
+        //strcpy(responseString, UDDRP_WRITE_OK);
+        return true;
+    }
+    else // -ve number means error
+    {
+        responseString = (char *) malloc(20);
+        printf("[WIFI-MAN] hostname translation failed\r\n");
+        strcpy(responseString, UDDRP_ERROR);
+        return false;
+    }
 }
 
 
@@ -548,6 +606,42 @@
 }
 
 
+void WiFiManager::gethostbyname_callback(nsapi_error_t result, SocketAddress *address)
+{
+    printf("gethostbyname_callback called... result = %d \r\n", result);
+    responseBytes = (uint8_t *) malloc(HOSTNAME_RESPONSE_LEN);
+    int i = 0;
+    responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
+    responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
+    memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
+    i +=4;
+    int port = address->get_port();
+    memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
+    i +=2;
+    if(is_connected)
+    {
+        // local IPv4 address
+        int ipAddr[4];
+        sscanf(network->get_ip_address(), "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1], 
+                                                         &ipAddr[2], &ipAddr[3]);
+        responseBytes[i++] = (uint8_t) ipAddr[0];
+        responseBytes[i++] = (uint8_t) ipAddr[1];
+        responseBytes[i++] = (uint8_t) ipAddr[2];
+        responseBytes[i++] = (uint8_t) ipAddr[3];
+        // local port number
+        responseBytes[i++] = 0;
+        responseBytes[i]   = 0;
+        printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
+    }
+    else
+    {
+        // if unconnected set ip and port to zeroes
+        memset(&responseBytes[i], 0x00, 6);
+    }
+    sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+}   
+
+
 nsapi_error_t WiFiManager::disconnect()
 {
     nsapi_error_t error;
@@ -726,8 +820,9 @@
     r = socket->set_root_ca_cert(SSL_CA_PEM);
     if(r != NSAPI_ERROR_OK)
     { 
+        printf("TLS set_root_ca_cert failed!!\n");
         socket->close();
-        printf("TLS set_root_ca_cert failed!!\n");
+        printf("closing TLS socket!!\n");
         return false;
     }
     printf("TLS set_root_ca_cert passed!!\n");