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

Dependencies:   mbed-http

Revision:
100:80ef4bc31b7a
Parent:
99:05398b3184f8
Child:
101:1cfd468e5009
--- a/source/WiFiManager.cpp	Mon Apr 22 11:11:41 2019 +0000
+++ b/source/WiFiManager.cpp	Mon Apr 22 18:34:31 2019 +0000
@@ -33,6 +33,7 @@
  wifiCmd = WIFI_CMD_NONE;
  internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
  is_connected = false;
+ http_response = NULL;
  chunkNum = 0;
  socket = NULL;
  https_connection_active = false;
@@ -123,15 +124,8 @@
                 queueATresponse(AT_CONFIG_RESP);
                 break;
             case WIFI_CMD_INTERNET_CONFIG:
-                result = set_internet_config();
-                if(result == true)
-                {
-                    queueATresponse(AT_INTERNET_CONFIG_RESP);
-                }
-                else
-                {
-                    sendATresponseString(AT_COMMAND_FAILED);
-                }
+                set_internet_config();
+                queueATresponse(AT_INTERNET_CONFIG_RESP);
                 wifiCmd = WIFI_CMD_NONE;
                 break;
             case WIFI_CMD_NETWORK_STATUS:
@@ -363,9 +357,30 @@
     delete socket;
 }
 
-
+void WiFiManager::gethostbyname()
+{
+    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);
+        //printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
+        //sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+    }
+    else // -ve number means error
+    {
+        responseString = (char *) malloc(20);
+        printf("[WIFI-MAN] hostname translation failed\r\n");
+        strcpy(responseString, UDDRP_ERROR);
+        sendATresponseString(AT_COMMAND_FAILED);
+    }
 
-bool WiFiManager::set_internet_config()
+}
+
+void WiFiManager::set_internet_config()
 {
     internet_config_t *internet_cfg  = (internet_config_t *) data_msg->buffer;
     internet_config.peer_id          = internet_cfg->peer_id;
@@ -383,23 +398,7 @@
         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;
-    }
+    _event_queue.call_in(10, this, &WiFiManager::gethostbyname);
 }
 
 
@@ -614,16 +613,19 @@
     responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
     responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
     memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
+    strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
     i +=4;
-    int port = address->get_port();
+    uint16_t port = address->get_port();
+    internet_config.remote_port = 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]);
+        strcpy(internet_config.local_IPv4Address, network->get_ip_address());
+        sscanf(internet_config.local_IPv4Address, "%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];
@@ -638,9 +640,25 @@
         // if unconnected set ip and port to zeroes
         memset(&responseBytes[i], 0x00, 6);
     }
-    sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+    _event_queue.call_in(2, this, &WiFiManager::sendATresponseBytes, 
+                                    CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
 }   
 
+void WiFiManager::sendSocketConnectionEvent()
+{
+    // 
+    responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); 
+    sprintf(responseString, "\r\n%s%d,%d,%d,%s,%d,%s,%d\r\n", PEER_CONNECTED_URC, 
+                                                              IP_PEER_HANDLE,
+                                                              IPv4_CONNECTION,
+                                                              TCP_PROTOCOL,
+                                                              internet_config.local_IPv4Address,
+                                                              DEFAULT_LOCAL_PORT,
+                                                              internet_config.remote_IPv4Address,
+                                                              internet_config.remote_port);
+    sendATresponseString(AT_EVENT);
+}
+
 
 nsapi_error_t WiFiManager::disconnect()
 {
@@ -698,6 +716,7 @@
     }while(queueResult ==  false || pos < bufLen);
     printf("[WIFI-MAN] response data queued - deleting data memory\r\n");
     delete at_data_resp;
+    at_data_resp = NULL;
 }
 
 bool WiFiManager::copyResponseHdr2Queue()
@@ -706,7 +725,12 @@
     // create message pointer for response header generation
     char * msgPtr = (char *)at_data_resp->buffer;
     // do status line
-    printf("before getting HTTP status line\n");
+    if(http_response == NULL)
+    {
+        printf("[WIFI-MAN] copy failed: response pointer NULL!!\r\n");
+        return false;
+    }
+    printf("before getting HTTP status line http_response = 0x%08X\n", http_response);
     numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(), 
                                                      http_response->get_status_message().c_str());
     msgPtr += numChars;
@@ -780,6 +804,7 @@
         queueResult = queueWiFiDataResponse(*at_data_resp);
     }while(queueResult == false);
     delete at_data_resp;
+    at_data_resp = NULL;
 }
 
 
@@ -838,6 +863,20 @@
     printf("TLS connection successful for https site :  %s\n", hostName);
     return true;
 }
+
+
+void WiFiManager::updateRemotePeerDetails()
+{
+    printf("Updating internet_config... \r\n");
+    nsapi_error_t error;
+    SocketAddress * address = new SocketAddress;
+    error = socket->getpeername(address);   
+    strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
+    uint16_t port = address->get_port();
+    internet_config.remote_port = port;
+    delete address;
+}
+
 #define TESTING_HTTPS
 
 //#define DONT_USE_TLS_SOCKET
@@ -921,6 +960,11 @@
                  printf("data msg deleted \r\n");
                  return false;
             }
+            // update remote peer details after socket connection
+            updateRemotePeerDetails();
+            // send socket connection event before proceeding to send https request
+            // give about 2 ms
+            _event_queue.call_in(2, this, &WiFiManager::sendSocketConnectionEvent);
             //printf("[create https] TLS connection successful for https site :  %s\n", host);
             //printf("after call to createTLSconnection \n");
             //print_memory_info();
@@ -1015,6 +1059,7 @@
     delete https_request; // free the request & response memory
     printf("deleted https_request\r\n");
     https_request = NULL;
+    http_response = NULL;
     return true;
 }