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

Dependencies:   mbed-http

Files at this revision

API Documentation at this revision

Comitter:
ocomeni
Date:
Mon Apr 01 08:00:41 2019 +0000
Parent:
88:7ffa053be662
Child:
90:ed0267eca7b5
Commit message:
download working ok. still need to fix memory leak.

Changed in this revision

source/ATCmdManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/ATCmdManager.h Show annotated file Show diff for this revision Revisions of this file
source/WiFiManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/common_types.h Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Sun Mar 31 22:09:45 2019 +0000
+++ b/source/ATCmdManager.cpp	Mon Apr 01 08:00:41 2019 +0000
@@ -68,7 +68,7 @@
     printf("\n --- ATCmdManager constructor completed ---\n");
     at_resp = AT_RESP_NONE;
     dataMode = AT_CMD_DATA_MODE;
-    event_queue.call_every(120000,&print_memory_info);
+    event_queue.call_every(3600000,&print_memory_info);
     //AT+UMRS=230400,2,8,1,1,1
     // AT+UBTLE
 }
@@ -148,10 +148,29 @@
                 //////_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
+            case AT_HTTPS_RESP_DOWNLOAD:  
+                // AT_HTTPS_RESP_DOWNLOAD response state 
+                //////_smutex.lock();
+                printf("\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\n");
+                return_response(true); // set download paramter to true
+                _parser.send("OK\n");
+                //////_smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
             case AT_HTTP_RESP:  
                 // AT_HTTP_RESP response state 
                 //////_smutex.lock();
                 printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
+                return_response(); 
+                _parser.send("OK\n");
+                //////_smutex.unlock();
+                at_resp = AT_RESP_NONE;
+                break;
+            case AT_HTTP_RESP_DOWNLOAD:  
+                // AT_HTTP_RESP response state 
+                //////_smutex.lock();
+                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
+                return_response(true); // set download paramter to true
                 _parser.send("OK\n");
                 //////_smutex.unlock();
                 at_resp = AT_RESP_NONE;
@@ -167,6 +186,14 @@
         }
 }
 
+void ATCmdManager::printBufferInHex(uint8_t *buf, int pLen)
+{
+    for(int i =0;i<pLen;i++){
+        if(i%8==0) printf("\n[%3d]",i);
+        printf("%02x ", buf[i]);
+    }
+    printf("\n");
+}
 
 bool ATCmdManager::validate(edm_header_t edm_header)
 {
@@ -326,11 +353,7 @@
                     n = _parser.read((char *)rx_buf_ptr, pLen);
                     if(n == -1) break; // timeout!
                     printf("%d bytes read - expected %d!\n", n, pLen);
-                    for(int i =0;i<pLen;i++){
-                        if(i%8==0) printf("\n[%3d]",i/8);
-                        printf("%02x ", rx_buf_ptr[i]);
-                    }
-                    printf("\n");
+                    printBufferInHex(rx_buf_ptr, pLen);
                     printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
                     if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
                         break; // exit if stop byte now found - possible data corruption!
@@ -814,10 +837,17 @@
 }
 
 
