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

Dependencies:   mbed-http

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

Who changed what in which revision?

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