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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Mar 31 22:09:45 2019 +0000
Revision:
88:7ffa053be662
Parent:
87:99b37d26ff2a
Child:
89:45f6db09a76d
tls connection now working.

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