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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Mar 24 17:32:06 2019 +0000
Revision:
82:10072c1794d3
Parent:
81:637a87eb8170
Child:
83:9c271a50a70b
wifi configuration setup implemented

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 79:a2187bbfa407 4
ocomeni 74:f26e846adfe9 5
ocomeni 78:07bb86e3ce14 6 ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral,
ocomeni 79:a2187bbfa407 7 events::EventQueue &event_queue, WiFiManager *wifi,
ocomeni 79:a2187bbfa407 8 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool,
ocomeni 80:e8f0e92e3ac9 9 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
ocomeni 81:637a87eb8170 10 MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool,
ocomeni 81:637a87eb8170 11 Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue,
ocomeni 80:e8f0e92e3ac9 12 MemoryPool<wifi_data_msg_t, 4> *aT2WiFiDatamPool,
ocomeni 80:e8f0e92e3ac9 13 Queue<wifi_data_msg_t, 4> *aT2WiFiDataQueue,
ocomeni 81:637a87eb8170 14 MemoryPool<at_data_msg_t, 4> *wiFi2ATDatamPool,
ocomeni 81:637a87eb8170 15 Queue<at_data_msg_t, 4> *wiFi2ATDataQueue,
ocomeni 79:a2187bbfa407 16 bool debug)
ocomeni 74:f26e846adfe9 17 :
ocomeni 74:f26e846adfe9 18 _serial(tx, rx, DEFAULT_BAUD_RATE),
ocomeni 78:07bb86e3ce14 19 blePeripheral(blePeripheral),
ocomeni 79:a2187bbfa407 20 _event_queue(event_queue),
ocomeni 79:a2187bbfa407 21 wiFiManager(wiFiManager),
ocomeni 79:a2187bbfa407 22 _aT2WiFimPool(aT2WiFimPool),
ocomeni 80:e8f0e92e3ac9 23 _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
ocomeni 80:e8f0e92e3ac9 24
ocomeni 80:e8f0e92e3ac9 25 _wiFi2ATmPool(wiFi2ATmPool),
ocomeni 80:e8f0e92e3ac9 26 _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
ocomeni 80:e8f0e92e3ac9 27
ocomeni 80:e8f0e92e3ac9 28 _aT2WiFiDatamPool(aT2WiFiDatamPool),
ocomeni 80:e8f0e92e3ac9 29 _aT2WiFiDataQueue(aT2WiFiDataQueue),
ocomeni 80:e8f0e92e3ac9 30
ocomeni 80:e8f0e92e3ac9 31 _wiFi2ATDatamPool(wiFi2ATDatamPool),
ocomeni 80:e8f0e92e3ac9 32 _wiFi2ATDataQueue(wiFi2ATDataQueue),
ocomeni 80:e8f0e92e3ac9 33
ocomeni 80:e8f0e92e3ac9 34 _parser(&_serial)
ocomeni 80:e8f0e92e3ac9 35
ocomeni 74:f26e846adfe9 36
ocomeni 74:f26e846adfe9 37 {
ocomeni 74:f26e846adfe9 38 // constructor
ocomeni 74:f26e846adfe9 39 _serial.set_baud(DEFAULT_BAUD_RATE);
ocomeni 74:f26e846adfe9 40 _parser.debug_on(debug);
ocomeni 74:f26e846adfe9 41 _parser.set_delimiter("\r\n");
ocomeni 75:08eff6258e1b 42 _parser.send("+STARTUP");
ocomeni 81:637a87eb8170 43 _parser.oob("AT\n", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 75:08eff6258e1b 44 _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off));
ocomeni 75:08eff6258e1b 45 _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on));
ocomeni 75:08eff6258e1b 46 _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup));
ocomeni 75:08eff6258e1b 47
ocomeni 75:08eff6258e1b 48 _parser.oob("ATO", callback(this, &ATCmdManager::_oob_data_mode));
ocomeni 75:08eff6258e1b 49 _parser.oob("AT+UMLA", callback(this, &ATCmdManager::_oob_get_mac_addr));
ocomeni 75:08eff6258e1b 50 _parser.oob("AT+UBTLE?", callback(this, &ATCmdManager::_oob_get_ble_role));
ocomeni 75:08eff6258e1b 51 _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri));
ocomeni 75:08eff6258e1b 52 _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot));
ocomeni 75:08eff6258e1b 53 _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
ocomeni 82:10072c1794d3 54 _parser.oob("AT+UWSCAN", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
ocomeni 79:a2187bbfa407 55 _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
ocomeni 81:637a87eb8170 56 _parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
ocomeni 81:637a87eb8170 57 _parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection));
ocomeni 82:10072c1794d3 58 _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID));
ocomeni 82:10072c1794d3 59 _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD));
ocomeni 82:10072c1794d3 60 _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity));
ocomeni 81:637a87eb8170 61
ocomeni 82:10072c1794d3 62 //AT+UWSC=0,2,<SSID>
ocomeni 82:10072c1794d3 63 //AT+UWSC=0,8,<PWD>
ocomeni 82:10072c1794d3 64 //AT+UWSC=0,5,2 (WPA)
ocomeni 75:08eff6258e1b 65 //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr));
ocomeni 74:f26e846adfe9 66 printf("\n --- ATCmdManager constructor completed ---\n");
ocomeni 81:637a87eb8170 67 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 68 dataMode = AT_CMD_DATA_MODE;
ocomeni 75:08eff6258e1b 69 //AT+UMRS=230400,2,8,1,1,1
ocomeni 74:f26e846adfe9 70 // AT+UBTLE
ocomeni 74:f26e846adfe9 71 }
ocomeni 74:f26e846adfe9 72
ocomeni 74:f26e846adfe9 73
ocomeni 74:f26e846adfe9 74 void ATCmdManager::runMain(){
ocomeni 74:f26e846adfe9 75 while(true){
ocomeni 74:f26e846adfe9 76 _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
ocomeni 81:637a87eb8170 77 wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time
ocomeni 81:637a87eb8170 78 processResponses();
ocomeni 74:f26e846adfe9 79 }
ocomeni 74:f26e846adfe9 80
ocomeni 74:f26e846adfe9 81 }
ocomeni 74:f26e846adfe9 82
ocomeni 81:637a87eb8170 83 void ATCmdManager::processResponses(){
ocomeni 81:637a87eb8170 84 dequeueATresponse();
ocomeni 81:637a87eb8170 85 switch(at_resp){
ocomeni 81:637a87eb8170 86 case AT_RESP_NONE:
ocomeni 81:637a87eb8170 87 // IDLE response state
ocomeni 81:637a87eb8170 88 break;
ocomeni 81:637a87eb8170 89 case AT_SCAN_RESP:
ocomeni 81:637a87eb8170 90 // AT_SCAN_RESP response state
ocomeni 81:637a87eb8170 91 _smutex.lock();
ocomeni 81:637a87eb8170 92 printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n");
ocomeni 81:637a87eb8170 93 _parser.send("OK\n");
ocomeni 81:637a87eb8170 94 _smutex.unlock();
ocomeni 81:637a87eb8170 95 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 96 break;
ocomeni 81:637a87eb8170 97 case AT_DETAILED_SCAN_RESP:
ocomeni 81:637a87eb8170 98 // AT_DETAILED_SCAN_RESP response state
ocomeni 81:637a87eb8170 99 _smutex.lock();
ocomeni 81:637a87eb8170 100 printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n");
ocomeni 81:637a87eb8170 101 _parser.send("OK\n");
ocomeni 81:637a87eb8170 102 _smutex.unlock();
ocomeni 81:637a87eb8170 103 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 104 break;
ocomeni 81:637a87eb8170 105 case AT_CONNECT_RESP:
ocomeni 81:637a87eb8170 106 // AT_CONNECT_RESP response state
ocomeni 81:637a87eb8170 107 _smutex.lock();
ocomeni 81:637a87eb8170 108 printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n");
ocomeni 81:637a87eb8170 109 _parser.send("OK\n");
ocomeni 81:637a87eb8170 110 _smutex.unlock();
ocomeni 81:637a87eb8170 111 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 112 break;
ocomeni 81:637a87eb8170 113 case AT_DISCONNECT_RESP:
ocomeni 81:637a87eb8170 114 // AT_DISCONNECT_RESP response state
ocomeni 81:637a87eb8170 115 _smutex.lock();
ocomeni 81:637a87eb8170 116 printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n");
ocomeni 81:637a87eb8170 117 _parser.send("OK\n");
ocomeni 81:637a87eb8170 118 _smutex.unlock();
ocomeni 81:637a87eb8170 119 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 120 break;
ocomeni 82:10072c1794d3 121 case AT_CONFIG_RESP:
ocomeni 82:10072c1794d3 122 // AT_CONFIG_RESP response state
ocomeni 81:637a87eb8170 123 _smutex.lock();
ocomeni 82:10072c1794d3 124 printf("\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\n");
ocomeni 81:637a87eb8170 125 _parser.send("OK\n");
ocomeni 81:637a87eb8170 126 _smutex.unlock();
ocomeni 81:637a87eb8170 127 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 128 break;
ocomeni 81:637a87eb8170 129 case AT_HTTP_RESP:
ocomeni 81:637a87eb8170 130 // AT_HTTP_RESP response state
ocomeni 81:637a87eb8170 131 _smutex.lock();
ocomeni 81:637a87eb8170 132 printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
ocomeni 81:637a87eb8170 133 _parser.send("OK\n");
ocomeni 81:637a87eb8170 134 _smutex.unlock();
ocomeni 81:637a87eb8170 135 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 136 break;
ocomeni 81:637a87eb8170 137 default:
ocomeni 81:637a87eb8170 138 // UNKNOWN response state
ocomeni 82:10072c1794d3 139 _smutex.lock();
ocomeni 82:10072c1794d3 140 printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\n");
ocomeni 82:10072c1794d3 141 _parser.send("OK\n");
ocomeni 82:10072c1794d3 142 _smutex.unlock();
ocomeni 81:637a87eb8170 143 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 144 break;
ocomeni 81:637a87eb8170 145 }
ocomeni 81:637a87eb8170 146 }
ocomeni 81:637a87eb8170 147
ocomeni 81:637a87eb8170 148
ocomeni 81:637a87eb8170 149
ocomeni 74:f26e846adfe9 150
ocomeni 74:f26e846adfe9 151 // OOB processing
ocomeni 74:f26e846adfe9 152 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
ocomeni 74:f26e846adfe9 153 set_timeout(timeout);
ocomeni 74:f26e846adfe9 154 // Poll for inbound packets
ocomeni 74:f26e846adfe9 155 while (_parser.process_oob() && all) {
ocomeni 74:f26e846adfe9 156 }
ocomeni 74:f26e846adfe9 157 set_timeout();
ocomeni 74:f26e846adfe9 158 }
ocomeni 74:f26e846adfe9 159
ocomeni 74:f26e846adfe9 160
ocomeni 74:f26e846adfe9 161 // OOB message handlers
ocomeni 74:f26e846adfe9 162 void ATCmdManager::_oob_startup_hdlr(){
ocomeni 74:f26e846adfe9 163 }
ocomeni 74:f26e846adfe9 164
ocomeni 81:637a87eb8170 165 void ATCmdManager::_oob_ok_hdlr(){
ocomeni 81:637a87eb8170 166 _smutex.lock();
ocomeni 81:637a87eb8170 167 _parser.send("OK\n");
ocomeni 81:637a87eb8170 168 _smutex.unlock();
ocomeni 81:637a87eb8170 169 }
ocomeni 81:637a87eb8170 170
ocomeni 74:f26e846adfe9 171
ocomeni 74:f26e846adfe9 172 void ATCmdManager::_oob_bleRole_hdlr(){
ocomeni 74:f26e846adfe9 173 }
ocomeni 74:f26e846adfe9 174
ocomeni 74:f26e846adfe9 175
ocomeni 74:f26e846adfe9 176 void ATCmdManager::_oob_wifiMode_err(){
ocomeni 74:f26e846adfe9 177 }
ocomeni 74:f26e846adfe9 178
ocomeni 74:f26e846adfe9 179
ocomeni 74:f26e846adfe9 180 void ATCmdManager::_oob_conn_already(){
ocomeni 74:f26e846adfe9 181 }
ocomeni 74:f26e846adfe9 182
ocomeni 74:f26e846adfe9 183
ocomeni 74:f26e846adfe9 184 void ATCmdManager::_oob_err(){
ocomeni 74:f26e846adfe9 185 }
ocomeni 74:f26e846adfe9 186
ocomeni 79:a2187bbfa407 187 void ATCmdManager::_oob_get_fw_ver()
ocomeni 79:a2187bbfa407 188 {
ocomeni 79:a2187bbfa407 189 }
ocomeni 79:a2187bbfa407 190
ocomeni 79:a2187bbfa407 191
ocomeni 75:08eff6258e1b 192 void ATCmdManager::_oob_uart_setup(){
ocomeni 75:08eff6258e1b 193 int uOpts[NUM_UART_OPTIONS];
ocomeni 75:08eff6258e1b 194 //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
ocomeni 75:08eff6258e1b 195 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 196 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 197 //AT+UMRS=230400,2,8,1,1,1
ocomeni 81:637a87eb8170 198 printf("\n Changing Baud Rate to %d\n", uOpts[0]);
ocomeni 81:637a87eb8170 199
ocomeni 81:637a87eb8170 200 _serial.set_baud(uOpts[0]);
ocomeni 81:637a87eb8170 201 printf("\n Baud Rate now %d\n", uOpts[0]);
ocomeni 81:637a87eb8170 202
ocomeni 75:08eff6258e1b 203 } else {
ocomeni 75:08eff6258e1b 204 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 205 }
ocomeni 75:08eff6258e1b 206 }
ocomeni 75:08eff6258e1b 207
ocomeni 74:f26e846adfe9 208 void ATCmdManager::set_timeout(uint32_t timeout_ms)
ocomeni 74:f26e846adfe9 209 {
ocomeni 74:f26e846adfe9 210 _parser.set_timeout(timeout_ms);
ocomeni 74:f26e846adfe9 211 }
ocomeni 74:f26e846adfe9 212
ocomeni 75:08eff6258e1b 213
ocomeni 75:08eff6258e1b 214 void ATCmdManager::_oob_echo_off()
ocomeni 75:08eff6258e1b 215 {
ocomeni 75:08eff6258e1b 216 _smutex.lock();
ocomeni 75:08eff6258e1b 217 printf("\n Received ATEO OOB command!!\n");
ocomeni 75:08eff6258e1b 218 printf("\n turning echo OFF!!\n");
ocomeni 75:08eff6258e1b 219 _parser.debug_on(false);
ocomeni 75:08eff6258e1b 220 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 221 _smutex.unlock();
ocomeni 75:08eff6258e1b 222 }
ocomeni 75:08eff6258e1b 223
ocomeni 75:08eff6258e1b 224
ocomeni 75:08eff6258e1b 225 void ATCmdManager::_oob_echo_on()
ocomeni 75:08eff6258e1b 226 {
ocomeni 75:08eff6258e1b 227 _smutex.lock();
ocomeni 75:08eff6258e1b 228 printf("\n Received ATE1 OOB command!!\n");
ocomeni 75:08eff6258e1b 229 printf("\n turning echo ON!!\n");
ocomeni 75:08eff6258e1b 230 _parser.debug_on(true);
ocomeni 75:08eff6258e1b 231 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 232 _smutex.unlock();
ocomeni 75:08eff6258e1b 233 }
ocomeni 75:08eff6258e1b 234
ocomeni 75:08eff6258e1b 235
ocomeni 75:08eff6258e1b 236 void ATCmdManager::_oob_data_mode(){
ocomeni 75:08eff6258e1b 237 int dataMode;
ocomeni 75:08eff6258e1b 238 _smutex.lock();
ocomeni 75:08eff6258e1b 239 printf("\n Received EDM mode command!!\n");
ocomeni 75:08eff6258e1b 240 if(_parser.scanf("%d", &dataMode) >0) {
ocomeni 75:08eff6258e1b 241 printf("\nATCmdParser: Data mode=%d\n", dataMode);
ocomeni 75:08eff6258e1b 242 switch(dataMode)
ocomeni 75:08eff6258e1b 243 {
ocomeni 75:08eff6258e1b 244 case 0:
ocomeni 75:08eff6258e1b 245 printf("\nATCmdParser: Command Mode request received");
ocomeni 81:637a87eb8170 246 dataMode = AT_CMD_DATA_MODE;
ocomeni 75:08eff6258e1b 247 break;
ocomeni 75:08eff6258e1b 248 case 1:
ocomeni 75:08eff6258e1b 249 printf("\nATCmdParser: Data Mode request received");
ocomeni 81:637a87eb8170 250 dataMode = AT_STD_DATA_MODE;
ocomeni 75:08eff6258e1b 251 break;
ocomeni 75:08eff6258e1b 252 case 2:
ocomeni 75:08eff6258e1b 253 printf("\nATCmdParser: Extended data Mode request received");
ocomeni 81:637a87eb8170 254 dataMode = AT_EXT_DATA_MODE;
ocomeni 75:08eff6258e1b 255 break;
ocomeni 75:08eff6258e1b 256 default:
ocomeni 75:08eff6258e1b 257 printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 258 break;
ocomeni 75:08eff6258e1b 259 }
ocomeni 75:08eff6258e1b 260 } else {
ocomeni 75:08eff6258e1b 261 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 262 }
ocomeni 75:08eff6258e1b 263 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 264 _smutex.unlock();
ocomeni 75:08eff6258e1b 265 }
ocomeni 75:08eff6258e1b 266
ocomeni 75:08eff6258e1b 267 void ATCmdManager::_oob_get_mac_addr(){
ocomeni 75:08eff6258e1b 268 int bleOrWifi;
ocomeni 75:08eff6258e1b 269 _smutex.lock();
ocomeni 75:08eff6258e1b 270 if(_parser.scanf("=%d", &bleOrWifi) >0) {
ocomeni 75:08eff6258e1b 271 switch(bleOrWifi)
ocomeni 75:08eff6258e1b 272 {
ocomeni 75:08eff6258e1b 273 case 1:
ocomeni 75:08eff6258e1b 274 printf("\nATCmdParser: BLE MAC Address request received");
ocomeni 75:08eff6258e1b 275 break;
ocomeni 75:08eff6258e1b 276 case 2:
ocomeni 75:08eff6258e1b 277 printf("\nATCmdParser: WiFi MAC Address request received");
ocomeni 75:08eff6258e1b 278 break;
ocomeni 75:08eff6258e1b 279 default:
ocomeni 75:08eff6258e1b 280 printf("\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 281 break;
ocomeni 75:08eff6258e1b 282 }
ocomeni 75:08eff6258e1b 283 } else {
ocomeni 75:08eff6258e1b 284 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 285 }
ocomeni 75:08eff6258e1b 286 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 287 _smutex.unlock();
ocomeni 75:08eff6258e1b 288 }
ocomeni 75:08eff6258e1b 289
ocomeni 75:08eff6258e1b 290 void ATCmdManager::_oob_get_ble_role(){
ocomeni 75:08eff6258e1b 291 _smutex.lock();
ocomeni 75:08eff6258e1b 292 printf("\n Received get BLE role command!!\n");
ocomeni 75:08eff6258e1b 293 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 294 _smutex.unlock();
ocomeni 75:08eff6258e1b 295 }
ocomeni 75:08eff6258e1b 296
ocomeni 75:08eff6258e1b 297 void ATCmdManager::_oob_ena_ble_peri(){
ocomeni 75:08eff6258e1b 298 _smutex.lock();
ocomeni 75:08eff6258e1b 299 printf("\n Received enable BLE Peripheral command!!\n");
ocomeni 75:08eff6258e1b 300 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 301 _smutex.unlock();
ocomeni 75:08eff6258e1b 302 }
ocomeni 75:08eff6258e1b 303
ocomeni 75:08eff6258e1b 304 void ATCmdManager::_oob_reboot(){
ocomeni 75:08eff6258e1b 305 _smutex.lock();
ocomeni 75:08eff6258e1b 306 printf("\n Received reboot command!!\n");
ocomeni 75:08eff6258e1b 307 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 308 _parser.send("System Resetting....\n");
ocomeni 75:08eff6258e1b 309 system_reset();
ocomeni 75:08eff6258e1b 310 _smutex.unlock();
ocomeni 75:08eff6258e1b 311 }
ocomeni 75:08eff6258e1b 312
ocomeni 79:a2187bbfa407 313 const char * ATCmdManager::sec2str(nsapi_security_t sec)
ocomeni 79:a2187bbfa407 314 {
ocomeni 79:a2187bbfa407 315 switch (sec) {
ocomeni 79:a2187bbfa407 316 case NSAPI_SECURITY_NONE:
ocomeni 79:a2187bbfa407 317 return "None";
ocomeni 79:a2187bbfa407 318 case NSAPI_SECURITY_WEP:
ocomeni 79:a2187bbfa407 319 return "WEP";
ocomeni 79:a2187bbfa407 320 case NSAPI_SECURITY_WPA:
ocomeni 79:a2187bbfa407 321 return "WPA";
ocomeni 79:a2187bbfa407 322 case NSAPI_SECURITY_WPA2:
ocomeni 79:a2187bbfa407 323 return "WPA2";
ocomeni 79:a2187bbfa407 324 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 79:a2187bbfa407 325 return "WPA/WPA2";
ocomeni 79:a2187bbfa407 326 case NSAPI_SECURITY_UNKNOWN:
ocomeni 79:a2187bbfa407 327 default:
ocomeni 79:a2187bbfa407 328 return "Unknown";
ocomeni 79:a2187bbfa407 329 }
ocomeni 79:a2187bbfa407 330 }
ocomeni 81:637a87eb8170 331
ocomeni 81:637a87eb8170 332 bool ATCmdManager::setNextResponse(at_cmd_resp_t resp)
ocomeni 81:637a87eb8170 333 {
ocomeni 81:637a87eb8170 334 if(at_resp == AT_RESP_NONE){
ocomeni 81:637a87eb8170 335 at_resp = resp;
ocomeni 81:637a87eb8170 336 return true; // success
ocomeni 81:637a87eb8170 337 }
ocomeni 81:637a87eb8170 338 return false; // wiFiManager busy
ocomeni 79:a2187bbfa407 339 }
ocomeni 79:a2187bbfa407 340
ocomeni 81:637a87eb8170 341
ocomeni 79:a2187bbfa407 342 void ATCmdManager::_oob_scanWiFiNetworks(){
ocomeni 75:08eff6258e1b 343 _smutex.lock();
ocomeni 79:a2187bbfa407 344 printf("\n Received scanWiFiNetworks command!!\n");
ocomeni 75:08eff6258e1b 345 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 346 _smutex.unlock();
ocomeni 79:a2187bbfa407 347 wifi_cmd_t cmd = WIFI_CMD_SCAN;
ocomeni 79:a2187bbfa407 348 // queue next command
ocomeni 79:a2187bbfa407 349 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 350 return;
ocomeni 75:08eff6258e1b 351 }
ocomeni 75:08eff6258e1b 352
ocomeni 79:a2187bbfa407 353 void ATCmdManager::_oob_connect2WiFiNetwork()
ocomeni 79:a2187bbfa407 354 {
ocomeni 81:637a87eb8170 355 wifi_cmd_t cmd = WIFI_CMD_CONNECT;
ocomeni 81:637a87eb8170 356 // queue next command
ocomeni 81:637a87eb8170 357 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 358 return;
ocomeni 79:a2187bbfa407 359 }
ocomeni 79:a2187bbfa407 360
ocomeni 79:a2187bbfa407 361
ocomeni 79:a2187bbfa407 362 void ATCmdManager::_oob_disconnectWiFiNetwork()
ocomeni 79:a2187bbfa407 363 {
ocomeni 81:637a87eb8170 364 wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
ocomeni 81:637a87eb8170 365 // queue next command
ocomeni 81:637a87eb8170 366 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 367 return;
ocomeni 81:637a87eb8170 368 }
ocomeni 81:637a87eb8170 369
ocomeni 81:637a87eb8170 370 void ATCmdManager::_oob_setupInternetConnection()
ocomeni 81:637a87eb8170 371 {
ocomeni 81:637a87eb8170 372 char url[200];
ocomeni 81:637a87eb8170 373 int n;
ocomeni 81:637a87eb8170 374 internet_config_t internet_config;
ocomeni 81:637a87eb8170 375 _smutex.lock();
ocomeni 82:10072c1794d3 376 n = _parser.scanf("=%1d,%199[^,],%1d", &internet_config.peer_id,
ocomeni 81:637a87eb8170 377 url,
ocomeni 81:637a87eb8170 378 &internet_config.connectionScheme);
ocomeni 81:637a87eb8170 379 if(n>0)
ocomeni 81:637a87eb8170 380 {
ocomeni 81:637a87eb8170 381 internet_config.url = url;
ocomeni 81:637a87eb8170 382 printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 81:637a87eb8170 383 internet_config.url.c_str(),
ocomeni 81:637a87eb8170 384 internet_config.connectionScheme);
ocomeni 81:637a87eb8170 385 // package and send on wifi data queue
ocomeni 81:637a87eb8170 386 wifi_data_msg_t data_msg;
ocomeni 81:637a87eb8170 387 data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG;
ocomeni 81:637a87eb8170 388 data_msg.dataLen = sizeof(internet_config) + internet_config.url.length();
ocomeni 81:637a87eb8170 389 memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
ocomeni 82:10072c1794d3 390 // queue next data request
ocomeni 81:637a87eb8170 391 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 392 _parser.send("OK\n");
ocomeni 81:637a87eb8170 393 } else {
ocomeni 81:637a87eb8170 394 printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n);
ocomeni 82:10072c1794d3 395 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 396 }
ocomeni 82:10072c1794d3 397 _smutex.unlock();
ocomeni 82:10072c1794d3 398 }
ocomeni 82:10072c1794d3 399
ocomeni 82:10072c1794d3 400 wifi_config_t ATCmdManager::init_wifi_config()
ocomeni 82:10072c1794d3 401 {
ocomeni 82:10072c1794d3 402 wifi_config_t wifi_cfg;
ocomeni 82:10072c1794d3 403 wifi_cfg.ssid[0] = NULL;
ocomeni 82:10072c1794d3 404 wifi_cfg.pass[0] = NULL;
ocomeni 82:10072c1794d3 405 wifi_cfg.security = NSAPI_SECURITY_UNKNOWN;
ocomeni 82:10072c1794d3 406 return wifi_cfg;
ocomeni 82:10072c1794d3 407 }
ocomeni 82:10072c1794d3 408
ocomeni 82:10072c1794d3 409 int ATCmdManager::readBytes(uint8_t *buf, int maxBytes)
ocomeni 82:10072c1794d3 410 {
ocomeni 82:10072c1794d3 411 int c;
ocomeni 82:10072c1794d3 412 int sptr = 0;
ocomeni 82:10072c1794d3 413 for(int i=0;i<maxBytes;i++){
ocomeni 82:10072c1794d3 414 c = _parser.getc();
ocomeni 82:10072c1794d3 415 if(c==-1){
ocomeni 82:10072c1794d3 416 buf[sptr] = '\0'; // null terminate if string
ocomeni 82:10072c1794d3 417 return i;
ocomeni 82:10072c1794d3 418 }
ocomeni 82:10072c1794d3 419 if(c != ',' && c!= '"'){
ocomeni 82:10072c1794d3 420 buf[sptr++] = (uint8_t) c;
ocomeni 82:10072c1794d3 421 }
ocomeni 81:637a87eb8170 422 }
ocomeni 82:10072c1794d3 423 }
ocomeni 82:10072c1794d3 424
ocomeni 82:10072c1794d3 425 void ATCmdManager::_oob_setWiFiSSID()
ocomeni 82:10072c1794d3 426 {
ocomeni 82:10072c1794d3 427 int n;
ocomeni 82:10072c1794d3 428 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 82:10072c1794d3 429 _smutex.lock();
ocomeni 82:10072c1794d3 430 n = readBytes((uint8_t *)wifi_cfg.ssid, 32);
ocomeni 82:10072c1794d3 431 printf("[ATCMD MAN]: number of bytes read = %d\n", n);
ocomeni 82:10072c1794d3 432 if(n>0)
ocomeni 82:10072c1794d3 433 {
ocomeni 82:10072c1794d3 434 printf("[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid);
ocomeni 82:10072c1794d3 435 // package and send on wifi data queue
ocomeni 82:10072c1794d3 436 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 437 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 438 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 439 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 440 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 441 _parser.send("OK\n");
ocomeni 82:10072c1794d3 442 } else {
ocomeni 82:10072c1794d3 443 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 444 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 445 }
ocomeni 82:10072c1794d3 446 _smutex.unlock();
ocomeni 82:10072c1794d3 447 }
ocomeni 82:10072c1794d3 448
ocomeni 82:10072c1794d3 449 void ATCmdManager::_oob_setWiFiPWD()
ocomeni 82:10072c1794d3 450 {
ocomeni 82:10072c1794d3 451 int n;
ocomeni 82:10072c1794d3 452 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 82:10072c1794d3 453 _smutex.lock();
ocomeni 82:10072c1794d3 454 //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass);
ocomeni 82:10072c1794d3 455 n = readBytes((uint8_t *)wifi_cfg.pass, 32);
ocomeni 82:10072c1794d3 456 if(n>0)
ocomeni 82:10072c1794d3 457 {
ocomeni 82:10072c1794d3 458 printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass);
ocomeni 82:10072c1794d3 459 // package and send on wifi data queue
ocomeni 82:10072c1794d3 460 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 461 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 462 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 463 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 464 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 465 _parser.send("OK\n");
ocomeni 82:10072c1794d3 466 } else {
ocomeni 82:10072c1794d3 467 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 468 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 469 }
ocomeni 82:10072c1794d3 470 _smutex.unlock();
ocomeni 82:10072c1794d3 471 }
ocomeni 82:10072c1794d3 472
ocomeni 82:10072c1794d3 473 void ATCmdManager::_oob_setWiFiSecurity()
ocomeni 82:10072c1794d3 474 {
ocomeni 82:10072c1794d3 475 int n;
ocomeni 82:10072c1794d3 476 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 82:10072c1794d3 477 _smutex.lock();
ocomeni 82:10072c1794d3 478 n = _parser.scanf(",%d", &wifi_cfg.security);
ocomeni 82:10072c1794d3 479 if(n>0)
ocomeni 82:10072c1794d3 480 {
ocomeni 82:10072c1794d3 481 printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
ocomeni 82:10072c1794d3 482 // package and send on wifi data queue
ocomeni 82:10072c1794d3 483 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 484 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 485 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 486 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 487 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 488 _parser.send("OK\n");
ocomeni 82:10072c1794d3 489 } else {
ocomeni 82:10072c1794d3 490 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 491 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 492 }
ocomeni 81:637a87eb8170 493 _smutex.unlock();
ocomeni 79:a2187bbfa407 494 }
ocomeni 81:637a87eb8170 495
ocomeni 81:637a87eb8170 496 bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
ocomeni 81:637a87eb8170 497 wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
ocomeni 81:637a87eb8170 498 wifiCmd->wifi_cmd = cmd;
ocomeni 81:637a87eb8170 499 _aT2WiFiCmdQueue->put(wifiCmd);
ocomeni 81:637a87eb8170 500 return true;
ocomeni 81:637a87eb8170 501 }
ocomeni 81:637a87eb8170 502
ocomeni 81:637a87eb8170 503 bool ATCmdManager::dequeueATresponse(){
ocomeni 81:637a87eb8170 504 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 505 osEvent evt = _wiFi2ATCmdQueue->get(0);
ocomeni 81:637a87eb8170 506 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 507 at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
ocomeni 81:637a87eb8170 508 setNextResponse(resp->at_resp);
ocomeni 81:637a87eb8170 509 _wiFi2ATmPool->free(resp);
ocomeni 81:637a87eb8170 510 }
ocomeni 81:637a87eb8170 511 return true;
ocomeni 81:637a87eb8170 512 }
ocomeni 81:637a87eb8170 513
ocomeni 81:637a87eb8170 514 bool ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
ocomeni 81:637a87eb8170 515 wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
ocomeni 81:637a87eb8170 516 wifiData->wifi_cmd = data_req.wifi_cmd;
ocomeni 81:637a87eb8170 517 wifiData->dataLen = data_req.dataLen;
ocomeni 81:637a87eb8170 518 memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen);
ocomeni 81:637a87eb8170 519 _aT2WiFiDataQueue->put(wifiData);
ocomeni 81:637a87eb8170 520 printf("[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd);
ocomeni 81:637a87eb8170 521 return true;
ocomeni 81:637a87eb8170 522 }
ocomeni 81:637a87eb8170 523
ocomeni 81:637a87eb8170 524 bool ATCmdManager::dequeueATdataResponse(){
ocomeni 81:637a87eb8170 525 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 526 osEvent evt = _wiFi2ATDataQueue->get(0);
ocomeni 81:637a87eb8170 527 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 528 resp_data = (at_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 529 setNextResponse(resp_data->at_resp);
ocomeni 81:637a87eb8170 530 //_wiFi2ATDatamPool->free(resp_data);
ocomeni 81:637a87eb8170 531 }
ocomeni 81:637a87eb8170 532 return true;
ocomeni 81:637a87eb8170 533 }
ocomeni 81:637a87eb8170 534
ocomeni 81:637a87eb8170 535
ocomeni 81:637a87eb8170 536