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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Wed May 08 19:38:35 2019 +0000
Branch:
PassingRegression
Revision:
109:c274780ff609
Parent:
108:3c8fb2c6e7bf
Child:
110:c722dda4f2ff
1. updated HTTP packet state to monitor for multiple packets; 2. fixed bug causing crash (was canceling an active task on event queue); 2. updates to watchdog. added ticker.;

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