Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/ATCmdManager.cpp@114:b11bb96c09f3, 2019-05-18 (annotated)
- Committer:
- ocomeni
- Date:
- Sat May 18 15:44:48 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 114:b11bb96c09f3
- Parent:
- 112:a0999ea4ece0
- Child:
- 117:8fd05113efc1
main program refactored:; 1. only atcmd manager started by default; 2. added state in main to allow ATCMD to trigger enabling BLE and WiFi; 3. added main.h to expose trigger functions to ATCMD; 4. added triggers to ATCMD; ; Testing not yet done.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 103:7b566b522427 | 1 | #include "debug.h" |
ocomeni | 74:f26e846adfe9 | 2 | #include "ATCmdManager.h" |
ocomeni | 74:f26e846adfe9 | 3 | #include "common_config.h" |
ocomeni | 79:a2187bbfa407 | 4 | #include "common_types.h" |
ocomeni | 114:b11bb96c09f3 | 5 | #include "main.h" |
ocomeni | 84:7c7add00f4bf | 6 | #include "http_request.h" |
ocomeni | 92:ec9550034276 | 7 | #include "mbed_memory_status.h" |
ocomeni | 84:7c7add00f4bf | 8 | //#include "mbed_memory_status.h" |
ocomeni | 103:7b566b522427 | 9 | #define FILE_CODE "atcmd" |
ocomeni | 74:f26e846adfe9 | 10 | |
ocomeni | 78:07bb86e3ce14 | 11 | ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, |
ocomeni | 79:a2187bbfa407 | 12 | events::EventQueue &event_queue, WiFiManager *wifi, |
ocomeni | 79:a2187bbfa407 | 13 | MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, |
ocomeni | 80:e8f0e92e3ac9 | 14 | Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, |
ocomeni | 81:637a87eb8170 | 15 | MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, |
ocomeni | 81:637a87eb8170 | 16 | Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, |
ocomeni | 87:99b37d26ff2a | 17 | MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, |
ocomeni | 87:99b37d26ff2a | 18 | Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, |
ocomeni | 87:99b37d26ff2a | 19 | MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, |
ocomeni | 87:99b37d26ff2a | 20 | Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue, |
ocomeni | 79:a2187bbfa407 | 21 | bool debug) |
ocomeni | 74:f26e846adfe9 | 22 | : |
ocomeni | 74:f26e846adfe9 | 23 | _serial(tx, rx, DEFAULT_BAUD_RATE), |
ocomeni | 78:07bb86e3ce14 | 24 | blePeripheral(blePeripheral), |
ocomeni | 79:a2187bbfa407 | 25 | _event_queue(event_queue), |
ocomeni | 79:a2187bbfa407 | 26 | wiFiManager(wiFiManager), |
ocomeni | 79:a2187bbfa407 | 27 | _aT2WiFimPool(aT2WiFimPool), |
ocomeni | 80:e8f0e92e3ac9 | 28 | _aT2WiFiCmdQueue(aT2WiFiCmdQueue), |
ocomeni | 80:e8f0e92e3ac9 | 29 | |
ocomeni | 80:e8f0e92e3ac9 | 30 | _wiFi2ATmPool(wiFi2ATmPool), |
ocomeni | 80:e8f0e92e3ac9 | 31 | _wiFi2ATCmdQueue(wiFi2ATCmdQueue), |
ocomeni | 80:e8f0e92e3ac9 | 32 | |
ocomeni | 80:e8f0e92e3ac9 | 33 | _aT2WiFiDatamPool(aT2WiFiDatamPool), |
ocomeni | 80:e8f0e92e3ac9 | 34 | _aT2WiFiDataQueue(aT2WiFiDataQueue), |
ocomeni | 80:e8f0e92e3ac9 | 35 | |
ocomeni | 80:e8f0e92e3ac9 | 36 | _wiFi2ATDatamPool(wiFi2ATDatamPool), |
ocomeni | 80:e8f0e92e3ac9 | 37 | _wiFi2ATDataQueue(wiFi2ATDataQueue), |
ocomeni | 80:e8f0e92e3ac9 | 38 | |
ocomeni | 80:e8f0e92e3ac9 | 39 | _parser(&_serial) |
ocomeni | 80:e8f0e92e3ac9 | 40 | |
ocomeni | 74:f26e846adfe9 | 41 | |
ocomeni | 74:f26e846adfe9 | 42 | { |
ocomeni | 74:f26e846adfe9 | 43 | // constructor |
ocomeni | 103:7b566b522427 | 44 | _serial.set_baud(2*DEFAULT_BAUD_RATE); |
ocomeni | 103:7b566b522427 | 45 | //Flow flowCtrl = Flow::Disabled; |
ocomeni | 103:7b566b522427 | 46 | //_serial.set_flow_control(mbed::SerialBase::Disabled); |
ocomeni | 74:f26e846adfe9 | 47 | _parser.debug_on(debug); |
ocomeni | 74:f26e846adfe9 | 48 | _parser.set_delimiter("\r\n"); |
ocomeni | 103:7b566b522427 | 49 | _parser.send("+STARTUP\r\n\r\n"); |
ocomeni | 103:7b566b522427 | 50 | _parser.oob("AT\r", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 75:08eff6258e1b | 51 | _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off)); |
ocomeni | 75:08eff6258e1b | 52 | _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on)); |
ocomeni | 75:08eff6258e1b | 53 | _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup)); |
ocomeni | 75:08eff6258e1b | 54 | |
ocomeni | 75:08eff6258e1b | 55 | _parser.oob("ATO", callback(this, &ATCmdManager::_oob_data_mode)); |
ocomeni | 75:08eff6258e1b | 56 | _parser.oob("AT+UMLA", callback(this, &ATCmdManager::_oob_get_mac_addr)); |
ocomeni | 75:08eff6258e1b | 57 | _parser.oob("AT+UBTLE?", callback(this, &ATCmdManager::_oob_get_ble_role)); |
ocomeni | 75:08eff6258e1b | 58 | _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri)); |
ocomeni | 75:08eff6258e1b | 59 | _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot)); |
ocomeni | 75:08eff6258e1b | 60 | _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver)); |
ocomeni | 82:10072c1794d3 | 61 | _parser.oob("AT+UWSCAN", callback(this, &ATCmdManager::_oob_scanWiFiNetworks)); |
ocomeni | 98:65c2333a38b6 | 62 | _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_WiFiStationConfigAction)); |
ocomeni | 102:9748f290a1a5 | 63 | |
ocomeni | 102:9748f290a1a5 | 64 | _parser.oob("AT+UMLA=", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 65 | _parser.oob("AT+UBTLN", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 66 | _parser.oob("AT+UBTSM?", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 67 | _parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 68 | |
ocomeni | 102:9748f290a1a5 | 69 | _parser.oob("AT+UDSC=", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 70 | _parser.oob("AT&W", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 71 | //_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 102:9748f290a1a5 | 72 | //_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr)); |
ocomeni | 95:290859010c8c | 73 | //_parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork)); |
ocomeni | 81:637a87eb8170 | 74 | _parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection)); |
ocomeni | 114:b11bb96c09f3 | 75 | _parser.oob("AT+UWSC=0,0,0", callback(this, &ATCmdManager::_oob_enable_wifi)); |
ocomeni | 82:10072c1794d3 | 76 | _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID)); |
ocomeni | 82:10072c1794d3 | 77 | _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD)); |
ocomeni | 82:10072c1794d3 | 78 | _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity)); |
ocomeni | 95:290859010c8c | 79 | _parser.oob("AT+UNSTAT=", callback(this, &ATCmdManager::_oob_getNetworkStatus)); |
ocomeni | 95:290859010c8c | 80 | _parser.oob("AT+UWSSTAT", callback(this, &ATCmdManager::_oob_WiFiNetworkStatus)); |
ocomeni | 95:290859010c8c | 81 | |
ocomeni | 84:7c7add00f4bf | 82 | //_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage)); |
ocomeni | 103:7b566b522427 | 83 | //sendAtConfirmation("Testing:: +UBTLE:2\r\nOK\r\n"); |
ocomeni | 103:7b566b522427 | 84 | dbg_printf(LOG, "\n --- ATCmdManager constructor completed ---\n"); |
ocomeni | 81:637a87eb8170 | 85 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 86 | dataMode = AT_CMD_DATA_MODE; |
ocomeni | 98:65c2333a38b6 | 87 | debug_flag = 0; |
ocomeni | 104:11e9605093c9 | 88 | wifiStateControl = AT_RESP_NONE; |
ocomeni | 108:3c8fb2c6e7bf | 89 | //_event_queue.call_in(10, &print_heap_and_isr_stack_info); |
ocomeni | 98:65c2333a38b6 | 90 | //print_heap_and_isr_stack_info(); |
ocomeni | 108:3c8fb2c6e7bf | 91 | //_event_queue.call_every(3600000,&print_memory_info); |
ocomeni | 108:3c8fb2c6e7bf | 92 | _event_queue.call_every(10000,&blinkLEDs); |
ocomeni | 103:7b566b522427 | 93 | #ifdef BOX_UBLOX_DEMO_TESTING |
ocomeni | 103:7b566b522427 | 94 | check_for_at_cmd = false; |
ocomeni | 103:7b566b522427 | 95 | #endif |
ocomeni | 74:f26e846adfe9 | 96 | } |
ocomeni | 74:f26e846adfe9 | 97 | |
ocomeni | 74:f26e846adfe9 | 98 | |
ocomeni | 74:f26e846adfe9 | 99 | void ATCmdManager::runMain(){ |
ocomeni | 103:7b566b522427 | 100 | dbg_printf(LOG, "\r\n [ATCMD MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); |
ocomeni | 74:f26e846adfe9 | 101 | while(true){ |
ocomeni | 74:f26e846adfe9 | 102 | _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true); |
ocomeni | 81:637a87eb8170 | 103 | wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time |
ocomeni | 81:637a87eb8170 | 104 | processResponses(); |
ocomeni | 74:f26e846adfe9 | 105 | } |
ocomeni | 74:f26e846adfe9 | 106 | |
ocomeni | 74:f26e846adfe9 | 107 | } |
ocomeni | 74:f26e846adfe9 | 108 | |
ocomeni | 81:637a87eb8170 | 109 | void ATCmdManager::processResponses(){ |
ocomeni | 81:637a87eb8170 | 110 | dequeueATresponse(); |
ocomeni | 87:99b37d26ff2a | 111 | dequeueWiFidataResponse(); |
ocomeni | 104:11e9605093c9 | 112 | updateWiFiMgrStatus(); |
ocomeni | 95:290859010c8c | 113 | char * respStr; |
ocomeni | 81:637a87eb8170 | 114 | switch(at_resp){ |
ocomeni | 81:637a87eb8170 | 115 | case AT_RESP_NONE: |
ocomeni | 81:637a87eb8170 | 116 | // IDLE response state |
ocomeni | 81:637a87eb8170 | 117 | break; |
ocomeni | 81:637a87eb8170 | 118 | case AT_SCAN_RESP: |
ocomeni | 81:637a87eb8170 | 119 | // AT_SCAN_RESP response state |
ocomeni | 103:7b566b522427 | 120 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\r\n"); |
ocomeni | 95:290859010c8c | 121 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 122 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 81:637a87eb8170 | 123 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 124 | break; |
ocomeni | 81:637a87eb8170 | 125 | case AT_DETAILED_SCAN_RESP: |
ocomeni | 81:637a87eb8170 | 126 | // AT_DETAILED_SCAN_RESP response state |
ocomeni | 103:7b566b522427 | 127 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\r\n"); |
ocomeni | 95:290859010c8c | 128 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 129 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 81:637a87eb8170 | 130 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 131 | break; |
ocomeni | 81:637a87eb8170 | 132 | case AT_CONNECT_RESP: |
ocomeni | 99:05398b3184f8 | 133 | { |
ocomeni | 81:637a87eb8170 | 134 | // AT_CONNECT_RESP response state |
ocomeni | 103:7b566b522427 | 135 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\r\n"); |
ocomeni | 103:7b566b522427 | 136 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 95:290859010c8c | 137 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 138 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 99:05398b3184f8 | 139 | char * urc = new char[20]; |
ocomeni | 104:11e9605093c9 | 140 | sprintf(urc, "\r\n%s:%d\r\n", NETWORK_UP_URC, WIFI_INTERFACE_ID); |
ocomeni | 99:05398b3184f8 | 141 | sendAtEvent(urc); |
ocomeni | 99:05398b3184f8 | 142 | delete urc; |
ocomeni | 81:637a87eb8170 | 143 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 144 | break; |
ocomeni | 99:05398b3184f8 | 145 | } |
ocomeni | 81:637a87eb8170 | 146 | case AT_DISCONNECT_RESP: |
ocomeni | 81:637a87eb8170 | 147 | // AT_DISCONNECT_RESP response state |
ocomeni | 103:7b566b522427 | 148 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\r\n"); |
ocomeni | 95:290859010c8c | 149 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 150 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 81:637a87eb8170 | 151 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 152 | break; |
ocomeni | 82:10072c1794d3 | 153 | case AT_CONFIG_RESP: |
ocomeni | 82:10072c1794d3 | 154 | // AT_CONFIG_RESP response state |
ocomeni | 103:7b566b522427 | 155 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\r\n"); |
ocomeni | 95:290859010c8c | 156 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 84:7c7add00f4bf | 157 | at_resp = AT_RESP_NONE; |
ocomeni | 84:7c7add00f4bf | 158 | break; |
ocomeni | 95:290859010c8c | 159 | case AT_NETWORK_STATUS_RESP: |
ocomeni | 95:290859010c8c | 160 | // AT_CONFIG_RESP response state |
ocomeni | 103:7b566b522427 | 161 | dbg_printf(LOG, "\n [ATCMD MAN] NETWORK STATUS RESPONSE RECEIVED!!\r\n"); |
ocomeni | 95:290859010c8c | 162 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 163 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 95:290859010c8c | 164 | at_resp = AT_RESP_NONE; |
ocomeni | 95:290859010c8c | 165 | break; |
ocomeni | 95:290859010c8c | 166 | case AT_WIFI_STATUS_RESP: |
ocomeni | 95:290859010c8c | 167 | // AT_CONFIG_RESP response state |
ocomeni | 103:7b566b522427 | 168 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI RESPONSE RECEIVED!!\r\n"); |
ocomeni | 95:290859010c8c | 169 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 170 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 95:290859010c8c | 171 | at_resp = AT_RESP_NONE; |
ocomeni | 95:290859010c8c | 172 | break; |
ocomeni | 84:7c7add00f4bf | 173 | case AT_INTERNET_CONFIG_RESP: |
ocomeni | 84:7c7add00f4bf | 174 | // AT_CONFIG_RESP response state |
ocomeni | 103:7b566b522427 | 175 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\r\n"); |
ocomeni | 96:f5ed273881af | 176 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 84:7c7add00f4bf | 177 | at_resp = AT_RESP_NONE; |
ocomeni | 84:7c7add00f4bf | 178 | break; |
ocomeni | 84:7c7add00f4bf | 179 | case AT_HTTPS_RESP: |
ocomeni | 84:7c7add00f4bf | 180 | // AT_HTTP_RESP response state |
ocomeni | 103:7b566b522427 | 181 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\r\n"); |
ocomeni | 87:99b37d26ff2a | 182 | return_response(); |
ocomeni | 95:290859010c8c | 183 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 81:637a87eb8170 | 184 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 185 | break; |
ocomeni | 89:45f6db09a76d | 186 | case AT_HTTPS_RESP_DOWNLOAD: |
ocomeni | 89:45f6db09a76d | 187 | // AT_HTTPS_RESP_DOWNLOAD response state |
ocomeni | 103:7b566b522427 | 188 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\r\n"); |
ocomeni | 89:45f6db09a76d | 189 | return_response(true); // set download paramter to true |
ocomeni | 89:45f6db09a76d | 190 | at_resp = AT_RESP_NONE; |
ocomeni | 89:45f6db09a76d | 191 | break; |
ocomeni | 81:637a87eb8170 | 192 | case AT_HTTP_RESP: |
ocomeni | 81:637a87eb8170 | 193 | // AT_HTTP_RESP response state |
ocomeni | 103:7b566b522427 | 194 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\r\n"); |
ocomeni | 89:45f6db09a76d | 195 | return_response(); |
ocomeni | 89:45f6db09a76d | 196 | at_resp = AT_RESP_NONE; |
ocomeni | 89:45f6db09a76d | 197 | break; |
ocomeni | 89:45f6db09a76d | 198 | case AT_HTTP_RESP_DOWNLOAD: |
ocomeni | 89:45f6db09a76d | 199 | // AT_HTTP_RESP response state |
ocomeni | 103:7b566b522427 | 200 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\r\n"); |
ocomeni | 89:45f6db09a76d | 201 | return_response(true); // set download paramter to true |
ocomeni | 81:637a87eb8170 | 202 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 203 | break; |
ocomeni | 99:05398b3184f8 | 204 | case CONNECT_EVENT: |
ocomeni | 99:05398b3184f8 | 205 | { |
ocomeni | 99:05398b3184f8 | 206 | // Connect Event state |
ocomeni | 103:7b566b522427 | 207 | dbg_printf(LOG, "\n [ATCMD MAN] CONNECT EVENT RECEIVED!!\r\n"); |
ocomeni | 99:05398b3184f8 | 208 | sendConnectEvent(resp_data->buffer, resp_data->dataLen); |
ocomeni | 99:05398b3184f8 | 209 | at_resp = AT_RESP_NONE; |
ocomeni | 99:05398b3184f8 | 210 | break; |
ocomeni | 99:05398b3184f8 | 211 | } |
ocomeni | 100:80ef4bc31b7a | 212 | case AT_EVENT: |
ocomeni | 100:80ef4bc31b7a | 213 | { |
ocomeni | 100:80ef4bc31b7a | 214 | // AT Event state |
ocomeni | 103:7b566b522427 | 215 | dbg_printf(LOG, "\n [ATCMD MAN] AT EVENT RECEIVED!!\r\n"); |
ocomeni | 100:80ef4bc31b7a | 216 | respStr = (char *) resp_data->buffer; |
ocomeni | 100:80ef4bc31b7a | 217 | sendAtEvent(respStr); |
ocomeni | 100:80ef4bc31b7a | 218 | at_resp = AT_RESP_NONE; |
ocomeni | 100:80ef4bc31b7a | 219 | break; |
ocomeni | 100:80ef4bc31b7a | 220 | } |
ocomeni | 104:11e9605093c9 | 221 | case WIFI_WATCH_DOG: |
ocomeni | 104:11e9605093c9 | 222 | { |
ocomeni | 104:11e9605093c9 | 223 | // AT_HTTP_RESP response state |
ocomeni | 104:11e9605093c9 | 224 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI Watchdog message!!\r\n"); |
ocomeni | 104:11e9605093c9 | 225 | respStr = (char *) resp_data->buffer; |
ocomeni | 104:11e9605093c9 | 226 | sendConnectIndicationFreeMpool(respStr); |
ocomeni | 104:11e9605093c9 | 227 | at_resp = AT_RESP_NONE; |
ocomeni | 104:11e9605093c9 | 228 | break; |
ocomeni | 104:11e9605093c9 | 229 | } |
ocomeni | 93:06e755a80187 | 230 | case AT_COMMAND_FAILED: |
ocomeni | 98:65c2333a38b6 | 231 | { |
ocomeni | 93:06e755a80187 | 232 | // AT_HTTP_RESP response state |
ocomeni | 103:7b566b522427 | 233 | dbg_printf(LOG, "\n [ATCMD MAN] WIFI COMMAND FAILED!!\r\n"); |
ocomeni | 98:65c2333a38b6 | 234 | respStr = (char *) resp_data->buffer; |
ocomeni | 96:f5ed273881af | 235 | sendAtConfirmationFreeMpool(respStr); |
ocomeni | 98:65c2333a38b6 | 236 | at_resp = AT_RESP_NONE; |
ocomeni | 93:06e755a80187 | 237 | break; |
ocomeni | 98:65c2333a38b6 | 238 | } |
ocomeni | 100:80ef4bc31b7a | 239 | default: |
ocomeni | 100:80ef4bc31b7a | 240 | //UNKNOWN response state |
ocomeni | 103:7b566b522427 | 241 | dbg_printf(LOG, "\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n"); |
ocomeni | 100:80ef4bc31b7a | 242 | sendAtConfirmationFreeMpool(ERROR_RESP); //_parser.send(OK_RESP); |
ocomeni | 100:80ef4bc31b7a | 243 | at_resp = AT_RESP_NONE; |
ocomeni | 100:80ef4bc31b7a | 244 | break; |
ocomeni | 81:637a87eb8170 | 245 | } |
ocomeni | 81:637a87eb8170 | 246 | } |
ocomeni | 81:637a87eb8170 | 247 | |
ocomeni | 96:f5ed273881af | 248 | |
ocomeni | 104:11e9605093c9 | 249 | void ATCmdManager::updateWiFiMgrStatus() |
ocomeni | 104:11e9605093c9 | 250 | { |
ocomeni | 104:11e9605093c9 | 251 | static int wifiBusyMonitor = 0; |
ocomeni | 104:11e9605093c9 | 252 | // if next response is the expected response, |
ocomeni | 104:11e9605093c9 | 253 | // then last command complete, reset wifi status |
ocomeni | 104:11e9605093c9 | 254 | // pass or fail!! |
ocomeni | 104:11e9605093c9 | 255 | if(at_resp == wifiStateControl || at_resp == AT_COMMAND_FAILED) |
ocomeni | 104:11e9605093c9 | 256 | { |
ocomeni | 104:11e9605093c9 | 257 | wifiStateControl = AT_RESP_NONE; |
ocomeni | 104:11e9605093c9 | 258 | } |
ocomeni | 104:11e9605093c9 | 259 | if(at_resp == WIFI_WATCH_DOG && strstr((char *) resp_data->buffer, "WiFi Main Loop inActive") != NULL) |
ocomeni | 104:11e9605093c9 | 260 | { |
ocomeni | 104:11e9605093c9 | 261 | wifiBusyMonitor++; |
ocomeni | 104:11e9605093c9 | 262 | if(wifiBusyMonitor >= 4) |
ocomeni | 104:11e9605093c9 | 263 | { |
ocomeni | 104:11e9605093c9 | 264 | wifiStateControl = AT_RESP_NONE; |
ocomeni | 104:11e9605093c9 | 265 | wifiBusyMonitor = 0; |
ocomeni | 104:11e9605093c9 | 266 | } |
ocomeni | 104:11e9605093c9 | 267 | } |
ocomeni | 104:11e9605093c9 | 268 | } |
ocomeni | 104:11e9605093c9 | 269 | void ATCmdManager::sendConnectIndicationFreeMpool(const char *buf) |
ocomeni | 104:11e9605093c9 | 270 | { |
ocomeni | 104:11e9605093c9 | 271 | int len = strlen(buf); |
ocomeni | 105:e5ce023eee93 | 272 | outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL); |
ocomeni | 104:11e9605093c9 | 273 | _wiFi2ATDatamPool->free(resp_data); |
ocomeni | 104:11e9605093c9 | 274 | resp_data = NULL; |
ocomeni | 104:11e9605093c9 | 275 | } |
ocomeni | 104:11e9605093c9 | 276 | |
ocomeni | 96:f5ed273881af | 277 | void ATCmdManager::sendAtConfirmationFreeMpool(const char *buf) |
ocomeni | 96:f5ed273881af | 278 | { |
ocomeni | 96:f5ed273881af | 279 | sendAtConfirmation(buf); |
ocomeni | 96:f5ed273881af | 280 | _wiFi2ATDatamPool->free(resp_data); |
ocomeni | 96:f5ed273881af | 281 | resp_data = NULL; |
ocomeni | 96:f5ed273881af | 282 | } |
ocomeni | 96:f5ed273881af | 283 | |
ocomeni | 96:f5ed273881af | 284 | |
ocomeni | 104:11e9605093c9 | 285 | void ATCmdManager::printBufferInHex(const uint8_t *buf, int pLen) |
ocomeni | 89:45f6db09a76d | 286 | { |
ocomeni | 104:11e9605093c9 | 287 | print_debug_hex(buf, pLen); |
ocomeni | 89:45f6db09a76d | 288 | } |
ocomeni | 81:637a87eb8170 | 289 | |
ocomeni | 83:9c271a50a70b | 290 | bool ATCmdManager::validate(edm_header_t edm_header) |
ocomeni | 83:9c271a50a70b | 291 | { |
ocomeni | 83:9c271a50a70b | 292 | if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found! |
ocomeni | 99:05398b3184f8 | 293 | if(edm_header.payloadID != CONNECT_EVENT_ID && |
ocomeni | 99:05398b3184f8 | 294 | edm_header.payloadID != DISCONNECT_EVENT_ID && |
ocomeni | 99:05398b3184f8 | 295 | edm_header.payloadID != DATA_EVENT_ID && |
ocomeni | 99:05398b3184f8 | 296 | edm_header.payloadID != DATA_COMMAND_ID && |
ocomeni | 99:05398b3184f8 | 297 | edm_header.payloadID != AT_REQUEST_ID && |
ocomeni | 99:05398b3184f8 | 298 | edm_header.payloadID != AT_CONFIRMATION_ID && |
ocomeni | 99:05398b3184f8 | 299 | edm_header.payloadID != AT_EVENT_ID |
ocomeni | 83:9c271a50a70b | 300 | )return false; // unexpected payload ID found! |
ocomeni | 83:9c271a50a70b | 301 | if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN || |
ocomeni | 83:9c271a50a70b | 302 | edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN |
ocomeni | 83:9c271a50a70b | 303 | ) return false; // unexpected length received! |
ocomeni | 90:ed0267eca7b5 | 304 | if(edm_header.channel_id != WIFI_CHANNEL && |
ocomeni | 90:ed0267eca7b5 | 305 | edm_header.channel_id != BLE_CHANNEL |
ocomeni | 90:ed0267eca7b5 | 306 | ) return false; // unexpected channel_id received! |
ocomeni | 83:9c271a50a70b | 307 | return true; |
ocomeni | 83:9c271a50a70b | 308 | } |
ocomeni | 74:f26e846adfe9 | 309 | |
ocomeni | 84:7c7add00f4bf | 310 | http_method ATCmdManager::str2HttpMethod(const char * methodStr) |
ocomeni | 84:7c7add00f4bf | 311 | { |
ocomeni | 84:7c7add00f4bf | 312 | http_method _http_method; |
ocomeni | 84:7c7add00f4bf | 313 | if(strstr(methodStr, "POST")!= NULL){ |
ocomeni | 84:7c7add00f4bf | 314 | _http_method = HTTP_POST; |
ocomeni | 84:7c7add00f4bf | 315 | } |
ocomeni | 84:7c7add00f4bf | 316 | else if(strstr(methodStr, "GET")!= NULL){ |
ocomeni | 84:7c7add00f4bf | 317 | _http_method = HTTP_GET; |
ocomeni | 84:7c7add00f4bf | 318 | } |
ocomeni | 84:7c7add00f4bf | 319 | else{ |
ocomeni | 84:7c7add00f4bf | 320 | _http_method = HTTP_DELETE; // unsupported method - set to HTTP_DELETE |
ocomeni | 84:7c7add00f4bf | 321 | } |
ocomeni | 84:7c7add00f4bf | 322 | return _http_method; |
ocomeni | 84:7c7add00f4bf | 323 | } |
ocomeni | 84:7c7add00f4bf | 324 | bool ATCmdManager::createHttpRequest() |
ocomeni | 84:7c7add00f4bf | 325 | { |
ocomeni | 110:c722dda4f2ff | 326 | #ifdef USE_WIFI_STATE_CONTROL |
ocomeni | 108:3c8fb2c6e7bf | 327 | static int wifiBusyTimeOut = 0; |
ocomeni | 108:3c8fb2c6e7bf | 328 | if(wifiStateControl != AT_RESP_NONE && wifiBusyTimeOut < 10) // wifi busy! |
ocomeni | 104:11e9605093c9 | 329 | { |
ocomeni | 108:3c8fb2c6e7bf | 330 | wifiBusyTimeOut++; |
ocomeni | 108:3c8fb2c6e7bf | 331 | return false; |
ocomeni | 104:11e9605093c9 | 332 | } |
ocomeni | 108:3c8fb2c6e7bf | 333 | wifiBusyTimeOut = 0; |
ocomeni | 110:c722dda4f2ff | 334 | #endif |
ocomeni | 90:ed0267eca7b5 | 335 | http_request_t *http_req; // = new http_request_t; |
ocomeni | 90:ed0267eca7b5 | 336 | wifi_data_msg_t data_msg; |
ocomeni | 90:ed0267eca7b5 | 337 | http_req = (http_request_t *)data_msg.buffer; |
ocomeni | 84:7c7add00f4bf | 338 | char s1[32]; |
ocomeni | 84:7c7add00f4bf | 339 | char s2[32]; |
ocomeni | 84:7c7add00f4bf | 340 | char s3[32]; |
ocomeni | 90:ed0267eca7b5 | 341 | int len = 0; |
ocomeni | 90:ed0267eca7b5 | 342 | int n; |
ocomeni | 90:ed0267eca7b5 | 343 | char * strPtr = (char *)rx_buf_ptr; |
ocomeni | 84:7c7add00f4bf | 344 | char * p; |
ocomeni | 84:7c7add00f4bf | 345 | char * p2 = strstr(strPtr, "\r\n\r\n"); |
ocomeni | 84:7c7add00f4bf | 346 | char * nxtPtr = strPtr; |
ocomeni | 90:ed0267eca7b5 | 347 | char * outPtr; |
ocomeni | 84:7c7add00f4bf | 348 | char * bodyPtr = p2+4; |
ocomeni | 103:7b566b522427 | 349 | dbg_printf(LOG, "\nstrPtr address= %x",strPtr); |
ocomeni | 103:7b566b522427 | 350 | dbg_printf(LOG, "\np2 address= %x", p2); |
ocomeni | 84:7c7add00f4bf | 351 | for(int i = 0; i < 5; i++){ |
ocomeni | 84:7c7add00f4bf | 352 | if(i == 0)// firstline scan method uri and http_ver |
ocomeni | 84:7c7add00f4bf | 353 | { |
ocomeni | 84:7c7add00f4bf | 354 | n = sscanf(nxtPtr,"%s %s %s", s1, s2, s3); |
ocomeni | 90:ed0267eca7b5 | 355 | if(n!=3) return false; // error in input abort |
ocomeni | 105:e5ce023eee93 | 356 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 357 | dbg_printf(LOG, "\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 ); |
ocomeni | 105:e5ce023eee93 | 358 | #endif |
ocomeni | 84:7c7add00f4bf | 359 | //http_req.method = str2HttpMethod(s1.c_str()); |
ocomeni | 90:ed0267eca7b5 | 360 | http_req->method = str2HttpMethod(s1); |
ocomeni | 90:ed0267eca7b5 | 361 | http_req->request_URI = (char *) http_req->buffer; // point 1st string to start of buffer |
ocomeni | 90:ed0267eca7b5 | 362 | len = strlen(s2)+1; |
ocomeni | 90:ed0267eca7b5 | 363 | strncpy(http_req->request_URI, s2, len); |
ocomeni | 90:ed0267eca7b5 | 364 | http_req->http_version = http_req->request_URI + len; // point 2nd string to after 1st |
ocomeni | 90:ed0267eca7b5 | 365 | len = strlen(s3)+1; |
ocomeni | 90:ed0267eca7b5 | 366 | strncpy(http_req->http_version, s3, len); |
ocomeni | 105:e5ce023eee93 | 367 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 368 | dbg_printf(LOG, "\nhttp_request 1st line:\n method = %d\nuri = %s\nhttp_ver = %s",http_req->method, |
ocomeni | 90:ed0267eca7b5 | 369 | http_req->request_URI, |
ocomeni | 90:ed0267eca7b5 | 370 | http_req->http_version ); |
ocomeni | 103:7b566b522427 | 371 | dbg_printf(LOG, "\nhttp_request str ptrs\nuri = %X\nhttp_ver = %X",http_req->request_URI, |
ocomeni | 90:ed0267eca7b5 | 372 | http_req->http_version ); |
ocomeni | 105:e5ce023eee93 | 373 | #endif |
ocomeni | 90:ed0267eca7b5 | 374 | outPtr = http_req->http_version + len; // point output buffer ptr to after 2nd string |
ocomeni | 84:7c7add00f4bf | 375 | } |
ocomeni | 84:7c7add00f4bf | 376 | else{ // scan header pairs |
ocomeni | 84:7c7add00f4bf | 377 | n = sscanf(nxtPtr,"%s %s", s1, s2); |
ocomeni | 90:ed0267eca7b5 | 378 | if(n!=2) return false; // error in input abort |
ocomeni | 84:7c7add00f4bf | 379 | p = strstr(s1,":"); |
ocomeni | 84:7c7add00f4bf | 380 | *p = NULL; |
ocomeni | 105:e5ce023eee93 | 381 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 382 | dbg_printf(LOG, "\nname = %s value = %s",s1, s2); |
ocomeni | 105:e5ce023eee93 | 383 | #endif |
ocomeni | 86:04fc2fcda7ec | 384 | if(strstr(s1, "Host")!= NULL){ |
ocomeni | 90:ed0267eca7b5 | 385 | http_req->hostName = outPtr; |
ocomeni | 104:11e9605093c9 | 386 | #ifdef BOX_UBLOX_DEMO_TESTING |
ocomeni | 104:11e9605093c9 | 387 | p = strstr(s2, "//"); |
ocomeni | 104:11e9605093c9 | 388 | if(p == NULL) |
ocomeni | 104:11e9605093c9 | 389 | p = s2; |
ocomeni | 104:11e9605093c9 | 390 | else |
ocomeni | 104:11e9605093c9 | 391 | p+=2; |
ocomeni | 104:11e9605093c9 | 392 | len = strlen(p)+1; |
ocomeni | 104:11e9605093c9 | 393 | strncpy(outPtr, p, len); |
ocomeni | 104:11e9605093c9 | 394 | #else |
ocomeni | 90:ed0267eca7b5 | 395 | len = strlen(s2)+1; |
ocomeni | 90:ed0267eca7b5 | 396 | strncpy(outPtr, s2, len); |
ocomeni | 104:11e9605093c9 | 397 | #endif |
ocomeni | 105:e5ce023eee93 | 398 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 399 | dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr); |
ocomeni | 105:e5ce023eee93 | 400 | #endif |
ocomeni | 90:ed0267eca7b5 | 401 | outPtr += len; // point output buffer ptr to after current string |
ocomeni | 86:04fc2fcda7ec | 402 | } |
ocomeni | 86:04fc2fcda7ec | 403 | else if(strstr(s1, "Accept")!= NULL){ |
ocomeni | 90:ed0267eca7b5 | 404 | http_req->AcceptVal = outPtr; |
ocomeni | 90:ed0267eca7b5 | 405 | len = strlen(s2)+1; |
ocomeni | 90:ed0267eca7b5 | 406 | strncpy(outPtr, s2, len); |
ocomeni | 105:e5ce023eee93 | 407 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 408 | dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr); |
ocomeni | 105:e5ce023eee93 | 409 | #endif |
ocomeni | 90:ed0267eca7b5 | 410 | outPtr += len; // point output buffer ptr to after current string |
ocomeni | 86:04fc2fcda7ec | 411 | } |
ocomeni | 86:04fc2fcda7ec | 412 | else if(strstr(s1, "Content-Type")!= NULL){ |
ocomeni | 90:ed0267eca7b5 | 413 | http_req->contentType = outPtr; |
ocomeni | 90:ed0267eca7b5 | 414 | len = strlen(s2)+1; |
ocomeni | 90:ed0267eca7b5 | 415 | strncpy(outPtr, s2, len); |
ocomeni | 105:e5ce023eee93 | 416 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 417 | dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr); |
ocomeni | 105:e5ce023eee93 | 418 | #endif |
ocomeni | 90:ed0267eca7b5 | 419 | outPtr += len; // point output buffer ptr to after current string |
ocomeni | 86:04fc2fcda7ec | 420 | } |
ocomeni | 86:04fc2fcda7ec | 421 | else if(strstr(s1, "Content-Length")!= NULL){ |
ocomeni | 90:ed0267eca7b5 | 422 | http_req->contentLen = outPtr; |
ocomeni | 90:ed0267eca7b5 | 423 | len = strlen(s2)+1; |
ocomeni | 90:ed0267eca7b5 | 424 | strncpy(outPtr, s2, len); |
ocomeni | 105:e5ce023eee93 | 425 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 426 | dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr); |
ocomeni | 105:e5ce023eee93 | 427 | #endif |
ocomeni | 90:ed0267eca7b5 | 428 | outPtr += len; // point output buffer ptr to after current string |
ocomeni | 84:7c7add00f4bf | 429 | } |
ocomeni | 84:7c7add00f4bf | 430 | |
ocomeni | 105:e5ce023eee93 | 431 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 432 | dbg_printf(LOG, "\noutPtr = %X len = %d\n", outPtr, len); |
ocomeni | 105:e5ce023eee93 | 433 | #endif |
ocomeni | 84:7c7add00f4bf | 434 | } |
ocomeni | 84:7c7add00f4bf | 435 | nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line |
ocomeni | 84:7c7add00f4bf | 436 | if(nxtPtr >= p2) break; |
ocomeni | 84:7c7add00f4bf | 437 | } |
ocomeni | 90:ed0267eca7b5 | 438 | // print header from http_req_struct |
ocomeni | 105:e5ce023eee93 | 439 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 440 | dbg_printf(LOG, "\nhttp request header: \n %s\n", http_req->buffer); |
ocomeni | 105:e5ce023eee93 | 441 | #endif |
ocomeni | 84:7c7add00f4bf | 442 | int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr); |
ocomeni | 105:e5ce023eee93 | 443 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 444 | dbg_printf(LOG, "\nLen = %d\n", bodyLen); |
ocomeni | 105:e5ce023eee93 | 445 | #endif |
ocomeni | 90:ed0267eca7b5 | 446 | http_req->body = (uint8_t *) outPtr; |
ocomeni | 90:ed0267eca7b5 | 447 | memcpy(outPtr, bodyPtr, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 448 | if(bodyLen > 10){ |
ocomeni | 103:7b566b522427 | 449 | dbg_printf(LOG, "\n Message Body:\n"); |
ocomeni | 90:ed0267eca7b5 | 450 | printBufferInHex(http_req->body, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 451 | } |
ocomeni | 90:ed0267eca7b5 | 452 | outPtr += bodyLen; // move output pointer to end output (header + body) |
ocomeni | 84:7c7add00f4bf | 453 | // package and send on wifi data queue |
ocomeni | 84:7c7add00f4bf | 454 | data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ; |
ocomeni | 105:e5ce023eee93 | 455 | data_msg.dataLen = (uint8_t *)outPtr - http_req->buffer;//sizeof(http_request_t); |
ocomeni | 105:e5ce023eee93 | 456 | #ifdef FULL_DEBUG_ENABLED |
ocomeni | 103:7b566b522427 | 457 | dbg_printf(LOG, "\nsizeof(http_req) on population = %d\n", sizeof(*http_req)); |
ocomeni | 103:7b566b522427 | 458 | dbg_printf(LOG, "\ndata_msg.dataLen = %d\n", data_msg.dataLen); |
ocomeni | 105:e5ce023eee93 | 459 | #endif |
ocomeni | 84:7c7add00f4bf | 460 | // queue next data request |
ocomeni | 104:11e9605093c9 | 461 | if(queueWiFiDataRequest(data_msg) == true) |
ocomeni | 104:11e9605093c9 | 462 | { |
ocomeni | 104:11e9605093c9 | 463 | wifiStateControl = AT_HTTPS_RESP_DOWNLOAD; |
ocomeni | 110:c722dda4f2ff | 464 | return true; |
ocomeni | 104:11e9605093c9 | 465 | } |
ocomeni | 84:7c7add00f4bf | 466 | return true; |
ocomeni | 84:7c7add00f4bf | 467 | } |
ocomeni | 74:f26e846adfe9 | 468 | // OOB processing |
ocomeni | 74:f26e846adfe9 | 469 | void ATCmdManager::_process_oob(uint32_t timeout, bool all){ |
ocomeni | 74:f26e846adfe9 | 470 | set_timeout(timeout); |
ocomeni | 84:7c7add00f4bf | 471 | int start; |
ocomeni | 83:9c271a50a70b | 472 | if(dataMode == AT_EXT_DATA_MODE) |
ocomeni | 83:9c271a50a70b | 473 | { |
ocomeni | 83:9c271a50a70b | 474 | int n; |
ocomeni | 84:7c7add00f4bf | 475 | uint8_t edm[EDM_HDR_LEN]; |
ocomeni | 92:ec9550034276 | 476 | char cmdType[16]; |
ocomeni | 83:9c271a50a70b | 477 | // Poll for edm packets |
ocomeni | 83:9c271a50a70b | 478 | do{ |
ocomeni | 92:ec9550034276 | 479 | n = _parser.read((char *)edm, EDM_HDR_LEN-1); |
ocomeni | 84:7c7add00f4bf | 480 | edm_hdr.startByte = edm[0]; |
ocomeni | 84:7c7add00f4bf | 481 | edm_hdr.payloadLen = edm[1]*256 + edm[2]; |
ocomeni | 84:7c7add00f4bf | 482 | edm_hdr.payloadID = edm[3]*256 + edm[4]; |
ocomeni | 92:ec9550034276 | 483 | edm_hdr.channel_id = (channel_id_t) 0; |
ocomeni | 84:7c7add00f4bf | 484 | start = Kernel::get_ms_count(); |
ocomeni | 83:9c271a50a70b | 485 | if(n == -1) break; // break if it times out |
ocomeni | 92:ec9550034276 | 486 | int pT; |
ocomeni | 99:05398b3184f8 | 487 | if(edm_hdr.payloadID < AT_REQUEST_ID) // |
ocomeni | 92:ec9550034276 | 488 | { |
ocomeni | 92:ec9550034276 | 489 | n += _parser.read((char *)edm, 1); |
ocomeni | 92:ec9550034276 | 490 | edm_hdr.channel_id = (channel_id_t) edm[0]; |
ocomeni | 92:ec9550034276 | 491 | pT = 0; |
ocomeni | 92:ec9550034276 | 492 | strcpy(cmdType, "DATA COMMAND"); |
ocomeni | 92:ec9550034276 | 493 | } |
ocomeni | 92:ec9550034276 | 494 | else |
ocomeni | 92:ec9550034276 | 495 | { |
ocomeni | 92:ec9550034276 | 496 | pT = 1; |
ocomeni | 92:ec9550034276 | 497 | strcpy(cmdType, "AT REQUEST"); |
ocomeni | 92:ec9550034276 | 498 | } |
ocomeni | 103:7b566b522427 | 499 | dbg_printf(LOG, "%d bytes read! : CMD type = %s\n", n, cmdType); |
ocomeni | 92:ec9550034276 | 500 | if(n==(EDM_HDR_LEN-pT)) |
ocomeni | 103:7b566b522427 | 501 | dbg_printf(LOG, "Start = %d, payloadID = %d len = %d chan_id = %d\n", edm_hdr.startByte, |
ocomeni | 90:ed0267eca7b5 | 502 | edm_hdr.payloadID, |
ocomeni | 90:ed0267eca7b5 | 503 | edm_hdr.payloadLen, |
ocomeni | 90:ed0267eca7b5 | 504 | edm_hdr.channel_id); |
ocomeni | 92:ec9550034276 | 505 | if(n == (EDM_HDR_LEN-pT) && validate(edm_hdr)) // if AT command use process oob to decode |
ocomeni | 83:9c271a50a70b | 506 | { |
ocomeni | 99:05398b3184f8 | 507 | if(edm_hdr.payloadID == AT_REQUEST_ID) |
ocomeni | 83:9c271a50a70b | 508 | { |
ocomeni | 92:ec9550034276 | 509 | while (_parser.process_oob() && all) { |
ocomeni | 92:ec9550034276 | 510 | } |
ocomeni | 83:9c271a50a70b | 511 | break; |
ocomeni | 83:9c271a50a70b | 512 | } |
ocomeni | 83:9c271a50a70b | 513 | else |
ocomeni | 83:9c271a50a70b | 514 | { |
ocomeni | 92:ec9550034276 | 515 | int pLen = edm_hdr.payloadLen-2-pT; |
ocomeni | 83:9c271a50a70b | 516 | rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte |
ocomeni | 83:9c271a50a70b | 517 | rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected |
ocomeni | 83:9c271a50a70b | 518 | n = _parser.read((char *)rx_buf_ptr, pLen); |
ocomeni | 83:9c271a50a70b | 519 | if(n == -1) break; // timeout! |
ocomeni | 103:7b566b522427 | 520 | dbg_printf(LOG, "%d bytes read - expected %d!\n", n, pLen); |
ocomeni | 89:45f6db09a76d | 521 | printBufferInHex(rx_buf_ptr, pLen); |
ocomeni | 103:7b566b522427 | 522 | dbg_printf(LOG, "rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]); |
ocomeni | 83:9c271a50a70b | 523 | if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) { |
ocomeni | 90:ed0267eca7b5 | 524 | _smutex.lock(); |
ocomeni | 90:ed0267eca7b5 | 525 | _parser.send("ERR"); |
ocomeni | 90:ed0267eca7b5 | 526 | _smutex.unlock(); |
ocomeni | 97:0aa46e5ef15d | 527 | free(rx_buf_ptr); // make sure to free buffer |
ocomeni | 97:0aa46e5ef15d | 528 | rx_buf_ptr = NULL; |
ocomeni | 90:ed0267eca7b5 | 529 | break; // exit if stop byte not found - possible data corruption! |
ocomeni | 83:9c271a50a70b | 530 | } |
ocomeni | 83:9c271a50a70b | 531 | switch(edm_hdr.payloadID) |
ocomeni | 83:9c271a50a70b | 532 | { |
ocomeni | 99:05398b3184f8 | 533 | case CONNECT_EVENT_ID: |
ocomeni | 103:7b566b522427 | 534 | dbg_printf(LOG, "Connection Event received!\n"); |
ocomeni | 83:9c271a50a70b | 535 | break; |
ocomeni | 99:05398b3184f8 | 536 | case DISCONNECT_EVENT_ID: |
ocomeni | 103:7b566b522427 | 537 | dbg_printf(LOG, "DISCONNECT_EVENT received!\n"); |
ocomeni | 83:9c271a50a70b | 538 | break; |
ocomeni | 99:05398b3184f8 | 539 | case DATA_EVENT_ID: |
ocomeni | 103:7b566b522427 | 540 | dbg_printf(LOG, "DATA_EVENT received!\n"); |
ocomeni | 83:9c271a50a70b | 541 | break; |
ocomeni | 99:05398b3184f8 | 542 | case DATA_COMMAND_ID: |
ocomeni | 98:65c2333a38b6 | 543 | { |
ocomeni | 103:7b566b522427 | 544 | dbg_printf(LOG, "DATA_COMMAND received!\n"); |
ocomeni | 108:3c8fb2c6e7bf | 545 | if(createHttpRequest() == false) |
ocomeni | 108:3c8fb2c6e7bf | 546 | { |
ocomeni | 109:c274780ff609 | 547 | #ifdef SEND_DEBUG_MESSAGES |
ocomeni | 104:11e9605093c9 | 548 | sendAtConfirmation(WIFI_BUSY_RESP); |
ocomeni | 109:c274780ff609 | 549 | #endif |
ocomeni | 84:7c7add00f4bf | 550 | } |
ocomeni | 92:ec9550034276 | 551 | free(rx_buf_ptr); |
ocomeni | 96:f5ed273881af | 552 | rx_buf_ptr = NULL; |
ocomeni | 84:7c7add00f4bf | 553 | int stop = Kernel::get_ms_count(); |
ocomeni | 103:7b566b522427 | 554 | dbg_printf(LOG, "\n Time Elapsed = %d\n", stop-start); |
ocomeni | 83:9c271a50a70b | 555 | break; |
ocomeni | 98:65c2333a38b6 | 556 | } |
ocomeni | 99:05398b3184f8 | 557 | case AT_REQUEST_ID: |
ocomeni | 103:7b566b522427 | 558 | dbg_printf(LOG, "AT_REQUEST received!\n"); |
ocomeni | 83:9c271a50a70b | 559 | break; |
ocomeni | 99:05398b3184f8 | 560 | case AT_CONFIRMATION_ID: |
ocomeni | 103:7b566b522427 | 561 | dbg_printf(LOG, "AT_CONFIRMATION received!\n"); |
ocomeni | 83:9c271a50a70b | 562 | break; |
ocomeni | 99:05398b3184f8 | 563 | case AT_EVENT_ID: |
ocomeni | 103:7b566b522427 | 564 | dbg_printf(LOG, "AT_EVENT received!\n"); |
ocomeni | 83:9c271a50a70b | 565 | break; |
ocomeni | 83:9c271a50a70b | 566 | default: |
ocomeni | 103:7b566b522427 | 567 | dbg_printf(LOG, "UNKNOWN MESSAGE received!\n"); |
ocomeni | 83:9c271a50a70b | 568 | break; |
ocomeni | 83:9c271a50a70b | 569 | } |
ocomeni | 83:9c271a50a70b | 570 | } |
ocomeni | 83:9c271a50a70b | 571 | } |
ocomeni | 83:9c271a50a70b | 572 | else // incorrect # of bytes received abort!! |
ocomeni | 83:9c271a50a70b | 573 | { |
ocomeni | 83:9c271a50a70b | 574 | break; |
ocomeni | 83:9c271a50a70b | 575 | } |
ocomeni | 83:9c271a50a70b | 576 | }while (all); // continue to process until timeout |
ocomeni | 74:f26e846adfe9 | 577 | } |
ocomeni | 83:9c271a50a70b | 578 | else |
ocomeni | 83:9c271a50a70b | 579 | { |
ocomeni | 83:9c271a50a70b | 580 | // Poll for inbound packets |
ocomeni | 112:a0999ea4ece0 | 581 | #ifdef BOX_UBLOX_DEMO_TESTING |
ocomeni | 103:7b566b522427 | 582 | static char * ATCMD = new char[32]; |
ocomeni | 103:7b566b522427 | 583 | int n = 0; |
ocomeni | 103:7b566b522427 | 584 | if(check_for_at_cmd) |
ocomeni | 103:7b566b522427 | 585 | { |
ocomeni | 103:7b566b522427 | 586 | while (n>=0 && all) { |
ocomeni | 103:7b566b522427 | 587 | n = readAtCommandString(ATCMD, 32); |
ocomeni | 103:7b566b522427 | 588 | if(strstr(ATCMD,"AT\r") != NULL) |
ocomeni | 103:7b566b522427 | 589 | { |
ocomeni | 103:7b566b522427 | 590 | _oob_ok_hdlr(); |
ocomeni | 103:7b566b522427 | 591 | check_for_at_cmd = false; |
ocomeni | 103:7b566b522427 | 592 | break; |
ocomeni | 103:7b566b522427 | 593 | } |
ocomeni | 103:7b566b522427 | 594 | } |
ocomeni | 103:7b566b522427 | 595 | } |
ocomeni | 103:7b566b522427 | 596 | else |
ocomeni | 103:7b566b522427 | 597 | #endif |
ocomeni | 103:7b566b522427 | 598 | { |
ocomeni | 83:9c271a50a70b | 599 | while (_parser.process_oob() && all) { |
ocomeni | 110:c722dda4f2ff | 600 | } |
ocomeni | 103:7b566b522427 | 601 | } |
ocomeni | 83:9c271a50a70b | 602 | } |
ocomeni | 74:f26e846adfe9 | 603 | set_timeout(); |
ocomeni | 74:f26e846adfe9 | 604 | } |
ocomeni | 74:f26e846adfe9 | 605 | |
ocomeni | 74:f26e846adfe9 | 606 | |
ocomeni | 103:7b566b522427 | 607 | int ATCmdManager::readAtCommandString(char *strbuf, size_t bufLen) |
ocomeni | 103:7b566b522427 | 608 | { |
ocomeni | 103:7b566b522427 | 609 | int n; |
ocomeni | 103:7b566b522427 | 610 | bool atstr_found = false; |
ocomeni | 103:7b566b522427 | 611 | int i = 0; |
ocomeni | 103:7b566b522427 | 612 | while(n != -1) |
ocomeni | 103:7b566b522427 | 613 | { |
ocomeni | 103:7b566b522427 | 614 | char c; |
ocomeni | 103:7b566b522427 | 615 | n = _parser.read(&c, 1); |
ocomeni | 103:7b566b522427 | 616 | if(n) |
ocomeni | 103:7b566b522427 | 617 | strbuf[i++] = c; |
ocomeni | 103:7b566b522427 | 618 | if(c=='\r') |
ocomeni | 103:7b566b522427 | 619 | { |
ocomeni | 103:7b566b522427 | 620 | strbuf[i++] = NULL; // terminate string and exit |
ocomeni | 103:7b566b522427 | 621 | atstr_found = true; |
ocomeni | 103:7b566b522427 | 622 | break; |
ocomeni | 103:7b566b522427 | 623 | } |
ocomeni | 103:7b566b522427 | 624 | } |
ocomeni | 103:7b566b522427 | 625 | if(atstr_found) |
ocomeni | 103:7b566b522427 | 626 | { |
ocomeni | 103:7b566b522427 | 627 | return i; |
ocomeni | 103:7b566b522427 | 628 | } |
ocomeni | 103:7b566b522427 | 629 | else |
ocomeni | 103:7b566b522427 | 630 | { |
ocomeni | 103:7b566b522427 | 631 | return -1; |
ocomeni | 103:7b566b522427 | 632 | } |
ocomeni | 103:7b566b522427 | 633 | } |
ocomeni | 103:7b566b522427 | 634 | |
ocomeni | 74:f26e846adfe9 | 635 | // OOB message handlers |
ocomeni | 74:f26e846adfe9 | 636 | void ATCmdManager::_oob_startup_hdlr(){ |
ocomeni | 74:f26e846adfe9 | 637 | } |
ocomeni | 74:f26e846adfe9 | 638 | |
ocomeni | 81:637a87eb8170 | 639 | void ATCmdManager::_oob_ok_hdlr(){ |
ocomeni | 95:290859010c8c | 640 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 81:637a87eb8170 | 641 | } |
ocomeni | 81:637a87eb8170 | 642 | |
ocomeni | 74:f26e846adfe9 | 643 | |
ocomeni | 74:f26e846adfe9 | 644 | void ATCmdManager::_oob_bleRole_hdlr(){ |
ocomeni | 74:f26e846adfe9 | 645 | } |
ocomeni | 74:f26e846adfe9 | 646 | |
ocomeni | 74:f26e846adfe9 | 647 | |
ocomeni | 74:f26e846adfe9 | 648 | void ATCmdManager::_oob_wifiMode_err(){ |
ocomeni | 74:f26e846adfe9 | 649 | } |
ocomeni | 74:f26e846adfe9 | 650 | |
ocomeni | 74:f26e846adfe9 | 651 | |
ocomeni | 74:f26e846adfe9 | 652 | void ATCmdManager::_oob_conn_already(){ |
ocomeni | 74:f26e846adfe9 | 653 | } |
ocomeni | 74:f26e846adfe9 | 654 | |
ocomeni | 114:b11bb96c09f3 | 655 | void ATCmdManager::_oob_enable_wifi() |
ocomeni | 114:b11bb96c09f3 | 656 | { |
ocomeni | 114:b11bb96c09f3 | 657 | trigger_start_WiFi(); |
ocomeni | 114:b11bb96c09f3 | 658 | sendAtConfirmation(OK_RESP); |
ocomeni | 114:b11bb96c09f3 | 659 | } |
ocomeni | 74:f26e846adfe9 | 660 | |
ocomeni | 74:f26e846adfe9 | 661 | void ATCmdManager::_oob_err(){ |
ocomeni | 74:f26e846adfe9 | 662 | } |
ocomeni | 74:f26e846adfe9 | 663 | |
ocomeni | 79:a2187bbfa407 | 664 | void ATCmdManager::_oob_get_fw_ver() |
ocomeni | 79:a2187bbfa407 | 665 | { |
ocomeni | 102:9748f290a1a5 | 666 | #ifdef MBED_MAJOR_VERSION |
ocomeni | 102:9748f290a1a5 | 667 | char * fw_ver_str = new char[20]; |
ocomeni | 102:9748f290a1a5 | 668 | sprintf(fw_ver_str, "Mbed OS version %d.%d.%d.0\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
ocomeni | 102:9748f290a1a5 | 669 | sendAtConfirmation(fw_ver_str); |
ocomeni | 102:9748f290a1a5 | 670 | delete fw_ver_str; |
ocomeni | 102:9748f290a1a5 | 671 | #endif |
ocomeni | 79:a2187bbfa407 | 672 | } |
ocomeni | 79:a2187bbfa407 | 673 | |
ocomeni | 103:7b566b522427 | 674 | #define TTP_DEBUGGING |
ocomeni | 107:f1a83fd41b17 | 675 | #define TTP_DEBUGGING_UART_NOCHANGE |
ocomeni | 75:08eff6258e1b | 676 | void ATCmdManager::_oob_uart_setup(){ |
ocomeni | 75:08eff6258e1b | 677 | int uOpts[NUM_UART_OPTIONS]; |
ocomeni | 75:08eff6258e1b | 678 | if(_parser.scanf("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5]) >0) { |
ocomeni | 103:7b566b522427 | 679 | dbg_printf(LOG, "\nATCmdParser: Uart Options=%d,%d,%d,%d,%d,%d\n", uOpts[0], uOpts[1], uOpts[2], uOpts[3], uOpts[4], uOpts[5]); |
ocomeni | 103:7b566b522427 | 680 | dbg_printf(LOG, "\n Changing Baud Rate to %d\n", uOpts[0]); |
ocomeni | 107:f1a83fd41b17 | 681 | #ifdef TTP_DEBUGGING_UART_NOCHANGE |
ocomeni | 103:7b566b522427 | 682 | sendAtConfirmation(OK_RESP); |
ocomeni | 103:7b566b522427 | 683 | #else |
ocomeni | 81:637a87eb8170 | 684 | |
ocomeni | 95:290859010c8c | 685 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 81:637a87eb8170 | 686 | _serial.set_baud(uOpts[0]); |
ocomeni | 92:ec9550034276 | 687 | wait(0.1); |
ocomeni | 103:7b566b522427 | 688 | dbg_printf(LOG, "\n Baud Rate now %d\n", uOpts[0]); |
ocomeni | 103:7b566b522427 | 689 | #endif |
ocomeni | 103:7b566b522427 | 690 | #ifdef BOX_UBLOX_DEMO_TESTING |
ocomeni | 103:7b566b522427 | 691 | check_for_at_cmd = true; |
ocomeni | 103:7b566b522427 | 692 | #endif |
ocomeni | 81:637a87eb8170 | 693 | |
ocomeni | 75:08eff6258e1b | 694 | } else { |
ocomeni | 103:7b566b522427 | 695 | dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed"); |
ocomeni | 75:08eff6258e1b | 696 | } |
ocomeni | 75:08eff6258e1b | 697 | } |
ocomeni | 75:08eff6258e1b | 698 | |
ocomeni | 74:f26e846adfe9 | 699 | void ATCmdManager::set_timeout(uint32_t timeout_ms) |
ocomeni | 74:f26e846adfe9 | 700 | { |
ocomeni | 74:f26e846adfe9 | 701 | _parser.set_timeout(timeout_ms); |
ocomeni | 74:f26e846adfe9 | 702 | } |
ocomeni | 74:f26e846adfe9 | 703 | |
ocomeni | 75:08eff6258e1b | 704 | |
ocomeni | 75:08eff6258e1b | 705 | void ATCmdManager::_oob_echo_off() |
ocomeni | 75:08eff6258e1b | 706 | { |
ocomeni | 103:7b566b522427 | 707 | dbg_printf(LOG, "\n Received ATEO OOB command!!\n"); |
ocomeni | 103:7b566b522427 | 708 | dbg_printf(LOG, "\n turning echo OFF!!\n"); |
ocomeni | 75:08eff6258e1b | 709 | _parser.debug_on(false); |
ocomeni | 95:290859010c8c | 710 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 75:08eff6258e1b | 711 | } |
ocomeni | 75:08eff6258e1b | 712 | |
ocomeni | 75:08eff6258e1b | 713 | |
ocomeni | 75:08eff6258e1b | 714 | void ATCmdManager::_oob_echo_on() |
ocomeni | 75:08eff6258e1b | 715 | { |
ocomeni | 103:7b566b522427 | 716 | dbg_printf(LOG, "\n Received ATE1 OOB command!!\n"); |
ocomeni | 103:7b566b522427 | 717 | dbg_printf(LOG, "\n turning echo ON!!\n"); |
ocomeni | 75:08eff6258e1b | 718 | _parser.debug_on(true); |
ocomeni | 95:290859010c8c | 719 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 75:08eff6258e1b | 720 | } |
ocomeni | 75:08eff6258e1b | 721 | |
ocomeni | 75:08eff6258e1b | 722 | |
ocomeni | 75:08eff6258e1b | 723 | void ATCmdManager::_oob_data_mode(){ |
ocomeni | 103:7b566b522427 | 724 | dbg_printf(LOG, "\n Received EDM mode command!!\n"); |
ocomeni | 103:7b566b522427 | 725 | int dmode; |
ocomeni | 103:7b566b522427 | 726 | if(_parser.scanf("%d", &dmode) >0) { |
ocomeni | 103:7b566b522427 | 727 | dbg_printf(LOG, "\nATCmdParser: Data mode=%d\r\n", dataMode); |
ocomeni | 96:f5ed273881af | 728 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 103:7b566b522427 | 729 | switch(dmode) |
ocomeni | 75:08eff6258e1b | 730 | { |
ocomeni | 75:08eff6258e1b | 731 | case 0: |
ocomeni | 103:7b566b522427 | 732 | dbg_printf(LOG, "\nATCmdParser: Command Mode request received\n"); |
ocomeni | 81:637a87eb8170 | 733 | dataMode = AT_CMD_DATA_MODE; |
ocomeni | 75:08eff6258e1b | 734 | break; |
ocomeni | 75:08eff6258e1b | 735 | case 1: |
ocomeni | 103:7b566b522427 | 736 | dbg_printf(LOG, "\nATCmdParser: Data Mode request received\n"); |
ocomeni | 81:637a87eb8170 | 737 | dataMode = AT_STD_DATA_MODE; |
ocomeni | 75:08eff6258e1b | 738 | break; |
ocomeni | 75:08eff6258e1b | 739 | case 2: |
ocomeni | 103:7b566b522427 | 740 | dbg_printf(LOG, "\nATCmdParser: Extended data Mode request received\n"); |
ocomeni | 81:637a87eb8170 | 741 | dataMode = AT_EXT_DATA_MODE; |
ocomeni | 98:65c2333a38b6 | 742 | _event_queue.call_in(10, &print_heap_and_isr_stack_info); |
ocomeni | 98:65c2333a38b6 | 743 | //print_heap_and_isr_stack_info(); |
ocomeni | 75:08eff6258e1b | 744 | break; |
ocomeni | 75:08eff6258e1b | 745 | default: |
ocomeni | 103:7b566b522427 | 746 | dbg_printf(LOG, "\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n"); |
ocomeni | 75:08eff6258e1b | 747 | break; |
ocomeni | 75:08eff6258e1b | 748 | } |
ocomeni | 75:08eff6258e1b | 749 | } else { |
ocomeni | 103:7b566b522427 | 750 | dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed\n"); |
ocomeni | 75:08eff6258e1b | 751 | } |
ocomeni | 75:08eff6258e1b | 752 | } |
ocomeni | 75:08eff6258e1b | 753 | |
ocomeni | 75:08eff6258e1b | 754 | void ATCmdManager::_oob_get_mac_addr(){ |
ocomeni | 75:08eff6258e1b | 755 | int bleOrWifi; |
ocomeni | 75:08eff6258e1b | 756 | if(_parser.scanf("=%d", &bleOrWifi) >0) { |
ocomeni | 75:08eff6258e1b | 757 | switch(bleOrWifi) |
ocomeni | 75:08eff6258e1b | 758 | { |
ocomeni | 75:08eff6258e1b | 759 | case 1: |
ocomeni | 103:7b566b522427 | 760 | dbg_printf(LOG, "\nATCmdParser: BLE MAC Address request received"); |
ocomeni | 75:08eff6258e1b | 761 | break; |
ocomeni | 75:08eff6258e1b | 762 | case 2: |
ocomeni | 103:7b566b522427 | 763 | dbg_printf(LOG, "\nATCmdParser: WiFi MAC Address request received"); |
ocomeni | 75:08eff6258e1b | 764 | break; |
ocomeni | 75:08eff6258e1b | 765 | default: |
ocomeni | 103:7b566b522427 | 766 | dbg_printf(LOG, "\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n"); |
ocomeni | 75:08eff6258e1b | 767 | break; |
ocomeni | 75:08eff6258e1b | 768 | } |
ocomeni | 75:08eff6258e1b | 769 | } else { |
ocomeni | 103:7b566b522427 | 770 | dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed"); |
ocomeni | 75:08eff6258e1b | 771 | } |
ocomeni | 95:290859010c8c | 772 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 75:08eff6258e1b | 773 | } |
ocomeni | 75:08eff6258e1b | 774 | |
ocomeni | 75:08eff6258e1b | 775 | void ATCmdManager::_oob_get_ble_role(){ |
ocomeni | 103:7b566b522427 | 776 | dbg_printf(LOG, "\n Received get BLE role command!!\n"); |
ocomeni | 103:7b566b522427 | 777 | sendAtConfirmation("+UBTLE:2\r\nOK\r\n"); //_parser.send(OK_RESP); |
ocomeni | 75:08eff6258e1b | 778 | } |
ocomeni | 75:08eff6258e1b | 779 | |
ocomeni | 75:08eff6258e1b | 780 | void ATCmdManager::_oob_ena_ble_peri(){ |
ocomeni | 114:b11bb96c09f3 | 781 | trigger_start_BLE(); |
ocomeni | 103:7b566b522427 | 782 | dbg_printf(LOG, "\n Received enable BLE Peripheral command!!\n"); |
ocomeni | 95:290859010c8c | 783 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 75:08eff6258e1b | 784 | } |
ocomeni | 75:08eff6258e1b | 785 | |
ocomeni | 75:08eff6258e1b | 786 | void ATCmdManager::_oob_reboot(){ |
ocomeni | 103:7b566b522427 | 787 | dbg_printf(LOG, "\n Received reboot command!!\n"); |
ocomeni | 95:290859010c8c | 788 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 75:08eff6258e1b | 789 | system_reset(); |
ocomeni | 75:08eff6258e1b | 790 | } |
ocomeni | 75:08eff6258e1b | 791 | |
ocomeni | 79:a2187bbfa407 | 792 | const char * ATCmdManager::sec2str(nsapi_security_t sec) |
ocomeni | 79:a2187bbfa407 | 793 | { |
ocomeni | 79:a2187bbfa407 | 794 | switch (sec) { |
ocomeni | 79:a2187bbfa407 | 795 | case NSAPI_SECURITY_NONE: |
ocomeni | 79:a2187bbfa407 | 796 | return "None"; |
ocomeni | 79:a2187bbfa407 | 797 | case NSAPI_SECURITY_WEP: |
ocomeni | 79:a2187bbfa407 | 798 | return "WEP"; |
ocomeni | 79:a2187bbfa407 | 799 | case NSAPI_SECURITY_WPA: |
ocomeni | 79:a2187bbfa407 | 800 | return "WPA"; |
ocomeni | 79:a2187bbfa407 | 801 | case NSAPI_SECURITY_WPA2: |
ocomeni | 79:a2187bbfa407 | 802 | return "WPA2"; |
ocomeni | 79:a2187bbfa407 | 803 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 79:a2187bbfa407 | 804 | return "WPA/WPA2"; |
ocomeni | 79:a2187bbfa407 | 805 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 79:a2187bbfa407 | 806 | default: |
ocomeni | 79:a2187bbfa407 | 807 | return "Unknown"; |
ocomeni | 79:a2187bbfa407 | 808 | } |
ocomeni | 79:a2187bbfa407 | 809 | } |
ocomeni | 81:637a87eb8170 | 810 | |
ocomeni | 81:637a87eb8170 | 811 | bool ATCmdManager::setNextResponse(at_cmd_resp_t resp) |
ocomeni | 81:637a87eb8170 | 812 | { |
ocomeni | 81:637a87eb8170 | 813 | if(at_resp == AT_RESP_NONE){ |
ocomeni | 81:637a87eb8170 | 814 | at_resp = resp; |
ocomeni | 81:637a87eb8170 | 815 | return true; // success |
ocomeni | 81:637a87eb8170 | 816 | } |
ocomeni | 81:637a87eb8170 | 817 | return false; // wiFiManager busy |
ocomeni | 79:a2187bbfa407 | 818 | } |
ocomeni | 79:a2187bbfa407 | 819 | |
ocomeni | 81:637a87eb8170 | 820 | |
ocomeni | 79:a2187bbfa407 | 821 | void ATCmdManager::_oob_scanWiFiNetworks(){ |
ocomeni | 103:7b566b522427 | 822 | dbg_printf(LOG, "\n Received scanWiFiNetworks command!!\n"); |
ocomeni | 95:290859010c8c | 823 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 79:a2187bbfa407 | 824 | wifi_cmd_t cmd = WIFI_CMD_SCAN; |
ocomeni | 79:a2187bbfa407 | 825 | // queue next command |
ocomeni | 79:a2187bbfa407 | 826 | queueWiFiCommand(cmd); |
ocomeni | 81:637a87eb8170 | 827 | return; |
ocomeni | 75:08eff6258e1b | 828 | } |
ocomeni | 75:08eff6258e1b | 829 | |
ocomeni | 95:290859010c8c | 830 | |
ocomeni | 95:290859010c8c | 831 | |
ocomeni | 98:65c2333a38b6 | 832 | void ATCmdManager::_oob_WiFiStationConfigAction() |
ocomeni | 79:a2187bbfa407 | 833 | { |
ocomeni | 98:65c2333a38b6 | 834 | int if_id; // interface id for request |
ocomeni | 98:65c2333a38b6 | 835 | int aId; // interface id for request |
ocomeni | 98:65c2333a38b6 | 836 | _parser.recv("%d,%d", &if_id, &aId); |
ocomeni | 103:7b566b522427 | 837 | dbg_printf(LOG, "\n Received WiFi Configuration Action command %d %d!!\r\n", if_id, aId); |
ocomeni | 98:65c2333a38b6 | 838 | if(if_id == WIFI_CONFIG_ID && aId <5){ |
ocomeni | 95:290859010c8c | 839 | wifi_cmd_t cmd; |
ocomeni | 98:65c2333a38b6 | 840 | action_id_t action_id = (action_id_t) aId; |
ocomeni | 95:290859010c8c | 841 | switch(action_id){ |
ocomeni | 95:290859010c8c | 842 | case WIFI_CONFIG_RESET: |
ocomeni | 95:290859010c8c | 843 | break; |
ocomeni | 95:290859010c8c | 844 | case WIFI_CONFIG_STORE: |
ocomeni | 95:290859010c8c | 845 | break; |
ocomeni | 95:290859010c8c | 846 | case WIFI_CONFIG_LOAD: |
ocomeni | 95:290859010c8c | 847 | break; |
ocomeni | 95:290859010c8c | 848 | case WIFI_CONFIG_ACTIVATE: |
ocomeni | 95:290859010c8c | 849 | cmd = WIFI_CMD_CONNECT; |
ocomeni | 103:7b566b522427 | 850 | dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd); |
ocomeni | 95:290859010c8c | 851 | queueWiFiCommand(cmd); |
ocomeni | 95:290859010c8c | 852 | break; |
ocomeni | 95:290859010c8c | 853 | case WIFI_CONFIG_DEACTIVATE: |
ocomeni | 95:290859010c8c | 854 | cmd = WIFI_CMD_DISCONNECT; |
ocomeni | 103:7b566b522427 | 855 | dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd); |
ocomeni | 95:290859010c8c | 856 | queueWiFiCommand(cmd); |
ocomeni | 95:290859010c8c | 857 | break; |
ocomeni | 95:290859010c8c | 858 | default: |
ocomeni | 95:290859010c8c | 859 | break; |
ocomeni | 95:290859010c8c | 860 | } |
ocomeni | 95:290859010c8c | 861 | } |
ocomeni | 81:637a87eb8170 | 862 | return; |
ocomeni | 79:a2187bbfa407 | 863 | } |
ocomeni | 79:a2187bbfa407 | 864 | |
ocomeni | 79:a2187bbfa407 | 865 | |
ocomeni | 79:a2187bbfa407 | 866 | void ATCmdManager::_oob_disconnectWiFiNetwork() |
ocomeni | 79:a2187bbfa407 | 867 | { |
ocomeni | 103:7b566b522427 | 868 | dbg_printf(LOG, "\n Received WiFi Disconnect command!!\n"); |
ocomeni | 95:290859010c8c | 869 | sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP); |
ocomeni | 81:637a87eb8170 | 870 | wifi_cmd_t cmd = WIFI_CMD_DISCONNECT; |
ocomeni | 81:637a87eb8170 | 871 | queueWiFiCommand(cmd); |
ocomeni | 81:637a87eb8170 | 872 | return; |
ocomeni | 81:637a87eb8170 | 873 | } |
ocomeni | 81:637a87eb8170 | 874 | |
ocomeni | 81:637a87eb8170 | 875 | void ATCmdManager::_oob_setupInternetConnection() |
ocomeni | 81:637a87eb8170 | 876 | { |
ocomeni | 90:ed0267eca7b5 | 877 | char str[MAX_URL_LEN]; |
ocomeni | 90:ed0267eca7b5 | 878 | char url[MAX_URL_LEN]; |
ocomeni | 81:637a87eb8170 | 879 | int n; |
ocomeni | 81:637a87eb8170 | 880 | internet_config_t internet_config; |
ocomeni | 103:7b566b522427 | 881 | dbg_printf(LOG, "sizeof internet_config_t = %d\n", sizeof(internet_config_t)); |
ocomeni | 90:ed0267eca7b5 | 882 | n = ReadBytes((uint8_t *)str, MAX_URL_LEN); |
ocomeni | 84:7c7add00f4bf | 883 | str[n]=NULL; |
ocomeni | 112:a0999ea4ece0 | 884 | int id; |
ocomeni | 112:a0999ea4ece0 | 885 | int scheme; |
ocomeni | 103:7b566b522427 | 886 | dbg_printf(LOG, "\n read string = %s , n = %d\n", str, n); |
ocomeni | 112:a0999ea4ece0 | 887 | n = sscanf(str, "=%1d,%99[^,],%1d", &id, |
ocomeni | 92:ec9550034276 | 888 | url, //internet_config.url, |
ocomeni | 112:a0999ea4ece0 | 889 | &scheme); |
ocomeni | 112:a0999ea4ece0 | 890 | internet_config.peer_id = (uint8_t) id; |
ocomeni | 112:a0999ea4ece0 | 891 | internet_config.connectionScheme = (conn_scheme_t) scheme; |
ocomeni | 103:7b566b522427 | 892 | #ifdef BOX_UBLOX_DEMO_TESTING |
ocomeni | 103:7b566b522427 | 893 | char *p = strstr(url,"/https:"); |
ocomeni | 103:7b566b522427 | 894 | #else |
ocomeni | 92:ec9550034276 | 895 | char *p = strstr(url,"\""); |
ocomeni | 103:7b566b522427 | 896 | #endif |
ocomeni | 92:ec9550034276 | 897 | if(p!=NULL) |
ocomeni | 92:ec9550034276 | 898 | { |
ocomeni | 92:ec9550034276 | 899 | strncpy(internet_config.url, &p[1], strlen(url)); |
ocomeni | 92:ec9550034276 | 900 | p = strstr(internet_config.url,"\""); |
ocomeni | 92:ec9550034276 | 901 | *p = NULL; |
ocomeni | 92:ec9550034276 | 902 | } |
ocomeni | 92:ec9550034276 | 903 | else |
ocomeni | 92:ec9550034276 | 904 | { |
ocomeni | 92:ec9550034276 | 905 | strncpy(internet_config.url, url, strlen(url)+1); |
ocomeni | 92:ec9550034276 | 906 | } |
ocomeni | 103:7b566b522427 | 907 | dbg_printf(LOG, "\n read string = %s , n = %d -- strlen(url) = %d\n", internet_config.url, n, strlen(internet_config.url)); |
ocomeni | 81:637a87eb8170 | 908 | if(n>0) |
ocomeni | 81:637a87eb8170 | 909 | { |
ocomeni | 103:7b566b522427 | 910 | dbg_printf(LOG, "peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, |
ocomeni | 90:ed0267eca7b5 | 911 | internet_config.url, |
ocomeni | 90:ed0267eca7b5 | 912 | internet_config.connectionScheme); |
ocomeni | 81:637a87eb8170 | 913 | // package and send on wifi data queue |
ocomeni | 90:ed0267eca7b5 | 914 | wifi_data_msg_t data_msg; |
ocomeni | 81:637a87eb8170 | 915 | data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG; |
ocomeni | 84:7c7add00f4bf | 916 | data_msg.dataLen = sizeof(internet_config_t); // + strlen(internet_config.url); |
ocomeni | 103:7b566b522427 | 917 | dbg_printf(LOG, "\n url size = %d url str = %s\n",strlen(internet_config.url), internet_config.url ); |
ocomeni | 81:637a87eb8170 | 918 | memcpy(data_msg.buffer,&internet_config, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 919 | // queue next data request |
ocomeni | 81:637a87eb8170 | 920 | queueWiFiDataRequest(data_msg); |
ocomeni | 84:7c7add00f4bf | 921 | print_memory_info(); |
ocomeni | 81:637a87eb8170 | 922 | } else { |
ocomeni | 103:7b566b522427 | 923 | dbg_printf(LOG, "\n[ATCMD MAN]: internet configuration failed %d fields parsed \r\n", n); |
ocomeni | 82:10072c1794d3 | 924 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 925 | } |
ocomeni | 82:10072c1794d3 | 926 | } |
ocomeni | 82:10072c1794d3 | 927 | |
ocomeni | 82:10072c1794d3 | 928 | wifi_config_t ATCmdManager::init_wifi_config() |
ocomeni | 82:10072c1794d3 | 929 | { |
ocomeni | 82:10072c1794d3 | 930 | wifi_config_t wifi_cfg; |
ocomeni | 82:10072c1794d3 | 931 | wifi_cfg.ssid[0] = NULL; |
ocomeni | 82:10072c1794d3 | 932 | wifi_cfg.pass[0] = NULL; |
ocomeni | 82:10072c1794d3 | 933 | wifi_cfg.security = NSAPI_SECURITY_UNKNOWN; |
ocomeni | 82:10072c1794d3 | 934 | return wifi_cfg; |
ocomeni | 82:10072c1794d3 | 935 | } |
ocomeni | 112:a0999ea4ece0 | 936 | |
ocomeni | 83:9c271a50a70b | 937 | /* read ASCII characters into buffer and null terminate */ |
ocomeni | 83:9c271a50a70b | 938 | int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes) |
ocomeni | 82:10072c1794d3 | 939 | { |
ocomeni | 82:10072c1794d3 | 940 | int c; |
ocomeni | 82:10072c1794d3 | 941 | int sptr = 0; |
ocomeni | 93:06e755a80187 | 942 | int quoteCnt = 0; |
ocomeni | 82:10072c1794d3 | 943 | for(int i=0;i<maxBytes;i++){ |
ocomeni | 82:10072c1794d3 | 944 | c = _parser.getc(); |
ocomeni | 93:06e755a80187 | 945 | if(c== '"')quoteCnt++; |
ocomeni | 93:06e755a80187 | 946 | if(c==-1 || quoteCnt==2){ |
ocomeni | 83:9c271a50a70b | 947 | buf[sptr] = NULL; // null terminate if string |
ocomeni | 82:10072c1794d3 | 948 | return i; |
ocomeni | 82:10072c1794d3 | 949 | } |
ocomeni | 82:10072c1794d3 | 950 | if(c != ',' && c!= '"'){ |
ocomeni | 82:10072c1794d3 | 951 | buf[sptr++] = (uint8_t) c; |
ocomeni | 82:10072c1794d3 | 952 | } |
ocomeni | 81:637a87eb8170 | 953 | } |
ocomeni | 83:9c271a50a70b | 954 | return maxBytes; |
ocomeni | 83:9c271a50a70b | 955 | } |
ocomeni | 83:9c271a50a70b | 956 | |
ocomeni | 83:9c271a50a70b | 957 | int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes) |
ocomeni | 83:9c271a50a70b | 958 | { |
ocomeni | 83:9c271a50a70b | 959 | int c; |
ocomeni | 83:9c271a50a70b | 960 | int sptr = 0; |
ocomeni | 83:9c271a50a70b | 961 | for(int i=0;i<maxBytes;i++){ |
ocomeni | 83:9c271a50a70b | 962 | c = _parser.getc(); |
ocomeni | 83:9c271a50a70b | 963 | if(c==-1){ |
ocomeni | 83:9c271a50a70b | 964 | return i; |
ocomeni | 83:9c271a50a70b | 965 | } |
ocomeni | 83:9c271a50a70b | 966 | buf[sptr++] = (uint8_t) c; |
ocomeni | 83:9c271a50a70b | 967 | } |
ocomeni | 83:9c271a50a70b | 968 | return maxBytes; |
ocomeni | 82:10072c1794d3 | 969 | } |
ocomeni | 82:10072c1794d3 | 970 | |
ocomeni | 82:10072c1794d3 | 971 | void ATCmdManager::_oob_setWiFiSSID() |
ocomeni | 82:10072c1794d3 | 972 | { |
ocomeni | 82:10072c1794d3 | 973 | int n; |
ocomeni | 82:10072c1794d3 | 974 | wifi_config_t wifi_cfg = init_wifi_config(); |
ocomeni | 83:9c271a50a70b | 975 | n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32); |
ocomeni | 103:7b566b522427 | 976 | dbg_printf(LOG, "[ATCMD MAN]: number of bytes read = %d\n", n); |
ocomeni | 82:10072c1794d3 | 977 | if(n>0) |
ocomeni | 82:10072c1794d3 | 978 | { |
ocomeni | 103:7b566b522427 | 979 | dbg_printf(LOG, "[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid); |
ocomeni | 82:10072c1794d3 | 980 | // package and send on wifi data queue |
ocomeni | 82:10072c1794d3 | 981 | wifi_data_msg_t data_msg; |
ocomeni | 82:10072c1794d3 | 982 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 82:10072c1794d3 | 983 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 82:10072c1794d3 | 984 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 985 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 986 | } else { |
ocomeni | 103:7b566b522427 | 987 | dbg_printf(LOG, "\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 82:10072c1794d3 | 988 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 989 | } |
ocomeni | 82:10072c1794d3 | 990 | } |
ocomeni | 82:10072c1794d3 | 991 | |
ocomeni | 82:10072c1794d3 | 992 | void ATCmdManager::_oob_setWiFiPWD() |
ocomeni | 82:10072c1794d3 | 993 | { |
ocomeni | 82:10072c1794d3 | 994 | int n; |
ocomeni | 82:10072c1794d3 | 995 | wifi_config_t wifi_cfg = init_wifi_config(); |
ocomeni | 83:9c271a50a70b | 996 | n = readStringBytes((uint8_t *)wifi_cfg.pass, 32); |
ocomeni | 82:10072c1794d3 | 997 | if(n>0) |
ocomeni | 82:10072c1794d3 | 998 | { |
ocomeni | 103:7b566b522427 | 999 | dbg_printf(LOG, "ATCMD MAN]: wifi_cfg.pass = %s\n", "****************"); |
ocomeni | 82:10072c1794d3 | 1000 | // package and send on wifi data queue |
ocomeni | 82:10072c1794d3 | 1001 | wifi_data_msg_t data_msg; |
ocomeni | 82:10072c1794d3 | 1002 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 82:10072c1794d3 | 1003 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 82:10072c1794d3 | 1004 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 1005 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 1006 | } else { |
ocomeni | 103:7b566b522427 | 1007 | dbg_printf(LOG, "\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 82:10072c1794d3 | 1008 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 1009 | } |
ocomeni | 82:10072c1794d3 | 1010 | } |
ocomeni | 82:10072c1794d3 | 1011 | |
ocomeni | 82:10072c1794d3 | 1012 | void ATCmdManager::_oob_setWiFiSecurity() |
ocomeni | 82:10072c1794d3 | 1013 | { |
ocomeni | 82:10072c1794d3 | 1014 | int n; |
ocomeni | 82:10072c1794d3 | 1015 | wifi_config_t wifi_cfg = init_wifi_config(); |
ocomeni | 112:a0999ea4ece0 | 1016 | int security; |
ocomeni | 112:a0999ea4ece0 | 1017 | n = _parser.scanf(",%d", &security); |
ocomeni | 82:10072c1794d3 | 1018 | if(n>0) |
ocomeni | 82:10072c1794d3 | 1019 | { |
ocomeni | 112:a0999ea4ece0 | 1020 | wifi_cfg.security = (nsapi_security_t) security; |
ocomeni | 103:7b566b522427 | 1021 | dbg_printf(LOG, "ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security)); |
ocomeni | 82:10072c1794d3 | 1022 | // package and send on wifi data queue |
ocomeni | 82:10072c1794d3 | 1023 | wifi_data_msg_t data_msg; |
ocomeni | 82:10072c1794d3 | 1024 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 82:10072c1794d3 | 1025 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 82:10072c1794d3 | 1026 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 1027 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 1028 | } else { |
ocomeni | 103:7b566b522427 | 1029 | dbg_printf(LOG, "\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 90:ed0267eca7b5 | 1030 | _smutex.lock(); |
ocomeni | 82:10072c1794d3 | 1031 | _parser.send("NAK\n"); |
ocomeni | 90:ed0267eca7b5 | 1032 | _smutex.unlock(); |
ocomeni | 82:10072c1794d3 | 1033 | } |
ocomeni | 79:a2187bbfa407 | 1034 | } |
ocomeni | 81:637a87eb8170 | 1035 | |
ocomeni | 83:9c271a50a70b | 1036 | |
ocomeni | 95:290859010c8c | 1037 | |
ocomeni | 95:290859010c8c | 1038 | void ATCmdManager::_oob_getNetworkStatus() |
ocomeni | 95:290859010c8c | 1039 | { |
ocomeni | 112:a0999ea4ece0 | 1040 | int if_id; // interface id for request |
ocomeni | 95:290859010c8c | 1041 | _parser.scanf(",%d", &if_id); |
ocomeni | 103:7b566b522427 | 1042 | dbg_printf(LOG, "\n Received Get Network Status command!!\n"); |
ocomeni | 95:290859010c8c | 1043 | if(if_id == WIFI_INTERFACE_ID){ |
ocomeni | 95:290859010c8c | 1044 | wifi_cmd_t cmd = WIFI_CMD_NETWORK_STATUS; |
ocomeni | 103:7b566b522427 | 1045 | dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd); |
ocomeni | 95:290859010c8c | 1046 | queueWiFiCommand(cmd); |
ocomeni | 95:290859010c8c | 1047 | } |
ocomeni | 95:290859010c8c | 1048 | return; |
ocomeni | 95:290859010c8c | 1049 | } |
ocomeni | 95:290859010c8c | 1050 | |
ocomeni | 95:290859010c8c | 1051 | |
ocomeni | 95:290859010c8c | 1052 | void ATCmdManager::_oob_WiFiNetworkStatus() |
ocomeni | 95:290859010c8c | 1053 | { |
ocomeni | 103:7b566b522427 | 1054 | dbg_printf(LOG, "\n Received Get WiFi Network Status command!!\n"); |
ocomeni | 95:290859010c8c | 1055 | wifi_cmd_t cmd = WIFI_CMD_WIFI_STATUS; |
ocomeni | 103:7b566b522427 | 1056 | dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd); |
ocomeni | 95:290859010c8c | 1057 | // queue next command |
ocomeni | 95:290859010c8c | 1058 | queueWiFiCommand(cmd); |
ocomeni | 95:290859010c8c | 1059 | return; |
ocomeni | 95:290859010c8c | 1060 | } |
ocomeni | 95:290859010c8c | 1061 | |
ocomeni | 83:9c271a50a70b | 1062 | void ATCmdManager::_oob_sendHttpMessage() |
ocomeni | 83:9c271a50a70b | 1063 | { |
ocomeni | 83:9c271a50a70b | 1064 | } |
ocomeni | 83:9c271a50a70b | 1065 | |
ocomeni | 81:637a87eb8170 | 1066 | bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){ |
ocomeni | 103:7b566b522427 | 1067 | dbg_printf(LOG, "[ATCMD MAN] about to be queued with wifi_cmd = %d\n", cmd); |
ocomeni | 92:ec9550034276 | 1068 | #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL |
ocomeni | 81:637a87eb8170 | 1069 | wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc(); |
ocomeni | 92:ec9550034276 | 1070 | if(wifiCmd == NULL){ |
ocomeni | 103:7b566b522427 | 1071 | dbg_printf(LOG, "[ATCMD MAN] queued memory allocation failed\n"); |
ocomeni | 92:ec9550034276 | 1072 | return false; |
ocomeni | 92:ec9550034276 | 1073 | } |
ocomeni | 92:ec9550034276 | 1074 | #else |
ocomeni | 92:ec9550034276 | 1075 | wifi_cmd_message_t *wifiCmd = (wifi_cmd_message_t *) malloc(sizeof(wifi_cmd_message_t)); |
ocomeni | 92:ec9550034276 | 1076 | if(wifiCmd == NULL){ |
ocomeni | 103:7b566b522427 | 1077 | dbg_printf(LOG, "[ATCMD MAN] try malloc() : queued memory allocation failed\n"); |
ocomeni | 92:ec9550034276 | 1078 | return false; |
ocomeni | 92:ec9550034276 | 1079 | } |
ocomeni | 92:ec9550034276 | 1080 | #endif |
ocomeni | 81:637a87eb8170 | 1081 | wifiCmd->wifi_cmd = cmd; |
ocomeni | 81:637a87eb8170 | 1082 | _aT2WiFiCmdQueue->put(wifiCmd); |
ocomeni | 103:7b566b522427 | 1083 | dbg_printf(LOG, "[ATCMD MAN] queued wifi_cmd = %d\n", wifiCmd->wifi_cmd); |
ocomeni | 98:65c2333a38b6 | 1084 | if((int)cmd == 5) |
ocomeni | 98:65c2333a38b6 | 1085 | debug_flag = 3; |
ocomeni | 81:637a87eb8170 | 1086 | return true; |
ocomeni | 81:637a87eb8170 | 1087 | } |
ocomeni | 81:637a87eb8170 | 1088 | |
ocomeni | 81:637a87eb8170 | 1089 | bool ATCmdManager::dequeueATresponse(){ |
ocomeni | 81:637a87eb8170 | 1090 | if(at_resp != AT_RESP_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 1091 | osEvent evt = _wiFi2ATCmdQueue->get(0); |
ocomeni | 81:637a87eb8170 | 1092 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 1093 | at_resp_message_t *resp = (at_resp_message_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 1094 | setNextResponse(resp->at_resp); |
ocomeni | 103:7b566b522427 | 1095 | dbg_printf(LOG, "[ATCMD MAN] dequeued AT CMD : at_resp = %d\n", resp->at_resp); |
ocomeni | 92:ec9550034276 | 1096 | #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL |
ocomeni | 81:637a87eb8170 | 1097 | _wiFi2ATmPool->free(resp); |
ocomeni | 96:f5ed273881af | 1098 | resp = NULL; |
ocomeni | 92:ec9550034276 | 1099 | #else |
ocomeni | 92:ec9550034276 | 1100 | free(resp); |
ocomeni | 96:f5ed273881af | 1101 | resp = NULL; |
ocomeni | 92:ec9550034276 | 1102 | #endif |
ocomeni | 81:637a87eb8170 | 1103 | } |
ocomeni | 81:637a87eb8170 | 1104 | return true; |
ocomeni | 81:637a87eb8170 | 1105 | } |
ocomeni | 81:637a87eb8170 | 1106 | |
ocomeni | 81:637a87eb8170 | 1107 | bool ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){ |
ocomeni | 104:11e9605093c9 | 1108 | static bool memFull = false; |
ocomeni | 81:637a87eb8170 | 1109 | wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc(); |
ocomeni | 104:11e9605093c9 | 1110 | if(wifiData == NULL) |
ocomeni | 104:11e9605093c9 | 1111 | { |
ocomeni | 109:c274780ff609 | 1112 | #ifdef SEND_DEBUG_MESSAGES |
ocomeni | 104:11e9605093c9 | 1113 | sendAtConfirmation("\r\nQUEUE MEMORY FULL\r\n"); |
ocomeni | 109:c274780ff609 | 1114 | #endif |
ocomeni | 104:11e9605093c9 | 1115 | memFull = true; |
ocomeni | 104:11e9605093c9 | 1116 | return false; |
ocomeni | 104:11e9605093c9 | 1117 | } |
ocomeni | 104:11e9605093c9 | 1118 | if(memFull) |
ocomeni | 104:11e9605093c9 | 1119 | { |
ocomeni | 104:11e9605093c9 | 1120 | memFull = false; |
ocomeni | 109:c274780ff609 | 1121 | #ifdef SEND_DEBUG_MESSAGES |
ocomeni | 104:11e9605093c9 | 1122 | sendAtConfirmation("\r\n[ATCMD-MAN] memory released...\r\n"); |
ocomeni | 109:c274780ff609 | 1123 | #endif |
ocomeni | 104:11e9605093c9 | 1124 | } |
ocomeni | 81:637a87eb8170 | 1125 | wifiData->wifi_cmd = data_req.wifi_cmd; |
ocomeni | 81:637a87eb8170 | 1126 | wifiData->dataLen = data_req.dataLen; |
ocomeni | 81:637a87eb8170 | 1127 | memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen); |
ocomeni | 81:637a87eb8170 | 1128 | _aT2WiFiDataQueue->put(wifiData); |
ocomeni | 103:7b566b522427 | 1129 | dbg_printf(LOG, "[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd); |
ocomeni | 81:637a87eb8170 | 1130 | return true; |
ocomeni | 81:637a87eb8170 | 1131 | } |
ocomeni | 81:637a87eb8170 | 1132 | |
ocomeni | 87:99b37d26ff2a | 1133 | bool ATCmdManager::dequeueWiFidataResponse(){ |
ocomeni | 81:637a87eb8170 | 1134 | if(at_resp != AT_RESP_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 1135 | osEvent evt = _wiFi2ATDataQueue->get(0); |
ocomeni | 81:637a87eb8170 | 1136 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 1137 | resp_data = (at_data_msg_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 1138 | setNextResponse(resp_data->at_resp); |
ocomeni | 103:7b566b522427 | 1139 | dbg_printf(LOG, "[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", resp_data->dataLen, resp_data->at_resp); |
ocomeni | 81:637a87eb8170 | 1140 | } |
ocomeni | 81:637a87eb8170 | 1141 | return true; |
ocomeni | 81:637a87eb8170 | 1142 | } |
ocomeni | 81:637a87eb8170 | 1143 | |
ocomeni | 93:06e755a80187 | 1144 | |
ocomeni | 93:06e755a80187 | 1145 | void ATCmdManager::sendAtConfirmation(const char *buf) |
ocomeni | 93:06e755a80187 | 1146 | { |
ocomeni | 96:f5ed273881af | 1147 | _smutex.lock(); |
ocomeni | 95:290859010c8c | 1148 | switch(dataMode){ |
ocomeni | 95:290859010c8c | 1149 | case AT_CMD_DATA_MODE: |
ocomeni | 98:65c2333a38b6 | 1150 | _parser.send("%s", buf); |
ocomeni | 95:290859010c8c | 1151 | break; |
ocomeni | 95:290859010c8c | 1152 | case AT_STD_DATA_MODE: |
ocomeni | 98:65c2333a38b6 | 1153 | _parser.send("%s", buf); |
ocomeni | 95:290859010c8c | 1154 | break; |
ocomeni | 95:290859010c8c | 1155 | case AT_EXT_DATA_MODE: |
ocomeni | 98:65c2333a38b6 | 1156 | { |
ocomeni | 96:f5ed273881af | 1157 | int len = strlen(buf); |
ocomeni | 95:290859010c8c | 1158 | outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL); |
ocomeni | 95:290859010c8c | 1159 | break; |
ocomeni | 98:65c2333a38b6 | 1160 | } |
ocomeni | 95:290859010c8c | 1161 | default: |
ocomeni | 98:65c2333a38b6 | 1162 | _parser.send("%s", buf); |
ocomeni | 95:290859010c8c | 1163 | break; |
ocomeni | 95:290859010c8c | 1164 | } |
ocomeni | 96:f5ed273881af | 1165 | _smutex.unlock(); |
ocomeni | 93:06e755a80187 | 1166 | } |
ocomeni | 93:06e755a80187 | 1167 | void ATCmdManager::sendAtEvent(const char *buf) |
ocomeni | 93:06e755a80187 | 1168 | { |
ocomeni | 96:f5ed273881af | 1169 | _smutex.lock(); |
ocomeni | 95:290859010c8c | 1170 | switch(dataMode){ |
ocomeni | 95:290859010c8c | 1171 | case AT_CMD_DATA_MODE: |
ocomeni | 95:290859010c8c | 1172 | _parser.send(buf); |
ocomeni | 95:290859010c8c | 1173 | break; |
ocomeni | 95:290859010c8c | 1174 | case AT_STD_DATA_MODE: |
ocomeni | 95:290859010c8c | 1175 | _parser.send(buf); |
ocomeni | 95:290859010c8c | 1176 | break; |
ocomeni | 95:290859010c8c | 1177 | case AT_EXT_DATA_MODE: |
ocomeni | 99:05398b3184f8 | 1178 | { |
ocomeni | 96:f5ed273881af | 1179 | int len = strlen(buf); |
ocomeni | 95:290859010c8c | 1180 | outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, EVENT_MSG_TYPE, NO_CHANNEL); |
ocomeni | 95:290859010c8c | 1181 | break; |
ocomeni | 99:05398b3184f8 | 1182 | } |
ocomeni | 99:05398b3184f8 | 1183 | default: |
ocomeni | 99:05398b3184f8 | 1184 | _parser.send(buf); |
ocomeni | 99:05398b3184f8 | 1185 | break; |
ocomeni | 95:290859010c8c | 1186 | } |
ocomeni | 96:f5ed273881af | 1187 | _smutex.unlock(); |
ocomeni | 100:80ef4bc31b7a | 1188 | _wiFi2ATDatamPool->free(resp_data); |
ocomeni | 100:80ef4bc31b7a | 1189 | resp_data = NULL; |
ocomeni | 93:06e755a80187 | 1190 | } |
ocomeni | 93:06e755a80187 | 1191 | |
ocomeni | 99:05398b3184f8 | 1192 | |
ocomeni | 99:05398b3184f8 | 1193 | void ATCmdManager::sendConnectEvent(const uint8_t *buf, int len) |
ocomeni | 99:05398b3184f8 | 1194 | { |
ocomeni | 99:05398b3184f8 | 1195 | switch(dataMode){ |
ocomeni | 99:05398b3184f8 | 1196 | case AT_CMD_DATA_MODE: |
ocomeni | 99:05398b3184f8 | 1197 | _parser.send((const char*) buf); |
ocomeni | 99:05398b3184f8 | 1198 | break; |
ocomeni | 99:05398b3184f8 | 1199 | case AT_STD_DATA_MODE: |
ocomeni | 99:05398b3184f8 | 1200 | _parser.send((const char*)buf); |
ocomeni | 99:05398b3184f8 | 1201 | break; |
ocomeni | 99:05398b3184f8 | 1202 | case AT_EXT_DATA_MODE: |
ocomeni | 99:05398b3184f8 | 1203 | outputEDMdata((const uint8_t *) buf, len, CONNECT_MSG_ID, EVENT_MSG_TYPE, WIFI_CHANNEL); |
ocomeni | 99:05398b3184f8 | 1204 | break; |
ocomeni | 99:05398b3184f8 | 1205 | } |
ocomeni | 100:80ef4bc31b7a | 1206 | _wiFi2ATDatamPool->free(resp_data); |
ocomeni | 100:80ef4bc31b7a | 1207 | resp_data = NULL; |
ocomeni | 100:80ef4bc31b7a | 1208 | |
ocomeni | 99:05398b3184f8 | 1209 | } |
ocomeni | 99:05398b3184f8 | 1210 | |
ocomeni | 99:05398b3184f8 | 1211 | |
ocomeni | 90:ed0267eca7b5 | 1212 | void ATCmdManager::outputEDMdata(const uint8_t *buf, int pLen, |
ocomeni | 91:d6b6319ad681 | 1213 | edm_msg_id_t identifier, edm_msg_type_t type, |
ocomeni | 91:d6b6319ad681 | 1214 | channel_id_t channel_id) |
ocomeni | 90:ed0267eca7b5 | 1215 | { |
ocomeni | 90:ed0267eca7b5 | 1216 | int epLen = pLen + 2; // edm payload length = data length + 2 |
ocomeni | 96:f5ed273881af | 1217 | if(channel_id != NO_CHANNEL) |
ocomeni | 96:f5ed273881af | 1218 | epLen += 1; |
ocomeni | 90:ed0267eca7b5 | 1219 | _smutex.lock(); |
ocomeni | 90:ed0267eca7b5 | 1220 | // send EDM Message start byte |
ocomeni | 90:ed0267eca7b5 | 1221 | _parser.putc(EDM_START_BYTE); |
ocomeni | 90:ed0267eca7b5 | 1222 | // send EDM Message length |
ocomeni | 90:ed0267eca7b5 | 1223 | _parser.putc(epLen>>8); |
ocomeni | 90:ed0267eca7b5 | 1224 | _parser.putc(epLen%256); |
ocomeni | 90:ed0267eca7b5 | 1225 | // send EDM Identifier + Type |
ocomeni | 90:ed0267eca7b5 | 1226 | _parser.putc(identifier>>8); |
ocomeni | 90:ed0267eca7b5 | 1227 | _parser.putc(identifier%256 | type); |
ocomeni | 93:06e755a80187 | 1228 | // send channel id if valid |
ocomeni | 93:06e755a80187 | 1229 | if(channel_id != NO_CHANNEL) |
ocomeni | 93:06e755a80187 | 1230 | _parser.putc(channel_id); |
ocomeni | 90:ed0267eca7b5 | 1231 | // send the data |
ocomeni | 90:ed0267eca7b5 | 1232 | _parser.write((const char *)buf, pLen); |
ocomeni | 90:ed0267eca7b5 | 1233 | // send EDM Message stop byte |
ocomeni | 90:ed0267eca7b5 | 1234 | _parser.putc(EDM_STOP_BYTE); |
ocomeni | 98:65c2333a38b6 | 1235 | _smutex.unlock(); |
ocomeni | 96:f5ed273881af | 1236 | int msWait = (pLen + 5+20)/20; |
ocomeni | 96:f5ed273881af | 1237 | wait_ms(msWait); |
ocomeni | 90:ed0267eca7b5 | 1238 | } |
ocomeni | 81:637a87eb8170 | 1239 | |
ocomeni | 89:45f6db09a76d | 1240 | void ATCmdManager::return_response(bool download) { |
ocomeni | 87:99b37d26ff2a | 1241 | char * resp = (char *) resp_data->buffer; |
ocomeni | 103:7b566b522427 | 1242 | dbg_printf(LOG, "\n[ATCMD MAN] received response:\n"); |
ocomeni | 89:45f6db09a76d | 1243 | if(download == false) // not download must be ascii header |
ocomeni | 89:45f6db09a76d | 1244 | { |
ocomeni | 103:7b566b522427 | 1245 | dbg_printf(LOG, "%.*s\r\n", resp_data->dataLen, resp); |
ocomeni | 89:45f6db09a76d | 1246 | } |
ocomeni | 89:45f6db09a76d | 1247 | else // dump payload as hex |
ocomeni | 89:45f6db09a76d | 1248 | { |
ocomeni | 89:45f6db09a76d | 1249 | printBufferInHex((uint8_t *)resp, resp_data->dataLen); |
ocomeni | 89:45f6db09a76d | 1250 | } |
ocomeni | 91:d6b6319ad681 | 1251 | outputEDMdata((const uint8_t *)resp, resp_data->dataLen, DATA_MSG_ID, |
ocomeni | 91:d6b6319ad681 | 1252 | EVENT_MSG_TYPE, WIFI_CHANNEL); |
ocomeni | 87:99b37d26ff2a | 1253 | _wiFi2ATDatamPool->free(resp_data); |
ocomeni | 96:f5ed273881af | 1254 | resp_data = NULL; |
ocomeni | 87:99b37d26ff2a | 1255 | } |