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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Apr 21 13:06:41 2019 +0000
Revision:
98:65c2333a38b6
Parent:
97:0aa46e5ef15d
Child:
99:05398b3184f8
now working with to stage of 3 tries of hello msg

Who changed what in which revision?

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