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