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 May 06 09:06:36 2019 +0000
Branch:
PassingRegression
Parent:
104:11e9605093c9
Child:
106:e1f04c3d0647
Commit message:
still having issues - put defines around much of the verbose comments

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/WiFiManager.h Show annotated file Show diff for this revision Revisions of this file
source/common_config.h Show annotated file Show diff for this revision Revisions of this file
source/debug.cpp Show annotated file Show diff for this revision Revisions of this file
source/main-https.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Sun May 05 08:24:46 2019 +0000
+++ b/source/ATCmdManager.cpp	Mon May 06 09:06:36 2019 +0000
@@ -219,7 +219,7 @@
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\r\n");
                 return_response(true); // set download paramter to true
                 //_smutex.lock();
-                sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
+                //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
@@ -317,7 +317,7 @@
 void ATCmdManager::sendConnectIndicationFreeMpool(const char *buf)
 {
     int len =  strlen(buf);
-    outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, INDICATION_MSG_TYPE, NO_CHANNEL);
+    outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL);
     _wiFi2ATDatamPool->free(resp_data);
     resp_data = NULL;
 }
@@ -406,7 +406,9 @@
         {
             n = sscanf(nxtPtr,"%s %s %s", s1, s2, s3);
             if(n!=3) return false; // error in input abort
+#ifdef FULL_DEBUG_ENABLED
             dbg_printf(LOG, "\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 );
+#endif    
             //http_req.method = str2HttpMethod(s1.c_str());
             http_req->method = str2HttpMethod(s1);
             http_req->request_URI = (char *) http_req->buffer; // point 1st string to start of buffer
@@ -415,11 +417,13 @@
             http_req->http_version = http_req->request_URI + len; // point 2nd string to after 1st
             len = strlen(s3)+1;
             strncpy(http_req->http_version, s3, len);
+#ifdef FULL_DEBUG_ENABLED
             dbg_printf(LOG, "\nhttp_request 1st line:\n method = %d\nuri = %s\nhttp_ver = %s",http_req->method, 
                                                                       http_req->request_URI, 
                                                                       http_req->http_version );
             dbg_printf(LOG, "\nhttp_request str ptrs\nuri = %X\nhttp_ver = %X",http_req->request_URI, 
                                                                       http_req->http_version );
+#endif
             outPtr = http_req->http_version + len; // point output buffer ptr to after 2nd string
         }
         else{ // scan header pairs
@@ -428,7 +432,9 @@
             if(n!=2) return false; // error in input abort
             p = strstr(s1,":");
             *p = NULL;
+#ifdef FULL_DEBUG_ENABLED
             dbg_printf(LOG, "\nname = %s value = %s",s1, s2);
+#endif
             if(strstr(s1, "Host")!= NULL){
                 http_req->hostName = outPtr;
 #ifdef BOX_UBLOX_DEMO_TESTING
@@ -443,21 +449,27 @@
                 len = strlen(s2)+1;                
                 strncpy(outPtr, s2, len);
 #endif
+#ifdef FULL_DEBUG_ENABLED
                 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+#endif
                 outPtr += len; // point output buffer ptr to after current string
             }
             else if(strstr(s1, "Accept")!= NULL){
                 http_req->AcceptVal = outPtr;
                 len = strlen(s2)+1;                
                 strncpy(outPtr, s2, len);
+#ifdef FULL_DEBUG_ENABLED
                 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+#endif
                 outPtr += len; // point output buffer ptr to after current string
             }
             else if(strstr(s1, "Content-Type")!= NULL){
                 http_req->contentType = outPtr;
                 len = strlen(s2)+1;                
                 strncpy(outPtr, s2, len);
+#ifdef FULL_DEBUG_ENABLED
                 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+#endif
                 outPtr += len; // point output buffer ptr to after current string
             }
             else if(strstr(s1, "Content-Length")!= NULL){
@@ -465,19 +477,27 @@
                 http_req->contentLen = outPtr;
                 len = strlen(s2)+1;                
                 strncpy(outPtr, s2, len);
+#ifdef FULL_DEBUG_ENABLED
                 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
+#endif
                 outPtr += len; // point output buffer ptr to after current string
             }
             
+#ifdef FULL_DEBUG_ENABLED
             dbg_printf(LOG, "\noutPtr = %X len = %d\n", outPtr, len);
+#endif
         }
         nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line
         if(nxtPtr >= p2) break;
     }
     // print header from http_req_struct
+#ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "\nhttp request header: \n %s\n", http_req->buffer);
+#endif
     int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr);
