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

Dependencies:   mbed-http

Revision:
87:99b37d26ff2a
Parent:
86:04fc2fcda7ec
Child:
88:7ffa053be662
--- a/source/WiFiManager.cpp	Thu Mar 28 23:41:10 2019 +0000
+++ b/source/WiFiManager.cpp	Fri Mar 29 22:18:33 2019 +0000
@@ -7,10 +7,10 @@
                          Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 
                          MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, 
                          Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, 
-                         MemoryPool<wifi_data_msg_t, 4> *aT2WiFiDatamPool, 
-                         Queue<wifi_data_msg_t, 4> *aT2WiFiDataQueue, 
-                         MemoryPool<at_data_msg_t, 4> *wiFi2ATDatamPool, 
-                         Queue<at_data_msg_t, 4> *wiFi2ATDataQueue) 
+                         MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, 
+                         Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, 
+                         MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, 
+                         Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue) 
 :
      wifi_config(wifi_config),
      network(wifi),
@@ -52,6 +52,7 @@
     atData->dataLen        = at_resp.dataLen;
     memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen);
     _wiFi2ATDataQueue->put(atData);
+    printf("[WIFI MAN] queued data size = %d : at_resp = %d\n", at_resp.dataLen, at_resp.at_resp);
     return true;
 }
 
@@ -59,6 +60,7 @@
 
 void WiFiManager::runMain(){
     nsapi_error_t error;
+    printf("\r\n [WIFI MAN]  Thread Id = %d\r\n", Thread::gettid());
     while(true){
         dequeueWiFiCommands();
         dequeueATdataResponse();
@@ -323,29 +325,6 @@
                      wifi_config.pass,
                      wifi_config.security);
     return error;
-    /*
-    if(error)
-    {
-        printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n");
-        return error;
-    }
-    nsapi_connection_status_t conn_status = NSAPI_STATUS_CONNECTING;
-    int loopCount = 0;
-    while(conn_status == NSAPI_STATUS_CONNECTING){
-        loopCount++;
-        conn_status = network->get_connection_status();
-        printf("\n [WIFI-MAN] Waiting for WiFi network connect to complete asynchronously [status = %d] - %d\n", conn_status, loopCount);
-        wait(0.1);
-    }
-    if(conn_status < 0)
-    {
-        printf("\n [WIFI-MAN] Error connecting to Wifi -- %d!! - \n", conn_status);
-        return conn_status;
-    }
-    printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
-    printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
-    return conn_status;
-    */
 }
 
 
@@ -357,49 +336,46 @@
 }
 
 
-/*
 
-        HttpsRequest(NetworkInterface* network,
-                 const char* ssl_ca_pem,
-                 http_method method,
-                 const char* url,
-                 Callback<void(const char *at, uint32_t length)> body_callback = 0)
-       HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response);
-    HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0)
-        post_req->set_header("Content-Type", "application/json");
-        HttpResponse* get_res = get_req->send();
-
-        const char body[] = "{\"hello\":\"world\"}";
-
-        HttpResponse* post_res = post_req->send(body, strlen(body));
-
-
-*/
 void WiFiManager::return_response(HttpResponse* res) {
     
     //queueWiFiDataResponse(at_data_msg_t at_data_resp);
-    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
-    printf("Headers:\n");
+    
     at_data_msg_t at_data_resp; 
+    int numChars = 0;
+    // create message pointer for response header generation
+    char * msgPtr = (char *)at_data_resp.buffer;
+    // do status line
+    numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str());
+    msgPtr += numChars;
     //at_data_resp = &at_data;
-    at_data_resp.dataLen = sizeof(res); // start with minimum size of response
+    at_data_resp.dataLen = sizeof(*res); // start with minimum size of response
     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
-        printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
-        at_data_resp.dataLen+= res->get_headers_fields()[ix]->size();
-        at_data_resp.dataLen+= res->get_headers_values()[ix]->size();
+        numChars = sprintf(msgPtr, "%s: %s\r\n", 
+                           res->get_headers_fields()[ix]->c_str(), 
+                           res->get_headers_values()[ix]->c_str());
+        msgPtr += numChars;
     }
-    at_data_resp.dataLen+= res->get_body_length();
-    printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+    numChars = sprintf(msgPtr, "\r\n\r\n");
+    msgPtr += numChars;
+    strncpy(msgPtr, res->get_body_as_string().c_str(), res->get_body_length()+1);
+    // print out generated header
+    printf("generated response:\n");
+    printf("%s\r\n", (char *)at_data_resp.buffer);
+    //printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+    at_data_resp.dataLen = (msgPtr - (char *)at_data_resp.buffer) + res->get_body_length();
     //printf("WIFI MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
     // package and send on wifi data queue
     
     at_data_resp.at_resp = AT_HTTPS_RESP;
     //at_data_resp.at_data_resp = sizeof(wifi_config_t);
-    memcpy(at_data_resp.buffer, res, at_data_resp.dataLen);
+    //memcpy(at_data_resp.buffer, res, at_data_resp.dataLen);
     queueWiFiDataResponse(at_data_resp);
