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

Dependencies:   mbed-http

Revision:
96:f5ed273881af
Parent:
95:290859010c8c
Child:
97:0aa46e5ef15d
--- a/source/ATCmdManager.cpp	Wed Apr 17 18:11:45 2019 +0000
+++ b/source/ATCmdManager.cpp	Fri Apr 19 08:51:58 2019 +0000
@@ -93,110 +93,110 @@
                 break;
             case AT_SCAN_RESP:
                 // AT_SCAN_RESP response state
-                printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\r\n");
                 //_smutex.lock();
                 //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
                 respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_DETAILED_SCAN_RESP:
                 // AT_DETAILED_SCAN_RESP response state
-                printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\r\n");
                 //_smutex.lock();
                 //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
                 respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_CONNECT_RESP:  
                 // AT_CONNECT_RESP response state 
-                printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\r\n");
                 //_smutex.lock();
                 //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_DISCONNECT_RESP:  
                 // AT_DISCONNECT_RESP response state 
                 //////_smutex.lock();
-                printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\r\n");
                 //_smutex.lock();
                 //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
                 respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_CONFIG_RESP:  
                 // AT_CONFIG_RESP response state 
                 //////_smutex.lock();
-                printf("\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\n");
-                _smutex.lock();
+                printf("\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\r\n");
+                //_smutex.lock();
                 sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-                _smutex.unlock();
+                //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_NETWORK_STATUS_RESP:  
                 // AT_CONFIG_RESP response state 
                 //////_smutex.lock();
-                printf("\n [ATCMD MAN] NETWORK STATUS RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] NETWORK STATUS RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_WIFI_STATUS_RESP:  
                 // AT_CONFIG_RESP response state 
                 //////_smutex.lock();
-                printf("\n [ATCMD MAN] WIFI RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI RESPONSE RECEIVED!!\r\n");
                 respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_INTERNET_CONFIG_RESP:  
                 // AT_CONFIG_RESP response state 
-                printf("\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\r\n");
                 //_smutex.lock();
-                //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
+                sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
-                respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                //respStr = (char *) resp_data->buffer;
+                //sendAtConfirmationFreeMpool(respStr);
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_HTTPS_RESP:  
                 // AT_HTTP_RESP response state 
-                printf("\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\r\n");
                 return_response();
-                _smutex.lock();
+                //_smutex.lock();
                 sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-                _smutex.unlock();
+                //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_HTTPS_RESP_DOWNLOAD:  
                 // AT_HTTPS_RESP_DOWNLOAD response state 
-                printf("\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\r\n");
                 return_response(true); // set download paramter to true
-                _smutex.lock();
+                //_smutex.lock();
                 sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-                _smutex.unlock();
+                //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_HTTP_RESP:  
                 // AT_HTTP_RESP response state 
-                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\r\n");
                 return_response(); 
-                _smutex.lock();
+                //_smutex.lock();
                 sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-                _smutex.unlock();
+                //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
             case AT_HTTP_RESP_DOWNLOAD:  
                 // AT_HTTP_RESP response state 
-                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
+                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\r\n");
                 return_response(true); // set download paramter to true
                 //_smutex.lock();
                 //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
@@ -205,25 +205,34 @@
                 break;
             case AT_COMMAND_FAILED:  
                 // AT_HTTP_RESP response state 
-                printf("\n [ATCMD MAN] WIFI COMMAND FAILED!!\n");
+                printf("\n [ATCMD MAN] WIFI COMMAND FAILED!!\r\n");
                 //_smutex.lock();
                 //_parser.send("\r\nNACK\r\n");
                 //_smutex.unlock();
                 char * respStr = (char *) resp_data->buffer;
-                sendAtConfirmation(respStr);
+                sendAtConfirmationFreeMpool(respStr);
                 AT_RESP_NONE;
                 break;
             default:
                 // UNKNOWN response state
                 //_smutex.lock();
-                printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\n");
-                sendAtConfirmation(ERROR_RESP);   //_parser.send(OK_RESP);
+                printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n");
+                sendAtConfirmationFreeMpool(ERROR_RESP);   //_parser.send(OK_RESP);
                 //_smutex.unlock();
                 at_resp = AT_RESP_NONE;
                 break;
         }
 }
 
