Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/ATCmdManager.cpp@83:9c271a50a70b, 2019-03-26 (annotated)
- Committer:
- ocomeni
- Date:
- Tue Mar 26 23:33:51 2019 +0000
- Revision:
- 83:9c271a50a70b
- Parent:
- 82:10072c1794d3
- Child:
- 84:7c7add00f4bf
implemented parser for header
Who changed what in which revision?
User | Revision | Line number | New 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 | 83:9c271a50a70b | 61 | _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage)); |
ocomeni | 81:637a87eb8170 | 62 | |
ocomeni | 82:10072c1794d3 | 63 | //AT+UWSC=0,2,<SSID> |
ocomeni | 82:10072c1794d3 | 64 | //AT+UWSC=0,8,<PWD> |
ocomeni | 82:10072c1794d3 | 65 | //AT+UWSC=0,5,2 (WPA) |
ocomeni | 75:08eff6258e1b | 66 | //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr)); |
ocomeni | 74:f26e846adfe9 | 67 | printf("\n --- ATCmdManager constructor completed ---\n"); |
ocomeni | 81:637a87eb8170 | 68 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 69 | dataMode = AT_CMD_DATA_MODE; |
ocomeni | 75:08eff6258e1b | 70 | //AT+UMRS=230400,2,8,1,1,1 |
ocomeni | 74:f26e846adfe9 | 71 | // AT+UBTLE |
ocomeni | 74:f26e846adfe9 | 72 | } |
ocomeni | 74:f26e846adfe9 | 73 | |
ocomeni | 74:f26e846adfe9 | 74 | |
ocomeni | 74:f26e846adfe9 | 75 | void ATCmdManager::runMain(){ |
ocomeni | 74:f26e846adfe9 | 76 | while(true){ |
ocomeni | 74:f26e846adfe9 | 77 | _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true); |
ocomeni | 81:637a87eb8170 | 78 | wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time |
ocomeni | 81:637a87eb8170 | 79 | processResponses(); |
ocomeni | 74:f26e846adfe9 | 80 | } |
ocomeni | 74:f26e846adfe9 | 81 | |
ocomeni | 74:f26e846adfe9 | 82 | } |
ocomeni | 74:f26e846adfe9 | 83 | |
ocomeni | 81:637a87eb8170 | 84 | void ATCmdManager::processResponses(){ |
ocomeni | 81:637a87eb8170 | 85 | dequeueATresponse(); |
ocomeni | 81:637a87eb8170 | 86 | switch(at_resp){ |
ocomeni | 81:637a87eb8170 | 87 | case AT_RESP_NONE: |
ocomeni | 81:637a87eb8170 | 88 | // IDLE response state |
ocomeni | 81:637a87eb8170 | 89 | break; |
ocomeni | 81:637a87eb8170 | 90 | case AT_SCAN_RESP: |
ocomeni | 81:637a87eb8170 | 91 | // AT_SCAN_RESP response state |
ocomeni | 81:637a87eb8170 | 92 | _smutex.lock(); |
ocomeni | 81:637a87eb8170 | 93 | printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n"); |
ocomeni | 81:637a87eb8170 | 94 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 95 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 96 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 97 | break; |
ocomeni | 81:637a87eb8170 | 98 | case AT_DETAILED_SCAN_RESP: |
ocomeni | 81:637a87eb8170 | 99 | // AT_DETAILED_SCAN_RESP response state |
ocomeni | 81:637a87eb8170 | 100 | _smutex.lock(); |
ocomeni | 81:637a87eb8170 | 101 | printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n"); |
ocomeni | 81:637a87eb8170 | 102 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 103 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 104 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 105 | break; |
ocomeni | 81:637a87eb8170 | 106 | case AT_CONNECT_RESP: |
ocomeni | 81:637a87eb8170 | 107 | // AT_CONNECT_RESP response state |
ocomeni | 81:637a87eb8170 | 108 | _smutex.lock(); |
ocomeni | 81:637a87eb8170 | 109 | printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n"); |
ocomeni | 81:637a87eb8170 | 110 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 111 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 112 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 113 | break; |
ocomeni | 81:637a87eb8170 | 114 | case AT_DISCONNECT_RESP: |
ocomeni | 81:637a87eb8170 | 115 | // AT_DISCONNECT_RESP response state |
ocomeni | 81:637a87eb8170 | 116 | _smutex.lock(); |
ocomeni | 81:637a87eb8170 | 117 | printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n"); |
ocomeni | 81:637a87eb8170 | 118 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 119 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 120 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 121 | break; |
ocomeni | 82:10072c1794d3 | 122 | case AT_CONFIG_RESP: |
ocomeni | 82:10072c1794d3 | 123 | // AT_CONFIG_RESP response state |
ocomeni | 81:637a87eb8170 | 124 | _smutex.lock(); |
ocomeni | 82:10072c1794d3 | 125 | printf("\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\n"); |
ocomeni | 81:637a87eb8170 | 126 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 127 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 128 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 129 | break; |
ocomeni | 81:637a87eb8170 | 130 | case AT_HTTP_RESP: |
ocomeni | 81:637a87eb8170 | 131 | // AT_HTTP_RESP response state |
ocomeni | 81:637a87eb8170 | 132 | _smutex.lock(); |
ocomeni | 81:637a87eb8170 | 133 | printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n"); |
ocomeni | 81:637a87eb8170 | 134 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 135 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 136 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 137 | break; |
ocomeni | 81:637a87eb8170 | 138 | default: |
ocomeni | 81:637a87eb8170 | 139 | // UNKNOWN response state |
ocomeni | 82:10072c1794d3 | 140 | _smutex.lock(); |
ocomeni | 82:10072c1794d3 | 141 | printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\n"); |
ocomeni | 82:10072c1794d3 | 142 | _parser.send("OK\n"); |
ocomeni | 82:10072c1794d3 | 143 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 144 | at_resp = AT_RESP_NONE; |
ocomeni | 81:637a87eb8170 | 145 | break; |
ocomeni | 81:637a87eb8170 | 146 | } |
ocomeni | 81:637a87eb8170 | 147 | } |
ocomeni | 81:637a87eb8170 | 148 | |
ocomeni | 81:637a87eb8170 | 149 | |
ocomeni | 83:9c271a50a70b | 150 | bool ATCmdManager::validate(edm_header_t edm_header) |
ocomeni | 83:9c271a50a70b | 151 | { |
ocomeni | 83:9c271a50a70b | 152 | if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found! |
ocomeni | 83:9c271a50a70b | 153 | if(edm_header.payloadID != CONNECT_EVENT && |
ocomeni | 83:9c271a50a70b | 154 | edm_header.payloadID != DISCONNECT_EVENT && |
ocomeni | 83:9c271a50a70b | 155 | edm_header.payloadID != DATA_EVENT && |
ocomeni | 83:9c271a50a70b | 156 | edm_header.payloadID != DATA_COMMAND && |
ocomeni | 83:9c271a50a70b | 157 | edm_header.payloadID != AT_REQUEST && |
ocomeni | 83:9c271a50a70b | 158 | edm_header.payloadID != AT_CONFIRMATION && |
ocomeni | 83:9c271a50a70b | 159 | edm_header.payloadID != AT_EVENT |
ocomeni | 83:9c271a50a70b | 160 | )return false; // unexpected payload ID found! |
ocomeni | 83:9c271a50a70b | 161 | if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN || |
ocomeni | 83:9c271a50a70b | 162 | edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN |
ocomeni | 83:9c271a50a70b | 163 | ) return false; // unexpected length received! |
ocomeni | 83:9c271a50a70b | 164 | return true; |
ocomeni | 83:9c271a50a70b | 165 | } |
ocomeni | 74:f26e846adfe9 | 166 | |
ocomeni | 74:f26e846adfe9 | 167 | // OOB processing |
ocomeni | 74:f26e846adfe9 | 168 | void ATCmdManager::_process_oob(uint32_t timeout, bool all){ |
ocomeni | 74:f26e846adfe9 | 169 | set_timeout(timeout); |
ocomeni | 83:9c271a50a70b | 170 | static int cnt = 0; |
ocomeni | 83:9c271a50a70b | 171 | if(dataMode == AT_EXT_DATA_MODE) |
ocomeni | 83:9c271a50a70b | 172 | { |
ocomeni | 83:9c271a50a70b | 173 | int n; |
ocomeni | 83:9c271a50a70b | 174 | if(cnt++ % 10 == 0)printf("In EDM mode\n"); |
ocomeni | 83:9c271a50a70b | 175 | // Poll for edm packets |
ocomeni | 83:9c271a50a70b | 176 | do{ |
ocomeni | 83:9c271a50a70b | 177 | n = _parser.read((char *)&edm_hdr, EDM_HDR_LEN); |
ocomeni | 83:9c271a50a70b | 178 | if(n == -1) break; // break if it times out |
ocomeni | 83:9c271a50a70b | 179 | printf("%d bytes read!\n", n); |
ocomeni | 83:9c271a50a70b | 180 | if(n==5) |
ocomeni | 83:9c271a50a70b | 181 | printf("Start = %d, payloadID = %d len = %d\n", edm_hdr.startByte, |
ocomeni | 83:9c271a50a70b | 182 | edm_hdr.payloadID, |
ocomeni | 83:9c271a50a70b | 183 | edm_hdr.payloadLen); |
ocomeni | 83:9c271a50a70b | 184 | if(n == EDM_HDR_LEN && validate(edm_hdr)) // if AT command use process oob to decode |
ocomeni | 83:9c271a50a70b | 185 | { |
ocomeni | 83:9c271a50a70b | 186 | if(edm_hdr.payloadID == AT_REQUEST) |
ocomeni | 83:9c271a50a70b | 187 | { |
ocomeni | 83:9c271a50a70b | 188 | _parser.process_oob(); |
ocomeni | 83:9c271a50a70b | 189 | break; |
ocomeni | 83:9c271a50a70b | 190 | } |
ocomeni | 83:9c271a50a70b | 191 | else |
ocomeni | 83:9c271a50a70b | 192 | { |
ocomeni | 83:9c271a50a70b | 193 | int pLen = edm_hdr.payloadLen-1; |
ocomeni | 83:9c271a50a70b | 194 | rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte |
ocomeni | 83:9c271a50a70b | 195 | rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected |
ocomeni | 83:9c271a50a70b | 196 | n = _parser.read((char *)rx_buf_ptr, pLen); |
ocomeni | 83:9c271a50a70b | 197 | if(n == -1) break; // timeout! |
ocomeni | 83:9c271a50a70b | 198 | printf("%d bytes read - expected %d!\n", n, pLen); |
ocomeni | 83:9c271a50a70b | 199 | for(int i =0;i<pLen;i++){ |
ocomeni | 83:9c271a50a70b | 200 | if(i%8==0) printf("\n[%3d]",i/8); |
ocomeni | 83:9c271a50a70b | 201 | printf("%2x ", rx_buf_ptr[i]); |
ocomeni | 83:9c271a50a70b | 202 | } |
ocomeni | 83:9c271a50a70b | 203 | printf("\n"); |
ocomeni | 83:9c271a50a70b | 204 | printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]); |
ocomeni | 83:9c271a50a70b | 205 | if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) { |
ocomeni | 83:9c271a50a70b | 206 | break; // exit if stop byte now found - possible data corruption! |
ocomeni | 83:9c271a50a70b | 207 | } |
ocomeni | 83:9c271a50a70b | 208 | switch(edm_hdr.payloadID) |
ocomeni | 83:9c271a50a70b | 209 | { |
ocomeni | 83:9c271a50a70b | 210 | case CONNECT_EVENT: |
ocomeni | 83:9c271a50a70b | 211 | printf("Connection Event received!\n"); |
ocomeni | 83:9c271a50a70b | 212 | break; |
ocomeni | 83:9c271a50a70b | 213 | case DISCONNECT_EVENT: |
ocomeni | 83:9c271a50a70b | 214 | printf("DISCONNECT_EVENT received!\n"); |
ocomeni | 83:9c271a50a70b | 215 | break; |
ocomeni | 83:9c271a50a70b | 216 | case DATA_EVENT: |
ocomeni | 83:9c271a50a70b | 217 | printf("DATA_EVENT received!\n"); |
ocomeni | 83:9c271a50a70b | 218 | break; |
ocomeni | 83:9c271a50a70b | 219 | case DATA_COMMAND: |
ocomeni | 83:9c271a50a70b | 220 | char s1[100]; |
ocomeni | 83:9c271a50a70b | 221 | printf("DATA_COMMAND received!\n"); |
ocomeni | 83:9c271a50a70b | 222 | int n = 0; |
ocomeni | 83:9c271a50a70b | 223 | int cnt = 0; |
ocomeni | 83:9c271a50a70b | 224 | int len = 0; |
ocomeni | 83:9c271a50a70b | 225 | char * strPtr = (char *)&rx_buf_ptr[1]; |
ocomeni | 83:9c271a50a70b | 226 | char * p2 = strstr(strPtr, "\r\n\r\n"); |
ocomeni | 83:9c271a50a70b | 227 | char * nxtPtr = strPtr; |
ocomeni | 83:9c271a50a70b | 228 | printf("\nstrPtr address= %x",strPtr); |
ocomeni | 83:9c271a50a70b | 229 | printf("\np2 address= %x", p2); |
ocomeni | 83:9c271a50a70b | 230 | while(1){ |
ocomeni | 83:9c271a50a70b | 231 | n = sscanf(nxtPtr,"%s[^:]", s1); |
ocomeni | 83:9c271a50a70b | 232 | len = strlen(s1); |
ocomeni | 83:9c271a50a70b | 233 | if(n<=0) break; |
ocomeni | 83:9c271a50a70b | 234 | printf("\ns[%d] = %s len = %d, n = %d",cnt++,s1, len, n ); |
ocomeni | 83:9c271a50a70b | 235 | if(cnt==20) break; |
ocomeni | 83:9c271a50a70b | 236 | nxtPtr = strstr(nxtPtr, s1)+len; |
ocomeni | 83:9c271a50a70b | 237 | printf("\nxtPtr address= %x p2 addr = %x",nxtPtr, p2); |
ocomeni | 83:9c271a50a70b | 238 | if(nxtPtr > p2) break; |
ocomeni | 83:9c271a50a70b | 239 | } |
ocomeni | 83:9c271a50a70b | 240 | |
ocomeni | 83:9c271a50a70b | 241 | break; |
ocomeni | 83:9c271a50a70b | 242 | case AT_REQUEST: |
ocomeni | 83:9c271a50a70b | 243 | printf("AT_REQUEST received!\n"); |
ocomeni | 83:9c271a50a70b | 244 | break; |
ocomeni | 83:9c271a50a70b | 245 | case AT_CONFIRMATION: |
ocomeni | 83:9c271a50a70b | 246 | printf("AT_CONFIRMATION received!\n"); |
ocomeni | 83:9c271a50a70b | 247 | break; |
ocomeni | 83:9c271a50a70b | 248 | case AT_EVENT: |
ocomeni | 83:9c271a50a70b | 249 | printf("AT_EVENT received!\n"); |
ocomeni | 83:9c271a50a70b | 250 | break; |
ocomeni | 83:9c271a50a70b | 251 | default: |
ocomeni | 83:9c271a50a70b | 252 | printf("UNKNOWN MESSAGE received!\n"); |
ocomeni | 83:9c271a50a70b | 253 | break; |
ocomeni | 83:9c271a50a70b | 254 | } |
ocomeni | 83:9c271a50a70b | 255 | } |
ocomeni | 83:9c271a50a70b | 256 | //_parser.process_oob(); |
ocomeni | 83:9c271a50a70b | 257 | } |
ocomeni | 83:9c271a50a70b | 258 | else // incorrect # of bytes received abort!! |
ocomeni | 83:9c271a50a70b | 259 | { |
ocomeni | 83:9c271a50a70b | 260 | break; |
ocomeni | 83:9c271a50a70b | 261 | } |
ocomeni | 83:9c271a50a70b | 262 | }while (all); // continue to process until timeout |
ocomeni | 74:f26e846adfe9 | 263 | } |
ocomeni | 83:9c271a50a70b | 264 | else |
ocomeni | 83:9c271a50a70b | 265 | { |
ocomeni | 83:9c271a50a70b | 266 | // Poll for inbound packets |
ocomeni | 83:9c271a50a70b | 267 | while (_parser.process_oob() && all) { |
ocomeni | 83:9c271a50a70b | 268 | } |
ocomeni | 83:9c271a50a70b | 269 | } |
ocomeni | 74:f26e846adfe9 | 270 | set_timeout(); |
ocomeni | 74:f26e846adfe9 | 271 | } |
ocomeni | 74:f26e846adfe9 | 272 | |
ocomeni | 74:f26e846adfe9 | 273 | |
ocomeni | 74:f26e846adfe9 | 274 | // OOB message handlers |
ocomeni | 74:f26e846adfe9 | 275 | void ATCmdManager::_oob_startup_hdlr(){ |
ocomeni | 74:f26e846adfe9 | 276 | } |
ocomeni | 74:f26e846adfe9 | 277 | |
ocomeni | 81:637a87eb8170 | 278 | void ATCmdManager::_oob_ok_hdlr(){ |
ocomeni | 81:637a87eb8170 | 279 | _smutex.lock(); |
ocomeni | 81:637a87eb8170 | 280 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 281 | _smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 282 | } |
ocomeni | 81:637a87eb8170 | 283 | |
ocomeni | 74:f26e846adfe9 | 284 | |
ocomeni | 74:f26e846adfe9 | 285 | void ATCmdManager::_oob_bleRole_hdlr(){ |
ocomeni | 74:f26e846adfe9 | 286 | } |
ocomeni | 74:f26e846adfe9 | 287 | |
ocomeni | 74:f26e846adfe9 | 288 | |
ocomeni | 74:f26e846adfe9 | 289 | void ATCmdManager::_oob_wifiMode_err(){ |
ocomeni | 74:f26e846adfe9 | 290 | } |
ocomeni | 74:f26e846adfe9 | 291 | |
ocomeni | 74:f26e846adfe9 | 292 | |
ocomeni | 74:f26e846adfe9 | 293 | void ATCmdManager::_oob_conn_already(){ |
ocomeni | 74:f26e846adfe9 | 294 | } |
ocomeni | 74:f26e846adfe9 | 295 | |
ocomeni | 74:f26e846adfe9 | 296 | |
ocomeni | 74:f26e846adfe9 | 297 | void ATCmdManager::_oob_err(){ |
ocomeni | 74:f26e846adfe9 | 298 | } |
ocomeni | 74:f26e846adfe9 | 299 | |
ocomeni | 79:a2187bbfa407 | 300 | void ATCmdManager::_oob_get_fw_ver() |
ocomeni | 79:a2187bbfa407 | 301 | { |
ocomeni | 79:a2187bbfa407 | 302 | } |
ocomeni | 79:a2187bbfa407 | 303 | |
ocomeni | 79:a2187bbfa407 | 304 | |
ocomeni | 75:08eff6258e1b | 305 | void ATCmdManager::_oob_uart_setup(){ |
ocomeni | 75:08eff6258e1b | 306 | int uOpts[NUM_UART_OPTIONS]; |
ocomeni | 75:08eff6258e1b | 307 | //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) { |
ocomeni | 75:08eff6258e1b | 308 | 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 | 309 | 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 | 310 | //AT+UMRS=230400,2,8,1,1,1 |
ocomeni | 81:637a87eb8170 | 311 | printf("\n Changing Baud Rate to %d\n", uOpts[0]); |
ocomeni | 81:637a87eb8170 | 312 | |
ocomeni | 81:637a87eb8170 | 313 | _serial.set_baud(uOpts[0]); |
ocomeni | 81:637a87eb8170 | 314 | printf("\n Baud Rate now %d\n", uOpts[0]); |
ocomeni | 81:637a87eb8170 | 315 | |
ocomeni | 75:08eff6258e1b | 316 | } else { |
ocomeni | 75:08eff6258e1b | 317 | printf("\nATCmdParser: Retrieving Uart Options failed"); |
ocomeni | 75:08eff6258e1b | 318 | } |
ocomeni | 75:08eff6258e1b | 319 | } |
ocomeni | 75:08eff6258e1b | 320 | |
ocomeni | 74:f26e846adfe9 | 321 | void ATCmdManager::set_timeout(uint32_t timeout_ms) |
ocomeni | 74:f26e846adfe9 | 322 | { |
ocomeni | 74:f26e846adfe9 | 323 | _parser.set_timeout(timeout_ms); |
ocomeni | 74:f26e846adfe9 | 324 | } |
ocomeni | 74:f26e846adfe9 | 325 | |
ocomeni | 75:08eff6258e1b | 326 | |
ocomeni | 75:08eff6258e1b | 327 | void ATCmdManager::_oob_echo_off() |
ocomeni | 75:08eff6258e1b | 328 | { |
ocomeni | 75:08eff6258e1b | 329 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 330 | printf("\n Received ATEO OOB command!!\n"); |
ocomeni | 75:08eff6258e1b | 331 | printf("\n turning echo OFF!!\n"); |
ocomeni | 75:08eff6258e1b | 332 | _parser.debug_on(false); |
ocomeni | 75:08eff6258e1b | 333 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 334 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 335 | } |
ocomeni | 75:08eff6258e1b | 336 | |
ocomeni | 75:08eff6258e1b | 337 | |
ocomeni | 75:08eff6258e1b | 338 | void ATCmdManager::_oob_echo_on() |
ocomeni | 75:08eff6258e1b | 339 | { |
ocomeni | 75:08eff6258e1b | 340 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 341 | printf("\n Received ATE1 OOB command!!\n"); |
ocomeni | 75:08eff6258e1b | 342 | printf("\n turning echo ON!!\n"); |
ocomeni | 75:08eff6258e1b | 343 | _parser.debug_on(true); |
ocomeni | 75:08eff6258e1b | 344 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 345 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 346 | } |
ocomeni | 75:08eff6258e1b | 347 | |
ocomeni | 75:08eff6258e1b | 348 | |
ocomeni | 75:08eff6258e1b | 349 | void ATCmdManager::_oob_data_mode(){ |
ocomeni | 75:08eff6258e1b | 350 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 351 | printf("\n Received EDM mode command!!\n"); |
ocomeni | 75:08eff6258e1b | 352 | if(_parser.scanf("%d", &dataMode) >0) { |
ocomeni | 75:08eff6258e1b | 353 | printf("\nATCmdParser: Data mode=%d\n", dataMode); |
ocomeni | 75:08eff6258e1b | 354 | switch(dataMode) |
ocomeni | 75:08eff6258e1b | 355 | { |
ocomeni | 75:08eff6258e1b | 356 | case 0: |
ocomeni | 83:9c271a50a70b | 357 | printf("\nATCmdParser: Command Mode request received\n"); |
ocomeni | 81:637a87eb8170 | 358 | dataMode = AT_CMD_DATA_MODE; |
ocomeni | 75:08eff6258e1b | 359 | break; |
ocomeni | 75:08eff6258e1b | 360 | case 1: |
ocomeni | 83:9c271a50a70b | 361 | printf("\nATCmdParser: Data Mode request received\n"); |
ocomeni | 81:637a87eb8170 | 362 | dataMode = AT_STD_DATA_MODE; |
ocomeni | 75:08eff6258e1b | 363 | break; |
ocomeni | 75:08eff6258e1b | 364 | case 2: |
ocomeni | 83:9c271a50a70b | 365 | printf("\nATCmdParser: Extended data Mode request received\n"); |
ocomeni | 81:637a87eb8170 | 366 | dataMode = AT_EXT_DATA_MODE; |
ocomeni | 75:08eff6258e1b | 367 | break; |
ocomeni | 75:08eff6258e1b | 368 | default: |
ocomeni | 75:08eff6258e1b | 369 | printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n"); |
ocomeni | 75:08eff6258e1b | 370 | break; |
ocomeni | 75:08eff6258e1b | 371 | } |
ocomeni | 75:08eff6258e1b | 372 | } else { |
ocomeni | 83:9c271a50a70b | 373 | printf("\nATCmdParser: Retrieving Uart Options failed\n"); |
ocomeni | 75:08eff6258e1b | 374 | } |
ocomeni | 75:08eff6258e1b | 375 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 376 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 377 | } |
ocomeni | 75:08eff6258e1b | 378 | |
ocomeni | 75:08eff6258e1b | 379 | void ATCmdManager::_oob_get_mac_addr(){ |
ocomeni | 75:08eff6258e1b | 380 | int bleOrWifi; |
ocomeni | 75:08eff6258e1b | 381 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 382 | if(_parser.scanf("=%d", &bleOrWifi) >0) { |
ocomeni | 75:08eff6258e1b | 383 | switch(bleOrWifi) |
ocomeni | 75:08eff6258e1b | 384 | { |
ocomeni | 75:08eff6258e1b | 385 | case 1: |
ocomeni | 75:08eff6258e1b | 386 | printf("\nATCmdParser: BLE MAC Address request received"); |
ocomeni | 75:08eff6258e1b | 387 | break; |
ocomeni | 75:08eff6258e1b | 388 | case 2: |
ocomeni | 75:08eff6258e1b | 389 | printf("\nATCmdParser: WiFi MAC Address request received"); |
ocomeni | 75:08eff6258e1b | 390 | break; |
ocomeni | 75:08eff6258e1b | 391 | default: |
ocomeni | 75:08eff6258e1b | 392 | printf("\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n"); |
ocomeni | 75:08eff6258e1b | 393 | break; |
ocomeni | 75:08eff6258e1b | 394 | } |
ocomeni | 75:08eff6258e1b | 395 | } else { |
ocomeni | 75:08eff6258e1b | 396 | printf("\nATCmdParser: Retrieving Uart Options failed"); |
ocomeni | 75:08eff6258e1b | 397 | } |
ocomeni | 75:08eff6258e1b | 398 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 399 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 400 | } |
ocomeni | 75:08eff6258e1b | 401 | |
ocomeni | 75:08eff6258e1b | 402 | void ATCmdManager::_oob_get_ble_role(){ |
ocomeni | 75:08eff6258e1b | 403 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 404 | printf("\n Received get BLE role command!!\n"); |
ocomeni | 75:08eff6258e1b | 405 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 406 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 407 | } |
ocomeni | 75:08eff6258e1b | 408 | |
ocomeni | 75:08eff6258e1b | 409 | void ATCmdManager::_oob_ena_ble_peri(){ |
ocomeni | 75:08eff6258e1b | 410 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 411 | printf("\n Received enable BLE Peripheral command!!\n"); |
ocomeni | 75:08eff6258e1b | 412 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 413 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 414 | } |
ocomeni | 75:08eff6258e1b | 415 | |
ocomeni | 75:08eff6258e1b | 416 | void ATCmdManager::_oob_reboot(){ |
ocomeni | 75:08eff6258e1b | 417 | _smutex.lock(); |
ocomeni | 75:08eff6258e1b | 418 | printf("\n Received reboot command!!\n"); |
ocomeni | 75:08eff6258e1b | 419 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 420 | _parser.send("System Resetting....\n"); |
ocomeni | 75:08eff6258e1b | 421 | system_reset(); |
ocomeni | 75:08eff6258e1b | 422 | _smutex.unlock(); |
ocomeni | 75:08eff6258e1b | 423 | } |
ocomeni | 75:08eff6258e1b | 424 | |
ocomeni | 79:a2187bbfa407 | 425 | const char * ATCmdManager::sec2str(nsapi_security_t sec) |
ocomeni | 79:a2187bbfa407 | 426 | { |
ocomeni | 79:a2187bbfa407 | 427 | switch (sec) { |
ocomeni | 79:a2187bbfa407 | 428 | case NSAPI_SECURITY_NONE: |
ocomeni | 79:a2187bbfa407 | 429 | return "None"; |
ocomeni | 79:a2187bbfa407 | 430 | case NSAPI_SECURITY_WEP: |
ocomeni | 79:a2187bbfa407 | 431 | return "WEP"; |
ocomeni | 79:a2187bbfa407 | 432 | case NSAPI_SECURITY_WPA: |
ocomeni | 79:a2187bbfa407 | 433 | return "WPA"; |
ocomeni | 79:a2187bbfa407 | 434 | case NSAPI_SECURITY_WPA2: |
ocomeni | 79:a2187bbfa407 | 435 | return "WPA2"; |
ocomeni | 79:a2187bbfa407 | 436 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 79:a2187bbfa407 | 437 | return "WPA/WPA2"; |
ocomeni | 79:a2187bbfa407 | 438 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 79:a2187bbfa407 | 439 | default: |
ocomeni | 79:a2187bbfa407 | 440 | return "Unknown"; |
ocomeni | 79:a2187bbfa407 | 441 | } |
ocomeni | 79:a2187bbfa407 | 442 | } |
ocomeni | 81:637a87eb8170 | 443 | |
ocomeni | 81:637a87eb8170 | 444 | bool ATCmdManager::setNextResponse(at_cmd_resp_t resp) |
ocomeni | 81:637a87eb8170 | 445 | { |
ocomeni | 81:637a87eb8170 | 446 | if(at_resp == AT_RESP_NONE){ |
ocomeni | 81:637a87eb8170 | 447 | at_resp = resp; |
ocomeni | 81:637a87eb8170 | 448 | return true; // success |
ocomeni | 81:637a87eb8170 | 449 | } |
ocomeni | 81:637a87eb8170 | 450 | return false; // wiFiManager busy |
ocomeni | 79:a2187bbfa407 | 451 | } |
ocomeni | 79:a2187bbfa407 | 452 | |
ocomeni | 81:637a87eb8170 | 453 | |
ocomeni | 79:a2187bbfa407 | 454 | void ATCmdManager::_oob_scanWiFiNetworks(){ |
ocomeni | 75:08eff6258e1b | 455 | _smutex.lock(); |
ocomeni | 79:a2187bbfa407 | 456 | printf("\n Received scanWiFiNetworks command!!\n"); |
ocomeni | 75:08eff6258e1b | 457 | _parser.send("OK\n"); |
ocomeni | 75:08eff6258e1b | 458 | _smutex.unlock(); |
ocomeni | 79:a2187bbfa407 | 459 | wifi_cmd_t cmd = WIFI_CMD_SCAN; |
ocomeni | 79:a2187bbfa407 | 460 | // queue next command |
ocomeni | 79:a2187bbfa407 | 461 | queueWiFiCommand(cmd); |
ocomeni | 81:637a87eb8170 | 462 | return; |
ocomeni | 75:08eff6258e1b | 463 | } |
ocomeni | 75:08eff6258e1b | 464 | |
ocomeni | 79:a2187bbfa407 | 465 | void ATCmdManager::_oob_connect2WiFiNetwork() |
ocomeni | 79:a2187bbfa407 | 466 | { |
ocomeni | 81:637a87eb8170 | 467 | wifi_cmd_t cmd = WIFI_CMD_CONNECT; |
ocomeni | 81:637a87eb8170 | 468 | // queue next command |
ocomeni | 81:637a87eb8170 | 469 | queueWiFiCommand(cmd); |
ocomeni | 81:637a87eb8170 | 470 | return; |
ocomeni | 79:a2187bbfa407 | 471 | } |
ocomeni | 79:a2187bbfa407 | 472 | |
ocomeni | 79:a2187bbfa407 | 473 | |
ocomeni | 79:a2187bbfa407 | 474 | void ATCmdManager::_oob_disconnectWiFiNetwork() |
ocomeni | 79:a2187bbfa407 | 475 | { |
ocomeni | 81:637a87eb8170 | 476 | wifi_cmd_t cmd = WIFI_CMD_DISCONNECT; |
ocomeni | 81:637a87eb8170 | 477 | // queue next command |
ocomeni | 81:637a87eb8170 | 478 | queueWiFiCommand(cmd); |
ocomeni | 81:637a87eb8170 | 479 | return; |
ocomeni | 81:637a87eb8170 | 480 | } |
ocomeni | 81:637a87eb8170 | 481 | |
ocomeni | 81:637a87eb8170 | 482 | void ATCmdManager::_oob_setupInternetConnection() |
ocomeni | 81:637a87eb8170 | 483 | { |
ocomeni | 81:637a87eb8170 | 484 | char url[200]; |
ocomeni | 81:637a87eb8170 | 485 | int n; |
ocomeni | 81:637a87eb8170 | 486 | internet_config_t internet_config; |
ocomeni | 81:637a87eb8170 | 487 | _smutex.lock(); |
ocomeni | 82:10072c1794d3 | 488 | n = _parser.scanf("=%1d,%199[^,],%1d", &internet_config.peer_id, |
ocomeni | 81:637a87eb8170 | 489 | url, |
ocomeni | 81:637a87eb8170 | 490 | &internet_config.connectionScheme); |
ocomeni | 81:637a87eb8170 | 491 | if(n>0) |
ocomeni | 81:637a87eb8170 | 492 | { |
ocomeni | 81:637a87eb8170 | 493 | internet_config.url = url; |
ocomeni | 81:637a87eb8170 | 494 | printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, |
ocomeni | 81:637a87eb8170 | 495 | internet_config.url.c_str(), |
ocomeni | 81:637a87eb8170 | 496 | internet_config.connectionScheme); |
ocomeni | 81:637a87eb8170 | 497 | // package and send on wifi data queue |
ocomeni | 81:637a87eb8170 | 498 | wifi_data_msg_t data_msg; |
ocomeni | 81:637a87eb8170 | 499 | data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG; |
ocomeni | 81:637a87eb8170 | 500 | data_msg.dataLen = sizeof(internet_config) + internet_config.url.length(); |
ocomeni | 81:637a87eb8170 | 501 | memcpy(data_msg.buffer,&internet_config, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 502 | // queue next data request |
ocomeni | 81:637a87eb8170 | 503 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 504 | _parser.send("OK\n"); |
ocomeni | 81:637a87eb8170 | 505 | } else { |
ocomeni | 81:637a87eb8170 | 506 | printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n); |
ocomeni | 82:10072c1794d3 | 507 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 508 | } |
ocomeni | 82:10072c1794d3 | 509 | _smutex.unlock(); |
ocomeni | 82:10072c1794d3 | 510 | } |
ocomeni | 82:10072c1794d3 | 511 | |
ocomeni | 82:10072c1794d3 | 512 | wifi_config_t ATCmdManager::init_wifi_config() |
ocomeni | 82:10072c1794d3 | 513 | { |
ocomeni | 82:10072c1794d3 | 514 | wifi_config_t wifi_cfg; |
ocomeni | 82:10072c1794d3 | 515 | wifi_cfg.ssid[0] = NULL; |
ocomeni | 82:10072c1794d3 | 516 | wifi_cfg.pass[0] = NULL; |
ocomeni | 82:10072c1794d3 | 517 | wifi_cfg.security = NSAPI_SECURITY_UNKNOWN; |
ocomeni | 82:10072c1794d3 | 518 | return wifi_cfg; |
ocomeni | 82:10072c1794d3 | 519 | } |
ocomeni | 83:9c271a50a70b | 520 | /* read ASCII characters into buffer and null terminate */ |
ocomeni | 83:9c271a50a70b | 521 | int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes) |
ocomeni | 82:10072c1794d3 | 522 | { |
ocomeni | 82:10072c1794d3 | 523 | int c; |
ocomeni | 82:10072c1794d3 | 524 | int sptr = 0; |
ocomeni | 82:10072c1794d3 | 525 | for(int i=0;i<maxBytes;i++){ |
ocomeni | 82:10072c1794d3 | 526 | c = _parser.getc(); |
ocomeni | 82:10072c1794d3 | 527 | if(c==-1){ |
ocomeni | 83:9c271a50a70b | 528 | buf[sptr] = NULL; // null terminate if string |
ocomeni | 82:10072c1794d3 | 529 | return i; |
ocomeni | 82:10072c1794d3 | 530 | } |
ocomeni | 82:10072c1794d3 | 531 | if(c != ',' && c!= '"'){ |
ocomeni | 82:10072c1794d3 | 532 | buf[sptr++] = (uint8_t) c; |
ocomeni | 82:10072c1794d3 | 533 | } |
ocomeni | 81:637a87eb8170 | 534 | } |
ocomeni | 83:9c271a50a70b | 535 | return maxBytes; |
ocomeni | 83:9c271a50a70b | 536 | } |
ocomeni | 83:9c271a50a70b | 537 | |
ocomeni | 83:9c271a50a70b | 538 | int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes) |
ocomeni | 83:9c271a50a70b | 539 | { |
ocomeni | 83:9c271a50a70b | 540 | int c; |
ocomeni | 83:9c271a50a70b | 541 | int sptr = 0; |
ocomeni | 83:9c271a50a70b | 542 | for(int i=0;i<maxBytes;i++){ |
ocomeni | 83:9c271a50a70b | 543 | c = _parser.getc(); |
ocomeni | 83:9c271a50a70b | 544 | if(c==-1){ |
ocomeni | 83:9c271a50a70b | 545 | return i; |
ocomeni | 83:9c271a50a70b | 546 | } |
ocomeni | 83:9c271a50a70b | 547 | buf[sptr++] = (uint8_t) c; |
ocomeni | 83:9c271a50a70b | 548 | } |
ocomeni | 83:9c271a50a70b | 549 | return maxBytes; |
ocomeni | 82:10072c1794d3 | 550 | } |
ocomeni | 82:10072c1794d3 | 551 | |
ocomeni | 82:10072c1794d3 | 552 | void ATCmdManager::_oob_setWiFiSSID() |
ocomeni | 82:10072c1794d3 | 553 | { |
ocomeni | 82:10072c1794d3 | 554 | int n; |
ocomeni | 82:10072c1794d3 | 555 | wifi_config_t wifi_cfg = init_wifi_config(); |
ocomeni | 82:10072c1794d3 | 556 | _smutex.lock(); |
ocomeni | 83:9c271a50a70b | 557 | n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32); |
ocomeni | 82:10072c1794d3 | 558 | printf("[ATCMD MAN]: number of bytes read = %d\n", n); |
ocomeni | 82:10072c1794d3 | 559 | if(n>0) |
ocomeni | 82:10072c1794d3 | 560 | { |
ocomeni | 82:10072c1794d3 | 561 | printf("[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid); |
ocomeni | 82:10072c1794d3 | 562 | // package and send on wifi data queue |
ocomeni | 82:10072c1794d3 | 563 | wifi_data_msg_t data_msg; |
ocomeni | 82:10072c1794d3 | 564 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 82:10072c1794d3 | 565 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 82:10072c1794d3 | 566 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 567 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 568 | _parser.send("OK\n"); |
ocomeni | 82:10072c1794d3 | 569 | } else { |
ocomeni | 82:10072c1794d3 | 570 | printf("\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 82:10072c1794d3 | 571 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 572 | } |
ocomeni | 82:10072c1794d3 | 573 | _smutex.unlock(); |
ocomeni | 82:10072c1794d3 | 574 | } |
ocomeni | 82:10072c1794d3 | 575 | |
ocomeni | 82:10072c1794d3 | 576 | void ATCmdManager::_oob_setWiFiPWD() |
ocomeni | 82:10072c1794d3 | 577 | { |
ocomeni | 82:10072c1794d3 | 578 | int n; |
ocomeni | 82:10072c1794d3 | 579 | wifi_config_t wifi_cfg = init_wifi_config(); |
ocomeni | 82:10072c1794d3 | 580 | _smutex.lock(); |
ocomeni | 82:10072c1794d3 | 581 | //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass); |
ocomeni | 83:9c271a50a70b | 582 | n = readStringBytes((uint8_t *)wifi_cfg.pass, 32); |
ocomeni | 82:10072c1794d3 | 583 | if(n>0) |
ocomeni | 82:10072c1794d3 | 584 | { |
ocomeni | 82:10072c1794d3 | 585 | printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass); |
ocomeni | 82:10072c1794d3 | 586 | // package and send on wifi data queue |
ocomeni | 82:10072c1794d3 | 587 | wifi_data_msg_t data_msg; |
ocomeni | 82:10072c1794d3 | 588 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 82:10072c1794d3 | 589 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 82:10072c1794d3 | 590 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 591 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 592 | _parser.send("OK\n"); |
ocomeni | 82:10072c1794d3 | 593 | } else { |
ocomeni | 82:10072c1794d3 | 594 | printf("\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 82:10072c1794d3 | 595 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 596 | } |
ocomeni | 82:10072c1794d3 | 597 | _smutex.unlock(); |
ocomeni | 82:10072c1794d3 | 598 | } |
ocomeni | 82:10072c1794d3 | 599 | |
ocomeni | 82:10072c1794d3 | 600 | void ATCmdManager::_oob_setWiFiSecurity() |
ocomeni | 82:10072c1794d3 | 601 | { |
ocomeni | 82:10072c1794d3 | 602 | int n; |
ocomeni | 82:10072c1794d3 | 603 | wifi_config_t wifi_cfg = init_wifi_config(); |
ocomeni | 82:10072c1794d3 | 604 | _smutex.lock(); |
ocomeni | 82:10072c1794d3 | 605 | n = _parser.scanf(",%d", &wifi_cfg.security); |
ocomeni | 82:10072c1794d3 | 606 | if(n>0) |
ocomeni | 82:10072c1794d3 | 607 | { |
ocomeni | 82:10072c1794d3 | 608 | printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security)); |
ocomeni | 82:10072c1794d3 | 609 | // package and send on wifi data queue |
ocomeni | 82:10072c1794d3 | 610 | wifi_data_msg_t data_msg; |
ocomeni | 82:10072c1794d3 | 611 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 82:10072c1794d3 | 612 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 82:10072c1794d3 | 613 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 82:10072c1794d3 | 614 | queueWiFiDataRequest(data_msg); |
ocomeni | 82:10072c1794d3 | 615 | _parser.send("OK\n"); |
ocomeni | 82:10072c1794d3 | 616 | } else { |
ocomeni | 82:10072c1794d3 | 617 | printf("\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 82:10072c1794d3 | 618 | _parser.send("NAK\n"); |
ocomeni | 82:10072c1794d3 | 619 | } |
ocomeni | 81:637a87eb8170 | 620 | _smutex.unlock(); |
ocomeni | 79:a2187bbfa407 | 621 | } |
ocomeni | 81:637a87eb8170 | 622 | |
ocomeni | 83:9c271a50a70b | 623 | |
ocomeni | 83:9c271a50a70b | 624 | void ATCmdManager::_oob_sendHttpMessage() |
ocomeni | 83:9c271a50a70b | 625 | { |
ocomeni | 83:9c271a50a70b | 626 | int n; |
ocomeni | 83:9c271a50a70b | 627 | http_post_request_t http_post_request; |
ocomeni | 83:9c271a50a70b | 628 | _smutex.lock(); |
ocomeni | 83:9c271a50a70b | 629 | /* |
ocomeni | 83:9c271a50a70b | 630 | n = _parser.scanf(",%d", &wifi_cfg.security); |
ocomeni | 83:9c271a50a70b | 631 | if(n>0) |
ocomeni | 83:9c271a50a70b | 632 | { |
ocomeni | 83:9c271a50a70b | 633 | printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security)); |
ocomeni | 83:9c271a50a70b | 634 | // package and send on wifi data queue |
ocomeni | 83:9c271a50a70b | 635 | wifi_data_msg_t data_msg; |
ocomeni | 83:9c271a50a70b | 636 | data_msg.wifi_cmd = WIFI_CMD_CONFIG; |
ocomeni | 83:9c271a50a70b | 637 | data_msg.dataLen = sizeof(wifi_config_t); |
ocomeni | 83:9c271a50a70b | 638 | memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); |
ocomeni | 83:9c271a50a70b | 639 | queueWiFiDataRequest(data_msg); |
ocomeni | 83:9c271a50a70b | 640 | _parser.send("OK\n"); |
ocomeni | 83:9c271a50a70b | 641 | } else { |
ocomeni | 83:9c271a50a70b | 642 | printf("\n[ATCMD MAN]: wifi configuration failed \n"); |
ocomeni | 83:9c271a50a70b | 643 | _parser.send("NAK\n"); |
ocomeni | 83:9c271a50a70b | 644 | } |
ocomeni | 83:9c271a50a70b | 645 | */ |
ocomeni | 83:9c271a50a70b | 646 | _smutex.unlock(); |
ocomeni | 83:9c271a50a70b | 647 | |
ocomeni | 83:9c271a50a70b | 648 | } |
ocomeni | 83:9c271a50a70b | 649 | |
ocomeni | 81:637a87eb8170 | 650 | bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){ |
ocomeni | 81:637a87eb8170 | 651 | wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc(); |
ocomeni | 81:637a87eb8170 | 652 | wifiCmd->wifi_cmd = cmd; |
ocomeni | 81:637a87eb8170 | 653 | _aT2WiFiCmdQueue->put(wifiCmd); |
ocomeni | 81:637a87eb8170 | 654 | return true; |
ocomeni | 81:637a87eb8170 | 655 | } |
ocomeni | 81:637a87eb8170 | 656 | |
ocomeni | 81:637a87eb8170 | 657 | bool ATCmdManager::dequeueATresponse(){ |
ocomeni | 81:637a87eb8170 | 658 | if(at_resp != AT_RESP_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 659 | osEvent evt = _wiFi2ATCmdQueue->get(0); |
ocomeni | 81:637a87eb8170 | 660 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 661 | at_resp_message_t *resp = (at_resp_message_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 662 | setNextResponse(resp->at_resp); |
ocomeni | 81:637a87eb8170 | 663 | _wiFi2ATmPool->free(resp); |
ocomeni | 81:637a87eb8170 | 664 | } |
ocomeni | 81:637a87eb8170 | 665 | return true; |
ocomeni | 81:637a87eb8170 | 666 | } |
ocomeni | 81:637a87eb8170 | 667 | |
ocomeni | 81:637a87eb8170 | 668 | bool ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){ |
ocomeni | 81:637a87eb8170 | 669 | wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc(); |
ocomeni | 81:637a87eb8170 | 670 | wifiData->wifi_cmd = data_req.wifi_cmd; |
ocomeni | 81:637a87eb8170 | 671 | wifiData->dataLen = data_req.dataLen; |
ocomeni | 81:637a87eb8170 | 672 | memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen); |
ocomeni | 81:637a87eb8170 | 673 | _aT2WiFiDataQueue->put(wifiData); |
ocomeni | 81:637a87eb8170 | 674 | printf("[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd); |
ocomeni | 81:637a87eb8170 | 675 | return true; |
ocomeni | 81:637a87eb8170 | 676 | } |
ocomeni | 81:637a87eb8170 | 677 | |
ocomeni | 81:637a87eb8170 | 678 | bool ATCmdManager::dequeueATdataResponse(){ |
ocomeni | 81:637a87eb8170 | 679 | if(at_resp != AT_RESP_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 680 | osEvent evt = _wiFi2ATDataQueue->get(0); |
ocomeni | 81:637a87eb8170 | 681 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 682 | resp_data = (at_data_msg_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 683 | setNextResponse(resp_data->at_resp); |
ocomeni | 81:637a87eb8170 | 684 | //_wiFi2ATDatamPool->free(resp_data); |
ocomeni | 81:637a87eb8170 | 685 | } |
ocomeni | 81:637a87eb8170 | 686 | return true; |
ocomeni | 81:637a87eb8170 | 687 | } |
ocomeni | 81:637a87eb8170 | 688 | |
ocomeni | 81:637a87eb8170 | 689 | |
ocomeni | 81:637a87eb8170 | 690 |