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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Apr 14 14:38:51 2019 +0000
Revision:
93:06e755a80187
Parent:
92:ec9550034276
Child:
95:290859010c8c
fixed bug with wifi configuration API (was adding an extra character causing wifi connection to fail because of incorrect credentials)

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 92:ec9550034276 5 #include "mbed_memory_status.h"
ocomeni 84:7c7add00f4bf 6 //#include "mbed_memory_status.h"
ocomeni 74:f26e846adfe9 7
ocomeni 78:07bb86e3ce14 8 ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral,
ocomeni 79:a2187bbfa407 9 events::EventQueue &event_queue, WiFiManager *wifi,
ocomeni 79:a2187bbfa407 10 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool,
ocomeni 80:e8f0e92e3ac9 11 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
ocomeni 81:637a87eb8170 12 MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool,
ocomeni 81:637a87eb8170 13 Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue,
ocomeni 87:99b37d26ff2a 14 MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool,
ocomeni 87:99b37d26ff2a 15 Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue,
ocomeni 87:99b37d26ff2a 16 MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool,
ocomeni 87:99b37d26ff2a 17 Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue,
ocomeni 79:a2187bbfa407 18 bool debug)
ocomeni 74:f26e846adfe9 19 :
ocomeni 74:f26e846adfe9 20 _serial(tx, rx, DEFAULT_BAUD_RATE),
ocomeni 78:07bb86e3ce14 21 blePeripheral(blePeripheral),
ocomeni 79:a2187bbfa407 22 _event_queue(event_queue),
ocomeni 79:a2187bbfa407 23 wiFiManager(wiFiManager),
ocomeni 79:a2187bbfa407 24 _aT2WiFimPool(aT2WiFimPool),
ocomeni 80:e8f0e92e3ac9 25 _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
ocomeni 80:e8f0e92e3ac9 26
ocomeni 80:e8f0e92e3ac9 27 _wiFi2ATmPool(wiFi2ATmPool),
ocomeni 80:e8f0e92e3ac9 28 _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
ocomeni 80:e8f0e92e3ac9 29
ocomeni 80:e8f0e92e3ac9 30 _aT2WiFiDatamPool(aT2WiFiDatamPool),
ocomeni 80:e8f0e92e3ac9 31 _aT2WiFiDataQueue(aT2WiFiDataQueue),
ocomeni 80:e8f0e92e3ac9 32
ocomeni 80:e8f0e92e3ac9 33 _wiFi2ATDatamPool(wiFi2ATDatamPool),
ocomeni 80:e8f0e92e3ac9 34 _wiFi2ATDataQueue(wiFi2ATDataQueue),
ocomeni 80:e8f0e92e3ac9 35
ocomeni 80:e8f0e92e3ac9 36 _parser(&_serial)
ocomeni 80:e8f0e92e3ac9 37
ocomeni 74:f26e846adfe9 38
ocomeni 74:f26e846adfe9 39 {
ocomeni 74:f26e846adfe9 40 // constructor
ocomeni 74:f26e846adfe9 41 _serial.set_baud(DEFAULT_BAUD_RATE);
ocomeni 74:f26e846adfe9 42 _parser.debug_on(debug);
ocomeni 74:f26e846adfe9 43 _parser.set_delimiter("\r\n");
ocomeni 75:08eff6258e1b 44 _parser.send("+STARTUP");
ocomeni 81:637a87eb8170 45 _parser.oob("AT\n", callback(this, &ATCmdManager::_oob_ok_hdlr));
ocomeni 75:08eff6258e1b 46 _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off));
ocomeni 75:08eff6258e1b 47 _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on));
ocomeni 75:08eff6258e1b 48 _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup));
ocomeni 75:08eff6258e1b 49
ocomeni 75:08eff6258e1b 50 _parser.oob("ATO", callback(this, &ATCmdManager::_oob_data_mode));
ocomeni 75:08eff6258e1b 51 _parser.oob("AT+UMLA", callback(this, &ATCmdManager::_oob_get_mac_addr));
ocomeni 75:08eff6258e1b 52 _parser.oob("AT+UBTLE?", callback(this, &ATCmdManager::_oob_get_ble_role));
ocomeni 75:08eff6258e1b 53 _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri));
ocomeni 75:08eff6258e1b 54 _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot));
ocomeni 75:08eff6258e1b 55 _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
ocomeni 82:10072c1794d3 56 _parser.oob("AT+UWSCAN", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
ocomeni 79:a2187bbfa407 57 _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
ocomeni 81:637a87eb8170 58 _parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
ocomeni 81:637a87eb8170 59 _parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection));
ocomeni 82:10072c1794d3 60 _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID));
ocomeni 82:10072c1794d3 61 _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD));
ocomeni 82:10072c1794d3 62 _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity));
ocomeni 84:7c7add00f4bf 63 //_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage));
ocomeni 81:637a87eb8170 64
ocomeni 74:f26e846adfe9 65 printf("\n --- ATCmdManager constructor completed ---\n");
ocomeni 81:637a87eb8170 66 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 67 dataMode = AT_CMD_DATA_MODE;
ocomeni 92:ec9550034276 68 print_heap_and_isr_stack_info();
ocomeni 89:45f6db09a76d 69 event_queue.call_every(3600000,&print_memory_info);
ocomeni 74:f26e846adfe9 70 }
ocomeni 74:f26e846adfe9 71
ocomeni 74:f26e846adfe9 72
ocomeni 74:f26e846adfe9 73 void ATCmdManager::runMain(){
ocomeni 90:ed0267eca7b5 74 printf("\r\n [ATCMD MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
ocomeni 74:f26e846adfe9 75 while(true){
ocomeni 74:f26e846adfe9 76 _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
ocomeni 81:637a87eb8170 77 wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time
ocomeni 81:637a87eb8170 78 processResponses();
ocomeni 74:f26e846adfe9 79 }
ocomeni 74:f26e846adfe9 80
ocomeni 74:f26e846adfe9 81 }
ocomeni 74:f26e846adfe9 82
ocomeni 81:637a87eb8170 83 void ATCmdManager::processResponses(){
ocomeni 81:637a87eb8170 84 dequeueATresponse();
ocomeni 87:99b37d26ff2a 85 dequeueWiFidataResponse();
ocomeni 81:637a87eb8170 86 switch(at_resp){
ocomeni 81:637a87eb8170 87 case AT_RESP_NONE:
ocomeni 81:637a87eb8170 88 // IDLE response state
ocomeni 81:637a87eb8170 89 break;
ocomeni 81:637a87eb8170 90 case AT_SCAN_RESP:
ocomeni 81:637a87eb8170 91 // AT_SCAN_RESP response state
ocomeni 81:637a87eb8170 92 printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n");
ocomeni 90:ed0267eca7b5 93 _smutex.lock();
ocomeni 92:ec9550034276 94 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 95 _smutex.unlock();
ocomeni 81:637a87eb8170 96 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 97 break;
ocomeni 81:637a87eb8170 98 case AT_DETAILED_SCAN_RESP:
ocomeni 81:637a87eb8170 99 // AT_DETAILED_SCAN_RESP response state
ocomeni 81:637a87eb8170 100 printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n");
ocomeni 90:ed0267eca7b5 101 _smutex.lock();
ocomeni 92:ec9550034276 102 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 103 _smutex.unlock();
ocomeni 81:637a87eb8170 104 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 105 break;
ocomeni 81:637a87eb8170 106 case AT_CONNECT_RESP:
ocomeni 81:637a87eb8170 107 // AT_CONNECT_RESP response state
ocomeni 81:637a87eb8170 108 printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n");
ocomeni 90:ed0267eca7b5 109 _smutex.lock();
ocomeni 92:ec9550034276 110 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 111 _smutex.unlock();
ocomeni 81:637a87eb8170 112 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 113 break;
ocomeni 81:637a87eb8170 114 case AT_DISCONNECT_RESP:
ocomeni 81:637a87eb8170 115 // AT_DISCONNECT_RESP response state
ocomeni 84:7c7add00f4bf 116 //////_smutex.lock();
ocomeni 81:637a87eb8170 117 printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n");
ocomeni 90:ed0267eca7b5 118 _smutex.lock();
ocomeni 92:ec9550034276 119 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 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 90:ed0267eca7b5 127 _smutex.lock();
ocomeni 92:ec9550034276 128 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 129 _smutex.unlock();
ocomeni 84:7c7add00f4bf 130 at_resp = AT_RESP_NONE;
ocomeni 84:7c7add00f4bf 131 break;
ocomeni 84:7c7add00f4bf 132 case AT_INTERNET_CONFIG_RESP:
ocomeni 84:7c7add00f4bf 133 // AT_CONFIG_RESP response state
ocomeni 84:7c7add00f4bf 134 printf("\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\n");
ocomeni 90:ed0267eca7b5 135 _smutex.lock();
ocomeni 92:ec9550034276 136 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 137 _smutex.unlock();
ocomeni 84:7c7add00f4bf 138 at_resp = AT_RESP_NONE;
ocomeni 84:7c7add00f4bf 139 break;
ocomeni 84:7c7add00f4bf 140 case AT_HTTPS_RESP:
ocomeni 84:7c7add00f4bf 141 // AT_HTTP_RESP response state
ocomeni 84:7c7add00f4bf 142 printf("\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\n");
ocomeni 87:99b37d26ff2a 143 return_response();
ocomeni 90:ed0267eca7b5 144 _smutex.lock();
ocomeni 92:ec9550034276 145 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 146 _smutex.unlock();
ocomeni 81:637a87eb8170 147 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 148 break;
ocomeni 89:45f6db09a76d 149 case AT_HTTPS_RESP_DOWNLOAD:
ocomeni 89:45f6db09a76d 150 // AT_HTTPS_RESP_DOWNLOAD response state
ocomeni 89:45f6db09a76d 151 printf("\n [ATCMD MAN] WIFI HTTPS DOWNLOAD RESPONSE RECEIVED!!\n");
ocomeni 89:45f6db09a76d 152 return_response(true); // set download paramter to true
ocomeni 90:ed0267eca7b5 153 _smutex.lock();
ocomeni 92:ec9550034276 154 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 155 _smutex.unlock();
ocomeni 89:45f6db09a76d 156 at_resp = AT_RESP_NONE;
ocomeni 89:45f6db09a76d 157 break;
ocomeni 81:637a87eb8170 158 case AT_HTTP_RESP:
ocomeni 81:637a87eb8170 159 // AT_HTTP_RESP response state
ocomeni 81:637a87eb8170 160 printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
ocomeni 89:45f6db09a76d 161 return_response();
ocomeni 90:ed0267eca7b5 162 _smutex.lock();
ocomeni 92:ec9550034276 163 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 164 _smutex.unlock();
ocomeni 89:45f6db09a76d 165 at_resp = AT_RESP_NONE;
ocomeni 89:45f6db09a76d 166 break;
ocomeni 89:45f6db09a76d 167 case AT_HTTP_RESP_DOWNLOAD:
ocomeni 89:45f6db09a76d 168 // AT_HTTP_RESP response state
ocomeni 89:45f6db09a76d 169 printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
ocomeni 89:45f6db09a76d 170 return_response(true); // set download paramter to true
ocomeni 90:ed0267eca7b5 171 _smutex.lock();
ocomeni 92:ec9550034276 172 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 173 _smutex.unlock();
ocomeni 81:637a87eb8170 174 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 175 break;
ocomeni 93:06e755a80187 176 case AT_COMMAND_FAILED:
ocomeni 93:06e755a80187 177 // AT_HTTP_RESP response state
ocomeni 93:06e755a80187 178 printf("\n [ATCMD MAN] WIFI COMMAND FAILED!!\n");
ocomeni 93:06e755a80187 179 _smutex.lock();
ocomeni 93:06e755a80187 180 _parser.send("\r\nNACK\r\n");
ocomeni 93:06e755a80187 181 _smutex.unlock();
ocomeni 93:06e755a80187 182 at_resp = AT_RESP_NONE;
ocomeni 93:06e755a80187 183 break;
ocomeni 81:637a87eb8170 184 default:
ocomeni 81:637a87eb8170 185 // UNKNOWN response state
ocomeni 90:ed0267eca7b5 186 _smutex.lock();
ocomeni 82:10072c1794d3 187 printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\n");
ocomeni 92:ec9550034276 188 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 189 _smutex.unlock();
ocomeni 81:637a87eb8170 190 at_resp = AT_RESP_NONE;
ocomeni 81:637a87eb8170 191 break;
ocomeni 81:637a87eb8170 192 }
ocomeni 81:637a87eb8170 193 }
ocomeni 81:637a87eb8170 194
ocomeni 89:45f6db09a76d 195 void ATCmdManager::printBufferInHex(uint8_t *buf, int pLen)
ocomeni 89:45f6db09a76d 196 {
ocomeni 89:45f6db09a76d 197 for(int i =0;i<pLen;i++){
ocomeni 89:45f6db09a76d 198 if(i%8==0) printf("\n[%3d]",i);
ocomeni 89:45f6db09a76d 199 printf("%02x ", buf[i]);
ocomeni 89:45f6db09a76d 200 }
ocomeni 89:45f6db09a76d 201 printf("\n");
ocomeni 89:45f6db09a76d 202 }
ocomeni 81:637a87eb8170 203
ocomeni 83:9c271a50a70b 204 bool ATCmdManager::validate(edm_header_t edm_header)
ocomeni 83:9c271a50a70b 205 {
ocomeni 83:9c271a50a70b 206 if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found!
ocomeni 83:9c271a50a70b 207 if(edm_header.payloadID != CONNECT_EVENT &&
ocomeni 83:9c271a50a70b 208 edm_header.payloadID != DISCONNECT_EVENT &&
ocomeni 83:9c271a50a70b 209 edm_header.payloadID != DATA_EVENT &&
ocomeni 83:9c271a50a70b 210 edm_header.payloadID != DATA_COMMAND &&
ocomeni 83:9c271a50a70b 211 edm_header.payloadID != AT_REQUEST &&
ocomeni 83:9c271a50a70b 212 edm_header.payloadID != AT_CONFIRMATION &&
ocomeni 83:9c271a50a70b 213 edm_header.payloadID != AT_EVENT
ocomeni 83:9c271a50a70b 214 )return false; // unexpected payload ID found!
ocomeni 83:9c271a50a70b 215 if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN ||
ocomeni 83:9c271a50a70b 216 edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN
ocomeni 83:9c271a50a70b 217 ) return false; // unexpected length received!
ocomeni 90:ed0267eca7b5 218 if(edm_header.channel_id != WIFI_CHANNEL &&
ocomeni 90:ed0267eca7b5 219 edm_header.channel_id != BLE_CHANNEL
ocomeni 90:ed0267eca7b5 220 ) return false; // unexpected channel_id received!
ocomeni 83:9c271a50a70b 221 return true;
ocomeni 83:9c271a50a70b 222 }
ocomeni 74:f26e846adfe9 223
ocomeni 84:7c7add00f4bf 224 http_method ATCmdManager::str2HttpMethod(const char * methodStr)
ocomeni 84:7c7add00f4bf 225 {
ocomeni 84:7c7add00f4bf 226 http_method _http_method;
ocomeni 84:7c7add00f4bf 227 if(strstr(methodStr, "POST")!= NULL){
ocomeni 84:7c7add00f4bf 228 _http_method = HTTP_POST;
ocomeni 84:7c7add00f4bf 229 }
ocomeni 84:7c7add00f4bf 230 else if(strstr(methodStr, "GET")!= NULL){
ocomeni 84:7c7add00f4bf 231 _http_method = HTTP_GET;
ocomeni 84:7c7add00f4bf 232 }
ocomeni 84:7c7add00f4bf 233 else{
ocomeni 84:7c7add00f4bf 234 _http_method = HTTP_DELETE; // unsupported method - set to HTTP_DELETE
ocomeni 84:7c7add00f4bf 235 }
ocomeni 84:7c7add00f4bf 236 return _http_method;
ocomeni 84:7c7add00f4bf 237 }
ocomeni 84:7c7add00f4bf 238 bool ATCmdManager::createHttpRequest()
ocomeni 84:7c7add00f4bf 239 {
ocomeni 90:ed0267eca7b5 240 http_request_t *http_req; // = new http_request_t;
ocomeni 90:ed0267eca7b5 241 wifi_data_msg_t data_msg;
ocomeni 90:ed0267eca7b5 242 http_req = (http_request_t *)data_msg.buffer;
ocomeni 84:7c7add00f4bf 243 char s1[32];
ocomeni 84:7c7add00f4bf 244 char s2[32];
ocomeni 84:7c7add00f4bf 245 char s3[32];
ocomeni 90:ed0267eca7b5 246 int len = 0;
ocomeni 90:ed0267eca7b5 247 int n;
ocomeni 90:ed0267eca7b5 248 char * strPtr = (char *)rx_buf_ptr;
ocomeni 84:7c7add00f4bf 249 char * p;
ocomeni 84:7c7add00f4bf 250 char * p2 = strstr(strPtr, "\r\n\r\n");
ocomeni 84:7c7add00f4bf 251 char * nxtPtr = strPtr;
ocomeni 90:ed0267eca7b5 252 char * outPtr;
ocomeni 84:7c7add00f4bf 253 char * bodyPtr = p2+4;
ocomeni 84:7c7add00f4bf 254 printf("\nstrPtr address= %x",strPtr);
ocomeni 84:7c7add00f4bf 255 printf("\np2 address= %x", p2);
ocomeni 84:7c7add00f4bf 256 for(int i = 0; i < 5; i++){
ocomeni 84:7c7add00f4bf 257 if(i == 0)// firstline scan method uri and http_ver
ocomeni 84:7c7add00f4bf 258 {
ocomeni 84:7c7add00f4bf 259 n = sscanf(nxtPtr,"%s %s %s", s1, s2, s3);
ocomeni 90:ed0267eca7b5 260 if(n!=3) return false; // error in input abort
ocomeni 84:7c7add00f4bf 261 printf("\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 );
ocomeni 84:7c7add00f4bf 262 //http_req.method = str2HttpMethod(s1.c_str());
ocomeni 90:ed0267eca7b5 263 http_req->method = str2HttpMethod(s1);
ocomeni 90:ed0267eca7b5 264 http_req->request_URI = (char *) http_req->buffer; // point 1st string to start of buffer
ocomeni 90:ed0267eca7b5 265 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 266 strncpy(http_req->request_URI, s2, len);
ocomeni 90:ed0267eca7b5 267 http_req->http_version = http_req->request_URI + len; // point 2nd string to after 1st
ocomeni 90:ed0267eca7b5 268 len = strlen(s3)+1;
ocomeni 90:ed0267eca7b5 269 strncpy(http_req->http_version, s3, len);
ocomeni 90:ed0267eca7b5 270 printf("\nhttp_request 1st line:\n method = %d\nuri = %s\nhttp_ver = %s",http_req->method,
ocomeni 90:ed0267eca7b5 271 http_req->request_URI,
ocomeni 90:ed0267eca7b5 272 http_req->http_version );
ocomeni 90:ed0267eca7b5 273 printf("\nhttp_request str ptrs\nuri = %X\nhttp_ver = %X",http_req->request_URI,
ocomeni 90:ed0267eca7b5 274 http_req->http_version );
ocomeni 90:ed0267eca7b5 275 outPtr = http_req->http_version + len; // point output buffer ptr to after 2nd string
ocomeni 84:7c7add00f4bf 276 }
ocomeni 84:7c7add00f4bf 277 else{ // scan header pairs
ocomeni 84:7c7add00f4bf 278 //n = sscanf(nxtPtr,"%s %s", &s1[0], &s2[0]);
ocomeni 84:7c7add00f4bf 279 n = sscanf(nxtPtr,"%s %s", s1, s2);
ocomeni 90:ed0267eca7b5 280 if(n!=2) return false; // error in input abort
ocomeni 84:7c7add00f4bf 281 p = strstr(s1,":");
ocomeni 84:7c7add00f4bf 282 *p = NULL;
ocomeni 84:7c7add00f4bf 283 printf("\nname = %s value = %s",s1, s2);
ocomeni 86:04fc2fcda7ec 284 if(strstr(s1, "Host")!= NULL){
ocomeni 90:ed0267eca7b5 285 http_req->hostName = outPtr;
ocomeni 90:ed0267eca7b5 286 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 287 strncpy(outPtr, s2, len);
ocomeni 90:ed0267eca7b5 288 printf("\nname = %s value = %s",s1, outPtr);
ocomeni 90:ed0267eca7b5 289 outPtr += len; // point output buffer ptr to after current string
ocomeni 86:04fc2fcda7ec 290 }
ocomeni 86:04fc2fcda7ec 291 else if(strstr(s1, "Accept")!= NULL){
ocomeni 90:ed0267eca7b5 292 http_req->AcceptVal = outPtr;
ocomeni 90:ed0267eca7b5 293 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 294 strncpy(outPtr, s2, len);
ocomeni 90:ed0267eca7b5 295 printf("\nname = %s value = %s",s1, outPtr);
ocomeni 90:ed0267eca7b5 296 outPtr += len; // point output buffer ptr to after current string
ocomeni 86:04fc2fcda7ec 297 }
ocomeni 86:04fc2fcda7ec 298 else if(strstr(s1, "Content-Type")!= NULL){
ocomeni 90:ed0267eca7b5 299 http_req->contentType = outPtr;
ocomeni 90:ed0267eca7b5 300 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 301 strncpy(outPtr, s2, len);
ocomeni 90:ed0267eca7b5 302 printf("\nname = %s value = %s",s1, outPtr);
ocomeni 90:ed0267eca7b5 303 outPtr += len; // point output buffer ptr to after current string
ocomeni 86:04fc2fcda7ec 304 }
ocomeni 86:04fc2fcda7ec 305 else if(strstr(s1, "Content-Length")!= NULL){
ocomeni 90:ed0267eca7b5 306 //http_req.contentLen.assign(s2);
ocomeni 90:ed0267eca7b5 307 http_req->contentLen = outPtr;
ocomeni 90:ed0267eca7b5 308 len = strlen(s2)+1;
ocomeni 90:ed0267eca7b5 309 strncpy(outPtr, s2, len);
ocomeni 90:ed0267eca7b5 310 printf("\nname = %s value = %s",s1, outPtr);
ocomeni 90:ed0267eca7b5 311 outPtr += len; // point output buffer ptr to after current string
ocomeni 84:7c7add00f4bf 312 }
ocomeni 84:7c7add00f4bf 313
ocomeni 90:ed0267eca7b5 314 printf("\noutPtr = %X len = %d\n", outPtr, len);
ocomeni 84:7c7add00f4bf 315 }
ocomeni 84:7c7add00f4bf 316 nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line
ocomeni 84:7c7add00f4bf 317 if(nxtPtr >= p2) break;
ocomeni 84:7c7add00f4bf 318 }
ocomeni 90:ed0267eca7b5 319 // print header from http_req_struct
ocomeni 90:ed0267eca7b5 320 printf("\nhttp request header: \n %s\n", http_req->buffer);
ocomeni 84:7c7add00f4bf 321 int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr);
ocomeni 84:7c7add00f4bf 322 printf("\nLen = %d\n", bodyLen);
ocomeni 90:ed0267eca7b5 323 http_req->body = (uint8_t *) outPtr;
ocomeni 90:ed0267eca7b5 324 memcpy(outPtr, bodyPtr, bodyLen);
ocomeni 90:ed0267eca7b5 325 if(bodyLen > 10){
ocomeni 90:ed0267eca7b5 326 printf("\n Message Body:\n");
ocomeni 90:ed0267eca7b5 327 printBufferInHex(http_req->body, bodyLen);
ocomeni 90:ed0267eca7b5 328 }
ocomeni 90:ed0267eca7b5 329 outPtr += bodyLen; // move output pointer to end output (header + body)
ocomeni 84:7c7add00f4bf 330 // package and send on wifi data queue
ocomeni 84:7c7add00f4bf 331 data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ;
ocomeni 90:ed0267eca7b5 332 printf("\nsizeof(http_req) on population = %d\n", sizeof(*http_req));
ocomeni 90:ed0267eca7b5 333 data_msg.dataLen = (uint8_t *)outPtr - http_req->buffer;//sizeof(http_request_t);
ocomeni 90:ed0267eca7b5 334 printf("\ndata_msg.dataLen = %d\n", data_msg.dataLen);
ocomeni 84:7c7add00f4bf 335 // queue next data request
ocomeni 84:7c7add00f4bf 336 queueWiFiDataRequest(data_msg);
ocomeni 84:7c7add00f4bf 337 return true;
ocomeni 84:7c7add00f4bf 338 }
ocomeni 74:f26e846adfe9 339 // OOB processing
ocomeni 74:f26e846adfe9 340 void ATCmdManager::_process_oob(uint32_t timeout, bool all){
ocomeni 74:f26e846adfe9 341 set_timeout(timeout);
ocomeni 92:ec9550034276 342 int cnt = 0;
ocomeni 84:7c7add00f4bf 343 int start;
ocomeni 90:ed0267eca7b5 344 //channel_id_t chan_id;
ocomeni 83:9c271a50a70b 345 if(dataMode == AT_EXT_DATA_MODE)
ocomeni 83:9c271a50a70b 346 {
ocomeni 83:9c271a50a70b 347 int n;
ocomeni 87:99b37d26ff2a 348 //if(cnt++ % 10 == 0)printf("In EDM mode\n");
ocomeni 84:7c7add00f4bf 349 uint8_t edm[EDM_HDR_LEN];
ocomeni 92:ec9550034276 350 char cmdType[16];
ocomeni 83:9c271a50a70b 351 // Poll for edm packets
ocomeni 83:9c271a50a70b 352 do{
ocomeni 92:ec9550034276 353 n = _parser.read((char *)edm, EDM_HDR_LEN-1);
ocomeni 84:7c7add00f4bf 354 edm_hdr.startByte = edm[0];
ocomeni 84:7c7add00f4bf 355 edm_hdr.payloadLen = edm[1]*256 + edm[2];
ocomeni 84:7c7add00f4bf 356 edm_hdr.payloadID = edm[3]*256 + edm[4];
ocomeni 92:ec9550034276 357 edm_hdr.channel_id = (channel_id_t) 0;
ocomeni 84:7c7add00f4bf 358 start = Kernel::get_ms_count();
ocomeni 83:9c271a50a70b 359 if(n == -1) break; // break if it times out
ocomeni 92:ec9550034276 360 int pT;
ocomeni 92:ec9550034276 361 if(edm_hdr.payloadID < AT_REQUEST) //
ocomeni 92:ec9550034276 362 {
ocomeni 92:ec9550034276 363 n += _parser.read((char *)edm, 1);
ocomeni 92:ec9550034276 364 edm_hdr.channel_id = (channel_id_t) edm[0];
ocomeni 92:ec9550034276 365 pT = 0;
ocomeni 92:ec9550034276 366 strcpy(cmdType, "DATA COMMAND");
ocomeni 92:ec9550034276 367 }
ocomeni 92:ec9550034276 368 else
ocomeni 92:ec9550034276 369 {
ocomeni 92:ec9550034276 370 pT = 1;
ocomeni 92:ec9550034276 371 strcpy(cmdType, "AT REQUEST");
ocomeni 92:ec9550034276 372 }
ocomeni 92:ec9550034276 373 //printf("%d bytes read! : type = %s\n", n, cmdType2str(edm_hdr.payloadID));
ocomeni 92:ec9550034276 374 printf("%d bytes read! : CMD type = %s\n", n, cmdType);
ocomeni 92:ec9550034276 375 if(n==(EDM_HDR_LEN-pT))
ocomeni 90:ed0267eca7b5 376 printf("Start = %d, payloadID = %d len = %d chan_id = %d\n", edm_hdr.startByte,
ocomeni 90:ed0267eca7b5 377 edm_hdr.payloadID,
ocomeni 90:ed0267eca7b5 378 edm_hdr.payloadLen,
ocomeni 90:ed0267eca7b5 379 edm_hdr.channel_id);
ocomeni 92:ec9550034276 380 if(n == (EDM_HDR_LEN-pT) && validate(edm_hdr)) // if AT command use process oob to decode
ocomeni 83:9c271a50a70b 381 {
ocomeni 83:9c271a50a70b 382 if(edm_hdr.payloadID == AT_REQUEST)
ocomeni 83:9c271a50a70b 383 {
ocomeni 92:ec9550034276 384 //_parser.process_oob();
ocomeni 92:ec9550034276 385 while (_parser.process_oob() && all) {
ocomeni 92:ec9550034276 386 }
ocomeni 83:9c271a50a70b 387 break;
ocomeni 83:9c271a50a70b 388 }
ocomeni 83:9c271a50a70b 389 else
ocomeni 83:9c271a50a70b 390 {
ocomeni 92:ec9550034276 391 int pLen = edm_hdr.payloadLen-2-pT;
ocomeni 83:9c271a50a70b 392 rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte
ocomeni 83:9c271a50a70b 393 rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected
ocomeni 83:9c271a50a70b 394 n = _parser.read((char *)rx_buf_ptr, pLen);
ocomeni 83:9c271a50a70b 395 if(n == -1) break; // timeout!
ocomeni 83:9c271a50a70b 396 printf("%d bytes read - expected %d!\n", n, pLen);
ocomeni 89:45f6db09a76d 397 printBufferInHex(rx_buf_ptr, pLen);
ocomeni 83:9c271a50a70b 398 printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
ocomeni 83:9c271a50a70b 399 if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
ocomeni 90:ed0267eca7b5 400 _smutex.lock();
ocomeni 90:ed0267eca7b5 401 _parser.send("ERR");
ocomeni 90:ed0267eca7b5 402 _smutex.unlock();
ocomeni 90:ed0267eca7b5 403 break; // exit if stop byte not found - possible data corruption!
ocomeni 83:9c271a50a70b 404 }
ocomeni 83:9c271a50a70b 405 switch(edm_hdr.payloadID)
ocomeni 83:9c271a50a70b 406 {
ocomeni 83:9c271a50a70b 407 case CONNECT_EVENT:
ocomeni 83:9c271a50a70b 408 printf("Connection Event received!\n");
ocomeni 83:9c271a50a70b 409 break;
ocomeni 83:9c271a50a70b 410 case DISCONNECT_EVENT:
ocomeni 83:9c271a50a70b 411 printf("DISCONNECT_EVENT received!\n");
ocomeni 83:9c271a50a70b 412 break;
ocomeni 83:9c271a50a70b 413 case DATA_EVENT:
ocomeni 83:9c271a50a70b 414 printf("DATA_EVENT received!\n");
ocomeni 83:9c271a50a70b 415 break;
ocomeni 83:9c271a50a70b 416 case DATA_COMMAND:
ocomeni 83:9c271a50a70b 417 printf("DATA_COMMAND received!\n");
ocomeni 84:7c7add00f4bf 418 if(createHttpRequest() == true){
ocomeni 90:ed0267eca7b5 419 _smutex.lock();
ocomeni 92:ec9550034276 420 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 421 _smutex.unlock();
ocomeni 83:9c271a50a70b 422 }
ocomeni 84:7c7add00f4bf 423 else{
ocomeni 90:ed0267eca7b5 424 _smutex.lock();
ocomeni 84:7c7add00f4bf 425 _parser.send("NACK");
ocomeni 90:ed0267eca7b5 426 _smutex.unlock();
ocomeni 84:7c7add00f4bf 427 }
ocomeni 92:ec9550034276 428 free(rx_buf_ptr);
ocomeni 84:7c7add00f4bf 429 int stop = Kernel::get_ms_count();
ocomeni 84:7c7add00f4bf 430 printf("\n Time Elapsed = %d\n", stop-start);
ocomeni 83:9c271a50a70b 431 break;
ocomeni 83:9c271a50a70b 432 case AT_REQUEST:
ocomeni 83:9c271a50a70b 433 printf("AT_REQUEST received!\n");
ocomeni 83:9c271a50a70b 434 break;
ocomeni 83:9c271a50a70b 435 case AT_CONFIRMATION:
ocomeni 83:9c271a50a70b 436 printf("AT_CONFIRMATION received!\n");
ocomeni 83:9c271a50a70b 437 break;
ocomeni 83:9c271a50a70b 438 case AT_EVENT:
ocomeni 83:9c271a50a70b 439 printf("AT_EVENT received!\n");
ocomeni 83:9c271a50a70b 440 break;
ocomeni 83:9c271a50a70b 441 default:
ocomeni 83:9c271a50a70b 442 printf("UNKNOWN MESSAGE received!\n");
ocomeni 83:9c271a50a70b 443 break;
ocomeni 83:9c271a50a70b 444 }
ocomeni 83:9c271a50a70b 445 }
ocomeni 83:9c271a50a70b 446 //_parser.process_oob();
ocomeni 83:9c271a50a70b 447 }
ocomeni 83:9c271a50a70b 448 else // incorrect # of bytes received abort!!
ocomeni 83:9c271a50a70b 449 {
ocomeni 83:9c271a50a70b 450 break;
ocomeni 83:9c271a50a70b 451 }
ocomeni 83:9c271a50a70b 452 }while (all); // continue to process until timeout
ocomeni 74:f26e846adfe9 453 }
ocomeni 83:9c271a50a70b 454 else
ocomeni 83:9c271a50a70b 455 {
ocomeni 83:9c271a50a70b 456 // Poll for inbound packets
ocomeni 83:9c271a50a70b 457 while (_parser.process_oob() && all) {
ocomeni 83:9c271a50a70b 458 }
ocomeni 83:9c271a50a70b 459 }
ocomeni 74:f26e846adfe9 460 set_timeout();
ocomeni 74:f26e846adfe9 461 }
ocomeni 74:f26e846adfe9 462
ocomeni 74:f26e846adfe9 463
ocomeni 74:f26e846adfe9 464 // OOB message handlers
ocomeni 74:f26e846adfe9 465 void ATCmdManager::_oob_startup_hdlr(){
ocomeni 74:f26e846adfe9 466 }
ocomeni 74:f26e846adfe9 467
ocomeni 81:637a87eb8170 468 void ATCmdManager::_oob_ok_hdlr(){
ocomeni 90:ed0267eca7b5 469 _smutex.lock();
ocomeni 92:ec9550034276 470 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 471 _smutex.unlock();
ocomeni 81:637a87eb8170 472 }
ocomeni 81:637a87eb8170 473
ocomeni 74:f26e846adfe9 474
ocomeni 74:f26e846adfe9 475 void ATCmdManager::_oob_bleRole_hdlr(){
ocomeni 74:f26e846adfe9 476 }
ocomeni 74:f26e846adfe9 477
ocomeni 74:f26e846adfe9 478
ocomeni 74:f26e846adfe9 479 void ATCmdManager::_oob_wifiMode_err(){
ocomeni 74:f26e846adfe9 480 }
ocomeni 74:f26e846adfe9 481
ocomeni 74:f26e846adfe9 482
ocomeni 74:f26e846adfe9 483 void ATCmdManager::_oob_conn_already(){
ocomeni 74:f26e846adfe9 484 }
ocomeni 74:f26e846adfe9 485
ocomeni 74:f26e846adfe9 486
ocomeni 74:f26e846adfe9 487 void ATCmdManager::_oob_err(){
ocomeni 74:f26e846adfe9 488 }
ocomeni 74:f26e846adfe9 489
ocomeni 79:a2187bbfa407 490 void ATCmdManager::_oob_get_fw_ver()
ocomeni 79:a2187bbfa407 491 {
ocomeni 79:a2187bbfa407 492 }
ocomeni 79:a2187bbfa407 493
ocomeni 79:a2187bbfa407 494
ocomeni 75:08eff6258e1b 495 void ATCmdManager::_oob_uart_setup(){
ocomeni 75:08eff6258e1b 496 int uOpts[NUM_UART_OPTIONS];
ocomeni 75:08eff6258e1b 497 //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
ocomeni 75:08eff6258e1b 498 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 499 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 500 //AT+UMRS=230400,2,8,1,1,1
ocomeni 81:637a87eb8170 501 printf("\n Changing Baud Rate to %d\n", uOpts[0]);
ocomeni 81:637a87eb8170 502
ocomeni 92:ec9550034276 503 _parser.send(OK_RESP);
ocomeni 92:ec9550034276 504 wait(0.1);
ocomeni 81:637a87eb8170 505 _serial.set_baud(uOpts[0]);
ocomeni 92:ec9550034276 506 wait(0.1);
ocomeni 81:637a87eb8170 507 printf("\n Baud Rate now %d\n", uOpts[0]);
ocomeni 81:637a87eb8170 508
ocomeni 75:08eff6258e1b 509 } else {
ocomeni 75:08eff6258e1b 510 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 511 }
ocomeni 75:08eff6258e1b 512 }
ocomeni 75:08eff6258e1b 513
ocomeni 74:f26e846adfe9 514 void ATCmdManager::set_timeout(uint32_t timeout_ms)
ocomeni 74:f26e846adfe9 515 {
ocomeni 74:f26e846adfe9 516 _parser.set_timeout(timeout_ms);
ocomeni 74:f26e846adfe9 517 }
ocomeni 74:f26e846adfe9 518
ocomeni 75:08eff6258e1b 519
ocomeni 75:08eff6258e1b 520 void ATCmdManager::_oob_echo_off()
ocomeni 75:08eff6258e1b 521 {
ocomeni 90:ed0267eca7b5 522 _smutex.lock();
ocomeni 75:08eff6258e1b 523 printf("\n Received ATEO OOB command!!\n");
ocomeni 75:08eff6258e1b 524 printf("\n turning echo OFF!!\n");
ocomeni 75:08eff6258e1b 525 _parser.debug_on(false);
ocomeni 92:ec9550034276 526 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 527 _smutex.unlock();
ocomeni 75:08eff6258e1b 528 }
ocomeni 75:08eff6258e1b 529
ocomeni 75:08eff6258e1b 530
ocomeni 75:08eff6258e1b 531 void ATCmdManager::_oob_echo_on()
ocomeni 75:08eff6258e1b 532 {
ocomeni 90:ed0267eca7b5 533 _smutex.lock();
ocomeni 75:08eff6258e1b 534 printf("\n Received ATE1 OOB command!!\n");
ocomeni 75:08eff6258e1b 535 printf("\n turning echo ON!!\n");
ocomeni 75:08eff6258e1b 536 _parser.debug_on(true);
ocomeni 92:ec9550034276 537 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 538 _smutex.unlock();
ocomeni 75:08eff6258e1b 539 }
ocomeni 75:08eff6258e1b 540
ocomeni 75:08eff6258e1b 541
ocomeni 75:08eff6258e1b 542 void ATCmdManager::_oob_data_mode(){
ocomeni 90:ed0267eca7b5 543 _smutex.lock();
ocomeni 75:08eff6258e1b 544 printf("\n Received EDM mode command!!\n");
ocomeni 75:08eff6258e1b 545 if(_parser.scanf("%d", &dataMode) >0) {
ocomeni 75:08eff6258e1b 546 printf("\nATCmdParser: Data mode=%d\n", dataMode);
ocomeni 75:08eff6258e1b 547 switch(dataMode)
ocomeni 75:08eff6258e1b 548 {
ocomeni 75:08eff6258e1b 549 case 0:
ocomeni 83:9c271a50a70b 550 printf("\nATCmdParser: Command Mode request received\n");
ocomeni 81:637a87eb8170 551 dataMode = AT_CMD_DATA_MODE;
ocomeni 75:08eff6258e1b 552 break;
ocomeni 75:08eff6258e1b 553 case 1:
ocomeni 83:9c271a50a70b 554 printf("\nATCmdParser: Data Mode request received\n");
ocomeni 81:637a87eb8170 555 dataMode = AT_STD_DATA_MODE;
ocomeni 75:08eff6258e1b 556 break;
ocomeni 75:08eff6258e1b 557 case 2:
ocomeni 83:9c271a50a70b 558 printf("\nATCmdParser: Extended data Mode request received\n");
ocomeni 81:637a87eb8170 559 dataMode = AT_EXT_DATA_MODE;
ocomeni 92:ec9550034276 560 print_heap_and_isr_stack_info();
ocomeni 75:08eff6258e1b 561 break;
ocomeni 75:08eff6258e1b 562 default:
ocomeni 75:08eff6258e1b 563 printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 564 break;
ocomeni 75:08eff6258e1b 565 }
ocomeni 75:08eff6258e1b 566 } else {
ocomeni 83:9c271a50a70b 567 printf("\nATCmdParser: Retrieving Uart Options failed\n");
ocomeni 75:08eff6258e1b 568 }
ocomeni 92:ec9550034276 569 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 570 _smutex.unlock();
ocomeni 75:08eff6258e1b 571 }
ocomeni 75:08eff6258e1b 572
ocomeni 75:08eff6258e1b 573 void ATCmdManager::_oob_get_mac_addr(){
ocomeni 75:08eff6258e1b 574 int bleOrWifi;
ocomeni 90:ed0267eca7b5 575 _smutex.lock();
ocomeni 75:08eff6258e1b 576 if(_parser.scanf("=%d", &bleOrWifi) >0) {
ocomeni 75:08eff6258e1b 577 switch(bleOrWifi)
ocomeni 75:08eff6258e1b 578 {
ocomeni 75:08eff6258e1b 579 case 1:
ocomeni 75:08eff6258e1b 580 printf("\nATCmdParser: BLE MAC Address request received");
ocomeni 75:08eff6258e1b 581 break;
ocomeni 75:08eff6258e1b 582 case 2:
ocomeni 75:08eff6258e1b 583 printf("\nATCmdParser: WiFi MAC Address request received");
ocomeni 75:08eff6258e1b 584 break;
ocomeni 75:08eff6258e1b 585 default:
ocomeni 75:08eff6258e1b 586 printf("\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
ocomeni 75:08eff6258e1b 587 break;
ocomeni 75:08eff6258e1b 588 }
ocomeni 75:08eff6258e1b 589 } else {
ocomeni 75:08eff6258e1b 590 printf("\nATCmdParser: Retrieving Uart Options failed");
ocomeni 75:08eff6258e1b 591 }
ocomeni 92:ec9550034276 592 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 593 _smutex.unlock();
ocomeni 75:08eff6258e1b 594 }
ocomeni 75:08eff6258e1b 595
ocomeni 75:08eff6258e1b 596 void ATCmdManager::_oob_get_ble_role(){
ocomeni 90:ed0267eca7b5 597 _smutex.lock();
ocomeni 75:08eff6258e1b 598 printf("\n Received get BLE role command!!\n");
ocomeni 92:ec9550034276 599 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 600 _smutex.unlock();
ocomeni 75:08eff6258e1b 601 }
ocomeni 75:08eff6258e1b 602
ocomeni 75:08eff6258e1b 603 void ATCmdManager::_oob_ena_ble_peri(){
ocomeni 90:ed0267eca7b5 604 _smutex.lock();
ocomeni 75:08eff6258e1b 605 printf("\n Received enable BLE Peripheral command!!\n");
ocomeni 92:ec9550034276 606 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 607 _smutex.unlock();
ocomeni 75:08eff6258e1b 608 }
ocomeni 75:08eff6258e1b 609
ocomeni 75:08eff6258e1b 610 void ATCmdManager::_oob_reboot(){
ocomeni 90:ed0267eca7b5 611 _smutex.lock();
ocomeni 75:08eff6258e1b 612 printf("\n Received reboot command!!\n");
ocomeni 92:ec9550034276 613 _parser.send(OK_RESP);
ocomeni 75:08eff6258e1b 614 _parser.send("System Resetting....\n");
ocomeni 75:08eff6258e1b 615 system_reset();
ocomeni 90:ed0267eca7b5 616 _smutex.unlock();
ocomeni 75:08eff6258e1b 617 }
ocomeni 75:08eff6258e1b 618
ocomeni 79:a2187bbfa407 619 const char * ATCmdManager::sec2str(nsapi_security_t sec)
ocomeni 79:a2187bbfa407 620 {
ocomeni 79:a2187bbfa407 621 switch (sec) {
ocomeni 79:a2187bbfa407 622 case NSAPI_SECURITY_NONE:
ocomeni 79:a2187bbfa407 623 return "None";
ocomeni 79:a2187bbfa407 624 case NSAPI_SECURITY_WEP:
ocomeni 79:a2187bbfa407 625 return "WEP";
ocomeni 79:a2187bbfa407 626 case NSAPI_SECURITY_WPA:
ocomeni 79:a2187bbfa407 627 return "WPA";
ocomeni 79:a2187bbfa407 628 case NSAPI_SECURITY_WPA2:
ocomeni 79:a2187bbfa407 629 return "WPA2";
ocomeni 79:a2187bbfa407 630 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 79:a2187bbfa407 631 return "WPA/WPA2";
ocomeni 79:a2187bbfa407 632 case NSAPI_SECURITY_UNKNOWN:
ocomeni 79:a2187bbfa407 633 default:
ocomeni 79:a2187bbfa407 634 return "Unknown";
ocomeni 79:a2187bbfa407 635 }
ocomeni 79:a2187bbfa407 636 }
ocomeni 81:637a87eb8170 637
ocomeni 81:637a87eb8170 638 bool ATCmdManager::setNextResponse(at_cmd_resp_t resp)
ocomeni 81:637a87eb8170 639 {
ocomeni 81:637a87eb8170 640 if(at_resp == AT_RESP_NONE){
ocomeni 81:637a87eb8170 641 at_resp = resp;
ocomeni 81:637a87eb8170 642 return true; // success
ocomeni 81:637a87eb8170 643 }
ocomeni 81:637a87eb8170 644 return false; // wiFiManager busy
ocomeni 79:a2187bbfa407 645 }
ocomeni 79:a2187bbfa407 646
ocomeni 81:637a87eb8170 647
ocomeni 79:a2187bbfa407 648 void ATCmdManager::_oob_scanWiFiNetworks(){
ocomeni 90:ed0267eca7b5 649 _smutex.lock();
ocomeni 79:a2187bbfa407 650 printf("\n Received scanWiFiNetworks command!!\n");
ocomeni 92:ec9550034276 651 _parser.send(OK_RESP);
ocomeni 90:ed0267eca7b5 652 _smutex.unlock();
ocomeni 79:a2187bbfa407 653 wifi_cmd_t cmd = WIFI_CMD_SCAN;
ocomeni 79:a2187bbfa407 654 // queue next command
ocomeni 79:a2187bbfa407 655 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 656 return;
ocomeni 75:08eff6258e1b 657 }
ocomeni 75:08eff6258e1b 658
ocomeni 79:a2187bbfa407 659 void ATCmdManager::_oob_connect2WiFiNetwork()
ocomeni 79:a2187bbfa407 660 {
ocomeni 92:ec9550034276 661 //_smutex.lock();
ocomeni 92:ec9550034276 662 printf("\n Received WiFi Connect command!!\n");
ocomeni 92:ec9550034276 663 _parser.send(OK_RESP);
ocomeni 81:637a87eb8170 664 wifi_cmd_t cmd = WIFI_CMD_CONNECT;
ocomeni 92:ec9550034276 665 printf("\n About to Queue wifi cmd = %d!!\n", cmd);
ocomeni 92:ec9550034276 666 //_smutex.unlock();
ocomeni 81:637a87eb8170 667 // queue next command
ocomeni 81:637a87eb8170 668 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 669 return;
ocomeni 79:a2187bbfa407 670 }
ocomeni 79:a2187bbfa407 671
ocomeni 79:a2187bbfa407 672
ocomeni 79:a2187bbfa407 673 void ATCmdManager::_oob_disconnectWiFiNetwork()
ocomeni 79:a2187bbfa407 674 {
ocomeni 92:ec9550034276 675 _smutex.lock();
ocomeni 92:ec9550034276 676 printf("\n Received WiFi Disconnect command!!\n");
ocomeni 92:ec9550034276 677 _parser.send(OK_RESP);
ocomeni 81:637a87eb8170 678 wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
ocomeni 92:ec9550034276 679 _smutex.unlock();
ocomeni 81:637a87eb8170 680 // queue next command
ocomeni 81:637a87eb8170 681 queueWiFiCommand(cmd);
ocomeni 81:637a87eb8170 682 return;
ocomeni 81:637a87eb8170 683 }
ocomeni 81:637a87eb8170 684
ocomeni 81:637a87eb8170 685 void ATCmdManager::_oob_setupInternetConnection()
ocomeni 81:637a87eb8170 686 {
ocomeni 90:ed0267eca7b5 687 char str[MAX_URL_LEN];
ocomeni 90:ed0267eca7b5 688 char url[MAX_URL_LEN];
ocomeni 81:637a87eb8170 689 int n;
ocomeni 81:637a87eb8170 690 internet_config_t internet_config;
ocomeni 90:ed0267eca7b5 691 _smutex.lock();
ocomeni 84:7c7add00f4bf 692 printf("sizeof internet_config_t = %d\n", sizeof(internet_config_t));
ocomeni 90:ed0267eca7b5 693 n = ReadBytes((uint8_t *)str, MAX_URL_LEN);
ocomeni 84:7c7add00f4bf 694 str[n]=NULL;
ocomeni 84:7c7add00f4bf 695 printf("\n read string = %s , n = %d\n", str, n);
ocomeni 90:ed0267eca7b5 696 //n = sscanf(str, "=%1d,%199[^,],%1d", &(uint8_t)internet_config.peer_id,
ocomeni 90:ed0267eca7b5 697 n = sscanf(str, "=%1d,%99[^,],%1d", &internet_config.peer_id,
ocomeni 92:ec9550034276 698 url, //internet_config.url,
ocomeni 92:ec9550034276 699 &internet_config.connectionScheme);
ocomeni 92:ec9550034276 700 char *p = strstr(url,"\"");
ocomeni 92:ec9550034276 701 if(p!=NULL)
ocomeni 92:ec9550034276 702 {
ocomeni 92:ec9550034276 703 strncpy(internet_config.url, &p[1], strlen(url));
ocomeni 92:ec9550034276 704 p = strstr(internet_config.url,"\"");
ocomeni 92:ec9550034276 705 *p = NULL;
ocomeni 92:ec9550034276 706 }
ocomeni 92:ec9550034276 707 else
ocomeni 92:ec9550034276 708 {
ocomeni 92:ec9550034276 709 strncpy(internet_config.url, url, strlen(url)+1);
ocomeni 92:ec9550034276 710 }
ocomeni 90:ed0267eca7b5 711 printf("\n read string = %s , n = %d -- strlen(url) = %d\n", internet_config.url, n, strlen(internet_config.url));
ocomeni 81:637a87eb8170 712 if(n>0)
ocomeni 81:637a87eb8170 713 {
ocomeni 81:637a87eb8170 714 printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 90:ed0267eca7b5 715 internet_config.url,
ocomeni 90:ed0267eca7b5 716 internet_config.connectionScheme);
ocomeni 81:637a87eb8170 717 // package and send on wifi data queue
ocomeni 90:ed0267eca7b5 718 wifi_data_msg_t data_msg;
ocomeni 81:637a87eb8170 719 data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG;
ocomeni 84:7c7add00f4bf 720 data_msg.dataLen = sizeof(internet_config_t); // + strlen(internet_config.url);
ocomeni 84:7c7add00f4bf 721 printf("\n url size = %d url str = %s\n",strlen(internet_config.url), internet_config.url );
ocomeni 81:637a87eb8170 722 memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
ocomeni 82:10072c1794d3 723 // queue next data request
ocomeni 81:637a87eb8170 724 queueWiFiDataRequest(data_msg);
ocomeni 84:7c7add00f4bf 725 print_memory_info();
ocomeni 92:ec9550034276 726 _parser.send(OK_RESP);
ocomeni 81:637a87eb8170 727 } else {
ocomeni 81:637a87eb8170 728 printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n);
ocomeni 82:10072c1794d3 729 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 730 }
ocomeni 90:ed0267eca7b5 731 _smutex.unlock();
ocomeni 82:10072c1794d3 732 }
ocomeni 82:10072c1794d3 733
ocomeni 82:10072c1794d3 734 wifi_config_t ATCmdManager::init_wifi_config()
ocomeni 82:10072c1794d3 735 {
ocomeni 82:10072c1794d3 736 wifi_config_t wifi_cfg;
ocomeni 82:10072c1794d3 737 wifi_cfg.ssid[0] = NULL;
ocomeni 82:10072c1794d3 738 wifi_cfg.pass[0] = NULL;
ocomeni 82:10072c1794d3 739 wifi_cfg.security = NSAPI_SECURITY_UNKNOWN;
ocomeni 82:10072c1794d3 740 return wifi_cfg;
ocomeni 82:10072c1794d3 741 }
ocomeni 83:9c271a50a70b 742 /* read ASCII characters into buffer and null terminate */
ocomeni 83:9c271a50a70b 743 int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes)
ocomeni 82:10072c1794d3 744 {
ocomeni 82:10072c1794d3 745 int c;
ocomeni 82:10072c1794d3 746 int sptr = 0;
ocomeni 93:06e755a80187 747 int quoteCnt = 0;
ocomeni 82:10072c1794d3 748 for(int i=0;i<maxBytes;i++){
ocomeni 82:10072c1794d3 749 c = _parser.getc();
ocomeni 93:06e755a80187 750 if(c== '"')quoteCnt++;
ocomeni 93:06e755a80187 751 if(c==-1 || quoteCnt==2){
ocomeni 83:9c271a50a70b 752 buf[sptr] = NULL; // null terminate if string
ocomeni 82:10072c1794d3 753 return i;
ocomeni 82:10072c1794d3 754 }
ocomeni 82:10072c1794d3 755 if(c != ',' && c!= '"'){
ocomeni 82:10072c1794d3 756 buf[sptr++] = (uint8_t) c;
ocomeni 82:10072c1794d3 757 }
ocomeni 81:637a87eb8170 758 }
ocomeni 83:9c271a50a70b 759 return maxBytes;
ocomeni 83:9c271a50a70b 760 }
ocomeni 83:9c271a50a70b 761
ocomeni 83:9c271a50a70b 762 int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes)
ocomeni 83:9c271a50a70b 763 {
ocomeni 83:9c271a50a70b 764 int c;
ocomeni 83:9c271a50a70b 765 int sptr = 0;
ocomeni 83:9c271a50a70b 766 for(int i=0;i<maxBytes;i++){
ocomeni 83:9c271a50a70b 767 c = _parser.getc();
ocomeni 83:9c271a50a70b 768 if(c==-1){
ocomeni 83:9c271a50a70b 769 return i;
ocomeni 83:9c271a50a70b 770 }
ocomeni 83:9c271a50a70b 771 buf[sptr++] = (uint8_t) c;
ocomeni 83:9c271a50a70b 772 }
ocomeni 83:9c271a50a70b 773 return maxBytes;
ocomeni 82:10072c1794d3 774 }
ocomeni 82:10072c1794d3 775
ocomeni 82:10072c1794d3 776 void ATCmdManager::_oob_setWiFiSSID()
ocomeni 82:10072c1794d3 777 {
ocomeni 82:10072c1794d3 778 int n;
ocomeni 82:10072c1794d3 779 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 90:ed0267eca7b5 780 _smutex.lock();
ocomeni 83:9c271a50a70b 781 n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32);
ocomeni 82:10072c1794d3 782 printf("[ATCMD MAN]: number of bytes read = %d\n", n);
ocomeni 82:10072c1794d3 783 if(n>0)
ocomeni 82:10072c1794d3 784 {
ocomeni 82:10072c1794d3 785 printf("[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid);
ocomeni 82:10072c1794d3 786 // package and send on wifi data queue
ocomeni 82:10072c1794d3 787 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 788 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 789 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 790 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 791 queueWiFiDataRequest(data_msg);
ocomeni 92:ec9550034276 792 _parser.send(OK_RESP);
ocomeni 82:10072c1794d3 793 } else {
ocomeni 82:10072c1794d3 794 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 795 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 796 }
ocomeni 90:ed0267eca7b5 797 _smutex.unlock();
ocomeni 82:10072c1794d3 798 }
ocomeni 82:10072c1794d3 799
ocomeni 82:10072c1794d3 800 void ATCmdManager::_oob_setWiFiPWD()
ocomeni 82:10072c1794d3 801 {
ocomeni 82:10072c1794d3 802 int n;
ocomeni 82:10072c1794d3 803 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 90:ed0267eca7b5 804 _smutex.lock();
ocomeni 82:10072c1794d3 805 //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass);
ocomeni 83:9c271a50a70b 806 n = readStringBytes((uint8_t *)wifi_cfg.pass, 32);
ocomeni 82:10072c1794d3 807 if(n>0)
ocomeni 82:10072c1794d3 808 {
ocomeni 92:ec9550034276 809 printf("ATCMD MAN]: wifi_cfg.pass = %s\n", "****************");
ocomeni 82:10072c1794d3 810 // package and send on wifi data queue
ocomeni 82:10072c1794d3 811 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 812 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 813 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 814 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 815 queueWiFiDataRequest(data_msg);
ocomeni 92:ec9550034276 816 _parser.send(OK_RESP);
ocomeni 82:10072c1794d3 817 } else {
ocomeni 82:10072c1794d3 818 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 82:10072c1794d3 819 _parser.send("NAK\n");
ocomeni 82:10072c1794d3 820 }
ocomeni 90:ed0267eca7b5 821 _smutex.unlock();
ocomeni 82:10072c1794d3 822 }
ocomeni 82:10072c1794d3 823
ocomeni 82:10072c1794d3 824 void ATCmdManager::_oob_setWiFiSecurity()
ocomeni 82:10072c1794d3 825 {
ocomeni 82:10072c1794d3 826 int n;
ocomeni 82:10072c1794d3 827 wifi_config_t wifi_cfg = init_wifi_config();
ocomeni 90:ed0267eca7b5 828 _smutex.lock();
ocomeni 82:10072c1794d3 829 n = _parser.scanf(",%d", &wifi_cfg.security);
ocomeni 90:ed0267eca7b5 830 _smutex.unlock();
ocomeni 82:10072c1794d3 831 if(n>0)
ocomeni 82:10072c1794d3 832 {
ocomeni 82:10072c1794d3 833 printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
ocomeni 82:10072c1794d3 834 // package and send on wifi data queue
ocomeni 82:10072c1794d3 835 wifi_data_msg_t data_msg;
ocomeni 82:10072c1794d3 836 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 82:10072c1794d3 837 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 82:10072c1794d3 838 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 82:10072c1794d3 839 queueWiFiDataRequest(data_msg);
ocomeni 92:ec9550034276 840 _parser.send(OK_RESP);
ocomeni 82:10072c1794d3 841 } else {
ocomeni 82:10072c1794d3 842 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 90:ed0267eca7b5 843 _smutex.lock();
ocomeni 82:10072c1794d3 844 _parser.send("NAK\n");
ocomeni 90:ed0267eca7b5 845 _smutex.unlock();
ocomeni 82:10072c1794d3 846 }
ocomeni 84:7c7add00f4bf 847 //_smutex.unlock();
ocomeni 79:a2187bbfa407 848 }
ocomeni 81:637a87eb8170 849
ocomeni 83:9c271a50a70b 850
ocomeni 83:9c271a50a70b 851 void ATCmdManager::_oob_sendHttpMessage()
ocomeni 83:9c271a50a70b 852 {
ocomeni 83:9c271a50a70b 853 int n;
ocomeni 84:7c7add00f4bf 854 http_request_t http_post_request;
ocomeni 84:7c7add00f4bf 855 //_smutex.lock();
ocomeni 83:9c271a50a70b 856 /*
ocomeni 83:9c271a50a70b 857 n = _parser.scanf(",%d", &wifi_cfg.security);
ocomeni 83:9c271a50a70b 858 if(n>0)
ocomeni 83:9c271a50a70b 859 {
ocomeni 83:9c271a50a70b 860 printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
ocomeni 83:9c271a50a70b 861 // package and send on wifi data queue
ocomeni 83:9c271a50a70b 862 wifi_data_msg_t data_msg;
ocomeni 83:9c271a50a70b 863 data_msg.wifi_cmd = WIFI_CMD_CONFIG;
ocomeni 83:9c271a50a70b 864 data_msg.dataLen = sizeof(wifi_config_t);
ocomeni 83:9c271a50a70b 865 memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
ocomeni 83:9c271a50a70b 866 queueWiFiDataRequest(data_msg);
ocomeni 92:ec9550034276 867 _parser.send(OK_RESP);
ocomeni 83:9c271a50a70b 868 } else {
ocomeni 83:9c271a50a70b 869 printf("\n[ATCMD MAN]: wifi configuration failed \n");
ocomeni 83:9c271a50a70b 870 _parser.send("NAK\n");
ocomeni 83:9c271a50a70b 871 }
ocomeni 83:9c271a50a70b 872 */
ocomeni 84:7c7add00f4bf 873 //_smutex.unlock();
ocomeni 83:9c271a50a70b 874
ocomeni 83:9c271a50a70b 875 }
ocomeni 83:9c271a50a70b 876
ocomeni 81:637a87eb8170 877 bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
ocomeni 92:ec9550034276 878 printf("[ATCMD MAN] about to be queued with wifi_cmd = %d\n", cmd);
ocomeni 92:ec9550034276 879 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 81:637a87eb8170 880 wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
ocomeni 92:ec9550034276 881 if(wifiCmd == NULL){
ocomeni 92:ec9550034276 882 printf("[ATCMD MAN] queued memory allocation failed\n");
ocomeni 92:ec9550034276 883 return false;
ocomeni 92:ec9550034276 884 }
ocomeni 92:ec9550034276 885 #else
ocomeni 92:ec9550034276 886 wifi_cmd_message_t *wifiCmd = (wifi_cmd_message_t *) malloc(sizeof(wifi_cmd_message_t));
ocomeni 92:ec9550034276 887 if(wifiCmd == NULL){
ocomeni 92:ec9550034276 888 printf("[ATCMD MAN] tr malloc() : queued memory allocation failed\n");
ocomeni 92:ec9550034276 889 print_memory_info();
ocomeni 92:ec9550034276 890 print_heap_and_isr_stack_info();
ocomeni 92:ec9550034276 891 return false;
ocomeni 92:ec9550034276 892 }
ocomeni 92:ec9550034276 893 #endif
ocomeni 81:637a87eb8170 894 wifiCmd->wifi_cmd = cmd;
ocomeni 81:637a87eb8170 895 _aT2WiFiCmdQueue->put(wifiCmd);
ocomeni 92:ec9550034276 896 printf("[ATCMD MAN] queued wifi_cmd = %d\n", wifiCmd->wifi_cmd);
ocomeni 81:637a87eb8170 897 return true;
ocomeni 81:637a87eb8170 898 }
ocomeni 81:637a87eb8170 899
ocomeni 81:637a87eb8170 900 bool ATCmdManager::dequeueATresponse(){
ocomeni 81:637a87eb8170 901 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 81:637a87eb8170 902 osEvent evt = _wiFi2ATCmdQueue->get(0);
ocomeni 81:637a87eb8170 903 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 904 at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
ocomeni 81:637a87eb8170 905 setNextResponse(resp->at_resp);
ocomeni 92:ec9550034276 906 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 81:637a87eb8170 907 _wiFi2ATmPool->free(resp);
ocomeni 92:ec9550034276 908 #else
ocomeni 92:ec9550034276 909 free(resp);
ocomeni 92:ec9550034276 910 #endif
ocomeni 81:637a87eb8170 911 }
ocomeni 81:637a87eb8170 912 return true;
ocomeni 81:637a87eb8170 913 }
ocomeni 81:637a87eb8170 914
ocomeni 81:637a87eb8170 915 bool ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
ocomeni 93:06e755a80187 916 //print_memory_info();
ocomeni 93:06e755a80187 917 print_heap_and_isr_stack_info();
ocomeni 81:637a87eb8170 918 wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
ocomeni 81:637a87eb8170 919 wifiData->wifi_cmd = data_req.wifi_cmd;
ocomeni 81:637a87eb8170 920 wifiData->dataLen = data_req.dataLen;
ocomeni 81:637a87eb8170 921 memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen);
ocomeni 81:637a87eb8170 922 _aT2WiFiDataQueue->put(wifiData);
ocomeni 81:637a87eb8170 923 printf("[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd);
ocomeni 84:7c7add00f4bf 924 //free(&data_req);
ocomeni 81:637a87eb8170 925 return true;
ocomeni 81:637a87eb8170 926 }
ocomeni 81:637a87eb8170 927
ocomeni 87:99b37d26ff2a 928 bool ATCmdManager::dequeueWiFidataResponse(){
ocomeni 81:637a87eb8170 929 if(at_resp != AT_RESP_NONE) return false; // busy
ocomeni 93:06e755a80187 930 //print_memory_info();
ocomeni 81:637a87eb8170 931 osEvent evt = _wiFi2ATDataQueue->get(0);
ocomeni 81:637a87eb8170 932 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 933 resp_data = (at_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 934 setNextResponse(resp_data->at_resp);
ocomeni 81:637a87eb8170 935 //_wiFi2ATDatamPool->free(resp_data);
ocomeni 93:06e755a80187 936 print_heap_and_isr_stack_info();
ocomeni 81:637a87eb8170 937 }
ocomeni 81:637a87eb8170 938 return true;
ocomeni 81:637a87eb8170 939 }
ocomeni 81:637a87eb8170 940
ocomeni 93:06e755a80187 941
ocomeni 93:06e755a80187 942 void ATCmdManager::sendAtConfirmation(const char *buf)
ocomeni 93:06e755a80187 943 {
ocomeni 93:06e755a80187 944 int len = strlen(buf);
ocomeni 93:06e755a80187 945 outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, CONFIRMATION_MSG_TYPE, NO_CHANNEL);
ocomeni 93:06e755a80187 946 }
ocomeni 93:06e755a80187 947 void ATCmdManager::sendAtEvent(const char *buf)
ocomeni 93:06e755a80187 948 {
ocomeni 93:06e755a80187 949 int len = strlen(buf);
ocomeni 93:06e755a80187 950 outputEDMdata((const uint8_t *) buf, len, AT_MSG_ID, EVENT_MSG_TYPE, NO_CHANNEL);
ocomeni 93:06e755a80187 951 }
ocomeni 93:06e755a80187 952
ocomeni 90:ed0267eca7b5 953 void ATCmdManager::outputEDMdata(const uint8_t *buf, int pLen,
ocomeni 91:d6b6319ad681 954 edm_msg_id_t identifier, edm_msg_type_t type,
ocomeni 91:d6b6319ad681 955 channel_id_t channel_id)
ocomeni 90:ed0267eca7b5 956 {
ocomeni 90:ed0267eca7b5 957 int epLen = pLen + 2; // edm payload length = data length + 2
ocomeni 90:ed0267eca7b5 958 _smutex.lock();
ocomeni 90:ed0267eca7b5 959 // send EDM Message start byte
ocomeni 90:ed0267eca7b5 960 _parser.putc(EDM_START_BYTE);
ocomeni 90:ed0267eca7b5 961 // send EDM Message length
ocomeni 90:ed0267eca7b5 962 _parser.putc(epLen>>8);
ocomeni 90:ed0267eca7b5 963 _parser.putc(epLen%256);
ocomeni 90:ed0267eca7b5 964 // send EDM Identifier + Type
ocomeni 90:ed0267eca7b5 965 _parser.putc(identifier>>8);
ocomeni 90:ed0267eca7b5 966 _parser.putc(identifier%256 | type);
ocomeni 93:06e755a80187 967 // send channel id if valid
ocomeni 93:06e755a80187 968 if(channel_id != NO_CHANNEL)
ocomeni 93:06e755a80187 969 _parser.putc(channel_id);
ocomeni 90:ed0267eca7b5 970 // send the data
ocomeni 90:ed0267eca7b5 971 _parser.write((const char *)buf, pLen);
ocomeni 90:ed0267eca7b5 972 // send EDM Message stop byte
ocomeni 90:ed0267eca7b5 973 _parser.putc(EDM_STOP_BYTE);
ocomeni 90:ed0267eca7b5 974 _smutex.unlock();
ocomeni 90:ed0267eca7b5 975 }
ocomeni 81:637a87eb8170 976
ocomeni 89:45f6db09a76d 977 void ATCmdManager::return_response(bool download) {
ocomeni 87:99b37d26ff2a 978 char * resp = (char *) resp_data->buffer;
ocomeni 90:ed0267eca7b5 979 _smutex.lock();
ocomeni 87:99b37d26ff2a 980 printf("\n[ATCMD MAN] received response:\n");
ocomeni 89:45f6db09a76d 981 if(download == false) // not download must be ascii header
ocomeni 89:45f6db09a76d 982 {
ocomeni 89:45f6db09a76d 983 printf("%.*s\r\n", resp_data->dataLen, resp);
ocomeni 89:45f6db09a76d 984 }
ocomeni 89:45f6db09a76d 985 else // dump payload as hex
ocomeni 89:45f6db09a76d 986 {
ocomeni 89:45f6db09a76d 987 printBufferInHex((uint8_t *)resp, resp_data->dataLen);
ocomeni 89:45f6db09a76d 988 }
ocomeni 90:ed0267eca7b5 989 //_parser.write(resp, resp_data->dataLen);
ocomeni 91:d6b6319ad681 990 outputEDMdata((const uint8_t *)resp, resp_data->dataLen, DATA_MSG_ID,
ocomeni 91:d6b6319ad681 991 EVENT_MSG_TYPE, WIFI_CHANNEL);
ocomeni 90:ed0267eca7b5 992 _smutex.unlock();
ocomeni 87:99b37d26ff2a 993 _wiFi2ATDatamPool->free(resp_data);
ocomeni 87:99b37d26ff2a 994
ocomeni 93:06e755a80187 995 //print_memory_info();
ocomeni 93:06e755a80187 996 print_heap_and_isr_stack_info();
ocomeni 87:99b37d26ff2a 997 }