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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Wed Mar 20 21:02:47 2019 +0000
Revision:
79:a2187bbfa407
Parent:
78:07bb86e3ce14
Child:
80:e8f0e92e3ac9
now have working mechanism for comms between threads using memory pool and queue.; Next stage expand this for data.

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 79:a2187bbfa407 9 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
ocomeni 79:a2187bbfa407 10 bool debug)
ocomeni 74:f26e846adfe9 11 :
ocomeni 74:f26e846adfe9 12 _serial(tx, rx, DEFAULT_BAUD_RATE),
ocomeni 78:07bb86e3ce14 13 _parser(&_serial),
ocomeni 78:07bb86e3ce14 14 blePeripheral(blePeripheral),
ocomeni 79:a2187bbfa407 15 _event_queue(event_queue),
ocomeni 79:a2187bbfa407 16 wiFiManager(wiFiManager),
ocomeni 79:a2187bbfa407 17 _aT2WiFimPool(aT2WiFimPool),
ocomeni 79:a2187bbfa407 18 _aT2WiFiCmdQueue(aT2WiFiCmdQueue)
ocomeni 74:f26e846adfe9 19
ocomeni 74:f26e846adfe9 20 {
ocomeni 74:f26e846adfe9 21 // constructor
ocomeni 74:f26e846adfe9 22 _serial.set_baud(DEFAULT_BAUD_RATE);
ocomeni 74:f26e846adfe9 23 _parser.debug_on(debug);
ocomeni 74:f26e846adfe9 24 _parser.set_delimiter("\r\n");
ocomeni 75:08eff6258e1b 25 _parser.send("+STARTUP");
ocomeni 75:08eff6258e1b 26 _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off));
ocomeni 75:08eff6258e1b 27 _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on));
ocomeni 75:08eff6258e1b 28 _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup));
ocomeni 75:08eff6258e1b 29
ocomeni 75:08eff6258e1b 30 _parser.oob("ATO", callback(this, &ATCmdManager::_oob_data_mode));
ocomeni 75:08eff6258e1b 31 _parser.oob("AT+UMLA", callback(this, &ATCmdManager::_oob_get_mac_addr));
ocomeni 75:08eff6258e1b 32 _parser.oob("AT+UBTLE?", callback(this, &ATCmdManager::_oob_get_ble_role));
ocomeni 75:08eff6258e1b 33 _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri));
ocomeni 75:08eff6258e1b 34 _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot));
ocomeni 75:08eff6258e1b 35 _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
ocomeni 79:a2187bbfa407 36 _parser.oob("AT+UWSC=", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
ocomeni 79:a2187bbfa407 37 _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
ocomeni 75:08eff6258e1b 38
ocomeni 75:08eff6258e1b 39
ocomeni 75:08eff6258e1b 40 //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr));
ocomeni 74:f26e846adfe9 41 printf("\n --- ATCmdManager constructor completed ---\n");
ocomeni 75:08eff6258e1b 42
ocomeni 75:08eff6258e1b 43 //AT+UMRS=230400,2,8,1,1,1
ocomeni 74:f26e846adfe9 44 // AT+UBTLE
ocomeni 74:f26e846adfe9 45 }
ocomeni 74:f26e846adfe9 46
ocomeni 74:f26e846adfe9 47
ocomeni 74:f26e846adfe9 48 void ATCmdManager::runMain(){
ocomeni 74:f26e846adfe9 49 while(true){
ocomeni 74:f26e846adfe9 50 _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
ocomeni 74:f26e846adfe9 51 wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE to be
ocomeni 74:f26e846adfe9 52 }
ocomeni 74:f26e846adfe9 53
ocomeni 74:f26e846adfe9 54 }
ocomeni 74:f26e846adfe9 55
ocomeni 74:f26e846adfe9 56
ocomeni 74:f26e846adfe9 57 // OOB processing
ocomeni 74:f26e846adfe9 58 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
ocomeni 74:f26e846adfe9 59 set_timeout(timeout);
ocomeni 74:f26e846adfe9 60 // Poll for inbound packets
ocomeni 74:f26e846adfe9 61 while (_parser.process_oob() && all) {
ocomeni 74:f26e846adfe9 62 }
ocomeni 74:f26e846adfe9 63 set_timeout();
ocomeni 74:f26e846adfe9 64 }
ocomeni 74:f26e846adfe9 65
ocomeni 74:f26e846adfe9 66
ocomeni 74:f26e846adfe9 67 // OOB message handlers
ocomeni 74:f26e846adfe9 68 void ATCmdManager::_oob_startup_hdlr(){
ocomeni 74:f26e846adfe9 69 }
ocomeni 74:f26e846adfe9 70
ocomeni 74:f26e846adfe9 71
ocomeni 74:f26e846adfe9 72 void ATCmdManager::_oob_bleRole_hdlr(){
ocomeni 74:f26e846adfe9 73 }
ocomeni 74:f26e846adfe9 74
ocomeni 74:f26e846adfe9 75
ocomeni 74:f26e846adfe9 76 void ATCmdManager::_oob_wifiMode_err(){
ocomeni 74:f26e846adfe9 77 }
ocomeni 74:f26e846adfe9 78
ocomeni 74:f26e846adfe9 79
ocomeni 74:f26e846adfe9 80 void ATCmdManager::_oob_conn_already(){
ocomeni 74:f26e846adfe9 81 }
ocomeni 74:f26e846adfe9 82
ocomeni 74:f26e846adfe9 83
ocomeni 74:f26e846adfe9 84 void ATCmdManager::_oob_err(){
ocomeni 74:f26e846adfe9 85 }
ocomeni 74:f26e846adfe9 86
ocomeni 79:a2187bbfa407 87 void ATCmdManager::_oob_get_fw_ver()
ocomeni 79:a2187bbfa407 88 {
ocomeni 79:a2187bbfa407 89 }
ocomeni 79:a2187bbfa407 90
ocomeni 79:a2187bbfa407 91
ocomeni 75:08eff6258e1b 92 void ATCmdManager::_oob_uart_setup(){
ocomeni 75:08eff6258e1b 93 int uOpts[NUM_UART_OPTIONS];
ocomeni 75:08eff6258e1b 94 //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
ocomeni 75:08eff6258e1b 95 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 96 printf("\nATCmdParser: Uart Options=%d,%d,%d,%d,%d,%d\n", uOpts[0], uOpts[1], uOpts[2], uOpts[3], uOpts[4], uOpts[5]);
ocomeni 75:08eff6258e1b 97 } else {
ocomeni 75:08eff6258e1b 98 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 99 }
ocomeni 75:08eff6258e1b 100 }
ocomeni 75:08eff6258e1b 101
ocomeni 74:f26e846adfe9 102 void ATCmdManager::set_timeout(uint32_t timeout_ms)
ocomeni 74:f26e846adfe9 103 {
ocomeni 74:f26e846adfe9 104 _parser.set_timeout(timeout_ms);
ocomeni 74:f26e846adfe9 105 }
ocomeni 74:f26e846adfe9 106
ocomeni 75:08eff6258e1b 107
ocomeni 75:08eff6258e1b 108 void ATCmdManager::_oob_echo_off()
ocomeni 75:08eff6258e1b 109 {
ocomeni 75:08eff6258e1b 110 _smutex.lock();
ocomeni 75:08eff6258e1b 111 printf("\n Received ATEO OOB command!!\n");
ocomeni 75:08eff6258e1b 112 printf("\n turning echo OFF!!\n");
ocomeni 75:08eff6258e1b 113 _parser.debug_on(false);
ocomeni 75:08eff6258e1b 114 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 115 _smutex.unlock();
ocomeni 75:08eff6258e1b 116 }
ocomeni 75:08eff6258e1b 117
ocomeni 75:08eff6258e1b 118
ocomeni 75:08eff6258e1b 119 void ATCmdManager::_oob_echo_on()
ocomeni 75:08eff6258e1b 120 {
ocomeni 75:08eff6258e1b 121 _smutex.lock();
ocomeni 75:08eff6258e1b 122 printf("\n Received ATE1 OOB command!!\n");
ocomeni 75:08eff6258e1b 123 printf("\n turning echo ON!!\n");
ocomeni 75:08eff6258e1b 124 _parser.debug_on(true);
ocomeni 75:08eff6258e1b 125 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 126 _smutex.unlock();
ocomeni 75:08eff6258e1b 127 }
ocomeni 75:08eff6258e1b 128
ocomeni 75:08eff6258e1b 129
ocomeni 75:08eff6258e1b 130 void ATCmdManager::_oob_data_mode(){
ocomeni 75:08eff6258e1b 131 int dataMode;
ocomeni 75:08eff6258e1b 132 _smutex.lock();
ocomeni 75:08eff6258e1b 133 printf("\n Received EDM mode command!!\n");
ocomeni 75:08eff6258e1b 134 if(_parser.scanf("%d", &dataMode) >0) {
ocomeni 75:08eff6258e1b 135 printf("\nATCmdParser: Data mode=%d\n", dataMode);
ocomeni 75:08eff6258e1b 136 switch(dataMode)
ocomeni 75:08eff6258e1b 137 {
ocomeni 75:08eff6258e1b 138 case 0:
ocomeni 75:08eff6258e1b 139 printf("\nATCmdParser: Command Mode request received");
ocomeni 75:08eff6258e1b 140 break;
ocomeni 75:08eff6258e1b 141 case 1:
ocomeni 75:08eff6258e1b 142 printf("\nATCmdParser: Data Mode request received");
ocomeni 75:08eff6258e1b 143 break;
ocomeni 75:08eff6258e1b 144 case 2:
ocomeni 75:08eff6258e1b 145 printf("\nATCmdParser: Extended data Mode request received");
ocomeni 75:08eff6258e1b 146 break;
ocomeni 75:08eff6258e1b 147 default:
ocomeni 75:08eff6258e1b 148 printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 149 break;
ocomeni 75:08eff6258e1b 150 }
ocomeni 75:08eff6258e1b 151 } else {
ocomeni 75:08eff6258e1b 152 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 153 }
ocomeni 75:08eff6258e1b 154 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 155 _smutex.unlock();
ocomeni 75:08eff6258e1b 156 }
ocomeni 75:08eff6258e1b 157
ocomeni 75:08eff6258e1b 158 void ATCmdManager::_oob_get_mac_addr(){
ocomeni 75:08eff6258e1b 159 int bleOrWifi;
ocomeni 75:08eff6258e1b 160 _smutex.lock();
ocomeni 75:08eff6258e1b 161 if(_parser.scanf("=%d", &bleOrWifi) >0) {
ocomeni 75:08eff6258e1b 162 switch(bleOrWifi)
ocomeni 75:08eff6258e1b 163 {
ocomeni 75:08eff6258e1b 164 case 1:
ocomeni 75:08eff6258e1b 165 printf("\nATCmdParser: BLE MAC Address request received");
ocomeni 75:08eff6258e1b 166 break;
ocomeni 75:08eff6258e1b 167 case 2:
ocomeni 75:08eff6258e1b 168 printf("\nATCmdParser: WiFi MAC Address request received");
ocomeni 75:08eff6258e1b 169 break;
ocomeni 75:08eff6258e1b 170 default:
ocomeni 75:08eff6258e1b 171 printf("\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 172 break;
ocomeni 75:08eff6258e1b 173 }
ocomeni 75:08eff6258e1b 174 } else {
ocomeni 75:08eff6258e1b 175 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 176 }
ocomeni 75:08eff6258e1b 177 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 178 _smutex.unlock();
ocomeni 75:08eff6258e1b 179 }
ocomeni 75:08eff6258e1b 180
ocomeni 75:08eff6258e1b 181 void ATCmdManager::_oob_get_ble_role(){
ocomeni 75:08eff6258e1b 182 _smutex.lock();
ocomeni 75:08eff6258e1b 183 printf("\n Received get BLE role command!!\n");
ocomeni 75:08eff6258e1b 184 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 185 _smutex.unlock();
ocomeni 75:08eff6258e1b 186 }
ocomeni 75:08eff6258e1b 187
ocomeni 75:08eff6258e1b 188 void ATCmdManager::_oob_ena_ble_peri(){
ocomeni 75:08eff6258e1b 189 _smutex.lock();
ocomeni 75:08eff6258e1b 190 printf("\n Received enable BLE Peripheral command!!\n");
ocomeni 75:08eff6258e1b 191 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 192 _smutex.unlock();
ocomeni 75:08eff6258e1b 193 }
ocomeni 75:08eff6258e1b 194
ocomeni 75:08eff6258e1b 195 void ATCmdManager::_oob_reboot(){
ocomeni 75:08eff6258e1b 196 _smutex.lock();
ocomeni 75:08eff6258e1b 197 printf("\n Received reboot command!!\n");
ocomeni 75:08eff6258e1b 198 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 199 _parser.send("System Resetting....\n");
ocomeni 75:08eff6258e1b 200 system_reset();
ocomeni 75:08eff6258e1b 201 _smutex.unlock();
ocomeni 75:08eff6258e1b 202 }
ocomeni 75:08eff6258e1b 203
ocomeni 79:a2187bbfa407 204 const char * ATCmdManager::sec2str(nsapi_security_t sec)
ocomeni 79:a2187bbfa407 205 {
ocomeni 79:a2187bbfa407 206 switch (sec) {
ocomeni 79:a2187bbfa407 207 case NSAPI_SECURITY_NONE:
ocomeni 79:a2187bbfa407 208 return "None";
ocomeni 79:a2187bbfa407 209 case NSAPI_SECURITY_WEP:
ocomeni 79:a2187bbfa407 210 return "WEP";
ocomeni 79:a2187bbfa407 211 case NSAPI_SECURITY_WPA:
ocomeni 79:a2187bbfa407 212 return "WPA";
ocomeni 79:a2187bbfa407 213 case NSAPI_SECURITY_WPA2:
ocomeni 79:a2187bbfa407 214 return "WPA2";
ocomeni 79:a2187bbfa407 215 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 79:a2187bbfa407 216 return "WPA/WPA2";
ocomeni 79:a2187bbfa407 217 case NSAPI_SECURITY_UNKNOWN:
ocomeni 79:a2187bbfa407 218 default:
ocomeni 79:a2187bbfa407 219 return "Unknown";
ocomeni 79:a2187bbfa407 220 }
ocomeni 79:a2187bbfa407 221 }
ocomeni 79:a2187bbfa407 222 bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
ocomeni 79:a2187bbfa407 223 wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
ocomeni 79:a2187bbfa407 224 wifiCmd->wifi_cmd = cmd;
ocomeni 79:a2187bbfa407 225 _aT2WiFiCmdQueue->put(wifiCmd);
ocomeni 79:a2187bbfa407 226 return true;
ocomeni 79:a2187bbfa407 227 }
ocomeni 79:a2187bbfa407 228
ocomeni 79:a2187bbfa407 229 void ATCmdManager::_oob_scanWiFiNetworks(){
ocomeni 75:08eff6258e1b 230 _smutex.lock();
ocomeni 79:a2187bbfa407 231 printf("\n Received scanWiFiNetworks command!!\n");
ocomeni 75:08eff6258e1b 232 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 233 _smutex.unlock();
ocomeni 79:a2187bbfa407 234 wifi_cmd_t cmd = WIFI_CMD_SCAN;
ocomeni 79:a2187bbfa407 235 // queue next command
ocomeni 79:a2187bbfa407 236 queueWiFiCommand(cmd);
ocomeni 79:a2187bbfa407 237 bool success;
ocomeni 79:a2187bbfa407 238 success = wiFiManager->setNextCommand(cmd);
ocomeni 79:a2187bbfa407 239 if(success){
ocomeni 79:a2187bbfa407 240 printf("\n scan command successfully sent to wiFiManager!!\n");
ocomeni 79:a2187bbfa407 241 }
ocomeni 79:a2187bbfa407 242 else {
ocomeni 79:a2187bbfa407 243 printf("\n ERROR: Failed to send scan command wiFiManager!!\n");
ocomeni 79:a2187bbfa407 244 }
ocomeni 79:a2187bbfa407 245 return;
ocomeni 79:a2187bbfa407 246 /* call WiFi Scan in 20 ms */
ocomeni 79:a2187bbfa407 247 //_event_queue.call_in(
ocomeni 79:a2187bbfa407 248 // 20, wiFiManager,
ocomeni 79:a2187bbfa407 249 // &WiFiManager::scanNetworks);
ocomeni 79:a2187bbfa407 250 //return;
ocomeni 79:a2187bbfa407 251 WiFiAccessPoint *ap;
ocomeni 79:a2187bbfa407 252 nsapi_size_or_error_t count;
ocomeni 79:a2187bbfa407 253 //count = wiFiManager->scanNetworks();
ocomeni 79:a2187bbfa407 254 if (count <= 0) {
ocomeni 79:a2187bbfa407 255 _smutex.lock();
ocomeni 79:a2187bbfa407 256 printf("scan() failed with return value: %d\n", count);
ocomeni 79:a2187bbfa407 257 _smutex.unlock();
ocomeni 79:a2187bbfa407 258 return;
ocomeni 79:a2187bbfa407 259 }
ocomeni 79:a2187bbfa407 260 /* Limit number of network arbitrary to 15 */
ocomeni 79:a2187bbfa407 261 count = count < 15 ? count : 15;
ocomeni 79:a2187bbfa407 262 ap = new WiFiAccessPoint[count];
ocomeni 79:a2187bbfa407 263 count = wiFiManager->getAvailableAPs(ap, count);
ocomeni 79:a2187bbfa407 264 if (count <= 0) {
ocomeni 79:a2187bbfa407 265 printf("scan() failed with return value: %d\n", count);
ocomeni 79:a2187bbfa407 266 return;
ocomeni 79:a2187bbfa407 267 }
ocomeni 79:a2187bbfa407 268
ocomeni 79:a2187bbfa407 269 for (int i = 0; i < count; i++) {
ocomeni 79:a2187bbfa407 270 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
ocomeni 79:a2187bbfa407 271 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ocomeni 79:a2187bbfa407 272 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
ocomeni 79:a2187bbfa407 273 }
ocomeni 79:a2187bbfa407 274 printf("%d networks available.\n", count);
ocomeni 79:a2187bbfa407 275
ocomeni 79:a2187bbfa407 276 delete[] ap;
ocomeni 79:a2187bbfa407 277
ocomeni 75:08eff6258e1b 278 }
ocomeni 75:08eff6258e1b 279
ocomeni 79:a2187bbfa407 280 void ATCmdManager::_oob_connect2WiFiNetwork()
ocomeni 79:a2187bbfa407 281 {
ocomeni 79:a2187bbfa407 282 wiFiManager->connect();
ocomeni 79:a2187bbfa407 283 }
ocomeni 79:a2187bbfa407 284
ocomeni 79:a2187bbfa407 285
ocomeni 79:a2187bbfa407 286 void ATCmdManager::_oob_disconnectWiFiNetwork()
ocomeni 79:a2187bbfa407 287 {
ocomeni 79:a2187bbfa407 288 wiFiManager->disconnect();
ocomeni 79:a2187bbfa407 289 }