+
+void ATCmdManager::sendAtConfirmationFreeMpool(const char *buf)
+{
+    sendAtConfirmation(buf);
+    _wiFi2ATDatamPool->free(resp_data);
+    resp_data = NULL;
+}
+
+
 void ATCmdManager::printBufferInHex(uint8_t *buf, int pLen)
 {
     for(int i =0;i<pLen;i++){
@@ -447,10 +456,11 @@
                             break;
                         case DATA_COMMAND:
                             printf("DATA_COMMAND received!\n");
+                            sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
                             if(createHttpRequest() == true){
-                                _smutex.lock();
-                                sendAtConfirmation(OK_RESP);   //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-                                _smutex.unlock();
+                                //_smutex.lock();
+                                //sendAtConfirmation(OK_RESP);   //sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
+                                //_smutex.unlock();
                             }
                             else{
                                 _smutex.lock();
@@ -458,6 +468,7 @@
                                 _smutex.unlock();
                             }
                             free(rx_buf_ptr);
+                            rx_buf_ptr = NULL;
                             int stop = Kernel::get_ms_count();
                             printf("\n Time Elapsed = %d\n", stop-start);
                             break;
@@ -498,9 +509,9 @@
 }
 
 void ATCmdManager::_oob_ok_hdlr(){
-    _smutex.lock();
+    //_smutex.lock();
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 
@@ -551,31 +562,32 @@
 
 void ATCmdManager::_oob_echo_off()
 {
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received ATEO OOB command!!\n");
     printf("\n turning echo OFF!!\n");
     _parser.debug_on(false);
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 
 void ATCmdManager::_oob_echo_on()
 {
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received ATE1 OOB command!!\n");
     printf("\n turning echo ON!!\n");
     _parser.debug_on(true);
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 
 void ATCmdManager::_oob_data_mode(){
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received EDM mode command!!\n");
     if(_parser.scanf("%d", &dataMode) >0) {
         printf("\nATCmdParser: Data mode=%d\n", dataMode);
+        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
         switch(dataMode)
         {
             case 0:
@@ -598,13 +610,12 @@
     } else {
         printf("\nATCmdParser: Retrieving Uart Options failed\n");
     }
-    sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 void ATCmdManager::_oob_get_mac_addr(){
     int bleOrWifi;
-    _smutex.lock();
+    //_smutex.lock();
     if(_parser.scanf("=%d", &bleOrWifi) >0) {
         switch(bleOrWifi)
         {
@@ -622,30 +633,30 @@
         printf("\nATCmdParser: Retrieving Uart Options failed");
     }
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 void ATCmdManager::_oob_get_ble_role(){
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received get BLE role command!!\n");
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 void ATCmdManager::_oob_ena_ble_peri(){
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received enable BLE Peripheral command!!\n");
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 void ATCmdManager::_oob_reboot(){
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received reboot command!!\n");
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     _parser.send("System Resetting....\n");
     system_reset();
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 const char * ATCmdManager::sec2str(nsapi_security_t sec)
@@ -678,10 +689,10 @@
 
 
 void ATCmdManager::_oob_scanWiFiNetworks(){
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received scanWiFiNetworks command!!\n");
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
-    _smutex.unlock();
+    //_smutex.unlock();
     wifi_cmd_t cmd = WIFI_CMD_SCAN;
     // queue next command
     queueWiFiCommand(cmd);
@@ -696,7 +707,7 @@
     action_id_t action_id; // interface id for request
     _parser.scanf("%d,%d", &if_id, &action_id);
     //_smutex.lock();
-    printf("\n Received WiFi Configuration Action command!!\n");
+    printf("\n Received WiFi Configuration Action command!!\r\n");
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     if(if_id == WIFI_CONFIG_ID && (uint8_t)action_id <5){
         wifi_cmd_t cmd;
@@ -731,11 +742,11 @@
 
 void ATCmdManager::_oob_disconnectWiFiNetwork()
 {
-    _smutex.lock();
+    //_smutex.lock();
     printf("\n Received WiFi Disconnect command!!\n");
     sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
-    _smutex.unlock();
+    //_smutex.unlock();
     // queue next command
     queueWiFiCommand(cmd);
     return;
@@ -747,7 +758,7 @@
     char url[MAX_URL_LEN];
     int n;
     internet_config_t internet_config;
-    _smutex.lock();
+    //_smutex.lock();
     printf("sizeof internet_config_t = %d\n", sizeof(internet_config_t));
     n = ReadBytes((uint8_t *)str, MAX_URL_LEN);
     str[n]=NULL;
@@ -779,15 +790,15 @@
         data_msg.dataLen = sizeof(internet_config_t); // + strlen(internet_config.url);
         printf("\n url size = %d url str = %s\n",strlen(internet_config.url), internet_config.url );
         memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
+        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
         // queue next data request
         queueWiFiDataRequest(data_msg);
         print_memory_info();
-        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     } else {
-        printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n);
+        printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \r\n", n);
         _parser.send("NAK\n");
     }
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 wifi_config_t ATCmdManager::init_wifi_config()
@@ -836,7 +847,7 @@
 {
     int n;
     wifi_config_t wifi_cfg = init_wifi_config();
-    _smutex.lock();
+    //_smutex.lock();
     n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32);
     printf("[ATCMD MAN]: number of bytes read = %d\n", n);
     if(n>0) 
@@ -847,20 +858,20 @@
         data_msg.wifi_cmd = WIFI_CMD_CONFIG;
         data_msg.dataLen = sizeof(wifi_config_t);
         memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
+        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
         queueWiFiDataRequest(data_msg);
-        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     } else {
         printf("\n[ATCMD MAN]: wifi configuration failed  \n");
         _parser.send("NAK\n");
     }
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 void ATCmdManager::_oob_setWiFiPWD()
 {
     int n;
     wifi_config_t wifi_cfg = init_wifi_config();
-    _smutex.lock();
+    //_smutex.lock();
     //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass);
     n = readStringBytes((uint8_t *)wifi_cfg.pass, 32);
     if(n>0) 
@@ -871,13 +882,13 @@
         data_msg.wifi_cmd = WIFI_CMD_CONFIG;
         data_msg.dataLen = sizeof(wifi_config_t);
         memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
+        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
         queueWiFiDataRequest(data_msg);
-        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     } else {
         printf("\n[ATCMD MAN]: wifi configuration failed  \n");
         _parser.send("NAK\n");
     }
-    _smutex.unlock();
+    //_smutex.unlock();
 }
 
 void ATCmdManager::_oob_setWiFiSecurity()
@@ -895,8 +906,8 @@
         data_msg.wifi_cmd = WIFI_CMD_CONFIG;
         data_msg.dataLen = sizeof(wifi_config_t);
         memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
+        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
         queueWiFiDataRequest(data_msg);
-        sendAtConfirmation(OK_RESP);   //_parser.send(OK_RESP);
     } else {
         printf("\n[ATCMD MAN]: wifi configuration failed  \n");
         _smutex.lock();
@@ -996,8 +1007,10 @@
         setNextResponse(resp->at_resp);
 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
         _wiFi2ATmPool->free(resp);
+        resp = NULL;
 #else
         free(resp);
+        resp = NULL;
 #endif
     }
     return true;
@@ -1030,6 +1043,7 @@
 
 void ATCmdManager::sendAtConfirmation(const char *buf)
 {
+    _smutex.lock();
     switch(dataMode){
         case AT_CMD_DATA_MODE:
             _parser.send(buf);
@@ -1038,16 +1052,19 @@
             _parser.send(buf);
             break;
         case AT_EXT_DATA_MODE:
-            int len =  strlen(buf) + 1;
+            int len =  strlen(buf);
             outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL);
             break;
         default:
             _parser.send(buf);
             break;
     }
+    //wait_ms(1);
+    _smutex.unlock();
 }
 void ATCmdManager::sendAtEvent(const char *buf)
 {
+    _smutex.lock();
     switch(dataMode){
         case AT_CMD_DATA_MODE:
             _parser.send(buf);
@@ -1056,13 +1073,14 @@
             _parser.send(buf);
             break;
         case AT_EXT_DATA_MODE:
-            int len =  strlen(buf) + 1;
+            int len =  strlen(buf);
             outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, EVENT_MSG_TYPE, NO_CHANNEL);
             break;
         default:
             _parser.send(buf);
             break;
     }
+    _smutex.unlock();
 }
 
 void ATCmdManager::outputEDMdata(const uint8_t *buf, int pLen, 
@@ -1070,6 +1088,8 @@
                                  channel_id_t channel_id)
 {
     int epLen = pLen + 2; // edm payload length = data length + 2
+    if(channel_id != NO_CHANNEL)
+        epLen += 1;
     _smutex.lock();
     // send EDM Message start byte
     _parser.putc(EDM_START_BYTE);
@@ -1086,6 +1106,8 @@
     _parser.write((const char *)buf, pLen);
     // send EDM Message stop byte
     _parser.putc(EDM_STOP_BYTE);
+    int msWait = (pLen + 5+20)/20;
+    wait_ms(msWait);
     _smutex.unlock();
 }
 
@@ -1106,7 +1128,7 @@
                   EVENT_MSG_TYPE, WIFI_CHANNEL);
     _smutex.unlock();
     _wiFi2ATDatamPool->free(resp_data);
-    
+    resp_data = NULL;
     //print_memory_info();
     print_heap_and_isr_stack_info();   
 }