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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Mon May 06 16:44:40 2019 +0000
Branch:
PassingRegression
Revision:
107:f1a83fd41b17
Parent:
105:e5ce023eee93
Child:
108:3c8fb2c6e7bf
now succefully sending and responsing to Box cloud message up till BLE ID and OTP request

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