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:
Sun Jul 07 20:36:41 2019 +0000
Branch:
PassingRegression
Parent:
125:d2830421006c
Child:
127:a21788227ca6
Commit message:
added some initial error handling:; - on an mbed os crash, the system now resets.; currently testing

Changed in this revision

mbed_app.json Show annotated file Show diff for this revision Revisions of this file
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_config.h 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/mbed_app.json	Thu Jun 20 20:46:25 2019 +0000
+++ b/mbed_app.json	Sun Jul 07 20:36:41 2019 +0000
@@ -17,15 +17,7 @@
         },
         "overrides": {
             "lse_available": false 
-        }    ,
-        "uart1_tx": {
-            "help": "UART1 TX pin",
-            "value": "NC"
-        },
-        "uart1_rx": {
-            "help": "UART1 RX pin",
-            "value": "NC"
-        }
+        } 
     },
     "macros": [
         "MBEDTLS_MPI_MAX_SIZE=1024",
@@ -37,25 +29,18 @@
         "MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=0",
         "MBEDTLS_SSL_MAX_CONTENT_LEN=16384",
         "DEBUG_ISR_STACK_USAGE=0",
-        "MBED_CONF_APP_ERROR_FILENAME_CAPTURE_ENABLED=1"
+        "MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED=1",
+        "MBED_CONF_PLATFORM_ERROR_REBOOT_MAX=10",
+        "MBED_CONF_APP_ERROR_FILENAME_CAPTURE_ENABLED=1",
+        "MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED=1"
     ],
     "target_overrides": {
          "*": {
             "platform.stdio-baud-rate": 115200,
             "platform.stdio-convert-newlines": true,
-            "platform.fatal-error-auto-reboot-enabled": 1,
             "platform.thread-stats-enabled":1,
             "mbed-trace.enable": 1,
-            "mbed-http.http-buffer-size": 1024,
-            "nsapi.default-wifi-security": "WPA_WPA2",
-            "nsapi.default-wifi-ssid": "\"VM9109107_5GEXT\"",
-            "nsapi.default-wifi-password": "\"Bonga01shop02\""
-      },
-        "MTB_UBLOX_ODIN_W2": {
-            "target.features_add": ["BLE"],
-            "target.extra_labels_add": ["CORDIO", "CORDIO_BLUENRG"],
-            "uart1_tx": "D1",
-            "uart1_rx": "D0"
-        }
+            "mbed-http.http-buffer-size": 1024
+      }
      }
 }
--- a/source/ATCmdManager.cpp	Thu Jun 20 20:46:25 2019 +0000
+++ b/source/ATCmdManager.cpp	Sun Jul 07 20:36:41 2019 +0000
@@ -131,19 +131,23 @@
                 // IDLE response state
                 break;
             case AT_SCAN_RESP:
+            {
                 // AT_SCAN_RESP response state
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
                 sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
+            }
             case AT_DETAILED_SCAN_RESP:
+            {
                 // AT_DETAILED_SCAN_RESP response state
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
                 sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
+            }
             case AT_CONNECT_RESP:  
             case WIFI_RECONNECT_INFO:  
             {
@@ -155,11 +159,12 @@
                 char * urc = new char[20];
                 sprintf(urc, "\r\n%s:%d\r\n", NETWORK_UP_URC, WIFI_INTERFACE_ID);
                 sendAtEvent(urc);
-                delete urc;
+                delete[] urc;
                 at_resp = AT_RESP_NONE;
                 break;
             }
-            case AT_DISCONNECT_RESP:  
+            case AT_DISCONNECT_RESP: 
+            { 
                 // AT_DISCONNECT_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
@@ -167,29 +172,36 @@
                 char * urc = new char[20];
                 sprintf(urc, "\r\n%s:%d\r\n", NETWORK_DOWN_URC, WIFI_INTERFACE_ID);
                 sendAtEvent(urc);
-                delete urc;
+                delete[] urc;
                 at_resp = AT_RESP_NONE;
                 break;
-            case AT_CONFIG_RESP:  
+            }
+            case AT_CONFIG_RESP: 
+            { 
                 // AT_CONFIG_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\r\n");
                 sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 at_resp = AT_RESP_NONE;
                 break;
-            case AT_NETWORK_STATUS_RESP:  
+            }
+            case AT_NETWORK_STATUS_RESP: 
+            { 
                 // AT_CONFIG_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] NETWORK STATUS RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
                 sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
+            }
             case AT_WIFI_STATUS_RESP:  
+            {
                 // AT_CONFIG_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
                 sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
+            }
             case AT_INTERNET_CONFIG_RESP:  
                 // AT_CONFIG_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\r\n");
@@ -320,6 +332,7 @@
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_BLE_MAC_RESP:  
+            {
                 // AT_BLE_MAC_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_MAC_RESP RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) ble_resp_data->buffer;
@@ -328,7 +341,9 @@
                 ble_resp_data = NULL;                
                 at_resp = AT_RESP_NONE;
                 break;
+            }
             case AT_BLE_NAME_RESP:  
