Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.cpp@103:7b566b522427, 2019-05-02 (annotated)
- Committer:
- ocomeni
- Date:
- Thu May 02 21:50:17 2019 +0000
- Revision:
- 103:7b566b522427
- Parent:
- 102:9748f290a1a5
- Child:
- 104:11e9605093c9
reached cloud hello message in box comms sequence
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 103:7b566b522427 | 1 | #include "debug.h" |
ocomeni | 78:07bb86e3ce14 | 2 | #include "WiFiManager.h" |
ocomeni | 78:07bb86e3ce14 | 3 | #include "common_config.h" |
ocomeni | 103:7b566b522427 | 4 | #define FILE_CODE "wifi" |
ocomeni | 78:07bb86e3ce14 | 5 | |
ocomeni | 78:07bb86e3ce14 | 6 | |
ocomeni | 79:a2187bbfa407 | 7 | WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, |
ocomeni | 98:65c2333a38b6 | 8 | events::EventQueue &event_queue, |
ocomeni | 80:e8f0e92e3ac9 | 9 | MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, |
ocomeni | 80:e8f0e92e3ac9 | 10 | Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, |
ocomeni | 81:637a87eb8170 | 11 | MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, |
ocomeni | 81:637a87eb8170 | 12 | Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, |
ocomeni | 87:99b37d26ff2a | 13 | MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, |
ocomeni | 87:99b37d26ff2a | 14 | Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, |
ocomeni | 87:99b37d26ff2a | 15 | MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, |
ocomeni | 87:99b37d26ff2a | 16 | Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue) |
ocomeni | 78:07bb86e3ce14 | 17 | : |
ocomeni | 81:637a87eb8170 | 18 | wifi_config(wifi_config), |
ocomeni | 81:637a87eb8170 | 19 | network(wifi), |
ocomeni | 98:65c2333a38b6 | 20 | _event_queue(event_queue), |
ocomeni | 81:637a87eb8170 | 21 | _aT2WiFimPool(aT2WiFimPool), |
ocomeni | 81:637a87eb8170 | 22 | _aT2WiFiCmdQueue(aT2WiFiCmdQueue), |
ocomeni | 81:637a87eb8170 | 23 | |
ocomeni | 81:637a87eb8170 | 24 | _wiFi2ATmPool(wiFi2ATmPool), |
ocomeni | 81:637a87eb8170 | 25 | _wiFi2ATCmdQueue(wiFi2ATCmdQueue), |
ocomeni | 81:637a87eb8170 | 26 | |
ocomeni | 81:637a87eb8170 | 27 | _aT2WiFiDatamPool(aT2WiFiDatamPool), |
ocomeni | 81:637a87eb8170 | 28 | _aT2WiFiDataQueue(aT2WiFiDataQueue), |
ocomeni | 81:637a87eb8170 | 29 | |
ocomeni | 81:637a87eb8170 | 30 | _wiFi2ATDatamPool(wiFi2ATDatamPool), |
ocomeni | 81:637a87eb8170 | 31 | _wiFi2ATDataQueue(wiFi2ATDataQueue) |
ocomeni | 78:07bb86e3ce14 | 32 | |
ocomeni | 78:07bb86e3ce14 | 33 | { |
ocomeni | 79:a2187bbfa407 | 34 | lastScanCount = 0; |
ocomeni | 79:a2187bbfa407 | 35 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 36 | internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme |
ocomeni | 81:637a87eb8170 | 37 | is_connected = false; |
ocomeni | 100:80ef4bc31b7a | 38 | http_response = NULL; |
ocomeni | 84:7c7add00f4bf | 39 | chunkNum = 0; |
ocomeni | 88:7ffa053be662 | 40 | socket = NULL; |
ocomeni | 88:7ffa053be662 | 41 | https_connection_active = false; |
ocomeni | 78:07bb86e3ce14 | 42 | } |
ocomeni | 78:07bb86e3ce14 | 43 | |
ocomeni | 78:07bb86e3ce14 | 44 | WiFiManager::~WiFiManager() |
ocomeni | 78:07bb86e3ce14 | 45 | { |
ocomeni | 78:07bb86e3ce14 | 46 | } |
ocomeni | 79:a2187bbfa407 | 47 | |
ocomeni | 81:637a87eb8170 | 48 | bool WiFiManager::queueATresponse(at_cmd_resp_t resp){ |
ocomeni | 92:ec9550034276 | 49 | #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL |
ocomeni | 81:637a87eb8170 | 50 | at_resp_message_t *atResp = _wiFi2ATmPool->alloc(); |
ocomeni | 92:ec9550034276 | 51 | #else |
ocomeni | 92:ec9550034276 | 52 | at_resp_message_t *atResp = (at_resp_message_t *) malloc(sizeof(at_resp_message_t)); |
ocomeni | 92:ec9550034276 | 53 | #endif |
ocomeni | 88:7ffa053be662 | 54 | if(atResp == NULL) return false; // queue full; |
ocomeni | 81:637a87eb8170 | 55 | atResp->at_resp = resp; |
ocomeni | 81:637a87eb8170 | 56 | _wiFi2ATCmdQueue->put(atResp); |
ocomeni | 81:637a87eb8170 | 57 | return true; |
ocomeni | 81:637a87eb8170 | 58 | } |
ocomeni | 81:637a87eb8170 | 59 | |
ocomeni | 81:637a87eb8170 | 60 | |
ocomeni | 81:637a87eb8170 | 61 | bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){ |
ocomeni | 81:637a87eb8170 | 62 | at_data_msg_t *atData = _wiFi2ATDatamPool->alloc(); |
ocomeni | 88:7ffa053be662 | 63 | if(atData == NULL) return false; // queue full; |
ocomeni | 81:637a87eb8170 | 64 | atData->at_resp = at_resp.at_resp; |
ocomeni | 81:637a87eb8170 | 65 | atData->dataLen = at_resp.dataLen; |
ocomeni | 81:637a87eb8170 | 66 | memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen); |
ocomeni | 81:637a87eb8170 | 67 | _wiFi2ATDataQueue->put(atData); |
ocomeni | 103:7b566b522427 | 68 | dbg_printf(LOG, "[WIFI MAN] queued data size = %d : at_resp = %d\n", at_resp.dataLen, at_resp.at_resp); |
ocomeni | 81:637a87eb8170 | 69 | return true; |
ocomeni | 81:637a87eb8170 | 70 | } |
ocomeni | 81:637a87eb8170 | 71 | |
ocomeni | 79:a2187bbfa407 | 72 | |
ocomeni | 79:a2187bbfa407 | 73 | |
ocomeni | 79:a2187bbfa407 | 74 | void WiFiManager::runMain(){ |
ocomeni | 81:637a87eb8170 | 75 | nsapi_error_t error; |
ocomeni | 99:05398b3184f8 | 76 | bool result; |
ocomeni | 103:7b566b522427 | 77 | dbg_printf(LOG, "\r\n [WIFI MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); |
ocomeni | 79:a2187bbfa407 | 78 | while(true){ |
ocomeni | 79:a2187bbfa407 | 79 | dequeueWiFiCommands(); |
ocomeni | 81:637a87eb8170 | 80 | dequeueATdataResponse(); |
ocomeni | 79:a2187bbfa407 | 81 | switch(wifiCmd){ |
ocomeni | 79:a2187bbfa407 | 82 | case WIFI_CMD_NONE: |
ocomeni | 79:a2187bbfa407 | 83 | // IDLE STATE |
ocomeni | 79:a2187bbfa407 | 84 | break; |
ocomeni | 79:a2187bbfa407 | 85 | case WIFI_CMD_SCAN: |
ocomeni | 79:a2187bbfa407 | 86 | error = scanNetworks(); |
ocomeni | 79:a2187bbfa407 | 87 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 88 | queueATresponse(AT_SCAN_RESP); |
ocomeni | 81:637a87eb8170 | 89 | break; |
ocomeni | 81:637a87eb8170 | 90 | case WIFI_CMD_DETAILED_SCAN: |
ocomeni | 98:65c2333a38b6 | 91 | { |
ocomeni | 81:637a87eb8170 | 92 | nsapi_size_or_error_t cnt_err; |
ocomeni | 81:637a87eb8170 | 93 | cnt_err = getAvailableAPs(lastScanCount); |
ocomeni | 81:637a87eb8170 | 94 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 95 | queueATresponse(AT_DETAILED_SCAN_RESP); |
ocomeni | 79:a2187bbfa407 | 96 | break; |
ocomeni | 98:65c2333a38b6 | 97 | } |
ocomeni | 79:a2187bbfa407 | 98 | case WIFI_CMD_CONNECT: |
ocomeni | 98:65c2333a38b6 | 99 | { |
ocomeni | 81:637a87eb8170 | 100 | error = connect(); |
ocomeni | 93:06e755a80187 | 101 | int secCount = 0; |
ocomeni | 93:06e755a80187 | 102 | while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){ |
ocomeni | 93:06e755a80187 | 103 | wait(1); // wait 1 sec |
ocomeni | 93:06e755a80187 | 104 | } |
ocomeni | 81:637a87eb8170 | 105 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 93:06e755a80187 | 106 | if(is_connected==false){ |
ocomeni | 103:7b566b522427 | 107 | dbg_printf(LOG, "[WIFI MAN] +++ WIFI CONNECTION TIMEOUT +++ \r\n"); |
ocomeni | 95:290859010c8c | 108 | //queueATresponse(AT_COMMAND_FAILED); |
ocomeni | 95:290859010c8c | 109 | responseString = (char *) malloc(100); |
ocomeni | 95:290859010c8c | 110 | sprintf(responseString, "\r\n+UUTIMEOUT\r\n"); |
ocomeni | 95:290859010c8c | 111 | sendATresponseString(AT_COMMAND_FAILED); |
ocomeni | 93:06e755a80187 | 112 | } |
ocomeni | 93:06e755a80187 | 113 | else { |
ocomeni | 95:290859010c8c | 114 | sendATresponseString(AT_CONNECT_RESP); |
ocomeni | 93:06e755a80187 | 115 | } |
ocomeni | 79:a2187bbfa407 | 116 | break; |
ocomeni | 98:65c2333a38b6 | 117 | } |
ocomeni | 79:a2187bbfa407 | 118 | case WIFI_CMD_DISCONNECT: |
ocomeni | 81:637a87eb8170 | 119 | error = disconnect(); |
ocomeni | 81:637a87eb8170 | 120 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 121 | queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 122 | break; |
ocomeni | 81:637a87eb8170 | 123 | case WIFI_CMD_CONFIG: |
ocomeni | 81:637a87eb8170 | 124 | set_WIFI_CONFIG(); |
ocomeni | 81:637a87eb8170 | 125 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 126 | queueATresponse(AT_CONFIG_RESP); |
ocomeni | 82:10072c1794d3 | 127 | break; |
ocomeni | 81:637a87eb8170 | 128 | case WIFI_CMD_INTERNET_CONFIG: |
ocomeni | 100:80ef4bc31b7a | 129 | set_internet_config(); |
ocomeni | 100:80ef4bc31b7a | 130 | queueATresponse(AT_INTERNET_CONFIG_RESP); |
ocomeni | 81:637a87eb8170 | 131 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 95:290859010c8c | 132 | break; |
ocomeni | 95:290859010c8c | 133 | case WIFI_CMD_NETWORK_STATUS: |
ocomeni | 95:290859010c8c | 134 | getNetworkStatus(); |
ocomeni | 95:290859010c8c | 135 | sendATresponseString(AT_NETWORK_STATUS_RESP); |
ocomeni | 95:290859010c8c | 136 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 95:290859010c8c | 137 | break; |
ocomeni | 95:290859010c8c | 138 | case WIFI_CMD_WIFI_STATUS: |
ocomeni | 95:290859010c8c | 139 | getWiFiStatus(); |
ocomeni | 95:290859010c8c | 140 | sendATresponseString(AT_WIFI_STATUS_RESP); |
ocomeni | 95:290859010c8c | 141 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 79:a2187bbfa407 | 142 | break; |
ocomeni | 79:a2187bbfa407 | 143 | case WIFI_CMD_SEND_HTTPS_REQ: |
ocomeni | 103:7b566b522427 | 144 | dbg_printf(LOG, "before call to send http request \n"); |
ocomeni | 88:7ffa053be662 | 145 | print_memory_info(); |
ocomeni | 99:05398b3184f8 | 146 | result = createHttpsRequest(); |
ocomeni | 99:05398b3184f8 | 147 | if(result == false) |
ocomeni | 98:65c2333a38b6 | 148 | { |
ocomeni | 98:65c2333a38b6 | 149 | sendATresponseString(AT_COMMAND_FAILED); |
ocomeni | 98:65c2333a38b6 | 150 | } |
ocomeni | 103:7b566b522427 | 151 | dbg_printf(LOG, "after call to send http request \n"); |
ocomeni | 88:7ffa053be662 | 152 | print_memory_info(); |
ocomeni | 84:7c7add00f4bf | 153 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 79:a2187bbfa407 | 154 | break; |
ocomeni | 79:a2187bbfa407 | 155 | case WIFI_CMD_SEND_HTTP_REQ: |
ocomeni | 79:a2187bbfa407 | 156 | break; |
ocomeni | 79:a2187bbfa407 | 157 | default: |
ocomeni | 79:a2187bbfa407 | 158 | break; |
ocomeni | 79:a2187bbfa407 | 159 | } |
ocomeni | 79:a2187bbfa407 | 160 | wait_ms(100); // |
ocomeni | 79:a2187bbfa407 | 161 | } |
ocomeni | 79:a2187bbfa407 | 162 | |
ocomeni | 78:07bb86e3ce14 | 163 | } |
ocomeni | 79:a2187bbfa407 | 164 | |
ocomeni | 91:d6b6319ad681 | 165 | |
ocomeni | 95:290859010c8c | 166 | void WiFiManager::sendATresponseString(at_cmd_resp_t at_cmd) |
ocomeni | 91:d6b6319ad681 | 167 | { |
ocomeni | 95:290859010c8c | 168 | int strLen = strlen(responseString) + 1; |
ocomeni | 91:d6b6319ad681 | 169 | at_data_resp = new at_data_msg_t; |
ocomeni | 91:d6b6319ad681 | 170 | // set string length |
ocomeni | 91:d6b6319ad681 | 171 | at_data_resp->dataLen = strLen; |
ocomeni | 91:d6b6319ad681 | 172 | memcpy(at_data_resp->buffer, responseString, strLen); |
ocomeni | 95:290859010c8c | 173 | free(responseString); |
ocomeni | 99:05398b3184f8 | 174 | responseString = NULL; |
ocomeni | 91:d6b6319ad681 | 175 | // package and send on wifi data queue |
ocomeni | 91:d6b6319ad681 | 176 | at_data_resp->at_resp = at_cmd; |
ocomeni | 91:d6b6319ad681 | 177 | bool queueResult = true; |
ocomeni | 91:d6b6319ad681 | 178 | int wait_count = 0; |
ocomeni | 91:d6b6319ad681 | 179 | do |
ocomeni | 91:d6b6319ad681 | 180 | { |
ocomeni | 91:d6b6319ad681 | 181 | if(!queueResult){ |
ocomeni | 91:d6b6319ad681 | 182 | wait_count++; |
ocomeni | 103:7b566b522427 | 183 | dbg_printf(LOG, "ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 91:d6b6319ad681 | 184 | wait_ms(10); |
ocomeni | 91:d6b6319ad681 | 185 | } |
ocomeni | 91:d6b6319ad681 | 186 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 91:d6b6319ad681 | 187 | }while(queueResult == false); |
ocomeni | 91:d6b6319ad681 | 188 | delete at_data_resp; |
ocomeni | 99:05398b3184f8 | 189 | at_data_resp = NULL; |
ocomeni | 91:d6b6319ad681 | 190 | } |
ocomeni | 91:d6b6319ad681 | 191 | |
ocomeni | 99:05398b3184f8 | 192 | |
ocomeni | 99:05398b3184f8 | 193 | void WiFiManager::sendATresponseBytes(at_cmd_resp_t at_cmd, int len) |
ocomeni | 99:05398b3184f8 | 194 | { |
ocomeni | 99:05398b3184f8 | 195 | at_data_resp = new at_data_msg_t; |
ocomeni | 99:05398b3184f8 | 196 | // set string length |
ocomeni | 99:05398b3184f8 | 197 | at_data_resp->dataLen = len; |
ocomeni | 99:05398b3184f8 | 198 | memcpy(at_data_resp->buffer, responseBytes, len); |
ocomeni | 99:05398b3184f8 | 199 | free(responseBytes); |
ocomeni | 99:05398b3184f8 | 200 | responseBytes = NULL; |
ocomeni | 99:05398b3184f8 | 201 | // package and send on wifi data queue |
ocomeni | 99:05398b3184f8 | 202 | at_data_resp->at_resp = at_cmd; |
ocomeni | 99:05398b3184f8 | 203 | bool queueResult = true; |
ocomeni | 99:05398b3184f8 | 204 | int wait_count = 0; |
ocomeni | 99:05398b3184f8 | 205 | do |
ocomeni | 99:05398b3184f8 | 206 | { |
ocomeni | 99:05398b3184f8 | 207 | if(!queueResult){ |
ocomeni | 99:05398b3184f8 | 208 | wait_count++; |
ocomeni | 99:05398b3184f8 | 209 | wait_ms(10); |
ocomeni | 103:7b566b522427 | 210 | dbg_printf(LOG, "ATCMD Queue full waited %d ms so far...\n", wait_count*10); |
ocomeni | 99:05398b3184f8 | 211 | } |
ocomeni | 99:05398b3184f8 | 212 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 99:05398b3184f8 | 213 | }while(queueResult == false); |
ocomeni | 99:05398b3184f8 | 214 | delete at_data_resp; |
ocomeni | 99:05398b3184f8 | 215 | at_data_resp = NULL; |
ocomeni | 103:7b566b522427 | 216 | dbg_printf(LOG, "[WIFI-MAN] sendATresponseBytes completed successfully\r\n"); |
ocomeni | 99:05398b3184f8 | 217 | } |
ocomeni | 99:05398b3184f8 | 218 | |
ocomeni | 99:05398b3184f8 | 219 | |
ocomeni | 99:05398b3184f8 | 220 | |
ocomeni | 79:a2187bbfa407 | 221 | bool WiFiManager::dequeueWiFiCommands(){ |
ocomeni | 81:637a87eb8170 | 222 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 223 | osEvent evt = _aT2WiFiCmdQueue->get(0); |
ocomeni | 79:a2187bbfa407 | 224 | if(evt.status == osEventMessage){ |
ocomeni | 79:a2187bbfa407 | 225 | wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p; |
ocomeni | 79:a2187bbfa407 | 226 | setNextCommand(cmd->wifi_cmd); |
ocomeni | 92:ec9550034276 | 227 | #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL |
ocomeni | 79:a2187bbfa407 | 228 | _aT2WiFimPool->free(cmd); |
ocomeni | 96:f5ed273881af | 229 | cmd = NULL; |
ocomeni | 92:ec9550034276 | 230 | #else |
ocomeni | 92:ec9550034276 | 231 | free(cmd); |
ocomeni | 96:f5ed273881af | 232 | cmd = NULL; |
ocomeni | 92:ec9550034276 | 233 | #endif |
ocomeni | 79:a2187bbfa407 | 234 | } |
ocomeni | 79:a2187bbfa407 | 235 | return true; |
ocomeni | 79:a2187bbfa407 | 236 | } |
ocomeni | 79:a2187bbfa407 | 237 | |
ocomeni | 79:a2187bbfa407 | 238 | |
ocomeni | 81:637a87eb8170 | 239 | bool WiFiManager::dequeueATdataResponse(){ |
ocomeni | 81:637a87eb8170 | 240 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 241 | osEvent evt = _aT2WiFiDataQueue->get(0); |
ocomeni | 81:637a87eb8170 | 242 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 243 | data_msg = (wifi_data_msg_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 244 | setNextCommand(data_msg->wifi_cmd); |
ocomeni | 81:637a87eb8170 | 245 | //_wiFi2ATDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 246 | } |
ocomeni | 81:637a87eb8170 | 247 | return true; |
ocomeni | 81:637a87eb8170 | 248 | } |
ocomeni | 81:637a87eb8170 | 249 | |
ocomeni | 81:637a87eb8170 | 250 | |
ocomeni | 79:a2187bbfa407 | 251 | bool WiFiManager::setNextCommand(wifi_cmd_t cmd) |
ocomeni | 78:07bb86e3ce14 | 252 | { |
ocomeni | 103:7b566b522427 | 253 | dbg_printf(LOG, "\n [WIFI-MAN] About to set next WiFi manager command to %d\n", cmd); |
ocomeni | 79:a2187bbfa407 | 254 | if(wifiCmd == WIFI_CMD_NONE){ |
ocomeni | 79:a2187bbfa407 | 255 | wifiCmd = cmd; |
ocomeni | 79:a2187bbfa407 | 256 | return true; // success |
ocomeni | 79:a2187bbfa407 | 257 | } |
ocomeni | 103:7b566b522427 | 258 | dbg_printf(LOG, "\n [WIFI-MAN] Busy : current state = %d \n", wifiCmd); |
ocomeni | 79:a2187bbfa407 | 259 | return false; // wiFiManager busy |
ocomeni | 78:07bb86e3ce14 | 260 | } |
ocomeni | 79:a2187bbfa407 | 261 | |
ocomeni | 81:637a87eb8170 | 262 | const char * WiFiManager::sec2str(nsapi_security_t sec) |
ocomeni | 81:637a87eb8170 | 263 | { |
ocomeni | 81:637a87eb8170 | 264 | switch (sec) { |
ocomeni | 81:637a87eb8170 | 265 | case NSAPI_SECURITY_NONE: |
ocomeni | 81:637a87eb8170 | 266 | return "None"; |
ocomeni | 81:637a87eb8170 | 267 | case NSAPI_SECURITY_WEP: |
ocomeni | 81:637a87eb8170 | 268 | return "WEP"; |
ocomeni | 81:637a87eb8170 | 269 | case NSAPI_SECURITY_WPA: |
ocomeni | 81:637a87eb8170 | 270 | return "WPA"; |
ocomeni | 81:637a87eb8170 | 271 | case NSAPI_SECURITY_WPA2: |
ocomeni | 81:637a87eb8170 | 272 | return "WPA2"; |
ocomeni | 81:637a87eb8170 | 273 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 81:637a87eb8170 | 274 | return "WPA/WPA2"; |
ocomeni | 81:637a87eb8170 | 275 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 81:637a87eb8170 | 276 | default: |
ocomeni | 81:637a87eb8170 | 277 | return "Unknown"; |
ocomeni | 81:637a87eb8170 | 278 | } |
ocomeni | 81:637a87eb8170 | 279 | } |
ocomeni | 81:637a87eb8170 | 280 | |
ocomeni | 79:a2187bbfa407 | 281 | |
ocomeni | 79:a2187bbfa407 | 282 | nsapi_size_or_error_t WiFiManager::scanNetworks() |
ocomeni | 79:a2187bbfa407 | 283 | { |
ocomeni | 79:a2187bbfa407 | 284 | nsapi_error_t error; |
ocomeni | 103:7b566b522427 | 285 | dbg_printf(LOG, "\n [WIFI-MAN] About to start scan for WiFi networks\n"); |
ocomeni | 79:a2187bbfa407 | 286 | lastScanCount = network->scan(NULL, 0); |
ocomeni | 103:7b566b522427 | 287 | dbg_printf(LOG, "\n [WIFI-MAN] Scan for WiFi networks completed - \n"); |
ocomeni | 79:a2187bbfa407 | 288 | return lastScanCount; |
ocomeni | 79:a2187bbfa407 | 289 | } |
ocomeni | 79:a2187bbfa407 | 290 | |
ocomeni | 79:a2187bbfa407 | 291 | |
ocomeni | 81:637a87eb8170 | 292 | //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, |
ocomeni | 81:637a87eb8170 | 293 | // nsapi_size_t ncount) |
ocomeni | 81:637a87eb8170 | 294 | nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount) |
ocomeni | 79:a2187bbfa407 | 295 | { |
ocomeni | 81:637a87eb8170 | 296 | WiFiAccessPoint *ap; |
ocomeni | 81:637a87eb8170 | 297 | nsapi_size_or_error_t count; |
ocomeni | 81:637a87eb8170 | 298 | count = ncount; |
ocomeni | 81:637a87eb8170 | 299 | //count = wiFiManager->scanNetworks(); |
ocomeni | 81:637a87eb8170 | 300 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 301 | //_smutex.lock(); |
ocomeni | 103:7b566b522427 | 302 | dbg_printf(LOG, "[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 303 | //_smutex.unlock(); |
ocomeni | 98:65c2333a38b6 | 304 | return 0; |
ocomeni | 81:637a87eb8170 | 305 | } |
ocomeni | 81:637a87eb8170 | 306 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 81:637a87eb8170 | 307 | count = count < 15 ? count : 15; |
ocomeni | 81:637a87eb8170 | 308 | ap = new WiFiAccessPoint[count]; |
ocomeni | 81:637a87eb8170 | 309 | count = network->scan(ap, count); |
ocomeni | 81:637a87eb8170 | 310 | if (count <= 0) { |
ocomeni | 103:7b566b522427 | 311 | dbg_printf(LOG, "[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 98:65c2333a38b6 | 312 | return 0; |
ocomeni | 81:637a87eb8170 | 313 | } |
ocomeni | 81:637a87eb8170 | 314 | |
ocomeni | 81:637a87eb8170 | 315 | for (int i = 0; i < count; i++) { |
ocomeni | 103:7b566b522427 | 316 | dbg_printf(LOG, "[WIFI-MAN]: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
ocomeni | 81:637a87eb8170 | 317 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 81:637a87eb8170 | 318 | 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 | 319 | } |
ocomeni | 103:7b566b522427 | 320 | dbg_printf(LOG, "[WIFI-MAN] %d networks available.\n", count); |
ocomeni | 81:637a87eb8170 | 321 | |
ocomeni | 81:637a87eb8170 | 322 | delete[] ap; |
ocomeni | 79:a2187bbfa407 | 323 | return count; |
ocomeni | 79:a2187bbfa407 | 324 | } |
ocomeni | 79:a2187bbfa407 | 325 | |
ocomeni | 79:a2187bbfa407 | 326 | |
ocomeni | 81:637a87eb8170 | 327 | void WiFiManager::set_WIFI_CONFIG() |
ocomeni | 81:637a87eb8170 | 328 | { |
ocomeni | 81:637a87eb8170 | 329 | wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer; |
ocomeni | 82:10072c1794d3 | 330 | if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid); |
ocomeni | 82:10072c1794d3 | 331 | if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass); |
ocomeni | 82:10072c1794d3 | 332 | if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security); |
ocomeni | 81:637a87eb8170 | 333 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 334 | } |
ocomeni | 81:637a87eb8170 | 335 | |
ocomeni | 78:07bb86e3ce14 | 336 | void WiFiManager::set_WIFI_SSID(char * wifi_ssid) |
ocomeni | 78:07bb86e3ce14 | 337 | { |
ocomeni | 78:07bb86e3ce14 | 338 | strcpy(wifi_config.ssid, wifi_ssid); |
ocomeni | 103:7b566b522427 | 339 | dbg_printf(LOG, "[WIFI-MAN] wifi_ssid set to %s\n", wifi_config.ssid); |
ocomeni | 88:7ffa053be662 | 340 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 341 | delete socket; |
ocomeni | 78:07bb86e3ce14 | 342 | } |
ocomeni | 79:a2187bbfa407 | 343 | |
ocomeni | 79:a2187bbfa407 | 344 | |
ocomeni | 78:07bb86e3ce14 | 345 | void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass) |
ocomeni | 78:07bb86e3ce14 | 346 | { |
ocomeni | 78:07bb86e3ce14 | 347 | strcpy(wifi_config.pass, wifi_pass); |
ocomeni | 103:7b566b522427 | 348 | dbg_printf(LOG, "[WIFI-MAN] wifi_pass set to %s\n", "****************"); |
ocomeni | 88:7ffa053be662 | 349 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 350 | delete socket; |
ocomeni | 78:07bb86e3ce14 | 351 | } |
ocomeni | 79:a2187bbfa407 | 352 | |
ocomeni | 79:a2187bbfa407 | 353 | |
ocomeni | 78:07bb86e3ce14 | 354 | void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security) |
ocomeni | 78:07bb86e3ce14 | 355 | { |
ocomeni | 78:07bb86e3ce14 | 356 | wifi_config.security = wifi_security; |
ocomeni | 103:7b566b522427 | 357 | dbg_printf(LOG, "[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config.security)); |
ocomeni | 88:7ffa053be662 | 358 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 359 | delete socket; |
ocomeni | 78:07bb86e3ce14 | 360 | } |
ocomeni | 79:a2187bbfa407 | 361 | |
ocomeni | 100:80ef4bc31b7a | 362 | void WiFiManager::gethostbyname() |
ocomeni | 100:80ef4bc31b7a | 363 | { |
ocomeni | 100:80ef4bc31b7a | 364 | nsapi_value_or_error_t value_or_error; |
ocomeni | 100:80ef4bc31b7a | 365 | value_or_error = network->gethostbyname_async(internet_config.url, |
ocomeni | 100:80ef4bc31b7a | 366 | callback(this, &WiFiManager::gethostbyname_callback), |
ocomeni | 100:80ef4bc31b7a | 367 | NSAPI_UNSPEC); |
ocomeni | 100:80ef4bc31b7a | 368 | if(value_or_error >= NSAPI_ERROR_OK) // success |
ocomeni | 100:80ef4bc31b7a | 369 | { |
ocomeni | 103:7b566b522427 | 370 | dbg_printf(LOG, "[WIFI-MAN] hostname translation successful value_or_error = %d\r\n", value_or_error); |
ocomeni | 100:80ef4bc31b7a | 371 | //strcpy(responseString, UDDRP_WRITE_OK); |
ocomeni | 100:80ef4bc31b7a | 372 | //printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN); |
ocomeni | 100:80ef4bc31b7a | 373 | //sendATresponseBytes(CONNECT_EVENT, HOSTNAME_RESPONSE_LEN); |
ocomeni | 100:80ef4bc31b7a | 374 | } |
ocomeni | 100:80ef4bc31b7a | 375 | else // -ve number means error |
ocomeni | 100:80ef4bc31b7a | 376 | { |
ocomeni | 100:80ef4bc31b7a | 377 | responseString = (char *) malloc(20); |
ocomeni | 103:7b566b522427 | 378 | dbg_printf(LOG, "[WIFI-MAN] hostname translation failed\r\n"); |
ocomeni | 100:80ef4bc31b7a | 379 | strcpy(responseString, UDDRP_ERROR); |
ocomeni | 100:80ef4bc31b7a | 380 | sendATresponseString(AT_COMMAND_FAILED); |
ocomeni | 100:80ef4bc31b7a | 381 | } |
ocomeni | 81:637a87eb8170 | 382 | |
ocomeni | 100:80ef4bc31b7a | 383 | } |
ocomeni | 100:80ef4bc31b7a | 384 | |
ocomeni | 100:80ef4bc31b7a | 385 | void WiFiManager::set_internet_config() |
ocomeni | 81:637a87eb8170 | 386 | { |
ocomeni | 81:637a87eb8170 | 387 | internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer; |
ocomeni | 81:637a87eb8170 | 388 | internet_config.peer_id = internet_cfg->peer_id; |
ocomeni | 84:7c7add00f4bf | 389 | strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1); |
ocomeni | 81:637a87eb8170 | 390 | internet_config.connectionScheme = internet_cfg->connectionScheme; |
ocomeni | 81:637a87eb8170 | 391 | free_DataMsg(); |
ocomeni | 103:7b566b522427 | 392 | dbg_printf(LOG, "[WIFI MAN] Internet configuration setup completed\n"); |
ocomeni | 103:7b566b522427 | 393 | dbg_printf(LOG, "peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, |
ocomeni | 84:7c7add00f4bf | 394 | internet_config.url, |
ocomeni | 81:637a87eb8170 | 395 | internet_config.connectionScheme); |
ocomeni | 99:05398b3184f8 | 396 | if(https_connection_active) |
ocomeni | 99:05398b3184f8 | 397 | { |
ocomeni | 99:05398b3184f8 | 398 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 99:05398b3184f8 | 399 | socket->close(); // close socket before deleting memory |
ocomeni | 99:05398b3184f8 | 400 | delete socket; |
ocomeni | 99:05398b3184f8 | 401 | socket = NULL; |
ocomeni | 99:05398b3184f8 | 402 | } |
ocomeni | 100:80ef4bc31b7a | 403 | _event_queue.call_in(10, this, &WiFiManager::gethostbyname); |
ocomeni | 81:637a87eb8170 | 404 | } |
ocomeni | 81:637a87eb8170 | 405 | |
ocomeni | 95:290859010c8c | 406 | |
ocomeni | 95:290859010c8c | 407 | |
ocomeni | 95:290859010c8c | 408 | void WiFiManager::getNetworkStatus(){ |
ocomeni | 95:290859010c8c | 409 | |
ocomeni | 95:290859010c8c | 410 | responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); |
ocomeni | 95:290859010c8c | 411 | net_stat_id_t status_id; |
ocomeni | 95:290859010c8c | 412 | char * nextStrPtr = responseString; |
ocomeni | 95:290859010c8c | 413 | for(int i=0; i< NumNetworkStatus;i++){ |
ocomeni | 95:290859010c8c | 414 | status_id = netStatusIds[i]; // get current status id |
ocomeni | 95:290859010c8c | 415 | switch(status_id){ |
ocomeni | 95:290859010c8c | 416 | case IF_HW_ADDRESS: |
ocomeni | 95:290859010c8c | 417 | sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 418 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 419 | status_id, |
ocomeni | 95:290859010c8c | 420 | network->get_mac_address()); |
ocomeni | 95:290859010c8c | 421 | break; |
ocomeni | 95:290859010c8c | 422 | case NETWORK_IF_STATUS: |
ocomeni | 95:290859010c8c | 423 | sprintf(nextStrPtr, "\r\n%s%d,%d, %d\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 424 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 425 | status_id, |
ocomeni | 95:290859010c8c | 426 | (uint8_t)is_connected); |
ocomeni | 95:290859010c8c | 427 | break; |
ocomeni | 95:290859010c8c | 428 | case INTERFACE_TYPE: |
ocomeni | 95:290859010c8c | 429 | sprintf(nextStrPtr, "\r\n%s%d,%d,%d\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 430 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 431 | status_id, |
ocomeni | 95:290859010c8c | 432 | WIFI_STATION); |
ocomeni | 95:290859010c8c | 433 | break; |
ocomeni | 95:290859010c8c | 434 | case IPv4_ADDRESS: |
ocomeni | 95:290859010c8c | 435 | sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 436 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 437 | status_id, |
ocomeni | 95:290859010c8c | 438 | network->get_ip_address()); |
ocomeni | 95:290859010c8c | 439 | break; |
ocomeni | 95:290859010c8c | 440 | case SUBNET_MASK: |
ocomeni | 95:290859010c8c | 441 | sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 442 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 443 | status_id, |
ocomeni | 95:290859010c8c | 444 | network->get_netmask()); |
ocomeni | 95:290859010c8c | 445 | break; |
ocomeni | 95:290859010c8c | 446 | case GATEWAY_ADDRESS: |
ocomeni | 95:290859010c8c | 447 | sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 448 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 449 | status_id, |
ocomeni | 95:290859010c8c | 450 | network->get_gateway()); |
ocomeni | 95:290859010c8c | 451 | break; |
ocomeni | 95:290859010c8c | 452 | case PRIMARY_DNS_SERVER: |
ocomeni | 95:290859010c8c | 453 | sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 454 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 455 | status_id, |
ocomeni | 95:290859010c8c | 456 | DEFAULT_DNS_ADDRESS); |
ocomeni | 95:290859010c8c | 457 | break; |
ocomeni | 95:290859010c8c | 458 | case SECONDARY_DNS_SERVER: |
ocomeni | 95:290859010c8c | 459 | sprintf(nextStrPtr, "\r\n%s%d,%d,%s\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 460 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 461 | status_id, |
ocomeni | 95:290859010c8c | 462 | DEFAULT_DNS_ADDRESS); |
ocomeni | 95:290859010c8c | 463 | break; |
ocomeni | 95:290859010c8c | 464 | case IPv6_ADDRESS: |
ocomeni | 95:290859010c8c | 465 | sprintf(nextStrPtr, "\r\n%s%d,%d,::\r\n", NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 466 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 467 | status_id); |
ocomeni | 95:290859010c8c | 468 | break; |
ocomeni | 95:290859010c8c | 469 | default: |
ocomeni | 95:290859010c8c | 470 | sprintf(nextStrPtr, "\r\n%s,::\r\n", NETWORK_STATUS); |
ocomeni | 95:290859010c8c | 471 | break; |
ocomeni | 95:290859010c8c | 472 | } |
ocomeni | 103:7b566b522427 | 473 | nextStrPtr += strlen(nextStrPtr) ; // progress to end of current string |
ocomeni | 95:290859010c8c | 474 | } |
ocomeni | 103:7b566b522427 | 475 | sprintf(nextStrPtr, "%s", UDDRP_WRITE_OK); |
ocomeni | 95:290859010c8c | 476 | } |
ocomeni | 95:290859010c8c | 477 | |
ocomeni | 95:290859010c8c | 478 | |
ocomeni | 95:290859010c8c | 479 | |
ocomeni | 95:290859010c8c | 480 | void WiFiManager::getWiFiStatus(){ |
ocomeni | 95:290859010c8c | 481 | |
ocomeni | 95:290859010c8c | 482 | responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); |
ocomeni | 95:290859010c8c | 483 | wifi_stat_id_t status_id; |
ocomeni | 95:290859010c8c | 484 | char * nextStrPtr = responseString; |
ocomeni | 95:290859010c8c | 485 | for(int i=0; i< NumWiFiStatus;i++){ |
ocomeni | 95:290859010c8c | 486 | status_id = wifiStatusIds[i]; // get current status id |
ocomeni | 95:290859010c8c | 487 | switch(status_id){ |
ocomeni | 95:290859010c8c | 488 | case WIFI_SSID: |
ocomeni | 95:290859010c8c | 489 | sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 490 | status_id, |
ocomeni | 95:290859010c8c | 491 | wifi_config.ssid); |
ocomeni | 95:290859010c8c | 492 | break; |
ocomeni | 95:290859010c8c | 493 | case WIFI_BSSID: |
ocomeni | 95:290859010c8c | 494 | sprintf(nextStrPtr, "\r\n%s%d,%s\r\n", WIFI_NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 495 | status_id, |
ocomeni | 95:290859010c8c | 496 | network->get_mac_address()); |
ocomeni | 95:290859010c8c | 497 | break; |
ocomeni | 95:290859010c8c | 498 | case WIFI__CURRENT_CHANNEL: |
ocomeni | 95:290859010c8c | 499 | sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 500 | status_id, |
ocomeni | 95:290859010c8c | 501 | DEFAULT_WIFI_CHANNEL); |
ocomeni | 95:290859010c8c | 502 | break; |
ocomeni | 95:290859010c8c | 503 | case WIFI_STA_STATUS: |
ocomeni | 95:290859010c8c | 504 | sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 505 | status_id, |
ocomeni | 95:290859010c8c | 506 | (uint8_t)is_connected); |
ocomeni | 95:290859010c8c | 507 | break; |
ocomeni | 95:290859010c8c | 508 | case WIFI_RSSI: |
ocomeni | 95:290859010c8c | 509 | sprintf(nextStrPtr, "\r\n%s%d,%d\r\n", WIFI_NETWORK_STATUS, |
ocomeni | 95:290859010c8c | 510 | status_id, |
ocomeni | 95:290859010c8c | 511 | network->get_rssi()); |
ocomeni | 95:290859010c8c | 512 | break; |
ocomeni | 95:290859010c8c | 513 | default: |
ocomeni | 95:290859010c8c | 514 | sprintf(nextStrPtr, "\r\n%s,::\r\n", WIFI_NETWORK_STATUS); |
ocomeni | 95:290859010c8c | 515 | break; |
ocomeni | 95:290859010c8c | 516 | } |
ocomeni | 103:7b566b522427 | 517 | nextStrPtr += strlen(nextStrPtr) ; // progress to end of current string |
ocomeni | 95:290859010c8c | 518 | } |
ocomeni | 103:7b566b522427 | 519 | sprintf(nextStrPtr, "%s", UDDRP_WRITE_OK); |
ocomeni | 95:290859010c8c | 520 | } |
ocomeni | 95:290859010c8c | 521 | |
ocomeni | 95:290859010c8c | 522 | |
ocomeni | 81:637a87eb8170 | 523 | void WiFiManager::free_DataMsg() |
ocomeni | 81:637a87eb8170 | 524 | { |
ocomeni | 81:637a87eb8170 | 525 | // free memory after processing |
ocomeni | 81:637a87eb8170 | 526 | _aT2WiFiDatamPool->free(data_msg); |
ocomeni | 96:f5ed273881af | 527 | data_msg = NULL; |
ocomeni | 81:637a87eb8170 | 528 | } |
ocomeni | 81:637a87eb8170 | 529 | |
ocomeni | 81:637a87eb8170 | 530 | |
ocomeni | 88:7ffa053be662 | 531 | |
ocomeni | 81:637a87eb8170 | 532 | void WiFiManager::status_callback(nsapi_event_t status, intptr_t param) |
ocomeni | 81:637a87eb8170 | 533 | { |
ocomeni | 103:7b566b522427 | 534 | dbg_printf(LOG, "[WIFI-MAN] about call callback... \r\n"); |
ocomeni | 98:65c2333a38b6 | 535 | _event_queue.call_in(50, this, &WiFiManager::status_callback_event, status, param); |
ocomeni | 98:65c2333a38b6 | 536 | //status_callback_event(status, param); |
ocomeni | 98:65c2333a38b6 | 537 | } |
ocomeni | 98:65c2333a38b6 | 538 | void WiFiManager::status_callback_event(nsapi_event_t status, intptr_t param) |
ocomeni | 98:65c2333a38b6 | 539 | { |
ocomeni | 81:637a87eb8170 | 540 | //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) { |
ocomeni | 81:637a87eb8170 | 541 | //} |
ocomeni | 81:637a87eb8170 | 542 | switch(param) { |
ocomeni | 81:637a87eb8170 | 543 | case NSAPI_STATUS_LOCAL_UP: |
ocomeni | 103:7b566b522427 | 544 | dbg_printf(LOG, "[WIFI-MAN] Local IP address set!\r\n"); |
ocomeni | 103:7b566b522427 | 545 | dbg_printf(LOG, "[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 546 | break; |
ocomeni | 81:637a87eb8170 | 547 | case NSAPI_STATUS_GLOBAL_UP: |
ocomeni | 103:7b566b522427 | 548 | dbg_printf(LOG, "Global IP address set!\r\n"); |
ocomeni | 103:7b566b522427 | 549 | dbg_printf(LOG, "[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 103:7b566b522427 | 550 | dbg_printf(LOG, "[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid); |
ocomeni | 95:290859010c8c | 551 | responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); |
ocomeni | 95:290859010c8c | 552 | sprintf(responseString, "\r\n%s%d,%s,%d\r\n", WIFI_LINK_ENABLED, |
ocomeni | 95:290859010c8c | 553 | WIFI_CHANNEL, |
ocomeni | 95:290859010c8c | 554 | network->get_mac_address(), |
ocomeni | 95:290859010c8c | 555 | DEFAULT_WIFI_CHANNEL); |
ocomeni | 95:290859010c8c | 556 | |
ocomeni | 81:637a87eb8170 | 557 | is_connected = true; |
ocomeni | 81:637a87eb8170 | 558 | break; |
ocomeni | 81:637a87eb8170 | 559 | case NSAPI_STATUS_DISCONNECTED: |
ocomeni | 103:7b566b522427 | 560 | dbg_printf(LOG, "No connection to network!\r\n"); |
ocomeni | 103:7b566b522427 | 561 | dbg_printf(LOG, "\n [WIFI-MAN] No connection to network!\n"); |
ocomeni | 81:637a87eb8170 | 562 | is_connected = false; |
ocomeni | 81:637a87eb8170 | 563 | //queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 564 | // attempt reconnection if always connected scheme is set |
ocomeni | 81:637a87eb8170 | 565 | if(internet_config.connectionScheme == ALWAYS_CONNECTED) |
ocomeni | 81:637a87eb8170 | 566 | { |
ocomeni | 81:637a87eb8170 | 567 | nsapi_error_t error; |
ocomeni | 91:d6b6319ad681 | 568 | error = connect(); |
ocomeni | 81:637a87eb8170 | 569 | queueATresponse(WIFI_RECONNECT_INFO); |
ocomeni | 81:637a87eb8170 | 570 | } |
ocomeni | 81:637a87eb8170 | 571 | break; |
ocomeni | 81:637a87eb8170 | 572 | case NSAPI_STATUS_CONNECTING: |
ocomeni | 103:7b566b522427 | 573 | dbg_printf(LOG, "Connecting to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 574 | break; |
ocomeni | 81:637a87eb8170 | 575 | default: |
ocomeni | 103:7b566b522427 | 576 | dbg_printf(LOG, "Not supported"); |
ocomeni | 81:637a87eb8170 | 577 | break; |
ocomeni | 81:637a87eb8170 | 578 | } |
ocomeni | 81:637a87eb8170 | 579 | } |
ocomeni | 81:637a87eb8170 | 580 | |
ocomeni | 81:637a87eb8170 | 581 | |
ocomeni | 81:637a87eb8170 | 582 | |
ocomeni | 79:a2187bbfa407 | 583 | // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */ |
ocomeni | 79:a2187bbfa407 | 584 | // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */ |
ocomeni | 79:a2187bbfa407 | 585 | // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */ |
ocomeni | 79:a2187bbfa407 | 586 | // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */ |
ocomeni | 79:a2187bbfa407 | 587 | // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED |
ocomeni | 79:a2187bbfa407 | 588 | |
ocomeni | 79:a2187bbfa407 | 589 | nsapi_error_t WiFiManager::connect() |
ocomeni | 79:a2187bbfa407 | 590 | { |
ocomeni | 79:a2187bbfa407 | 591 | nsapi_error_t error; |
ocomeni | 103:7b566b522427 | 592 | dbg_printf(LOG, "\n [WIFI-MAN] About to connect to WiFi network\n"); |
ocomeni | 81:637a87eb8170 | 593 | network->attach(callback(this, &WiFiManager::status_callback)); |
ocomeni | 79:a2187bbfa407 | 594 | error = network->set_blocking(false); |
ocomeni | 79:a2187bbfa407 | 595 | if(error) |
ocomeni | 79:a2187bbfa407 | 596 | { |
ocomeni | 103:7b566b522427 | 597 | dbg_printf(LOG, "\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n"); |
ocomeni | 79:a2187bbfa407 | 598 | return error; |
ocomeni | 79:a2187bbfa407 | 599 | } |
ocomeni | 103:7b566b522427 | 600 | dbg_printf(LOG, "[WIFI-MAN] Connecting to network ssid = %s passwd = %s security = %s \r\n", |
ocomeni | 93:06e755a80187 | 601 | wifi_config.ssid, |
ocomeni | 96:f5ed273881af | 602 | "****************", |
ocomeni | 93:06e755a80187 | 603 | sec2str(wifi_config.security)); |
ocomeni | 79:a2187bbfa407 | 604 | error = network->connect(wifi_config.ssid, |
ocomeni | 79:a2187bbfa407 | 605 | wifi_config.pass, |
ocomeni | 79:a2187bbfa407 | 606 | wifi_config.security); |
ocomeni | 103:7b566b522427 | 607 | dbg_printf(LOG, "[WIFI-MAN] network->connect called. error = %d\r\n", error); |
ocomeni | 81:637a87eb8170 | 608 | return error; |
ocomeni | 79:a2187bbfa407 | 609 | } |
ocomeni | 79:a2187bbfa407 | 610 | |
ocomeni | 79:a2187bbfa407 | 611 | |
ocomeni | 99:05398b3184f8 | 612 | void WiFiManager::gethostbyname_callback(nsapi_error_t result, SocketAddress *address) |
ocomeni | 99:05398b3184f8 | 613 | { |
ocomeni | 103:7b566b522427 | 614 | dbg_printf(LOG, "gethostbyname_callback called... result = %d \r\n", result); |
ocomeni | 99:05398b3184f8 | 615 | responseBytes = (uint8_t *) malloc(HOSTNAME_RESPONSE_LEN); |
ocomeni | 99:05398b3184f8 | 616 | int i = 0; |
ocomeni | 99:05398b3184f8 | 617 | responseBytes[i++] = IPv4_CONNECTION; // connect type IPv4 |
ocomeni | 99:05398b3184f8 | 618 | responseBytes[i++] = TCP_PROTOCOL; // Protocol = TCP |
ocomeni | 99:05398b3184f8 | 619 | memcpy(&responseBytes[i], address->get_ip_bytes(), 4); // remote IPv4 address |
ocomeni | 100:80ef4bc31b7a | 620 | strcpy(internet_config.remote_IPv4Address, address->get_ip_address()); |
ocomeni | 99:05398b3184f8 | 621 | i +=4; |
ocomeni | 100:80ef4bc31b7a | 622 | uint16_t port = address->get_port(); |
ocomeni | 100:80ef4bc31b7a | 623 | internet_config.remote_port = port; |
ocomeni | 99:05398b3184f8 | 624 | memcpy(&responseBytes[i], &port, 2); // remote IPv4 port # |
ocomeni | 99:05398b3184f8 | 625 | i +=2; |
ocomeni | 99:05398b3184f8 | 626 | if(is_connected) |
ocomeni | 99:05398b3184f8 | 627 | { |
ocomeni | 99:05398b3184f8 | 628 | // local IPv4 address |
ocomeni | 99:05398b3184f8 | 629 | int ipAddr[4]; |
ocomeni | 100:80ef4bc31b7a | 630 | strcpy(internet_config.local_IPv4Address, network->get_ip_address()); |
ocomeni | 100:80ef4bc31b7a | 631 | sscanf(internet_config.local_IPv4Address, "%d.%d.%d.%d", &ipAddr[0], &ipAddr[1], |
ocomeni | 100:80ef4bc31b7a | 632 | &ipAddr[2], &ipAddr[3]); |
ocomeni | 99:05398b3184f8 | 633 | responseBytes[i++] = (uint8_t) ipAddr[0]; |
ocomeni | 99:05398b3184f8 | 634 | responseBytes[i++] = (uint8_t) ipAddr[1]; |
ocomeni | 99:05398b3184f8 | 635 | responseBytes[i++] = (uint8_t) ipAddr[2]; |
ocomeni | 99:05398b3184f8 | 636 | responseBytes[i++] = (uint8_t) ipAddr[3]; |
ocomeni | 99:05398b3184f8 | 637 | // local port number |
ocomeni | 99:05398b3184f8 | 638 | responseBytes[i++] = 0; |
ocomeni | 99:05398b3184f8 | 639 | responseBytes[i] = 0; |
ocomeni | 99:05398b3184f8 | 640 | printBufferInHex(responseBytes, HOSTNAME_RESPONSE_LEN); |
ocomeni | 99:05398b3184f8 | 641 | } |
ocomeni | 99:05398b3184f8 | 642 | else |
ocomeni | 99:05398b3184f8 | 643 | { |
ocomeni | 99:05398b3184f8 | 644 | // if unconnected set ip and port to zeroes |
ocomeni | 99:05398b3184f8 | 645 | memset(&responseBytes[i], 0x00, 6); |
ocomeni | 99:05398b3184f8 | 646 | } |
ocomeni | 100:80ef4bc31b7a | 647 | _event_queue.call_in(2, this, &WiFiManager::sendATresponseBytes, |
ocomeni | 100:80ef4bc31b7a | 648 | CONNECT_EVENT, HOSTNAME_RESPONSE_LEN); |
ocomeni | 99:05398b3184f8 | 649 | } |
ocomeni | 99:05398b3184f8 | 650 | |
ocomeni | 100:80ef4bc31b7a | 651 | void WiFiManager::sendSocketConnectionEvent() |
ocomeni | 100:80ef4bc31b7a | 652 | { |
ocomeni | 100:80ef4bc31b7a | 653 | // |
ocomeni | 100:80ef4bc31b7a | 654 | responseString = (char *) malloc(MAX_RESPONSE_STRING_LEN); |
ocomeni | 100:80ef4bc31b7a | 655 | sprintf(responseString, "\r\n%s%d,%d,%d,%s,%d,%s,%d\r\n", PEER_CONNECTED_URC, |
ocomeni | 100:80ef4bc31b7a | 656 | IP_PEER_HANDLE, |
ocomeni | 100:80ef4bc31b7a | 657 | IPv4_CONNECTION, |
ocomeni | 100:80ef4bc31b7a | 658 | TCP_PROTOCOL, |
ocomeni | 100:80ef4bc31b7a | 659 | internet_config.local_IPv4Address, |
ocomeni | 100:80ef4bc31b7a | 660 | DEFAULT_LOCAL_PORT, |
ocomeni | 100:80ef4bc31b7a | 661 | internet_config.remote_IPv4Address, |
ocomeni | 100:80ef4bc31b7a | 662 | internet_config.remote_port); |
ocomeni | 100:80ef4bc31b7a | 663 | sendATresponseString(AT_EVENT); |
ocomeni | 100:80ef4bc31b7a | 664 | } |
ocomeni | 100:80ef4bc31b7a | 665 | |
ocomeni | 99:05398b3184f8 | 666 | |
ocomeni | 79:a2187bbfa407 | 667 | nsapi_error_t WiFiManager::disconnect() |
ocomeni | 78:07bb86e3ce14 | 668 | { |
ocomeni | 79:a2187bbfa407 | 669 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 670 | error = network->disconnect(); |
ocomeni | 79:a2187bbfa407 | 671 | return error; |
ocomeni | 78:07bb86e3ce14 | 672 | } |
ocomeni | 79:a2187bbfa407 | 673 | |
ocomeni | 98:65c2333a38b6 | 674 | #define MIX_HDR_AND_BODY |
ocomeni | 88:7ffa053be662 | 675 | void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen) |
ocomeni | 90:ed0267eca7b5 | 676 | { |
ocomeni | 90:ed0267eca7b5 | 677 | |
ocomeni | 103:7b566b522427 | 678 | //dbg_printf(LOG, "before call to new at_data_msg_t \n"); |
ocomeni | 98:65c2333a38b6 | 679 | //print_memory_info(); |
ocomeni | 88:7ffa053be662 | 680 | at_data_resp = new at_data_msg_t; |
ocomeni | 103:7b566b522427 | 681 | //dbg_printf(LOG, "after call to new at_data_msg_t \n"); |
ocomeni | 98:65c2333a38b6 | 682 | //print_memory_info(); |
ocomeni | 88:7ffa053be662 | 683 | at_data_resp->at_resp = at_cmd; |
ocomeni | 88:7ffa053be662 | 684 | size_t bufSize = sizeof(at_data_resp->buffer); |
ocomeni | 88:7ffa053be662 | 685 | int pos = 0; |
ocomeni | 88:7ffa053be662 | 686 | at_data_resp->dataLen = 0; |
ocomeni | 88:7ffa053be662 | 687 | bool queueResult = true; |
ocomeni | 90:ed0267eca7b5 | 688 | int hdrLen = 0; |
ocomeni | 94:fb4414aff957 | 689 | int wait_count = 0; |
ocomeni | 88:7ffa053be662 | 690 | do { |
ocomeni | 94:fb4414aff957 | 691 | if(!queueResult){ |
ocomeni | 94:fb4414aff957 | 692 | wait_count++; |
ocomeni | 103:7b566b522427 | 693 | dbg_printf(LOG, "[WIFI-MAN] ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 94:fb4414aff957 | 694 | wait_ms(10); |
ocomeni | 90:ed0267eca7b5 | 695 | } |
ocomeni | 90:ed0267eca7b5 | 696 | else { |
ocomeni | 98:65c2333a38b6 | 697 | if(http_response_hdr_sent == false && chunkNum==1){ // only do this for first chunk |
ocomeni | 102:9748f290a1a5 | 698 | bool status = copyResponseHdr2Queue(buf); |
ocomeni | 98:65c2333a38b6 | 699 | if(status == true){ |
ocomeni | 103:7b566b522427 | 700 | dbg_printf(LOG, "[WIFI-MAN] Http Response header copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen); |
ocomeni | 98:65c2333a38b6 | 701 | hdrLen = at_data_resp->dataLen; |
ocomeni | 98:65c2333a38b6 | 702 | http_response_hdr_sent = true; |
ocomeni | 98:65c2333a38b6 | 703 | } |
ocomeni | 98:65c2333a38b6 | 704 | else { |
ocomeni | 103:7b566b522427 | 705 | dbg_printf(LOG, "[WIFI-MAN] Http Response header copy failed\r\n"); |
ocomeni | 98:65c2333a38b6 | 706 | } |
ocomeni | 90:ed0267eca7b5 | 707 | } |
ocomeni | 94:fb4414aff957 | 708 | int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos) ; |
ocomeni | 103:7b566b522427 | 709 | dbg_printf(LOG, "[WIFI-MAN] Http Response body [bytes = %d] \r\n",cpyLen); |
ocomeni | 90:ed0267eca7b5 | 710 | at_data_resp->dataLen += cpyLen; |
ocomeni | 90:ed0267eca7b5 | 711 | memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen); |
ocomeni | 103:7b566b522427 | 712 | dbg_printf(LOG, "[WIFI-MAN] Http Response header and body copied to response buffer [bytes = %d] \r\n",at_data_resp->dataLen); |
ocomeni | 90:ed0267eca7b5 | 713 | } |
ocomeni | 88:7ffa053be662 | 714 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 90:ed0267eca7b5 | 715 | if(queueResult){ |
ocomeni | 90:ed0267eca7b5 | 716 | pos+= at_data_resp->dataLen; |
ocomeni | 90:ed0267eca7b5 | 717 | at_data_resp->dataLen = 0; |
ocomeni | 90:ed0267eca7b5 | 718 | hdrLen = 0; |
ocomeni | 90:ed0267eca7b5 | 719 | } |
ocomeni | 88:7ffa053be662 | 720 | }while(queueResult == false || pos < bufLen); |
ocomeni | 103:7b566b522427 | 721 | dbg_printf(LOG, "[WIFI-MAN] response data queued - deleting data memory\r\n"); |
ocomeni | 88:7ffa053be662 | 722 | delete at_data_resp; |
ocomeni | 100:80ef4bc31b7a | 723 | at_data_resp = NULL; |
ocomeni | 88:7ffa053be662 | 724 | } |
ocomeni | 79:a2187bbfa407 | 725 | |
ocomeni | 102:9748f290a1a5 | 726 | bool WiFiManager::copyResponseHdr2Queue(const uint8_t * buf) |
ocomeni | 90:ed0267eca7b5 | 727 | { |
ocomeni | 102:9748f290a1a5 | 728 | const char *arbPtr = (const char *)buf - MAX_HTTP_HDR_LEN; |
ocomeni | 102:9748f290a1a5 | 729 | const char *hdrPtr; |
ocomeni | 102:9748f290a1a5 | 730 | int len; |
ocomeni | 102:9748f290a1a5 | 731 | bool hdrFound = false; |
ocomeni | 102:9748f290a1a5 | 732 | int i; |
ocomeni | 102:9748f290a1a5 | 733 | for(i=0;i<(MAX_HTTP_HDR_LEN-50);i++) |
ocomeni | 101:1cfd468e5009 | 734 | { |
ocomeni | 102:9748f290a1a5 | 735 | // get location of start of the http header string |
ocomeni | 102:9748f290a1a5 | 736 | hdrPtr = strstr(&arbPtr[i], HTTP_HEADER_START_LINE); |
ocomeni | 102:9748f290a1a5 | 737 | len = strlen(HTTP_HEADER_START_LINE); |
ocomeni | 102:9748f290a1a5 | 738 | // validate that header string |
ocomeni | 102:9748f290a1a5 | 739 | if((strstr(&arbPtr[i+len], HTTP_HEADER_START_LINE) == NULL || |
ocomeni | 102:9748f290a1a5 | 740 | (strstr(&arbPtr[i+len], HTTP_HEADER_START_LINE) > (const char*)buf)) && // |
ocomeni | 102:9748f290a1a5 | 741 | strstr(&arbPtr[i+len], HTTP_HEADER_CONTENT_TYPE) != NULL && |
ocomeni | 102:9748f290a1a5 | 742 | strstr(&arbPtr[i+len], HTTP_HEADER_CONTENT_LEN) != NULL && |
ocomeni | 102:9748f290a1a5 | 743 | hdrPtr != NULL) |
ocomeni | 102:9748f290a1a5 | 744 | { |
ocomeni | 102:9748f290a1a5 | 745 | hdrFound = true; |
ocomeni | 102:9748f290a1a5 | 746 | break; |
ocomeni | 102:9748f290a1a5 | 747 | } |
ocomeni | 101:1cfd468e5009 | 748 | } |
ocomeni | 102:9748f290a1a5 | 749 | // calculate header length |
ocomeni | 102:9748f290a1a5 | 750 | int hdrLen = (const char*) buf -hdrPtr; |
ocomeni | 102:9748f290a1a5 | 751 | // copy header |
ocomeni | 102:9748f290a1a5 | 752 | memcpy(at_data_resp->buffer, (const uint8_t *) hdrPtr, hdrLen); |
ocomeni | 103:7b566b522427 | 753 | dbg_printf(LOG, "[i = %d] This is the header\r\n%s\r\n", i, hdrPtr); |
ocomeni | 102:9748f290a1a5 | 754 | if(hdrFound == false) |
ocomeni | 100:80ef4bc31b7a | 755 | { |
ocomeni | 103:7b566b522427 | 756 | dbg_printf(LOG, "[WIFI-MAN] copy failed: HTTP header not found!!\r\n"); |
ocomeni | 100:80ef4bc31b7a | 757 | return false; |
ocomeni | 100:80ef4bc31b7a | 758 | } |
ocomeni | 102:9748f290a1a5 | 759 | at_data_resp->dataLen = hdrLen; |
ocomeni | 98:65c2333a38b6 | 760 | return true; |
ocomeni | 90:ed0267eca7b5 | 761 | } |
ocomeni | 90:ed0267eca7b5 | 762 | |
ocomeni | 84:7c7add00f4bf | 763 | void WiFiManager::return_response(HttpResponse* res) { |
ocomeni | 84:7c7add00f4bf | 764 | |
ocomeni | 88:7ffa053be662 | 765 | at_data_resp = new at_data_msg_t; |
ocomeni | 87:99b37d26ff2a | 766 | int numChars = 0; |
ocomeni | 87:99b37d26ff2a | 767 | // create message pointer for response header generation |
ocomeni | 88:7ffa053be662 | 768 | char * msgPtr = (char *)at_data_resp->buffer; |
ocomeni | 87:99b37d26ff2a | 769 | // do status line |
ocomeni | 87:99b37d26ff2a | 770 | numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str()); |
ocomeni | 87:99b37d26ff2a | 771 | msgPtr += numChars; |
ocomeni | 84:7c7add00f4bf | 772 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
ocomeni | 87:99b37d26ff2a | 773 | numChars = sprintf(msgPtr, "%s: %s\r\n", |
ocomeni | 87:99b37d26ff2a | 774 | res->get_headers_fields()[ix]->c_str(), |
ocomeni | 87:99b37d26ff2a | 775 | res->get_headers_values()[ix]->c_str()); |
ocomeni | 87:99b37d26ff2a | 776 | msgPtr += numChars; |
ocomeni | 84:7c7add00f4bf | 777 | } |
ocomeni | 87:99b37d26ff2a | 778 | numChars = sprintf(msgPtr, "\r\n\r\n"); |
ocomeni | 87:99b37d26ff2a | 779 | msgPtr += numChars; |
ocomeni | 87:99b37d26ff2a | 780 | // print out generated header |
ocomeni | 103:7b566b522427 | 781 | dbg_printf(LOG, "[WiFi MAN] generated response header:\n"); |
ocomeni | 103:7b566b522427 | 782 | dbg_printf(LOG, "%s\r\n", (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 783 | // calculate header length |
ocomeni | 89:45f6db09a76d | 784 | at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer); |
ocomeni | 84:7c7add00f4bf | 785 | |
ocomeni | 90:ed0267eca7b5 | 786 | // package and send on wifi data queue |
ocomeni | 88:7ffa053be662 | 787 | at_data_resp->at_resp = AT_HTTPS_RESP; |
ocomeni | 89:45f6db09a76d | 788 | bool queueResult = true; |
ocomeni | 89:45f6db09a76d | 789 | int wait_count = 0; |
ocomeni | 89:45f6db09a76d | 790 | do |
ocomeni | 89:45f6db09a76d | 791 | { |
ocomeni | 89:45f6db09a76d | 792 | if(!queueResult){ |
ocomeni | 89:45f6db09a76d | 793 | wait_count++; |
ocomeni | 103:7b566b522427 | 794 | dbg_printf(LOG, "ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 89:45f6db09a76d | 795 | wait_ms(10); |
ocomeni | 89:45f6db09a76d | 796 | } |
ocomeni | 89:45f6db09a76d | 797 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 89:45f6db09a76d | 798 | }while(queueResult == false); |
ocomeni | 88:7ffa053be662 | 799 | delete at_data_resp; |
ocomeni | 100:80ef4bc31b7a | 800 | at_data_resp = NULL; |
ocomeni | 84:7c7add00f4bf | 801 | } |
ocomeni | 90:ed0267eca7b5 | 802 | |
ocomeni | 90:ed0267eca7b5 | 803 | |
ocomeni | 90:ed0267eca7b5 | 804 | void WiFiManager::printBufferInHex(uint8_t *buf, int pLen) |
ocomeni | 90:ed0267eca7b5 | 805 | { |
ocomeni | 90:ed0267eca7b5 | 806 | for(int i =0;i<pLen;i++){ |
ocomeni | 103:7b566b522427 | 807 | if(i%8==0) dbg_printf(LOG, "\n[%3d]",i); |
ocomeni | 103:7b566b522427 | 808 | dbg_printf(LOG, "%02x ", buf[i]); |
ocomeni | 90:ed0267eca7b5 | 809 | } |
ocomeni | 103:7b566b522427 | 810 | dbg_printf(LOG, "\n"); |
ocomeni | 90:ed0267eca7b5 | 811 | } |
ocomeni | 90:ed0267eca7b5 | 812 | |
ocomeni | 88:7ffa053be662 | 813 | //#define TRY_PRINTF |
ocomeni | 84:7c7add00f4bf | 814 | |
ocomeni | 90:ed0267eca7b5 | 815 | void WiFiManager::body_callback(const char *at, uint32_t length) { |
ocomeni | 103:7b566b522427 | 816 | dbg_printf(LOG, "\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); |
ocomeni | 84:7c7add00f4bf | 817 | chunkNum++; |
ocomeni | 102:9748f290a1a5 | 818 | if(http_response == NULL) |
ocomeni | 102:9748f290a1a5 | 819 | { |
ocomeni | 103:7b566b522427 | 820 | dbg_printf(LOG, "[WIFI-MAN] response pointer NULL!!\r\n"); |
ocomeni | 102:9748f290a1a5 | 821 | } |
ocomeni | 102:9748f290a1a5 | 822 | else |
ocomeni | 102:9748f290a1a5 | 823 | { |
ocomeni | 103:7b566b522427 | 824 | dbg_printf(LOG, "[WIFI-MAN] response pointer NULL not!!\r\n"); |
ocomeni | 102:9748f290a1a5 | 825 | } |
ocomeni | 89:45f6db09a76d | 826 | sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length); |
ocomeni | 84:7c7add00f4bf | 827 | } |
ocomeni | 84:7c7add00f4bf | 828 | |
ocomeni | 88:7ffa053be662 | 829 | |
ocomeni | 88:7ffa053be662 | 830 | bool WiFiManager::createTLSconnection(const char * hostName) |
ocomeni | 88:7ffa053be662 | 831 | { |
ocomeni | 93:06e755a80187 | 832 | #ifdef ENABLE_MBED_TRACE |
ocomeni | 93:06e755a80187 | 833 | mbed_trace_init(); |
ocomeni | 93:06e755a80187 | 834 | #endif |
ocomeni | 88:7ffa053be662 | 835 | socket = new TLSSocket(); |
ocomeni | 88:7ffa053be662 | 836 | |
ocomeni | 88:7ffa053be662 | 837 | nsapi_error_t r; |
ocomeni | 88:7ffa053be662 | 838 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 839 | r = socket->open(network); |
ocomeni | 88:7ffa053be662 | 840 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 841 | { |
ocomeni | 103:7b566b522427 | 842 | dbg_printf(LOG, "TLS open failed!!\n"); |
ocomeni | 88:7ffa053be662 | 843 | return false; |
ocomeni | 88:7ffa053be662 | 844 | } |
ocomeni | 103:7b566b522427 | 845 | dbg_printf(LOG, "TLS open passed!!\n"); |
ocomeni | 88:7ffa053be662 | 846 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
ocomeni | 88:7ffa053be662 | 847 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 848 | { |
ocomeni | 103:7b566b522427 | 849 | dbg_printf(LOG, "TLS set_root_ca_cert failed!!\n"); |
ocomeni | 98:65c2333a38b6 | 850 | socket->close(); |
ocomeni | 103:7b566b522427 | 851 | dbg_printf(LOG, "closing TLS socket!!\n"); |
ocomeni | 88:7ffa053be662 | 852 | return false; |
ocomeni | 88:7ffa053be662 | 853 | } |
ocomeni | 103:7b566b522427 | 854 | dbg_printf(LOG, "TLS set_root_ca_cert passed!!\n"); |
ocomeni | 88:7ffa053be662 | 855 | r = socket->connect(hostName, 443); |
ocomeni | 88:7ffa053be662 | 856 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 857 | { |
ocomeni | 93:06e755a80187 | 858 | char errstr[100]; |
ocomeni | 93:06e755a80187 | 859 | mbedtls_strerror(r, errstr, 100); |
ocomeni | 103:7b566b522427 | 860 | dbg_printf(LOG, "TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr); |
ocomeni | 93:06e755a80187 | 861 | socket->close(); |
ocomeni | 88:7ffa053be662 | 862 | return false; |
ocomeni | 88:7ffa053be662 | 863 | } |
ocomeni | 103:7b566b522427 | 864 | dbg_printf(LOG, "TLS connection successful for https site : %s\n", hostName); |
ocomeni | 88:7ffa053be662 | 865 | return true; |
ocomeni | 88:7ffa053be662 | 866 | } |
ocomeni | 100:80ef4bc31b7a | 867 | |
ocomeni | 100:80ef4bc31b7a | 868 | |
ocomeni | 100:80ef4bc31b7a | 869 | void WiFiManager::updateRemotePeerDetails() |
ocomeni | 100:80ef4bc31b7a | 870 | { |
ocomeni | 103:7b566b522427 | 871 | dbg_printf(LOG, "Updating internet_config... \r\n"); |
ocomeni | 100:80ef4bc31b7a | 872 | nsapi_error_t error; |
ocomeni | 100:80ef4bc31b7a | 873 | SocketAddress * address = new SocketAddress; |
ocomeni | 100:80ef4bc31b7a | 874 | error = socket->getpeername(address); |
ocomeni | 100:80ef4bc31b7a | 875 | strcpy(internet_config.remote_IPv4Address, address->get_ip_address()); |
ocomeni | 100:80ef4bc31b7a | 876 | uint16_t port = address->get_port(); |
ocomeni | 100:80ef4bc31b7a | 877 | internet_config.remote_port = port; |
ocomeni | 100:80ef4bc31b7a | 878 | delete address; |
ocomeni | 100:80ef4bc31b7a | 879 | } |
ocomeni | 100:80ef4bc31b7a | 880 | |
ocomeni | 88:7ffa053be662 | 881 | #define TESTING_HTTPS |
ocomeni | 98:65c2333a38b6 | 882 | |
ocomeni | 96:f5ed273881af | 883 | //#define DONT_USE_TLS_SOCKET |
ocomeni | 98:65c2333a38b6 | 884 | bool WiFiManager::createHttpsRequest() |
ocomeni | 79:a2187bbfa407 | 885 | { |
ocomeni | 87:99b37d26ff2a | 886 | // reset chunk #; |
ocomeni | 87:99b37d26ff2a | 887 | chunkNum = 0; |
ocomeni | 96:f5ed273881af | 888 | #ifdef MIX_HDR_AND_BODY |
ocomeni | 90:ed0267eca7b5 | 889 | http_response_hdr_sent = false; |
ocomeni | 96:f5ed273881af | 890 | #else |
ocomeni | 96:f5ed273881af | 891 | http_response_hdr_sent = true; |
ocomeni | 96:f5ed273881af | 892 | #endif |
ocomeni | 103:7b566b522427 | 893 | dbg_printf(LOG, "\n[WIFI MAN] Http Request received:\n"); |
ocomeni | 84:7c7add00f4bf | 894 | http_req_cfg = (http_request_t *) data_msg->buffer; |
ocomeni | 103:7b566b522427 | 895 | dbg_printf(LOG, "\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI); |
ocomeni | 103:7b566b522427 | 896 | dbg_printf(LOG, "\n[WIFI MAN] internet cfg url = %s\n", internet_config.url); |
ocomeni | 87:99b37d26ff2a | 897 | char full_url[100]; |
ocomeni | 88:7ffa053be662 | 898 | char host[60] ; |
ocomeni | 87:99b37d26ff2a | 899 | strncpy(full_url,internet_config.url, strlen(internet_config.url)+1); |
ocomeni | 90:ed0267eca7b5 | 900 | strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1); |
ocomeni | 90:ed0267eca7b5 | 901 | strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1); |
ocomeni | 103:7b566b522427 | 902 | dbg_printf(LOG, "\n[WIFI MAN] server url+uri = %s\n", full_url); |
ocomeni | 103:7b566b522427 | 903 | dbg_printf(LOG, "\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName); |
ocomeni | 103:7b566b522427 | 904 | dbg_printf(LOG, "\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal); |
ocomeni | 103:7b566b522427 | 905 | dbg_printf(LOG, "\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType); |
ocomeni | 103:7b566b522427 | 906 | dbg_printf(LOG, "\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen); |
ocomeni | 84:7c7add00f4bf | 907 | |
ocomeni | 84:7c7add00f4bf | 908 | int bodyLen; |
ocomeni | 90:ed0267eca7b5 | 909 | sscanf(http_req_cfg->contentLen, "%d", &bodyLen); |
ocomeni | 103:7b566b522427 | 910 | dbg_printf(LOG, "contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 911 | |
ocomeni | 90:ed0267eca7b5 | 912 | if(bodyLen > 10){ |
ocomeni | 103:7b566b522427 | 913 | dbg_printf(LOG, "\n [WIFI MAN] Message Body:\n"); |
ocomeni | 90:ed0267eca7b5 | 914 | printBufferInHex(http_req_cfg->body, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 915 | } |
ocomeni | 87:99b37d26ff2a | 916 | if(strstr(internet_config.url, "http:")!=NULL) // http request |
ocomeni | 87:99b37d26ff2a | 917 | { |
ocomeni | 87:99b37d26ff2a | 918 | http_request = new HttpRequest(network, |
ocomeni | 90:ed0267eca7b5 | 919 | http_req_cfg->method, |
ocomeni | 90:ed0267eca7b5 | 920 | full_url, |
ocomeni | 90:ed0267eca7b5 | 921 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 87:99b37d26ff2a | 922 | setHttpHeader("Host", http_req_cfg->hostName); |
ocomeni | 87:99b37d26ff2a | 923 | setHttpHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 103:7b566b522427 | 924 | dbg_printf(LOG, "http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 925 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 103:7b566b522427 | 926 | dbg_printf(LOG, "HTTP_GET -- ignoring body\n"); |
ocomeni | 88:7ffa053be662 | 927 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 928 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 929 | http_response = http_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 930 | } |
ocomeni | 88:7ffa053be662 | 931 | else{ |
ocomeni | 88:7ffa053be662 | 932 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 933 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 934 | http_response = http_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 935 | } |
ocomeni | 87:99b37d26ff2a | 936 | free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 937 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 938 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 939 | mbedtls_strerror(http_request->get_error(), buf, 100); |
ocomeni | 103:7b566b522427 | 940 | dbg_printf(LOG, "HttpRequest failed (error code %s)\n", buf); |
ocomeni | 103:7b566b522427 | 941 | //dbg_printf(LOG, "HttpsRequest failed (error code %d)\n", https_request->get_error()); |
ocomeni | 87:99b37d26ff2a | 942 | delete http_request; // free the memory |
ocomeni | 98:65c2333a38b6 | 943 | return false; |
ocomeni | 87:99b37d26ff2a | 944 | } |
ocomeni | 88:7ffa053be662 | 945 | delete http_request; // free the memory |
ocomeni | 103:7b566b522427 | 946 | dbg_printf(LOG, "\n----- HTTP POST response -----\n"); |
ocomeni | 84:7c7add00f4bf | 947 | } |
ocomeni | 87:99b37d26ff2a | 948 | else |
ocomeni | 87:99b37d26ff2a | 949 | { |
ocomeni | 90:ed0267eca7b5 | 950 | #ifndef DONT_USE_TLS_SOCKET |
ocomeni | 88:7ffa053be662 | 951 | if(https_connection_active == false){ |
ocomeni | 88:7ffa053be662 | 952 | bool tlsResult; |
ocomeni | 88:7ffa053be662 | 953 | tlsResult = createTLSconnection(host); |
ocomeni | 93:06e755a80187 | 954 | #ifdef ENABLE_MBED_TRACE |
ocomeni | 93:06e755a80187 | 955 | mbed_trace_free(); // free trace memory |
ocomeni | 93:06e755a80187 | 956 | #endif |
ocomeni | 88:7ffa053be662 | 957 | if(tlsResult == false){ |
ocomeni | 88:7ffa053be662 | 958 | delete socket; |
ocomeni | 103:7b566b522427 | 959 | dbg_printf(LOG, "TLS Socket connection failed - deleting data msg\r\n"); |
ocomeni | 88:7ffa053be662 | 960 | free_DataMsg(); |
ocomeni | 103:7b566b522427 | 961 | dbg_printf(LOG, "data msg deleted \r\n"); |
ocomeni | 98:65c2333a38b6 | 962 | return false; |
ocomeni | 88:7ffa053be662 | 963 | } |
ocomeni | 100:80ef4bc31b7a | 964 | // update remote peer details after socket connection |
ocomeni | 100:80ef4bc31b7a | 965 | updateRemotePeerDetails(); |
ocomeni | 100:80ef4bc31b7a | 966 | // send socket connection event before proceeding to send https request |
ocomeni | 100:80ef4bc31b7a | 967 | // give about 2 ms |
ocomeni | 100:80ef4bc31b7a | 968 | _event_queue.call_in(2, this, &WiFiManager::sendSocketConnectionEvent); |
ocomeni | 103:7b566b522427 | 969 | return true; |
ocomeni | 103:7b566b522427 | 970 | //dbg_printf(LOG, "[create https] TLS connection successful for https site : %s\n", host); |
ocomeni | 103:7b566b522427 | 971 | //dbg_printf(LOG, "after call to createTLSconnection \n"); |
ocomeni | 98:65c2333a38b6 | 972 | //print_memory_info(); |
ocomeni | 88:7ffa053be662 | 973 | } |
ocomeni | 88:7ffa053be662 | 974 | // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument |
ocomeni | 88:7ffa053be662 | 975 | //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); |
ocomeni | 88:7ffa053be662 | 976 | //_wmutex.lock(); |
ocomeni | 88:7ffa053be662 | 977 | https_request = new HttpsRequest(socket, |
ocomeni | 87:99b37d26ff2a | 978 | http_req_cfg->method, |
ocomeni | 88:7ffa053be662 | 979 | full_url, |
ocomeni | 87:99b37d26ff2a | 980 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 90:ed0267eca7b5 | 981 | #else |
ocomeni | 90:ed0267eca7b5 | 982 | https_request = new HttpsRequest(network, |
ocomeni | 90:ed0267eca7b5 | 983 | SSL_CA_PEM, |
ocomeni | 90:ed0267eca7b5 | 984 | http_req_cfg->method, |
ocomeni | 90:ed0267eca7b5 | 985 | full_url, |
ocomeni | 90:ed0267eca7b5 | 986 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 90:ed0267eca7b5 | 987 | #endif |
ocomeni | 88:7ffa053be662 | 988 | #ifdef TESTING_HTTPS |
ocomeni | 103:7b566b522427 | 989 | dbg_printf(LOG, "http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 990 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 103:7b566b522427 | 991 | dbg_printf(LOG, "HTTP_GET -- ignoring body\n"); |
ocomeni | 89:45f6db09a76d | 992 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 993 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 994 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 995 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 996 | http_response = https_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 997 | } |
ocomeni | 88:7ffa053be662 | 998 | else{ |
ocomeni | 103:7b566b522427 | 999 | dbg_printf(LOG, "about to setup https headers\r\n"); |
ocomeni | 89:45f6db09a76d | 1000 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 1001 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 90:ed0267eca7b5 | 1002 | setHttpsHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 90:ed0267eca7b5 | 1003 | setHttpsHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 103:7b566b522427 | 1004 | dbg_printf(LOG, "https headers setup - about to send request\r\n"); |
ocomeni | 98:65c2333a38b6 | 1005 | mbedtls_ssl_context* tlsContext ; |
ocomeni | 98:65c2333a38b6 | 1006 | tlsContext = socket->get_ssl_context(); |
ocomeni | 98:65c2333a38b6 | 1007 | mbedtls_ssl_config * tlsConfig; |
ocomeni | 98:65c2333a38b6 | 1008 | tlsConfig = socket->get_ssl_config(); |
ocomeni | 98:65c2333a38b6 | 1009 | if(tlsContext != NULL) |
ocomeni | 98:65c2333a38b6 | 1010 | { |
ocomeni | 103:7b566b522427 | 1011 | dbg_printf(LOG, "current TLS tlsContext is not null [%d] \r\n", tlsContext->state); |
ocomeni | 98:65c2333a38b6 | 1012 | } |
ocomeni | 98:65c2333a38b6 | 1013 | else |
ocomeni | 98:65c2333a38b6 | 1014 | { |
ocomeni | 103:7b566b522427 | 1015 | dbg_printf(LOG, "TLS Context is NULL \r\n"); |
ocomeni | 98:65c2333a38b6 | 1016 | } |
ocomeni | 96:f5ed273881af | 1017 | //_wmutex.lock(); |
ocomeni | 88:7ffa053be662 | 1018 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 102:9748f290a1a5 | 1019 | //https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 96:f5ed273881af | 1020 | //_wmutex.unlock(); |
ocomeni | 88:7ffa053be662 | 1021 | } |
ocomeni | 88:7ffa053be662 | 1022 | #else |
ocomeni | 89:45f6db09a76d | 1023 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 1024 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 96:f5ed273881af | 1025 | setHttpsHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 96:f5ed273881af | 1026 | setHttpsHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 87:99b37d26ff2a | 1027 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 1028 | #endif |
ocomeni | 88:7ffa053be662 | 1029 | |
ocomeni | 88:7ffa053be662 | 1030 | //_wmutex.unlock(); |
ocomeni | 90:ed0267eca7b5 | 1031 | //free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 1032 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 1033 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 1034 | mbedtls_strerror(https_request->get_error(), buf, 100); |
ocomeni | 103:7b566b522427 | 1035 | dbg_printf(LOG, "HttpsRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 1036 | delete https_request; // free the memory |
ocomeni | 96:f5ed273881af | 1037 | https_request = NULL; |
ocomeni | 88:7ffa053be662 | 1038 | https_connection_active = false; // reset true whenever connection fails |
ocomeni | 98:65c2333a38b6 | 1039 | socket->close(); |
ocomeni | 88:7ffa053be662 | 1040 | delete socket; |
ocomeni | 96:f5ed273881af | 1041 | socket = NULL; |
ocomeni | 90:ed0267eca7b5 | 1042 | free_DataMsg(); |
ocomeni | 98:65c2333a38b6 | 1043 | return false; |
ocomeni | 87:99b37d26ff2a | 1044 | } |
ocomeni | 88:7ffa053be662 | 1045 | https_connection_active = true; // set true whenever connection succeeds |
ocomeni | 103:7b566b522427 | 1046 | dbg_printf(LOG, "\n----- HTTPS POST response -----\r\n"); |
ocomeni | 90:ed0267eca7b5 | 1047 | } |
ocomeni | 96:f5ed273881af | 1048 | if(http_response != NULL){ |
ocomeni | 96:f5ed273881af | 1049 | |
ocomeni | 96:f5ed273881af | 1050 | #ifndef MIX_HDR_AND_BODY |
ocomeni | 96:f5ed273881af | 1051 | return_response(http_response); |
ocomeni | 98:65c2333a38b6 | 1052 | #else |
ocomeni | 98:65c2333a38b6 | 1053 | if(http_response_hdr_sent == false){ // if it failed to add to first chunk send separately |
ocomeni | 98:65c2333a38b6 | 1054 | return_response(http_response); |
ocomeni | 98:65c2333a38b6 | 1055 | } |
ocomeni | 96:f5ed273881af | 1056 | #endif |
ocomeni | 96:f5ed273881af | 1057 | //delete http_response; // free the response memory |
ocomeni | 96:f5ed273881af | 1058 | //http_response = NULL; |
ocomeni | 103:7b566b522427 | 1059 | //dbg_printf(LOG, "deleted http_response\r\n"); |
ocomeni | 96:f5ed273881af | 1060 | } |
ocomeni | 96:f5ed273881af | 1061 | free_DataMsg(); |
ocomeni | 96:f5ed273881af | 1062 | delete https_request; // free the request & response memory |
ocomeni | 103:7b566b522427 | 1063 | dbg_printf(LOG, "deleted https_request\r\n"); |
ocomeni | 96:f5ed273881af | 1064 | https_request = NULL; |
ocomeni | 100:80ef4bc31b7a | 1065 | http_response = NULL; |
ocomeni | 98:65c2333a38b6 | 1066 | return true; |
ocomeni | 79:a2187bbfa407 | 1067 | } |
ocomeni | 79:a2187bbfa407 | 1068 | |
ocomeni | 79:a2187bbfa407 | 1069 | void WiFiManager::createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 1070 | const char* url, |
ocomeni | 79:a2187bbfa407 | 1071 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 1072 | { |
ocomeni | 84:7c7add00f4bf | 1073 | http_request = new HttpRequest(network, |
ocomeni | 84:7c7add00f4bf | 1074 | method, url, body_callback);; |
ocomeni | 79:a2187bbfa407 | 1075 | } |
ocomeni | 79:a2187bbfa407 | 1076 | |
ocomeni | 79:a2187bbfa407 | 1077 | void WiFiManager::setHttpHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 1078 | { |
ocomeni | 79:a2187bbfa407 | 1079 | http_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 1080 | } |
ocomeni | 79:a2187bbfa407 | 1081 | |
ocomeni | 79:a2187bbfa407 | 1082 | void WiFiManager::setHttpsHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 1083 | { |
ocomeni | 79:a2187bbfa407 | 1084 | https_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 1085 | } |
ocomeni | 79:a2187bbfa407 | 1086 | |
ocomeni | 79:a2187bbfa407 | 1087 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 1088 | { |
ocomeni | 78:07bb86e3ce14 | 1089 | } |
ocomeni | 79:a2187bbfa407 | 1090 | |
ocomeni | 79:a2187bbfa407 | 1091 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 1092 | { |
ocomeni | 78:07bb86e3ce14 | 1093 | } |
ocomeni | 79:a2187bbfa407 | 1094 |