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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Fri Apr 19 08:51:58 2019 +0000
Revision:
96:f5ed273881af
Parent:
95:290859010c8c
Child:
98:65c2333a38b6
response instability issues resolved. ; Able to run 10 consecutive hello cloud requests.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ocomeni 78:07bb86e3ce14 1 #include "WiFiManager.h"
ocomeni 78:07bb86e3ce14 2 #include "common_config.h"
ocomeni 78:07bb86e3ce14 3
ocomeni 78:07bb86e3ce14 4
ocomeni 79:a2187bbfa407 5 WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi,
ocomeni 80:e8f0e92e3ac9 6 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool,
ocomeni 80:e8f0e92e3ac9 7 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue,
ocomeni 81:637a87eb8170 8 MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool,
ocomeni 81:637a87eb8170 9 Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue,
ocomeni 87:99b37d26ff2a 10 MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool,
ocomeni 87:99b37d26ff2a 11 Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue,
ocomeni 87:99b37d26ff2a 12 MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool,
ocomeni 87:99b37d26ff2a 13 Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue)
ocomeni 78:07bb86e3ce14 14 :
ocomeni 81:637a87eb8170 15 wifi_config(wifi_config),
ocomeni 81:637a87eb8170 16 network(wifi),
ocomeni 81:637a87eb8170 17 _aT2WiFimPool(aT2WiFimPool),
ocomeni 81:637a87eb8170 18 _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
ocomeni 81:637a87eb8170 19
ocomeni 81:637a87eb8170 20 _wiFi2ATmPool(wiFi2ATmPool),
ocomeni 81:637a87eb8170 21 _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
ocomeni 81:637a87eb8170 22
ocomeni 81:637a87eb8170 23 _aT2WiFiDatamPool(aT2WiFiDatamPool),
ocomeni 81:637a87eb8170 24 _aT2WiFiDataQueue(aT2WiFiDataQueue),
ocomeni 81:637a87eb8170 25
ocomeni 81:637a87eb8170 26 _wiFi2ATDatamPool(wiFi2ATDatamPool),
ocomeni 81:637a87eb8170 27 _wiFi2ATDataQueue(wiFi2ATDataQueue)
ocomeni 78:07bb86e3ce14 28
ocomeni 78:07bb86e3ce14 29 {
ocomeni 79:a2187bbfa407 30 lastScanCount = 0;
ocomeni 79:a2187bbfa407 31 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 32 internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme
ocomeni 81:637a87eb8170 33 is_connected = false;
ocomeni 84:7c7add00f4bf 34 chunkNum = 0;
ocomeni 88:7ffa053be662 35 socket = NULL;
ocomeni 88:7ffa053be662 36 https_connection_active = false;
ocomeni 78:07bb86e3ce14 37 }
ocomeni 78:07bb86e3ce14 38
ocomeni 78:07bb86e3ce14 39 WiFiManager::~WiFiManager()
ocomeni 78:07bb86e3ce14 40 {
ocomeni 78:07bb86e3ce14 41 }
ocomeni 79:a2187bbfa407 42
ocomeni 81:637a87eb8170 43 bool WiFiManager::queueATresponse(at_cmd_resp_t resp){
ocomeni 92:ec9550034276 44 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 81:637a87eb8170 45 at_resp_message_t *atResp = _wiFi2ATmPool->alloc();
ocomeni 92:ec9550034276 46 #else
ocomeni 92:ec9550034276 47 at_resp_message_t *atResp = (at_resp_message_t *) malloc(sizeof(at_resp_message_t));
ocomeni 92:ec9550034276 48 #endif
ocomeni 88:7ffa053be662 49 if(atResp == NULL) return false; // queue full;
ocomeni 81:637a87eb8170 50 atResp->at_resp = resp;
ocomeni 81:637a87eb8170 51 _wiFi2ATCmdQueue->put(atResp);
ocomeni 81:637a87eb8170 52 return true;
ocomeni 81:637a87eb8170 53 }
ocomeni 81:637a87eb8170 54
ocomeni 81:637a87eb8170 55
ocomeni 81:637a87eb8170 56 bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){
ocomeni 81:637a87eb8170 57 at_data_msg_t *atData = _wiFi2ATDatamPool->alloc();
ocomeni 88:7ffa053be662 58 if(atData == NULL) return false; // queue full;
ocomeni 81:637a87eb8170 59 atData->at_resp = at_resp.at_resp;
ocomeni 81:637a87eb8170 60 atData->dataLen = at_resp.dataLen;
ocomeni 81:637a87eb8170 61 memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen);
ocomeni 81:637a87eb8170 62 _wiFi2ATDataQueue->put(atData);
ocomeni 87:99b37d26ff2a 63 printf("[WIFI MAN] queued data size = %d : at_resp = %d\n", at_resp.dataLen, at_resp.at_resp);
ocomeni 81:637a87eb8170 64 return true;
ocomeni 81:637a87eb8170 65 }
ocomeni 81:637a87eb8170 66
ocomeni 79:a2187bbfa407 67
ocomeni 79:a2187bbfa407 68
ocomeni 79:a2187bbfa407 69 void WiFiManager::runMain(){
ocomeni 81:637a87eb8170 70 nsapi_error_t error;
ocomeni 90:ed0267eca7b5 71 printf("\r\n [WIFI MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id());
ocomeni 79:a2187bbfa407 72 while(true){
ocomeni 79:a2187bbfa407 73 dequeueWiFiCommands();
ocomeni 81:637a87eb8170 74 dequeueATdataResponse();
ocomeni 79:a2187bbfa407 75 switch(wifiCmd){
ocomeni 79:a2187bbfa407 76 case WIFI_CMD_NONE:
ocomeni 79:a2187bbfa407 77 // IDLE STATE
ocomeni 79:a2187bbfa407 78 break;
ocomeni 79:a2187bbfa407 79 case WIFI_CMD_SCAN:
ocomeni 79:a2187bbfa407 80 error = scanNetworks();
ocomeni 79:a2187bbfa407 81 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 82 queueATresponse(AT_SCAN_RESP);
ocomeni 81:637a87eb8170 83 break;
ocomeni 81:637a87eb8170 84 case WIFI_CMD_DETAILED_SCAN:
ocomeni 81:637a87eb8170 85 nsapi_size_or_error_t cnt_err;
ocomeni 81:637a87eb8170 86 cnt_err = getAvailableAPs(lastScanCount);
ocomeni 81:637a87eb8170 87 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 88 queueATresponse(AT_DETAILED_SCAN_RESP);
ocomeni 79:a2187bbfa407 89 break;
ocomeni 79:a2187bbfa407 90 case WIFI_CMD_CONNECT:
ocomeni 81:637a87eb8170 91 error = connect();
ocomeni 93:06e755a80187 92 int secCount = 0;
ocomeni 93:06e755a80187 93 while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){
ocomeni 93:06e755a80187 94 wait(1); // wait 1 sec
ocomeni 93:06e755a80187 95 }
ocomeni 81:637a87eb8170 96 wifiCmd = WIFI_CMD_NONE;
ocomeni 93:06e755a80187 97 if(is_connected==false){
ocomeni 93:06e755a80187 98 printf("[WIFI MAN] +++ WIFI CONNECTION TIMEOUT +++ \r\n");
ocomeni 95:290859010c8c 99 //queueATresponse(AT_COMMAND_FAILED);
ocomeni 95:290859010c8c 100 responseString = (char *) malloc(100);
ocomeni 95:290859010c8c 101 sprintf(responseString, "\r\n+UUTIMEOUT\r\n");
ocomeni 95:290859010c8c 102 sendATresponseString(AT_COMMAND_FAILED);
ocomeni 93:06e755a80187 103 }
ocomeni 93:06e755a80187 104 else {
ocomeni 95:290859010c8c 105 sendATresponseString(AT_CONNECT_RESP);
ocomeni 93:06e755a80187 106 }
ocomeni 79:a2187bbfa407 107 break;
ocomeni 79:a2187bbfa407 108 case WIFI_CMD_DISCONNECT:
ocomeni 81:637a87eb8170 109 error = disconnect();
ocomeni 81:637a87eb8170 110 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 111 queueATresponse(AT_DISCONNECT_RESP);
ocomeni 81:637a87eb8170 112 break;
ocomeni 81:637a87eb8170 113 case WIFI_CMD_CONFIG:
ocomeni 81:637a87eb8170 114 set_WIFI_CONFIG();
ocomeni 81:637a87eb8170 115 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 116 queueATresponse(AT_CONFIG_RESP);
ocomeni 82:10072c1794d3 117 break;
ocomeni 81:637a87eb8170 118 case WIFI_CMD_INTERNET_CONFIG:
ocomeni 81:637a87eb8170 119 set_internet_config();
ocomeni 95:290859010c8c 120 queueATresponse(AT_INTERNET_CONFIG_RESP);
ocomeni 81:637a87eb8170 121 wifiCmd = WIFI_CMD_NONE;
ocomeni 95:290859010c8c 122 break;
ocomeni 95:290859010c8c 123 case WIFI_CMD_NETWORK_STATUS:
ocomeni 95:290859010c8c 124 getNetworkStatus();
ocomeni 95:290859010c8c 125 sendATresponseString(AT_NETWORK_STATUS_RESP);
ocomeni 95:290859010c8c 126 wifiCmd = WIFI_CMD_NONE;
ocomeni 95:290859010c8c 127 break;
ocomeni 95:290859010c8c 128 case WIFI_CMD_WIFI_STATUS:
ocomeni 95:290859010c8c 129 getWiFiStatus();
ocomeni 95:290859010c8c 130 sendATresponseString(AT_WIFI_STATUS_RESP);
ocomeni 95:290859010c8c 131 wifiCmd = WIFI_CMD_NONE;
ocomeni 79:a2187bbfa407 132 break;
ocomeni 79:a2187bbfa407 133 case WIFI_CMD_SEND_HTTPS_REQ:
ocomeni 88:7ffa053be662 134 printf("before call to send http request \n");
ocomeni 88:7ffa053be662 135 print_memory_info();
ocomeni 84:7c7add00f4bf 136 createHttpsRequest();
ocomeni 88:7ffa053be662 137 printf("after call to send http request \n");
ocomeni 88:7ffa053be662 138 print_memory_info();
ocomeni 84:7c7add00f4bf 139 wifiCmd = WIFI_CMD_NONE;
ocomeni 79:a2187bbfa407 140 break;
ocomeni 79:a2187bbfa407 141 case WIFI_CMD_SEND_HTTP_REQ:
ocomeni 79:a2187bbfa407 142 break;
ocomeni 79:a2187bbfa407 143 default:
ocomeni 79:a2187bbfa407 144 break;
ocomeni 79:a2187bbfa407 145 }
ocomeni 79:a2187bbfa407 146 wait_ms(100); //
ocomeni 79:a2187bbfa407 147 }
ocomeni 79:a2187bbfa407 148
ocomeni 78:07bb86e3ce14 149 }
ocomeni 79:a2187bbfa407 150
ocomeni 91:d6b6319ad681 151
ocomeni 95:290859010c8c 152 void WiFiManager::sendATresponseString(at_cmd_resp_t at_cmd)
ocomeni 91:d6b6319ad681 153 {
ocomeni 95:290859010c8c 154 int strLen = strlen(responseString) + 1;
ocomeni 91:d6b6319ad681 155 at_data_resp = new at_data_msg_t;
ocomeni 91:d6b6319ad681 156 // create message pointer for response header generation
ocomeni 91:d6b6319ad681 157 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 91:d6b6319ad681 158 // set string length
ocomeni 91:d6b6319ad681 159 at_data_resp->dataLen = strLen;
ocomeni 91:d6b6319ad681 160 memcpy(at_data_resp->buffer, responseString, strLen);
ocomeni 95:290859010c8c 161 free(responseString);
ocomeni 91:d6b6319ad681 162 // package and send on wifi data queue
ocomeni 91:d6b6319ad681 163 at_data_resp->at_resp = at_cmd;
ocomeni 91:d6b6319ad681 164 bool queueResult = true;
ocomeni 91:d6b6319ad681 165 int wait_count = 0;
ocomeni 91:d6b6319ad681 166 do
ocomeni 91:d6b6319ad681 167 {
ocomeni 91:d6b6319ad681 168 if(!queueResult){
ocomeni 91:d6b6319ad681 169 wait_count++;
ocomeni 91:d6b6319ad681 170 printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 91:d6b6319ad681 171 wait_ms(10);
ocomeni 91:d6b6319ad681 172 }
ocomeni 91:d6b6319ad681 173 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 91:d6b6319ad681 174 }while(queueResult == false);
ocomeni 91:d6b6319ad681 175 delete at_data_resp;
ocomeni 91:d6b6319ad681 176 }
ocomeni 91:d6b6319ad681 177
ocomeni 79:a2187bbfa407 178 bool WiFiManager::dequeueWiFiCommands(){
ocomeni 81:637a87eb8170 179 if(wifiCmd != WIFI_CMD_NONE) return false; // busy
ocomeni 81:637a87eb8170 180 osEvent evt = _aT2WiFiCmdQueue->get(0);
ocomeni 79:a2187bbfa407 181 if(evt.status == osEventMessage){
ocomeni 79:a2187bbfa407 182 wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
ocomeni 79:a2187bbfa407 183 setNextCommand(cmd->wifi_cmd);
ocomeni 92:ec9550034276 184 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 79:a2187bbfa407 185 _aT2WiFimPool->free(cmd);
ocomeni 96:f5ed273881af 186 cmd = NULL;
ocomeni 92:ec9550034276 187 #else
ocomeni 92:ec9550034276 188 free(cmd);
ocomeni 96:f5ed273881af 189 cmd = NULL;
ocomeni 92:ec9550034276 190 #endif
ocomeni 79:a2187bbfa407 191 }
ocomeni 79:a2187bbfa407 192 return true;
ocomeni 79:a2187bbfa407 193 }
ocomeni 79:a2187bbfa407 194
ocomeni 79:a2187bbfa407 195
ocomeni 81:637a87eb8170 196 bool WiFiManager::dequeueATdataResponse(){
ocomeni 81:637a87eb8170 197 if(wifiCmd != WIFI_CMD_NONE) return false; // busy
ocomeni 81:637a87eb8170 198 osEvent evt = _aT2WiFiDataQueue->get(0);
ocomeni 81:637a87eb8170 199 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 200 data_msg = (wifi_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 201 setNextCommand(data_msg->wifi_cmd);
ocomeni 81:637a87eb8170 202 //_wiFi2ATDatamPool->free(data_msg);
ocomeni 81:637a87eb8170 203 }
ocomeni 81:637a87eb8170 204 return true;
ocomeni 81:637a87eb8170 205 }
ocomeni 81:637a87eb8170 206
ocomeni 81:637a87eb8170 207
ocomeni 79:a2187bbfa407 208 bool WiFiManager::setNextCommand(wifi_cmd_t cmd)
ocomeni 78:07bb86e3ce14 209 {
ocomeni 92:ec9550034276 210 printf("\n [WIFI-MAN] About to set next WiFi manager command to %d\n", cmd);
ocomeni 79:a2187bbfa407 211 if(wifiCmd == WIFI_CMD_NONE){
ocomeni 79:a2187bbfa407 212 wifiCmd = cmd;
ocomeni 79:a2187bbfa407 213 return true; // success
ocomeni 79:a2187bbfa407 214 }
ocomeni 92:ec9550034276 215 printf("\n [WIFI-MAN] Busy : current state = %d \n", wifiCmd);
ocomeni 79:a2187bbfa407 216 return false; // wiFiManager busy
ocomeni 78:07bb86e3ce14 217 }
ocomeni 79:a2187bbfa407 218
ocomeni 81:637a87eb8170 219 const char * WiFiManager::sec2str(nsapi_security_t sec)
ocomeni 81:637a87eb8170 220 {
ocomeni 81:637a87eb8170 221 switch (sec) {
ocomeni 81:637a87eb8170 222 case NSAPI_SECURITY_NONE:
ocomeni 81:637a87eb8170 223 return "None";
ocomeni 81:637a87eb8170 224 case NSAPI_SECURITY_WEP:
ocomeni 81:637a87eb8170 225 return "WEP";
ocomeni 81:637a87eb8170 226 case NSAPI_SECURITY_WPA:
ocomeni 81:637a87eb8170 227 return "WPA";
ocomeni 81:637a87eb8170 228 case NSAPI_SECURITY_WPA2:
ocomeni 81:637a87eb8170 229 return "WPA2";
ocomeni 81:637a87eb8170 230 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 81:637a87eb8170 231 return "WPA/WPA2";
ocomeni 81:637a87eb8170 232 case NSAPI_SECURITY_UNKNOWN:
ocomeni 81:637a87eb8170 233 default:
ocomeni 81:637a87eb8170 234 return "Unknown";
ocomeni 81:637a87eb8170 235 }
ocomeni 81:637a87eb8170 236 }
ocomeni 81:637a87eb8170 237
ocomeni 79:a2187bbfa407 238
ocomeni 79:a2187bbfa407 239 nsapi_size_or_error_t WiFiManager::scanNetworks()
ocomeni 79:a2187bbfa407 240 {
ocomeni 79:a2187bbfa407 241 nsapi_error_t error;
ocomeni 79:a2187bbfa407 242 printf("\n [WIFI-MAN] About to start scan for WiFi networks\n");
ocomeni 79:a2187bbfa407 243 lastScanCount = network->scan(NULL, 0);
ocomeni 79:a2187bbfa407 244 printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n");
ocomeni 79:a2187bbfa407 245 return lastScanCount;
ocomeni 79:a2187bbfa407 246 }
ocomeni 79:a2187bbfa407 247
ocomeni 79:a2187bbfa407 248
ocomeni 81:637a87eb8170 249 //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res,
ocomeni 81:637a87eb8170 250 // nsapi_size_t ncount)
ocomeni 81:637a87eb8170 251 nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount)
ocomeni 79:a2187bbfa407 252 {
ocomeni 81:637a87eb8170 253 WiFiAccessPoint *ap;
ocomeni 81:637a87eb8170 254 nsapi_size_or_error_t count;
ocomeni 81:637a87eb8170 255 count = ncount;
ocomeni 81:637a87eb8170 256 //count = wiFiManager->scanNetworks();
ocomeni 81:637a87eb8170 257 if (count <= 0) {
ocomeni 81:637a87eb8170 258 //_smutex.lock();
ocomeni 81:637a87eb8170 259 printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 81:637a87eb8170 260 //_smutex.unlock();
ocomeni 81:637a87eb8170 261 return;
ocomeni 81:637a87eb8170 262 }
ocomeni 81:637a87eb8170 263 /* Limit number of network arbitrary to 15 */
ocomeni 81:637a87eb8170 264 count = count < 15 ? count : 15;
ocomeni 81:637a87eb8170 265 ap = new WiFiAccessPoint[count];
ocomeni 81:637a87eb8170 266 count = network->scan(ap, count);
ocomeni 81:637a87eb8170 267 if (count <= 0) {
ocomeni 81:637a87eb8170 268 printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 81:637a87eb8170 269 return;
ocomeni 81:637a87eb8170 270 }
ocomeni 81:637a87eb8170 271
ocomeni 81:637a87eb8170 272 for (int i = 0; i < count; i++) {
ocomeni 81:637a87eb8170 273 printf("[WIFI-MAN]: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
ocomeni 81:637a87eb8170 274 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ocomeni 81:637a87eb8170 275 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
ocomeni 81:637a87eb8170 276 }
ocomeni 81:637a87eb8170 277 printf("[WIFI-MAN] %d networks available.\n", count);
ocomeni 81:637a87eb8170 278
ocomeni 81:637a87eb8170 279 delete[] ap;
ocomeni 79:a2187bbfa407 280 return count;
ocomeni 79:a2187bbfa407 281 }
ocomeni 79:a2187bbfa407 282
ocomeni 79:a2187bbfa407 283
ocomeni 81:637a87eb8170 284 void WiFiManager::set_WIFI_CONFIG()
ocomeni 81:637a87eb8170 285 {
ocomeni 81:637a87eb8170 286 wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer;
ocomeni 82:10072c1794d3 287 if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid);
ocomeni 82:10072c1794d3 288 if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass);
ocomeni 82:10072c1794d3 289 if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security);
ocomeni 81:637a87eb8170 290 free_DataMsg();
ocomeni 81:637a87eb8170 291 }
ocomeni 81:637a87eb8170 292
ocomeni 78:07bb86e3ce14 293 void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
ocomeni 78:07bb86e3ce14 294 {
ocomeni 78:07bb86e3ce14 295 strcpy(wifi_config.ssid, wifi_ssid);
ocomeni 82:10072c1794d3 296 printf("[WIFI-MAN] wifi_ssid set to %s\n", wifi_config.ssid);
ocomeni 88:7ffa053be662 297 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 298 delete socket;
ocomeni 78:07bb86e3ce14 299 }
ocomeni 79:a2187bbfa407 300
ocomeni 79:a2187bbfa407 301
ocomeni 78:07bb86e3ce14 302 void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass)
ocomeni 78:07bb86e3ce14 303 {
ocomeni 78:07bb86e3ce14 304 strcpy(wifi_config.pass, wifi_pass);
ocomeni 92:ec9550034276 305 printf("[WIFI-MAN] wifi_pass set to %s\n", "****************");
ocomeni 88:7ffa053be662 306 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 307 delete socket;
ocomeni 78:07bb86e3ce14 308 }
ocomeni 79:a2187bbfa407 309
ocomeni 79:a2187bbfa407 310
ocomeni 78:07bb86e3ce14 311 void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security)
ocomeni 78:07bb86e3ce14 312 {
ocomeni 78:07bb86e3ce14 313 wifi_config.security = wifi_security;
ocomeni 82:10072c1794d3 314 printf("[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config.security));
ocomeni 88:7ffa053be662 315 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 316 delete socket;
ocomeni 78:07bb86e3ce14 317 }
ocomeni 79:a2187bbfa407 318
ocomeni 79:a2187bbfa407 319
ocomeni 81:637a87eb8170 320
ocomeni 81:637a87eb8170 321 void WiFiManager::set_internet_config()
ocomeni 81:637a87eb8170 322 {
ocomeni 81:637a87eb8170 323 internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer;
ocomeni 81:637a87eb8170 324 internet_config.peer_id = internet_cfg->peer_id;
ocomeni 84:7c7add00f4bf 325 strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1);
ocomeni 81:637a87eb8170 326 internet_config.connectionScheme = internet_cfg->connectionScheme;
ocomeni 81:637a87eb8170 327 free_DataMsg();
ocomeni 81:637a87eb8170 328 printf("[WIFI MAN] Internet configuration setup completed\n");
ocomeni 81:637a87eb8170 329 printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 84:7c7add00f4bf 330 internet_config.url,
ocomeni 81:637a87eb8170 331 internet_config.connectionScheme);
ocomeni 88:7ffa053be662 332 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 333 delete socket;
ocomeni 81:637a87eb8170 334 }
ocomeni 81:637a87eb8170 335
ocomeni 95:290859010c8c 336
ocomeni 95:290859010c8c 337
ocomeni 95:290859010c8c 338 void WiFiManager::getNetworkStatus(){
ocomeni 95:290859010c8c 339
ocomeni 95:290859010c8c 340 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 95:290859010c8c 341 net_stat_id_t status_id;
ocomeni 95:290859010c8c 342 char * nextStrPtr = responseString;
ocomeni 95:290859010c8c 343 for(int i=0; i< NumNetworkStatus;i++){
ocomeni 95:290859010c8c 344 status_id = netStatusIds[i]; // get current status id
ocomeni 95:290859010c8c 345 switch(status_id){
ocomeni 95:290859010c8c 346 case IF_HW_ADDRESS:
ocomeni 95:290859010c8c 347 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 348 WIFI_CHANNEL,
ocomeni 95:290859010c8c 349 status_id,
ocomeni 95:290859010c8c 350 network->get_mac_address());
ocomeni 95:290859010c8c 351 break;
ocomeni 95:290859010c8c 352 case NETWORK_IF_STATUS:
ocomeni 95:290859010c8c 353 sprintf(nextStrPtr, "\r\n%s%d,%d, %d\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 354 WIFI_CHANNEL,
ocomeni 95:290859010c8c 355 status_id,
ocomeni 95:290859010c8c 356 (uint8_t)is_connected);
ocomeni 95:290859010c8c 357 break;
ocomeni 95:290859010c8c 358 case INTERFACE_TYPE:
ocomeni 95:290859010c8c 359 sprintf(nextStrPtr, "\r\n%s%d,%d,%d\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 360 WIFI_CHANNEL,
ocomeni 95:290859010c8c 361 status_id,
ocomeni 95:290859010c8c 362 WIFI_STATION);
ocomeni 95:290859010c8c 363 break;
ocomeni 95:290859010c8c 364 case IPv4_ADDRESS:
ocomeni 95:290859010c8c 365 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 366 WIFI_CHANNEL,
ocomeni 95:290859010c8c 367 status_id,
ocomeni 95:290859010c8c 368 network->get_ip_address());
ocomeni 95:290859010c8c 369 break;
ocomeni 95:290859010c8c 370 case SUBNET_MASK:
ocomeni 95:290859010c8c 371 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 372 WIFI_CHANNEL,
ocomeni 95:290859010c8c 373 status_id,
ocomeni 95:290859010c8c 374 network->get_netmask());
ocomeni 95:290859010c8c 375 break;
ocomeni 95:290859010c8c 376 case GATEWAY_ADDRESS:
ocomeni 95:290859010c8c 377 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 378 WIFI_CHANNEL,
ocomeni 95:290859010c8c 379 status_id,
ocomeni 95:290859010c8c 380 network->get_gateway());
ocomeni 95:290859010c8c 381 break;
ocomeni 95:290859010c8c 382 case PRIMARY_DNS_SERVER:
ocomeni 95:290859010c8c 383 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 384 WIFI_CHANNEL,
ocomeni 95:290859010c8c 385 status_id,
ocomeni 95:290859010c8c 386 DEFAULT_DNS_ADDRESS);
ocomeni 95:290859010c8c 387 break;
ocomeni 95:290859010c8c 388 case SECONDARY_DNS_SERVER:
ocomeni 95:290859010c8c 389 sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 390 WIFI_CHANNEL,
ocomeni 95:290859010c8c 391 status_id,
ocomeni 95:290859010c8c 392 DEFAULT_DNS_ADDRESS);
ocomeni 95:290859010c8c 393 break;
ocomeni 95:290859010c8c 394 case IPv6_ADDRESS:
ocomeni 95:290859010c8c 395 sprintf(nextStrPtr, "\r\n%s%d,%d,::\r\n", NETWORK_STATUS,
ocomeni 95:290859010c8c 396 WIFI_CHANNEL,
ocomeni 95:290859010c8c 397 status_id);
ocomeni 95:290859010c8c 398 break;
ocomeni 95:290859010c8c 399 default:
ocomeni 95:290859010c8c 400 sprintf(nextStrPtr, "\r\n%s,::\r\n", NETWORK_STATUS);
ocomeni 95:290859010c8c 401 break;
ocomeni 95:290859010c8c 402 }
ocomeni 95:290859010c8c 403 nextStrPtr += strlen(nextStrPtr) +1; // progress to end of current string
ocomeni 95:290859010c8c 404 }
ocomeni 95:290859010c8c 405 }
ocomeni 95:290859010c8c 406
ocomeni 95:290859010c8c 407
ocomeni 95:290859010c8c 408
ocomeni 95:290859010c8c 409 void WiFiManager::getWiFiStatus(){
ocomeni 95:290859010c8c 410
ocomeni 95:290859010c8c 411 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 95:290859010c8c 412 wifi_stat_id_t status_id;
ocomeni 95:290859010c8c 413 char * nextStrPtr = responseString;
ocomeni 95:290859010c8c 414 for(int i=0; i< NumWiFiStatus;i++){
ocomeni 95:290859010c8c 415 status_id = wifiStatusIds[i]; // get current status id
ocomeni 95:290859010c8c 416 switch(status_id){
ocomeni 95:290859010c8c 417 case WIFI_SSID:
ocomeni 95:290859010c8c 418 sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 419 status_id,
ocomeni 95:290859010c8c 420 wifi_config.ssid);
ocomeni 95:290859010c8c 421 break;
ocomeni 95:290859010c8c 422 case WIFI_BSSID:
ocomeni 95:290859010c8c 423 sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 424 status_id,
ocomeni 95:290859010c8c 425 network->get_mac_address());
ocomeni 95:290859010c8c 426 break;
ocomeni 95:290859010c8c 427 case WIFI__CURRENT_CHANNEL:
ocomeni 95:290859010c8c 428 sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 429 status_id,
ocomeni 95:290859010c8c 430 DEFAULT_WIFI_CHANNEL);
ocomeni 95:290859010c8c 431 break;
ocomeni 95:290859010c8c 432 case WIFI_STA_STATUS:
ocomeni 95:290859010c8c 433 sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 434 status_id,
ocomeni 95:290859010c8c 435 (uint8_t)is_connected);
ocomeni 95:290859010c8c 436 break;
ocomeni 95:290859010c8c 437 case WIFI_RSSI:
ocomeni 95:290859010c8c 438 sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS,
ocomeni 95:290859010c8c 439 status_id,
ocomeni 95:290859010c8c 440 network->get_rssi());
ocomeni 95:290859010c8c 441 break;
ocomeni 95:290859010c8c 442 default:
ocomeni 95:290859010c8c 443 sprintf(nextStrPtr, "\r\n%s,::\r\n", WIFI_NETWORK_STATUS);
ocomeni 95:290859010c8c 444 break;
ocomeni 95:290859010c8c 445 }
ocomeni 95:290859010c8c 446 nextStrPtr += strlen(nextStrPtr) +1; // progress to end of current string
ocomeni 95:290859010c8c 447 }
ocomeni 95:290859010c8c 448 }
ocomeni 95:290859010c8c 449
ocomeni 95:290859010c8c 450
ocomeni 81:637a87eb8170 451 void WiFiManager::free_DataMsg()
ocomeni 81:637a87eb8170 452 {
ocomeni 81:637a87eb8170 453 // free memory after processing
ocomeni 81:637a87eb8170 454 _aT2WiFiDatamPool->free(data_msg);
ocomeni 96:f5ed273881af 455 data_msg = NULL;
ocomeni 81:637a87eb8170 456 }
ocomeni 81:637a87eb8170 457
ocomeni 81:637a87eb8170 458
ocomeni 88:7ffa053be662 459
ocomeni 81:637a87eb8170 460 void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
ocomeni 81:637a87eb8170 461 {
ocomeni 81:637a87eb8170 462 //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
ocomeni 81:637a87eb8170 463 //}
ocomeni 81:637a87eb8170 464 switch(param) {
ocomeni 81:637a87eb8170 465 case NSAPI_STATUS_LOCAL_UP:
ocomeni 81:637a87eb8170 466 printf("[WIFI-MAN] Local IP address set!\r\n");
ocomeni 81:637a87eb8170 467 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 468 break;
ocomeni 81:637a87eb8170 469 case NSAPI_STATUS_GLOBAL_UP:
ocomeni 81:637a87eb8170 470 printf("Global IP address set!\r\n");
ocomeni 81:637a87eb8170 471 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 472 printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
ocomeni 95:290859010c8c 473 responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN);
ocomeni 95:290859010c8c 474 sprintf(responseString, "\r\n%s%d,%s,%d\r\n", WIFI_LINK_ENABLED,
ocomeni 95:290859010c8c 475 WIFI_CHANNEL,
ocomeni 95:290859010c8c 476 network->get_mac_address(),
ocomeni 95:290859010c8c 477 DEFAULT_WIFI_CHANNEL);
ocomeni 95:290859010c8c 478
ocomeni 81:637a87eb8170 479 is_connected = true;
ocomeni 81:637a87eb8170 480 break;
ocomeni 81:637a87eb8170 481 case NSAPI_STATUS_DISCONNECTED:
ocomeni 81:637a87eb8170 482 printf("No connection to network!\r\n");
ocomeni 81:637a87eb8170 483 printf("\n [WIFI-MAN] No connection to network!\n");
ocomeni 81:637a87eb8170 484 is_connected = false;
ocomeni 81:637a87eb8170 485 //queueATresponse(AT_DISCONNECT_RESP);
ocomeni 81:637a87eb8170 486 // attempt reconnection if always connected scheme is set
ocomeni 81:637a87eb8170 487 if(internet_config.connectionScheme == ALWAYS_CONNECTED)
ocomeni 81:637a87eb8170 488 {
ocomeni 81:637a87eb8170 489 nsapi_error_t error;
ocomeni 91:d6b6319ad681 490 error = connect();
ocomeni 81:637a87eb8170 491 queueATresponse(WIFI_RECONNECT_INFO);
ocomeni 81:637a87eb8170 492 }
ocomeni 81:637a87eb8170 493 break;
ocomeni 81:637a87eb8170 494 case NSAPI_STATUS_CONNECTING:
ocomeni 81:637a87eb8170 495 printf("Connecting to network!\r\n");
ocomeni 81:637a87eb8170 496 break;
ocomeni 81:637a87eb8170 497 default:
ocomeni 81:637a87eb8170 498 printf("Not supported");
ocomeni 81:637a87eb8170 499 break;
ocomeni 81:637a87eb8170 500 }
ocomeni 81:637a87eb8170 501 }
ocomeni 81:637a87eb8170 502
ocomeni 81:637a87eb8170 503
ocomeni 81:637a87eb8170 504
ocomeni 79:a2187bbfa407 505 // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */
ocomeni 79:a2187bbfa407 506 // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */
ocomeni 79:a2187bbfa407 507 // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */
ocomeni 79:a2187bbfa407 508 // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */
ocomeni 79:a2187bbfa407 509 // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED
ocomeni 79:a2187bbfa407 510
ocomeni 79:a2187bbfa407 511 nsapi_error_t WiFiManager::connect()
ocomeni 79:a2187bbfa407 512 {
ocomeni 79:a2187bbfa407 513 nsapi_error_t error;
ocomeni 81:637a87eb8170 514 printf("\n [WIFI-MAN] About to connect to WiFi network\n");
ocomeni 81:637a87eb8170 515 network->attach(callback(this, &WiFiManager::status_callback));
ocomeni 79:a2187bbfa407 516 error = network->set_blocking(false);
ocomeni 79:a2187bbfa407 517 if(error)
ocomeni 79:a2187bbfa407 518 {
ocomeni 79:a2187bbfa407 519 printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
ocomeni 79:a2187bbfa407 520 return error;
ocomeni 79:a2187bbfa407 521 }
ocomeni 93:06e755a80187 522 printf("[WIFI-MAN] Connecting to network ssid = %s passwd = %s security = %s \r\n",
ocomeni 93:06e755a80187 523 wifi_config.ssid,
ocomeni 96:f5ed273881af 524 "****************",
ocomeni 93:06e755a80187 525 sec2str(wifi_config.security));
ocomeni 79:a2187bbfa407 526 error = network->connect(wifi_config.ssid,
ocomeni 79:a2187bbfa407 527 wifi_config.pass,
ocomeni 79:a2187bbfa407 528 wifi_config.security);
ocomeni 81:637a87eb8170 529 return error;
ocomeni 79:a2187bbfa407 530 }
ocomeni 79:a2187bbfa407 531
ocomeni 79:a2187bbfa407 532
ocomeni 79:a2187bbfa407 533 nsapi_error_t WiFiManager::disconnect()
ocomeni 78:07bb86e3ce14 534 {
ocomeni 79:a2187bbfa407 535 nsapi_error_t error;
ocomeni 79:a2187bbfa407 536 error = network->disconnect();
ocomeni 79:a2187bbfa407 537 return error;
ocomeni 78:07bb86e3ce14 538 }
ocomeni 79:a2187bbfa407 539
ocomeni 88:7ffa053be662 540 void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen)
ocomeni 90:ed0267eca7b5 541 {
ocomeni 90:ed0267eca7b5 542
ocomeni 96:f5ed273881af 543 printf("before call to new at_data_msg_t \n");
ocomeni 96:f5ed273881af 544 print_memory_info();
ocomeni 88:7ffa053be662 545 at_data_resp = new at_data_msg_t;
ocomeni 96:f5ed273881af 546 printf("after call to new at_data_msg_t \n");
ocomeni 96:f5ed273881af 547 print_memory_info();
ocomeni 88:7ffa053be662 548 at_data_resp->at_resp = at_cmd;
ocomeni 88:7ffa053be662 549 size_t bufSize = sizeof(at_data_resp->buffer);
ocomeni 88:7ffa053be662 550 int pos = 0;
ocomeni 88:7ffa053be662 551 at_data_resp->dataLen = 0;
ocomeni 88:7ffa053be662 552 bool queueResult = true;
ocomeni 90:ed0267eca7b5 553 int hdrLen = 0;
ocomeni 94:fb4414aff957 554 int wait_count = 0;
ocomeni 88:7ffa053be662 555 do {
ocomeni 94:fb4414aff957 556 if(!queueResult){
ocomeni 94:fb4414aff957 557 wait_count++;
ocomeni 94:fb4414aff957 558 printf("[WIFI-MAN] ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 94:fb4414aff957 559 wait_ms(10);
ocomeni 90:ed0267eca7b5 560 }
ocomeni 90:ed0267eca7b5 561 else {
ocomeni 90:ed0267eca7b5 562 if(http_response_hdr_sent == false){
ocomeni 90:ed0267eca7b5 563 copyResponseHdr2Queue();
ocomeni 94:fb4414aff957 564 printf("[WIFI-MAN] Http Response header copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
ocomeni 90:ed0267eca7b5 565 hdrLen = at_data_resp->dataLen;
ocomeni 90:ed0267eca7b5 566 http_response_hdr_sent = true;
ocomeni 90:ed0267eca7b5 567 }
ocomeni 94:fb4414aff957 568 int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos) ;
ocomeni 94:fb4414aff957 569 printf("[WIFI-MAN] Http Response body [bytes = %d] \r\n",cpyLen);
ocomeni 90:ed0267eca7b5 570 at_data_resp->dataLen += cpyLen;
ocomeni 90:ed0267eca7b5 571 memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen);
ocomeni 94:fb4414aff957 572 printf("[WIFI-MAN] Http Response header and body copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
ocomeni 90:ed0267eca7b5 573 }
ocomeni 88:7ffa053be662 574 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 90:ed0267eca7b5 575 if(queueResult){
ocomeni 90:ed0267eca7b5 576 pos+= at_data_resp->dataLen;
ocomeni 90:ed0267eca7b5 577 at_data_resp->dataLen = 0;
ocomeni 90:ed0267eca7b5 578 hdrLen = 0;
ocomeni 90:ed0267eca7b5 579 }
ocomeni 88:7ffa053be662 580 }while(queueResult == false || pos < bufLen);
ocomeni 94:fb4414aff957 581 printf("[WIFI-MAN] response data queued - deleting data memory\r\n");
ocomeni 88:7ffa053be662 582 delete at_data_resp;
ocomeni 88:7ffa053be662 583 }
ocomeni 79:a2187bbfa407 584
ocomeni 90:ed0267eca7b5 585 void WiFiManager::copyResponseHdr2Queue()
ocomeni 90:ed0267eca7b5 586 {
ocomeni 90:ed0267eca7b5 587 int numChars = 0;
ocomeni 90:ed0267eca7b5 588 // create message pointer for response header generation
ocomeni 90:ed0267eca7b5 589 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 90:ed0267eca7b5 590 // do status line
ocomeni 90:ed0267eca7b5 591 numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(),
ocomeni 90:ed0267eca7b5 592 http_response->get_status_message().c_str());
ocomeni 96:f5ed273881af 593 printf("after getting HTTP status line \n");
ocomeni 96:f5ed273881af 594 print_memory_info();
ocomeni 90:ed0267eca7b5 595 msgPtr += numChars;
ocomeni 96:f5ed273881af 596 //vector<string*> hdrFields(http_response->get_headers_fields());
ocomeni 96:f5ed273881af 597 //hdrFields.reserve(http_response->get_headers_length());
ocomeni 96:f5ed273881af 598 //printf("after defining HTTP header fields vector \r\n");
ocomeni 96:f5ed273881af 599 //hdrFields = http_response->get_headers_fields();
ocomeni 96:f5ed273881af 600 //printf("after getting HTTP header fields \n");
ocomeni 96:f5ed273881af 601 //print_memory_info();
ocomeni 96:f5ed273881af 602 //vector<string*> hdrValues(http_response->get_headers_values());
ocomeni 96:f5ed273881af 603 //printf("after getting HTTP header values \n");
ocomeni 96:f5ed273881af 604 //print_memory_info();
ocomeni 96:f5ed273881af 605 int hdrsLen = http_response->get_headers_length();
ocomeni 96:f5ed273881af 606 printf("after getting HTTP headers length = %d\n", hdrsLen);
ocomeni 96:f5ed273881af 607 print_memory_info();
ocomeni 96:f5ed273881af 608 for (size_t ix = 0; ix < hdrsLen; ix++) {
ocomeni 96:f5ed273881af 609 int sLen = http_response->get_headers_fields()[ix]->size()+1;
ocomeni 96:f5ed273881af 610 printf("before creating allocation HTTP headers field [size = %d] \n", sLen);
ocomeni 96:f5ed273881af 611 char * hdrField = (char *) malloc(sLen);
ocomeni 96:f5ed273881af 612 printf("after creating allocation HTTP headers field \n");
ocomeni 96:f5ed273881af 613 print_memory_info();
ocomeni 96:f5ed273881af 614 std::strcpy (hdrField, http_response->get_headers_fields()[ix]->c_str());
ocomeni 96:f5ed273881af 615 printf("after getting HTTP headers field copy \n");
ocomeni 96:f5ed273881af 616 print_memory_info();
ocomeni 96:f5ed273881af 617 char * hdrValue = new char [http_response->get_headers_values()[ix]->size()+1];
ocomeni 96:f5ed273881af 618 std::strcpy (hdrValue, http_response->get_headers_values()[ix]->c_str());
ocomeni 96:f5ed273881af 619 numChars = sprintf(msgPtr, "%s: %s\r\n", hdrField, hdrValue);
ocomeni 96:f5ed273881af 620 //numChars = sprintf(msgPtr, "%s: %s\r\n",
ocomeni 96:f5ed273881af 621 // http_response->get_headers_fields()[ix]->c_str(),
ocomeni 96:f5ed273881af 622 // http_response->get_headers_values()[ix]->c_str());
ocomeni 96:f5ed273881af 623 printf("after call #%d to get http header field:values \r\n", ix);
ocomeni 96:f5ed273881af 624 print_memory_info();
ocomeni 96:f5ed273881af 625 free(hdrField);
ocomeni 96:f5ed273881af 626 delete hdrField;
ocomeni 90:ed0267eca7b5 627 msgPtr += numChars;
ocomeni 90:ed0267eca7b5 628 }
ocomeni 90:ed0267eca7b5 629 numChars = sprintf(msgPtr, "\r\n");
ocomeni 90:ed0267eca7b5 630 msgPtr += numChars;
ocomeni 90:ed0267eca7b5 631 // print out generated header
ocomeni 90:ed0267eca7b5 632 printf("[WiFi MAN] generated response header:\n");
ocomeni 90:ed0267eca7b5 633 printf("%s\r\n", (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 634 // calculate header length
ocomeni 90:ed0267eca7b5 635 at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 636 }
ocomeni 90:ed0267eca7b5 637
ocomeni 84:7c7add00f4bf 638 void WiFiManager::return_response(HttpResponse* res) {
ocomeni 84:7c7add00f4bf 639
ocomeni 88:7ffa053be662 640 at_data_resp = new at_data_msg_t;
ocomeni 87:99b37d26ff2a 641 int numChars = 0;
ocomeni 87:99b37d26ff2a 642 // create message pointer for response header generation
ocomeni 88:7ffa053be662 643 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 87:99b37d26ff2a 644 // do status line
ocomeni 87:99b37d26ff2a 645 numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str());
ocomeni 87:99b37d26ff2a 646 msgPtr += numChars;
ocomeni 84:7c7add00f4bf 647 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
ocomeni 87:99b37d26ff2a 648 numChars = sprintf(msgPtr, "%s: %s\r\n",
ocomeni 87:99b37d26ff2a 649 res->get_headers_fields()[ix]->c_str(),
ocomeni 87:99b37d26ff2a 650 res->get_headers_values()[ix]->c_str());
ocomeni 87:99b37d26ff2a 651 msgPtr += numChars;
ocomeni 84:7c7add00f4bf 652 }
ocomeni 87:99b37d26ff2a 653 numChars = sprintf(msgPtr, "\r\n\r\n");
ocomeni 87:99b37d26ff2a 654 msgPtr += numChars;
ocomeni 87:99b37d26ff2a 655 // print out generated header
ocomeni 90:ed0267eca7b5 656 printf("[WiFi MAN] generated response header:\n");
ocomeni 88:7ffa053be662 657 printf("%s\r\n", (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 658 // calculate header length
ocomeni 89:45f6db09a76d 659 at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
ocomeni 84:7c7add00f4bf 660
ocomeni 90:ed0267eca7b5 661 // package and send on wifi data queue
ocomeni 88:7ffa053be662 662 at_data_resp->at_resp = AT_HTTPS_RESP;
ocomeni 89:45f6db09a76d 663 bool queueResult = true;
ocomeni 89:45f6db09a76d 664 int wait_count = 0;
ocomeni 89:45f6db09a76d 665 do
ocomeni 89:45f6db09a76d 666 {
ocomeni 89:45f6db09a76d 667 if(!queueResult){
ocomeni 89:45f6db09a76d 668 wait_count++;
ocomeni 89:45f6db09a76d 669 printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 89:45f6db09a76d 670 wait_ms(10);
ocomeni 89:45f6db09a76d 671 }
ocomeni 89:45f6db09a76d 672 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 89:45f6db09a76d 673 }while(queueResult == false);
ocomeni 88:7ffa053be662 674 delete at_data_resp;
ocomeni 84:7c7add00f4bf 675 }
ocomeni 90:ed0267eca7b5 676
ocomeni 90:ed0267eca7b5 677
ocomeni 90:ed0267eca7b5 678 void WiFiManager::printBufferInHex(uint8_t *buf, int pLen)
ocomeni 90:ed0267eca7b5 679 {
ocomeni 90:ed0267eca7b5 680 for(int i =0;i<pLen;i++){
ocomeni 90:ed0267eca7b5 681 if(i%8==0) printf("\n[%3d]",i);
ocomeni 90:ed0267eca7b5 682 printf("%02x ", buf[i]);
ocomeni 90:ed0267eca7b5 683 }
ocomeni 90:ed0267eca7b5 684 printf("\n");
ocomeni 90:ed0267eca7b5 685 }
ocomeni 90:ed0267eca7b5 686
ocomeni 88:7ffa053be662 687 //#define TRY_PRINTF
ocomeni 84:7c7add00f4bf 688
ocomeni 90:ed0267eca7b5 689 void WiFiManager::body_callback(const char *at, uint32_t length) {
ocomeni 84:7c7add00f4bf 690 printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length);
ocomeni 84:7c7add00f4bf 691 chunkNum++;
ocomeni 89:45f6db09a76d 692 sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
ocomeni 84:7c7add00f4bf 693 }
ocomeni 84:7c7add00f4bf 694
ocomeni 88:7ffa053be662 695
ocomeni 88:7ffa053be662 696 bool WiFiManager::createTLSconnection(const char * hostName)
ocomeni 88:7ffa053be662 697 {
ocomeni 93:06e755a80187 698 #ifdef ENABLE_MBED_TRACE
ocomeni 93:06e755a80187 699 mbed_trace_init();
ocomeni 93:06e755a80187 700 #endif
ocomeni 88:7ffa053be662 701 socket = new TLSSocket();
ocomeni 88:7ffa053be662 702
ocomeni 88:7ffa053be662 703 nsapi_error_t r;
ocomeni 88:7ffa053be662 704 // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 705 r = socket->open(network);
ocomeni 88:7ffa053be662 706 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 707 {
ocomeni 89:45f6db09a76d 708 printf("TLS open failed!!\n");
ocomeni 88:7ffa053be662 709 return false;
ocomeni 88:7ffa053be662 710 }
ocomeni 88:7ffa053be662 711 printf("TLS open passed!!\n");
ocomeni 88:7ffa053be662 712 r = socket->set_root_ca_cert(SSL_CA_PEM);
ocomeni 88:7ffa053be662 713 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 714 {
ocomeni 88:7ffa053be662 715 printf("TLS set_root_ca_cert failed!!\n");
ocomeni 88:7ffa053be662 716 return false;
ocomeni 88:7ffa053be662 717 }
ocomeni 88:7ffa053be662 718 printf("TLS set_root_ca_cert passed!!\n");
ocomeni 88:7ffa053be662 719 r = socket->connect(hostName, 443);
ocomeni 88:7ffa053be662 720 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 721 {
ocomeni 93:06e755a80187 722 char errstr[100];
ocomeni 93:06e755a80187 723 mbedtls_strerror(r, errstr, 100);
ocomeni 93:06e755a80187 724 printf("TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr);
ocomeni 93:06e755a80187 725 socket->close();
ocomeni 88:7ffa053be662 726 return false;
ocomeni 88:7ffa053be662 727 }
ocomeni 88:7ffa053be662 728 printf("TLS connection successful for https site : %s\n", hostName);
ocomeni 88:7ffa053be662 729 return true;
ocomeni 88:7ffa053be662 730 }
ocomeni 88:7ffa053be662 731 #define TESTING_HTTPS
ocomeni 96:f5ed273881af 732 //#define DONT_USE_TLS_SOCKET
ocomeni 84:7c7add00f4bf 733 void WiFiManager::createHttpsRequest()
ocomeni 79:a2187bbfa407 734 {
ocomeni 87:99b37d26ff2a 735 // reset chunk #;
ocomeni 87:99b37d26ff2a 736 chunkNum = 0;
ocomeni 96:f5ed273881af 737 #ifdef MIX_HDR_AND_BODY
ocomeni 90:ed0267eca7b5 738 http_response_hdr_sent = false;
ocomeni 96:f5ed273881af 739 #else
ocomeni 96:f5ed273881af 740 http_response_hdr_sent = true;
ocomeni 96:f5ed273881af 741 #endif
ocomeni 89:45f6db09a76d 742 printf("\n[WIFI MAN] Http Request received:\n");
ocomeni 84:7c7add00f4bf 743 http_req_cfg = (http_request_t *) data_msg->buffer;
ocomeni 90:ed0267eca7b5 744 printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
ocomeni 89:45f6db09a76d 745 printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url);
ocomeni 87:99b37d26ff2a 746 char full_url[100];
ocomeni 88:7ffa053be662 747 char host[60] ;
ocomeni 87:99b37d26ff2a 748 strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
ocomeni 90:ed0267eca7b5 749 strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1);
ocomeni 90:ed0267eca7b5 750 strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1);
ocomeni 87:99b37d26ff2a 751 printf("\n[WIFI MAN] server url+uri = %s\n", full_url);
ocomeni 90:ed0267eca7b5 752 printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName);
ocomeni 90:ed0267eca7b5 753 printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal);
ocomeni 90:ed0267eca7b5 754 printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType);
ocomeni 90:ed0267eca7b5 755 printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen);
ocomeni 84:7c7add00f4bf 756
ocomeni 84:7c7add00f4bf 757 int bodyLen;
ocomeni 90:ed0267eca7b5 758 sscanf(http_req_cfg->contentLen, "%d", &bodyLen);
ocomeni 90:ed0267eca7b5 759 printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen);
ocomeni 90:ed0267eca7b5 760
ocomeni 90:ed0267eca7b5 761 if(bodyLen > 10){
ocomeni 93:06e755a80187 762 printf("\n [WIFI MAN] Message Body:\n");
ocomeni 90:ed0267eca7b5 763 printBufferInHex(http_req_cfg->body, bodyLen);
ocomeni 90:ed0267eca7b5 764 }
ocomeni 87:99b37d26ff2a 765 if(strstr(internet_config.url, "http:")!=NULL) // http request
ocomeni 87:99b37d26ff2a 766 {
ocomeni 87:99b37d26ff2a 767 http_request = new HttpRequest(network,
ocomeni 90:ed0267eca7b5 768 http_req_cfg->method,
ocomeni 90:ed0267eca7b5 769 full_url,
ocomeni 90:ed0267eca7b5 770 callback(this, &WiFiManager::body_callback));
ocomeni 87:99b37d26ff2a 771 setHttpHeader("Host", http_req_cfg->hostName);
ocomeni 87:99b37d26ff2a 772 setHttpHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 89:45f6db09a76d 773 printf("http_req_cfg->method = %d\n", http_req_cfg->method);
ocomeni 88:7ffa053be662 774 if(http_req_cfg->method == HTTP_GET){
ocomeni 88:7ffa053be662 775 printf("HTTP_GET -- ignoring body\n");
ocomeni 88:7ffa053be662 776 //setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 777 //setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 778 http_response = http_request->send(NULL, 0);
ocomeni 88:7ffa053be662 779 }
ocomeni 88:7ffa053be662 780 else{
ocomeni 88:7ffa053be662 781 setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 782 setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 783 http_response = http_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 784 }
ocomeni 87:99b37d26ff2a 785 free_DataMsg();
ocomeni 87:99b37d26ff2a 786 if (!http_response) {
ocomeni 87:99b37d26ff2a 787 char buf[100];
ocomeni 87:99b37d26ff2a 788 mbedtls_strerror(http_request->get_error(), buf, 100);
ocomeni 87:99b37d26ff2a 789 printf("HttpRequest failed (error code %s)\n", buf);
ocomeni 87:99b37d26ff2a 790 //printf("HttpsRequest failed (error code %d)\n", https_request->get_error());
ocomeni 87:99b37d26ff2a 791 delete http_request; // free the memory
ocomeni 87:99b37d26ff2a 792 return;
ocomeni 87:99b37d26ff2a 793 }
ocomeni 88:7ffa053be662 794 delete http_request; // free the memory
ocomeni 87:99b37d26ff2a 795 printf("\n----- HTTP POST response -----\n");
ocomeni 84:7c7add00f4bf 796 }
ocomeni 87:99b37d26ff2a 797 else
ocomeni 87:99b37d26ff2a 798 {
ocomeni 90:ed0267eca7b5 799 #ifndef DONT_USE_TLS_SOCKET
ocomeni 88:7ffa053be662 800 if(https_connection_active == false){
ocomeni 88:7ffa053be662 801 bool tlsResult;
ocomeni 88:7ffa053be662 802 tlsResult = createTLSconnection(host);
ocomeni 93:06e755a80187 803 #ifdef ENABLE_MBED_TRACE
ocomeni 93:06e755a80187 804 mbed_trace_free(); // free trace memory
ocomeni 93:06e755a80187 805 #endif
ocomeni 88:7ffa053be662 806 if(tlsResult == false){
ocomeni 88:7ffa053be662 807 delete socket;
ocomeni 88:7ffa053be662 808 free_DataMsg();
ocomeni 88:7ffa053be662 809 return;
ocomeni 88:7ffa053be662 810 }
ocomeni 96:f5ed273881af 811 //printf("[create https] TLS connection successful for https site : %s\n", host);
ocomeni 96:f5ed273881af 812 printf("after call to createTLSconnection \n");
ocomeni 96:f5ed273881af 813 print_memory_info();
ocomeni 88:7ffa053be662 814 }
ocomeni 88:7ffa053be662 815 // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
ocomeni 88:7ffa053be662 816 //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
ocomeni 88:7ffa053be662 817 //_wmutex.lock();
ocomeni 88:7ffa053be662 818 https_request = new HttpsRequest(socket,
ocomeni 87:99b37d26ff2a 819 http_req_cfg->method,
ocomeni 88:7ffa053be662 820 full_url,
ocomeni 87:99b37d26ff2a 821 callback(this, &WiFiManager::body_callback));
ocomeni 90:ed0267eca7b5 822 #else
ocomeni 90:ed0267eca7b5 823 https_request = new HttpsRequest(network,
ocomeni 90:ed0267eca7b5 824 SSL_CA_PEM,
ocomeni 90:ed0267eca7b5 825 http_req_cfg->method,
ocomeni 90:ed0267eca7b5 826 full_url,
ocomeni 90:ed0267eca7b5 827 callback(this, &WiFiManager::body_callback));
ocomeni 90:ed0267eca7b5 828 #endif
ocomeni 88:7ffa053be662 829 #ifdef TESTING_HTTPS
ocomeni 90:ed0267eca7b5 830 printf("http_req_cfg->method = %d\n", http_req_cfg->method);
ocomeni 88:7ffa053be662 831 if(http_req_cfg->method == HTTP_GET){
ocomeni 88:7ffa053be662 832 printf("HTTP_GET -- ignoring body\n");
ocomeni 89:45f6db09a76d 833 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 834 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 88:7ffa053be662 835 //setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 836 //setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 837 http_response = https_request->send(NULL, 0);
ocomeni 88:7ffa053be662 838 }
ocomeni 88:7ffa053be662 839 else{
ocomeni 96:f5ed273881af 840 printf("about to setup https headers\r\n");
ocomeni 89:45f6db09a76d 841 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 842 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 90:ed0267eca7b5 843 setHttpsHeader("Content-Type", http_req_cfg->contentType);
ocomeni 90:ed0267eca7b5 844 setHttpsHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 96:f5ed273881af 845 printf("https headers setup - about to send request\r\n");
ocomeni 96:f5ed273881af 846 //_wmutex.lock();
ocomeni 88:7ffa053be662 847 http_response = https_request->send(http_req_cfg->body, bodyLen);
ocomeni 96:f5ed273881af 848 //_wmutex.unlock();
ocomeni 88:7ffa053be662 849 }
ocomeni 88:7ffa053be662 850 #else
ocomeni 89:45f6db09a76d 851 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 852 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 96:f5ed273881af 853 setHttpsHeader("Content-Type", http_req_cfg->contentType);
ocomeni 96:f5ed273881af 854 setHttpsHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 87:99b37d26ff2a 855 http_response = https_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 856 #endif
ocomeni 88:7ffa053be662 857
ocomeni 88:7ffa053be662 858 //_wmutex.unlock();
ocomeni 90:ed0267eca7b5 859 //free_DataMsg();
ocomeni 87:99b37d26ff2a 860 if (!http_response) {
ocomeni 87:99b37d26ff2a 861 char buf[100];
ocomeni 87:99b37d26ff2a 862 mbedtls_strerror(https_request->get_error(), buf, 100);
ocomeni 88:7ffa053be662 863 printf("HttpsRequest failed (error code %s)\n", buf);
ocomeni 87:99b37d26ff2a 864 delete https_request; // free the memory
ocomeni 96:f5ed273881af 865 https_request = NULL;
ocomeni 88:7ffa053be662 866 https_connection_active = false; // reset true whenever connection fails
ocomeni 88:7ffa053be662 867 delete socket;
ocomeni 96:f5ed273881af 868 socket = NULL;
ocomeni 90:ed0267eca7b5 869 free_DataMsg();
ocomeni 87:99b37d26ff2a 870 return;
ocomeni 87:99b37d26ff2a 871 }
ocomeni 88:7ffa053be662 872 https_connection_active = true; // set true whenever connection succeeds
ocomeni 87:99b37d26ff2a 873 printf("\n----- HTTPS POST response -----\r\n");
ocomeni 90:ed0267eca7b5 874 }
ocomeni 96:f5ed273881af 875 if(http_response != NULL){
ocomeni 96:f5ed273881af 876
ocomeni 96:f5ed273881af 877 #ifndef MIX_HDR_AND_BODY
ocomeni 96:f5ed273881af 878 return_response(http_response);
ocomeni 96:f5ed273881af 879 #endif
ocomeni 96:f5ed273881af 880 //delete http_response; // free the response memory
ocomeni 96:f5ed273881af 881 //http_response = NULL;
ocomeni 96:f5ed273881af 882 //printf("deleted http_response\r\n");
ocomeni 96:f5ed273881af 883 }
ocomeni 96:f5ed273881af 884 free_DataMsg();
ocomeni 96:f5ed273881af 885 delete https_request; // free the request & response memory
ocomeni 96:f5ed273881af 886 printf("deleted https_request\r\n");
ocomeni 96:f5ed273881af 887 https_request = NULL;
ocomeni 79:a2187bbfa407 888 }
ocomeni 79:a2187bbfa407 889
ocomeni 79:a2187bbfa407 890 void WiFiManager::createHttpRequest(http_method method,
ocomeni 79:a2187bbfa407 891 const char* url,
ocomeni 79:a2187bbfa407 892 Callback<void(const char *at, uint32_t length)> body_callback)
ocomeni 79:a2187bbfa407 893 {
ocomeni 84:7c7add00f4bf 894 http_request = new HttpRequest(network,
ocomeni 84:7c7add00f4bf 895 method, url, body_callback);;
ocomeni 79:a2187bbfa407 896 }
ocomeni 79:a2187bbfa407 897
ocomeni 79:a2187bbfa407 898 void WiFiManager::setHttpHeader(string key, string value)
ocomeni 79:a2187bbfa407 899 {
ocomeni 79:a2187bbfa407 900 http_request->set_header(key, value);
ocomeni 79:a2187bbfa407 901 }
ocomeni 79:a2187bbfa407 902
ocomeni 79:a2187bbfa407 903 void WiFiManager::setHttpsHeader(string key, string value)
ocomeni 79:a2187bbfa407 904 {
ocomeni 79:a2187bbfa407 905 https_request->set_header(key, value);
ocomeni 79:a2187bbfa407 906 }
ocomeni 79:a2187bbfa407 907
ocomeni 79:a2187bbfa407 908 void WiFiManager::sendHttpsRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 909 {
ocomeni 78:07bb86e3ce14 910 }
ocomeni 79:a2187bbfa407 911
ocomeni 79:a2187bbfa407 912 void WiFiManager::sendHttpRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 913 {
ocomeni 78:07bb86e3ce14 914 }
ocomeni 79:a2187bbfa407 915