+            {
                 // AT_BLE_MAC_RESP response state 
                 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_NAME_RESP RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) ble_resp_data->buffer;
@@ -337,12 +352,15 @@
                 ble_resp_data = NULL;                
                 at_resp = AT_RESP_NONE;
                 break;
+            }
             default:
+            {
                  //UNKNOWN response state
                 dbg_printf(LOG, "\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n");
                 sendAtConfirmationFreeMpool(ERROR_RESP);   //_parser.send(OK_RESP);
                 at_resp = AT_RESP_NONE;
                 break;
+            }
         }
 }
 
@@ -574,6 +592,29 @@
     }
     return false;
 }
+
+int ATCmdManager::readEDMmodeBytes(uint8_t *rx_buf_ptr, int pLen){
+    int n, m=0,sz=0;
+    do{
+        sz = (pLen-m);
+        if(sz > 224)
+        {
+            sz = 224;
+        }
+        n = _parser.read((char *)rx_buf_ptr, sz);
+        if(n == -1) 
+        {
+            dbg_printf(LOG, "Timeout while reading message payload bytes - expected %d but %d read so far!\r\n", pLen, m);
+            //free(rx_buf_ptr); // make sure to free buffer
+            //rx_buf_ptr = NULL;
+            //sendAtConfirmation(UART_TIMEOUT_ERROR);
+            return n;
+        }
+        m+=n;
+    }while(m < pLen);
+    return n;
+}
+
 // OOB processing
 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
     set_timeout(timeout);