+#ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "\nLen = %d\n", bodyLen);
+#endif
     http_req->body = (uint8_t *) outPtr;
     memcpy(outPtr, bodyPtr, bodyLen);
     if(bodyLen > 10){
@@ -487,9 +507,11 @@
     outPtr += bodyLen; // move output pointer to end output (header + body)
     // package and send on wifi data queue
     data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ;
+    data_msg.dataLen = (uint8_t *)outPtr - http_req->buffer;//sizeof(http_request_t);
+#ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "\nsizeof(http_req) on population = %d\n", sizeof(*http_req));
-    data_msg.dataLen = (uint8_t *)outPtr - http_req->buffer;//sizeof(http_request_t);
     dbg_printf(LOG, "\ndata_msg.dataLen = %d\n", data_msg.dataLen);
+#endif
     // queue next data request
     if(queueWiFiDataRequest(data_msg) == true)
     {
--- a/source/ATCmdManager.h	Sun May 05 08:24:46 2019 +0000
+++ b/source/ATCmdManager.h	Mon May 06 09:06:36 2019 +0000
@@ -7,7 +7,6 @@
 #include "BleManager.h"
 #include "WiFiManager.h"
 
-#define MAIN_LOOP_WAIT_TIME_MS 100 // milliseconds
 #define NUM_UART_OPTIONS       6
 #ifndef UBLOX_ODIN_W2_MISC_TIMEOUT
 #define UBLOX_ODIN_W2_MISC_TIMEOUT    2000
--- a/source/WiFiManager.cpp	Sun May 05 08:24:46 2019 +0000
+++ b/source/WiFiManager.cpp	Mon May 06 09:06:36 2019 +0000
@@ -48,9 +48,12 @@
 WiFiManager::~WiFiManager()
 {
 }
-
+//#define DISABLE_WATCHDOG
 void WiFiManager::callWifiWatchDog()
 {
+#ifdef DISABLE_WATCHDOG
+    return;
+#else
     static int inactivity_monitor = 0;
     watchdogCnt++;
     if(watchdogCnt >= 6 && responseString==NULL) // every minute
@@ -74,7 +77,7 @@
             inactivity_monitor = 0;
         }
     }
-    
+#endif    
 }
   
 bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
@@ -199,6 +202,7 @@
                 dbg_printf(LOG, "before call to send http request \n");
                 dbg_printf(LOG, "\r\n[WIFI-MAN] Received HTTPS request...\r\n");
                 print_memory_info();
+                //network->attach(NULL);
                 result = createHttpsRequest();
                 if(result == false)
                 {
@@ -217,13 +221,14 @@
                 print_memory_info();
                 wifiCmd = WIFI_CMD_NONE;
                 wifiBusy = 0;
+                //network->attach(callback(this, &WiFiManager::status_callback));
                 break;
             case WIFI_CMD_SEND_HTTP_REQ:
                 break;
             default:
                 break;
         }
-        wait_ms(100); // 
+        wait_ms(WIFI_MAIN_LOOP_WAIT_TIME_MS); // 
     }
     
 }
@@ -597,7 +602,7 @@
 
 void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
 {
-    dbg_printf(LOG, "[WIFI-MAN] about to call callback... \r\n");
+    dbg_printf(LOG, "[WIFI-MAN] about to call status_callback_event... \r\n");
     _event_queue.call_in(50, this, &WiFiManager::status_callback_event, status, param);
     //status_callback_event(status, param);
 }