+    
+    delete res;
 
 }
-
+#define TRY_PRINTF
 
 void WiFiManager::body_callback(const char *at, uint32_t length) {
     
@@ -420,47 +396,89 @@
         if((ix % 32) == 0 and ix)
         printf("\n");
     }
+#ifdef TRY_PRINTF
+    printf("%s\n", at);
+#endif    
+
     printf("\n\n");
     chunkNum++;
-    //device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
 }
 
 void WiFiManager::createHttpsRequest()
 {
-    
-    //http_method method,
-    //Callback<void(const char *at, uint32_t length)> body_callback = 0
+    // reset chunk #;
+    chunkNum = 0;
     printf("\n[WIFI MAN] Http Request received:");
     http_req_cfg = (http_request_t *) data_msg->buffer;
     printf("\n[WIFI MAN] uri = %s", http_req_cfg->request_URI.c_str());
-    strncat(internet_config.url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size());
-    printf("\n[WIFI MAN] server url = %s\n", internet_config.url);
+    printf("\n[WIFI MAN] internet cfg url = %s", internet_config.url);
+    char full_url[100];
+    strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
+    printf("\n[WIFI MAN] server url = %s\n", full_url);
+    //strncat(internet_config.url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size());
+    strncat(full_url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size());
+    //printf("\n[WIFI MAN] server url = %s\n", internet_config.url);
+    printf("\n[WIFI MAN] server url+uri = %s\n", full_url);
+    printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName.c_str());
+    printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal.c_str());
+    printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType.c_str());
     printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen.c_str());
     //printf("\n[WIFI MAN] server url = %s\n", internet_config.url.c_str());
     //const char* url = internet_config.url;
                                            
+    print_memory_info();
     //    http_request = new HttpRequest(network, 
-    //https_request = new HttpsRequest(network, 
-    http_request = new HttpRequest(network, 
-                                     //SSL_CA_PEM, 
-                                     http_req_cfg->method, 
-                                     internet_config.url,
-                                     callback(this, &WiFiManager::body_callback));
-    setHttpHeader("Host", http_req_cfg->hostName);
-    setHttpHeader("Accept", http_req_cfg->AcceptVal);
-    setHttpHeader("Content-Type", http_req_cfg->contentType);
-    setHttpHeader("Content-Length", http_req_cfg->contentLen);
+    //   http_request = new HttpRequest(network, 
     int bodyLen;
     sscanf(http_req_cfg->contentLen.c_str(), "%d", &bodyLen);
     printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen.c_str(), bodyLen);
-    http_response = https_request->send(http_req_cfg->body, bodyLen);
-    free_DataMsg();
-    if (!http_response) {
-        printf("HttpRequest failed (error code %d)\n", https_request->get_error());
-        return;
+    if(strstr(internet_config.url, "http:")!=NULL) // http request
+    {
+        http_request = new HttpRequest(network,  
+                                         //SSL_CA_PEM, 
+                                         http_req_cfg->method, 
+                                         internet_config.url,
+                                         callback(this, &WiFiManager::body_callback));
+        setHttpHeader("Host", http_req_cfg->hostName);
+        setHttpHeader("Accept", http_req_cfg->AcceptVal);
+        setHttpHeader("Content-Type", http_req_cfg->contentType);
+        setHttpHeader("Content-Length", http_req_cfg->contentLen);
+        http_response = http_request->send(http_req_cfg->body, bodyLen);
+        free_DataMsg();
+        if (!http_response) {
+            char buf[100];
+            mbedtls_strerror(http_request->get_error(), buf, 100);
+            printf("HttpRequest failed (error code %s)\n", buf);
+            //printf("HttpsRequest failed (error code %d)\n", https_request->get_error());
+            delete http_request; // free the memory
+            return;
+        }
+        printf("\n----- HTTP POST response -----\n");
     }
-
-    printf("\n----- HTTPS POST response -----\n");
+    else
+    {
+        https_request = new HttpsRequest(network, 
+                                         SSL_CA_PEM, 
+                                         http_req_cfg->method, 
+                                         internet_config.url,
+                                         callback(this, &WiFiManager::body_callback));
+        
+        setHttpsHeader("Host", http_req_cfg->hostName);
+        setHttpsHeader("Accept", http_req_cfg->AcceptVal);
+        setHttpsHeader("Content-Type", http_req_cfg->contentType);
+        setHttpsHeader("Content-Length", http_req_cfg->contentLen);
+        http_response = https_request->send(http_req_cfg->body, bodyLen);
+        free_DataMsg();
+        if (!http_response) {
+            char buf[100];
+            mbedtls_strerror(https_request->get_error(), buf, 100);
+            printf("HttpRequest failed (error code %s)\n", buf);
+            //printf("HttpsRequest failed (error code %d)\n", https_request->get_error());
+            delete https_request; // free the memory
+            return;
+        }
+        printf("\n----- HTTPS POST response -----\r\n");
+   }
     return_response(http_response);
 }