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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Apr 14 17:03:02 2019 +0000
Revision:
94:fb4414aff957
Parent:
93:06e755a80187
Child:
95:290859010c8c
working version - now passes python test.

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 93:06e755a80187 99 queueATresponse(AT_COMMAND_FAILED);
ocomeni 93:06e755a80187 100 }
ocomeni 93:06e755a80187 101 else {
ocomeni 93:06e755a80187 102 queueATresponse(AT_CONNECT_RESP);
ocomeni 93:06e755a80187 103 }
ocomeni 79:a2187bbfa407 104 break;
ocomeni 79:a2187bbfa407 105 case WIFI_CMD_DISCONNECT:
ocomeni 81:637a87eb8170 106 error = disconnect();
ocomeni 81:637a87eb8170 107 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 108 queueATresponse(AT_DISCONNECT_RESP);
ocomeni 81:637a87eb8170 109 break;
ocomeni 81:637a87eb8170 110 case WIFI_CMD_CONFIG:
ocomeni 81:637a87eb8170 111 set_WIFI_CONFIG();
ocomeni 81:637a87eb8170 112 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 113 queueATresponse(AT_CONFIG_RESP);
ocomeni 82:10072c1794d3 114 break;
ocomeni 81:637a87eb8170 115 case WIFI_CMD_INTERNET_CONFIG:
ocomeni 81:637a87eb8170 116 set_internet_config();
ocomeni 81:637a87eb8170 117 wifiCmd = WIFI_CMD_NONE;
ocomeni 81:637a87eb8170 118 queueATresponse(AT_INTERNET_CONFIG_RESP);
ocomeni 79:a2187bbfa407 119 break;
ocomeni 79:a2187bbfa407 120 case WIFI_CMD_SEND_HTTPS_REQ:
ocomeni 88:7ffa053be662 121 printf("before call to send http request \n");
ocomeni 88:7ffa053be662 122 print_memory_info();
ocomeni 84:7c7add00f4bf 123 createHttpsRequest();
ocomeni 88:7ffa053be662 124 printf("after call to send http request \n");
ocomeni 88:7ffa053be662 125 print_memory_info();
ocomeni 84:7c7add00f4bf 126 wifiCmd = WIFI_CMD_NONE;
ocomeni 79:a2187bbfa407 127 break;
ocomeni 79:a2187bbfa407 128 case WIFI_CMD_SEND_HTTP_REQ:
ocomeni 79:a2187bbfa407 129 break;
ocomeni 79:a2187bbfa407 130 default:
ocomeni 79:a2187bbfa407 131 break;
ocomeni 79:a2187bbfa407 132 }
ocomeni 79:a2187bbfa407 133 wait_ms(100); //
ocomeni 79:a2187bbfa407 134 }
ocomeni 79:a2187bbfa407 135
ocomeni 78:07bb86e3ce14 136 }
ocomeni 79:a2187bbfa407 137
ocomeni 91:d6b6319ad681 138
ocomeni 91:d6b6319ad681 139 void WiFiManager::sentATresponseString(at_cmd_resp_t at_cmd,
ocomeni 91:d6b6319ad681 140 const char *responseString,
ocomeni 91:d6b6319ad681 141 int strLen)
ocomeni 91:d6b6319ad681 142 {
ocomeni 91:d6b6319ad681 143 at_data_resp = new at_data_msg_t;
ocomeni 91:d6b6319ad681 144 // create message pointer for response header generation
ocomeni 91:d6b6319ad681 145 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 91:d6b6319ad681 146 // set string length
ocomeni 91:d6b6319ad681 147 at_data_resp->dataLen = strLen;
ocomeni 91:d6b6319ad681 148 memcpy(at_data_resp->buffer, responseString, strLen);
ocomeni 91:d6b6319ad681 149 // package and send on wifi data queue
ocomeni 91:d6b6319ad681 150 at_data_resp->at_resp = at_cmd;
ocomeni 91:d6b6319ad681 151 bool queueResult = true;
ocomeni 91:d6b6319ad681 152 int wait_count = 0;
ocomeni 91:d6b6319ad681 153 do
ocomeni 91:d6b6319ad681 154 {
ocomeni 91:d6b6319ad681 155 if(!queueResult){
ocomeni 91:d6b6319ad681 156 wait_count++;
ocomeni 91:d6b6319ad681 157 printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 91:d6b6319ad681 158 wait_ms(10);
ocomeni 91:d6b6319ad681 159 }
ocomeni 91:d6b6319ad681 160 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 91:d6b6319ad681 161 }while(queueResult == false);
ocomeni 91:d6b6319ad681 162 delete at_data_resp;
ocomeni 91:d6b6319ad681 163 }
ocomeni 91:d6b6319ad681 164
ocomeni 79:a2187bbfa407 165 bool WiFiManager::dequeueWiFiCommands(){
ocomeni 81:637a87eb8170 166 if(wifiCmd != WIFI_CMD_NONE) return false; // busy
ocomeni 81:637a87eb8170 167 osEvent evt = _aT2WiFiCmdQueue->get(0);
ocomeni 79:a2187bbfa407 168 if(evt.status == osEventMessage){
ocomeni 79:a2187bbfa407 169 wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p;
ocomeni 79:a2187bbfa407 170 setNextCommand(cmd->wifi_cmd);
ocomeni 92:ec9550034276 171 #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL
ocomeni 79:a2187bbfa407 172 _aT2WiFimPool->free(cmd);
ocomeni 92:ec9550034276 173 #else
ocomeni 92:ec9550034276 174 free(cmd);
ocomeni 92:ec9550034276 175 #endif
ocomeni 79:a2187bbfa407 176 }
ocomeni 79:a2187bbfa407 177 return true;
ocomeni 79:a2187bbfa407 178 }
ocomeni 79:a2187bbfa407 179
ocomeni 79:a2187bbfa407 180
ocomeni 81:637a87eb8170 181 bool WiFiManager::dequeueATdataResponse(){
ocomeni 81:637a87eb8170 182 if(wifiCmd != WIFI_CMD_NONE) return false; // busy
ocomeni 81:637a87eb8170 183 osEvent evt = _aT2WiFiDataQueue->get(0);
ocomeni 81:637a87eb8170 184 if(evt.status == osEventMessage){
ocomeni 81:637a87eb8170 185 data_msg = (wifi_data_msg_t*)evt.value.p;
ocomeni 81:637a87eb8170 186 setNextCommand(data_msg->wifi_cmd);
ocomeni 81:637a87eb8170 187 //_wiFi2ATDatamPool->free(data_msg);
ocomeni 81:637a87eb8170 188 }
ocomeni 81:637a87eb8170 189 return true;
ocomeni 81:637a87eb8170 190 }
ocomeni 81:637a87eb8170 191
ocomeni 81:637a87eb8170 192
ocomeni 79:a2187bbfa407 193 bool WiFiManager::setNextCommand(wifi_cmd_t cmd)
ocomeni 78:07bb86e3ce14 194 {
ocomeni 92:ec9550034276 195 printf("\n [WIFI-MAN] About to set next WiFi manager command to %d\n", cmd);
ocomeni 79:a2187bbfa407 196 if(wifiCmd == WIFI_CMD_NONE){
ocomeni 79:a2187bbfa407 197 wifiCmd = cmd;
ocomeni 79:a2187bbfa407 198 return true; // success
ocomeni 79:a2187bbfa407 199 }
ocomeni 92:ec9550034276 200 printf("\n [WIFI-MAN] Busy : current state = %d \n", wifiCmd);
ocomeni 79:a2187bbfa407 201 return false; // wiFiManager busy
ocomeni 78:07bb86e3ce14 202 }
ocomeni 79:a2187bbfa407 203
ocomeni 81:637a87eb8170 204 const char * WiFiManager::sec2str(nsapi_security_t sec)
ocomeni 81:637a87eb8170 205 {
ocomeni 81:637a87eb8170 206 switch (sec) {
ocomeni 81:637a87eb8170 207 case NSAPI_SECURITY_NONE:
ocomeni 81:637a87eb8170 208 return "None";
ocomeni 81:637a87eb8170 209 case NSAPI_SECURITY_WEP:
ocomeni 81:637a87eb8170 210 return "WEP";
ocomeni 81:637a87eb8170 211 case NSAPI_SECURITY_WPA:
ocomeni 81:637a87eb8170 212 return "WPA";
ocomeni 81:637a87eb8170 213 case NSAPI_SECURITY_WPA2:
ocomeni 81:637a87eb8170 214 return "WPA2";
ocomeni 81:637a87eb8170 215 case NSAPI_SECURITY_WPA_WPA2:
ocomeni 81:637a87eb8170 216 return "WPA/WPA2";
ocomeni 81:637a87eb8170 217 case NSAPI_SECURITY_UNKNOWN:
ocomeni 81:637a87eb8170 218 default:
ocomeni 81:637a87eb8170 219 return "Unknown";
ocomeni 81:637a87eb8170 220 }
ocomeni 81:637a87eb8170 221 }
ocomeni 81:637a87eb8170 222
ocomeni 79:a2187bbfa407 223
ocomeni 79:a2187bbfa407 224 nsapi_size_or_error_t WiFiManager::scanNetworks()
ocomeni 79:a2187bbfa407 225 {
ocomeni 79:a2187bbfa407 226 nsapi_error_t error;
ocomeni 79:a2187bbfa407 227 printf("\n [WIFI-MAN] About to start scan for WiFi networks\n");
ocomeni 79:a2187bbfa407 228 lastScanCount = network->scan(NULL, 0);
ocomeni 79:a2187bbfa407 229 printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n");
ocomeni 79:a2187bbfa407 230 return lastScanCount;
ocomeni 79:a2187bbfa407 231 }
ocomeni 79:a2187bbfa407 232
ocomeni 79:a2187bbfa407 233
ocomeni 81:637a87eb8170 234 //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res,
ocomeni 81:637a87eb8170 235 // nsapi_size_t ncount)
ocomeni 81:637a87eb8170 236 nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount)
ocomeni 79:a2187bbfa407 237 {
ocomeni 81:637a87eb8170 238 WiFiAccessPoint *ap;
ocomeni 81:637a87eb8170 239 nsapi_size_or_error_t count;
ocomeni 81:637a87eb8170 240 count = ncount;
ocomeni 81:637a87eb8170 241 //count = wiFiManager->scanNetworks();
ocomeni 81:637a87eb8170 242 if (count <= 0) {
ocomeni 81:637a87eb8170 243 //_smutex.lock();
ocomeni 81:637a87eb8170 244 printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 81:637a87eb8170 245 //_smutex.unlock();
ocomeni 81:637a87eb8170 246 return;
ocomeni 81:637a87eb8170 247 }
ocomeni 81:637a87eb8170 248 /* Limit number of network arbitrary to 15 */
ocomeni 81:637a87eb8170 249 count = count < 15 ? count : 15;
ocomeni 81:637a87eb8170 250 ap = new WiFiAccessPoint[count];
ocomeni 81:637a87eb8170 251 count = network->scan(ap, count);
ocomeni 81:637a87eb8170 252 if (count <= 0) {
ocomeni 81:637a87eb8170 253 printf("[WIFI-MAN] scan() failed with return value: %d\n", count);
ocomeni 81:637a87eb8170 254 return;
ocomeni 81:637a87eb8170 255 }
ocomeni 81:637a87eb8170 256
ocomeni 81:637a87eb8170 257 for (int i = 0; i < count; i++) {
ocomeni 81:637a87eb8170 258 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 259 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ocomeni 81:637a87eb8170 260 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 261 }
ocomeni 81:637a87eb8170 262 printf("[WIFI-MAN] %d networks available.\n", count);
ocomeni 81:637a87eb8170 263
ocomeni 81:637a87eb8170 264 delete[] ap;
ocomeni 79:a2187bbfa407 265 return count;
ocomeni 79:a2187bbfa407 266 }
ocomeni 79:a2187bbfa407 267
ocomeni 79:a2187bbfa407 268
ocomeni 81:637a87eb8170 269 void WiFiManager::set_WIFI_CONFIG()
ocomeni 81:637a87eb8170 270 {
ocomeni 81:637a87eb8170 271 wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer;
ocomeni 82:10072c1794d3 272 if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid);
ocomeni 82:10072c1794d3 273 if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass);
ocomeni 82:10072c1794d3 274 if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security);
ocomeni 81:637a87eb8170 275 free_DataMsg();
ocomeni 81:637a87eb8170 276 }
ocomeni 81:637a87eb8170 277
ocomeni 78:07bb86e3ce14 278 void WiFiManager::set_WIFI_SSID(char * wifi_ssid)
ocomeni 78:07bb86e3ce14 279 {
ocomeni 78:07bb86e3ce14 280 strcpy(wifi_config.ssid, wifi_ssid);
ocomeni 82:10072c1794d3 281 printf("[WIFI-MAN] wifi_ssid set to %s\n", wifi_config.ssid);
ocomeni 88:7ffa053be662 282 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 283 delete socket;
ocomeni 78:07bb86e3ce14 284 }
ocomeni 79:a2187bbfa407 285
ocomeni 79:a2187bbfa407 286
ocomeni 78:07bb86e3ce14 287 void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass)
ocomeni 78:07bb86e3ce14 288 {
ocomeni 78:07bb86e3ce14 289 strcpy(wifi_config.pass, wifi_pass);
ocomeni 92:ec9550034276 290 printf("[WIFI-MAN] wifi_pass set to %s\n", "****************");
ocomeni 88:7ffa053be662 291 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 292 delete socket;
ocomeni 78:07bb86e3ce14 293 }
ocomeni 79:a2187bbfa407 294
ocomeni 79:a2187bbfa407 295
ocomeni 78:07bb86e3ce14 296 void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security)
ocomeni 78:07bb86e3ce14 297 {
ocomeni 78:07bb86e3ce14 298 wifi_config.security = wifi_security;
ocomeni 82:10072c1794d3 299 printf("[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config.security));
ocomeni 88:7ffa053be662 300 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 301 delete socket;
ocomeni 78:07bb86e3ce14 302 }
ocomeni 79:a2187bbfa407 303
ocomeni 79:a2187bbfa407 304
ocomeni 81:637a87eb8170 305
ocomeni 81:637a87eb8170 306 void WiFiManager::set_internet_config()
ocomeni 81:637a87eb8170 307 {
ocomeni 81:637a87eb8170 308 internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer;
ocomeni 81:637a87eb8170 309 internet_config.peer_id = internet_cfg->peer_id;
ocomeni 84:7c7add00f4bf 310 strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1);
ocomeni 81:637a87eb8170 311 internet_config.connectionScheme = internet_cfg->connectionScheme;
ocomeni 81:637a87eb8170 312 free_DataMsg();
ocomeni 81:637a87eb8170 313 printf("[WIFI MAN] Internet configuration setup completed\n");
ocomeni 81:637a87eb8170 314 printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id,
ocomeni 84:7c7add00f4bf 315 internet_config.url,
ocomeni 81:637a87eb8170 316 internet_config.connectionScheme);
ocomeni 88:7ffa053be662 317 https_connection_active = false; // reset whenever any of the security credentials change
ocomeni 91:d6b6319ad681 318 delete socket;
ocomeni 81:637a87eb8170 319 }
ocomeni 81:637a87eb8170 320
ocomeni 81:637a87eb8170 321 void WiFiManager::free_DataMsg()
ocomeni 81:637a87eb8170 322 {
ocomeni 81:637a87eb8170 323 // free memory after processing
ocomeni 81:637a87eb8170 324 _aT2WiFiDatamPool->free(data_msg);
ocomeni 81:637a87eb8170 325 }
ocomeni 81:637a87eb8170 326
ocomeni 81:637a87eb8170 327
ocomeni 88:7ffa053be662 328
ocomeni 81:637a87eb8170 329 void WiFiManager::status_callback(nsapi_event_t status, intptr_t param)
ocomeni 81:637a87eb8170 330 {
ocomeni 81:637a87eb8170 331 //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
ocomeni 81:637a87eb8170 332 //}
ocomeni 81:637a87eb8170 333 switch(param) {
ocomeni 81:637a87eb8170 334 case NSAPI_STATUS_LOCAL_UP:
ocomeni 81:637a87eb8170 335 printf("[WIFI-MAN] Local IP address set!\r\n");
ocomeni 81:637a87eb8170 336 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 337 break;
ocomeni 81:637a87eb8170 338 case NSAPI_STATUS_GLOBAL_UP:
ocomeni 81:637a87eb8170 339 printf("Global IP address set!\r\n");
ocomeni 81:637a87eb8170 340 printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address());
ocomeni 81:637a87eb8170 341 printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid);
ocomeni 81:637a87eb8170 342 is_connected = true;
ocomeni 81:637a87eb8170 343 break;
ocomeni 81:637a87eb8170 344 case NSAPI_STATUS_DISCONNECTED:
ocomeni 81:637a87eb8170 345 printf("No connection to network!\r\n");
ocomeni 81:637a87eb8170 346 printf("\n [WIFI-MAN] No connection to network!\n");
ocomeni 81:637a87eb8170 347 is_connected = false;
ocomeni 81:637a87eb8170 348 //queueATresponse(AT_DISCONNECT_RESP);
ocomeni 81:637a87eb8170 349 // attempt reconnection if always connected scheme is set
ocomeni 81:637a87eb8170 350 if(internet_config.connectionScheme == ALWAYS_CONNECTED)
ocomeni 81:637a87eb8170 351 {
ocomeni 81:637a87eb8170 352 nsapi_error_t error;
ocomeni 91:d6b6319ad681 353 error = connect();
ocomeni 81:637a87eb8170 354 queueATresponse(WIFI_RECONNECT_INFO);
ocomeni 81:637a87eb8170 355 }
ocomeni 81:637a87eb8170 356 break;
ocomeni 81:637a87eb8170 357 case NSAPI_STATUS_CONNECTING:
ocomeni 81:637a87eb8170 358 printf("Connecting to network!\r\n");
ocomeni 81:637a87eb8170 359 break;
ocomeni 81:637a87eb8170 360 default:
ocomeni 81:637a87eb8170 361 printf("Not supported");
ocomeni 81:637a87eb8170 362 break;
ocomeni 81:637a87eb8170 363 }
ocomeni 81:637a87eb8170 364 }
ocomeni 81:637a87eb8170 365
ocomeni 81:637a87eb8170 366
ocomeni 81:637a87eb8170 367
ocomeni 79:a2187bbfa407 368 // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */
ocomeni 79:a2187bbfa407 369 // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */
ocomeni 79:a2187bbfa407 370 // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */
ocomeni 79:a2187bbfa407 371 // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */
ocomeni 79:a2187bbfa407 372 // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED
ocomeni 79:a2187bbfa407 373
ocomeni 79:a2187bbfa407 374 nsapi_error_t WiFiManager::connect()
ocomeni 79:a2187bbfa407 375 {
ocomeni 79:a2187bbfa407 376 nsapi_error_t error;
ocomeni 81:637a87eb8170 377 printf("\n [WIFI-MAN] About to connect to WiFi network\n");
ocomeni 81:637a87eb8170 378 network->attach(callback(this, &WiFiManager::status_callback));
ocomeni 79:a2187bbfa407 379 error = network->set_blocking(false);
ocomeni 79:a2187bbfa407 380 if(error)
ocomeni 79:a2187bbfa407 381 {
ocomeni 79:a2187bbfa407 382 printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n");
ocomeni 79:a2187bbfa407 383 return error;
ocomeni 79:a2187bbfa407 384 }
ocomeni 93:06e755a80187 385 printf("[WIFI-MAN] Connecting to network ssid = %s passwd = %s security = %s \r\n",
ocomeni 93:06e755a80187 386 wifi_config.ssid,
ocomeni 93:06e755a80187 387 wifi_config.pass,
ocomeni 93:06e755a80187 388 sec2str(wifi_config.security));
ocomeni 79:a2187bbfa407 389 error = network->connect(wifi_config.ssid,
ocomeni 79:a2187bbfa407 390 wifi_config.pass,
ocomeni 79:a2187bbfa407 391 wifi_config.security);
ocomeni 81:637a87eb8170 392 return error;
ocomeni 79:a2187bbfa407 393 }
ocomeni 79:a2187bbfa407 394
ocomeni 79:a2187bbfa407 395
ocomeni 79:a2187bbfa407 396 nsapi_error_t WiFiManager::disconnect()
ocomeni 78:07bb86e3ce14 397 {
ocomeni 79:a2187bbfa407 398 nsapi_error_t error;
ocomeni 79:a2187bbfa407 399 error = network->disconnect();
ocomeni 79:a2187bbfa407 400 return error;
ocomeni 78:07bb86e3ce14 401 }
ocomeni 79:a2187bbfa407 402
ocomeni 88:7ffa053be662 403 void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen)
ocomeni 90:ed0267eca7b5 404 {
ocomeni 90:ed0267eca7b5 405
ocomeni 88:7ffa053be662 406 at_data_resp = new at_data_msg_t;
ocomeni 88:7ffa053be662 407 at_data_resp->at_resp = at_cmd;
ocomeni 88:7ffa053be662 408 size_t bufSize = sizeof(at_data_resp->buffer);
ocomeni 88:7ffa053be662 409 int pos = 0;
ocomeni 88:7ffa053be662 410 at_data_resp->dataLen = 0;
ocomeni 88:7ffa053be662 411 bool queueResult = true;
ocomeni 90:ed0267eca7b5 412 int hdrLen = 0;
ocomeni 94:fb4414aff957 413 int wait_count = 0;
ocomeni 88:7ffa053be662 414 do {
ocomeni 94:fb4414aff957 415 if(!queueResult){
ocomeni 94:fb4414aff957 416 wait_count++;
ocomeni 94:fb4414aff957 417 printf("[WIFI-MAN] ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 94:fb4414aff957 418 wait_ms(10);
ocomeni 90:ed0267eca7b5 419 }
ocomeni 90:ed0267eca7b5 420 else {
ocomeni 90:ed0267eca7b5 421 if(http_response_hdr_sent == false){
ocomeni 90:ed0267eca7b5 422 copyResponseHdr2Queue();
ocomeni 94:fb4414aff957 423 printf("[WIFI-MAN] Http Response header copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
ocomeni 90:ed0267eca7b5 424 hdrLen = at_data_resp->dataLen;
ocomeni 90:ed0267eca7b5 425 http_response_hdr_sent = true;
ocomeni 90:ed0267eca7b5 426 }
ocomeni 94:fb4414aff957 427 int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos) ;
ocomeni 94:fb4414aff957 428 printf("[WIFI-MAN] Http Response body [bytes = %d] \r\n",cpyLen);
ocomeni 90:ed0267eca7b5 429 at_data_resp->dataLen += cpyLen;
ocomeni 90:ed0267eca7b5 430 memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen);
ocomeni 94:fb4414aff957 431 printf("[WIFI-MAN] Http Response header and body copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen);
ocomeni 90:ed0267eca7b5 432 }
ocomeni 88:7ffa053be662 433 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 90:ed0267eca7b5 434 if(queueResult){
ocomeni 90:ed0267eca7b5 435 pos+= at_data_resp->dataLen;
ocomeni 90:ed0267eca7b5 436 at_data_resp->dataLen = 0;
ocomeni 90:ed0267eca7b5 437 hdrLen = 0;
ocomeni 90:ed0267eca7b5 438 }
ocomeni 88:7ffa053be662 439 }while(queueResult == false || pos < bufLen);
ocomeni 94:fb4414aff957 440 printf("[WIFI-MAN] response data queued - deleting data memory\r\n");
ocomeni 88:7ffa053be662 441 delete at_data_resp;
ocomeni 88:7ffa053be662 442 }
ocomeni 79:a2187bbfa407 443
ocomeni 90:ed0267eca7b5 444 void WiFiManager::copyResponseHdr2Queue()
ocomeni 90:ed0267eca7b5 445 {
ocomeni 90:ed0267eca7b5 446 int numChars = 0;
ocomeni 90:ed0267eca7b5 447 // create message pointer for response header generation
ocomeni 90:ed0267eca7b5 448 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 90:ed0267eca7b5 449 // do status line
ocomeni 90:ed0267eca7b5 450 numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(),
ocomeni 90:ed0267eca7b5 451 http_response->get_status_message().c_str());
ocomeni 90:ed0267eca7b5 452 msgPtr += numChars;
ocomeni 90:ed0267eca7b5 453 for (size_t ix = 0; ix < http_response->get_headers_length(); ix++) {
ocomeni 90:ed0267eca7b5 454 numChars = sprintf(msgPtr, "%s: %s\r\n",
ocomeni 90:ed0267eca7b5 455 http_response->get_headers_fields()[ix]->c_str(),
ocomeni 90:ed0267eca7b5 456 http_response->get_headers_values()[ix]->c_str());
ocomeni 90:ed0267eca7b5 457 msgPtr += numChars;
ocomeni 90:ed0267eca7b5 458 }
ocomeni 90:ed0267eca7b5 459 numChars = sprintf(msgPtr, "\r\n");
ocomeni 90:ed0267eca7b5 460 msgPtr += numChars;
ocomeni 90:ed0267eca7b5 461 // print out generated header
ocomeni 90:ed0267eca7b5 462 printf("[WiFi MAN] generated response header:\n");
ocomeni 90:ed0267eca7b5 463 printf("%s\r\n", (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 464 // calculate header length
ocomeni 90:ed0267eca7b5 465 at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 466 }
ocomeni 90:ed0267eca7b5 467
ocomeni 84:7c7add00f4bf 468 void WiFiManager::return_response(HttpResponse* res) {
ocomeni 84:7c7add00f4bf 469
ocomeni 88:7ffa053be662 470 at_data_resp = new at_data_msg_t;
ocomeni 87:99b37d26ff2a 471 int numChars = 0;
ocomeni 87:99b37d26ff2a 472 // create message pointer for response header generation
ocomeni 88:7ffa053be662 473 char * msgPtr = (char *)at_data_resp->buffer;
ocomeni 87:99b37d26ff2a 474 // do status line
ocomeni 87:99b37d26ff2a 475 numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str());
ocomeni 87:99b37d26ff2a 476 msgPtr += numChars;
ocomeni 84:7c7add00f4bf 477 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
ocomeni 87:99b37d26ff2a 478 numChars = sprintf(msgPtr, "%s: %s\r\n",
ocomeni 87:99b37d26ff2a 479 res->get_headers_fields()[ix]->c_str(),
ocomeni 87:99b37d26ff2a 480 res->get_headers_values()[ix]->c_str());
ocomeni 87:99b37d26ff2a 481 msgPtr += numChars;
ocomeni 84:7c7add00f4bf 482 }
ocomeni 87:99b37d26ff2a 483 numChars = sprintf(msgPtr, "\r\n\r\n");
ocomeni 87:99b37d26ff2a 484 msgPtr += numChars;
ocomeni 87:99b37d26ff2a 485 // print out generated header
ocomeni 90:ed0267eca7b5 486 printf("[WiFi MAN] generated response header:\n");
ocomeni 88:7ffa053be662 487 printf("%s\r\n", (char *)at_data_resp->buffer);
ocomeni 90:ed0267eca7b5 488 // calculate header length
ocomeni 89:45f6db09a76d 489 at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer);
ocomeni 84:7c7add00f4bf 490
ocomeni 90:ed0267eca7b5 491 // package and send on wifi data queue
ocomeni 88:7ffa053be662 492 at_data_resp->at_resp = AT_HTTPS_RESP;
ocomeni 89:45f6db09a76d 493 bool queueResult = true;
ocomeni 89:45f6db09a76d 494 int wait_count = 0;
ocomeni 89:45f6db09a76d 495 do
ocomeni 89:45f6db09a76d 496 {
ocomeni 89:45f6db09a76d 497 if(!queueResult){
ocomeni 89:45f6db09a76d 498 wait_count++;
ocomeni 89:45f6db09a76d 499 printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10);
ocomeni 89:45f6db09a76d 500 wait_ms(10);
ocomeni 89:45f6db09a76d 501 }
ocomeni 89:45f6db09a76d 502 queueResult = queueWiFiDataResponse(*at_data_resp);
ocomeni 89:45f6db09a76d 503 }while(queueResult == false);
ocomeni 88:7ffa053be662 504 delete at_data_resp;
ocomeni 84:7c7add00f4bf 505 }
ocomeni 90:ed0267eca7b5 506
ocomeni 90:ed0267eca7b5 507
ocomeni 90:ed0267eca7b5 508 void WiFiManager::printBufferInHex(uint8_t *buf, int pLen)
ocomeni 90:ed0267eca7b5 509 {
ocomeni 90:ed0267eca7b5 510 for(int i =0;i<pLen;i++){
ocomeni 90:ed0267eca7b5 511 if(i%8==0) printf("\n[%3d]",i);
ocomeni 90:ed0267eca7b5 512 printf("%02x ", buf[i]);
ocomeni 90:ed0267eca7b5 513 }
ocomeni 90:ed0267eca7b5 514 printf("\n");
ocomeni 90:ed0267eca7b5 515 }
ocomeni 90:ed0267eca7b5 516
ocomeni 88:7ffa053be662 517 //#define TRY_PRINTF
ocomeni 84:7c7add00f4bf 518
ocomeni 90:ed0267eca7b5 519 void WiFiManager::body_callback(const char *at, uint32_t length) {
ocomeni 84:7c7add00f4bf 520 printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length);
ocomeni 84:7c7add00f4bf 521 chunkNum++;
ocomeni 89:45f6db09a76d 522 sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length);
ocomeni 84:7c7add00f4bf 523 }
ocomeni 84:7c7add00f4bf 524
ocomeni 88:7ffa053be662 525
ocomeni 88:7ffa053be662 526 bool WiFiManager::createTLSconnection(const char * hostName)
ocomeni 88:7ffa053be662 527 {
ocomeni 93:06e755a80187 528 #ifdef ENABLE_MBED_TRACE
ocomeni 93:06e755a80187 529 mbed_trace_init();
ocomeni 93:06e755a80187 530 #endif
ocomeni 88:7ffa053be662 531 socket = new TLSSocket();
ocomeni 88:7ffa053be662 532
ocomeni 88:7ffa053be662 533 nsapi_error_t r;
ocomeni 88:7ffa053be662 534 // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 535 r = socket->open(network);
ocomeni 88:7ffa053be662 536 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 537 {
ocomeni 89:45f6db09a76d 538 printf("TLS open failed!!\n");
ocomeni 88:7ffa053be662 539 return false;
ocomeni 88:7ffa053be662 540 }
ocomeni 88:7ffa053be662 541 printf("TLS open passed!!\n");
ocomeni 88:7ffa053be662 542 r = socket->set_root_ca_cert(SSL_CA_PEM);
ocomeni 88:7ffa053be662 543 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 544 {
ocomeni 88:7ffa053be662 545 printf("TLS set_root_ca_cert failed!!\n");
ocomeni 88:7ffa053be662 546 return false;
ocomeni 88:7ffa053be662 547 }
ocomeni 88:7ffa053be662 548 printf("TLS set_root_ca_cert passed!!\n");
ocomeni 88:7ffa053be662 549 r = socket->connect(hostName, 443);
ocomeni 88:7ffa053be662 550 if(r != NSAPI_ERROR_OK)
ocomeni 88:7ffa053be662 551 {
ocomeni 93:06e755a80187 552 char errstr[100];
ocomeni 93:06e755a80187 553 mbedtls_strerror(r, errstr, 100);
ocomeni 93:06e755a80187 554 printf("TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr);
ocomeni 93:06e755a80187 555 socket->close();
ocomeni 88:7ffa053be662 556 return false;
ocomeni 88:7ffa053be662 557 }
ocomeni 88:7ffa053be662 558 printf("TLS connection successful for https site : %s\n", hostName);
ocomeni 88:7ffa053be662 559 return true;
ocomeni 88:7ffa053be662 560 }
ocomeni 88:7ffa053be662 561 #define TESTING_HTTPS
ocomeni 84:7c7add00f4bf 562 void WiFiManager::createHttpsRequest()
ocomeni 79:a2187bbfa407 563 {
ocomeni 87:99b37d26ff2a 564 // reset chunk #;
ocomeni 87:99b37d26ff2a 565 chunkNum = 0;
ocomeni 90:ed0267eca7b5 566 http_response_hdr_sent = false;
ocomeni 89:45f6db09a76d 567 printf("\n[WIFI MAN] Http Request received:\n");
ocomeni 84:7c7add00f4bf 568 http_req_cfg = (http_request_t *) data_msg->buffer;
ocomeni 90:ed0267eca7b5 569 printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI);
ocomeni 89:45f6db09a76d 570 printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url);
ocomeni 87:99b37d26ff2a 571 char full_url[100];
ocomeni 88:7ffa053be662 572 char host[60] ;
ocomeni 87:99b37d26ff2a 573 strncpy(full_url,internet_config.url, strlen(internet_config.url)+1);
ocomeni 90:ed0267eca7b5 574 strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1);
ocomeni 90:ed0267eca7b5 575 strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1);
ocomeni 87:99b37d26ff2a 576 printf("\n[WIFI MAN] server url+uri = %s\n", full_url);
ocomeni 90:ed0267eca7b5 577 printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName);
ocomeni 90:ed0267eca7b5 578 printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal);
ocomeni 90:ed0267eca7b5 579 printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType);
ocomeni 90:ed0267eca7b5 580 printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen);
ocomeni 84:7c7add00f4bf 581
ocomeni 84:7c7add00f4bf 582 int bodyLen;
ocomeni 90:ed0267eca7b5 583 sscanf(http_req_cfg->contentLen, "%d", &bodyLen);
ocomeni 90:ed0267eca7b5 584 printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen);
ocomeni 90:ed0267eca7b5 585
ocomeni 90:ed0267eca7b5 586 if(bodyLen > 10){
ocomeni 93:06e755a80187 587 printf("\n [WIFI MAN] Message Body:\n");
ocomeni 90:ed0267eca7b5 588 printBufferInHex(http_req_cfg->body, bodyLen);
ocomeni 90:ed0267eca7b5 589 }
ocomeni 87:99b37d26ff2a 590 if(strstr(internet_config.url, "http:")!=NULL) // http request
ocomeni 87:99b37d26ff2a 591 {
ocomeni 87:99b37d26ff2a 592 http_request = new HttpRequest(network,
ocomeni 90:ed0267eca7b5 593 http_req_cfg->method,
ocomeni 90:ed0267eca7b5 594 full_url,
ocomeni 90:ed0267eca7b5 595 callback(this, &WiFiManager::body_callback));
ocomeni 87:99b37d26ff2a 596 setHttpHeader("Host", http_req_cfg->hostName);
ocomeni 87:99b37d26ff2a 597 setHttpHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 89:45f6db09a76d 598 printf("http_req_cfg->method = %d\n", http_req_cfg->method);
ocomeni 88:7ffa053be662 599 if(http_req_cfg->method == HTTP_GET){
ocomeni 88:7ffa053be662 600 printf("HTTP_GET -- ignoring body\n");
ocomeni 88:7ffa053be662 601 //setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 602 //setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 603 http_response = http_request->send(NULL, 0);
ocomeni 88:7ffa053be662 604 }
ocomeni 88:7ffa053be662 605 else{
ocomeni 88:7ffa053be662 606 setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 607 setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 608 http_response = http_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 609 }
ocomeni 87:99b37d26ff2a 610 free_DataMsg();
ocomeni 87:99b37d26ff2a 611 if (!http_response) {
ocomeni 87:99b37d26ff2a 612 char buf[100];
ocomeni 87:99b37d26ff2a 613 mbedtls_strerror(http_request->get_error(), buf, 100);
ocomeni 87:99b37d26ff2a 614 printf("HttpRequest failed (error code %s)\n", buf);
ocomeni 87:99b37d26ff2a 615 //printf("HttpsRequest failed (error code %d)\n", https_request->get_error());
ocomeni 87:99b37d26ff2a 616 delete http_request; // free the memory
ocomeni 87:99b37d26ff2a 617 return;
ocomeni 87:99b37d26ff2a 618 }
ocomeni 88:7ffa053be662 619 delete http_request; // free the memory
ocomeni 87:99b37d26ff2a 620 printf("\n----- HTTP POST response -----\n");
ocomeni 84:7c7add00f4bf 621 }
ocomeni 87:99b37d26ff2a 622 else
ocomeni 87:99b37d26ff2a 623 {
ocomeni 90:ed0267eca7b5 624 #ifndef DONT_USE_TLS_SOCKET
ocomeni 88:7ffa053be662 625 if(https_connection_active == false){
ocomeni 88:7ffa053be662 626 bool tlsResult;
ocomeni 88:7ffa053be662 627 tlsResult = createTLSconnection(host);
ocomeni 93:06e755a80187 628 #ifdef ENABLE_MBED_TRACE
ocomeni 93:06e755a80187 629 mbed_trace_free(); // free trace memory
ocomeni 93:06e755a80187 630 #endif
ocomeni 88:7ffa053be662 631 if(tlsResult == false){
ocomeni 88:7ffa053be662 632 delete socket;
ocomeni 88:7ffa053be662 633 free_DataMsg();
ocomeni 88:7ffa053be662 634 return;
ocomeni 88:7ffa053be662 635 }
ocomeni 88:7ffa053be662 636 printf("[create https] TLS connection successful for https site : %s\n", host);
ocomeni 88:7ffa053be662 637 }
ocomeni 90:ed0267eca7b5 638 printf("after call to createTLSconnection \n");
ocomeni 90:ed0267eca7b5 639 print_memory_info();
ocomeni 88:7ffa053be662 640 // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument
ocomeni 88:7ffa053be662 641 //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418");
ocomeni 88:7ffa053be662 642 //_wmutex.lock();
ocomeni 88:7ffa053be662 643 https_request = new HttpsRequest(socket,
ocomeni 87:99b37d26ff2a 644 http_req_cfg->method,
ocomeni 88:7ffa053be662 645 full_url,
ocomeni 87:99b37d26ff2a 646 callback(this, &WiFiManager::body_callback));
ocomeni 90:ed0267eca7b5 647 #else
ocomeni 90:ed0267eca7b5 648 https_request = new HttpsRequest(network,
ocomeni 90:ed0267eca7b5 649 SSL_CA_PEM,
ocomeni 90:ed0267eca7b5 650 http_req_cfg->method,
ocomeni 90:ed0267eca7b5 651 full_url,
ocomeni 90:ed0267eca7b5 652 callback(this, &WiFiManager::body_callback));
ocomeni 90:ed0267eca7b5 653 #endif
ocomeni 88:7ffa053be662 654 #ifdef TESTING_HTTPS
ocomeni 90:ed0267eca7b5 655 printf("http_req_cfg->method = %d\n", http_req_cfg->method);
ocomeni 88:7ffa053be662 656 if(http_req_cfg->method == HTTP_GET){
ocomeni 88:7ffa053be662 657 printf("HTTP_GET -- ignoring body\n");
ocomeni 89:45f6db09a76d 658 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 659 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 88:7ffa053be662 660 //setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 661 //setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 662 http_response = https_request->send(NULL, 0);
ocomeni 88:7ffa053be662 663 }
ocomeni 88:7ffa053be662 664 else{
ocomeni 89:45f6db09a76d 665 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 666 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 90:ed0267eca7b5 667 setHttpsHeader("Content-Type", http_req_cfg->contentType);
ocomeni 90:ed0267eca7b5 668 setHttpsHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 88:7ffa053be662 669 http_response = https_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 670 }
ocomeni 88:7ffa053be662 671 #else
ocomeni 89:45f6db09a76d 672 setHttpsHeader("Host", http_req_cfg->hostName);
ocomeni 89:45f6db09a76d 673 setHttpsHeader("Accept", http_req_cfg->AcceptVal);
ocomeni 88:7ffa053be662 674 setHttpHeader("Content-Type", http_req_cfg->contentType);
ocomeni 88:7ffa053be662 675 setHttpHeader("Content-Length", http_req_cfg->contentLen);
ocomeni 87:99b37d26ff2a 676 http_response = https_request->send(http_req_cfg->body, bodyLen);
ocomeni 88:7ffa053be662 677 #endif
ocomeni 88:7ffa053be662 678
ocomeni 88:7ffa053be662 679 //_wmutex.unlock();
ocomeni 90:ed0267eca7b5 680 //free_DataMsg();
ocomeni 87:99b37d26ff2a 681 if (!http_response) {
ocomeni 87:99b37d26ff2a 682 char buf[100];
ocomeni 87:99b37d26ff2a 683 mbedtls_strerror(https_request->get_error(), buf, 100);
ocomeni 88:7ffa053be662 684 printf("HttpsRequest failed (error code %s)\n", buf);
ocomeni 87:99b37d26ff2a 685 delete https_request; // free the memory
ocomeni 88:7ffa053be662 686 https_connection_active = false; // reset true whenever connection fails
ocomeni 88:7ffa053be662 687 delete socket;
ocomeni 90:ed0267eca7b5 688 free_DataMsg();
ocomeni 87:99b37d26ff2a 689 return;
ocomeni 87:99b37d26ff2a 690 }
ocomeni 88:7ffa053be662 691 https_connection_active = true; // set true whenever connection succeeds
ocomeni 87:99b37d26ff2a 692 printf("\n----- HTTPS POST response -----\r\n");
ocomeni 87:99b37d26ff2a 693 }
ocomeni 90:ed0267eca7b5 694 if(http_response != NULL){
ocomeni 90:ed0267eca7b5 695 //return_response(http_response);
ocomeni 90:ed0267eca7b5 696 delete http_response; // free the response memory
ocomeni 90:ed0267eca7b5 697 }
ocomeni 90:ed0267eca7b5 698 free_DataMsg();
ocomeni 90:ed0267eca7b5 699 //delete https_request; // free the request memory
ocomeni 79:a2187bbfa407 700 }
ocomeni 79:a2187bbfa407 701
ocomeni 79:a2187bbfa407 702 void WiFiManager::createHttpRequest(http_method method,
ocomeni 79:a2187bbfa407 703 const char* url,
ocomeni 79:a2187bbfa407 704 Callback<void(const char *at, uint32_t length)> body_callback)
ocomeni 79:a2187bbfa407 705 {
ocomeni 84:7c7add00f4bf 706 http_request = new HttpRequest(network,
ocomeni 84:7c7add00f4bf 707 method, url, body_callback);;
ocomeni 79:a2187bbfa407 708 }
ocomeni 79:a2187bbfa407 709
ocomeni 79:a2187bbfa407 710 void WiFiManager::setHttpHeader(string key, string value)
ocomeni 79:a2187bbfa407 711 {
ocomeni 79:a2187bbfa407 712 http_request->set_header(key, value);
ocomeni 79:a2187bbfa407 713 }
ocomeni 79:a2187bbfa407 714
ocomeni 79:a2187bbfa407 715 void WiFiManager::setHttpsHeader(string key, string value)
ocomeni 79:a2187bbfa407 716 {
ocomeni 79:a2187bbfa407 717 https_request->set_header(key, value);
ocomeni 79:a2187bbfa407 718 }
ocomeni 79:a2187bbfa407 719
ocomeni 79:a2187bbfa407 720 void WiFiManager::sendHttpsRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 721 {
ocomeni 78:07bb86e3ce14 722 }
ocomeni 79:a2187bbfa407 723
ocomeni 79:a2187bbfa407 724 void WiFiManager::sendHttpRequest(const char * body, int bodyLen)
ocomeni 78:07bb86e3ce14 725 {
ocomeni 78:07bb86e3ce14 726 }
ocomeni 79:a2187bbfa407 727