@@ -622,12 +663,25 @@
                 else
                 {
                     int pLen = edm_hdr.payloadLen-2-pT;
-                    rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte
+                    rx_buf_ptr = (uint8_t *) malloc(pLen); // we already read 2 bytes from payload but expect 1 stop byte
+                    if(rx_buf_ptr == NULL) 
+                    {
+                        dbg_printf(LOG, "Memory allocation failed!\r\n");
+                        free(rx_buf_ptr); // make sure to free buffer
+                        rx_buf_ptr = NULL;
+                        sendAtConfirmation(MEMORY_ALLOCATION_ERROR);
+                        break; // timeout!
+                    }
                     rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected
+                    // set timeout to 100 ms for large packet
+                    set_timeout(100);
                     n = _parser.read((char *)rx_buf_ptr, pLen);
+                    //n = readEDMmodeBytes(rx_buf_ptr, pLen);
+                    // reset timeout
+                    set_timeout(timeout);
                     if(n == -1) 
                     {
-                        dbg_printf(LOG, "Timeout while reading message payload bytes - expected %d!\r\n", pLen);
+                        //dbg_printf(LOG, "Timeout while reading message payload bytes - expected %d!\r\n", pLen);
                         free(rx_buf_ptr); // make sure to free buffer
                         rx_buf_ptr = NULL;
                         sendAtConfirmation(UART_TIMEOUT_ERROR);
@@ -637,9 +691,7 @@
                     printBufferInHex(rx_buf_ptr, pLen);
                     dbg_printf(LOG, "rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
                     if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
-                        _smutex.lock();
-                        _parser.send("ERR");
-                        _smutex.unlock();
+                        sendAtConfirmation("ERR");
                         free(rx_buf_ptr); // make sure to free buffer
                         rx_buf_ptr = NULL;
                         break; // exit if stop byte not found - possible data corruption!
@@ -895,21 +947,27 @@
         switch(bleOrWifi)
         {
             case 1:
+            {
                 dbg_printf(LOG, "\nATCmdParser: BLE MAC Address request received");
                 at_ble_msg_t data_req;
                 data_req.ble_cmd = BLE_CMD_MAC_ADDR;
                 // queue next BLE command
                 queueBleDataRequest(data_req);
                 break;
+            }
             case 2:
+            {
                 dbg_printf(LOG, "\nATCmdParser: WiFi MAC Address request received");                
                 wifi_cmd_t cmd = WIFI_CMD_WIFI_MAC_ADDR;
                 // queue next command
                 queueWiFiCommand(cmd);
                 break;
+            }
             default:
+            {
                 dbg_printf(LOG, "\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
                 break;
+            }
         }
     } else {
         dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed");
@@ -1228,7 +1286,7 @@
 void ATCmdManager::_oob_getNetworkStatus()
 {
     int if_id; // interface id for request
-    _parser.scanf("%d", &if_id);
+    _parser.scanf(",%d", &if_id);
     dbg_printf(LOG, "\n Received Get Network Status command!!\n");
     if(if_id == WIFI_INTERFACE_ID){
         wifi_cmd_t cmd = WIFI_CMD_NETWORK_STATUS;
--- a/source/ATCmdManager.h	Thu Jun 20 20:46:25 2019 +0000
+++ b/source/ATCmdManager.h	Sun Jul 07 20:36:41 2019 +0000
@@ -17,6 +17,7 @@
 #define ERROR_RESP "\r\nERROR\r\n"
 #define WIFI_BUSY_RESP "\r\nWIFI BUSY\r\n"
 #define UART_TIMEOUT_ERROR "\r\nUART TIMEOUT ERROR\r\n"
+#define MEMORY_ALLOCATION_ERROR "\r\nMEMORY ALLOCATION ERROR\r\n"
 #define BOX_UBLOX_DEMO_TESTING
 //extern void print_memory_info();
 //extern void blinkLEDs();
@@ -166,6 +167,7 @@
     bool        dequeueBleDataResponse();
     bool        queueBleDataRequest(at_ble_msg_t data_req);
     void        filterHttpResponse();
+    int         readEDMmodeBytes(uint8_t *rx_buf_ptr, int pLen);
     
     /**
     * Allows timeout to be changed between commands
--- a/source/WiFiManager.cpp	Thu Jun 20 20:46:25 2019 +0000
+++ b/source/WiFiManager.cpp	Sun Jul 07 20:36:41 2019 +0000
@@ -276,6 +276,7 @@
                 while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS && is_connected==false){
                     wait(1); // wait 1 sec
                 }
+                wifiCmd = WIFI_CMD_NONE;
                 if(is_connected==false){
                     if(outputBuffersAvailable() == false) // first free it
                     {
@@ -290,7 +291,6 @@
                 else {
                     sendATresponseString(AT_CONNECT_RESP);
                 }
-                wifiCmd = WIFI_CMD_NONE;
                 wifiBusy = 0;
                 break;
             }
@@ -303,6 +303,7 @@
                 }
                 wifiBusy = 1;
                 error = disconnect();
+                wifiCmd = WIFI_CMD_NONE;
                 if(error >= 0)
                 {
                     int secCount = 0;
@@ -324,11 +325,9 @@
                     // attempt reconnection if always connected scheme is set
                     if(internet_config->connectionScheme == ALWAYS_CONNECTED)
                     {
-                        wifiCmd = WIFI_CMD_NONE;
                         setNextCommand(WIFI_CMD_CONNECT);
                     }
                 }
-                wifiCmd = WIFI_CMD_NONE;
                 wifiBusy = 0;
                 break;
             }
@@ -349,7 +348,7 @@
                 callback_semaphore.wait();
 #endif
                 int msecCount = 0;
-                while(!backgroundTaskCompleted && msecCount < 2000)
+                while(!backgroundTaskCompleted && msecCount < 1000)
                 {
                     msecCount++;
                     wait_ms(10);
@@ -1008,19 +1007,13 @@
         int i = 0;
         responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4
         responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP
-        dbg_printf(LOG, "Buffer available \r\n");
         if(is_connected && result>=0)
         {
             memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address
-            dbg_printf(LOG, "IP Address OK \r\n");
             strcpy(internet_config->remote_IPv4Address, address->get_ip_address());
-            dbg_printf(LOG, "Remote IP address OK \r\n");
             i +=4;
             uint16_t port = address->get_port();
-            delete address;
-            dbg_printf(LOG, "Port OK \r\n");
             internet_config->remote_port = port;
-            dbg_printf(LOG, "Remote port OK \r\n");
             memcpy(&responseBytes[i], &port, 2); // remote IPv4 port #
             i +=2;
             // local IPv4 address
@@ -1036,7 +1029,6 @@
             responseBytes[i++] = 0;
             responseBytes[i]   = 0;
             printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN);
-            dbg_printf(LOG, "About to send response bytes... \r\n");
             sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN);
         }
         else
--- a/source/common_config.h	Thu Jun 20 20:46:25 2019 +0000
+++ b/source/common_config.h	Sun Jul 07 20:36:41 2019 +0000
@@ -12,7 +12,7 @@
 #define UBLOX_ODIN_W2_RECV_TIMEOUT    5
 #endif
 #define USE_MALLOC_FOR_COMMAND_MEMORY_POOL
-#define WIFI_CONNECT_TIMEOUT_SECS 120
+#define WIFI_CONNECT_TIMEOUT_SECS 30
 #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
--- a/source/main-https.cpp	Thu Jun 20 20:46:25 2019 +0000
+++ b/source/main-https.cpp	Sun Jul 07 20:36:41 2019 +0000
@@ -117,7 +117,7 @@
 Semaphore sync_sema;
 
 Thread atcmd_evt_thread(osPriorityNormal, 1024);
-Thread wifi_evt_thread; //(osPriorityNormal, 6*1024);
+Thread wifi_evt_thread;
 #include "network-helper.h"
 
 /* List of trusted root CA certificates
@@ -522,7 +522,7 @@
                 mainLoop = MAIN_IDLE;
                 break;
         }
-        wait(0.1);
+        wait(1);
     }
 }