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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Sun Apr 07 17:31:56 2019 +0000
Revision:
91:d6b6319ad681
Parent:
90:ed0267eca7b5
Child:
92:ec9550034276
added method to handle sending expected command response strings

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