Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
Diff: source/ATCmdManager.cpp
- Revision:
- 92:ec9550034276
- Parent:
- 91:d6b6319ad681
- Child:
- 93:06e755a80187
--- a/source/ATCmdManager.cpp Sun Apr 07 17:31:56 2019 +0000 +++ b/source/ATCmdManager.cpp Sat Apr 13 14:17:29 2019 +0000 @@ -2,6 +2,7 @@ #include "common_config.h" #include "common_types.h" #include "http_request.h" +#include "mbed_memory_status.h" //#include "mbed_memory_status.h" ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, @@ -64,6 +65,7 @@ printf("\n --- ATCmdManager constructor completed ---\n"); at_resp = AT_RESP_NONE; dataMode = AT_CMD_DATA_MODE; + print_heap_and_isr_stack_info(); event_queue.call_every(3600000,&print_memory_info); } @@ -89,7 +91,7 @@ // AT_SCAN_RESP response state printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n"); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -97,7 +99,7 @@ // AT_DETAILED_SCAN_RESP response state printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n"); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -105,7 +107,7 @@ // AT_CONNECT_RESP response state printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n"); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -114,7 +116,7 @@ //////_smutex.lock(); printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n"); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -123,7 +125,7 @@ //////_smutex.lock(); printf("\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\n"); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -131,7 +133,7 @@ // AT_CONFIG_RESP response state printf("\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\n"); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -140,7 +142,7 @@ printf("\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\n"); return_response(); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -149,7 +151,7 @@ printf("\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\n"); return_response(true); // set download paramter to true _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -158,7 +160,7 @@ printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n"); return_response(); _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -167,7 +169,7 @@ printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n"); return_response(true); // set download paramter to true _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -175,7 +177,7 @@ // UNKNOWN response state _smutex.lock(); printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\n"); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); at_resp = AT_RESP_NONE; break; @@ -329,7 +331,7 @@ // OOB processing void ATCmdManager::_process_oob(uint32_t timeout, bool all){ set_timeout(timeout); - static int cnt = 0; + int cnt = 0; int start; //channel_id_t chan_id; if(dataMode == AT_EXT_DATA_MODE) @@ -337,32 +339,48 @@ int n; //if(cnt++ % 10 == 0)printf("In EDM mode\n"); uint8_t edm[EDM_HDR_LEN]; - + char cmdType[16]; // Poll for edm packets do{ - n = _parser.read((char *)edm, EDM_HDR_LEN); + n = _parser.read((char *)edm, EDM_HDR_LEN-1); edm_hdr.startByte = edm[0]; edm_hdr.payloadLen = edm[1]*256 + edm[2]; edm_hdr.payloadID = edm[3]*256 + edm[4]; - edm_hdr.channel_id = (channel_id_t) edm[5]; + edm_hdr.channel_id = (channel_id_t) 0; start = Kernel::get_ms_count(); if(n == -1) break; // break if it times out - printf("%d bytes read!\n", n); - if(n==EDM_HDR_LEN) + int pT; + if(edm_hdr.payloadID < AT_REQUEST) // + { + n += _parser.read((char *)edm, 1); + edm_hdr.channel_id = (channel_id_t) edm[0]; + pT = 0; + strcpy(cmdType, "DATA COMMAND"); + } + else + { + pT = 1; + strcpy(cmdType, "AT REQUEST"); + } + //printf("%d bytes read! : type = %s\n", n, cmdType2str(edm_hdr.payloadID)); + printf("%d bytes read! : CMD type = %s\n", n, cmdType); + if(n==(EDM_HDR_LEN-pT)) printf("Start = %d, payloadID = %d len = %d chan_id = %d\n", edm_hdr.startByte, edm_hdr.payloadID, edm_hdr.payloadLen, edm_hdr.channel_id); - if(n == EDM_HDR_LEN && validate(edm_hdr)) // if AT command use process oob to decode + if(n == (EDM_HDR_LEN-pT) && validate(edm_hdr)) // if AT command use process oob to decode { if(edm_hdr.payloadID == AT_REQUEST) { - _parser.process_oob(); + //_parser.process_oob(); + while (_parser.process_oob() && all) { + } break; } else { - int pLen = edm_hdr.payloadLen-2; + 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[pLen-1] = 0x00; // clear last byte so the readback value is as expected n = _parser.read((char *)rx_buf_ptr, pLen); @@ -391,7 +409,7 @@ printf("DATA_COMMAND received!\n"); if(createHttpRequest() == true){ _smutex.lock(); - _parser.send("OK"); + _parser.send(OK_RESP); _smutex.unlock(); } else{ @@ -399,6 +417,7 @@ _parser.send("NACK"); _smutex.unlock(); } + free(rx_buf_ptr); int stop = Kernel::get_ms_count(); printf("\n Time Elapsed = %d\n", stop-start); break; @@ -440,7 +459,7 @@ void ATCmdManager::_oob_ok_hdlr(){ _smutex.lock(); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } @@ -473,7 +492,10 @@ //AT+UMRS=230400,2,8,1,1,1 printf("\n Changing Baud Rate to %d\n", uOpts[0]); + _parser.send(OK_RESP); + wait(0.1); _serial.set_baud(uOpts[0]); + wait(0.1); printf("\n Baud Rate now %d\n", uOpts[0]); } else { @@ -493,7 +515,7 @@ printf("\n Received ATEO OOB command!!\n"); printf("\n turning echo OFF!!\n"); _parser.debug_on(false); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } @@ -504,7 +526,7 @@ printf("\n Received ATE1 OOB command!!\n"); printf("\n turning echo ON!!\n"); _parser.debug_on(true); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } @@ -527,6 +549,7 @@ case 2: printf("\nATCmdParser: Extended data Mode request received\n"); dataMode = AT_EXT_DATA_MODE; + print_heap_and_isr_stack_info(); break; default: printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n"); @@ -535,7 +558,7 @@ } else { printf("\nATCmdParser: Retrieving Uart Options failed\n"); } - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } @@ -558,28 +581,28 @@ } else { printf("\nATCmdParser: Retrieving Uart Options failed"); } - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } void ATCmdManager::_oob_get_ble_role(){ _smutex.lock(); printf("\n Received get BLE role command!!\n"); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } void ATCmdManager::_oob_ena_ble_peri(){ _smutex.lock(); printf("\n Received enable BLE Peripheral command!!\n"); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); } void ATCmdManager::_oob_reboot(){ _smutex.lock(); printf("\n Received reboot command!!\n"); - _parser.send("OK\n"); + _parser.send(OK_RESP); _parser.send("System Resetting....\n"); system_reset(); _smutex.unlock(); @@ -617,7 +640,7 @@ void ATCmdManager::_oob_scanWiFiNetworks(){ _smutex.lock(); printf("\n Received scanWiFiNetworks command!!\n"); - _parser.send("OK\n"); + _parser.send(OK_RESP); _smutex.unlock(); wifi_cmd_t cmd = WIFI_CMD_SCAN; // queue next command @@ -627,7 +650,12 @@ void ATCmdManager::_oob_connect2WiFiNetwork() { + //_smutex.lock(); + printf("\n Received WiFi Connect command!!\n"); + _parser.send(OK_RESP); wifi_cmd_t cmd = WIFI_CMD_CONNECT; + printf("\n About to Queue wifi cmd = %d!!\n", cmd); + //_smutex.unlock(); // queue next command queueWiFiCommand(cmd); return; @@ -636,7 +664,11 @@ void ATCmdManager::_oob_disconnectWiFiNetwork() { + _smutex.lock(); + printf("\n Received WiFi Disconnect command!!\n"); + _parser.send(OK_RESP); wifi_cmd_t cmd = WIFI_CMD_DISCONNECT; + _smutex.unlock(); // queue next command queueWiFiCommand(cmd); return; @@ -655,9 +687,19 @@ printf("\n read string = %s , n = %d\n", str, n); //n = sscanf(str, "=%1d,%199[^,],%1d", &(uint8_t)internet_config.peer_id, n = sscanf(str, "=%1d,%99[^,],%1d", &internet_config.peer_id, - url, //internet_config.url, - &internet_config.connectionScheme); - strncpy(internet_config.url, url, strlen(url)+1); + url, //internet_config.url, + &internet_config.connectionScheme); + char *p = strstr(url,"\""); + if(p!=NULL) + { + strncpy(internet_config.url, &p[1], strlen(url)); + p = strstr(internet_config.url,"\""); + *p = NULL; + } + else + { + strncpy(internet_config.url, url, strlen(url)+1); + } printf("\n read string = %s , n = %d -- strlen(url) = %d\n", internet_config.url, n, strlen(internet_config.url)); if(n>0) { @@ -673,7 +715,7 @@ // queue next data request queueWiFiDataRequest(data_msg); print_memory_info(); - _parser.send("OK\n"); + _parser.send(OK_RESP); } else { printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n); _parser.send("NAK\n"); @@ -737,7 +779,7 @@ data_msg.dataLen = sizeof(wifi_config_t); memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); queueWiFiDataRequest(data_msg); - _parser.send("OK\n"); + _parser.send(OK_RESP); } else { printf("\n[ATCMD MAN]: wifi configuration failed \n"); _parser.send("NAK\n"); @@ -754,14 +796,14 @@ n = readStringBytes((uint8_t *)wifi_cfg.pass, 32); if(n>0) { - printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass); + printf("ATCMD MAN]: wifi_cfg.pass = %s\n", "****************"); // package and send on wifi data queue wifi_data_msg_t data_msg; data_msg.wifi_cmd = WIFI_CMD_CONFIG; data_msg.dataLen = sizeof(wifi_config_t); memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); queueWiFiDataRequest(data_msg); - _parser.send("OK\n"); + _parser.send(OK_RESP); } else { printf("\n[ATCMD MAN]: wifi configuration failed \n"); _parser.send("NAK\n"); @@ -785,7 +827,7 @@ data_msg.dataLen = sizeof(wifi_config_t); memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); queueWiFiDataRequest(data_msg); - _parser.send("OK\n"); + _parser.send(OK_RESP); } else { printf("\n[ATCMD MAN]: wifi configuration failed \n"); _smutex.lock(); @@ -812,7 +854,7 @@ data_msg.dataLen = sizeof(wifi_config_t); memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); queueWiFiDataRequest(data_msg); - _parser.send("OK\n"); + _parser.send(OK_RESP); } else { printf("\n[ATCMD MAN]: wifi configuration failed \n"); _parser.send("NAK\n"); @@ -823,9 +865,27 @@ } bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){ + printf("[ATCMD MAN] about to be queued with wifi_cmd = %d\n", cmd); +#ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc(); + if(wifiCmd == NULL){ + printf("[ATCMD MAN] queued memory allocation failed\n"); + print_memory_info(); + print_heap_and_isr_stack_info(); + return false; + } +#else + wifi_cmd_message_t *wifiCmd = (wifi_cmd_message_t *) malloc(sizeof(wifi_cmd_message_t)); + if(wifiCmd == NULL){ + printf("[ATCMD MAN] tr malloc() : queued memory allocation failed\n"); + print_memory_info(); + print_heap_and_isr_stack_info(); + return false; + } +#endif wifiCmd->wifi_cmd = cmd; _aT2WiFiCmdQueue->put(wifiCmd); + printf("[ATCMD MAN] queued wifi_cmd = %d\n", wifiCmd->wifi_cmd); return true; } @@ -835,7 +895,11 @@ if(evt.status == osEventMessage){ at_resp_message_t *resp = (at_resp_message_t*)evt.value.p; setNextResponse(resp->at_resp); +#ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL _wiFi2ATmPool->free(resp); +#else + free(resp); +#endif } return true; }