Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.cpp@90:ed0267eca7b5, 2019-04-07 (annotated)
- Committer:
- ocomeni
- Date:
- Sun Apr 07 10:52:37 2019 +0000
- Revision:
- 90:ed0267eca7b5
- Parent:
- 89:45f6db09a76d
- Child:
- 91:d6b6319ad681
https response format implemented and tested
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 | 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 | 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 | 90:ed0267eca7b5 | 351 | { |
ocomeni | 90:ed0267eca7b5 | 352 | |
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 | 90:ed0267eca7b5 | 359 | int hdrLen = 0; |
ocomeni | 88:7ffa053be662 | 360 | do { |
ocomeni | 90:ed0267eca7b5 | 361 | if(!queueResult) |
ocomeni | 90:ed0267eca7b5 | 362 | { |
ocomeni | 90:ed0267eca7b5 | 363 | wait_ms(10); // wait 10 ms to allow data to be transferred |
ocomeni | 90:ed0267eca7b5 | 364 | } |
ocomeni | 90:ed0267eca7b5 | 365 | else { |
ocomeni | 90:ed0267eca7b5 | 366 | if(http_response_hdr_sent == false){ |
ocomeni | 90:ed0267eca7b5 | 367 | copyResponseHdr2Queue(); |
ocomeni | 90:ed0267eca7b5 | 368 | hdrLen = at_data_resp->dataLen; |
ocomeni | 90:ed0267eca7b5 | 369 | http_response_hdr_sent = true; |
ocomeni | 90:ed0267eca7b5 | 370 | } |
ocomeni | 90:ed0267eca7b5 | 371 | int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos - hdrLen ) ; |
ocomeni | 90:ed0267eca7b5 | 372 | at_data_resp->dataLen += cpyLen; |
ocomeni | 90:ed0267eca7b5 | 373 | memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen); |
ocomeni | 90:ed0267eca7b5 | 374 | } |
ocomeni | 88:7ffa053be662 | 375 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 90:ed0267eca7b5 | 376 | if(queueResult){ |
ocomeni | 90:ed0267eca7b5 | 377 | pos+= at_data_resp->dataLen; |
ocomeni | 90:ed0267eca7b5 | 378 | at_data_resp->dataLen = 0; |
ocomeni | 90:ed0267eca7b5 | 379 | hdrLen = 0; |
ocomeni | 90:ed0267eca7b5 | 380 | } |
ocomeni | 88:7ffa053be662 | 381 | }while(queueResult == false || pos < bufLen); |
ocomeni | 88:7ffa053be662 | 382 | delete at_data_resp; |
ocomeni | 88:7ffa053be662 | 383 | } |
ocomeni | 79:a2187bbfa407 | 384 | |
ocomeni | 90:ed0267eca7b5 | 385 | void WiFiManager::copyResponseHdr2Queue() |
ocomeni | 90:ed0267eca7b5 | 386 | { |
ocomeni | 90:ed0267eca7b5 | 387 | int numChars = 0; |
ocomeni | 90:ed0267eca7b5 | 388 | // create message pointer for response header generation |
ocomeni | 90:ed0267eca7b5 | 389 | char * msgPtr = (char *)at_data_resp->buffer; |
ocomeni | 90:ed0267eca7b5 | 390 | // do status line |
ocomeni | 90:ed0267eca7b5 | 391 | numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(), |
ocomeni | 90:ed0267eca7b5 | 392 | http_response->get_status_message().c_str()); |
ocomeni | 90:ed0267eca7b5 | 393 | msgPtr += numChars; |
ocomeni | 90:ed0267eca7b5 | 394 | for (size_t ix = 0; ix < http_response->get_headers_length(); ix++) { |
ocomeni | 90:ed0267eca7b5 | 395 | numChars = sprintf(msgPtr, "%s: %s\r\n", |
ocomeni | 90:ed0267eca7b5 | 396 | http_response->get_headers_fields()[ix]->c_str(), |
ocomeni | 90:ed0267eca7b5 | 397 | http_response->get_headers_values()[ix]->c_str()); |
ocomeni | 90:ed0267eca7b5 | 398 | msgPtr += numChars; |
ocomeni | 90:ed0267eca7b5 | 399 | } |
ocomeni | 90:ed0267eca7b5 | 400 | numChars = sprintf(msgPtr, "\r\n"); |
ocomeni | 90:ed0267eca7b5 | 401 | msgPtr += numChars; |
ocomeni | 90:ed0267eca7b5 | 402 | // print out generated header |
ocomeni | 90:ed0267eca7b5 | 403 | printf("[WiFi MAN] generated response header:\n"); |
ocomeni | 90:ed0267eca7b5 | 404 | printf("%s\r\n", (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 405 | // calculate header length |
ocomeni | 90:ed0267eca7b5 | 406 | at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 407 | } |
ocomeni | 90:ed0267eca7b5 | 408 | |
ocomeni | 84:7c7add00f4bf | 409 | void WiFiManager::return_response(HttpResponse* res) { |
ocomeni | 84:7c7add00f4bf | 410 | |
ocomeni | 88:7ffa053be662 | 411 | at_data_resp = new at_data_msg_t; |
ocomeni | 87:99b37d26ff2a | 412 | int numChars = 0; |
ocomeni | 87:99b37d26ff2a | 413 | // create message pointer for response header generation |
ocomeni | 88:7ffa053be662 | 414 | char * msgPtr = (char *)at_data_resp->buffer; |
ocomeni | 87:99b37d26ff2a | 415 | // do status line |
ocomeni | 87:99b37d26ff2a | 416 | numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str()); |
ocomeni | 87:99b37d26ff2a | 417 | msgPtr += numChars; |
ocomeni | 84:7c7add00f4bf | 418 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
ocomeni | 87:99b37d26ff2a | 419 | numChars = sprintf(msgPtr, "%s: %s\r\n", |
ocomeni | 87:99b37d26ff2a | 420 | res->get_headers_fields()[ix]->c_str(), |
ocomeni | 87:99b37d26ff2a | 421 | res->get_headers_values()[ix]->c_str()); |
ocomeni | 87:99b37d26ff2a | 422 | msgPtr += numChars; |
ocomeni | 84:7c7add00f4bf | 423 | } |
ocomeni | 87:99b37d26ff2a | 424 | numChars = sprintf(msgPtr, "\r\n\r\n"); |
ocomeni | 87:99b37d26ff2a | 425 | msgPtr += numChars; |
ocomeni | 87:99b37d26ff2a | 426 | // print out generated header |
ocomeni | 90:ed0267eca7b5 | 427 | printf("[WiFi MAN] generated response header:\n"); |
ocomeni | 88:7ffa053be662 | 428 | printf("%s\r\n", (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 429 | // calculate header length |
ocomeni | 89:45f6db09a76d | 430 | at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer); |
ocomeni | 84:7c7add00f4bf | 431 | |
ocomeni | 90:ed0267eca7b5 | 432 | // package and send on wifi data queue |
ocomeni | 88:7ffa053be662 | 433 | at_data_resp->at_resp = AT_HTTPS_RESP; |
ocomeni | 89:45f6db09a76d | 434 | bool queueResult = true; |
ocomeni | 89:45f6db09a76d | 435 | int wait_count = 0; |
ocomeni | 89:45f6db09a76d | 436 | do |
ocomeni | 89:45f6db09a76d | 437 | { |
ocomeni | 89:45f6db09a76d | 438 | if(!queueResult){ |
ocomeni | 89:45f6db09a76d | 439 | wait_count++; |
ocomeni | 89:45f6db09a76d | 440 | printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 89:45f6db09a76d | 441 | wait_ms(10); |
ocomeni | 89:45f6db09a76d | 442 | } |
ocomeni | 89:45f6db09a76d | 443 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 89:45f6db09a76d | 444 | }while(queueResult == false); |
ocomeni | 88:7ffa053be662 | 445 | delete at_data_resp; |
ocomeni | 84:7c7add00f4bf | 446 | } |
ocomeni | 90:ed0267eca7b5 | 447 | |
ocomeni | 90:ed0267eca7b5 | 448 | |
ocomeni | 90:ed0267eca7b5 | 449 | void WiFiManager::printBufferInHex(uint8_t *buf, int pLen) |
ocomeni | 90:ed0267eca7b5 | 450 | { |
ocomeni | 90:ed0267eca7b5 | 451 | for(int i =0;i<pLen;i++){ |
ocomeni | 90:ed0267eca7b5 | 452 | if(i%8==0) printf("\n[%3d]",i); |
ocomeni | 90:ed0267eca7b5 | 453 | printf("%02x ", buf[i]); |
ocomeni | 90:ed0267eca7b5 | 454 | } |
ocomeni | 90:ed0267eca7b5 | 455 | printf("\n"); |
ocomeni | 90:ed0267eca7b5 | 456 | } |
ocomeni | 90:ed0267eca7b5 | 457 | |
ocomeni | 88:7ffa053be662 | 458 | //#define TRY_PRINTF |
ocomeni | 84:7c7add00f4bf | 459 | |
ocomeni | 90:ed0267eca7b5 | 460 | void WiFiManager::body_callback(const char *at, uint32_t length) { |
ocomeni | 84:7c7add00f4bf | 461 | printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); |
ocomeni | 84:7c7add00f4bf | 462 | chunkNum++; |
ocomeni | 89:45f6db09a76d | 463 | sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length); |
ocomeni | 84:7c7add00f4bf | 464 | } |
ocomeni | 84:7c7add00f4bf | 465 | |
ocomeni | 88:7ffa053be662 | 466 | |
ocomeni | 88:7ffa053be662 | 467 | bool WiFiManager::createTLSconnection(const char * hostName) |
ocomeni | 88:7ffa053be662 | 468 | { |
ocomeni | 90:ed0267eca7b5 | 469 | //mbed_trace_init(); |
ocomeni | 88:7ffa053be662 | 470 | socket = new TLSSocket(); |
ocomeni | 88:7ffa053be662 | 471 | |
ocomeni | 88:7ffa053be662 | 472 | nsapi_error_t r; |
ocomeni | 88:7ffa053be662 | 473 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 474 | r = socket->open(network); |
ocomeni | 88:7ffa053be662 | 475 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 476 | { |
ocomeni | 89:45f6db09a76d | 477 | printf("TLS open failed!!\n"); |
ocomeni | 88:7ffa053be662 | 478 | return false; |
ocomeni | 88:7ffa053be662 | 479 | } |
ocomeni | 88:7ffa053be662 | 480 | printf("TLS open passed!!\n"); |
ocomeni | 88:7ffa053be662 | 481 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
ocomeni | 88:7ffa053be662 | 482 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 483 | { |
ocomeni | 88:7ffa053be662 | 484 | printf("TLS set_root_ca_cert failed!!\n"); |
ocomeni | 88:7ffa053be662 | 485 | return false; |
ocomeni | 88:7ffa053be662 | 486 | } |
ocomeni | 88:7ffa053be662 | 487 | printf("TLS set_root_ca_cert passed!!\n"); |
ocomeni | 88:7ffa053be662 | 488 | r = socket->connect(hostName, 443); |
ocomeni | 88:7ffa053be662 | 489 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 490 | { |
ocomeni | 88:7ffa053be662 | 491 | printf("TLS connect failed for hostname %s!!\n", hostName); |
ocomeni | 88:7ffa053be662 | 492 | return false; |
ocomeni | 88:7ffa053be662 | 493 | } |
ocomeni | 88:7ffa053be662 | 494 | printf("TLS connection successful for https site : %s\n", hostName); |
ocomeni | 88:7ffa053be662 | 495 | return true; |
ocomeni | 88:7ffa053be662 | 496 | } |
ocomeni | 88:7ffa053be662 | 497 | #define TESTING_HTTPS |
ocomeni | 84:7c7add00f4bf | 498 | void WiFiManager::createHttpsRequest() |
ocomeni | 79:a2187bbfa407 | 499 | { |
ocomeni | 87:99b37d26ff2a | 500 | // reset chunk #; |
ocomeni | 87:99b37d26ff2a | 501 | chunkNum = 0; |
ocomeni | 90:ed0267eca7b5 | 502 | http_response_hdr_sent = false; |
ocomeni | 89:45f6db09a76d | 503 | printf("\n[WIFI MAN] Http Request received:\n"); |
ocomeni | 84:7c7add00f4bf | 504 | http_req_cfg = (http_request_t *) data_msg->buffer; |
ocomeni | 90:ed0267eca7b5 | 505 | printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI); |
ocomeni | 89:45f6db09a76d | 506 | printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url); |
ocomeni | 87:99b37d26ff2a | 507 | char full_url[100]; |
ocomeni | 88:7ffa053be662 | 508 | char host[60] ; |
ocomeni | 87:99b37d26ff2a | 509 | strncpy(full_url,internet_config.url, strlen(internet_config.url)+1); |
ocomeni | 90:ed0267eca7b5 | 510 | strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1); |
ocomeni | 90:ed0267eca7b5 | 511 | strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1); |
ocomeni | 87:99b37d26ff2a | 512 | printf("\n[WIFI MAN] server url+uri = %s\n", full_url); |
ocomeni | 90:ed0267eca7b5 | 513 | printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName); |
ocomeni | 90:ed0267eca7b5 | 514 | printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal); |
ocomeni | 90:ed0267eca7b5 | 515 | printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType); |
ocomeni | 90:ed0267eca7b5 | 516 | printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen); |
ocomeni | 84:7c7add00f4bf | 517 | |
ocomeni | 84:7c7add00f4bf | 518 | int bodyLen; |
ocomeni | 90:ed0267eca7b5 | 519 | sscanf(http_req_cfg->contentLen, "%d", &bodyLen); |
ocomeni | 90:ed0267eca7b5 | 520 | printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 521 | |
ocomeni | 90:ed0267eca7b5 | 522 | if(bodyLen > 10){ |
ocomeni | 90:ed0267eca7b5 | 523 | printf("\n Message Body:\n"); |
ocomeni | 90:ed0267eca7b5 | 524 | printBufferInHex(http_req_cfg->body, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 525 | } |
ocomeni | 87:99b37d26ff2a | 526 | if(strstr(internet_config.url, "http:")!=NULL) // http request |
ocomeni | 87:99b37d26ff2a | 527 | { |
ocomeni | 87:99b37d26ff2a | 528 | http_request = new HttpRequest(network, |
ocomeni | 90:ed0267eca7b5 | 529 | http_req_cfg->method, |
ocomeni | 90:ed0267eca7b5 | 530 | full_url, |
ocomeni | 90:ed0267eca7b5 | 531 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 87:99b37d26ff2a | 532 | setHttpHeader("Host", http_req_cfg->hostName); |
ocomeni | 87:99b37d26ff2a | 533 | setHttpHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 89:45f6db09a76d | 534 | printf("http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 535 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 88:7ffa053be662 | 536 | printf("HTTP_GET -- ignoring body\n"); |
ocomeni | 88:7ffa053be662 | 537 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 538 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 539 | http_response = http_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 540 | } |
ocomeni | 88:7ffa053be662 | 541 | else{ |
ocomeni | 88:7ffa053be662 | 542 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 543 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 544 | http_response = http_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 545 | } |
ocomeni | 87:99b37d26ff2a | 546 | free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 547 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 548 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 549 | mbedtls_strerror(http_request->get_error(), buf, 100); |
ocomeni | 87:99b37d26ff2a | 550 | printf("HttpRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 551 | //printf("HttpsRequest failed (error code %d)\n", https_request->get_error()); |
ocomeni | 87:99b37d26ff2a | 552 | delete http_request; // free the memory |
ocomeni | 87:99b37d26ff2a | 553 | return; |
ocomeni | 87:99b37d26ff2a | 554 | } |
ocomeni | 88:7ffa053be662 | 555 | delete http_request; // free the memory |
ocomeni | 87:99b37d26ff2a | 556 | printf("\n----- HTTP POST response -----\n"); |
ocomeni | 84:7c7add00f4bf | 557 | } |
ocomeni | 87:99b37d26ff2a | 558 | else |
ocomeni | 87:99b37d26ff2a | 559 | { |
ocomeni | 90:ed0267eca7b5 | 560 | #ifndef DONT_USE_TLS_SOCKET |
ocomeni | 88:7ffa053be662 | 561 | if(https_connection_active == false){ |
ocomeni | 88:7ffa053be662 | 562 | bool tlsResult; |
ocomeni | 88:7ffa053be662 | 563 | tlsResult = createTLSconnection(host); |
ocomeni | 88:7ffa053be662 | 564 | if(tlsResult == false){ |
ocomeni | 88:7ffa053be662 | 565 | delete socket; |
ocomeni | 88:7ffa053be662 | 566 | free_DataMsg(); |
ocomeni | 88:7ffa053be662 | 567 | return; |
ocomeni | 88:7ffa053be662 | 568 | } |
ocomeni | 88:7ffa053be662 | 569 | printf("[create https] TLS connection successful for https site : %s\n", host); |
ocomeni | 88:7ffa053be662 | 570 | } |
ocomeni | 90:ed0267eca7b5 | 571 | printf("after call to createTLSconnection \n"); |
ocomeni | 90:ed0267eca7b5 | 572 | print_memory_info(); |
ocomeni | 88:7ffa053be662 | 573 | // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument |
ocomeni | 88:7ffa053be662 | 574 | //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); |
ocomeni | 88:7ffa053be662 | 575 | //_wmutex.lock(); |
ocomeni | 88:7ffa053be662 | 576 | https_request = new HttpsRequest(socket, |
ocomeni | 87:99b37d26ff2a | 577 | http_req_cfg->method, |
ocomeni | 88:7ffa053be662 | 578 | full_url, |
ocomeni | 87:99b37d26ff2a | 579 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 90:ed0267eca7b5 | 580 | #else |
ocomeni | 90:ed0267eca7b5 | 581 | https_request = new HttpsRequest(network, |
ocomeni | 90:ed0267eca7b5 | 582 | SSL_CA_PEM, |
ocomeni | 90:ed0267eca7b5 | 583 | http_req_cfg->method, |
ocomeni | 90:ed0267eca7b5 | 584 | full_url, |
ocomeni | 90:ed0267eca7b5 | 585 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 90:ed0267eca7b5 | 586 | #endif |
ocomeni | 88:7ffa053be662 | 587 | #ifdef TESTING_HTTPS |
ocomeni | 90:ed0267eca7b5 | 588 | printf("http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 589 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 88:7ffa053be662 | 590 | printf("HTTP_GET -- ignoring body\n"); |
ocomeni | 89:45f6db09a76d | 591 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 592 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 593 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 594 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 595 | http_response = https_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 596 | } |
ocomeni | 88:7ffa053be662 | 597 | else{ |
ocomeni | 89:45f6db09a76d | 598 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 599 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 90:ed0267eca7b5 | 600 | setHttpsHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 90:ed0267eca7b5 | 601 | setHttpsHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 602 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 603 | } |
ocomeni | 88:7ffa053be662 | 604 | #else |
ocomeni | 89:45f6db09a76d | 605 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 606 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 607 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 608 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 87:99b37d26ff2a | 609 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 610 | #endif |
ocomeni | 88:7ffa053be662 | 611 | |
ocomeni | 88:7ffa053be662 | 612 | //_wmutex.unlock(); |
ocomeni | 90:ed0267eca7b5 | 613 | //free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 614 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 615 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 616 | mbedtls_strerror(https_request->get_error(), buf, 100); |
ocomeni | 88:7ffa053be662 | 617 | printf("HttpsRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 618 | delete https_request; // free the memory |
ocomeni | 88:7ffa053be662 | 619 | https_connection_active = false; // reset true whenever connection fails |
ocomeni | 88:7ffa053be662 | 620 | delete socket; |
ocomeni | 90:ed0267eca7b5 | 621 | free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 622 | return; |
ocomeni | 87:99b37d26ff2a | 623 | } |
ocomeni | 88:7ffa053be662 | 624 | https_connection_active = true; // set true whenever connection succeeds |
ocomeni | 87:99b37d26ff2a | 625 | printf("\n----- HTTPS POST response -----\r\n"); |
ocomeni | 87:99b37d26ff2a | 626 | } |
ocomeni | 90:ed0267eca7b5 | 627 | if(http_response != NULL){ |
ocomeni | 90:ed0267eca7b5 | 628 | //return_response(http_response); |
ocomeni | 90:ed0267eca7b5 | 629 | delete http_response; // free the response memory |
ocomeni | 90:ed0267eca7b5 | 630 | } |
ocomeni | 90:ed0267eca7b5 | 631 | free_DataMsg(); |
ocomeni | 90:ed0267eca7b5 | 632 | //delete https_request; // free the request memory |
ocomeni | 79:a2187bbfa407 | 633 | } |
ocomeni | 79:a2187bbfa407 | 634 | |
ocomeni | 79:a2187bbfa407 | 635 | void WiFiManager::createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 636 | const char* url, |
ocomeni | 79:a2187bbfa407 | 637 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 638 | { |
ocomeni | 84:7c7add00f4bf | 639 | http_request = new HttpRequest(network, |
ocomeni | 84:7c7add00f4bf | 640 | method, url, body_callback);; |
ocomeni | 79:a2187bbfa407 | 641 | } |
ocomeni | 79:a2187bbfa407 | 642 | |
ocomeni | 79:a2187bbfa407 | 643 | void WiFiManager::setHttpHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 644 | { |
ocomeni | 79:a2187bbfa407 | 645 | http_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 646 | } |
ocomeni | 79:a2187bbfa407 | 647 | |
ocomeni | 79:a2187bbfa407 | 648 | void WiFiManager::setHttpsHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 649 | { |
ocomeni | 79:a2187bbfa407 | 650 | https_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 651 | } |
ocomeni | 79:a2187bbfa407 | 652 | |
ocomeni | 79:a2187bbfa407 | 653 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 654 | { |
ocomeni | 78:07bb86e3ce14 | 655 | } |
ocomeni | 79:a2187bbfa407 | 656 | |
ocomeni | 79:a2187bbfa407 | 657 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 658 | { |
ocomeni | 78:07bb86e3ce14 | 659 | } |
ocomeni | 79:a2187bbfa407 | 660 |