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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Thu Mar 28 23:41:10 2019 +0000
Revision:
86:04fc2fcda7ec
Parent:
85:9f896e1e041c
Child:
87:99b37d26ff2a
http request correctly passed to WifiManager

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 74:f26e846adfe9 1 #include "ATCmdManager.h"
ocomeni 74:f26e846adfe9 2 #include "common_config.h"
ocomeni 79:a2187bbfa407 3 #include "common_types.h"
ocomeni 84:7c7add00f4bf 4 #include "http_request.h"
ocomeni 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 86:04fc2fcda7ec 233 if(strstr(s1, "Host")!= NULL){
ocomeni 86:04fc2fcda7ec 234 http_req.hostName.assign(s2);
ocomeni 86:04fc2fcda7ec 235 }
ocomeni 86:04fc2fcda7ec 236 else if(strstr(s1, "Accept")!= NULL){
ocomeni 86:04fc2fcda7ec 237 http_req.AcceptVal.assign(s2);
ocomeni 86:04fc2fcda7ec 238 }
ocomeni 86:04fc2fcda7ec 239 else if(strstr(s1, "Content-Type")!= NULL){
ocomeni 86:04fc2fcda7ec 240 http_req.contentType.assign(s2);
ocomeni 86:04fc2fcda7ec 241 }
ocomeni 86:04fc2fcda7ec 242 else if(strstr(s1, "Content-Length")!= NULL){
ocomeni 86:04fc2fcda7ec 243 http_req.contentLen.assign(s2);
ocomeni 84:7c7add00f4bf 244 }
ocomeni 84:7c7add00f4bf 245
ocomeni 84:7c7add00f4bf 246
ocomeni 84:7c7add00f4bf 247 }
ocomeni 84:7c7add00f4bf 248 nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line
ocomeni 84:7c7add00f4bf 249 if(nxtPtr >= p2) break;
ocomeni 84:7c7add00f4bf 250 }
ocomeni 84:7c7add00f4bf 251 int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr);
ocomeni 84:7c7add00f4bf 252 printf("\nLen = %d\n", bodyLen);
ocomeni 84:7c7add00f4bf 253 memcpy(http_req.body, bodyPtr, bodyLen);
ocomeni 84:7c7add00f4bf 254 // package and send on wifi data queue
ocomeni 84:7c7add00f4bf 255 wifi_data_msg_t data_msg;
ocomeni 84:7c7add00f4bf 256 data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ;
ocomeni 85:9f896e1e041c 257 data_msg.dataLen = sizeof(http_req.method) +
ocomeni 85:9f896e1e041c 258 sizeof(http_req.body) +
ocomeni 85:9f896e1e041c 259 http_req.request_URI.capacity() +
ocomeni 85:9f896e1e041c 260 http_req.http_version.capacity() +
ocomeni 85:9f896e1e041c 261 http_req.hostName.capacity() +
ocomeni 85:9f896e1e041c 262 http_req.AcceptVal.capacity() +
ocomeni 85:9f896e1e041c 263 http_req.contentType.capacity() +
ocomeni 85:9f896e1e041c 264 http_req.contentLen.capacity();
ocomeni 84:7c7add00f4bf 265 memcpy(data_msg.buffer,&http_req, data_msg.dataLen);
ocomeni 84:7c7add00f4bf 266 // queue next data request
ocomeni 84:7c7add00f4bf 267 queueWiFiDataRequest(data_msg);
ocomeni 84:7c7add00f4bf 268 return true;
ocomeni 84:7c7add00f4bf 269 }
ocomeni 74:f26e846adfe9 270 // OOB processing
ocomeni 74:f26e846adfe9 271 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
ocomeni 74:f26e846adfe9 272 set_timeout(timeout);
ocomeni 83:9c271a50a70b 273 static int cnt = 0;
ocomeni 84:7c7add00f4bf 274 int start;
ocomeni 83:9c271a50a70b 275 if(dataMode == AT_EXT_DATA_MODE)
ocomeni 83:9c271a50a70b 276 {
ocomeni 83:9c271a50a70b 277 int n;
ocomeni 83:9c271a50a70b 278 if(cnt++ % 10 == 0)printf("In EDM mode\n");
ocomeni 84:7c7add00f4bf 279 uint8_t edm[EDM_HDR_LEN];
ocomeni 84:7c7add00f4bf 280
ocomeni 83:9c271a50a70b 281 // Poll for edm packets
ocomeni 83:9c271a50a70b 282 do{
ocomeni 84:7c7add00f4bf 283 n = _parser.read((char *)edm, EDM_HDR_LEN);
ocomeni 84:7c7add00f4bf 284 edm_hdr.startByte = edm[0];
ocomeni 84:7c7add00f4bf 285 edm_hdr.payloadLen = edm[1]*256 + edm[2];
ocomeni 84:7c7add00f4bf 286 edm_hdr.payloadID = edm[3]*256 + edm[4];
ocomeni 84:7c7add00f4bf 287 start = Kernel::get_ms_count();
ocomeni 83:9c271a50a70b 288 if(n == -1) break; // break if it times out
ocomeni 83:9c271a50a70b 289 printf("%d bytes read!\n", n);
ocomeni 83:9c271a50a70b 290 if(n==5)
ocomeni 83:9c271a50a70b 291 printf("Start = %d, payloadID = %d len = %d\n", edm_hdr.startByte,
ocomeni 83:9c271a50a70b 292 edm_hdr.payloadID,
ocomeni 83:9c271a50a70b 293 edm_hdr.payloadLen);
ocomeni 83:9c271a50a70b 294 if(n == EDM_HDR_LEN && validate(edm_hdr)) // if AT command use process oob to decode
ocomeni 83:9c271a50a70b 295 {
ocomeni 83:9c271a50a70b 296 if(edm_hdr.payloadID == AT_REQUEST)
ocomeni 83:9c271a50a70b 297 {
ocomeni 83:9c271a50a70b 298 _parser.process_oob();
ocomeni 83:9c271a50a70b 299 break;
ocomeni 83:9c271a50a70b 300 }
ocomeni 83:9c271a50a70b 301 else
ocomeni 83:9c271a50a70b 302 {
ocomeni 83:9c271a50a70b 303 int pLen = edm_hdr.payloadLen-1;
ocomeni 83:9c271a50a70b 304 rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte
ocomeni 83:9c271a50a70b 305 rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected
ocomeni 83:9c271a50a70b 306 n = _parser.read((char *)rx_buf_ptr, pLen);
ocomeni 83:9c271a50a70b 307 if(n == -1) break; // timeout!
ocomeni 83:9c271a50a70b 308 printf("%d bytes read - expected %d!\n", n, pLen);
ocomeni 83:9c271a50a70b 309 for(int i =0;i<pLen;i++){
ocomeni 83:9c271a50a70b 310 if(i%8==0) printf("\n[%3d]",i/8);
ocomeni 83:9c271a50a70b 311 printf("%2x ", rx_buf_ptr[i]);
ocomeni 83:9c271a50a70b 312 }
ocomeni 83:9c271a50a70b 313 printf("\n");
ocomeni 83:9c271a50a70b 314 printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
ocomeni 83:9c271a50a70b 315 if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
ocomeni 83:9c271a50a70b 316 break; // exit if stop byte now found - possible data corruption!
ocomeni 83:9c271a50a70b 317 }
ocomeni 83:9c271a50a70b 318 switch(edm_hdr.payloadID)
ocomeni 83:9c271a50a70b 319 {
ocomeni 83:9c271a50a70b 320 case CONNECT_EVENT:
ocomeni 83:9c271a50a70b 321 printf("Connection Event received!\n");
ocomeni 83:9c271a50a70b 322 break;
ocomeni 83:9c271a50a70b 323 case DISCONNECT_EVENT:
ocomeni 83:9c271a50a70b 324 printf("DISCONNECT_EVENT received!\n");
ocomeni 83:9c271a50a70b 325 break;
ocomeni 83:9c271a50a70b 326 case DATA_EVENT:
ocomeni 83:9c271a50a70b 327 printf("DATA_EVENT received!\n");
ocomeni 83:9c271a50a70b 328 break;
ocomeni 83:9c271a50a70b 329 case DATA_COMMAND:
ocomeni 83:9c271a50a70b 330 printf("DATA_COMMAND received!\n");
ocomeni 84:7c7add00f4bf 331 if(createHttpRequest() == true){
ocomeni 84:7c7add00f4bf 332 _parser.send("OK");
ocomeni 83:9c271a50a70b 333 }
ocomeni 84:7c7add00f4bf 334 else{
ocomeni 84:7c7add00f4bf 335 _parser.send("NACK");
ocomeni 84:7c7add00f4bf 336 }
ocomeni 84:7c7add00f4bf 337 int stop = Kernel::get_ms_count();
ocomeni 84:7c7add00f4bf 338 printf("\n Time Elapsed = %d\n", stop-start);
ocomeni 83:9c271a50a70b 339 break;
ocomeni 83:9c271a50a70b 340 case AT_REQUEST:
ocomeni 83:9c271a50a70b 341 printf("AT_REQUEST received!\n");
ocomeni 83:9c271a50a70b 342 break;
ocomeni 83:9c271a50a70b 343 case AT_CONFIRMATION:
ocomeni 83:9c271a50a70b 344 printf("AT_CONFIRMATION received!\n");
ocomeni 83:9c271a50a70b 345 break;
ocomeni 83:9c271a50a70b 346 case AT_EVENT:
ocomeni 83:9c271a50a70b 347 printf("AT_EVENT received!\n");
ocomeni 83:9c271a50a70b 348 break;
ocomeni 83:9c271a50a70b 349 default:
ocomeni 83:9c271a50a70b 350 printf("UNKNOWN MESSAGE received!\n");
ocomeni 83:9c271a50a70b 351 break;
ocomeni 83:9c271a50a70b 352 }
ocomeni 83:9c271a50a70b 353 }
ocomeni 83:9c271a50a70b 354 //_parser.process_oob();
ocomeni 83:9c271a50a70b 355 }
ocomeni 83:9c271a50a70b 356 else // incorrect # of bytes received abort!!
ocomeni 83:9c271a50a70b 357 {
ocomeni 83:9c271a50a70b 358 break;
ocomeni 83:9c271a50a70b 359 }
ocomeni 83:9c271a50a70b 360 }while (all); // continue to process until timeout
ocomeni 74:f26e846adfe9 361 }
ocomeni 83:9c271a50a70b 362 else
ocomeni 83:9c271a50a70b 363 {
ocomeni 83:9c271a50a70b 364 // Poll for inbound packets
ocomeni 83:9c271a50a70b 365 while (_parser.process_oob() && all) {
ocomeni 83:9c271a50a70b 366 }
ocomeni 83:9c271a50a70b 367 }
ocomeni 74:f26e846adfe9 368 set_timeout();
ocomeni 74:f26e846adfe9 369 }
ocomeni 74:f26e846adfe9 370
ocomeni 74:f26e846adfe9 371
ocomeni 74:f26e846adfe9 372 // OOB message handlers
ocomeni 74:f26e846adfe9 373 void ATCmdManager::_oob_startup_hdlr(){
ocomeni 74:f26e846adfe9 374 }
ocomeni 74:f26e846adfe9 375
ocomeni 81:637a87eb8170 376 void ATCmdManager::_oob_ok_hdlr(){
ocomeni 84:7c7add00f4bf 377 //_smutex.lock();
ocomeni 81:637a87eb8170 378 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 379 //_smutex.unlock();
ocomeni 81:637a87eb8170 380 }
ocomeni 81:637a87eb8170 381
ocomeni 74:f26e846adfe9 382
ocomeni 74:f26e846adfe9 383 void ATCmdManager::_oob_bleRole_hdlr(){
ocomeni 74:f26e846adfe9 384 }
ocomeni 74:f26e846adfe9 385
ocomeni 74:f26e846adfe9 386
ocomeni 74:f26e846adfe9 387 void ATCmdManager::_oob_wifiMode_err(){
ocomeni 74:f26e846adfe9 388 }
ocomeni 74:f26e846adfe9 389
ocomeni 74:f26e846adfe9 390
ocomeni 74:f26e846adfe9 391 void ATCmdManager::_oob_conn_already(){
ocomeni 74:f26e846adfe9 392 }
ocomeni 74:f26e846adfe9 393
ocomeni 74:f26e846adfe9 394
ocomeni 74:f26e846adfe9 395 void ATCmdManager::_oob_err(){
ocomeni 74:f26e846adfe9 396 }
ocomeni 74:f26e846adfe9 397
ocomeni 79:a2187bbfa407 398 void ATCmdManager::_oob_get_fw_ver()
ocomeni 79:a2187bbfa407 399 {
ocomeni 79:a2187bbfa407 400 }
ocomeni 79:a2187bbfa407 401
ocomeni 79:a2187bbfa407 402
ocomeni 75:08eff6258e1b 403 void ATCmdManager::_oob_uart_setup(){
ocomeni 75:08eff6258e1b 404 int uOpts[NUM_UART_OPTIONS];
ocomeni 75:08eff6258e1b 405 //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
ocomeni 75:08eff6258e1b 406 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 407 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 408 //AT+UMRS=230400,2,8,1,1,1
ocomeni 81:637a87eb8170 409 printf("\n Changing Baud Rate to %d\n", uOpts[0]);
ocomeni 81:637a87eb8170 410
ocomeni 81:637a87eb8170 411 _serial.set_baud(uOpts[0]);
ocomeni 81:637a87eb8170 412 printf("\n Baud Rate now %d\n", uOpts[0]);
ocomeni 81:637a87eb8170 413
ocomeni 75:08eff6258e1b 414 } else {
ocomeni 75:08eff6258e1b 415 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 416 }
ocomeni 75:08eff6258e1b 417 }
ocomeni 75:08eff6258e1b 418
ocomeni 74:f26e846adfe9 419 void ATCmdManager::set_timeout(uint32_t timeout_ms)
ocomeni 74:f26e846adfe9 420 {
ocomeni 74:f26e846adfe9 421 _parser.set_timeout(timeout_ms);
ocomeni 74:f26e846adfe9 422 }
ocomeni 74:f26e846adfe9 423
ocomeni 75:08eff6258e1b 424
ocomeni 75:08eff6258e1b 425 void ATCmdManager::_oob_echo_off()
ocomeni 75:08eff6258e1b 426 {
ocomeni 84:7c7add00f4bf 427 //_smutex.lock();
ocomeni 75:08eff6258e1b 428 printf("\n Received ATEO OOB command!!\n");
ocomeni 75:08eff6258e1b 429 printf("\n turning echo OFF!!\n");
ocomeni 75:08eff6258e1b 430 _parser.debug_on(false);
ocomeni 75:08eff6258e1b 431 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 432 //_smutex.unlock();
ocomeni 75:08eff6258e1b 433 }
ocomeni 75:08eff6258e1b 434
ocomeni 75:08eff6258e1b 435
ocomeni 75:08eff6258e1b 436 void ATCmdManager::_oob_echo_on()
ocomeni 75:08eff6258e1b 437 {
ocomeni 84:7c7add00f4bf 438 //_smutex.lock();
ocomeni 75:08eff6258e1b 439 printf("\n Received ATE1 OOB command!!\n");
ocomeni 75:08eff6258e1b 440 printf("\n turning echo ON!!\n");
ocomeni 75:08eff6258e1b 441 _parser.debug_on(true);
ocomeni 75:08eff6258e1b 442 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 443 //_smutex.unlock();
ocomeni 75:08eff6258e1b 444 }
ocomeni 75:08eff6258e1b 445
ocomeni 75:08eff6258e1b 446
ocomeni 75:08eff6258e1b 447 void ATCmdManager::_oob_data_mode(){
ocomeni 84:7c7add00f4bf 448 //_smutex.lock();
ocomeni 75:08eff6258e1b 449 printf("\n Received EDM mode command!!\n");
ocomeni 75:08eff6258e1b 450 if(_parser.scanf("%d", &dataMode) >0) {
ocomeni 75:08eff6258e1b 451 printf("\nATCmdParser: Data mode=%d\n", dataMode);
ocomeni 75:08eff6258e1b 452 switch(dataMode)
ocomeni 75:08eff6258e1b 453 {
ocomeni 75:08eff6258e1b 454 case 0:
ocomeni 83:9c271a50a70b 455 printf("\nATCmdParser: Command Mode request received\n");
ocomeni 81:637a87eb8170 456 dataMode = AT_CMD_DATA_MODE;
ocomeni 75:08eff6258e1b 457 break;
ocomeni 75:08eff6258e1b 458 case 1:
ocomeni 83:9c271a50a70b 459 printf("\nATCmdParser: Data Mode request received\n");
ocomeni 81:637a87eb8170 460 dataMode = AT_STD_DATA_MODE;
ocomeni 75:08eff6258e1b 461 break;
ocomeni 75:08eff6258e1b 462 case 2:
ocomeni 83:9c271a50a70b 463 printf("\nATCmdParser: Extended data Mode request received\n");
ocomeni 81:637a87eb8170 464 dataMode = AT_EXT_DATA_MODE;
ocomeni 75:08eff6258e1b 465 break;
ocomeni 75:08eff6258e1b 466 default:
ocomeni 75:08eff6258e1b 467 printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 468 break;
ocomeni 75:08eff6258e1b 469 }
ocomeni 75:08eff6258e1b 470 } else {
ocomeni 83:9c271a50a70b 471 printf("\nATCmdParser: Retrieving Uart Options failed\n");
ocomeni 75:08eff6258e1b 472 }
ocomeni 75:08eff6258e1b 473 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 474 //_smutex.unlock();
ocomeni 75:08eff6258e1b 475 }
ocomeni 75:08eff6258e1b 476
ocomeni 75:08eff6258e1b 477 void ATCmdManager::_oob_get_mac_addr(){
ocomeni 75:08eff6258e1b 478 int bleOrWifi;
ocomeni 84:7c7add00f4bf 479 //_smutex.lock();
ocomeni 75:08eff6258e1b 480 if(_parser.scanf("=%d", &bleOrWifi) >0) {
ocomeni 75:08eff6258e1b 481 switch(bleOrWifi)
ocomeni 75:08eff6258e1b 482 {
ocomeni 75:08eff6258e1b 483 case 1:
ocomeni 75:08eff6258e1b 484 printf("\nATCmdParser: BLE MAC Address request received");
ocomeni 75:08eff6258e1b 485 break;
ocomeni 75:08eff6258e1b 486 case 2:
ocomeni 75:08eff6258e1b 487 printf("\nATCmdParser: WiFi MAC Address request received");
ocomeni 75:08eff6258e1b 488 break;
ocomeni 75:08eff6258e1b 489 default:
ocomeni 75:08eff6258e1b 490 printf("\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 491 break;
ocomeni 75:08eff6258e1b 492 }
ocomeni 75:08eff6258e1b 493 } else {
ocomeni 75:08eff6258e1b 494 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 495 }
ocomeni 75:08eff6258e1b 496 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 497 //_smutex.unlock();
ocomeni 75:08eff6258e1b 498 }
ocomeni 75:08eff6258e1b 499
ocomeni 75:08eff6258e1b 500 void ATCmdManager::_oob_get_ble_role(){
ocomeni 84:7c7add00f4bf 501 //_smutex.lock();
ocomeni 75:08eff6258e1b 502 printf("\n Received get BLE role command!!\n");
ocomeni 75:08eff6258e1b 503 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 504 //_smutex.unlock();
ocomeni 75:08eff6258e1b 505 }
ocomeni 75:08eff6258e1b 506
ocomeni 75:08eff6258e1b 507 void ATCmdManager::_oob_ena_ble_peri(){
ocomeni 84:7c7add00f4bf 508 //_smutex.lock();
ocomeni 75:08eff6258e1b 509 printf("\n Received enable BLE Peripheral command!!\n");
ocomeni 75:08eff6258e1b 510 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 511 //_smutex.unlock();
ocomeni 75:08eff6258e1b 512 }
ocomeni 75:08eff6258e1b 513
ocomeni 75:08eff6258e1b 514 void ATCmdManager::_oob_reboot(){
ocomeni 84:7c7add00f4bf 515 //_smutex.lock();
ocomeni 75:08eff6258e1b 516 printf("\n Received reboot command!!\n");
ocomeni 75:08eff6258e1b 517 _parser.send("OK\n");
ocomeni 75:08eff6258e1b 518 _parser.send("System Resetting....\n");
ocomeni 75:08eff6258e1b 519 system_reset();
ocomeni 84:7c7add00f4bf 520 //_smutex.unlock();
ocomeni 75:08eff6258e1b 521 }
ocomeni 75:08eff6258e1b 522
ocomeni 79:a2187bbfa407 523 const char * ATCmdManager::sec2str(nsapi_security_t sec)
ocomeni 79:a2187bbfa407 524 {
ocomeni 79:a2187bbfa407 525 switch (sec) {
ocomeni 79:a2187bbfa407 526 case NSAPI_SECURITY_NONE:
ocomeni 79:a2187bbfa407 527 return "None";
ocomeni 79:a2187bbfa407 528 case NSAPI_SECURITY_WEP:
ocomeni 79:a2187bbfa407 529 return "WEP";
ocomeni 79:a2187bbfa407 530 case NSAPI_SECURITY_WPA:
ocomeni 79:a2187bbfa407 531 return "WPA";
ocomeni 79:a2187bbfa407 532 case NSAPI_SECURITY_WPA2:
ocomeni 79:a2187bbfa407 533 return "WPA2";
ocomeni 79:a2187bbfa407 534 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 79:a2187bbfa407 535 return "WPA/WPA2";
ocomeni 79:a2187bbfa407 536 case NSAPI_SECURITY_UNKNOWN:
ocomeni 79:a2187bbfa407 537 default:
ocomeni 79:a2187bbfa407 538 return "Unknown";
ocomeni 79:a2187bbfa407 539 }
ocomeni 79:a2187bbfa407 540 }
ocomeni 81:637a87eb8170 541
ocomeni 81:637a87eb8170 542 bool ATCmdManager::setNextResponse(at_cmd_resp_t resp)
ocomeni 81:637a87eb8170 543 {
ocomeni 81:637a87eb8170 544 if(at_resp == AT_RESP_NONE){
ocomeni 81:637a87eb8170 545 at_resp = resp;
ocomeni 81:637a87eb8170 546 return true; // success
ocomeni 81:637a87eb8170 547 }
ocomeni 81:637a87eb8170 548 return false; // wiFiManager busy
ocomeni 79:a2187bbfa407 549 }
ocomeni 79:a2187bbfa407 550
ocomeni 81:637a87eb8170 551
ocomeni 79:a2187bbfa407 552 void ATCmdManager::_oob_scanWiFiNetworks(){
ocomeni 84:7c7add00f4bf 553 //_smutex.lock();
ocomeni 79:a2187bbfa407 554 printf("\n Received scanWiFiNetworks command!!\n");
ocomeni 75:08eff6258e1b 555 _parser.send("OK\n");
ocomeni 84:7c7add00f4bf 556 //_smutex.unlock();
ocomeni 79:a2187bbfa407 557 wifi_cmd_t cmd = WIFI_CMD_SCAN;
ocomeni 79:a2187bbfa407 558 // queue next command
ocomeni 79:a2187bbfa407 559 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 560 return;
ocomeni 75:08eff6258e1b 561 }
ocomeni 75:08eff6258e1b 562
ocomeni 79:a2187bbfa407 563 void ATCmdManager::_oob_connect2WiFiNetwork()
ocomeni 79:a2187bbfa407 564 {
ocomeni 81:637a87eb8170 565 wifi_cmd_t cmd = WIFI_CMD_CONNECT;
ocomeni 81:637a87eb8170 566 // queue next command
ocomeni 81:637a87eb8170 567 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 568 return;
ocomeni 79:a2187bbfa407 569 }
ocomeni 79:a2187bbfa407 570
ocomeni 79:a2187bbfa407 571
ocomeni 79:a2187bbfa407 572 void ATCmdManager::_oob_disconnectWiFiNetwork()
ocomeni 79:a2187bbfa407 573 {
ocomeni 81:637a87eb8170 574 wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
ocomeni 81:637a87eb8170 575 // queue next command
ocomeni 81:637a87eb8170 576 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 577 return;
ocomeni 81:637a87eb8170 578 }
ocomeni 81:637a87eb8170 579
ocomeni 81:637a87eb8170 580 void ATCmdManager::_oob_setupInternetConnection()
ocomeni 81:637a87eb8170 581 {
ocomeni 85:9f896e1e041c 582 char str[200];
ocomeni 85:9f896e1e041c 583 char url[60];
ocomeni 81:637a87eb8170 584 int n;
ocomeni 81:637a87eb8170 585 internet_config_t internet_config;
ocomeni 85:9f896e1e041c 586 //_smutex.lock();
ocomeni 84:7c7add00f4bf 587 printf("sizeof internet_config_t = %d\n", sizeof(internet_config_t));
ocomeni 84:7c7add00f4bf 588 n = ReadBytes((uint8_t *)str, 199);
ocomeni 84:7c7add00f4bf 589 str[n]=NULL;
ocomeni 84:7c7add00f4bf 590 printf("\n read string = %s , n = %d\n", str, n);
ocomeni 84:7c7add00f4bf 591 n = sscanf(str, "=%1d,%199[^,],%1d", &internet_config.peer_id,
ocomeni 81:637a87eb8170 592 url,
ocomeni 81:637a87eb8170 593 &internet_config.connectionScheme);
ocomeni 84:7c7add00f4bf 594 strncpy(internet_config.url, url, strlen(url)+1);
ocomeni 84:7c7add00f4bf 595 printf("\n read string = %s , n = %d -- strlen(url) = %d\n", internet_config.url, n, strlen(url));
ocomeni 81:637a87eb8170 596 if(n>0)
ocomeni 81:637a87eb8170 597 {
ocomeni 81:637a87eb8170 598 printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 84:7c7add00f4bf 599 internet_config.url, ///->c_str(),
ocomeni 81:637a87eb8170 600 internet_config.connectionScheme);
ocomeni 81:637a87eb8170 601 // package and send on wifi data queue
ocomeni 84:7c7add00f4bf 602 wifi_data_msg_t data_msg; // = (wifi_data_msg_t *) malloc(sizeof(wifi_data_msg_t));
ocomeni 81:637a87eb8170 603 data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG;
ocomeni 84:7c7add00f4bf 604 data_msg.dataLen = sizeof(internet_config_t); // + strlen(internet_config.url);
ocomeni 84:7c7add00f4bf 605 printf("\n url size = %d url str = %s\n",strlen(internet_config.url), internet_config.url );
ocomeni 81:637a87eb8170 606 memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
ocomeni 82:10072c1794d3 607 // queue next data request
ocomeni 81:637a87eb8170 608 queueWiFiDataRequest(data_msg);
ocomeni 84:7c7add00f4bf 609 print_memory_info();
ocomeni 82:10072c1794d3 610 _parser.send("OK\n");
ocomeni 81:637a87eb8170 611 } else {
ocomeni 81:637a87eb8170 612 printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n);
ocomeni 82:10072c1794d3 613 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 614 }
ocomeni 84:7c7add00f4bf 615 //_smutex.unlock();
ocomeni 82:10072c1794d3 616 }
ocomeni 82:10072c1794d3 617
ocomeni 82:10072c1794d3 618 wifi_config_t ATCmdManager::init_wifi_config()
ocomeni 82:10072c1794d3 619 {
ocomeni 82:10072c1794d3 620 wifi_config_t wifi_cfg;
ocomeni 82:10072c1794d3 621 wifi_cfg.ssid[0] = NULL;
ocomeni 82:10072c1794d3 622 wifi_cfg.pass[0] = NULL;
ocomeni 82:10072c1794d3 623 wifi_cfg.security = NSAPI_SECURITY_UNKNOWN;
ocomeni 82:10072c1794d3 624 return wifi_cfg;
ocomeni 82:10072c1794d3 625 }
ocomeni 83:9c271a50a70b 626 /* read ASCII characters into buffer and null terminate */
ocomeni 83:9c271a50a70b 627 int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes)
ocomeni 82:10072c1794d3 628 {
ocomeni 82:10072c1794d3 629 int c;
ocomeni 82:10072c1794d3 630 int sptr = 0;
ocomeni 82:10072c1794d3 631 for(int i=0;i<maxBytes;i++){
ocomeni 82:10072c1794d3 632 c = _parser.getc();
ocomeni 82:10072c1794d3 633 if(c==-1){
ocomeni 83:9c271a50a70b 634 buf[sptr] = NULL; // null terminate if string
ocomeni 82:10072c1794d3 635 return i;
ocomeni 82:10072c1794d3 636 }
ocomeni 82:10072c1794d3 637 if(c != ',' && c!= '"'){
ocomeni 82:10072c1794d3 638 buf[sptr++] = (uint8_t) c;
ocomeni 82:10072c1794d3 639 }
ocomeni 81:637a87eb8170 640 }
ocomeni 83:9c271a50a70b 641 return maxBytes;
ocomeni 83:9c271a50a70b 642 }
ocomeni 83:9c271a50a70b 643
ocomeni 83:9c271a50a70b 644 int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes)
ocomeni 83:9c271a50a70b 645 {
ocomeni 83:9c271a50a70b 646 int c;
ocomeni 83:9c271a50a70b 647 int sptr = 0;
ocomeni 83:9c271a50a70b 648 for(int i=0;i<maxBytes;i++){
ocomeni 83:9c271a50a70b 649 c = _parser.getc();
ocomeni 83:9c271a50a70b 650 if(c==-1){
ocomeni 83:9c271a50a70b 651 return i;
ocomeni 83:9c271a50a70b 652 }
ocomeni 83:9c271a50a70b 653 buf[sptr++] = (uint8_t) c;
ocomeni 83:9c271a50a70b 654 }
ocomeni 83:9c271a50a70b 655 return maxBytes;
ocomeni 82:10072c1794d3 656 }
ocomeni 82:10072c1794d3 657
ocomeni 82:10072c1794d3 658 void ATCmdManager::_oob_setWiFiSSID()
ocomeni 82:10072c1794d3 659 {
ocomeni 82:10072c1794d3 660 int n;
ocomeni 82:10072c1794d3 661 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 84:7c7add00f4bf 662 //_smutex.lock();
ocomeni 83:9c271a50a70b 663 n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32);
ocomeni 82:10072c1794d3 664 printf("[ATCMD MAN]: number of bytes read = %d\n", n);
ocomeni 82:10072c1794d3 665 if(n>0)
ocomeni 82:10072c1794d3 666 {
ocomeni 82:10072c1794d3 667 printf("[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid);
ocomeni 82:10072c1794d3 668 // package and send on wifi data queue
ocomeni 82:10072c1794d3 669 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 670 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 671 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 672 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 673 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 674 _parser.send("OK\n");
ocomeni 82:10072c1794d3 675 } else {
ocomeni 82:10072c1794d3 676 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 677 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 678 }
ocomeni 84:7c7add00f4bf 679 //_smutex.unlock();
ocomeni 82:10072c1794d3 680 }
ocomeni 82:10072c1794d3 681
ocomeni 82:10072c1794d3 682 void ATCmdManager::_oob_setWiFiPWD()
ocomeni 82:10072c1794d3 683 {
ocomeni 82:10072c1794d3 684 int n;
ocomeni 82:10072c1794d3 685 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 84:7c7add00f4bf 686 //_smutex.lock();
ocomeni 82:10072c1794d3 687 //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass);
ocomeni 83:9c271a50a70b 688 n = readStringBytes((uint8_t *)wifi_cfg.pass, 32);
ocomeni 82:10072c1794d3 689 if(n>0)
ocomeni 82:10072c1794d3 690 {
ocomeni 82:10072c1794d3 691 printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass);
ocomeni 82:10072c1794d3 692 // package and send on wifi data queue
ocomeni 82:10072c1794d3 693 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 694 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 695 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 696 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 697 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 698 _parser.send("OK\n");
ocomeni 82:10072c1794d3 699 } else {
ocomeni 82:10072c1794d3 700 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 701 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 702 }
ocomeni 84:7c7add00f4bf 703 //_smutex.unlock();
ocomeni 82:10072c1794d3 704 }
ocomeni 82:10072c1794d3 705
ocomeni 82:10072c1794d3 706 void ATCmdManager::_oob_setWiFiSecurity()
ocomeni 82:10072c1794d3 707 {
ocomeni 82:10072c1794d3 708 int n;
ocomeni 82:10072c1794d3 709 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 84:7c7add00f4bf 710 //_smutex.lock();
ocomeni 82:10072c1794d3 711 n = _parser.scanf(",%d", &wifi_cfg.security);
ocomeni 82:10072c1794d3 712 if(n>0)
ocomeni 82:10072c1794d3 713 {
ocomeni 82:10072c1794d3 714 printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
ocomeni 82:10072c1794d3 715 // package and send on wifi data queue
ocomeni 82:10072c1794d3 716 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 717 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 718 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 719 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 720 queueWiFiDataRequest(data_msg);
ocomeni 82:10072c1794d3 721 _parser.send("OK\n");
ocomeni 82:10072c1794d3 722 } else {
ocomeni 82:10072c1794d3 723 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 724 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 725 }
ocomeni 84:7c7add00f4bf 726 //_smutex.unlock();
ocomeni 79:a2187bbfa407 727 }
ocomeni 81:637a87eb8170 728
ocomeni 83:9c271a50a70b 729
ocomeni 83:9c271a50a70b 730 void ATCmdManager::_oob_sendHttpMessage()
ocomeni 83:9c271a50a70b 731 {
ocomeni 83:9c271a50a70b 732 int n;
ocomeni 84:7c7add00f4bf 733 http_request_t http_post_request;
ocomeni 84:7c7add00f4bf 734 //_smutex.lock();
ocomeni 83:9c271a50a70b 735 /*
ocomeni 83:9c271a50a70b 736 n = _parser.scanf(",%d", &wifi_cfg.security);
ocomeni 83:9c271a50a70b 737 if(n>0)
ocomeni 83:9c271a50a70b 738 {
ocomeni 83:9c271a50a70b 739 printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
ocomeni 83:9c271a50a70b 740 // package and send on wifi data queue
ocomeni 83:9c271a50a70b 741 wifi_data_msg_t data_msg;
ocomeni 83:9c271a50a70b 742 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 83:9c271a50a70b 743 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 83:9c271a50a70b 744 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 83:9c271a50a70b 745 queueWiFiDataRequest(data_msg);
ocomeni 83:9c271a50a70b 746 _parser.send("OK\n");
ocomeni 83:9c271a50a70b 747 } else {
ocomeni 83:9c271a50a70b 748 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 83:9c271a50a70b 749 _parser.send("NAK\n");
ocomeni 83:9c271a50a70b 750 }
ocomeni 83:9c271a50a70b 751 */
ocomeni 84:7c7add00f4bf 752 //_smutex.unlock();
ocomeni 83:9c271a50a70b 753
ocomeni 83:9c271a50a70b 754 }
ocomeni 83:9c271a50a70b 755
ocomeni 81:637a87eb8170 756 bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
ocomeni 81:637a87eb8170 757 wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
ocomeni 81:637a87eb8170 758 wifiCmd->wifi_cmd = cmd;
ocomeni 81:637a87eb8170 759 _aT2WiFiCmdQueue->put(wifiCmd);
ocomeni 81:637a87eb8170 760 return true;
ocomeni 81:637a87eb8170 761 }
ocomeni 81:637a87eb8170 762
ocomeni 81:637a87eb8170 763 bool ATCmdManager::dequeueATresponse(){
ocomeni 81:637a87eb8170 764 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 765 osEvent evt = _wiFi2ATCmdQueue->get(0);
ocomeni 81:637a87eb8170 766 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 767 at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
ocomeni 81:637a87eb8170 768 setNextResponse(resp->at_resp);
ocomeni 81:637a87eb8170 769 _wiFi2ATmPool->free(resp);
ocomeni 81:637a87eb8170 770 }
ocomeni 81:637a87eb8170 771 return true;
ocomeni 81:637a87eb8170 772 }
ocomeni 81:637a87eb8170 773
ocomeni 81:637a87eb8170 774 bool ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
ocomeni 81:637a87eb8170 775 wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
ocomeni 81:637a87eb8170 776 wifiData->wifi_cmd = data_req.wifi_cmd;
ocomeni 81:637a87eb8170 777 wifiData->dataLen = data_req.dataLen;
ocomeni 81:637a87eb8170 778 memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen);
ocomeni 81:637a87eb8170 779 _aT2WiFiDataQueue->put(wifiData);
ocomeni 81:637a87eb8170 780 printf("[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd);
ocomeni 84:7c7add00f4bf 781 //free(&data_req);
ocomeni 81:637a87eb8170 782 return true;
ocomeni 81:637a87eb8170 783 }
ocomeni 81:637a87eb8170 784
ocomeni 81:637a87eb8170 785 bool ATCmdManager::dequeueATdataResponse(){
ocomeni 81:637a87eb8170 786 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 787 osEvent evt = _wiFi2ATDataQueue->get(0);
ocomeni 81:637a87eb8170 788 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 789 resp_data = (at_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 790 setNextResponse(resp_data->at_resp);
ocomeni 81:637a87eb8170 791 //_wiFi2ATDatamPool->free(resp_data);
ocomeni 81:637a87eb8170 792 }
ocomeni 81:637a87eb8170 793 return true;
ocomeni 81:637a87eb8170 794 }
ocomeni 81:637a87eb8170 795
ocomeni 81:637a87eb8170 796
ocomeni 81:637a87eb8170 797