@@ -886,6 +891,7 @@
 void WiFiManager::body_callback(const char *at, uint32_t length) {    
     dbg_printf(LOG, "\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length);
     chunkNum++;
+    dbg_printf(LOG, "This is the start when response is excluded\r\n%s\r\nend of packet \r\n", at);
     if(http_response == NULL)
     {
         dbg_printf(LOG, "[WIFI-MAN] response pointer NULL!!\r\n");
@@ -904,14 +910,16 @@
     sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
 }
 
-
+//#define ENABLE_MBED_TRACE
 bool WiFiManager::createTLSconnection(const char * hostName)
 {
 #ifdef ENABLE_MBED_TRACE
     mbed_trace_init();
 #endif
     socket = new TLSSocket();
-
+    // set a 5 second timeout
+    //socket->set_timeout(5000);
+    //socket->set_blocking(false);
     nsapi_error_t r;
     // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
     r = socket->open(network);
@@ -935,7 +943,8 @@
     { 
         char errstr[100];
         mbedtls_strerror(r, errstr, 100);
-        dbg_printf(LOG, "TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr);
+        dbg_printf(LOG, "TLS connect failed (err = %d) for hostname '%s' -- ERROR = %s !!\n", r, hostName, errstr);
+        //printf("TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr);
         socket->close();
         return false;
     }
@@ -964,6 +973,12 @@
     delete address;
 }
 
+
+void WiFiManager::sendDebugMessage()
+{
+    sendATresponseString(AT_EVENT);
+    wait_ms(100);
+}
 #define TESTING_HTTPS
 
 //#define DONT_USE_TLS_SOCKET
@@ -978,27 +993,32 @@
 #endif    
     dbg_printf(LOG, "\n[WIFI MAN] Http Request received:\n");
     http_req_cfg = (http_request_t *) data_msg->buffer;
+#ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
     dbg_printf(LOG, "\n[WIFI MAN] internet cfg url = %s\n", internet_config.url);
+#endif
     char full_url[100];
     char host[60] ;
     strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
     strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1);
     strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1);
+#ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "\n[WIFI MAN] server url+uri = %s\n", full_url);
     dbg_printf(LOG, "\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName);
     dbg_printf(LOG, "\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal);
     dbg_printf(LOG, "\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType);
     dbg_printf(LOG, "\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen);
+#endif
                                            
     int bodyLen;
     sscanf(http_req_cfg->contentLen, "%d", &bodyLen);
+#ifdef FULL_DEBUG_ENABLED
     dbg_printf(LOG, "contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen);
-    
     if(bodyLen > 10){
         dbg_printf(LOG, "\n [WIFI MAN] Message Body:\n");
         printBufferInHex(http_req_cfg->body, bodyLen);
     }
+#endif    
     if(strstr(internet_config.url, "http:")!=NULL) // http request
     {
         http_request = new HttpRequest(network,  
@@ -1007,7 +1027,7 @@
                                        callback(this, &WiFiManager::body_callback));
         setHttpHeader("Host", http_req_cfg->hostName);
         setHttpHeader("Accept", http_req_cfg->AcceptVal);
-        dbg_printf(LOG, "http_req_cfg->method = %d\n", http_req_cfg->method);
+        //dbg_printf(LOG, "http_req_cfg->method = %d\n", http_req_cfg->method);
         if(http_req_cfg->method == HTTP_GET){
             dbg_printf(LOG, "HTTP_GET -- ignoring body\n");
             //setHttpHeader("Content-Type", http_req_cfg->contentType);
@@ -1033,6 +1053,9 @@
     }
     else
     {
+        mbed_stats_heap_t heap_stats;
+        mbed_stats_heap_get(&heap_stats);
+        dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
 #ifndef DONT_USE_TLS_SOCKET
         if(https_connection_active == false){
             bool tlsResult;
@@ -1067,6 +1090,9 @@
                                          http_req_cfg->method, 
                                          full_url,
                                          callback(this, &WiFiManager::body_callback));
+        responseString = (char *) malloc(100);
+        sprintf(responseString, "\r\nHTTP REQUEST OBJECT CREATED\r\n");
+        sendDebugMessage();
 #else
         https_request = new HttpsRequest(network, 
                                          SSL_CA_PEM,
@@ -1085,13 +1111,26 @@
             http_response = https_request->send(NULL, 0);
         }
         else{
-            dbg_printf(LOG, "about to setup https headers\r\n");
+            //dbg_printf(LOG, "about to setup https headers\r\n");
             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);
             dbg_printf(LOG, "https headers setup - about to send request\r\n");
-            http_response = https_request->send(http_req_cfg->body, bodyLen);
+            // Grab the heap statistics
+            //mbed_stats_heap_t heap_stats;
+            //mbed_stats_heap_get(&heap_stats);
+            dbg_printf(LOG, "Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
+            responseString = (char *) malloc(200);
+            sprintf(responseString, "\r\nABOUT TO SEND HTTP REQUEST\r\n");
+            sendDebugMessage();
+            responseString = (char *) malloc(200);
+            sprintf(responseString, "Heap size= %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
+            sendDebugMessage();
+            https_request->send(http_req_cfg->body, bodyLen);
+            responseString = (char *) malloc(100);
+            sprintf(responseString, "\r\nRETURNED FROM SENDING HTTP REQUEST\r\n");
+            sendDebugMessage();
         }
 #else
         setHttpsHeader("Host", http_req_cfg->hostName);
@@ -1100,14 +1139,12 @@
         setHttpsHeader("Content-Length", http_req_cfg->contentLen);
         http_response = https_request->send(http_req_cfg->body, bodyLen);
 #endif        
-        
-        //_wmutex.unlock();
-        //free_DataMsg();
-        if (!http_response) {
+        nsapi_error_t error = https_request->get_error();
+        if(error < 0)
+        {
             char buf[100];
-            mbedtls_strerror(https_request->get_error(), buf, 100);
-            dbg_printf(LOG, "HttpsRequest failed (error code %s)\n", buf);
-            http_result = HTTP_REQUEST_FAILED;
+            mbedtls_strerror(error, buf, 100);
+            printf("HttpsRequest failed (error code %s)\n", buf);
             delete https_request; // free the memory
             https_request = NULL;
             https_connection_active = false; // reset true whenever connection fails
@@ -1115,29 +1152,16 @@
             delete socket;
             socket = NULL;
             free_DataMsg();
+            http_result = HTTP_REQUEST_FAILED;
             return false;
         }
         https_connection_active = true; // set true whenever connection succeeds
         dbg_printf(LOG, "\n----- HTTPS POST response -----\r\n");
     }
-    if(http_response != NULL){
-
-#ifndef MIX_HDR_AND_BODY
-        return_response(http_response);
-#else
-    if(http_response_hdr_sent == false){ // if it failed to add to first chunk send separately
-        return_response(http_response);
-    }
-#endif    
-        //delete http_response; // free the response memory
-        //http_response = NULL;
-        //dbg_printf(LOG, "deleted http_response\r\n");
-    }
     free_DataMsg();
     delete https_request; // free the request & response memory
     dbg_printf(LOG, "deleted https_request\r\n");
     https_request = NULL;
-    http_response = NULL;
     http_result = RESPONSE_OK;
     return true;
 }
--- a/source/WiFiManager.h	Sun May 05 08:24:46 2019 +0000
+++ b/source/WiFiManager.h	Mon May 06 09:06:36 2019 +0000
@@ -120,6 +120,7 @@
     void                  gethostbyname();
     void                  sendSocketConnectionEvent();
     void                  updateRemotePeerDetails();
+    void                  sendDebugMessage();
     void                  callWifiWatchDog();
 
 
--- a/source/common_config.h	Sun May 05 08:24:46 2019 +0000
+++ b/source/common_config.h	Mon May 06 09:06:36 2019 +0000
@@ -6,13 +6,15 @@
 #define TX_BUFFER_LEN 4*256
 #define RX_BUFFER_LEN 4*256
 #define BTLE_THREAD_PRIORITY osPriorityRealtime
-#define WIFI_THREAD_PRIORITY osPriorityHigh
-#define ATCMD_THREAD_PRIORITY osPriorityRealtime
+#define WIFI_THREAD_PRIORITY osPriorityRealtime
+#define ATCMD_THREAD_PRIORITY osPriorityHigh
 #ifndef UBLOX_ODIN_W2_RECV_TIMEOUT
 #define UBLOX_ODIN_W2_RECV_TIMEOUT    5
 #endif
 #define USE_MALLOC_FOR_COMMAND_MEMORY_POOL
 #define WIFI_CONNECT_TIMEOUT_SECS 10
+#define MAIN_LOOP_WAIT_TIME_MS 100 // milliseconds
+#define WIFI_MAIN_LOOP_WAIT_TIME_MS 101 // milliseconds
 #define MAX_RESPONSE_STRING_LEN   512 // maximum response string length of 512 bytes
 #define DEFAULT_WIFI_CHANNEL      0
 #define DEFAULT_BTLE_CHANNEL      1
--- a/source/debug.cpp	Sun May 05 08:24:46 2019 +0000
+++ b/source/debug.cpp	Mon May 06 09:06:36 2019 +0000
@@ -91,7 +91,7 @@
 * @param        None 
 * @retval       None 
 */
-//#define DEBUG_ENABLED
+#define DEBUG_ENABLED
 void initialise_debug(void)
 {   
   memset(g_dbg_buffer, 0, sizeof(g_dbg_buffer));
--- a/source/main-https.cpp	Sun May 05 08:24:46 2019 +0000
+++ b/source/main-https.cpp	Mon May 06 09:06:36 2019 +0000
@@ -346,7 +346,7 @@
 
 
 
-
+//#define ENABLE_MEMORY_CHECKS
 
 void print_memory_info() {
 #ifdef ENABLE_MEMORY_CHECKS