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