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

Dependencies:   mbed-http

Branch:
PassingRegression
Revision:
107:f1a83fd41b17
Parent:
106:e1f04c3d0647
Child:
108:3c8fb2c6e7bf
--- a/source/WiFiManager.cpp	Mon May 06 10:24:53 2019 +0000
+++ b/source/WiFiManager.cpp	Mon May 06 16:44:40 2019 +0000
@@ -58,8 +58,9 @@
     watchdogCnt++;
     if(watchdogCnt >= 6 && responseString==NULL) // every minute
     {
-        responseString = (char *) malloc(100);
-        sprintf(responseString, "\r\n[WiFi-MAN] WiFi Manager Alive\r\n");
+        responseString = (char *) malloc(120);
+        sprintf(responseString, "\r\n[WiFi-MAN] WiFi Manager Alive : state = %d busy = %d httpsConnActive = %d\r\n",
+                                wifiCmd, wifiBusy, https_connection_active);
         sendATresponseString(WIFI_WATCH_DOG);
         watchdogCnt = 0;
     }
@@ -67,7 +68,7 @@
     {
         if(wifiCmd == WIFI_CMD_NONE)
             inactivity_monitor++;
-        responseString = (char *) malloc(100);
+        responseString = (char *) malloc(120);
         sprintf(responseString, "\r\n[WiFi-MAN] Main Loop InActive : state = %d busy = %d httpsConnActive = %d\r\n", 
                                  wifiCmd, wifiBusy, https_connection_active);
         sendATresponseString(WIFI_WATCH_DOG);
@@ -172,11 +173,24 @@
                 wifiBusy = 0;
                 break;
             case WIFI_CMD_INTERNET_CONFIG:
+            {
                 wifiBusy = 1;
+                backgroundTaskCompleted = false;
                 set_internet_config();
-                queueATresponse(AT_INTERNET_CONFIG_RESP);
+                int msecCount = 0;
+                while(!backgroundTaskCompleted && msecCount < 1000)
+                {
+                    msecCount++;
+                    wait_ms(10);
+                }
+                if(backgroundTaskCompleted)
+                {
+                    queueATresponse(AT_INTERNET_CONFIG_RESP);
+                }
+                backgroundTaskCompleted = false;
                 wifiCmd = WIFI_CMD_NONE;
                 break;
+            }
             case WIFI_CMD_NETWORK_STATUS:
                 wifiBusy = 1;
                 getNetworkStatus();
@@ -192,6 +206,7 @@
                 wifiBusy = 0;
                 break;
             case WIFI_CMD_SEND_HTTPS_REQ:
+            {
                 wifiBusy = 1;
                 if(responseString == NULL)
                 {
@@ -203,7 +218,32 @@
                 dbg_printf(LOG, "\r\n[WIFI-MAN] Received HTTPS request...\r\n");
                 print_memory_info();
                 //network->attach(NULL);
+#ifdef USE_EVENTS_FOR_HTTPS_REQUESTS
+                
+                // Events can be cancelled as long as they have not been dispatched. If the
+                // event has already expired, cancel has no side-effects.
+                int event_id = _event_queue.call(this, &WiFiManager::createSendHttpsRequest);
+                backgroundTaskCompleted = false;               
+                int msecCount = 0;
+                while(!backgroundTaskCompleted && msecCount < 1000)
+                {
+                    msecCount++;
+                    wait_ms(10);
+                }
+                if(backgroundTaskCompleted)
+                {
+                    queueATresponse(AT_INTERNET_CONFIG_RESP);
+                    result = true;
+                }
+                else
+                {
+                    _event_queue.cancel(event_id); 
+                    result = false;
+                }
+                backgroundTaskCompleted = false;
+#else
                 result = createHttpsRequest();
+#endif
                 if(result == false)
                 {
                     responseString = (char *) malloc(100);
@@ -223,6 +263,7 @@
                 wifiBusy = 0;
                 //network->attach(callback(this, &WiFiManager::status_callback));
                 break;
+            }
             case WIFI_CMD_SEND_HTTP_REQ:
                 break;
             default:
@@ -233,6 +274,10 @@
     
 }
 
+void  WiFiManager::createSendHttpsRequest()
+{
+    backgroundTaskCompleted = createHttpsRequest();
+}
 
 void  WiFiManager::sendATresponseString(at_cmd_resp_t at_cmd)
 {
@@ -267,7 +312,7 @@
     // set string length 
     at_data_resp->dataLen = len;
     memcpy(at_data_resp->buffer, responseBytes, len);
-    free(responseBytes);
+    delete responseBytes;
     responseBytes = NULL;
     // package and send on wifi data queue    
     at_data_resp->at_resp = at_cmd;
@@ -685,11 +730,12 @@
 void WiFiManager::gethostbyname_callback(nsapi_error_t result, SocketAddress *address)
 {
     dbg_printf(LOG, "gethostbyname_callback called... result = %d \r\n", result);
-    responseBytes = (uint8_t *) malloc(HOSTNAME_RESPONSE_LEN);
+    print_memory_info();
+    responseBytes = new uint8_t[HOSTNAME_RESPONSE_LEN]; //malloc(HOSTNAME_RESPONSE_LEN);
     int i = 0;
     responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
     responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
-    if(is_connected && result>0)
+    if(is_connected && result>=0)
     {
         memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
         strcpy(internet_config.remote_IPv4Address, address->get_ip_address());
@@ -711,15 +757,24 @@
         responseBytes[i++] = 0;
         responseBytes[i]   = 0;
         printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
+        _event_queue.call_in(10, this, &WiFiManager::sendATresponseBytes, 
+                                    CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
     }
     else
     {
         // if unconnected set ip and port to zeroes
         memset(&responseBytes[i], 0x00, 6);
-    }
-    _event_queue.call_in(2, this, &WiFiManager::sendATresponseBytes, 
-                                    CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
+        delete responseBytes;
+        dbg_printf(LOG, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
+        if(responseString == NULL)
+        {
+            //responseString = (char *) malloc(100);
+            //sprintf(responseString, "\r\nHOSTNAME TRANSLATION FAILURE : error code = %d \r\n", result);
+            //sendATresponseString(AT_EVENT);
+        }
+   }
     wifiBusy = 0;
+    backgroundTaskCompleted = true;
 
 }   
 
@@ -976,8 +1031,12 @@
 
 void WiFiManager::sendDebugMessage()
 {
+#ifdef SEND_HTTPS_DEBUG_MESSAGES
     sendATresponseString(AT_EVENT);
     wait_ms(100);
+#else
+    free(responseString);
+#endif
 }
 #define TESTING_HTTPS