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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Fri Jul 19 16:49:26 2019 +0000
Branch:
PassingRegression
Revision:
129:590bdc2dcf5b
Parent:
127:a21788227ca6
Implementation of Access token acquisition; 1. make request with credentials - DONE; 2. get response - DONE; 3. extract Id and refresh tokens from response - DONE; 4. integrate with code - DONE; Testing ongoing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 103:7b566b522427 1 #include "debug.h"
ocomeni 74:f26e846adfe9 2 #include "ATCmdManager.h"
ocomeni 79:a2187bbfa407 3 #include "common_types.h"
ocomeni 114:b11bb96c09f3 4 #include "main.h"
ocomeni 84:7c7add00f4bf 5 #include "http_request.h"
ocomeni 92:ec9550034276 6 #include "mbed_memory_status.h"
ocomeni 84:7c7add00f4bf 7 //#include "mbed_memory_status.h"
ocomeni 103:7b566b522427 8 #define FILE_CODE "atcmd"
ocomeni 74:f26e846adfe9 9
ocomeni 117:8fd05113efc1 10 ATCmdManager::ATCmdManager(PinName tx, PinName rx, uart_config_t *uart_config,
ocomeni 79:a2187bbfa407 11 events::EventQueue &event_queue, WiFiManager *wifi,
ocomeni 79:a2187bbfa407 12 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool,
ocomeni 80:e8f0e92e3ac9 13 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
ocomeni 81:637a87eb8170 14 MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool,
ocomeni 81:637a87eb8170 15 Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue,
ocomeni 87:99b37d26ff2a 16 MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool,
ocomeni 87:99b37d26ff2a 17 Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue,
ocomeni 87:99b37d26ff2a 18 MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool,
ocomeni 87:99b37d26ff2a 19 Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue,
ocomeni 118:8df0e9c2ee3f 20 MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool,
ocomeni 118:8df0e9c2ee3f 21 Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue,
ocomeni 118:8df0e9c2ee3f 22 MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool,
ocomeni 118:8df0e9c2ee3f 23 Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue,
ocomeni 121:ac4f59839e4f 24 startup_config_t *startup_config, bool debug)
ocomeni 74:f26e846adfe9 25 :
ocomeni 74:f26e846adfe9 26 _serial(tx, rx, DEFAULT_BAUD_RATE),
ocomeni 117:8fd05113efc1 27 uart_config(uart_config),
ocomeni 79:a2187bbfa407 28 _event_queue(event_queue),
ocomeni 79:a2187bbfa407 29 wiFiManager(wiFiManager),
ocomeni 79:a2187bbfa407 30 _aT2WiFimPool(aT2WiFimPool),
ocomeni 80:e8f0e92e3ac9 31 _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
ocomeni 80:e8f0e92e3ac9 32
ocomeni 80:e8f0e92e3ac9 33 _wiFi2ATmPool(wiFi2ATmPool),
ocomeni 80:e8f0e92e3ac9 34 _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
ocomeni 80:e8f0e92e3ac9 35
ocomeni 80:e8f0e92e3ac9 36 _aT2WiFiDatamPool(aT2WiFiDatamPool),
ocomeni 80:e8f0e92e3ac9 37 _aT2WiFiDataQueue(aT2WiFiDataQueue),
ocomeni 80:e8f0e92e3ac9 38
ocomeni 80:e8f0e92e3ac9 39 _wiFi2ATDatamPool(wiFi2ATDatamPool),
ocomeni 80:e8f0e92e3ac9 40 _wiFi2ATDataQueue(wiFi2ATDataQueue),
ocomeni 118:8df0e9c2ee3f 41 _aT2BleDatamPool(aT2BleDatamPool),
ocomeni 118:8df0e9c2ee3f 42 _aT2BleDataQueue(aT2BleDataQueue),
ocomeni 118:8df0e9c2ee3f 43 _ble2ATDatamPool(ble2ATDatamPool),
ocomeni 118:8df0e9c2ee3f 44 _ble2ATDataQueue(ble2ATDataQueue),
ocomeni 121:ac4f59839e4f 45 startup_config(startup_config),
ocomeni 118:8df0e9c2ee3f 46 _parser(&_serial)
ocomeni 80:e8f0e92e3ac9 47
ocomeni 74:f26e846adfe9 48
ocomeni 74:f26e846adfe9 49 {
ocomeni 74:f26e846adfe9 50 // constructor
ocomeni 117:8fd05113efc1 51 _serial.set_baud(uart_config->baudrate);
ocomeni 103:7b566b522427 52 //Flow flowCtrl = Flow::Disabled;
ocomeni 103:7b566b522427 53 //_serial.set_flow_control(mbed::SerialBase::Disabled);
ocomeni 74:f26e846adfe9 54 _parser.debug_on(debug);
ocomeni 74:f26e846adfe9 55 _parser.set_delimiter("\r\n");
ocomeni 103:7b566b522427 56 _parser.send("+STARTUP\r\n\r\n");
ocomeni 103:7b566b522427 57 _parser.oob("AT\r", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 75:08eff6258e1b 58 _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off));
ocomeni 75:08eff6258e1b 59 _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on));
ocomeni 118:8df0e9c2ee3f 60 _parser.oob("ATE2", callback(this, &ATCmdManager::_oob_debug_logs_on));
ocomeni 118:8df0e9c2ee3f 61 _parser.oob("ATE3", callback(this, &ATCmdManager::_oob_debug_logs_off));
ocomeni 75:08eff6258e1b 62 _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup));
ocomeni 75:08eff6258e1b 63
ocomeni 75:08eff6258e1b 64 _parser.oob("ATO", callback(this, &ATCmdManager::_oob_data_mode));
ocomeni 75:08eff6258e1b 65 _parser.oob("AT+UMLA", callback(this, &ATCmdManager::_oob_get_mac_addr));
ocomeni 75:08eff6258e1b 66 _parser.oob("AT+UBTLE?", callback(this, &ATCmdManager::_oob_get_ble_role));
ocomeni 75:08eff6258e1b 67 _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri));
ocomeni 75:08eff6258e1b 68 _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot));
ocomeni 75:08eff6258e1b 69 _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
ocomeni 82:10072c1794d3 70 _parser.oob("AT+UWSCAN", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
ocomeni 98:65c2333a38b6 71 _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_WiFiStationConfigAction));
ocomeni 102:9748f290a1a5 72
ocomeni 123:a49e9ffbaca6 73 _parser.oob("AAT+UBTLN?", callback(this, &ATCmdManager::_oob_get_ble_name));
ocomeni 123:a49e9ffbaca6 74 _parser.oob("AT+UBTLN=", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 102:9748f290a1a5 75 _parser.oob("AT+UBTSM?", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 102:9748f290a1a5 76 _parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 102:9748f290a1a5 77
ocomeni 102:9748f290a1a5 78 _parser.oob("AT+UDSC=", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 121:ac4f59839e4f 79 _parser.oob("AT&W", callback(this, &ATCmdManager::_oob_saveSettings_hdlr));
ocomeni 102:9748f290a1a5 80 //_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 102:9748f290a1a5 81 //_parser.oob("AT+UBTPM", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 95:290859010c8c 82 //_parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
ocomeni 81:637a87eb8170 83 _parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection));
ocomeni 114:b11bb96c09f3 84 _parser.oob("AT+UWSC=0,0,0", callback(this, &ATCmdManager::_oob_enable_wifi));
ocomeni 82:10072c1794d3 85 _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID));
ocomeni 82:10072c1794d3 86 _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD));
ocomeni 82:10072c1794d3 87 _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity));
ocomeni 95:290859010c8c 88 _parser.oob("AT+UNSTAT=", callback(this, &ATCmdManager::_oob_getNetworkStatus));
ocomeni 95:290859010c8c 89 _parser.oob("AT+UWSSTAT", callback(this, &ATCmdManager::_oob_WiFiNetworkStatus));
ocomeni 95:290859010c8c 90
ocomeni 121:ac4f59839e4f 91 _parser.oob("AT+UFACTORY", callback(this, &ATCmdManager::_oob_factoryReset));
ocomeni 121:ac4f59839e4f 92 _parser.oob("AT+UDELCFG=", callback(this, &ATCmdManager::_oob_deleteConfiguration));
ocomeni 127:a21788227ca6 93 _parser.oob("AT+SDBGFLG", callback(this, &ATCmdManager::_oob_set_debug_flag));
ocomeni 121:ac4f59839e4f 94
ocomeni 84:7c7add00f4bf 95 //_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage));
ocomeni 103:7b566b522427 96 //sendAtConfirmation("Testing:: +UBTLE:2\r\nOK\r\n");
ocomeni 103:7b566b522427 97 dbg_printf(LOG, "\n --- ATCmdManager constructor completed ---\n");
ocomeni 81:637a87eb8170 98 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 99 dataMode = AT_CMD_DATA_MODE;
ocomeni 127:a21788227ca6 100 debug_flags_map = 0;
ocomeni 104:11e9605093c9 101 wifiStateControl = AT_RESP_NONE;
ocomeni 108:3c8fb2c6e7bf 102 //_event_queue.call_in(10, &print_heap_and_isr_stack_info);
ocomeni 98:65c2333a38b6 103 //print_heap_and_isr_stack_info();
ocomeni 108:3c8fb2c6e7bf 104 //_event_queue.call_every(3600000,&print_memory_info);
ocomeni 118:8df0e9c2ee3f 105 lastHttpRespTime = Kernel::get_ms_count();
ocomeni 118:8df0e9c2ee3f 106 lastCloudMsgType = 0x00;
ocomeni 108:3c8fb2c6e7bf 107 _event_queue.call_every(10000,&blinkLEDs);
ocomeni 103:7b566b522427 108 #ifdef BOX_UBLOX_DEMO_TESTING
ocomeni 103:7b566b522427 109 check_for_at_cmd = false;
ocomeni 103:7b566b522427 110 #endif
ocomeni 74:f26e846adfe9 111 }
ocomeni 74:f26e846adfe9 112
ocomeni 74:f26e846adfe9 113
ocomeni 74:f26e846adfe9 114 void ATCmdManager::runMain(){
ocomeni 103:7b566b522427 115 dbg_printf(LOG, "\r\n [ATCMD MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
ocomeni 74:f26e846adfe9 116 while(true){
ocomeni 74:f26e846adfe9 117 _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
ocomeni 81:637a87eb8170 118 wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time
ocomeni 81:637a87eb8170 119 processResponses();
ocomeni 74:f26e846adfe9 120 }
ocomeni 74:f26e846adfe9 121
ocomeni 74:f26e846adfe9 122 }
ocomeni 74:f26e846adfe9 123
ocomeni 81:637a87eb8170 124 void ATCmdManager::processResponses(){
ocomeni 81:637a87eb8170 125 dequeueATresponse();
ocomeni 87:99b37d26ff2a 126 dequeueWiFidataResponse();
ocomeni 120:779b74689747 127 dequeueBleDataResponse();
ocomeni 104:11e9605093c9 128 updateWiFiMgrStatus();
ocomeni 95:290859010c8c 129 char * respStr;
ocomeni 81:637a87eb8170 130 switch(at_resp){
ocomeni 81:637a87eb8170 131 case AT_RESP_NONE:
ocomeni 81:637a87eb8170 132 // IDLE response state
ocomeni 81:637a87eb8170 133 break;
ocomeni 81:637a87eb8170 134 case AT_SCAN_RESP:
ocomeni 126:9bc33f8b57d5 135 {
ocomeni 81:637a87eb8170 136 // AT_SCAN_RESP response state
ocomeni 103:7b566b522427 137 dbg_printf(LOG, "\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\r\n");
ocomeni 95:290859010c8c 138 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 139 sendAtConfirmationFreeMpool(respStr);
ocomeni 81:637a87eb8170 140 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 141 break;
ocomeni 126:9bc33f8b57d5 142 }
ocomeni 81:637a87eb8170 143 case AT_DETAILED_SCAN_RESP:
ocomeni 126:9bc33f8b57d5 144 {
ocomeni 81:637a87eb8170 145 // AT_DETAILED_SCAN_RESP response state
ocomeni 103:7b566b522427 146 dbg_printf(LOG, "\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\r\n");
ocomeni 95:290859010c8c 147 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 148 sendAtConfirmationFreeMpool(respStr);
ocomeni 81:637a87eb8170 149 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 150 break;
ocomeni 126:9bc33f8b57d5 151 }
ocomeni 81:637a87eb8170 152 case AT_CONNECT_RESP:
ocomeni 124:eae4512b131b 153 case WIFI_RECONNECT_INFO:
ocomeni 99:05398b3184f8 154 {
ocomeni 81:637a87eb8170 155 // AT_CONNECT_RESP response state
ocomeni 103:7b566b522427 156 dbg_printf(LOG, "\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\r\n");
ocomeni 103:7b566b522427 157 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 95:290859010c8c 158 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 159 sendAtConfirmationFreeMpool(respStr);
ocomeni 99:05398b3184f8 160 char * urc = new char[20];
ocomeni 104:11e9605093c9 161 sprintf(urc, "\r\n%s:%d\r\n", NETWORK_UP_URC, WIFI_INTERFACE_ID);
ocomeni 99:05398b3184f8 162 sendAtEvent(urc);
ocomeni 126:9bc33f8b57d5 163 delete[] urc;
ocomeni 81:637a87eb8170 164 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 165 break;
ocomeni 99:05398b3184f8 166 }
ocomeni 126:9bc33f8b57d5 167 case AT_DISCONNECT_RESP:
ocomeni 126:9bc33f8b57d5 168 {
ocomeni 81:637a87eb8170 169 // AT_DISCONNECT_RESP response state
ocomeni 103:7b566b522427 170 dbg_printf(LOG, "\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\r\n");
ocomeni 95:290859010c8c 171 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 172 sendAtConfirmationFreeMpool(respStr);
ocomeni 124:eae4512b131b 173 char * urc = new char[20];
ocomeni 124:eae4512b131b 174 sprintf(urc, "\r\n%s:%d\r\n", NETWORK_DOWN_URC, WIFI_INTERFACE_ID);
ocomeni 124:eae4512b131b 175 sendAtEvent(urc);
ocomeni 126:9bc33f8b57d5 176 delete[] urc;
ocomeni 81:637a87eb8170 177 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 178 break;
ocomeni 126:9bc33f8b57d5 179 }
ocomeni 126:9bc33f8b57d5 180 case AT_CONFIG_RESP:
ocomeni 126:9bc33f8b57d5 181 {
ocomeni 82:10072c1794d3 182 // AT_CONFIG_RESP response state
ocomeni 103:7b566b522427 183 dbg_printf(LOG, "\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\r\n");
ocomeni 95:290859010c8c 184 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 84:7c7add00f4bf 185 at_resp = AT_RESP_NONE;
ocomeni 84:7c7add00f4bf 186 break;
ocomeni 126:9bc33f8b57d5 187 }
ocomeni 126:9bc33f8b57d5 188 case AT_NETWORK_STATUS_RESP:
ocomeni 126:9bc33f8b57d5 189 {
ocomeni 95:290859010c8c 190 // AT_CONFIG_RESP response state
ocomeni 103:7b566b522427 191 dbg_printf(LOG, "\n [ATCMD MAN] NETWORK STATUS RESPONSE RECEIVED!!\r\n");
ocomeni 95:290859010c8c 192 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 193 sendAtConfirmationFreeMpool(respStr);
ocomeni 95:290859010c8c 194 at_resp = AT_RESP_NONE;
ocomeni 95:290859010c8c 195 break;
ocomeni 126:9bc33f8b57d5 196 }
ocomeni 95:290859010c8c 197 case AT_WIFI_STATUS_RESP:
ocomeni 126:9bc33f8b57d5 198 {
ocomeni 95:290859010c8c 199 // AT_CONFIG_RESP response state
ocomeni 103:7b566b522427 200 dbg_printf(LOG, "\n [ATCMD MAN] WIFI RESPONSE RECEIVED!!\r\n");
ocomeni 95:290859010c8c 201 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 202 sendAtConfirmationFreeMpool(respStr);
ocomeni 95:290859010c8c 203 at_resp = AT_RESP_NONE;
ocomeni 95:290859010c8c 204 break;
ocomeni 126:9bc33f8b57d5 205 }
ocomeni 84:7c7add00f4bf 206 case AT_INTERNET_CONFIG_RESP:
ocomeni 84:7c7add00f4bf 207 // AT_CONFIG_RESP response state
ocomeni 103:7b566b522427 208 dbg_printf(LOG, "\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\r\n");
ocomeni 96:f5ed273881af 209 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 84:7c7add00f4bf 210 at_resp = AT_RESP_NONE;
ocomeni 84:7c7add00f4bf 211 break;
ocomeni 84:7c7add00f4bf 212 case AT_HTTPS_RESP:
ocomeni 84:7c7add00f4bf 213 // AT_HTTP_RESP response state
ocomeni 103:7b566b522427 214 dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\r\n");
ocomeni 87:99b37d26ff2a 215 return_response();
ocomeni 95:290859010c8c 216 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 81:637a87eb8170 217 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 218 break;
ocomeni 89:45f6db09a76d 219 case AT_HTTPS_RESP_DOWNLOAD:
ocomeni 89:45f6db09a76d 220 // AT_HTTPS_RESP_DOWNLOAD response state
ocomeni 103:7b566b522427 221 dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\r\n");
ocomeni 89:45f6db09a76d 222 return_response(true); // set download paramter to true
ocomeni 89:45f6db09a76d 223 at_resp = AT_RESP_NONE;
ocomeni 89:45f6db09a76d 224 break;
ocomeni 81:637a87eb8170 225 case AT_HTTP_RESP:
ocomeni 81:637a87eb8170 226 // AT_HTTP_RESP response state
ocomeni 103:7b566b522427 227 dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\r\n");
ocomeni 89:45f6db09a76d 228 return_response();
ocomeni 89:45f6db09a76d 229 at_resp = AT_RESP_NONE;
ocomeni 89:45f6db09a76d 230 break;
ocomeni 89:45f6db09a76d 231 case AT_HTTP_RESP_DOWNLOAD:
ocomeni 89:45f6db09a76d 232 // AT_HTTP_RESP response state
ocomeni 103:7b566b522427 233 dbg_printf(LOG, "\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\r\n");
ocomeni 89:45f6db09a76d 234 return_response(true); // set download paramter to true
ocomeni 81:637a87eb8170 235 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 236 break;
ocomeni 99:05398b3184f8 237 case CONNECT_EVENT:
ocomeni 99:05398b3184f8 238 {
ocomeni 99:05398b3184f8 239 // Connect Event state
ocomeni 103:7b566b522427 240 dbg_printf(LOG, "\n [ATCMD MAN] CONNECT EVENT RECEIVED!!\r\n");
ocomeni 99:05398b3184f8 241 sendConnectEvent(resp_data->buffer, resp_data->dataLen);
ocomeni 99:05398b3184f8 242 at_resp = AT_RESP_NONE;
ocomeni 99:05398b3184f8 243 break;
ocomeni 99:05398b3184f8 244 }
ocomeni 100:80ef4bc31b7a 245 case AT_EVENT:
ocomeni 100:80ef4bc31b7a 246 {
ocomeni 100:80ef4bc31b7a 247 // AT Event state
ocomeni 103:7b566b522427 248 dbg_printf(LOG, "\n [ATCMD MAN] AT EVENT RECEIVED!!\r\n");
ocomeni 100:80ef4bc31b7a 249 respStr = (char *) resp_data->buffer;
ocomeni 100:80ef4bc31b7a 250 sendAtEvent(respStr);
ocomeni 100:80ef4bc31b7a 251 at_resp = AT_RESP_NONE;
ocomeni 100:80ef4bc31b7a 252 break;
ocomeni 100:80ef4bc31b7a 253 }
ocomeni 104:11e9605093c9 254 case WIFI_WATCH_DOG:
ocomeni 104:11e9605093c9 255 {
ocomeni 104:11e9605093c9 256 // AT_HTTP_RESP response state
ocomeni 104:11e9605093c9 257 dbg_printf(LOG, "\n [ATCMD MAN] WIFI Watchdog message!!\r\n");
ocomeni 104:11e9605093c9 258 respStr = (char *) resp_data->buffer;
ocomeni 104:11e9605093c9 259 sendConnectIndicationFreeMpool(respStr);
ocomeni 104:11e9605093c9 260 at_resp = AT_RESP_NONE;
ocomeni 104:11e9605093c9 261 break;
ocomeni 104:11e9605093c9 262 }
ocomeni 93:06e755a80187 263 case AT_COMMAND_FAILED:
ocomeni 98:65c2333a38b6 264 {
ocomeni 93:06e755a80187 265 // AT_HTTP_RESP response state
ocomeni 103:7b566b522427 266 dbg_printf(LOG, "\n [ATCMD MAN] WIFI COMMAND FAILED!!\r\n");
ocomeni 98:65c2333a38b6 267 respStr = (char *) resp_data->buffer;
ocomeni 96:f5ed273881af 268 sendAtConfirmationFreeMpool(respStr);
ocomeni 98:65c2333a38b6 269 at_resp = AT_RESP_NONE;
ocomeni 93:06e755a80187 270 break;
ocomeni 98:65c2333a38b6 271 }
ocomeni 119:8d939a902333 272 case BLE_CONNECT_EVENT:
ocomeni 119:8d939a902333 273 {
ocomeni 119:8d939a902333 274 // AT Event state
ocomeni 119:8d939a902333 275 dbg_printf(LOG, "\n [ATCMD MAN] BLE_CONNECT_EVENT RESPONSE RECEIVED!!\r\n");
ocomeni 119:8d939a902333 276 uint8_t * respBytes = ble_resp_data->buffer;
ocomeni 119:8d939a902333 277 sendBleConnectEvent((const char *)respBytes, ble_resp_data->dataLen);
ocomeni 119:8d939a902333 278 at_resp = AT_RESP_NONE;
ocomeni 119:8d939a902333 279 break;
ocomeni 119:8d939a902333 280 }
ocomeni 119:8d939a902333 281 case AT_BLE_EVENT:
ocomeni 119:8d939a902333 282 {
ocomeni 119:8d939a902333 283 // AT Event state
ocomeni 119:8d939a902333 284 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_EVENT RESPONSE RECEIVED!!\r\n");
ocomeni 121:ac4f59839e4f 285 respStr = (char *) ble_resp_data->buffer;
ocomeni 121:ac4f59839e4f 286 sendBleAtEvent(respStr, ble_resp_data->dataLen);
ocomeni 119:8d939a902333 287 at_resp = AT_RESP_NONE;
ocomeni 119:8d939a902333 288 break;
ocomeni 119:8d939a902333 289 }
ocomeni 118:8df0e9c2ee3f 290 case AT_BLE_RESPONSE:
ocomeni 118:8df0e9c2ee3f 291 {
ocomeni 118:8df0e9c2ee3f 292 // AT Event state
ocomeni 119:8d939a902333 293 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_RESPONSE RECEIVED!!\r\n");
ocomeni 119:8d939a902333 294 respStr = (char *) ble_resp_data->buffer;
ocomeni 121:ac4f59839e4f 295 ble_resp_data->buffer[ble_resp_data->dataLen] = NULL;
ocomeni 119:8d939a902333 296 sendBleDataEvent(respStr, ble_resp_data->dataLen);
ocomeni 119:8d939a902333 297 at_resp = AT_RESP_NONE;
ocomeni 119:8d939a902333 298 break;
ocomeni 119:8d939a902333 299 }
ocomeni 119:8d939a902333 300 case AT_SOCKET_KEEP_ALIVE_OK:
ocomeni 119:8d939a902333 301 {
ocomeni 119:8d939a902333 302 // AT Event state
ocomeni 119:8d939a902333 303 dbg_printf(LOG, "\n [ATCMD MAN] AT_SOCKET_KEEP_ALIVE OK RESPONSE RECEIVED!!\r\n");
ocomeni 122:62166886db5f 304 #ifdef SEND_CLOUD_OK_MSG
ocomeni 122:62166886db5f 305 sendAtConfirmation("\r\nCLOUD CONNECTION OK\r\n");
ocomeni 122:62166886db5f 306 #endif
ocomeni 119:8d939a902333 307 at_resp = AT_RESP_NONE;
ocomeni 119:8d939a902333 308 break;
ocomeni 119:8d939a902333 309 }
ocomeni 119:8d939a902333 310 case AT_SOCKET_KEEP_ALIVE_FAILED:
ocomeni 119:8d939a902333 311 {
ocomeni 119:8d939a902333 312 // AT Event state
ocomeni 119:8d939a902333 313 dbg_printf(LOG, "\n [ATCMD MAN] AT_SOCKET_KEEP_ALIVE FAILED RESPONSE RECEIVED!!\r\n");
ocomeni 122:62166886db5f 314 #ifdef SEND_CLOUD_OK_MSG
ocomeni 119:8d939a902333 315 sendAtEvent("\r\nCLOUD CONNECTION FAILED\r\n");
ocomeni 122:62166886db5f 316 #endif
ocomeni 118:8df0e9c2ee3f 317 at_resp = AT_RESP_NONE;
ocomeni 118:8df0e9c2ee3f 318 break;
ocomeni 118:8df0e9c2ee3f 319 }
ocomeni 120:779b74689747 320 case BLE_DISCONNECT_EVENT:
ocomeni 120:779b74689747 321 {
ocomeni 123:a49e9ffbaca6 322 // BLE_DISCONNECT_EVENT state
ocomeni 120:779b74689747 323 dbg_printf(LOG, "\n [ATCMD MAN] BLE_DISCONNECT_EVENT RECEIVED!!\r\n");
ocomeni 120:779b74689747 324 sendBleDisconnectEvent();
ocomeni 120:779b74689747 325 at_resp = AT_RESP_NONE;
ocomeni 120:779b74689747 326 break;
ocomeni 120:779b74689747 327 }
ocomeni 123:a49e9ffbaca6 328 case AT_WIFI_MAC_RESP:
ocomeni 123:a49e9ffbaca6 329 // AT_WIFI_MAC_RESP response state
ocomeni 123:a49e9ffbaca6 330 dbg_printf(LOG, "\n [ATCMD MAN] AT_WIFI_MAC_RESP RESPONSE RECEIVED!!\r\n");
ocomeni 123:a49e9ffbaca6 331 respStr = (char *) resp_data->buffer;
ocomeni 123:a49e9ffbaca6 332 sendAtConfirmationFreeMpool(respStr);
ocomeni 123:a49e9ffbaca6 333 at_resp = AT_RESP_NONE;
ocomeni 123:a49e9ffbaca6 334 break;
ocomeni 123:a49e9ffbaca6 335 case AT_BLE_MAC_RESP:
ocomeni 126:9bc33f8b57d5 336 {
ocomeni 123:a49e9ffbaca6 337 // AT_BLE_MAC_RESP response state
ocomeni 123:a49e9ffbaca6 338 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_MAC_RESP RESPONSE RECEIVED!!\r\n");
ocomeni 123:a49e9ffbaca6 339 respStr = (char *) ble_resp_data->buffer;
ocomeni 123:a49e9ffbaca6 340 sendAtConfirmation(respStr);
ocomeni 123:a49e9ffbaca6 341 _ble2ATDatamPool->free(ble_resp_data);
ocomeni 123:a49e9ffbaca6 342 ble_resp_data = NULL;
ocomeni 123:a49e9ffbaca6 343 at_resp = AT_RESP_NONE;
ocomeni 123:a49e9ffbaca6 344 break;
ocomeni 126:9bc33f8b57d5 345 }
ocomeni 123:a49e9ffbaca6 346 case AT_BLE_NAME_RESP:
ocomeni 126:9bc33f8b57d5 347 {
ocomeni 123:a49e9ffbaca6 348 // AT_BLE_MAC_RESP response state
ocomeni 123:a49e9ffbaca6 349 dbg_printf(LOG, "\n [ATCMD MAN] AT_BLE_NAME_RESP RESPONSE RECEIVED!!\r\n");
ocomeni 123:a49e9ffbaca6 350 respStr = (char *) ble_resp_data->buffer;
ocomeni 123:a49e9ffbaca6 351 sendAtConfirmation(respStr);
ocomeni 123:a49e9ffbaca6 352 _ble2ATDatamPool->free(ble_resp_data);
ocomeni 123:a49e9ffbaca6 353 ble_resp_data = NULL;
ocomeni 123:a49e9ffbaca6 354 at_resp = AT_RESP_NONE;
ocomeni 123:a49e9ffbaca6 355 break;
ocomeni 126:9bc33f8b57d5 356 }
ocomeni 129:590bdc2dcf5b 357 case AT_ACCESS_TOKEN_SUCCESS:
ocomeni 129:590bdc2dcf5b 358 {
ocomeni 129:590bdc2dcf5b 359 // AT_ACCESS_TOKEN_SUCCESS response state
ocomeni 129:590bdc2dcf5b 360 dbg_printf(LOG, "\n [ATCMD MAN] AT_ACCESS_TOKEN_SUCCESS RESPONSE RECEIVED!!\r\n");
ocomeni 129:590bdc2dcf5b 361 sendAtConfirmation("\r\nAWS ACCESS TOKEN ACQUIRED \r\n");
ocomeni 129:590bdc2dcf5b 362 at_resp = AT_RESP_NONE;
ocomeni 129:590bdc2dcf5b 363 break;
ocomeni 129:590bdc2dcf5b 364 }
ocomeni 129:590bdc2dcf5b 365 case AT_ACCESS_TOKEN_FAILED:
ocomeni 129:590bdc2dcf5b 366 {
ocomeni 129:590bdc2dcf5b 367 // AT_ACCESS_TOKEN_FAILED response state
ocomeni 129:590bdc2dcf5b 368 dbg_printf(LOG, "\n [ATCMD MAN] AT_ACCESS_TOKEN_FAILED!!\r\n");
ocomeni 129:590bdc2dcf5b 369 sendAtConfirmation("\r\nAWS ACCESS TOKEN ACQUISITION FAILURE!!\r\n");
ocomeni 129:590bdc2dcf5b 370 at_resp = AT_RESP_NONE;
ocomeni 129:590bdc2dcf5b 371 break;
ocomeni 129:590bdc2dcf5b 372 }
ocomeni 100:80ef4bc31b7a 373 default:
ocomeni 126:9bc33f8b57d5 374 {
ocomeni 100:80ef4bc31b7a 375 //UNKNOWN response state
ocomeni 103:7b566b522427 376 dbg_printf(LOG, "\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n");
ocomeni 100:80ef4bc31b7a 377 sendAtConfirmationFreeMpool(ERROR_RESP); //_parser.send(OK_RESP);
ocomeni 100:80ef4bc31b7a 378 at_resp = AT_RESP_NONE;
ocomeni 100:80ef4bc31b7a 379 break;
ocomeni 126:9bc33f8b57d5 380 }
ocomeni 81:637a87eb8170 381 }
ocomeni 81:637a87eb8170 382 }
ocomeni 81:637a87eb8170 383
ocomeni 120:779b74689747 384 //
ocomeni 104:11e9605093c9 385 void ATCmdManager::updateWiFiMgrStatus()
ocomeni 104:11e9605093c9 386 {
ocomeni 104:11e9605093c9 387 static int wifiBusyMonitor = 0;
ocomeni 104:11e9605093c9 388 // if next response is the expected response,
ocomeni 104:11e9605093c9 389 // then last command complete, reset wifi status
ocomeni 104:11e9605093c9 390 // pass or fail!!
ocomeni 104:11e9605093c9 391 if(at_resp == wifiStateControl || at_resp == AT_COMMAND_FAILED)
ocomeni 104:11e9605093c9 392 {
ocomeni 104:11e9605093c9 393 wifiStateControl = AT_RESP_NONE;
ocomeni 104:11e9605093c9 394 }
ocomeni 104:11e9605093c9 395 if(at_resp == WIFI_WATCH_DOG && strstr((char *) resp_data->buffer, "WiFi Main Loop inActive") != NULL)
ocomeni 104:11e9605093c9 396 {
ocomeni 104:11e9605093c9 397 wifiBusyMonitor++;
ocomeni 104:11e9605093c9 398 if(wifiBusyMonitor >= 4)
ocomeni 104:11e9605093c9 399 {
ocomeni 104:11e9605093c9 400 wifiStateControl = AT_RESP_NONE;
ocomeni 104:11e9605093c9 401 wifiBusyMonitor = 0;
ocomeni 104:11e9605093c9 402 }
ocomeni 104:11e9605093c9 403 }
ocomeni 104:11e9605093c9 404 }
ocomeni 104:11e9605093c9 405 void ATCmdManager::sendConnectIndicationFreeMpool(const char *buf)
ocomeni 104:11e9605093c9 406 {
ocomeni 104:11e9605093c9 407 int len = strlen(buf);
ocomeni 105:e5ce023eee93 408 outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL);
ocomeni 104:11e9605093c9 409 _wiFi2ATDatamPool->free(resp_data);
ocomeni 104:11e9605093c9 410 resp_data = NULL;
ocomeni 104:11e9605093c9 411 }
ocomeni 104:11e9605093c9 412
ocomeni 96:f5ed273881af 413 void ATCmdManager::sendAtConfirmationFreeMpool(const char *buf)
ocomeni 96:f5ed273881af 414 {
ocomeni 96:f5ed273881af 415 sendAtConfirmation(buf);
ocomeni 96:f5ed273881af 416 _wiFi2ATDatamPool->free(resp_data);
ocomeni 96:f5ed273881af 417 resp_data = NULL;
ocomeni 96:f5ed273881af 418 }
ocomeni 96:f5ed273881af 419
ocomeni 96:f5ed273881af 420
ocomeni 104:11e9605093c9 421 void ATCmdManager::printBufferInHex(const uint8_t *buf, int pLen)
ocomeni 89:45f6db09a76d 422 {
ocomeni 104:11e9605093c9 423 print_debug_hex(buf, pLen);
ocomeni 89:45f6db09a76d 424 }
ocomeni 81:637a87eb8170 425
ocomeni 83:9c271a50a70b 426 bool ATCmdManager::validate(edm_header_t edm_header)
ocomeni 83:9c271a50a70b 427 {
ocomeni 83:9c271a50a70b 428 if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found!
ocomeni 99:05398b3184f8 429 if(edm_header.payloadID != CONNECT_EVENT_ID &&
ocomeni 99:05398b3184f8 430 edm_header.payloadID != DISCONNECT_EVENT_ID &&
ocomeni 99:05398b3184f8 431 edm_header.payloadID != DATA_EVENT_ID &&
ocomeni 99:05398b3184f8 432 edm_header.payloadID != DATA_COMMAND_ID &&
ocomeni 99:05398b3184f8 433 edm_header.payloadID != AT_REQUEST_ID &&
ocomeni 99:05398b3184f8 434 edm_header.payloadID != AT_CONFIRMATION_ID &&
ocomeni 99:05398b3184f8 435 edm_header.payloadID != AT_EVENT_ID
ocomeni 83:9c271a50a70b 436 )return false; // unexpected payload ID found!
ocomeni 83:9c271a50a70b 437 if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN ||
ocomeni 83:9c271a50a70b 438 edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN
ocomeni 83:9c271a50a70b 439 ) return false; // unexpected length received!
ocomeni 90:ed0267eca7b5 440 if(edm_header.channel_id != WIFI_CHANNEL &&
ocomeni 90:ed0267eca7b5 441 edm_header.channel_id != BLE_CHANNEL
ocomeni 90:ed0267eca7b5 442 ) return false; // unexpected channel_id received!
ocomeni 83:9c271a50a70b 443 return true;
ocomeni 83:9c271a50a70b 444 }
ocomeni 74:f26e846adfe9 445
ocomeni 84:7c7add00f4bf 446 http_method ATCmdManager::str2HttpMethod(const char * methodStr)
ocomeni 84:7c7add00f4bf 447 {
ocomeni 84:7c7add00f4bf 448 http_method _http_method;
ocomeni 84:7c7add00f4bf 449 if(strstr(methodStr, "POST")!= NULL){
ocomeni 84:7c7add00f4bf 450 _http_method = HTTP_POST;
ocomeni 84:7c7add00f4bf 451 }
ocomeni 84:7c7add00f4bf 452 else if(strstr(methodStr, "GET")!= NULL){
ocomeni 84:7c7add00f4bf 453 _http_method = HTTP_GET;
ocomeni 84:7c7add00f4bf 454 }
ocomeni 84:7c7add00f4bf 455 else{
ocomeni 84:7c7add00f4bf 456 _http_method = HTTP_DELETE; // unsupported method - set to HTTP_DELETE
ocomeni 84:7c7add00f4bf 457 }
ocomeni 84:7c7add00f4bf 458 return _http_method;
ocomeni 84:7c7add00f4bf 459 }
ocomeni 84:7c7add00f4bf 460 bool ATCmdManager::createHttpRequest()
ocomeni 84:7c7add00f4bf 461 {
ocomeni 110:c722dda4f2ff 462 #ifdef USE_WIFI_STATE_CONTROL
ocomeni 108:3c8fb2c6e7bf 463 static int wifiBusyTimeOut = 0;
ocomeni 108:3c8fb2c6e7bf 464 if(wifiStateControl != AT_RESP_NONE && wifiBusyTimeOut < 10) // wifi busy!
ocomeni 104:11e9605093c9 465 {
ocomeni 108:3c8fb2c6e7bf 466 wifiBusyTimeOut++;
ocomeni 108:3c8fb2c6e7bf 467 return false;
ocomeni 104:11e9605093c9 468 }
ocomeni 108:3c8fb2c6e7bf 469 wifiBusyTimeOut = 0;
ocomeni 110:c722dda4f2ff 470 #endif
ocomeni 127:a21788227ca6 471 bool full_debug_ena = (debug_flags_map & ATCMD_MGR_FULL_DEBUG_ENA) != 0x00;
ocomeni 90:ed0267eca7b5 472 http_request_t *http_req; // = new http_request_t;
ocomeni 90:ed0267eca7b5 473 wifi_data_msg_t data_msg;
ocomeni 90:ed0267eca7b5 474 http_req = (http_request_t *)data_msg.buffer;
ocomeni 84:7c7add00f4bf 475 char s1[32];
ocomeni 84:7c7add00f4bf 476 char s2[32];
ocomeni 84:7c7add00f4bf 477 char s3[32];
ocomeni 90:ed0267eca7b5 478 int len = 0;
ocomeni 90:ed0267eca7b5 479 int n;
ocomeni 90:ed0267eca7b5 480 char * strPtr = (char *)rx_buf_ptr;
ocomeni 84:7c7add00f4bf 481 char * p;
ocomeni 84:7c7add00f4bf 482 char * p2 = strstr(strPtr, "\r\n\r\n");
ocomeni 84:7c7add00f4bf 483 char * nxtPtr = strPtr;
ocomeni 90:ed0267eca7b5 484 char * outPtr;
ocomeni 84:7c7add00f4bf 485 char * bodyPtr = p2+4;
ocomeni 103:7b566b522427 486 dbg_printf(LOG, "\nstrPtr address= %x",strPtr);
ocomeni 103:7b566b522427 487 dbg_printf(LOG, "\np2 address= %x", p2);
ocomeni 129:590bdc2dcf5b 488 for(int i = 0; i < 6; i++){
ocomeni 84:7c7add00f4bf 489 if(i == 0)// firstline scan method uri and http_ver
ocomeni 84:7c7add00f4bf 490 {
ocomeni 84:7c7add00f4bf 491 n = sscanf(nxtPtr,"%s %s %s", s1, s2, s3);
ocomeni 90:ed0267eca7b5 492 if(n!=3) return false; // error in input abort
ocomeni 127:a21788227ca6 493 if(full_debug_ena)
ocomeni 127:a21788227ca6 494 {
ocomeni 127:a21788227ca6 495 dbg_printf(LOG, "\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 );
ocomeni 127:a21788227ca6 496 }
ocomeni 84:7c7add00f4bf 497 //http_req.method = str2HttpMethod(s1.c_str());
ocomeni 90:ed0267eca7b5 498 http_req->method = str2HttpMethod(s1);
ocomeni 90:ed0267eca7b5 499 http_req->request_URI = (char *) http_req->buffer; // point 1st string to start of buffer
ocomeni 90:ed0267eca7b5 500 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 501 strncpy(http_req->request_URI, s2, len);
ocomeni 90:ed0267eca7b5 502 http_req->http_version = http_req->request_URI + len; // point 2nd string to after 1st
ocomeni 90:ed0267eca7b5 503 len = strlen(s3)+1;
ocomeni 90:ed0267eca7b5 504 strncpy(http_req->http_version, s3, len);
ocomeni 127:a21788227ca6 505 if(full_debug_ena)
ocomeni 127:a21788227ca6 506 {
ocomeni 127:a21788227ca6 507 dbg_printf(LOG, "\nhttp_request 1st line:\n method = %d\nuri = %s\nhttp_ver = %s",http_req->method,
ocomeni 127:a21788227ca6 508 http_req->request_URI,
ocomeni 127:a21788227ca6 509 http_req->http_version );
ocomeni 127:a21788227ca6 510 dbg_printf(LOG, "\nhttp_request str ptrs\nuri = %X\nhttp_ver = %X",http_req->request_URI,
ocomeni 127:a21788227ca6 511 http_req->http_version );
ocomeni 127:a21788227ca6 512 }
ocomeni 90:ed0267eca7b5 513 outPtr = http_req->http_version + len; // point output buffer ptr to after 2nd string
ocomeni 84:7c7add00f4bf 514 }
ocomeni 84:7c7add00f4bf 515 else{ // scan header pairs
ocomeni 84:7c7add00f4bf 516 n = sscanf(nxtPtr,"%s %s", s1, s2);
ocomeni 90:ed0267eca7b5 517 if(n!=2) return false; // error in input abort
ocomeni 84:7c7add00f4bf 518 p = strstr(s1,":");
ocomeni 84:7c7add00f4bf 519 *p = NULL;
ocomeni 127:a21788227ca6 520 if(full_debug_ena)
ocomeni 127:a21788227ca6 521 {
ocomeni 127:a21788227ca6 522 dbg_printf(LOG, "\nname = %s value = %s",s1, s2);
ocomeni 127:a21788227ca6 523 }
ocomeni 86:04fc2fcda7ec 524 if(strstr(s1, "Host")!= NULL){
ocomeni 90:ed0267eca7b5 525 http_req->hostName = outPtr;
ocomeni 104:11e9605093c9 526 #ifdef BOX_UBLOX_DEMO_TESTING
ocomeni 104:11e9605093c9 527 p = strstr(s2, "//");
ocomeni 104:11e9605093c9 528 if(p == NULL)
ocomeni 104:11e9605093c9 529 p = s2;
ocomeni 104:11e9605093c9 530 else
ocomeni 104:11e9605093c9 531 p+=2;
ocomeni 104:11e9605093c9 532 len = strlen(p)+1;
ocomeni 104:11e9605093c9 533 strncpy(outPtr, p, len);
ocomeni 104:11e9605093c9 534 #else
ocomeni 90:ed0267eca7b5 535 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 536 strncpy(outPtr, s2, len);
ocomeni 104:11e9605093c9 537 #endif
ocomeni 127:a21788227ca6 538 if(full_debug_ena)
ocomeni 127:a21788227ca6 539 {
ocomeni 127:a21788227ca6 540 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
ocomeni 127:a21788227ca6 541 }
ocomeni 90:ed0267eca7b5 542 outPtr += len; // point output buffer ptr to after current string
ocomeni 86:04fc2fcda7ec 543 }
ocomeni 86:04fc2fcda7ec 544 else if(strstr(s1, "Accept")!= NULL){
ocomeni 90:ed0267eca7b5 545 http_req->AcceptVal = outPtr;
ocomeni 90:ed0267eca7b5 546 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 547 strncpy(outPtr, s2, len);
ocomeni 127:a21788227ca6 548 if(full_debug_ena)
ocomeni 127:a21788227ca6 549 {
ocomeni 127:a21788227ca6 550 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
ocomeni 127:a21788227ca6 551 }
ocomeni 90:ed0267eca7b5 552 outPtr += len; // point output buffer ptr to after current string
ocomeni 86:04fc2fcda7ec 553 }
ocomeni 86:04fc2fcda7ec 554 else if(strstr(s1, "Content-Type")!= NULL){
ocomeni 90:ed0267eca7b5 555 http_req->contentType = outPtr;
ocomeni 90:ed0267eca7b5 556 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 557 strncpy(outPtr, s2, len);
ocomeni 127:a21788227ca6 558 if(full_debug_ena)
ocomeni 127:a21788227ca6 559 {
ocomeni 127:a21788227ca6 560 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
ocomeni 127:a21788227ca6 561 }
ocomeni 90:ed0267eca7b5 562 outPtr += len; // point output buffer ptr to after current string
ocomeni 86:04fc2fcda7ec 563 }
ocomeni 86:04fc2fcda7ec 564 else if(strstr(s1, "Content-Length")!= NULL){
ocomeni 90:ed0267eca7b5 565 http_req->contentLen = outPtr;
ocomeni 90:ed0267eca7b5 566 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 567 strncpy(outPtr, s2, len);
ocomeni 127:a21788227ca6 568 if(full_debug_ena)
ocomeni 127:a21788227ca6 569 {
ocomeni 127:a21788227ca6 570 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
ocomeni 127:a21788227ca6 571 }
ocomeni 127:a21788227ca6 572 outPtr += len; // point output buffer ptr to after current string
ocomeni 127:a21788227ca6 573 }
ocomeni 127:a21788227ca6 574 else if(strstr(s1, "X-Amz-Target")!= NULL){
ocomeni 127:a21788227ca6 575 http_req->X_Amz_Target = outPtr;
ocomeni 127:a21788227ca6 576 len = strlen(s2)+1;
ocomeni 127:a21788227ca6 577 strncpy(outPtr, s2, len);
ocomeni 127:a21788227ca6 578 if(full_debug_ena)
ocomeni 127:a21788227ca6 579 {
ocomeni 127:a21788227ca6 580 dbg_printf(LOG, "\nname = %s value = %s",s1, outPtr);
ocomeni 127:a21788227ca6 581 }
ocomeni 90:ed0267eca7b5 582 outPtr += len; // point output buffer ptr to after current string
ocomeni 84:7c7add00f4bf 583 }
ocomeni 84:7c7add00f4bf 584
ocomeni 127:a21788227ca6 585
ocomeni 127:a21788227ca6 586
ocomeni 127:a21788227ca6 587 if(full_debug_ena)
ocomeni 127:a21788227ca6 588 {
ocomeni 127:a21788227ca6 589 dbg_printf(LOG, "\noutPtr = %X len = %d\n", outPtr, len);
ocomeni 127:a21788227ca6 590 }
ocomeni 84:7c7add00f4bf 591 }
ocomeni 84:7c7add00f4bf 592 nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line
ocomeni 84:7c7add00f4bf 593 if(nxtPtr >= p2) break;
ocomeni 84:7c7add00f4bf 594 }
ocomeni 90:ed0267eca7b5 595 // print header from http_req_struct
ocomeni 127:a21788227ca6 596 if(full_debug_ena)
ocomeni 127:a21788227ca6 597 {
ocomeni 127:a21788227ca6 598 dbg_printf(LOG, "\nhttp request header: \n %s\n", http_req->buffer);
ocomeni 127:a21788227ca6 599 }
ocomeni 84:7c7add00f4bf 600 int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr);
ocomeni 127:a21788227ca6 601 if(full_debug_ena)
ocomeni 127:a21788227ca6 602 {
ocomeni 127:a21788227ca6 603 dbg_printf(LOG, "\nLen = %d\n", bodyLen);
ocomeni 127:a21788227ca6 604 }
ocomeni 127:a21788227ca6 605
ocomeni 118:8df0e9c2ee3f 606 uint32_t msecsSinceLastMsg = Kernel::get_ms_count() - lastHttpRespTime;
ocomeni 118:8df0e9c2ee3f 607 if((lastCloudMsgType == BAR_DB_MSGTYPE || lastCloudMsgType == REC_DB_MSGTYPE) &&
ocomeni 118:8df0e9c2ee3f 608 lastCloudMsgType == (uint8_t) bodyPtr[8] &&
ocomeni 118:8df0e9c2ee3f 609 (msecsSinceLastMsg < CLOUD_RETRY_TIME_MS ))
ocomeni 118:8df0e9c2ee3f 610 {
ocomeni 118:8df0e9c2ee3f 611 return false;
ocomeni 118:8df0e9c2ee3f 612 }
ocomeni 118:8df0e9c2ee3f 613 lastCloudMsgType = (uint8_t) bodyPtr[8]; // pick out the MSGTYPE byte from payload
ocomeni 90:ed0267eca7b5 614 http_req->body = (uint8_t *) outPtr;
ocomeni 90:ed0267eca7b5 615 memcpy(outPtr, bodyPtr, bodyLen);
ocomeni 90:ed0267eca7b5 616 if(bodyLen > 10){
ocomeni 103:7b566b522427 617 dbg_printf(LOG, "\n Message Body:\n");
ocomeni 90:ed0267eca7b5 618 printBufferInHex(http_req->body, bodyLen);
ocomeni 90:ed0267eca7b5 619 }
ocomeni 90:ed0267eca7b5 620 outPtr += bodyLen; // move output pointer to end output (header + body)
ocomeni 84:7c7add00f4bf 621 // package and send on wifi data queue
ocomeni 84:7c7add00f4bf 622 data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ;
ocomeni 105:e5ce023eee93 623 data_msg.dataLen = (uint8_t *)outPtr - http_req->buffer;//sizeof(http_request_t);
ocomeni 127:a21788227ca6 624 if(full_debug_ena)
ocomeni 127:a21788227ca6 625 {
ocomeni 127:a21788227ca6 626 dbg_printf(LOG, "\nsizeof(http_req) on population = %d\n", sizeof(*http_req));
ocomeni 127:a21788227ca6 627 dbg_printf(LOG, "\ndata_msg.dataLen = %d\n", data_msg.dataLen);
ocomeni 127:a21788227ca6 628 }
ocomeni 84:7c7add00f4bf 629 // queue next data request
ocomeni 104:11e9605093c9 630 if(queueWiFiDataRequest(data_msg) == true)
ocomeni 104:11e9605093c9 631 {
ocomeni 104:11e9605093c9 632 wifiStateControl = AT_HTTPS_RESP_DOWNLOAD;
ocomeni 110:c722dda4f2ff 633 return true;
ocomeni 104:11e9605093c9 634 }
ocomeni 118:8df0e9c2ee3f 635 return false;
ocomeni 84:7c7add00f4bf 636 }
ocomeni 126:9bc33f8b57d5 637
ocomeni 126:9bc33f8b57d5 638 int ATCmdManager::readEDMmodeBytes(uint8_t *rx_buf_ptr, int pLen){
ocomeni 126:9bc33f8b57d5 639 int n, m=0,sz=0;
ocomeni 126:9bc33f8b57d5 640 do{
ocomeni 126:9bc33f8b57d5 641 sz = (pLen-m);
ocomeni 126:9bc33f8b57d5 642 if(sz > 224)
ocomeni 126:9bc33f8b57d5 643 {
ocomeni 126:9bc33f8b57d5 644 sz = 224;
ocomeni 126:9bc33f8b57d5 645 }
ocomeni 126:9bc33f8b57d5 646 n = _parser.read((char *)rx_buf_ptr, sz);
ocomeni 126:9bc33f8b57d5 647 if(n == -1)
ocomeni 126:9bc33f8b57d5 648 {
ocomeni 126:9bc33f8b57d5 649 dbg_printf(LOG, "Timeout while reading message payload bytes - expected %d but %d read so far!\r\n", pLen, m);
ocomeni 126:9bc33f8b57d5 650 //free(rx_buf_ptr); // make sure to free buffer
ocomeni 126:9bc33f8b57d5 651 //rx_buf_ptr = NULL;
ocomeni 126:9bc33f8b57d5 652 //sendAtConfirmation(UART_TIMEOUT_ERROR);
ocomeni 126:9bc33f8b57d5 653 return n;
ocomeni 126:9bc33f8b57d5 654 }
ocomeni 126:9bc33f8b57d5 655 m+=n;
ocomeni 126:9bc33f8b57d5 656 }while(m < pLen);
ocomeni 126:9bc33f8b57d5 657 return n;
ocomeni 126:9bc33f8b57d5 658 }
ocomeni 126:9bc33f8b57d5 659
ocomeni 74:f26e846adfe9 660 // OOB processing
ocomeni 74:f26e846adfe9 661 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
ocomeni 74:f26e846adfe9 662 set_timeout(timeout);
ocomeni 84:7c7add00f4bf 663 int start;
ocomeni 83:9c271a50a70b 664 if(dataMode == AT_EXT_DATA_MODE)
ocomeni 83:9c271a50a70b 665 {
ocomeni 83:9c271a50a70b 666 int n;
ocomeni 84:7c7add00f4bf 667 uint8_t edm[EDM_HDR_LEN];
ocomeni 92:ec9550034276 668 char cmdType[16];
ocomeni 83:9c271a50a70b 669 // Poll for edm packets
ocomeni 83:9c271a50a70b 670 do{
ocomeni 92:ec9550034276 671 n = _parser.read((char *)edm, EDM_HDR_LEN-1);
ocomeni 84:7c7add00f4bf 672 edm_hdr.startByte = edm[0];
ocomeni 84:7c7add00f4bf 673 edm_hdr.payloadLen = edm[1]*256 + edm[2];
ocomeni 84:7c7add00f4bf 674 edm_hdr.payloadID = edm[3]*256 + edm[4];
ocomeni 92:ec9550034276 675 edm_hdr.channel_id = (channel_id_t) 0;
ocomeni 84:7c7add00f4bf 676 start = Kernel::get_ms_count();
ocomeni 83:9c271a50a70b 677 if(n == -1) break; // break if it times out
ocomeni 92:ec9550034276 678 int pT;
ocomeni 99:05398b3184f8 679 if(edm_hdr.payloadID < AT_REQUEST_ID) //
ocomeni 92:ec9550034276 680 {
ocomeni 92:ec9550034276 681 n += _parser.read((char *)edm, 1);
ocomeni 92:ec9550034276 682 edm_hdr.channel_id = (channel_id_t) edm[0];
ocomeni 92:ec9550034276 683 pT = 0;
ocomeni 92:ec9550034276 684 strcpy(cmdType, "DATA COMMAND");
ocomeni 92:ec9550034276 685 }
ocomeni 92:ec9550034276 686 else
ocomeni 92:ec9550034276 687 {
ocomeni 92:ec9550034276 688 pT = 1;
ocomeni 92:ec9550034276 689 strcpy(cmdType, "AT REQUEST");
ocomeni 92:ec9550034276 690 }
ocomeni 103:7b566b522427 691 dbg_printf(LOG, "%d bytes read! : CMD type = %s\n", n, cmdType);
ocomeni 92:ec9550034276 692 if(n==(EDM_HDR_LEN-pT))
ocomeni 103:7b566b522427 693 dbg_printf(LOG, "Start = %d, payloadID = %d len = %d chan_id = %d\n", edm_hdr.startByte,
ocomeni 90:ed0267eca7b5 694 edm_hdr.payloadID,
ocomeni 90:ed0267eca7b5 695 edm_hdr.payloadLen,
ocomeni 90:ed0267eca7b5 696 edm_hdr.channel_id);
ocomeni 92:ec9550034276 697 if(n == (EDM_HDR_LEN-pT) && validate(edm_hdr)) // if AT command use process oob to decode
ocomeni 83:9c271a50a70b 698 {
ocomeni 99:05398b3184f8 699 if(edm_hdr.payloadID == AT_REQUEST_ID)
ocomeni 83:9c271a50a70b 700 {
ocomeni 92:ec9550034276 701 while (_parser.process_oob() && all) {
ocomeni 92:ec9550034276 702 }
ocomeni 83:9c271a50a70b 703 break;
ocomeni 83:9c271a50a70b 704 }
ocomeni 83:9c271a50a70b 705 else
ocomeni 83:9c271a50a70b 706 {
ocomeni 92:ec9550034276 707 int pLen = edm_hdr.payloadLen-2-pT;
ocomeni 126:9bc33f8b57d5 708 rx_buf_ptr = (uint8_t *) malloc(pLen); // we already read 2 bytes from payload but expect 1 stop byte
ocomeni 126:9bc33f8b57d5 709 if(rx_buf_ptr == NULL)
ocomeni 126:9bc33f8b57d5 710 {
ocomeni 126:9bc33f8b57d5 711 dbg_printf(LOG, "Memory allocation failed!\r\n");
ocomeni 126:9bc33f8b57d5 712 free(rx_buf_ptr); // make sure to free buffer
ocomeni 126:9bc33f8b57d5 713 rx_buf_ptr = NULL;
ocomeni 126:9bc33f8b57d5 714 sendAtConfirmation(MEMORY_ALLOCATION_ERROR);
ocomeni 126:9bc33f8b57d5 715 break; // timeout!
ocomeni 126:9bc33f8b57d5 716 }
ocomeni 83:9c271a50a70b 717 rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected
ocomeni 126:9bc33f8b57d5 718 // set timeout to 100 ms for large packet
ocomeni 126:9bc33f8b57d5 719 set_timeout(100);
ocomeni 83:9c271a50a70b 720 n = _parser.read((char *)rx_buf_ptr, pLen);
ocomeni 126:9bc33f8b57d5 721 //n = readEDMmodeBytes(rx_buf_ptr, pLen);
ocomeni 126:9bc33f8b57d5 722 // reset timeout
ocomeni 126:9bc33f8b57d5 723 set_timeout(timeout);
ocomeni 118:8df0e9c2ee3f 724 if(n == -1)
ocomeni 118:8df0e9c2ee3f 725 {
ocomeni 126:9bc33f8b57d5 726 //dbg_printf(LOG, "Timeout while reading message payload bytes - expected %d!\r\n", pLen);
ocomeni 118:8df0e9c2ee3f 727 free(rx_buf_ptr); // make sure to free buffer
ocomeni 118:8df0e9c2ee3f 728 rx_buf_ptr = NULL;
ocomeni 119:8d939a902333 729 sendAtConfirmation(UART_TIMEOUT_ERROR);
ocomeni 118:8df0e9c2ee3f 730 break; // timeout!
ocomeni 118:8df0e9c2ee3f 731 }
ocomeni 103:7b566b522427 732 dbg_printf(LOG, "%d bytes read - expected %d!\n", n, pLen);
ocomeni 89:45f6db09a76d 733 printBufferInHex(rx_buf_ptr, pLen);
ocomeni 103:7b566b522427 734 dbg_printf(LOG, "rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
ocomeni 83:9c271a50a70b 735 if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
ocomeni 126:9bc33f8b57d5 736 sendAtConfirmation("ERR");
ocomeni 97:0aa46e5ef15d 737 free(rx_buf_ptr); // make sure to free buffer
ocomeni 97:0aa46e5ef15d 738 rx_buf_ptr = NULL;
ocomeni 90:ed0267eca7b5 739 break; // exit if stop byte not found - possible data corruption!
ocomeni 83:9c271a50a70b 740 }
ocomeni 83:9c271a50a70b 741 switch(edm_hdr.payloadID)
ocomeni 83:9c271a50a70b 742 {
ocomeni 99:05398b3184f8 743 case CONNECT_EVENT_ID:
ocomeni 103:7b566b522427 744 dbg_printf(LOG, "Connection Event received!\n");
ocomeni 83:9c271a50a70b 745 break;
ocomeni 99:05398b3184f8 746 case DISCONNECT_EVENT_ID:
ocomeni 103:7b566b522427 747 dbg_printf(LOG, "DISCONNECT_EVENT received!\n");
ocomeni 83:9c271a50a70b 748 break;
ocomeni 99:05398b3184f8 749 case DATA_EVENT_ID:
ocomeni 103:7b566b522427 750 dbg_printf(LOG, "DATA_EVENT received!\n");
ocomeni 83:9c271a50a70b 751 break;
ocomeni 99:05398b3184f8 752 case DATA_COMMAND_ID:
ocomeni 98:65c2333a38b6 753 {
ocomeni 103:7b566b522427 754 dbg_printf(LOG, "DATA_COMMAND received!\n");
ocomeni 108:3c8fb2c6e7bf 755 if(createHttpRequest() == false)
ocomeni 108:3c8fb2c6e7bf 756 {
ocomeni 109:c274780ff609 757 #ifdef SEND_DEBUG_MESSAGES
ocomeni 104:11e9605093c9 758 sendAtConfirmation(WIFI_BUSY_RESP);
ocomeni 109:c274780ff609 759 #endif
ocomeni 84:7c7add00f4bf 760 }
ocomeni 92:ec9550034276 761 free(rx_buf_ptr);
ocomeni 96:f5ed273881af 762 rx_buf_ptr = NULL;
ocomeni 84:7c7add00f4bf 763 int stop = Kernel::get_ms_count();
ocomeni 103:7b566b522427 764 dbg_printf(LOG, "\n Time Elapsed = %d\n", stop-start);
ocomeni 83:9c271a50a70b 765 break;
ocomeni 98:65c2333a38b6 766 }
ocomeni 99:05398b3184f8 767 case AT_REQUEST_ID:
ocomeni 103:7b566b522427 768 dbg_printf(LOG, "AT_REQUEST received!\n");
ocomeni 83:9c271a50a70b 769 break;
ocomeni 99:05398b3184f8 770 case AT_CONFIRMATION_ID:
ocomeni 103:7b566b522427 771 dbg_printf(LOG, "AT_CONFIRMATION received!\n");
ocomeni 83:9c271a50a70b 772 break;
ocomeni 99:05398b3184f8 773 case AT_EVENT_ID:
ocomeni 103:7b566b522427 774 dbg_printf(LOG, "AT_EVENT received!\n");
ocomeni 83:9c271a50a70b 775 break;
ocomeni 83:9c271a50a70b 776 default:
ocomeni 103:7b566b522427 777 dbg_printf(LOG, "UNKNOWN MESSAGE received!\n");
ocomeni 83:9c271a50a70b 778 break;
ocomeni 83:9c271a50a70b 779 }
ocomeni 83:9c271a50a70b 780 }
ocomeni 83:9c271a50a70b 781 }
ocomeni 83:9c271a50a70b 782 else // incorrect # of bytes received abort!!
ocomeni 83:9c271a50a70b 783 {
ocomeni 83:9c271a50a70b 784 break;
ocomeni 83:9c271a50a70b 785 }
ocomeni 83:9c271a50a70b 786 }while (all); // continue to process until timeout
ocomeni 74:f26e846adfe9 787 }
ocomeni 83:9c271a50a70b 788 else
ocomeni 83:9c271a50a70b 789 {
ocomeni 83:9c271a50a70b 790 // Poll for inbound packets
ocomeni 112:a0999ea4ece0 791 #ifdef BOX_UBLOX_DEMO_TESTING
ocomeni 103:7b566b522427 792 static char * ATCMD = new char[32];
ocomeni 103:7b566b522427 793 int n = 0;
ocomeni 103:7b566b522427 794 if(check_for_at_cmd)
ocomeni 103:7b566b522427 795 {
ocomeni 103:7b566b522427 796 while (n>=0 && all) {
ocomeni 103:7b566b522427 797 n = readAtCommandString(ATCMD, 32);
ocomeni 103:7b566b522427 798 if(strstr(ATCMD,"AT\r") != NULL)
ocomeni 103:7b566b522427 799 {
ocomeni 103:7b566b522427 800 _oob_ok_hdlr();
ocomeni 103:7b566b522427 801 check_for_at_cmd = false;
ocomeni 103:7b566b522427 802 break;
ocomeni 103:7b566b522427 803 }
ocomeni 103:7b566b522427 804 }
ocomeni 103:7b566b522427 805 }
ocomeni 103:7b566b522427 806 else
ocomeni 103:7b566b522427 807 #endif
ocomeni 103:7b566b522427 808 {
ocomeni 83:9c271a50a70b 809 while (_parser.process_oob() && all) {
ocomeni 110:c722dda4f2ff 810 }
ocomeni 103:7b566b522427 811 }
ocomeni 83:9c271a50a70b 812 }
ocomeni 74:f26e846adfe9 813 set_timeout();
ocomeni 74:f26e846adfe9 814 }
ocomeni 74:f26e846adfe9 815
ocomeni 74:f26e846adfe9 816
ocomeni 103:7b566b522427 817 int ATCmdManager::readAtCommandString(char *strbuf, size_t bufLen)
ocomeni 103:7b566b522427 818 {
ocomeni 103:7b566b522427 819 int n;
ocomeni 103:7b566b522427 820 bool atstr_found = false;
ocomeni 103:7b566b522427 821 int i = 0;
ocomeni 103:7b566b522427 822 while(n != -1)
ocomeni 103:7b566b522427 823 {
ocomeni 103:7b566b522427 824 char c;
ocomeni 103:7b566b522427 825 n = _parser.read(&c, 1);
ocomeni 103:7b566b522427 826 if(n)
ocomeni 103:7b566b522427 827 strbuf[i++] = c;
ocomeni 103:7b566b522427 828 if(c=='\r')
ocomeni 103:7b566b522427 829 {
ocomeni 103:7b566b522427 830 strbuf[i++] = NULL; // terminate string and exit
ocomeni 103:7b566b522427 831 atstr_found = true;
ocomeni 103:7b566b522427 832 break;
ocomeni 103:7b566b522427 833 }
ocomeni 103:7b566b522427 834 }
ocomeni 103:7b566b522427 835 if(atstr_found)
ocomeni 103:7b566b522427 836 {
ocomeni 103:7b566b522427 837 return i;
ocomeni 103:7b566b522427 838 }
ocomeni 103:7b566b522427 839 else
ocomeni 103:7b566b522427 840 {
ocomeni 103:7b566b522427 841 return -1;
ocomeni 103:7b566b522427 842 }
ocomeni 103:7b566b522427 843 }
ocomeni 103:7b566b522427 844
ocomeni 74:f26e846adfe9 845 // OOB message handlers
ocomeni 74:f26e846adfe9 846 void ATCmdManager::_oob_startup_hdlr(){
ocomeni 74:f26e846adfe9 847 }
ocomeni 74:f26e846adfe9 848
ocomeni 81:637a87eb8170 849 void ATCmdManager::_oob_ok_hdlr(){
ocomeni 95:290859010c8c 850 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 81:637a87eb8170 851 }
ocomeni 81:637a87eb8170 852
ocomeni 74:f26e846adfe9 853
ocomeni 74:f26e846adfe9 854 void ATCmdManager::_oob_bleRole_hdlr(){
ocomeni 74:f26e846adfe9 855 }
ocomeni 74:f26e846adfe9 856
ocomeni 74:f26e846adfe9 857
ocomeni 74:f26e846adfe9 858 void ATCmdManager::_oob_wifiMode_err(){
ocomeni 74:f26e846adfe9 859 }
ocomeni 74:f26e846adfe9 860
ocomeni 74:f26e846adfe9 861
ocomeni 74:f26e846adfe9 862 void ATCmdManager::_oob_conn_already(){
ocomeni 74:f26e846adfe9 863 }
ocomeni 74:f26e846adfe9 864
ocomeni 114:b11bb96c09f3 865 void ATCmdManager::_oob_enable_wifi()
ocomeni 114:b11bb96c09f3 866 {
ocomeni 114:b11bb96c09f3 867 trigger_start_WiFi();
ocomeni 114:b11bb96c09f3 868 sendAtConfirmation(OK_RESP);
ocomeni 114:b11bb96c09f3 869 }
ocomeni 74:f26e846adfe9 870
ocomeni 74:f26e846adfe9 871 void ATCmdManager::_oob_err(){
ocomeni 74:f26e846adfe9 872 }
ocomeni 74:f26e846adfe9 873
ocomeni 79:a2187bbfa407 874 void ATCmdManager::_oob_get_fw_ver()
ocomeni 79:a2187bbfa407 875 {
ocomeni 102:9748f290a1a5 876 #ifdef MBED_MAJOR_VERSION
ocomeni 123:a49e9ffbaca6 877 char * fw_ver_str = new char[40];
ocomeni 124:eae4512b131b 878 sprintf(fw_ver_str, "API version:%d.%d.%d.%d.%d.%dOK\r\n", MBED_MAJOR_VERSION
ocomeni 123:a49e9ffbaca6 879 , MBED_MINOR_VERSION
ocomeni 123:a49e9ffbaca6 880 , MBED_PATCH_VERSION
ocomeni 123:a49e9ffbaca6 881 , API_MAJOR_VERSION
ocomeni 123:a49e9ffbaca6 882 , API_MINOR_VERSION
ocomeni 123:a49e9ffbaca6 883 , API_BUILD_NUMBER);
ocomeni 102:9748f290a1a5 884 sendAtConfirmation(fw_ver_str);
ocomeni 102:9748f290a1a5 885 delete fw_ver_str;
ocomeni 102:9748f290a1a5 886 #endif
ocomeni 79:a2187bbfa407 887 }
ocomeni 79:a2187bbfa407 888
ocomeni 127:a21788227ca6 889
ocomeni 127:a21788227ca6 890 void ATCmdManager::_oob_set_debug_flag()
ocomeni 127:a21788227ca6 891 {
ocomeni 127:a21788227ca6 892 int rdFlag;
ocomeni 127:a21788227ca6 893 if(_parser.scanf("=%3d", &rdFlag) >0)
ocomeni 127:a21788227ca6 894 {
ocomeni 127:a21788227ca6 895 debug_flags_map = rdFlag;
ocomeni 127:a21788227ca6 896 dbg_printf(LOG, "\n Changing debug_flags_map to %d %d\n", rdFlag, debug_flags_map);
ocomeni 127:a21788227ca6 897 sendAtConfirmation(OK_RESP);
ocomeni 127:a21788227ca6 898 }
ocomeni 127:a21788227ca6 899 }
ocomeni 103:7b566b522427 900 #define TTP_DEBUGGING
ocomeni 107:f1a83fd41b17 901 #define TTP_DEBUGGING_UART_NOCHANGE
ocomeni 75:08eff6258e1b 902 void ATCmdManager::_oob_uart_setup(){
ocomeni 75:08eff6258e1b 903 int uOpts[NUM_UART_OPTIONS];
ocomeni 75:08eff6258e1b 904 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 905 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 906 dbg_printf(LOG, "\n Changing Baud Rate to %d\n", uOpts[0]);
ocomeni 107:f1a83fd41b17 907 #ifdef TTP_DEBUGGING_UART_NOCHANGE
ocomeni 103:7b566b522427 908 sendAtConfirmation(OK_RESP);
ocomeni 103:7b566b522427 909 #else
ocomeni 81:637a87eb8170 910
ocomeni 95:290859010c8c 911 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 81:637a87eb8170 912 _serial.set_baud(uOpts[0]);
ocomeni 92:ec9550034276 913 wait(0.1);
ocomeni 103:7b566b522427 914 dbg_printf(LOG, "\n Baud Rate now %d\n", uOpts[0]);
ocomeni 103:7b566b522427 915 #endif
ocomeni 103:7b566b522427 916 #ifdef BOX_UBLOX_DEMO_TESTING
ocomeni 103:7b566b522427 917 check_for_at_cmd = true;
ocomeni 103:7b566b522427 918 #endif
ocomeni 81:637a87eb8170 919
ocomeni 75:08eff6258e1b 920 } else {
ocomeni 103:7b566b522427 921 dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 922 }
ocomeni 75:08eff6258e1b 923 }
ocomeni 75:08eff6258e1b 924
ocomeni 74:f26e846adfe9 925 void ATCmdManager::set_timeout(uint32_t timeout_ms)
ocomeni 74:f26e846adfe9 926 {
ocomeni 74:f26e846adfe9 927 _parser.set_timeout(timeout_ms);
ocomeni 74:f26e846adfe9 928 }
ocomeni 74:f26e846adfe9 929
ocomeni 75:08eff6258e1b 930
ocomeni 75:08eff6258e1b 931 void ATCmdManager::_oob_echo_off()
ocomeni 75:08eff6258e1b 932 {
ocomeni 103:7b566b522427 933 dbg_printf(LOG, "\n Received ATEO OOB command!!\n");
ocomeni 103:7b566b522427 934 dbg_printf(LOG, "\n turning echo OFF!!\n");
ocomeni 75:08eff6258e1b 935 _parser.debug_on(false);
ocomeni 95:290859010c8c 936 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 75:08eff6258e1b 937 }
ocomeni 75:08eff6258e1b 938
ocomeni 75:08eff6258e1b 939
ocomeni 75:08eff6258e1b 940 void ATCmdManager::_oob_echo_on()
ocomeni 75:08eff6258e1b 941 {
ocomeni 103:7b566b522427 942 dbg_printf(LOG, "\n Received ATE1 OOB command!!\n");
ocomeni 103:7b566b522427 943 dbg_printf(LOG, "\n turning echo ON!!\n");
ocomeni 75:08eff6258e1b 944 _parser.debug_on(true);
ocomeni 95:290859010c8c 945 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 75:08eff6258e1b 946 }
ocomeni 75:08eff6258e1b 947
ocomeni 75:08eff6258e1b 948
ocomeni 118:8df0e9c2ee3f 949
ocomeni 118:8df0e9c2ee3f 950 void ATCmdManager::_oob_debug_logs_on()
ocomeni 118:8df0e9c2ee3f 951 {
ocomeni 118:8df0e9c2ee3f 952 initialise_debug(LOG | ERR | TXT | DBG);
ocomeni 118:8df0e9c2ee3f 953 dbg_printf(LOG, "\n Received ATE2 OOB command!!\n");
ocomeni 118:8df0e9c2ee3f 954 dbg_printf(LOG, "\n debug logs ON!!\n");
ocomeni 118:8df0e9c2ee3f 955 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 118:8df0e9c2ee3f 956 }
ocomeni 118:8df0e9c2ee3f 957
ocomeni 118:8df0e9c2ee3f 958 void ATCmdManager::_oob_debug_logs_off()
ocomeni 118:8df0e9c2ee3f 959 {
ocomeni 118:8df0e9c2ee3f 960 dbg_printf(LOG, "\n Received ATE3 OOB command!!\n");
ocomeni 118:8df0e9c2ee3f 961 dbg_printf(LOG, "\n turning debug logs OFF!!\n");
ocomeni 118:8df0e9c2ee3f 962 initialise_debug(NONE);
ocomeni 118:8df0e9c2ee3f 963 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 118:8df0e9c2ee3f 964 }
ocomeni 118:8df0e9c2ee3f 965
ocomeni 75:08eff6258e1b 966 void ATCmdManager::_oob_data_mode(){
ocomeni 103:7b566b522427 967 dbg_printf(LOG, "\n Received EDM mode command!!\n");
ocomeni 103:7b566b522427 968 int dmode;
ocomeni 103:7b566b522427 969 if(_parser.scanf("%d", &dmode) >0) {
ocomeni 103:7b566b522427 970 dbg_printf(LOG, "\nATCmdParser: Data mode=%d\r\n", dataMode);
ocomeni 96:f5ed273881af 971 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 103:7b566b522427 972 switch(dmode)
ocomeni 75:08eff6258e1b 973 {
ocomeni 75:08eff6258e1b 974 case 0:
ocomeni 103:7b566b522427 975 dbg_printf(LOG, "\nATCmdParser: Command Mode request received\n");
ocomeni 81:637a87eb8170 976 dataMode = AT_CMD_DATA_MODE;
ocomeni 75:08eff6258e1b 977 break;
ocomeni 75:08eff6258e1b 978 case 1:
ocomeni 103:7b566b522427 979 dbg_printf(LOG, "\nATCmdParser: Data Mode request received\n");
ocomeni 81:637a87eb8170 980 dataMode = AT_STD_DATA_MODE;
ocomeni 75:08eff6258e1b 981 break;
ocomeni 75:08eff6258e1b 982 case 2:
ocomeni 103:7b566b522427 983 dbg_printf(LOG, "\nATCmdParser: Extended data Mode request received\n");
ocomeni 81:637a87eb8170 984 dataMode = AT_EXT_DATA_MODE;
ocomeni 98:65c2333a38b6 985 _event_queue.call_in(10, &print_heap_and_isr_stack_info);
ocomeni 98:65c2333a38b6 986 //print_heap_and_isr_stack_info();
ocomeni 75:08eff6258e1b 987 break;
ocomeni 75:08eff6258e1b 988 default:
ocomeni 103:7b566b522427 989 dbg_printf(LOG, "\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 990 break;
ocomeni 75:08eff6258e1b 991 }
ocomeni 75:08eff6258e1b 992 } else {
ocomeni 103:7b566b522427 993 dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed\n");
ocomeni 75:08eff6258e1b 994 }
ocomeni 75:08eff6258e1b 995 }
ocomeni 75:08eff6258e1b 996
ocomeni 75:08eff6258e1b 997 void ATCmdManager::_oob_get_mac_addr(){
ocomeni 75:08eff6258e1b 998 int bleOrWifi;
ocomeni 75:08eff6258e1b 999 if(_parser.scanf("=%d", &bleOrWifi) >0) {
ocomeni 75:08eff6258e1b 1000 switch(bleOrWifi)
ocomeni 75:08eff6258e1b 1001 {
ocomeni 75:08eff6258e1b 1002 case 1:
ocomeni 126:9bc33f8b57d5 1003 {
ocomeni 103:7b566b522427 1004 dbg_printf(LOG, "\nATCmdParser: BLE MAC Address request received");
ocomeni 123:a49e9ffbaca6 1005 at_ble_msg_t data_req;
ocomeni 123:a49e9ffbaca6 1006 data_req.ble_cmd = BLE_CMD_MAC_ADDR;
ocomeni 123:a49e9ffbaca6 1007 // queue next BLE command
ocomeni 123:a49e9ffbaca6 1008 queueBleDataRequest(data_req);
ocomeni 75:08eff6258e1b 1009 break;
ocomeni 126:9bc33f8b57d5 1010 }
ocomeni 75:08eff6258e1b 1011 case 2:
ocomeni 126:9bc33f8b57d5 1012 {
ocomeni 123:a49e9ffbaca6 1013 dbg_printf(LOG, "\nATCmdParser: WiFi MAC Address request received");
ocomeni 123:a49e9ffbaca6 1014 wifi_cmd_t cmd = WIFI_CMD_WIFI_MAC_ADDR;
ocomeni 123:a49e9ffbaca6 1015 // queue next command
ocomeni 123:a49e9ffbaca6 1016 queueWiFiCommand(cmd);
ocomeni 75:08eff6258e1b 1017 break;
ocomeni 126:9bc33f8b57d5 1018 }
ocomeni 75:08eff6258e1b 1019 default:
ocomeni 126:9bc33f8b57d5 1020 {
ocomeni 103:7b566b522427 1021 dbg_printf(LOG, "\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 1022 break;
ocomeni 126:9bc33f8b57d5 1023 }
ocomeni 75:08eff6258e1b 1024 }
ocomeni 75:08eff6258e1b 1025 } else {
ocomeni 103:7b566b522427 1026 dbg_printf(LOG, "\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 1027 }
ocomeni 95:290859010c8c 1028 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 75:08eff6258e1b 1029 }
ocomeni 75:08eff6258e1b 1030
ocomeni 123:a49e9ffbaca6 1031
ocomeni 123:a49e9ffbaca6 1032 void ATCmdManager::_oob_get_ble_name()
ocomeni 123:a49e9ffbaca6 1033 {
ocomeni 123:a49e9ffbaca6 1034 dbg_printf(LOG, "\nATCmdParser: BLE CMD DEVICE_NAME request received");
ocomeni 123:a49e9ffbaca6 1035 at_ble_msg_t data_req;
ocomeni 123:a49e9ffbaca6 1036 data_req.ble_cmd = BLE_CMD_DEVICE_NAME;
ocomeni 123:a49e9ffbaca6 1037 // queue next BLE command
ocomeni 123:a49e9ffbaca6 1038 queueBleDataRequest(data_req);
ocomeni 123:a49e9ffbaca6 1039 }
ocomeni 123:a49e9ffbaca6 1040
ocomeni 75:08eff6258e1b 1041 void ATCmdManager::_oob_get_ble_role(){
ocomeni 118:8df0e9c2ee3f 1042 trigger_start_BLE();
ocomeni 103:7b566b522427 1043 dbg_printf(LOG, "\n Received get BLE role command!!\n");
ocomeni 121:ac4f59839e4f 1044 if(startup_config->ble_enable)
ocomeni 121:ac4f59839e4f 1045 {
ocomeni 121:ac4f59839e4f 1046 sendAtConfirmation("+UBTLE:2\r\nOK\r\n"); //_parser.send(OK_RESP);
ocomeni 121:ac4f59839e4f 1047 }
ocomeni 121:ac4f59839e4f 1048 else // disabled
ocomeni 121:ac4f59839e4f 1049 {
ocomeni 121:ac4f59839e4f 1050 sendAtConfirmation("+UBTLE:0\r\nOK\r\n"); //_parser.send(OK_RESP);
ocomeni 121:ac4f59839e4f 1051 }
ocomeni 75:08eff6258e1b 1052 }
ocomeni 75:08eff6258e1b 1053
ocomeni 75:08eff6258e1b 1054 void ATCmdManager::_oob_ena_ble_peri(){
ocomeni 103:7b566b522427 1055 dbg_printf(LOG, "\n Received enable BLE Peripheral command!!\n");
ocomeni 121:ac4f59839e4f 1056 startup_config->ble_enable = true;
ocomeni 95:290859010c8c 1057 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 75:08eff6258e1b 1058 }
ocomeni 75:08eff6258e1b 1059
ocomeni 75:08eff6258e1b 1060 void ATCmdManager::_oob_reboot(){
ocomeni 103:7b566b522427 1061 dbg_printf(LOG, "\n Received reboot command!!\n");
ocomeni 95:290859010c8c 1062 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 75:08eff6258e1b 1063 system_reset();
ocomeni 75:08eff6258e1b 1064 }
ocomeni 75:08eff6258e1b 1065
ocomeni 121:ac4f59839e4f 1066
ocomeni 121:ac4f59839e4f 1067 void ATCmdManager::_oob_factoryReset(){
ocomeni 121:ac4f59839e4f 1068 dbg_printf(LOG, "\n Received factory reset command!!\n");
ocomeni 121:ac4f59839e4f 1069 resetConfiguration();
ocomeni 121:ac4f59839e4f 1070 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 121:ac4f59839e4f 1071 }
ocomeni 121:ac4f59839e4f 1072
ocomeni 121:ac4f59839e4f 1073
ocomeni 121:ac4f59839e4f 1074 void ATCmdManager::_oob_deleteConfiguration(){
ocomeni 121:ac4f59839e4f 1075 dbg_printf(LOG, "\n Received delete configuration command!!\n");
ocomeni 121:ac4f59839e4f 1076 int configKey;
ocomeni 121:ac4f59839e4f 1077 bool res = false;
ocomeni 121:ac4f59839e4f 1078 if(_parser.scanf("%d", &configKey) >0)
ocomeni 121:ac4f59839e4f 1079 {
ocomeni 121:ac4f59839e4f 1080 res = deleteConfiguration((nvstore_key_t) configKey);
ocomeni 121:ac4f59839e4f 1081 }
ocomeni 121:ac4f59839e4f 1082 if(res)
ocomeni 121:ac4f59839e4f 1083 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 121:ac4f59839e4f 1084 }
ocomeni 121:ac4f59839e4f 1085
ocomeni 121:ac4f59839e4f 1086
ocomeni 121:ac4f59839e4f 1087
ocomeni 121:ac4f59839e4f 1088 void ATCmdManager::_oob_saveSettings_hdlr()
ocomeni 121:ac4f59839e4f 1089 {
ocomeni 121:ac4f59839e4f 1090 saveConfiguration(APP_CONFIG_0);
ocomeni 121:ac4f59839e4f 1091 sendAtConfirmation(OK_RESP);
ocomeni 121:ac4f59839e4f 1092 }
ocomeni 79:a2187bbfa407 1093 const char * ATCmdManager::sec2str(nsapi_security_t sec)
ocomeni 79:a2187bbfa407 1094 {
ocomeni 79:a2187bbfa407 1095 switch (sec) {
ocomeni 79:a2187bbfa407 1096 case NSAPI_SECURITY_NONE:
ocomeni 79:a2187bbfa407 1097 return "None";
ocomeni 79:a2187bbfa407 1098 case NSAPI_SECURITY_WEP:
ocomeni 79:a2187bbfa407 1099 return "WEP";
ocomeni 79:a2187bbfa407 1100 case NSAPI_SECURITY_WPA:
ocomeni 79:a2187bbfa407 1101 return "WPA";
ocomeni 79:a2187bbfa407 1102 case NSAPI_SECURITY_WPA2:
ocomeni 79:a2187bbfa407 1103 return "WPA2";
ocomeni 79:a2187bbfa407 1104 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 79:a2187bbfa407 1105 return "WPA/WPA2";
ocomeni 79:a2187bbfa407 1106 case NSAPI_SECURITY_UNKNOWN:
ocomeni 79:a2187bbfa407 1107 default:
ocomeni 79:a2187bbfa407 1108 return "Unknown";
ocomeni 79:a2187bbfa407 1109 }
ocomeni 79:a2187bbfa407 1110 }
ocomeni 81:637a87eb8170 1111
ocomeni 81:637a87eb8170 1112 bool ATCmdManager::setNextResponse(at_cmd_resp_t resp)
ocomeni 81:637a87eb8170 1113 {
ocomeni 81:637a87eb8170 1114 if(at_resp == AT_RESP_NONE){
ocomeni 81:637a87eb8170 1115 at_resp = resp;
ocomeni 81:637a87eb8170 1116 return true; // success
ocomeni 81:637a87eb8170 1117 }
ocomeni 81:637a87eb8170 1118 return false; // wiFiManager busy
ocomeni 79:a2187bbfa407 1119 }
ocomeni 79:a2187bbfa407 1120
ocomeni 81:637a87eb8170 1121
ocomeni 79:a2187bbfa407 1122 void ATCmdManager::_oob_scanWiFiNetworks(){
ocomeni 103:7b566b522427 1123 dbg_printf(LOG, "\n Received scanWiFiNetworks command!!\n");
ocomeni 95:290859010c8c 1124 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 79:a2187bbfa407 1125 wifi_cmd_t cmd = WIFI_CMD_SCAN;
ocomeni 79:a2187bbfa407 1126 // queue next command
ocomeni 79:a2187bbfa407 1127 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 1128 return;
ocomeni 75:08eff6258e1b 1129 }
ocomeni 75:08eff6258e1b 1130
ocomeni 95:290859010c8c 1131
ocomeni 95:290859010c8c 1132
ocomeni 98:65c2333a38b6 1133 void ATCmdManager::_oob_WiFiStationConfigAction()
ocomeni 79:a2187bbfa407 1134 {
ocomeni 98:65c2333a38b6 1135 int if_id; // interface id for request
ocomeni 98:65c2333a38b6 1136 int aId; // interface id for request
ocomeni 98:65c2333a38b6 1137 _parser.recv("%d,%d", &if_id, &aId);
ocomeni 103:7b566b522427 1138 dbg_printf(LOG, "\n Received WiFi Configuration Action command %d %d!!\r\n", if_id, aId);
ocomeni 98:65c2333a38b6 1139 if(if_id == WIFI_CONFIG_ID && aId <5){
ocomeni 95:290859010c8c 1140 wifi_cmd_t cmd;
ocomeni 98:65c2333a38b6 1141 action_id_t action_id = (action_id_t) aId;
ocomeni 95:290859010c8c 1142 switch(action_id){
ocomeni 95:290859010c8c 1143 case WIFI_CONFIG_RESET:
ocomeni 95:290859010c8c 1144 break;
ocomeni 95:290859010c8c 1145 case WIFI_CONFIG_STORE:
ocomeni 95:290859010c8c 1146 break;
ocomeni 95:290859010c8c 1147 case WIFI_CONFIG_LOAD:
ocomeni 95:290859010c8c 1148 break;
ocomeni 95:290859010c8c 1149 case WIFI_CONFIG_ACTIVATE:
ocomeni 95:290859010c8c 1150 cmd = WIFI_CMD_CONNECT;
ocomeni 103:7b566b522427 1151 dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd);
ocomeni 95:290859010c8c 1152 queueWiFiCommand(cmd);
ocomeni 95:290859010c8c 1153 break;
ocomeni 95:290859010c8c 1154 case WIFI_CONFIG_DEACTIVATE:
ocomeni 95:290859010c8c 1155 cmd = WIFI_CMD_DISCONNECT;
ocomeni 103:7b566b522427 1156 dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd);
ocomeni 95:290859010c8c 1157 queueWiFiCommand(cmd);
ocomeni 95:290859010c8c 1158 break;
ocomeni 95:290859010c8c 1159 default:
ocomeni 95:290859010c8c 1160 break;
ocomeni 95:290859010c8c 1161 }
ocomeni 95:290859010c8c 1162 }
ocomeni 81:637a87eb8170 1163 return;
ocomeni 79:a2187bbfa407 1164 }
ocomeni 79:a2187bbfa407 1165
ocomeni 79:a2187bbfa407 1166
ocomeni 79:a2187bbfa407 1167 void ATCmdManager::_oob_disconnectWiFiNetwork()
ocomeni 79:a2187bbfa407 1168 {
ocomeni 103:7b566b522427 1169 dbg_printf(LOG, "\n Received WiFi Disconnect command!!\n");
ocomeni 95:290859010c8c 1170 sendAtConfirmation(OK_RESP); //_parser.send(OK_RESP);
ocomeni 81:637a87eb8170 1171 wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
ocomeni 81:637a87eb8170 1172 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 1173 return;
ocomeni 81:637a87eb8170 1174 }
ocomeni 81:637a87eb8170 1175
ocomeni 81:637a87eb8170 1176 void ATCmdManager::_oob_setupInternetConnection()
ocomeni 81:637a87eb8170 1177 {
ocomeni 90:ed0267eca7b5 1178 char str[MAX_URL_LEN];
ocomeni 90:ed0267eca7b5 1179 char url[MAX_URL_LEN];
ocomeni 81:637a87eb8170 1180 int n;
ocomeni 81:637a87eb8170 1181 internet_config_t internet_config;
ocomeni 103:7b566b522427 1182 dbg_printf(LOG, "sizeof internet_config_t = %d\n", sizeof(internet_config_t));
ocomeni 90:ed0267eca7b5 1183 n = ReadBytes((uint8_t *)str, MAX_URL_LEN);
ocomeni 84:7c7add00f4bf 1184 str[n]=NULL;
ocomeni 112:a0999ea4ece0 1185 int id;
ocomeni 112:a0999ea4ece0 1186 int scheme;
ocomeni 103:7b566b522427 1187 dbg_printf(LOG, "\n read string = %s , n = %d\n", str, n);
ocomeni 112:a0999ea4ece0 1188 n = sscanf(str, "=%1d,%99[^,],%1d", &id,
ocomeni 92:ec9550034276 1189 url, //internet_config.url,
ocomeni 112:a0999ea4ece0 1190 &scheme);
ocomeni 112:a0999ea4ece0 1191 internet_config.peer_id = (uint8_t) id;
ocomeni 112:a0999ea4ece0 1192 internet_config.connectionScheme = (conn_scheme_t) scheme;
ocomeni 103:7b566b522427 1193 #ifdef BOX_UBLOX_DEMO_TESTING
ocomeni 103:7b566b522427 1194 char *p = strstr(url,"/https:");
ocomeni 103:7b566b522427 1195 #else
ocomeni 92:ec9550034276 1196 char *p = strstr(url,"\"");
ocomeni 103:7b566b522427 1197 #endif
ocomeni 92:ec9550034276 1198 if(p!=NULL)
ocomeni 92:ec9550034276 1199 {
ocomeni 92:ec9550034276 1200 strncpy(internet_config.url, &p[1], strlen(url));
ocomeni 92:ec9550034276 1201 p = strstr(internet_config.url,"\"");
ocomeni 92:ec9550034276 1202 *p = NULL;
ocomeni 92:ec9550034276 1203 }
ocomeni 92:ec9550034276 1204 else
ocomeni 92:ec9550034276 1205 {
ocomeni 92:ec9550034276 1206 strncpy(internet_config.url, url, strlen(url)+1);
ocomeni 92:ec9550034276 1207 }
ocomeni 103:7b566b522427 1208 dbg_printf(LOG, "\n read string = %s , n = %d -- strlen(url) = %d\n", internet_config.url, n, strlen(internet_config.url));
ocomeni 81:637a87eb8170 1209 if(n>0)
ocomeni 81:637a87eb8170 1210 {
ocomeni 103:7b566b522427 1211 dbg_printf(LOG, "peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 90:ed0267eca7b5 1212 internet_config.url,
ocomeni 90:ed0267eca7b5 1213 internet_config.connectionScheme);
ocomeni 81:637a87eb8170 1214 // package and send on wifi data queue
ocomeni 90:ed0267eca7b5 1215 wifi_data_msg_t data_msg;
ocomeni 81:637a87eb8170 1216 data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG;
ocomeni 84:7c7add00f4bf 1217 data_msg.dataLen = sizeof(internet_config_t); // + strlen(internet_config.url);
ocomeni 103:7b566b522427 1218 dbg_printf(LOG, "\n url size = %d url str = %s\n",strlen(internet_config.url), internet_config.url );
ocomeni 81:637a87eb8170 1219 memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
ocomeni 82:10072c1794d3 1220 // queue next data request
ocomeni 81:637a87eb8170 1221 queueWiFiDataRequest(data_msg);
ocomeni 84:7c7add00f4bf 1222 print_memory_info();
ocomeni 81:637a87eb8170 1223 } else {
ocomeni 103:7b566b522427 1224 dbg_printf(LOG, "\n[ATCMD MAN]: internet configuration failed %d fields parsed \r\n", n);
ocomeni 82:10072c1794d3 1225 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 1226 }
ocomeni 82:10072c1794d3 1227 }
ocomeni 82:10072c1794d3 1228
ocomeni 82:10072c1794d3 1229 wifi_config_t ATCmdManager::init_wifi_config()
ocomeni 82:10072c1794d3 1230 {
ocomeni 82:10072c1794d3 1231 wifi_config_t wifi_cfg;
ocomeni 82:10072c1794d3 1232 wifi_cfg.ssid[0] = NULL;
ocomeni 82:10072c1794d3 1233 wifi_cfg.pass[0] = NULL;
ocomeni 82:10072c1794d3 1234 wifi_cfg.security = NSAPI_SECURITY_UNKNOWN;
ocomeni 82:10072c1794d3 1235 return wifi_cfg;
ocomeni 82:10072c1794d3 1236 }
ocomeni 112:a0999ea4ece0 1237
ocomeni 83:9c271a50a70b 1238 /* read ASCII characters into buffer and null terminate */
ocomeni 83:9c271a50a70b 1239 int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes)
ocomeni 82:10072c1794d3 1240 {
ocomeni 82:10072c1794d3 1241 int c;
ocomeni 82:10072c1794d3 1242 int sptr = 0;
ocomeni 93:06e755a80187 1243 int quoteCnt = 0;
ocomeni 82:10072c1794d3 1244 for(int i=0;i<maxBytes;i++){
ocomeni 82:10072c1794d3 1245 c = _parser.getc();
ocomeni 93:06e755a80187 1246 if(c== '"')quoteCnt++;
ocomeni 93:06e755a80187 1247 if(c==-1 || quoteCnt==2){
ocomeni 83:9c271a50a70b 1248 buf[sptr] = NULL; // null terminate if string
ocomeni 82:10072c1794d3 1249 return i;
ocomeni 82:10072c1794d3 1250 }
ocomeni 82:10072c1794d3 1251 if(c != ',' && c!= '"'){
ocomeni 82:10072c1794d3 1252 buf[sptr++] = (uint8_t) c;
ocomeni 82:10072c1794d3 1253 }
ocomeni 81:637a87eb8170 1254 }
ocomeni 83:9c271a50a70b 1255 return maxBytes;
ocomeni 83:9c271a50a70b 1256 }
ocomeni 83:9c271a50a70b 1257
ocomeni 83:9c271a50a70b 1258 int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes)
ocomeni 83:9c271a50a70b 1259 {
ocomeni 83:9c271a50a70b 1260 int c;
ocomeni 83:9c271a50a70b 1261 int sptr = 0;
ocomeni 83:9c271a50a70b 1262 for(int i=0;i<maxBytes;i++){
ocomeni 83:9c271a50a70b 1263 c = _parser.getc();
ocomeni 83:9c271a50a70b 1264 if(c==-1){
ocomeni 83:9c271a50a70b 1265 return i;
ocomeni 83:9c271a50a70b 1266 }
ocomeni 83:9c271a50a70b 1267 buf[sptr++] = (uint8_t) c;
ocomeni 83:9c271a50a70b 1268 }
ocomeni 83:9c271a50a70b 1269 return maxBytes;
ocomeni 82:10072c1794d3 1270 }
ocomeni 82:10072c1794d3 1271
ocomeni 82:10072c1794d3 1272 void ATCmdManager::_oob_setWiFiSSID()
ocomeni 82:10072c1794d3 1273 {
ocomeni 82:10072c1794d3 1274 int n;
ocomeni 82:10072c1794d3 1275 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 83:9c271a50a70b 1276 n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32);
ocomeni 103:7b566b522427 1277 dbg_printf(LOG, "[ATCMD MAN]: number of bytes read = %d\n", n);
ocomeni 82:10072c1794d3 1278 if(n>0)
ocomeni 82:10072c1794d3 1279 {
ocomeni 103:7b566b522427 1280 dbg_printf(LOG, "[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid);
ocomeni 82:10072c1794d3 1281 // package and send on wifi data queue
ocomeni 82:10072c1794d3 1282 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 1283 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 1284 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 1285 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 1286 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 1287 } else {
ocomeni 103:7b566b522427 1288 dbg_printf(LOG, "\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 1289 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 1290 }
ocomeni 82:10072c1794d3 1291 }
ocomeni 82:10072c1794d3 1292
ocomeni 82:10072c1794d3 1293 void ATCmdManager::_oob_setWiFiPWD()
ocomeni 82:10072c1794d3 1294 {
ocomeni 82:10072c1794d3 1295 int n;
ocomeni 82:10072c1794d3 1296 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 83:9c271a50a70b 1297 n = readStringBytes((uint8_t *)wifi_cfg.pass, 32);
ocomeni 82:10072c1794d3 1298 if(n>0)
ocomeni 82:10072c1794d3 1299 {
ocomeni 103:7b566b522427 1300 dbg_printf(LOG, "ATCMD MAN]: wifi_cfg.pass = %s\n", "****************");
ocomeni 82:10072c1794d3 1301 // package and send on wifi data queue
ocomeni 82:10072c1794d3 1302 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 1303 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 1304 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 1305 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 1306 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 1307 } else {
ocomeni 103:7b566b522427 1308 dbg_printf(LOG, "\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 1309 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 1310 }
ocomeni 82:10072c1794d3 1311 }
ocomeni 82:10072c1794d3 1312
ocomeni 82:10072c1794d3 1313 void ATCmdManager::_oob_setWiFiSecurity()
ocomeni 82:10072c1794d3 1314 {
ocomeni 82:10072c1794d3 1315 int n;
ocomeni 82:10072c1794d3 1316 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 112:a0999ea4ece0 1317 int security;
ocomeni 112:a0999ea4ece0 1318 n = _parser.scanf(",%d", &security);
ocomeni 82:10072c1794d3 1319 if(n>0)
ocomeni 82:10072c1794d3 1320 {
ocomeni 112:a0999ea4ece0 1321 wifi_cfg.security = (nsapi_security_t) security;
ocomeni 103:7b566b522427 1322 dbg_printf(LOG, "ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
ocomeni 82:10072c1794d3 1323 // package and send on wifi data queue
ocomeni 82:10072c1794d3 1324 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 1325 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 1326 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 1327 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 1328 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 1329 } else {
ocomeni 103:7b566b522427 1330 dbg_printf(LOG, "\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 90:ed0267eca7b5 1331 _smutex.lock();
ocomeni 82:10072c1794d3 1332 _parser.send("NAK\n");
ocomeni 90:ed0267eca7b5 1333 _smutex.unlock();
ocomeni 82:10072c1794d3 1334 }
ocomeni 79:a2187bbfa407 1335 }
ocomeni 81:637a87eb8170 1336
ocomeni 83:9c271a50a70b 1337
ocomeni 95:290859010c8c 1338
ocomeni 95:290859010c8c 1339 void ATCmdManager::_oob_getNetworkStatus()
ocomeni 95:290859010c8c 1340 {
ocomeni 112:a0999ea4ece0 1341 int if_id; // interface id for request
ocomeni 126:9bc33f8b57d5 1342 _parser.scanf(",%d", &if_id);
ocomeni 103:7b566b522427 1343 dbg_printf(LOG, "\n Received Get Network Status command!!\n");
ocomeni 95:290859010c8c 1344 if(if_id == WIFI_INTERFACE_ID){
ocomeni 95:290859010c8c 1345 wifi_cmd_t cmd = WIFI_CMD_NETWORK_STATUS;
ocomeni 103:7b566b522427 1346 dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd);
ocomeni 95:290859010c8c 1347 queueWiFiCommand(cmd);
ocomeni 95:290859010c8c 1348 }
ocomeni 95:290859010c8c 1349 return;
ocomeni 95:290859010c8c 1350 }
ocomeni 95:290859010c8c 1351
ocomeni 95:290859010c8c 1352
ocomeni 95:290859010c8c 1353 void ATCmdManager::_oob_WiFiNetworkStatus()
ocomeni 95:290859010c8c 1354 {
ocomeni 103:7b566b522427 1355 dbg_printf(LOG, "\n Received Get WiFi Network Status command!!\n");
ocomeni 95:290859010c8c 1356 wifi_cmd_t cmd = WIFI_CMD_WIFI_STATUS;
ocomeni 103:7b566b522427 1357 dbg_printf(LOG, "\n About to Queue wifi cmd = %d!!\n", cmd);
ocomeni 95:290859010c8c 1358 // queue next command
ocomeni 95:290859010c8c 1359 queueWiFiCommand(cmd);
ocomeni 95:290859010c8c 1360 return;
ocomeni 95:290859010c8c 1361 }
ocomeni 95:290859010c8c 1362
ocomeni 83:9c271a50a70b 1363 void ATCmdManager::_oob_sendHttpMessage()
ocomeni 83:9c271a50a70b 1364 {
ocomeni 83:9c271a50a70b 1365 }
ocomeni 83:9c271a50a70b 1366
ocomeni 81:637a87eb8170 1367 bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
ocomeni 103:7b566b522427 1368 dbg_printf(LOG, "[ATCMD MAN] about to be queued with wifi_cmd = %d\n", cmd);
ocomeni 92:ec9550034276 1369 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 81:637a87eb8170 1370 wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
ocomeni 92:ec9550034276 1371 if(wifiCmd == NULL){
ocomeni 103:7b566b522427 1372 dbg_printf(LOG, "[ATCMD MAN] queued memory allocation failed\n");
ocomeni 92:ec9550034276 1373 return false;
ocomeni 92:ec9550034276 1374 }
ocomeni 92:ec9550034276 1375 #else
ocomeni 92:ec9550034276 1376 wifi_cmd_message_t *wifiCmd = (wifi_cmd_message_t *) malloc(sizeof(wifi_cmd_message_t));
ocomeni 92:ec9550034276 1377 if(wifiCmd == NULL){
ocomeni 103:7b566b522427 1378 dbg_printf(LOG, "[ATCMD MAN] try malloc() : queued memory allocation failed\n");
ocomeni 92:ec9550034276 1379 return false;
ocomeni 92:ec9550034276 1380 }
ocomeni 92:ec9550034276 1381 #endif
ocomeni 81:637a87eb8170 1382 wifiCmd->wifi_cmd = cmd;
ocomeni 81:637a87eb8170 1383 _aT2WiFiCmdQueue->put(wifiCmd);
ocomeni 103:7b566b522427 1384 dbg_printf(LOG, "[ATCMD MAN] queued wifi_cmd = %d\n", wifiCmd->wifi_cmd);
ocomeni 81:637a87eb8170 1385 return true;
ocomeni 81:637a87eb8170 1386 }
ocomeni 81:637a87eb8170 1387
ocomeni 81:637a87eb8170 1388 bool ATCmdManager::dequeueATresponse(){
ocomeni 81:637a87eb8170 1389 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 1390 osEvent evt = _wiFi2ATCmdQueue->get(0);
ocomeni 81:637a87eb8170 1391 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 1392 at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
ocomeni 81:637a87eb8170 1393 setNextResponse(resp->at_resp);
ocomeni 103:7b566b522427 1394 dbg_printf(LOG, "[ATCMD MAN] dequeued AT CMD : at_resp = %d\n", resp->at_resp);
ocomeni 92:ec9550034276 1395 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 81:637a87eb8170 1396 _wiFi2ATmPool->free(resp);
ocomeni 96:f5ed273881af 1397 resp = NULL;
ocomeni 92:ec9550034276 1398 #else
ocomeni 92:ec9550034276 1399 free(resp);
ocomeni 96:f5ed273881af 1400 resp = NULL;
ocomeni 92:ec9550034276 1401 #endif
ocomeni 81:637a87eb8170 1402 }
ocomeni 81:637a87eb8170 1403 return true;
ocomeni 81:637a87eb8170 1404 }
ocomeni 81:637a87eb8170 1405
ocomeni 81:637a87eb8170 1406 bool ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
ocomeni 104:11e9605093c9 1407 static bool memFull = false;
ocomeni 81:637a87eb8170 1408 wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
ocomeni 104:11e9605093c9 1409 if(wifiData == NULL)
ocomeni 104:11e9605093c9 1410 {
ocomeni 109:c274780ff609 1411 #ifdef SEND_DEBUG_MESSAGES
ocomeni 104:11e9605093c9 1412 sendAtConfirmation("\r\nQUEUE MEMORY FULL\r\n");
ocomeni 109:c274780ff609 1413 #endif
ocomeni 104:11e9605093c9 1414 memFull = true;
ocomeni 104:11e9605093c9 1415 return false;
ocomeni 104:11e9605093c9 1416 }
ocomeni 104:11e9605093c9 1417 if(memFull)
ocomeni 104:11e9605093c9 1418 {
ocomeni 104:11e9605093c9 1419 memFull = false;
ocomeni 109:c274780ff609 1420 #ifdef SEND_DEBUG_MESSAGES
ocomeni 104:11e9605093c9 1421 sendAtConfirmation("\r\n[ATCMD-MAN] memory released...\r\n");
ocomeni 109:c274780ff609 1422 #endif
ocomeni 104:11e9605093c9 1423 }
ocomeni 81:637a87eb8170 1424 wifiData->wifi_cmd = data_req.wifi_cmd;
ocomeni 81:637a87eb8170 1425 wifiData->dataLen = data_req.dataLen;
ocomeni 81:637a87eb8170 1426 memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen);
ocomeni 81:637a87eb8170 1427 _aT2WiFiDataQueue->put(wifiData);
ocomeni 103:7b566b522427 1428 dbg_printf(LOG, "[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd);
ocomeni 81:637a87eb8170 1429 return true;
ocomeni 81:637a87eb8170 1430 }
ocomeni 81:637a87eb8170 1431
ocomeni 87:99b37d26ff2a 1432 bool ATCmdManager::dequeueWiFidataResponse(){
ocomeni 81:637a87eb8170 1433 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 1434 osEvent evt = _wiFi2ATDataQueue->get(0);
ocomeni 81:637a87eb8170 1435 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 1436 resp_data = (at_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 1437 setNextResponse(resp_data->at_resp);
ocomeni 103:7b566b522427 1438 dbg_printf(LOG, "[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", resp_data->dataLen, resp_data->at_resp);
ocomeni 81:637a87eb8170 1439 }
ocomeni 81:637a87eb8170 1440 return true;
ocomeni 81:637a87eb8170 1441 }
ocomeni 81:637a87eb8170 1442
ocomeni 120:779b74689747 1443 bool ATCmdManager::queueBleDataRequest(at_ble_msg_t data_req){
ocomeni 120:779b74689747 1444 at_ble_msg_t *bleData = _aT2BleDatamPool->alloc();
ocomeni 120:779b74689747 1445 if(bleData == NULL)
ocomeni 120:779b74689747 1446 {
ocomeni 120:779b74689747 1447 #ifdef SEND_DEBUG_MESSAGES
ocomeni 120:779b74689747 1448 sendAtConfirmation("\r\nBLE QUEUE MEMORY FULL\r\n");
ocomeni 120:779b74689747 1449 #endif
ocomeni 120:779b74689747 1450 return false;
ocomeni 120:779b74689747 1451 }
ocomeni 120:779b74689747 1452 bleData->ble_cmd = data_req.ble_cmd;
ocomeni 120:779b74689747 1453 bleData->dataLen = data_req.dataLen;
ocomeni 120:779b74689747 1454 memcpy(bleData->buffer, data_req.buffer, data_req.dataLen);
ocomeni 120:779b74689747 1455 _aT2BleDataQueue->put(bleData);
ocomeni 123:a49e9ffbaca6 1456 dbg_printf(LOG, "[ATCMD MAN] queued BLE data size = %d : ble_cmd = %d\n", data_req.dataLen, data_req.ble_cmd);
ocomeni 120:779b74689747 1457 return true;
ocomeni 120:779b74689747 1458 }
ocomeni 120:779b74689747 1459
ocomeni 120:779b74689747 1460 bool ATCmdManager::dequeueBleDataResponse(){
ocomeni 120:779b74689747 1461 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 120:779b74689747 1462 osEvent evt = _ble2ATDataQueue->get(0);
ocomeni 120:779b74689747 1463 if(evt.status == osEventMessage){
ocomeni 120:779b74689747 1464 ble_resp_data = (ble_at_msg_t*)evt.value.p;
ocomeni 120:779b74689747 1465 setNextResponse(ble_resp_data->at_resp);
ocomeni 121:ac4f59839e4f 1466 dbg_printf(LOG, "[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", ble_resp_data->dataLen, ble_resp_data->at_resp);
ocomeni 120:779b74689747 1467 }
ocomeni 120:779b74689747 1468 return true;
ocomeni 120:779b74689747 1469 }
ocomeni 120:779b74689747 1470
ocomeni 93:06e755a80187 1471
ocomeni 93:06e755a80187 1472 void ATCmdManager::sendAtConfirmation(const char *buf)
ocomeni 93:06e755a80187 1473 {
ocomeni 96:f5ed273881af 1474 _smutex.lock();
ocomeni 95:290859010c8c 1475 switch(dataMode){
ocomeni 95:290859010c8c 1476 case AT_CMD_DATA_MODE:
ocomeni 98:65c2333a38b6 1477 _parser.send("%s", buf);
ocomeni 95:290859010c8c 1478 break;
ocomeni 95:290859010c8c 1479 case AT_STD_DATA_MODE:
ocomeni 98:65c2333a38b6 1480 _parser.send("%s", buf);
ocomeni 95:290859010c8c 1481 break;
ocomeni 95:290859010c8c 1482 case AT_EXT_DATA_MODE:
ocomeni 98:65c2333a38b6 1483 {
ocomeni 96:f5ed273881af 1484 int len = strlen(buf);
ocomeni 95:290859010c8c 1485 outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL);
ocomeni 95:290859010c8c 1486 break;
ocomeni 98:65c2333a38b6 1487 }
ocomeni 95:290859010c8c 1488 default:
ocomeni 98:65c2333a38b6 1489 _parser.send("%s", buf);
ocomeni 95:290859010c8c 1490 break;
ocomeni 95:290859010c8c 1491 }
ocomeni 96:f5ed273881af 1492 _smutex.unlock();
ocomeni 93:06e755a80187 1493 }
ocomeni 93:06e755a80187 1494 void ATCmdManager::sendAtEvent(const char *buf)
ocomeni 93:06e755a80187 1495 {
ocomeni 96:f5ed273881af 1496 _smutex.lock();
ocomeni 95:290859010c8c 1497 switch(dataMode){
ocomeni 95:290859010c8c 1498 case AT_CMD_DATA_MODE:
ocomeni 95:290859010c8c 1499 _parser.send(buf);
ocomeni 95:290859010c8c 1500 break;
ocomeni 95:290859010c8c 1501 case AT_STD_DATA_MODE:
ocomeni 95:290859010c8c 1502 _parser.send(buf);
ocomeni 95:290859010c8c 1503 break;
ocomeni 95:290859010c8c 1504 case AT_EXT_DATA_MODE:
ocomeni 99:05398b3184f8 1505 {
ocomeni 96:f5ed273881af 1506 int len = strlen(buf);
ocomeni 95:290859010c8c 1507 outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, EVENT_MSG_TYPE, NO_CHANNEL);
ocomeni 95:290859010c8c 1508 break;
ocomeni 99:05398b3184f8 1509 }
ocomeni 99:05398b3184f8 1510 default:
ocomeni 99:05398b3184f8 1511 _parser.send(buf);
ocomeni 99:05398b3184f8 1512 break;
ocomeni 95:290859010c8c 1513 }
ocomeni 96:f5ed273881af 1514 _smutex.unlock();
ocomeni 100:80ef4bc31b7a 1515 _wiFi2ATDatamPool->free(resp_data);
ocomeni 100:80ef4bc31b7a 1516 resp_data = NULL;
ocomeni 93:06e755a80187 1517 }
ocomeni 93:06e755a80187 1518
ocomeni 99:05398b3184f8 1519
ocomeni 118:8df0e9c2ee3f 1520 void ATCmdManager::sendBleDataEvent(const char *buf, int len)
ocomeni 118:8df0e9c2ee3f 1521 {
ocomeni 118:8df0e9c2ee3f 1522 _smutex.lock();
ocomeni 118:8df0e9c2ee3f 1523 switch(dataMode){
ocomeni 118:8df0e9c2ee3f 1524 case AT_CMD_DATA_MODE:
ocomeni 118:8df0e9c2ee3f 1525 _parser.send(buf);
ocomeni 118:8df0e9c2ee3f 1526 break;
ocomeni 118:8df0e9c2ee3f 1527 case AT_STD_DATA_MODE:
ocomeni 118:8df0e9c2ee3f 1528 _parser.send(buf);
ocomeni 118:8df0e9c2ee3f 1529 break;
ocomeni 118:8df0e9c2ee3f 1530 case AT_EXT_DATA_MODE:
ocomeni 118:8df0e9c2ee3f 1531 {
ocomeni 118:8df0e9c2ee3f 1532 outputEDMdata((const uint8_t *) buf, len, DATA_MSG_ID, EVENT_MSG_TYPE, BLE_CHANNEL);
ocomeni 118:8df0e9c2ee3f 1533 break;
ocomeni 118:8df0e9c2ee3f 1534 }
ocomeni 118:8df0e9c2ee3f 1535 default:
ocomeni 118:8df0e9c2ee3f 1536 _parser.send(buf);
ocomeni 118:8df0e9c2ee3f 1537 break;
ocomeni 118:8df0e9c2ee3f 1538 }
ocomeni 118:8df0e9c2ee3f 1539 _smutex.unlock();
ocomeni 119:8d939a902333 1540 _ble2ATDatamPool->free(ble_resp_data);
ocomeni 119:8d939a902333 1541 ble_resp_data = NULL;
ocomeni 119:8d939a902333 1542 }
ocomeni 119:8d939a902333 1543
ocomeni 119:8d939a902333 1544
ocomeni 119:8d939a902333 1545
ocomeni 119:8d939a902333 1546 void ATCmdManager::sendBleAtEvent(const char *buf, int len)
ocomeni 119:8d939a902333 1547 {
ocomeni 119:8d939a902333 1548 _smutex.lock();
ocomeni 119:8d939a902333 1549 switch(dataMode){
ocomeni 119:8d939a902333 1550 case AT_CMD_DATA_MODE:
ocomeni 119:8d939a902333 1551 _parser.send(buf);
ocomeni 119:8d939a902333 1552 break;
ocomeni 119:8d939a902333 1553 case AT_STD_DATA_MODE:
ocomeni 119:8d939a902333 1554 _parser.send(buf);
ocomeni 119:8d939a902333 1555 break;
ocomeni 119:8d939a902333 1556 case AT_EXT_DATA_MODE:
ocomeni 119:8d939a902333 1557 {
ocomeni 122:62166886db5f 1558 outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, EVENT_MSG_TYPE, NO_CHANNEL);
ocomeni 119:8d939a902333 1559 break;
ocomeni 119:8d939a902333 1560 }
ocomeni 119:8d939a902333 1561 default:
ocomeni 119:8d939a902333 1562 _parser.send(buf);
ocomeni 119:8d939a902333 1563 break;
ocomeni 119:8d939a902333 1564 }
ocomeni 119:8d939a902333 1565 _smutex.unlock();
ocomeni 119:8d939a902333 1566 _ble2ATDatamPool->free(ble_resp_data);
ocomeni 119:8d939a902333 1567 ble_resp_data = NULL;
ocomeni 119:8d939a902333 1568 }
ocomeni 119:8d939a902333 1569
ocomeni 119:8d939a902333 1570 void ATCmdManager::sendBleConnectEvent(const char *buf, int len)
ocomeni 119:8d939a902333 1571 {
ocomeni 119:8d939a902333 1572 _smutex.lock();
ocomeni 119:8d939a902333 1573 switch(dataMode){
ocomeni 119:8d939a902333 1574 case AT_CMD_DATA_MODE:
ocomeni 119:8d939a902333 1575 _parser.send(buf);
ocomeni 119:8d939a902333 1576 break;
ocomeni 119:8d939a902333 1577 case AT_STD_DATA_MODE:
ocomeni 119:8d939a902333 1578 _parser.send(buf);
ocomeni 119:8d939a902333 1579 break;
ocomeni 119:8d939a902333 1580 case AT_EXT_DATA_MODE:
ocomeni 119:8d939a902333 1581 {
ocomeni 119:8d939a902333 1582 outputEDMdata((const uint8_t *) buf, len, CONNECT_MSG_ID, EVENT_MSG_TYPE, BLE_CHANNEL);
ocomeni 119:8d939a902333 1583 break;
ocomeni 119:8d939a902333 1584 }
ocomeni 119:8d939a902333 1585 default:
ocomeni 119:8d939a902333 1586 _parser.send(buf);
ocomeni 119:8d939a902333 1587 break;
ocomeni 119:8d939a902333 1588 }
ocomeni 119:8d939a902333 1589 _smutex.unlock();
ocomeni 119:8d939a902333 1590 _ble2ATDatamPool->free(ble_resp_data);
ocomeni 119:8d939a902333 1591 ble_resp_data = NULL;
ocomeni 118:8df0e9c2ee3f 1592 }
ocomeni 118:8df0e9c2ee3f 1593
ocomeni 120:779b74689747 1594 void ATCmdManager::sendBleDisconnectEvent()
ocomeni 120:779b74689747 1595 {
ocomeni 120:779b74689747 1596 _smutex.lock();
ocomeni 120:779b74689747 1597 outputEDMdata(NULL, 0, DISCONNECT_MSG_ID, EVENT_MSG_TYPE, BLE_CHANNEL);
ocomeni 120:779b74689747 1598 _smutex.unlock();
ocomeni 120:779b74689747 1599 _ble2ATDatamPool->free(ble_resp_data);
ocomeni 120:779b74689747 1600 ble_resp_data = NULL;
ocomeni 120:779b74689747 1601 }
ocomeni 120:779b74689747 1602
ocomeni 118:8df0e9c2ee3f 1603
ocomeni 99:05398b3184f8 1604 void ATCmdManager::sendConnectEvent(const uint8_t *buf, int len)
ocomeni 99:05398b3184f8 1605 {
ocomeni 99:05398b3184f8 1606 switch(dataMode){
ocomeni 99:05398b3184f8 1607 case AT_CMD_DATA_MODE:
ocomeni 99:05398b3184f8 1608 _parser.send((const char*) buf);
ocomeni 99:05398b3184f8 1609 break;
ocomeni 99:05398b3184f8 1610 case AT_STD_DATA_MODE:
ocomeni 99:05398b3184f8 1611 _parser.send((const char*)buf);
ocomeni 99:05398b3184f8 1612 break;
ocomeni 99:05398b3184f8 1613 case AT_EXT_DATA_MODE:
ocomeni 99:05398b3184f8 1614 outputEDMdata((const uint8_t *) buf, len, CONNECT_MSG_ID, EVENT_MSG_TYPE, WIFI_CHANNEL);
ocomeni 99:05398b3184f8 1615 break;
ocomeni 99:05398b3184f8 1616 }
ocomeni 100:80ef4bc31b7a 1617 _wiFi2ATDatamPool->free(resp_data);
ocomeni 100:80ef4bc31b7a 1618 resp_data = NULL;
ocomeni 100:80ef4bc31b7a 1619
ocomeni 99:05398b3184f8 1620 }
ocomeni 99:05398b3184f8 1621
ocomeni 99:05398b3184f8 1622
ocomeni 90:ed0267eca7b5 1623 void ATCmdManager::outputEDMdata(const uint8_t *buf, int pLen,
ocomeni 91:d6b6319ad681 1624 edm_msg_id_t identifier, edm_msg_type_t type,
ocomeni 91:d6b6319ad681 1625 channel_id_t channel_id)
ocomeni 90:ed0267eca7b5 1626 {
ocomeni 90:ed0267eca7b5 1627 int epLen = pLen + 2; // edm payload length = data length + 2
ocomeni 96:f5ed273881af 1628 if(channel_id != NO_CHANNEL)
ocomeni 96:f5ed273881af 1629 epLen += 1;
ocomeni 90:ed0267eca7b5 1630 _smutex.lock();
ocomeni 90:ed0267eca7b5 1631 // send EDM Message start byte
ocomeni 90:ed0267eca7b5 1632 _parser.putc(EDM_START_BYTE);
ocomeni 90:ed0267eca7b5 1633 // send EDM Message length
ocomeni 90:ed0267eca7b5 1634 _parser.putc(epLen>>8);
ocomeni 90:ed0267eca7b5 1635 _parser.putc(epLen%256);
ocomeni 90:ed0267eca7b5 1636 // send EDM Identifier + Type
ocomeni 90:ed0267eca7b5 1637 _parser.putc(identifier>>8);
ocomeni 90:ed0267eca7b5 1638 _parser.putc(identifier%256 | type);
ocomeni 93:06e755a80187 1639 // send channel id if valid
ocomeni 93:06e755a80187 1640 if(channel_id != NO_CHANNEL)
ocomeni 93:06e755a80187 1641 _parser.putc(channel_id);
ocomeni 90:ed0267eca7b5 1642 // send the data
ocomeni 120:779b74689747 1643 if(pLen > 0)
ocomeni 120:779b74689747 1644 {
ocomeni 120:779b74689747 1645 _parser.write((const char *)buf, pLen);
ocomeni 120:779b74689747 1646 }
ocomeni 90:ed0267eca7b5 1647 // send EDM Message stop byte
ocomeni 90:ed0267eca7b5 1648 _parser.putc(EDM_STOP_BYTE);
ocomeni 98:65c2333a38b6 1649 _smutex.unlock();
ocomeni 96:f5ed273881af 1650 int msWait = (pLen + 5+20)/20;
ocomeni 96:f5ed273881af 1651 wait_ms(msWait);
ocomeni 90:ed0267eca7b5 1652 }
ocomeni 122:62166886db5f 1653 void ATCmdManager::filterHttpResponse()
ocomeni 122:62166886db5f 1654 {
ocomeni 122:62166886db5f 1655 char * respbuf = (char *) resp_data->buffer;
ocomeni 122:62166886db5f 1656 char * strPtr;
ocomeni 122:62166886db5f 1657 char * strPtr2;
ocomeni 122:62166886db5f 1658 dbg_printf(LOG, "Header before trim:\r\n%s\r\n", respbuf);
ocomeni 122:62166886db5f 1659 strPtr = strstr(respbuf, "Date:"); // find start of the last required entry
ocomeni 122:62166886db5f 1660 if(strPtr == NULL) return;
ocomeni 122:62166886db5f 1661 strPtr2 = strstr(respbuf, "Connection:"); // find start of the last required entry
ocomeni 122:62166886db5f 1662 if(strPtr == NULL) return;
ocomeni 122:62166886db5f 1663 if(strPtr2 > strPtr)
ocomeni 122:62166886db5f 1664 {
ocomeni 122:62166886db5f 1665 strPtr = strPtr2;
ocomeni 122:62166886db5f 1666 }
ocomeni 122:62166886db5f 1667 strPtr2 = strstr(strPtr, "\r\n"); // find end of the last required entry
ocomeni 122:62166886db5f 1668 if(strPtr2 == NULL) return;
ocomeni 122:62166886db5f 1669 strPtr = strstr(strPtr2, "\r\n\r\n"); // find start of body
ocomeni 122:62166886db5f 1670 if(strPtr == NULL) return;
ocomeni 122:62166886db5f 1671 int hdrLen = (strPtr - respbuf);
ocomeni 122:62166886db5f 1672 int bytes2trim = (strPtr - strPtr2);
ocomeni 122:62166886db5f 1673 memmove(strPtr2, strPtr, (resp_data->dataLen-(hdrLen-bytes2trim)));
ocomeni 122:62166886db5f 1674 resp_data->dataLen -= bytes2trim; // reduce the length by bytes to trim
ocomeni 122:62166886db5f 1675 dbg_printf(LOG, "Header after trim:\r\n%s\r\n", respbuf);
ocomeni 122:62166886db5f 1676 }
ocomeni 89:45f6db09a76d 1677 void ATCmdManager::return_response(bool download) {
ocomeni 87:99b37d26ff2a 1678 char * resp = (char *) resp_data->buffer;
ocomeni 103:7b566b522427 1679 dbg_printf(LOG, "\n[ATCMD MAN] received response:\n");
ocomeni 89:45f6db09a76d 1680 if(download == false) // not download must be ascii header
ocomeni 89:45f6db09a76d 1681 {
ocomeni 103:7b566b522427 1682 dbg_printf(LOG, "%.*s\r\n", resp_data->dataLen, resp);
ocomeni 89:45f6db09a76d 1683 }
ocomeni 89:45f6db09a76d 1684 else // dump payload as hex
ocomeni 89:45f6db09a76d 1685 {
ocomeni 89:45f6db09a76d 1686 printBufferInHex((uint8_t *)resp, resp_data->dataLen);
ocomeni 89:45f6db09a76d 1687 }
ocomeni 122:62166886db5f 1688 #ifdef TRIM_AWS_HEADER_ENTRIES
ocomeni 122:62166886db5f 1689 int offset = resp - strstr(resp,"HTTP/1.1");
ocomeni 122:62166886db5f 1690 if(offset >=0 && offset < 2) // must be at start of header
ocomeni 122:62166886db5f 1691 {
ocomeni 122:62166886db5f 1692 filterHttpResponse();
ocomeni 122:62166886db5f 1693 }
ocomeni 122:62166886db5f 1694 #endif
ocomeni 91:d6b6319ad681 1695 outputEDMdata((const uint8_t *)resp, resp_data->dataLen, DATA_MSG_ID,
ocomeni 91:d6b6319ad681 1696 EVENT_MSG_TYPE, WIFI_CHANNEL);
ocomeni 87:99b37d26ff2a 1697 _wiFi2ATDatamPool->free(resp_data);
ocomeni 96:f5ed273881af 1698 resp_data = NULL;
ocomeni 118:8df0e9c2ee3f 1699 lastHttpRespTime = Kernel::get_ms_count();
ocomeni 87:99b37d26ff2a 1700 }