-void ATCmdManager::return_response() {
+void ATCmdManager::return_response(bool download) {
     char * resp = (char *) resp_data->buffer;
     printf("\n[ATCMD MAN] received response:\n");
-    printf("%s\r\n", (char *)resp);
+    if(download == false) // not download must be ascii header
+    {
+        printf("%.*s\r\n", resp_data->dataLen, resp);
+    }
+    else // dump payload as hex
+    {
+        printBufferInHex((uint8_t *)resp, resp_data->dataLen);
+    }
     _parser.write(resp, resp_data->dataLen);
     _wiFi2ATDatamPool->free(resp_data);
     
--- a/source/ATCmdManager.h	Sun Mar 31 22:09:45 2019 +0000
+++ b/source/ATCmdManager.h	Mon Apr 01 08:00:41 2019 +0000
@@ -106,8 +106,8 @@
     bool        validate(edm_header_t edm_header);
     bool        createHttpRequest();
     http_method str2HttpMethod(const char * methodStr);
-    void        return_response(); 
-
+    void        return_response(bool download=false); 
+    void        printBufferInHex(uint8_t *buf, int pLen);
     
     /**
     * Allows timeout to be changed between commands
--- a/source/WiFiManager.cpp	Sun Mar 31 22:09:45 2019 +0000
+++ b/source/WiFiManager.cpp	Mon Apr 01 08:00:41 2019 +0000
@@ -386,19 +386,29 @@
     }
     numChars = sprintf(msgPtr, "\r\n\r\n");
     msgPtr += numChars;
-    strncpy(msgPtr, res->get_body_as_string().c_str(), res->get_body_length()+1);
+    //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();
+    at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
     //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);
-    queueWiFiDataResponse(*at_data_resp);
+    bool queueResult = true;
+    int wait_count = 0;
+    do
+    {
+        if(!queueResult){
+            wait_count++;
+            printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
+            wait_ms(10);
+        }
+        queueResult = queueWiFiDataResponse(*at_data_resp);
+    }while(queueResult == false);
     delete at_data_resp;
 }
 //#define TRY_PRINTF
@@ -430,14 +440,12 @@
 
     printf("\n\n");
     chunkNum++;
-    sendResponseDownloadData(AT_HTTPS_RESP, (uint8_t *)at, length);
+    sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
 }
 
 
 bool WiFiManager::createTLSconnection(const char * hostName)
 {
-    printf("\n\nbefore TLS socket creation\n");
-    print_memory_info();
     socket = new TLSSocket();
     printf("\n\nafter TLS socket creation\n");
     print_memory_info();
@@ -448,11 +456,9 @@
     r = socket->open(network);
     if(r != NSAPI_ERROR_OK)
     { 
-        //printf("TLS open failed!!");
+        printf("TLS open failed!!\n");
         return false;
     }
-    printf("\n\nafter TLS socket opened\n");
-    print_memory_info();
     printf("TLS open passed!!\n");
     r = socket->set_root_ca_cert(SSL_CA_PEM);
     if(r != NSAPI_ERROR_OK)
@@ -460,8 +466,6 @@
         printf("TLS set_root_ca_cert failed!!\n");
         return false;
     }
-    printf("\n\nbefore set root passed\n");
-    print_memory_info();
     printf("TLS set_root_ca_cert passed!!\n");
     r = socket->connect(hostName, 443);
     if(r != NSAPI_ERROR_OK)
@@ -469,23 +473,18 @@
         printf("TLS connect failed for hostname %s!!\n", hostName);
         return false;
     }
-
-    printf("\n\nafter connection passed\n");
-    print_memory_info();
     printf("TLS connection successful for https site :  %s\n", hostName);
     return true;
 }
 #define TESTING_HTTPS
 void WiFiManager::createHttpsRequest()
 {
-    //printf("start of https request \n");
-    //print_memory_info();
     // reset chunk #;
     chunkNum = 0;
-    printf("\n[WIFI MAN] Http Request received:");
+    printf("\n[WIFI MAN] Http Request received:\n");
     http_req_cfg = (http_request_t *) data_msg->buffer;
-    printf("\n[WIFI MAN] uri = %s", http_req_cfg->request_URI.c_str());
-    printf("\n[WIFI MAN] internet cfg url = %s", internet_config.url);
+    printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI.c_str());
+    printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url);
     char full_url[100];
     char host[60] ;
     strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
@@ -502,10 +501,6 @@
     //printf("\n[WIFI MAN] server url = %s\n", internet_config.url.c_str());
     //const char* url = internet_config.url;
                                            
-    //printf("after http config processing\n");
-    //print_memory_info();
-    //    http_request = new HttpRequest(network, 
-    //   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);
@@ -518,7 +513,7 @@
                                          callback(this, &WiFiManager::body_callback));
         setHttpHeader("Host", http_req_cfg->hostName);
         setHttpHeader("Accept", http_req_cfg->AcceptVal);
-        printf("http_req_cfg->method = %d\n");
+        printf("http_req_cfg->method = %d\n", http_req_cfg->method);
         if(http_req_cfg->method == HTTP_GET){
             printf("HTTP_GET -- ignoring body\n");
             //setHttpHeader("Content-Type", http_req_cfg->contentType);
@@ -562,47 +557,43 @@
                                          full_url,
                                          callback(this, &WiFiManager::body_callback));
         
-        setHttpsHeader("Host", http_req_cfg->hostName);
-        setHttpsHeader("Accept", http_req_cfg->AcceptVal);
 #ifdef TESTING_HTTPS
         printf("http_req_cfg->method = %d\n");
         if(http_req_cfg->method == HTTP_GET){
             printf("HTTP_GET -- ignoring body\n");
+            setHttpsHeader("Host", http_req_cfg->hostName);
+            setHttpsHeader("Accept", http_req_cfg->AcceptVal);
             //setHttpHeader("Content-Type", http_req_cfg->contentType);
             //setHttpHeader("Content-Length", http_req_cfg->contentLen);
             http_response = https_request->send(NULL, 0);
         }
         else{
+            setHttpsHeader("Host", http_req_cfg->hostName);
+            setHttpsHeader("Accept", http_req_cfg->AcceptVal);
             setHttpHeader("Content-Type", http_req_cfg->contentType);
             setHttpHeader("Content-Length", http_req_cfg->contentLen);
             http_response = https_request->send(http_req_cfg->body, bodyLen);
         }
 #else
+        setHttpsHeader("Host", http_req_cfg->hostName);
+        setHttpsHeader("Accept", http_req_cfg->AcceptVal);
         setHttpHeader("Content-Type", http_req_cfg->contentType);
         setHttpHeader("Content-Length", http_req_cfg->contentLen);
         http_response = https_request->send(http_req_cfg->body, bodyLen);
 #endif        
         
         //_wmutex.unlock();
-        printf("after https request creation\n");
-        print_memory_info();
         free_DataMsg();
-        //printf("after https response and freeData alloc\n");
-        //print_memory_info();
         if (!http_response) {
             char buf[100];
             mbedtls_strerror(https_request->get_error(), buf, 100);
             printf("HttpsRequest failed (error code %s)\n", buf);
-            //printf("HttpsRequest failed (error code %d)\n", https_request->get_error());
             delete https_request; // free the memory
             https_connection_active = false; // reset true whenever connection fails
             delete socket;
             return;
         }
         https_connection_active = true; // set true whenever connection succeeds
-        //delete https_request; // free the memory
-        //printf("after https request deletion\n");
-        //print_memory_info();
         printf("\n----- HTTPS POST response -----\r\n");
    }
     return_response(http_response);
--- a/source/common_types.h	Sun Mar 31 22:09:45 2019 +0000
+++ b/source/common_types.h	Mon Apr 01 08:00:41 2019 +0000
@@ -68,7 +68,9 @@
   AT_CONFIG_RESP,
   AT_INTERNET_CONFIG_RESP,
   AT_HTTPS_RESP,
-  AT_HTTP_RESP
+  AT_HTTPS_RESP_DOWNLOAD,
+  AT_HTTP_RESP,
+  AT_HTTP_RESP_DOWNLOAD
 }at_cmd_resp_t;