Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.cpp@89:45f6db09a76d, 2019-04-01 (annotated)
- Committer:
- ocomeni
- Date:
- Mon Apr 01 08:00:41 2019 +0000
- Revision:
- 89:45f6db09a76d
- Parent:
- 88:7ffa053be662
- Child:
- 90:ed0267eca7b5
download working ok. still need to fix memory leak.
Who changed what in which revision?
User | Revision | Line number | New 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 | 89:45f6db09a76d | 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 | 89:45f6db09a76d | 394 | at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer); |
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 | 89:45f6db09a76d | 401 | bool queueResult = true; |
ocomeni | 89:45f6db09a76d | 402 | int wait_count = 0; |
ocomeni | 89:45f6db09a76d | 403 | do |
ocomeni | 89:45f6db09a76d | 404 | { |
ocomeni | 89:45f6db09a76d | 405 | if(!queueResult){ |
ocomeni | 89:45f6db09a76d | 406 | wait_count++; |
ocomeni | 89:45f6db09a76d | 407 | printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 89:45f6db09a76d | 408 | wait_ms(10); |
ocomeni | 89:45f6db09a76d | 409 | } |
ocomeni | 89:45f6db09a76d | 410 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 89:45f6db09a76d | 411 | }while(queueResult == false); |
ocomeni | 88:7ffa053be662 | 412 | delete at_data_resp; |
ocomeni | 84:7c7add00f4bf | 413 | } |
ocomeni | 88:7ffa053be662 | 414 | //#define TRY_PRINTF |
ocomeni | 84:7c7add00f4bf | 415 | |
ocomeni | 84:7c7add00f4bf | 416 | void WiFiManager::body_callback(const char *at, uint32_t length) { |
ocomeni | 84:7c7add00f4bf | 417 | |
ocomeni | 84:7c7add00f4bf | 418 | printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); |
ocomeni | 84:7c7add00f4bf | 419 | //device->printf("\n Try Print Header as string:\n\n "); |
ocomeni | 84:7c7add00f4bf | 420 | //device->printf("recv %d [%.*s]\n", length, strstr((char *)at, "\r\n")-(char *)at, (char *)at); |
ocomeni | 84:7c7add00f4bf | 421 | //if(false) |
ocomeni | 84:7c7add00f4bf | 422 | //if(chunkNum < 2) |
ocomeni | 84:7c7add00f4bf | 423 | //for(int i=0; i < length; i++){ |
ocomeni | 84:7c7add00f4bf | 424 | // |
ocomeni | 84:7c7add00f4bf | 425 | //putc((uint8_t)at[i]); |
ocomeni | 84:7c7add00f4bf | 426 | //int resp = write( (const uint8_t *)at, (int) length, &completed, SERIAL_EVENT_TX_COMPLETE); |
ocomeni | 84:7c7add00f4bf | 427 | //} |
ocomeni | 84:7c7add00f4bf | 428 | //if(false) |
ocomeni | 88:7ffa053be662 | 429 | #ifdef DUMP_BODY_BYTES |
ocomeni | 84:7c7add00f4bf | 430 | if(chunkNum < 2) |
ocomeni | 84:7c7add00f4bf | 431 | for (size_t ix = 0; ix < length; ix++) { |
ocomeni | 84:7c7add00f4bf | 432 | printf("%02X: ", (uint8_t)at[ix]); |
ocomeni | 84:7c7add00f4bf | 433 | if((ix % 32) == 0 and ix) |
ocomeni | 84:7c7add00f4bf | 434 | printf("\n"); |
ocomeni | 84:7c7add00f4bf | 435 | } |
ocomeni | 88:7ffa053be662 | 436 | #endif |
ocomeni | 87:99b37d26ff2a | 437 | #ifdef TRY_PRINTF |
ocomeni | 87:99b37d26ff2a | 438 | printf("%s\n", at); |
ocomeni | 87:99b37d26ff2a | 439 | #endif |
ocomeni | 87:99b37d26ff2a | 440 | |
ocomeni | 84:7c7add00f4bf | 441 | printf("\n\n"); |
ocomeni | 84:7c7add00f4bf | 442 | chunkNum++; |
ocomeni | 89:45f6db09a76d | 443 | sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length); |
ocomeni | 84:7c7add00f4bf | 444 | } |
ocomeni | 84:7c7add00f4bf | 445 | |
ocomeni | 88:7ffa053be662 | 446 | |
ocomeni | 88:7ffa053be662 | 447 | bool WiFiManager::createTLSconnection(const char * hostName) |
ocomeni | 88:7ffa053be662 | 448 | { |
ocomeni | 88:7ffa053be662 | 449 | socket = new TLSSocket(); |
ocomeni | 88:7ffa053be662 | 450 | printf("\n\nafter TLS socket creation\n"); |
ocomeni | 88:7ffa053be662 | 451 | print_memory_info(); |
ocomeni | 88:7ffa053be662 | 452 | //printf("\n[WIFI MAN] TLS Host = %s\n", hostName); |
ocomeni | 88:7ffa053be662 | 453 | |
ocomeni | 88:7ffa053be662 | 454 | nsapi_error_t r; |
ocomeni | 88:7ffa053be662 | 455 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 456 | r = socket->open(network); |
ocomeni | 88:7ffa053be662 | 457 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 458 | { |
ocomeni | 89:45f6db09a76d | 459 | printf("TLS open failed!!\n"); |
ocomeni | 88:7ffa053be662 | 460 | return false; |
ocomeni | 88:7ffa053be662 | 461 | } |
ocomeni | 88:7ffa053be662 | 462 | printf("TLS open passed!!\n"); |
ocomeni | 88:7ffa053be662 | 463 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
ocomeni | 88:7ffa053be662 | 464 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 465 | { |
ocomeni | 88:7ffa053be662 | 466 | printf("TLS set_root_ca_cert failed!!\n"); |
ocomeni | 88:7ffa053be662 | 467 | return false; |
ocomeni | 88:7ffa053be662 | 468 | } |
ocomeni | 88:7ffa053be662 | 469 | printf("TLS set_root_ca_cert passed!!\n"); |
ocomeni | 88:7ffa053be662 | 470 | r = socket->connect(hostName, 443); |
ocomeni | 88:7ffa053be662 | 471 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 472 | { |
ocomeni | 88:7ffa053be662 | 473 | printf("TLS connect failed for hostname %s!!\n", hostName); |
ocomeni | 88:7ffa053be662 | 474 | return false; |
ocomeni | 88:7ffa053be662 | 475 | } |
ocomeni | 88:7ffa053be662 | 476 | printf("TLS connection successful for https site : %s\n", hostName); |
ocomeni | 88:7ffa053be662 | 477 | return true; |
ocomeni | 88:7ffa053be662 | 478 | } |
ocomeni | 88:7ffa053be662 | 479 | #define TESTING_HTTPS |
ocomeni | 84:7c7add00f4bf | 480 | void WiFiManager::createHttpsRequest() |
ocomeni | 79:a2187bbfa407 | 481 | { |
ocomeni | 87:99b37d26ff2a | 482 | // reset chunk #; |
ocomeni | 87:99b37d26ff2a | 483 | chunkNum = 0; |
ocomeni | 89:45f6db09a76d | 484 | printf("\n[WIFI MAN] Http Request received:\n"); |
ocomeni | 84:7c7add00f4bf | 485 | http_req_cfg = (http_request_t *) data_msg->buffer; |
ocomeni | 89:45f6db09a76d | 486 | printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI.c_str()); |
ocomeni | 89:45f6db09a76d | 487 | printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url); |
ocomeni | 87:99b37d26ff2a | 488 | char full_url[100]; |
ocomeni | 88:7ffa053be662 | 489 | char host[60] ; |
ocomeni | 87:99b37d26ff2a | 490 | strncpy(full_url,internet_config.url, strlen(internet_config.url)+1); |
ocomeni | 88:7ffa053be662 | 491 | strncpy(host,http_req_cfg->hostName.c_str(), http_req_cfg->hostName.size()); |
ocomeni | 87:99b37d26ff2a | 492 | printf("\n[WIFI MAN] server url = %s\n", full_url); |
ocomeni | 87:99b37d26ff2a | 493 | //strncat(internet_config.url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size()); |
ocomeni | 87:99b37d26ff2a | 494 | strncat(full_url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size()); |
ocomeni | 87:99b37d26ff2a | 495 | //printf("\n[WIFI MAN] server url = %s\n", internet_config.url); |
ocomeni | 87:99b37d26ff2a | 496 | printf("\n[WIFI MAN] server url+uri = %s\n", full_url); |
ocomeni | 87:99b37d26ff2a | 497 | printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName.c_str()); |
ocomeni | 87:99b37d26ff2a | 498 | printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal.c_str()); |
ocomeni | 87:99b37d26ff2a | 499 | printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType.c_str()); |
ocomeni | 86:04fc2fcda7ec | 500 | printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen.c_str()); |
ocomeni | 84:7c7add00f4bf | 501 | //printf("\n[WIFI MAN] server url = %s\n", internet_config.url.c_str()); |
ocomeni | 84:7c7add00f4bf | 502 | //const char* url = internet_config.url; |
ocomeni | 84:7c7add00f4bf | 503 | |
ocomeni | 84:7c7add00f4bf | 504 | int bodyLen; |
ocomeni | 84:7c7add00f4bf | 505 | sscanf(http_req_cfg->contentLen.c_str(), "%d", &bodyLen); |
ocomeni | 86:04fc2fcda7ec | 506 | printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen.c_str(), bodyLen); |
ocomeni | 87:99b37d26ff2a | 507 | if(strstr(internet_config.url, "http:")!=NULL) // http request |
ocomeni | 87:99b37d26ff2a | 508 | { |
ocomeni | 87:99b37d26ff2a | 509 | http_request = new HttpRequest(network, |
ocomeni | 87:99b37d26ff2a | 510 | //SSL_CA_PEM, |
ocomeni | 87:99b37d26ff2a | 511 | http_req_cfg->method, |
ocomeni | 88:7ffa053be662 | 512 | full_url, |
ocomeni | 87:99b37d26ff2a | 513 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 87:99b37d26ff2a | 514 | setHttpHeader("Host", http_req_cfg->hostName); |
ocomeni | 87:99b37d26ff2a | 515 | setHttpHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 89:45f6db09a76d | 516 | printf("http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 517 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 88:7ffa053be662 | 518 | printf("HTTP_GET -- ignoring body\n"); |
ocomeni | 88:7ffa053be662 | 519 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 520 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 521 | http_response = http_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 522 | } |
ocomeni | 88:7ffa053be662 | 523 | else{ |
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(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 527 | } |
ocomeni | 87:99b37d26ff2a | 528 | free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 529 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 530 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 531 | mbedtls_strerror(http_request->get_error(), buf, 100); |
ocomeni | 87:99b37d26ff2a | 532 | printf("HttpRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 533 | //printf("HttpsRequest failed (error code %d)\n", https_request->get_error()); |
ocomeni | 87:99b37d26ff2a | 534 | delete http_request; // free the memory |
ocomeni | 87:99b37d26ff2a | 535 | return; |
ocomeni | 87:99b37d26ff2a | 536 | } |
ocomeni | 88:7ffa053be662 | 537 | delete http_request; // free the memory |
ocomeni | 87:99b37d26ff2a | 538 | printf("\n----- HTTP POST response -----\n"); |
ocomeni | 84:7c7add00f4bf | 539 | } |
ocomeni | 87:99b37d26ff2a | 540 | else |
ocomeni | 87:99b37d26ff2a | 541 | { |
ocomeni | 88:7ffa053be662 | 542 | if(https_connection_active == false){ |
ocomeni | 88:7ffa053be662 | 543 | bool tlsResult; |
ocomeni | 88:7ffa053be662 | 544 | tlsResult = createTLSconnection(host); |
ocomeni | 88:7ffa053be662 | 545 | if(tlsResult == false){ |
ocomeni | 88:7ffa053be662 | 546 | delete socket; |
ocomeni | 88:7ffa053be662 | 547 | free_DataMsg(); |
ocomeni | 88:7ffa053be662 | 548 | return; |
ocomeni | 88:7ffa053be662 | 549 | } |
ocomeni | 88:7ffa053be662 | 550 | printf("[create https] TLS connection successful for https site : %s\n", host); |
ocomeni | 88:7ffa053be662 | 551 | } |
ocomeni | 88:7ffa053be662 | 552 | // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument |
ocomeni | 88:7ffa053be662 | 553 | //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); |
ocomeni | 88:7ffa053be662 | 554 | //_wmutex.lock(); |
ocomeni | 88:7ffa053be662 | 555 | https_request = new HttpsRequest(socket, |
ocomeni | 87:99b37d26ff2a | 556 | http_req_cfg->method, |
ocomeni | 88:7ffa053be662 | 557 | full_url, |
ocomeni | 87:99b37d26ff2a | 558 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 87:99b37d26ff2a | 559 | |
ocomeni | 88:7ffa053be662 | 560 | #ifdef TESTING_HTTPS |
ocomeni | 88:7ffa053be662 | 561 | printf("http_req_cfg->method = %d\n"); |
ocomeni | 88:7ffa053be662 | 562 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 88:7ffa053be662 | 563 | printf("HTTP_GET -- ignoring body\n"); |
ocomeni | 89:45f6db09a76d | 564 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 565 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 566 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 567 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 568 | http_response = https_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 569 | } |
ocomeni | 88:7ffa053be662 | 570 | else{ |
ocomeni | 89:45f6db09a76d | 571 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 572 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
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 = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 576 | } |
ocomeni | 88:7ffa053be662 | 577 | #else |
ocomeni | 89:45f6db09a76d | 578 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 579 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 580 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 581 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 87:99b37d26ff2a | 582 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 583 | #endif |
ocomeni | 88:7ffa053be662 | 584 | |
ocomeni | 88:7ffa053be662 | 585 | //_wmutex.unlock(); |
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(https_request->get_error(), buf, 100); |
ocomeni | 88:7ffa053be662 | 590 | printf("HttpsRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 591 | delete https_request; // free the memory |
ocomeni | 88:7ffa053be662 | 592 | https_connection_active = false; // reset true whenever connection fails |
ocomeni | 88:7ffa053be662 | 593 | delete socket; |
ocomeni | 87:99b37d26ff2a | 594 | return; |
ocomeni | 87:99b37d26ff2a | 595 | } |
ocomeni | 88:7ffa053be662 | 596 | https_connection_active = true; // set true whenever connection succeeds |
ocomeni | 87:99b37d26ff2a | 597 | printf("\n----- HTTPS POST response -----\r\n"); |
ocomeni | 87:99b37d26ff2a | 598 | } |
ocomeni | 84:7c7add00f4bf | 599 | return_response(http_response); |
ocomeni | 88:7ffa053be662 | 600 | delete http_response; |
ocomeni | 79:a2187bbfa407 | 601 | } |
ocomeni | 79:a2187bbfa407 | 602 | |
ocomeni | 79:a2187bbfa407 | 603 | void WiFiManager::createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 604 | const char* url, |
ocomeni | 79:a2187bbfa407 | 605 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 606 | { |
ocomeni | 84:7c7add00f4bf | 607 | http_request = new HttpRequest(network, |
ocomeni | 84:7c7add00f4bf | 608 | method, url, body_callback);; |
ocomeni | 79:a2187bbfa407 | 609 | } |
ocomeni | 79:a2187bbfa407 | 610 | |
ocomeni | 79:a2187bbfa407 | 611 | void WiFiManager::setHttpHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 612 | { |
ocomeni | 79:a2187bbfa407 | 613 | http_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 614 | } |
ocomeni | 79:a2187bbfa407 | 615 | |
ocomeni | 79:a2187bbfa407 | 616 | void WiFiManager::setHttpsHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 617 | { |
ocomeni | 79:a2187bbfa407 | 618 | https_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 619 | } |
ocomeni | 79:a2187bbfa407 | 620 | |
ocomeni | 79:a2187bbfa407 | 621 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 622 | { |
ocomeni | 78:07bb86e3ce14 | 623 | } |
ocomeni | 79:a2187bbfa407 | 624 | |
ocomeni | 79:a2187bbfa407 | 625 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 626 | { |
ocomeni | 78:07bb86e3ce14 | 627 | } |
ocomeni | 79:a2187bbfa407 | 628 |