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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sat Apr 13 14:17:29 2019 +0000
Revision:
92:ec9550034276
Parent:
91:d6b6319ad681
Child:
93:06e755a80187
implemented DEFINE switch to resolve mbed memory pool allocation failure

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