Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.cpp@93:06e755a80187, 2019-04-14 (annotated)
- Committer:
- ocomeni
- Date:
- Sun Apr 14 14:38:51 2019 +0000
- Revision:
- 93:06e755a80187
- Parent:
- 92:ec9550034276
- Child:
- 94:fb4414aff957
fixed bug with wifi configuration API (was adding an extra character causing wifi connection to fail because of incorrect credentials)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 78:07bb86e3ce14 | 1 | #include "WiFiManager.h" |
ocomeni | 78:07bb86e3ce14 | 2 | #include "common_config.h" |
ocomeni | 78:07bb86e3ce14 | 3 | |
ocomeni | 78:07bb86e3ce14 | 4 | |
ocomeni | 79:a2187bbfa407 | 5 | WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, |
ocomeni | 80:e8f0e92e3ac9 | 6 | MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, |
ocomeni | 80:e8f0e92e3ac9 | 7 | Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, |
ocomeni | 81:637a87eb8170 | 8 | MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, |
ocomeni | 81:637a87eb8170 | 9 | Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, |
ocomeni | 87:99b37d26ff2a | 10 | MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, |
ocomeni | 87:99b37d26ff2a | 11 | Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, |
ocomeni | 87:99b37d26ff2a | 12 | MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, |
ocomeni | 87:99b37d26ff2a | 13 | Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue) |
ocomeni | 78:07bb86e3ce14 | 14 | : |
ocomeni | 81:637a87eb8170 | 15 | wifi_config(wifi_config), |
ocomeni | 81:637a87eb8170 | 16 | network(wifi), |
ocomeni | 81:637a87eb8170 | 17 | _aT2WiFimPool(aT2WiFimPool), |
ocomeni | 81:637a87eb8170 | 18 | _aT2WiFiCmdQueue(aT2WiFiCmdQueue), |
ocomeni | 81:637a87eb8170 | 19 | |
ocomeni | 81:637a87eb8170 | 20 | _wiFi2ATmPool(wiFi2ATmPool), |
ocomeni | 81:637a87eb8170 | 21 | _wiFi2ATCmdQueue(wiFi2ATCmdQueue), |
ocomeni | 81:637a87eb8170 | 22 | |
ocomeni | 81:637a87eb8170 | 23 | _aT2WiFiDatamPool(aT2WiFiDatamPool), |
ocomeni | 81:637a87eb8170 | 24 | _aT2WiFiDataQueue(aT2WiFiDataQueue), |
ocomeni | 81:637a87eb8170 | 25 | |
ocomeni | 81:637a87eb8170 | 26 | _wiFi2ATDatamPool(wiFi2ATDatamPool), |
ocomeni | 81:637a87eb8170 | 27 | _wiFi2ATDataQueue(wiFi2ATDataQueue) |
ocomeni | 78:07bb86e3ce14 | 28 | |
ocomeni | 78:07bb86e3ce14 | 29 | { |
ocomeni | 79:a2187bbfa407 | 30 | lastScanCount = 0; |
ocomeni | 79:a2187bbfa407 | 31 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 32 | internet_config.connectionScheme = ALWAYS_CONNECTED; // set default connection scheme |
ocomeni | 81:637a87eb8170 | 33 | is_connected = false; |
ocomeni | 84:7c7add00f4bf | 34 | chunkNum = 0; |
ocomeni | 88:7ffa053be662 | 35 | socket = NULL; |
ocomeni | 88:7ffa053be662 | 36 | https_connection_active = false; |
ocomeni | 78:07bb86e3ce14 | 37 | } |
ocomeni | 78:07bb86e3ce14 | 38 | |
ocomeni | 78:07bb86e3ce14 | 39 | WiFiManager::~WiFiManager() |
ocomeni | 78:07bb86e3ce14 | 40 | { |
ocomeni | 78:07bb86e3ce14 | 41 | } |
ocomeni | 79:a2187bbfa407 | 42 | |
ocomeni | 81:637a87eb8170 | 43 | bool WiFiManager::queueATresponse(at_cmd_resp_t resp){ |
ocomeni | 92:ec9550034276 | 44 | #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL |
ocomeni | 81:637a87eb8170 | 45 | at_resp_message_t *atResp = _wiFi2ATmPool->alloc(); |
ocomeni | 92:ec9550034276 | 46 | #else |
ocomeni | 92:ec9550034276 | 47 | at_resp_message_t *atResp = (at_resp_message_t *) malloc(sizeof(at_resp_message_t)); |
ocomeni | 92:ec9550034276 | 48 | #endif |
ocomeni | 88:7ffa053be662 | 49 | if(atResp == NULL) return false; // queue full; |
ocomeni | 81:637a87eb8170 | 50 | atResp->at_resp = resp; |
ocomeni | 81:637a87eb8170 | 51 | _wiFi2ATCmdQueue->put(atResp); |
ocomeni | 81:637a87eb8170 | 52 | return true; |
ocomeni | 81:637a87eb8170 | 53 | } |
ocomeni | 81:637a87eb8170 | 54 | |
ocomeni | 81:637a87eb8170 | 55 | |
ocomeni | 81:637a87eb8170 | 56 | bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){ |
ocomeni | 81:637a87eb8170 | 57 | at_data_msg_t *atData = _wiFi2ATDatamPool->alloc(); |
ocomeni | 88:7ffa053be662 | 58 | if(atData == NULL) return false; // queue full; |
ocomeni | 81:637a87eb8170 | 59 | atData->at_resp = at_resp.at_resp; |
ocomeni | 81:637a87eb8170 | 60 | atData->dataLen = at_resp.dataLen; |
ocomeni | 81:637a87eb8170 | 61 | memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen); |
ocomeni | 81:637a87eb8170 | 62 | _wiFi2ATDataQueue->put(atData); |
ocomeni | 87:99b37d26ff2a | 63 | printf("[WIFI MAN] queued data size = %d : at_resp = %d\n", at_resp.dataLen, at_resp.at_resp); |
ocomeni | 81:637a87eb8170 | 64 | return true; |
ocomeni | 81:637a87eb8170 | 65 | } |
ocomeni | 81:637a87eb8170 | 66 | |
ocomeni | 79:a2187bbfa407 | 67 | |
ocomeni | 79:a2187bbfa407 | 68 | |
ocomeni | 79:a2187bbfa407 | 69 | void WiFiManager::runMain(){ |
ocomeni | 81:637a87eb8170 | 70 | nsapi_error_t error; |
ocomeni | 90:ed0267eca7b5 | 71 | printf("\r\n [WIFI MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); |
ocomeni | 79:a2187bbfa407 | 72 | while(true){ |
ocomeni | 79:a2187bbfa407 | 73 | dequeueWiFiCommands(); |
ocomeni | 81:637a87eb8170 | 74 | dequeueATdataResponse(); |
ocomeni | 79:a2187bbfa407 | 75 | switch(wifiCmd){ |
ocomeni | 79:a2187bbfa407 | 76 | case WIFI_CMD_NONE: |
ocomeni | 79:a2187bbfa407 | 77 | // IDLE STATE |
ocomeni | 79:a2187bbfa407 | 78 | break; |
ocomeni | 79:a2187bbfa407 | 79 | case WIFI_CMD_SCAN: |
ocomeni | 79:a2187bbfa407 | 80 | error = scanNetworks(); |
ocomeni | 79:a2187bbfa407 | 81 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 82 | queueATresponse(AT_SCAN_RESP); |
ocomeni | 81:637a87eb8170 | 83 | break; |
ocomeni | 81:637a87eb8170 | 84 | case WIFI_CMD_DETAILED_SCAN: |
ocomeni | 81:637a87eb8170 | 85 | nsapi_size_or_error_t cnt_err; |
ocomeni | 81:637a87eb8170 | 86 | cnt_err = getAvailableAPs(lastScanCount); |
ocomeni | 81:637a87eb8170 | 87 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 88 | queueATresponse(AT_DETAILED_SCAN_RESP); |
ocomeni | 79:a2187bbfa407 | 89 | break; |
ocomeni | 79:a2187bbfa407 | 90 | case WIFI_CMD_CONNECT: |
ocomeni | 81:637a87eb8170 | 91 | error = connect(); |
ocomeni | 93:06e755a80187 | 92 | int secCount = 0; |
ocomeni | 93:06e755a80187 | 93 | while(secCount++ < WIFI_CONNECT_TIMEOUT_SECS || is_connected==false){ |
ocomeni | 93:06e755a80187 | 94 | wait(1); // wait 1 sec |
ocomeni | 93:06e755a80187 | 95 | } |
ocomeni | 81:637a87eb8170 | 96 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 93:06e755a80187 | 97 | if(is_connected==false){ |
ocomeni | 93:06e755a80187 | 98 | printf("[WIFI MAN] +++ WIFI CONNECTION TIMEOUT +++ \r\n"); |
ocomeni | 93:06e755a80187 | 99 | queueATresponse(AT_COMMAND_FAILED); |
ocomeni | 93:06e755a80187 | 100 | } |
ocomeni | 93:06e755a80187 | 101 | else { |
ocomeni | 93:06e755a80187 | 102 | queueATresponse(AT_CONNECT_RESP); |
ocomeni | 93:06e755a80187 | 103 | } |
ocomeni | 79:a2187bbfa407 | 104 | break; |
ocomeni | 79:a2187bbfa407 | 105 | case WIFI_CMD_DISCONNECT: |
ocomeni | 81:637a87eb8170 | 106 | error = disconnect(); |
ocomeni | 81:637a87eb8170 | 107 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 108 | queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 109 | break; |
ocomeni | 81:637a87eb8170 | 110 | case WIFI_CMD_CONFIG: |
ocomeni | 81:637a87eb8170 | 111 | set_WIFI_CONFIG(); |
ocomeni | 81:637a87eb8170 | 112 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 113 | queueATresponse(AT_CONFIG_RESP); |
ocomeni | 82:10072c1794d3 | 114 | break; |
ocomeni | 81:637a87eb8170 | 115 | case WIFI_CMD_INTERNET_CONFIG: |
ocomeni | 81:637a87eb8170 | 116 | set_internet_config(); |
ocomeni | 81:637a87eb8170 | 117 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 118 | queueATresponse(AT_INTERNET_CONFIG_RESP); |
ocomeni | 79:a2187bbfa407 | 119 | break; |
ocomeni | 79:a2187bbfa407 | 120 | case WIFI_CMD_SEND_HTTPS_REQ: |
ocomeni | 88:7ffa053be662 | 121 | printf("before call to send http request \n"); |
ocomeni | 88:7ffa053be662 | 122 | print_memory_info(); |
ocomeni | 84:7c7add00f4bf | 123 | createHttpsRequest(); |
ocomeni | 88:7ffa053be662 | 124 | printf("after call to send http request \n"); |
ocomeni | 88:7ffa053be662 | 125 | print_memory_info(); |
ocomeni | 84:7c7add00f4bf | 126 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 79:a2187bbfa407 | 127 | break; |
ocomeni | 79:a2187bbfa407 | 128 | case WIFI_CMD_SEND_HTTP_REQ: |
ocomeni | 79:a2187bbfa407 | 129 | break; |
ocomeni | 79:a2187bbfa407 | 130 | default: |
ocomeni | 79:a2187bbfa407 | 131 | break; |
ocomeni | 79:a2187bbfa407 | 132 | } |
ocomeni | 79:a2187bbfa407 | 133 | wait_ms(100); // |
ocomeni | 79:a2187bbfa407 | 134 | } |
ocomeni | 79:a2187bbfa407 | 135 | |
ocomeni | 78:07bb86e3ce14 | 136 | } |
ocomeni | 79:a2187bbfa407 | 137 | |
ocomeni | 91:d6b6319ad681 | 138 | |
ocomeni | 91:d6b6319ad681 | 139 | void WiFiManager::sentATresponseString(at_cmd_resp_t at_cmd, |
ocomeni | 91:d6b6319ad681 | 140 | const char *responseString, |
ocomeni | 91:d6b6319ad681 | 141 | int strLen) |
ocomeni | 91:d6b6319ad681 | 142 | { |
ocomeni | 91:d6b6319ad681 | 143 | at_data_resp = new at_data_msg_t; |
ocomeni | 91:d6b6319ad681 | 144 | // create message pointer for response header generation |
ocomeni | 91:d6b6319ad681 | 145 | char * msgPtr = (char *)at_data_resp->buffer; |
ocomeni | 91:d6b6319ad681 | 146 | // set string length |
ocomeni | 91:d6b6319ad681 | 147 | at_data_resp->dataLen = strLen; |
ocomeni | 91:d6b6319ad681 | 148 | memcpy(at_data_resp->buffer, responseString, strLen); |
ocomeni | 91:d6b6319ad681 | 149 | // package and send on wifi data queue |
ocomeni | 91:d6b6319ad681 | 150 | at_data_resp->at_resp = at_cmd; |
ocomeni | 91:d6b6319ad681 | 151 | bool queueResult = true; |
ocomeni | 91:d6b6319ad681 | 152 | int wait_count = 0; |
ocomeni | 91:d6b6319ad681 | 153 | do |
ocomeni | 91:d6b6319ad681 | 154 | { |
ocomeni | 91:d6b6319ad681 | 155 | if(!queueResult){ |
ocomeni | 91:d6b6319ad681 | 156 | wait_count++; |
ocomeni | 91:d6b6319ad681 | 157 | printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 91:d6b6319ad681 | 158 | wait_ms(10); |
ocomeni | 91:d6b6319ad681 | 159 | } |
ocomeni | 91:d6b6319ad681 | 160 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 91:d6b6319ad681 | 161 | }while(queueResult == false); |
ocomeni | 91:d6b6319ad681 | 162 | delete at_data_resp; |
ocomeni | 91:d6b6319ad681 | 163 | } |
ocomeni | 91:d6b6319ad681 | 164 | |
ocomeni | 79:a2187bbfa407 | 165 | bool WiFiManager::dequeueWiFiCommands(){ |
ocomeni | 81:637a87eb8170 | 166 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 167 | osEvent evt = _aT2WiFiCmdQueue->get(0); |
ocomeni | 79:a2187bbfa407 | 168 | if(evt.status == osEventMessage){ |
ocomeni | 79:a2187bbfa407 | 169 | wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p; |
ocomeni | 79:a2187bbfa407 | 170 | setNextCommand(cmd->wifi_cmd); |
ocomeni | 92:ec9550034276 | 171 | #ifndef USE_MALLOC_FOR_COMMAND_MEMORY_POOL |
ocomeni | 79:a2187bbfa407 | 172 | _aT2WiFimPool->free(cmd); |
ocomeni | 92:ec9550034276 | 173 | #else |
ocomeni | 92:ec9550034276 | 174 | free(cmd); |
ocomeni | 92:ec9550034276 | 175 | #endif |
ocomeni | 79:a2187bbfa407 | 176 | } |
ocomeni | 79:a2187bbfa407 | 177 | return true; |
ocomeni | 79:a2187bbfa407 | 178 | } |
ocomeni | 79:a2187bbfa407 | 179 | |
ocomeni | 79:a2187bbfa407 | 180 | |
ocomeni | 81:637a87eb8170 | 181 | bool WiFiManager::dequeueATdataResponse(){ |
ocomeni | 81:637a87eb8170 | 182 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 183 | osEvent evt = _aT2WiFiDataQueue->get(0); |
ocomeni | 81:637a87eb8170 | 184 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 185 | data_msg = (wifi_data_msg_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 186 | setNextCommand(data_msg->wifi_cmd); |
ocomeni | 81:637a87eb8170 | 187 | //_wiFi2ATDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 188 | } |
ocomeni | 81:637a87eb8170 | 189 | return true; |
ocomeni | 81:637a87eb8170 | 190 | } |
ocomeni | 81:637a87eb8170 | 191 | |
ocomeni | 81:637a87eb8170 | 192 | |
ocomeni | 79:a2187bbfa407 | 193 | bool WiFiManager::setNextCommand(wifi_cmd_t cmd) |
ocomeni | 78:07bb86e3ce14 | 194 | { |
ocomeni | 92:ec9550034276 | 195 | printf("\n [WIFI-MAN] About to set next WiFi manager command to %d\n", cmd); |
ocomeni | 79:a2187bbfa407 | 196 | if(wifiCmd == WIFI_CMD_NONE){ |
ocomeni | 79:a2187bbfa407 | 197 | wifiCmd = cmd; |
ocomeni | 79:a2187bbfa407 | 198 | return true; // success |
ocomeni | 79:a2187bbfa407 | 199 | } |
ocomeni | 92:ec9550034276 | 200 | printf("\n [WIFI-MAN] Busy : current state = %d \n", wifiCmd); |
ocomeni | 79:a2187bbfa407 | 201 | return false; // wiFiManager busy |
ocomeni | 78:07bb86e3ce14 | 202 | } |
ocomeni | 79:a2187bbfa407 | 203 | |
ocomeni | 81:637a87eb8170 | 204 | const char * WiFiManager::sec2str(nsapi_security_t sec) |
ocomeni | 81:637a87eb8170 | 205 | { |
ocomeni | 81:637a87eb8170 | 206 | switch (sec) { |
ocomeni | 81:637a87eb8170 | 207 | case NSAPI_SECURITY_NONE: |
ocomeni | 81:637a87eb8170 | 208 | return "None"; |
ocomeni | 81:637a87eb8170 | 209 | case NSAPI_SECURITY_WEP: |
ocomeni | 81:637a87eb8170 | 210 | return "WEP"; |
ocomeni | 81:637a87eb8170 | 211 | case NSAPI_SECURITY_WPA: |
ocomeni | 81:637a87eb8170 | 212 | return "WPA"; |
ocomeni | 81:637a87eb8170 | 213 | case NSAPI_SECURITY_WPA2: |
ocomeni | 81:637a87eb8170 | 214 | return "WPA2"; |
ocomeni | 81:637a87eb8170 | 215 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 81:637a87eb8170 | 216 | return "WPA/WPA2"; |
ocomeni | 81:637a87eb8170 | 217 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 81:637a87eb8170 | 218 | default: |
ocomeni | 81:637a87eb8170 | 219 | return "Unknown"; |
ocomeni | 81:637a87eb8170 | 220 | } |
ocomeni | 81:637a87eb8170 | 221 | } |
ocomeni | 81:637a87eb8170 | 222 | |
ocomeni | 79:a2187bbfa407 | 223 | |
ocomeni | 79:a2187bbfa407 | 224 | nsapi_size_or_error_t WiFiManager::scanNetworks() |
ocomeni | 79:a2187bbfa407 | 225 | { |
ocomeni | 79:a2187bbfa407 | 226 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 227 | printf("\n [WIFI-MAN] About to start scan for WiFi networks\n"); |
ocomeni | 79:a2187bbfa407 | 228 | lastScanCount = network->scan(NULL, 0); |
ocomeni | 79:a2187bbfa407 | 229 | printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n"); |
ocomeni | 79:a2187bbfa407 | 230 | return lastScanCount; |
ocomeni | 79:a2187bbfa407 | 231 | } |
ocomeni | 79:a2187bbfa407 | 232 | |
ocomeni | 79:a2187bbfa407 | 233 | |
ocomeni | 81:637a87eb8170 | 234 | //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, |
ocomeni | 81:637a87eb8170 | 235 | // nsapi_size_t ncount) |
ocomeni | 81:637a87eb8170 | 236 | nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount) |
ocomeni | 79:a2187bbfa407 | 237 | { |
ocomeni | 81:637a87eb8170 | 238 | WiFiAccessPoint *ap; |
ocomeni | 81:637a87eb8170 | 239 | nsapi_size_or_error_t count; |
ocomeni | 81:637a87eb8170 | 240 | count = ncount; |
ocomeni | 81:637a87eb8170 | 241 | //count = wiFiManager->scanNetworks(); |
ocomeni | 81:637a87eb8170 | 242 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 243 | //_smutex.lock(); |
ocomeni | 81:637a87eb8170 | 244 | printf("[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 245 | //_smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 246 | return; |
ocomeni | 81:637a87eb8170 | 247 | } |
ocomeni | 81:637a87eb8170 | 248 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 81:637a87eb8170 | 249 | count = count < 15 ? count : 15; |
ocomeni | 81:637a87eb8170 | 250 | ap = new WiFiAccessPoint[count]; |
ocomeni | 81:637a87eb8170 | 251 | count = network->scan(ap, count); |
ocomeni | 81:637a87eb8170 | 252 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 253 | printf("[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 254 | return; |
ocomeni | 81:637a87eb8170 | 255 | } |
ocomeni | 81:637a87eb8170 | 256 | |
ocomeni | 81:637a87eb8170 | 257 | for (int i = 0; i < count; i++) { |
ocomeni | 81:637a87eb8170 | 258 | 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 | 259 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 81:637a87eb8170 | 260 | 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 | 261 | } |
ocomeni | 81:637a87eb8170 | 262 | printf("[WIFI-MAN] %d networks available.\n", count); |
ocomeni | 81:637a87eb8170 | 263 | |
ocomeni | 81:637a87eb8170 | 264 | delete[] ap; |
ocomeni | 79:a2187bbfa407 | 265 | return count; |
ocomeni | 79:a2187bbfa407 | 266 | } |
ocomeni | 79:a2187bbfa407 | 267 | |
ocomeni | 79:a2187bbfa407 | 268 | |
ocomeni | 81:637a87eb8170 | 269 | void WiFiManager::set_WIFI_CONFIG() |
ocomeni | 81:637a87eb8170 | 270 | { |
ocomeni | 81:637a87eb8170 | 271 | wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer; |
ocomeni | 82:10072c1794d3 | 272 | if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid); |
ocomeni | 82:10072c1794d3 | 273 | if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass); |
ocomeni | 82:10072c1794d3 | 274 | if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security); |
ocomeni | 81:637a87eb8170 | 275 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 276 | } |
ocomeni | 81:637a87eb8170 | 277 | |
ocomeni | 78:07bb86e3ce14 | 278 | void WiFiManager::set_WIFI_SSID(char * wifi_ssid) |
ocomeni | 78:07bb86e3ce14 | 279 | { |
ocomeni | 78:07bb86e3ce14 | 280 | strcpy(wifi_config.ssid, wifi_ssid); |
ocomeni | 82:10072c1794d3 | 281 | printf("[WIFI-MAN] wifi_ssid set to %s\n", wifi_config.ssid); |
ocomeni | 88:7ffa053be662 | 282 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 283 | delete socket; |
ocomeni | 78:07bb86e3ce14 | 284 | } |
ocomeni | 79:a2187bbfa407 | 285 | |
ocomeni | 79:a2187bbfa407 | 286 | |
ocomeni | 78:07bb86e3ce14 | 287 | void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass) |
ocomeni | 78:07bb86e3ce14 | 288 | { |
ocomeni | 78:07bb86e3ce14 | 289 | strcpy(wifi_config.pass, wifi_pass); |
ocomeni | 92:ec9550034276 | 290 | printf("[WIFI-MAN] wifi_pass set to %s\n", "****************"); |
ocomeni | 88:7ffa053be662 | 291 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 292 | delete socket; |
ocomeni | 78:07bb86e3ce14 | 293 | } |
ocomeni | 79:a2187bbfa407 | 294 | |
ocomeni | 79:a2187bbfa407 | 295 | |
ocomeni | 78:07bb86e3ce14 | 296 | void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security) |
ocomeni | 78:07bb86e3ce14 | 297 | { |
ocomeni | 78:07bb86e3ce14 | 298 | wifi_config.security = wifi_security; |
ocomeni | 82:10072c1794d3 | 299 | printf("[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config.security)); |
ocomeni | 88:7ffa053be662 | 300 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 301 | delete socket; |
ocomeni | 78:07bb86e3ce14 | 302 | } |
ocomeni | 79:a2187bbfa407 | 303 | |
ocomeni | 79:a2187bbfa407 | 304 | |
ocomeni | 81:637a87eb8170 | 305 | |
ocomeni | 81:637a87eb8170 | 306 | void WiFiManager::set_internet_config() |
ocomeni | 81:637a87eb8170 | 307 | { |
ocomeni | 81:637a87eb8170 | 308 | internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer; |
ocomeni | 81:637a87eb8170 | 309 | internet_config.peer_id = internet_cfg->peer_id; |
ocomeni | 84:7c7add00f4bf | 310 | strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1); |
ocomeni | 81:637a87eb8170 | 311 | internet_config.connectionScheme = internet_cfg->connectionScheme; |
ocomeni | 81:637a87eb8170 | 312 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 313 | printf("[WIFI MAN] Internet configuration setup completed\n"); |
ocomeni | 81:637a87eb8170 | 314 | printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, |
ocomeni | 84:7c7add00f4bf | 315 | internet_config.url, |
ocomeni | 81:637a87eb8170 | 316 | internet_config.connectionScheme); |
ocomeni | 88:7ffa053be662 | 317 | https_connection_active = false; // reset whenever any of the security credentials change |
ocomeni | 91:d6b6319ad681 | 318 | delete socket; |
ocomeni | 81:637a87eb8170 | 319 | } |
ocomeni | 81:637a87eb8170 | 320 | |
ocomeni | 81:637a87eb8170 | 321 | void WiFiManager::free_DataMsg() |
ocomeni | 81:637a87eb8170 | 322 | { |
ocomeni | 81:637a87eb8170 | 323 | // free memory after processing |
ocomeni | 81:637a87eb8170 | 324 | _aT2WiFiDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 325 | } |
ocomeni | 81:637a87eb8170 | 326 | |
ocomeni | 81:637a87eb8170 | 327 | |
ocomeni | 88:7ffa053be662 | 328 | |
ocomeni | 81:637a87eb8170 | 329 | void WiFiManager::status_callback(nsapi_event_t status, intptr_t param) |
ocomeni | 81:637a87eb8170 | 330 | { |
ocomeni | 81:637a87eb8170 | 331 | //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) { |
ocomeni | 81:637a87eb8170 | 332 | //} |
ocomeni | 81:637a87eb8170 | 333 | switch(param) { |
ocomeni | 81:637a87eb8170 | 334 | case NSAPI_STATUS_LOCAL_UP: |
ocomeni | 81:637a87eb8170 | 335 | printf("[WIFI-MAN] Local IP address set!\r\n"); |
ocomeni | 81:637a87eb8170 | 336 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 337 | break; |
ocomeni | 81:637a87eb8170 | 338 | case NSAPI_STATUS_GLOBAL_UP: |
ocomeni | 81:637a87eb8170 | 339 | printf("Global IP address set!\r\n"); |
ocomeni | 81:637a87eb8170 | 340 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 341 | printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid); |
ocomeni | 81:637a87eb8170 | 342 | is_connected = true; |
ocomeni | 81:637a87eb8170 | 343 | break; |
ocomeni | 81:637a87eb8170 | 344 | case NSAPI_STATUS_DISCONNECTED: |
ocomeni | 81:637a87eb8170 | 345 | printf("No connection to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 346 | printf("\n [WIFI-MAN] No connection to network!\n"); |
ocomeni | 81:637a87eb8170 | 347 | is_connected = false; |
ocomeni | 81:637a87eb8170 | 348 | //queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 349 | // attempt reconnection if always connected scheme is set |
ocomeni | 81:637a87eb8170 | 350 | if(internet_config.connectionScheme == ALWAYS_CONNECTED) |
ocomeni | 81:637a87eb8170 | 351 | { |
ocomeni | 81:637a87eb8170 | 352 | nsapi_error_t error; |
ocomeni | 91:d6b6319ad681 | 353 | error = connect(); |
ocomeni | 81:637a87eb8170 | 354 | queueATresponse(WIFI_RECONNECT_INFO); |
ocomeni | 81:637a87eb8170 | 355 | } |
ocomeni | 81:637a87eb8170 | 356 | break; |
ocomeni | 81:637a87eb8170 | 357 | case NSAPI_STATUS_CONNECTING: |
ocomeni | 81:637a87eb8170 | 358 | printf("Connecting to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 359 | break; |
ocomeni | 81:637a87eb8170 | 360 | default: |
ocomeni | 81:637a87eb8170 | 361 | printf("Not supported"); |
ocomeni | 81:637a87eb8170 | 362 | break; |
ocomeni | 81:637a87eb8170 | 363 | } |
ocomeni | 81:637a87eb8170 | 364 | } |
ocomeni | 81:637a87eb8170 | 365 | |
ocomeni | 81:637a87eb8170 | 366 | |
ocomeni | 81:637a87eb8170 | 367 | |
ocomeni | 79:a2187bbfa407 | 368 | // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */ |
ocomeni | 79:a2187bbfa407 | 369 | // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */ |
ocomeni | 79:a2187bbfa407 | 370 | // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */ |
ocomeni | 79:a2187bbfa407 | 371 | // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */ |
ocomeni | 79:a2187bbfa407 | 372 | // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED |
ocomeni | 79:a2187bbfa407 | 373 | |
ocomeni | 79:a2187bbfa407 | 374 | nsapi_error_t WiFiManager::connect() |
ocomeni | 79:a2187bbfa407 | 375 | { |
ocomeni | 79:a2187bbfa407 | 376 | nsapi_error_t error; |
ocomeni | 81:637a87eb8170 | 377 | printf("\n [WIFI-MAN] About to connect to WiFi network\n"); |
ocomeni | 81:637a87eb8170 | 378 | network->attach(callback(this, &WiFiManager::status_callback)); |
ocomeni | 79:a2187bbfa407 | 379 | error = network->set_blocking(false); |
ocomeni | 79:a2187bbfa407 | 380 | if(error) |
ocomeni | 79:a2187bbfa407 | 381 | { |
ocomeni | 79:a2187bbfa407 | 382 | printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n"); |
ocomeni | 79:a2187bbfa407 | 383 | return error; |
ocomeni | 79:a2187bbfa407 | 384 | } |
ocomeni | 93:06e755a80187 | 385 | printf("[WIFI-MAN] Connecting to network ssid = %s passwd = %s security = %s \r\n", |
ocomeni | 93:06e755a80187 | 386 | wifi_config.ssid, |
ocomeni | 93:06e755a80187 | 387 | wifi_config.pass, |
ocomeni | 93:06e755a80187 | 388 | sec2str(wifi_config.security)); |
ocomeni | 79:a2187bbfa407 | 389 | error = network->connect(wifi_config.ssid, |
ocomeni | 79:a2187bbfa407 | 390 | wifi_config.pass, |
ocomeni | 79:a2187bbfa407 | 391 | wifi_config.security); |
ocomeni | 81:637a87eb8170 | 392 | return error; |
ocomeni | 79:a2187bbfa407 | 393 | } |
ocomeni | 79:a2187bbfa407 | 394 | |
ocomeni | 79:a2187bbfa407 | 395 | |
ocomeni | 79:a2187bbfa407 | 396 | nsapi_error_t WiFiManager::disconnect() |
ocomeni | 78:07bb86e3ce14 | 397 | { |
ocomeni | 79:a2187bbfa407 | 398 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 399 | error = network->disconnect(); |
ocomeni | 79:a2187bbfa407 | 400 | return error; |
ocomeni | 78:07bb86e3ce14 | 401 | } |
ocomeni | 79:a2187bbfa407 | 402 | |
ocomeni | 88:7ffa053be662 | 403 | void WiFiManager::sendResponseDownloadData(at_cmd_resp_t at_cmd, const uint8_t * buf, int bufLen) |
ocomeni | 90:ed0267eca7b5 | 404 | { |
ocomeni | 90:ed0267eca7b5 | 405 | |
ocomeni | 88:7ffa053be662 | 406 | at_data_resp = new at_data_msg_t; |
ocomeni | 88:7ffa053be662 | 407 | at_data_resp->at_resp = at_cmd; |
ocomeni | 88:7ffa053be662 | 408 | size_t bufSize = sizeof(at_data_resp->buffer); |
ocomeni | 88:7ffa053be662 | 409 | int pos = 0; |
ocomeni | 88:7ffa053be662 | 410 | at_data_resp->dataLen = 0; |
ocomeni | 88:7ffa053be662 | 411 | bool queueResult = true; |
ocomeni | 90:ed0267eca7b5 | 412 | int hdrLen = 0; |
ocomeni | 88:7ffa053be662 | 413 | do { |
ocomeni | 90:ed0267eca7b5 | 414 | if(!queueResult) |
ocomeni | 90:ed0267eca7b5 | 415 | { |
ocomeni | 90:ed0267eca7b5 | 416 | wait_ms(10); // wait 10 ms to allow data to be transferred |
ocomeni | 90:ed0267eca7b5 | 417 | } |
ocomeni | 90:ed0267eca7b5 | 418 | else { |
ocomeni | 90:ed0267eca7b5 | 419 | if(http_response_hdr_sent == false){ |
ocomeni | 90:ed0267eca7b5 | 420 | copyResponseHdr2Queue(); |
ocomeni | 90:ed0267eca7b5 | 421 | hdrLen = at_data_resp->dataLen; |
ocomeni | 90:ed0267eca7b5 | 422 | http_response_hdr_sent = true; |
ocomeni | 90:ed0267eca7b5 | 423 | } |
ocomeni | 90:ed0267eca7b5 | 424 | int cpyLen = (bufLen - pos) > bufSize? bufSize : (bufLen - pos - hdrLen ) ; |
ocomeni | 90:ed0267eca7b5 | 425 | at_data_resp->dataLen += cpyLen; |
ocomeni | 90:ed0267eca7b5 | 426 | memcpy(&at_data_resp->buffer[hdrLen], &buf[pos], cpyLen); |
ocomeni | 90:ed0267eca7b5 | 427 | } |
ocomeni | 88:7ffa053be662 | 428 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 90:ed0267eca7b5 | 429 | if(queueResult){ |
ocomeni | 90:ed0267eca7b5 | 430 | pos+= at_data_resp->dataLen; |
ocomeni | 90:ed0267eca7b5 | 431 | at_data_resp->dataLen = 0; |
ocomeni | 90:ed0267eca7b5 | 432 | hdrLen = 0; |
ocomeni | 90:ed0267eca7b5 | 433 | } |
ocomeni | 88:7ffa053be662 | 434 | }while(queueResult == false || pos < bufLen); |
ocomeni | 88:7ffa053be662 | 435 | delete at_data_resp; |
ocomeni | 88:7ffa053be662 | 436 | } |
ocomeni | 79:a2187bbfa407 | 437 | |
ocomeni | 90:ed0267eca7b5 | 438 | void WiFiManager::copyResponseHdr2Queue() |
ocomeni | 90:ed0267eca7b5 | 439 | { |
ocomeni | 90:ed0267eca7b5 | 440 | int numChars = 0; |
ocomeni | 90:ed0267eca7b5 | 441 | // create message pointer for response header generation |
ocomeni | 90:ed0267eca7b5 | 442 | char * msgPtr = (char *)at_data_resp->buffer; |
ocomeni | 90:ed0267eca7b5 | 443 | // do status line |
ocomeni | 90:ed0267eca7b5 | 444 | numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", http_response->get_status_code(), |
ocomeni | 90:ed0267eca7b5 | 445 | http_response->get_status_message().c_str()); |
ocomeni | 90:ed0267eca7b5 | 446 | msgPtr += numChars; |
ocomeni | 90:ed0267eca7b5 | 447 | for (size_t ix = 0; ix < http_response->get_headers_length(); ix++) { |
ocomeni | 90:ed0267eca7b5 | 448 | numChars = sprintf(msgPtr, "%s: %s\r\n", |
ocomeni | 90:ed0267eca7b5 | 449 | http_response->get_headers_fields()[ix]->c_str(), |
ocomeni | 90:ed0267eca7b5 | 450 | http_response->get_headers_values()[ix]->c_str()); |
ocomeni | 90:ed0267eca7b5 | 451 | msgPtr += numChars; |
ocomeni | 90:ed0267eca7b5 | 452 | } |
ocomeni | 90:ed0267eca7b5 | 453 | numChars = sprintf(msgPtr, "\r\n"); |
ocomeni | 90:ed0267eca7b5 | 454 | msgPtr += numChars; |
ocomeni | 90:ed0267eca7b5 | 455 | // print out generated header |
ocomeni | 90:ed0267eca7b5 | 456 | printf("[WiFi MAN] generated response header:\n"); |
ocomeni | 90:ed0267eca7b5 | 457 | printf("%s\r\n", (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 458 | // calculate header length |
ocomeni | 90:ed0267eca7b5 | 459 | at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 460 | } |
ocomeni | 90:ed0267eca7b5 | 461 | |
ocomeni | 84:7c7add00f4bf | 462 | void WiFiManager::return_response(HttpResponse* res) { |
ocomeni | 84:7c7add00f4bf | 463 | |
ocomeni | 88:7ffa053be662 | 464 | at_data_resp = new at_data_msg_t; |
ocomeni | 87:99b37d26ff2a | 465 | int numChars = 0; |
ocomeni | 87:99b37d26ff2a | 466 | // create message pointer for response header generation |
ocomeni | 88:7ffa053be662 | 467 | char * msgPtr = (char *)at_data_resp->buffer; |
ocomeni | 87:99b37d26ff2a | 468 | // do status line |
ocomeni | 87:99b37d26ff2a | 469 | numChars = sprintf(msgPtr, "HTTP/1.1 %d %s\r\n", res->get_status_code(), res->get_status_message().c_str()); |
ocomeni | 87:99b37d26ff2a | 470 | msgPtr += numChars; |
ocomeni | 84:7c7add00f4bf | 471 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
ocomeni | 87:99b37d26ff2a | 472 | numChars = sprintf(msgPtr, "%s: %s\r\n", |
ocomeni | 87:99b37d26ff2a | 473 | res->get_headers_fields()[ix]->c_str(), |
ocomeni | 87:99b37d26ff2a | 474 | res->get_headers_values()[ix]->c_str()); |
ocomeni | 87:99b37d26ff2a | 475 | msgPtr += numChars; |
ocomeni | 84:7c7add00f4bf | 476 | } |
ocomeni | 87:99b37d26ff2a | 477 | numChars = sprintf(msgPtr, "\r\n\r\n"); |
ocomeni | 87:99b37d26ff2a | 478 | msgPtr += numChars; |
ocomeni | 87:99b37d26ff2a | 479 | // print out generated header |
ocomeni | 90:ed0267eca7b5 | 480 | printf("[WiFi MAN] generated response header:\n"); |
ocomeni | 88:7ffa053be662 | 481 | printf("%s\r\n", (char *)at_data_resp->buffer); |
ocomeni | 90:ed0267eca7b5 | 482 | // calculate header length |
ocomeni | 89:45f6db09a76d | 483 | at_data_resp->dataLen = (msgPtr - (char *)at_data_resp->buffer); |
ocomeni | 84:7c7add00f4bf | 484 | |
ocomeni | 90:ed0267eca7b5 | 485 | // package and send on wifi data queue |
ocomeni | 88:7ffa053be662 | 486 | at_data_resp->at_resp = AT_HTTPS_RESP; |
ocomeni | 89:45f6db09a76d | 487 | bool queueResult = true; |
ocomeni | 89:45f6db09a76d | 488 | int wait_count = 0; |
ocomeni | 89:45f6db09a76d | 489 | do |
ocomeni | 89:45f6db09a76d | 490 | { |
ocomeni | 89:45f6db09a76d | 491 | if(!queueResult){ |
ocomeni | 89:45f6db09a76d | 492 | wait_count++; |
ocomeni | 89:45f6db09a76d | 493 | printf("ATCMD Queue full waiting %d ms so far...\n", wait_count*10); |
ocomeni | 89:45f6db09a76d | 494 | wait_ms(10); |
ocomeni | 89:45f6db09a76d | 495 | } |
ocomeni | 89:45f6db09a76d | 496 | queueResult = queueWiFiDataResponse(*at_data_resp); |
ocomeni | 89:45f6db09a76d | 497 | }while(queueResult == false); |
ocomeni | 88:7ffa053be662 | 498 | delete at_data_resp; |
ocomeni | 84:7c7add00f4bf | 499 | } |
ocomeni | 90:ed0267eca7b5 | 500 | |
ocomeni | 90:ed0267eca7b5 | 501 | |
ocomeni | 90:ed0267eca7b5 | 502 | void WiFiManager::printBufferInHex(uint8_t *buf, int pLen) |
ocomeni | 90:ed0267eca7b5 | 503 | { |
ocomeni | 90:ed0267eca7b5 | 504 | for(int i =0;i<pLen;i++){ |
ocomeni | 90:ed0267eca7b5 | 505 | if(i%8==0) printf("\n[%3d]",i); |
ocomeni | 90:ed0267eca7b5 | 506 | printf("%02x ", buf[i]); |
ocomeni | 90:ed0267eca7b5 | 507 | } |
ocomeni | 90:ed0267eca7b5 | 508 | printf("\n"); |
ocomeni | 90:ed0267eca7b5 | 509 | } |
ocomeni | 90:ed0267eca7b5 | 510 | |
ocomeni | 88:7ffa053be662 | 511 | //#define TRY_PRINTF |
ocomeni | 84:7c7add00f4bf | 512 | |
ocomeni | 90:ed0267eca7b5 | 513 | void WiFiManager::body_callback(const char *at, uint32_t length) { |
ocomeni | 84:7c7add00f4bf | 514 | printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); |
ocomeni | 84:7c7add00f4bf | 515 | chunkNum++; |
ocomeni | 89:45f6db09a76d | 516 | sendResponseDownloadData(AT_HTTPS_RESP_DOWNLOAD, (uint8_t *)at, length); |
ocomeni | 84:7c7add00f4bf | 517 | } |
ocomeni | 84:7c7add00f4bf | 518 | |
ocomeni | 88:7ffa053be662 | 519 | |
ocomeni | 88:7ffa053be662 | 520 | bool WiFiManager::createTLSconnection(const char * hostName) |
ocomeni | 88:7ffa053be662 | 521 | { |
ocomeni | 93:06e755a80187 | 522 | #ifdef ENABLE_MBED_TRACE |
ocomeni | 93:06e755a80187 | 523 | mbed_trace_init(); |
ocomeni | 93:06e755a80187 | 524 | #endif |
ocomeni | 88:7ffa053be662 | 525 | socket = new TLSSocket(); |
ocomeni | 88:7ffa053be662 | 526 | |
ocomeni | 88:7ffa053be662 | 527 | nsapi_error_t r; |
ocomeni | 88:7ffa053be662 | 528 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 529 | r = socket->open(network); |
ocomeni | 88:7ffa053be662 | 530 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 531 | { |
ocomeni | 89:45f6db09a76d | 532 | printf("TLS open failed!!\n"); |
ocomeni | 88:7ffa053be662 | 533 | return false; |
ocomeni | 88:7ffa053be662 | 534 | } |
ocomeni | 88:7ffa053be662 | 535 | printf("TLS open passed!!\n"); |
ocomeni | 88:7ffa053be662 | 536 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
ocomeni | 88:7ffa053be662 | 537 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 538 | { |
ocomeni | 88:7ffa053be662 | 539 | printf("TLS set_root_ca_cert failed!!\n"); |
ocomeni | 88:7ffa053be662 | 540 | return false; |
ocomeni | 88:7ffa053be662 | 541 | } |
ocomeni | 88:7ffa053be662 | 542 | printf("TLS set_root_ca_cert passed!!\n"); |
ocomeni | 88:7ffa053be662 | 543 | r = socket->connect(hostName, 443); |
ocomeni | 88:7ffa053be662 | 544 | if(r != NSAPI_ERROR_OK) |
ocomeni | 88:7ffa053be662 | 545 | { |
ocomeni | 93:06e755a80187 | 546 | char errstr[100]; |
ocomeni | 93:06e755a80187 | 547 | mbedtls_strerror(r, errstr, 100); |
ocomeni | 93:06e755a80187 | 548 | printf("TLS connect failed for hostname %s -- ERROR = %s !!\n", hostName, errstr); |
ocomeni | 93:06e755a80187 | 549 | socket->close(); |
ocomeni | 88:7ffa053be662 | 550 | return false; |
ocomeni | 88:7ffa053be662 | 551 | } |
ocomeni | 88:7ffa053be662 | 552 | printf("TLS connection successful for https site : %s\n", hostName); |
ocomeni | 88:7ffa053be662 | 553 | return true; |
ocomeni | 88:7ffa053be662 | 554 | } |
ocomeni | 88:7ffa053be662 | 555 | #define TESTING_HTTPS |
ocomeni | 84:7c7add00f4bf | 556 | void WiFiManager::createHttpsRequest() |
ocomeni | 79:a2187bbfa407 | 557 | { |
ocomeni | 87:99b37d26ff2a | 558 | // reset chunk #; |
ocomeni | 87:99b37d26ff2a | 559 | chunkNum = 0; |
ocomeni | 90:ed0267eca7b5 | 560 | http_response_hdr_sent = false; |
ocomeni | 89:45f6db09a76d | 561 | printf("\n[WIFI MAN] Http Request received:\n"); |
ocomeni | 84:7c7add00f4bf | 562 | http_req_cfg = (http_request_t *) data_msg->buffer; |
ocomeni | 90:ed0267eca7b5 | 563 | printf("\n[WIFI MAN] uri = %s\n", http_req_cfg->request_URI); |
ocomeni | 89:45f6db09a76d | 564 | printf("\n[WIFI MAN] internet cfg url = %s\n", internet_config.url); |
ocomeni | 87:99b37d26ff2a | 565 | char full_url[100]; |
ocomeni | 88:7ffa053be662 | 566 | char host[60] ; |
ocomeni | 87:99b37d26ff2a | 567 | strncpy(full_url,internet_config.url, strlen(internet_config.url)+1); |
ocomeni | 90:ed0267eca7b5 | 568 | strncpy(host,http_req_cfg->hostName, strlen(http_req_cfg->hostName)+1); |
ocomeni | 90:ed0267eca7b5 | 569 | strncat(full_url, http_req_cfg->request_URI, strlen(http_req_cfg->request_URI)+1); |
ocomeni | 87:99b37d26ff2a | 570 | printf("\n[WIFI MAN] server url+uri = %s\n", full_url); |
ocomeni | 90:ed0267eca7b5 | 571 | printf("\n[WIFI MAN] Host = %s\n", http_req_cfg->hostName); |
ocomeni | 90:ed0267eca7b5 | 572 | printf("\n[WIFI MAN] Accept = %s\n", http_req_cfg->AcceptVal); |
ocomeni | 90:ed0267eca7b5 | 573 | printf("\n[WIFI MAN] Content-Type = %s\n", http_req_cfg->contentType); |
ocomeni | 90:ed0267eca7b5 | 574 | printf("\n[WIFI MAN] contentLenstr = %s\n", http_req_cfg->contentLen); |
ocomeni | 84:7c7add00f4bf | 575 | |
ocomeni | 84:7c7add00f4bf | 576 | int bodyLen; |
ocomeni | 90:ed0267eca7b5 | 577 | sscanf(http_req_cfg->contentLen, "%d", &bodyLen); |
ocomeni | 90:ed0267eca7b5 | 578 | printf("contenLenstr = %s bodyLen = %d\n", http_req_cfg->contentLen, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 579 | |
ocomeni | 90:ed0267eca7b5 | 580 | if(bodyLen > 10){ |
ocomeni | 93:06e755a80187 | 581 | printf("\n [WIFI MAN] Message Body:\n"); |
ocomeni | 90:ed0267eca7b5 | 582 | printBufferInHex(http_req_cfg->body, bodyLen); |
ocomeni | 90:ed0267eca7b5 | 583 | } |
ocomeni | 87:99b37d26ff2a | 584 | if(strstr(internet_config.url, "http:")!=NULL) // http request |
ocomeni | 87:99b37d26ff2a | 585 | { |
ocomeni | 87:99b37d26ff2a | 586 | http_request = new HttpRequest(network, |
ocomeni | 90:ed0267eca7b5 | 587 | http_req_cfg->method, |
ocomeni | 90:ed0267eca7b5 | 588 | full_url, |
ocomeni | 90:ed0267eca7b5 | 589 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 87:99b37d26ff2a | 590 | setHttpHeader("Host", http_req_cfg->hostName); |
ocomeni | 87:99b37d26ff2a | 591 | setHttpHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 89:45f6db09a76d | 592 | printf("http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 593 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 88:7ffa053be662 | 594 | printf("HTTP_GET -- ignoring body\n"); |
ocomeni | 88:7ffa053be662 | 595 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 596 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 597 | http_response = http_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 598 | } |
ocomeni | 88:7ffa053be662 | 599 | else{ |
ocomeni | 88:7ffa053be662 | 600 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 601 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 602 | http_response = http_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 603 | } |
ocomeni | 87:99b37d26ff2a | 604 | free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 605 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 606 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 607 | mbedtls_strerror(http_request->get_error(), buf, 100); |
ocomeni | 87:99b37d26ff2a | 608 | printf("HttpRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 609 | //printf("HttpsRequest failed (error code %d)\n", https_request->get_error()); |
ocomeni | 87:99b37d26ff2a | 610 | delete http_request; // free the memory |
ocomeni | 87:99b37d26ff2a | 611 | return; |
ocomeni | 87:99b37d26ff2a | 612 | } |
ocomeni | 88:7ffa053be662 | 613 | delete http_request; // free the memory |
ocomeni | 87:99b37d26ff2a | 614 | printf("\n----- HTTP POST response -----\n"); |
ocomeni | 84:7c7add00f4bf | 615 | } |
ocomeni | 87:99b37d26ff2a | 616 | else |
ocomeni | 87:99b37d26ff2a | 617 | { |
ocomeni | 90:ed0267eca7b5 | 618 | #ifndef DONT_USE_TLS_SOCKET |
ocomeni | 88:7ffa053be662 | 619 | if(https_connection_active == false){ |
ocomeni | 88:7ffa053be662 | 620 | bool tlsResult; |
ocomeni | 88:7ffa053be662 | 621 | tlsResult = createTLSconnection(host); |
ocomeni | 93:06e755a80187 | 622 | #ifdef ENABLE_MBED_TRACE |
ocomeni | 93:06e755a80187 | 623 | mbed_trace_free(); // free trace memory |
ocomeni | 93:06e755a80187 | 624 | #endif |
ocomeni | 88:7ffa053be662 | 625 | if(tlsResult == false){ |
ocomeni | 88:7ffa053be662 | 626 | delete socket; |
ocomeni | 88:7ffa053be662 | 627 | free_DataMsg(); |
ocomeni | 88:7ffa053be662 | 628 | return; |
ocomeni | 88:7ffa053be662 | 629 | } |
ocomeni | 88:7ffa053be662 | 630 | printf("[create https] TLS connection successful for https site : %s\n", host); |
ocomeni | 88:7ffa053be662 | 631 | } |
ocomeni | 90:ed0267eca7b5 | 632 | printf("after call to createTLSconnection \n"); |
ocomeni | 90:ed0267eca7b5 | 633 | print_memory_info(); |
ocomeni | 88:7ffa053be662 | 634 | // Pass in `socket`, instead of `network` as first argument, and omit the `SSL_CA_PEM` argument |
ocomeni | 88:7ffa053be662 | 635 | //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://httpbin.org/status/418"); |
ocomeni | 88:7ffa053be662 | 636 | //_wmutex.lock(); |
ocomeni | 88:7ffa053be662 | 637 | https_request = new HttpsRequest(socket, |
ocomeni | 87:99b37d26ff2a | 638 | http_req_cfg->method, |
ocomeni | 88:7ffa053be662 | 639 | full_url, |
ocomeni | 87:99b37d26ff2a | 640 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 90:ed0267eca7b5 | 641 | #else |
ocomeni | 90:ed0267eca7b5 | 642 | https_request = new HttpsRequest(network, |
ocomeni | 90:ed0267eca7b5 | 643 | SSL_CA_PEM, |
ocomeni | 90:ed0267eca7b5 | 644 | http_req_cfg->method, |
ocomeni | 90:ed0267eca7b5 | 645 | full_url, |
ocomeni | 90:ed0267eca7b5 | 646 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 90:ed0267eca7b5 | 647 | #endif |
ocomeni | 88:7ffa053be662 | 648 | #ifdef TESTING_HTTPS |
ocomeni | 90:ed0267eca7b5 | 649 | printf("http_req_cfg->method = %d\n", http_req_cfg->method); |
ocomeni | 88:7ffa053be662 | 650 | if(http_req_cfg->method == HTTP_GET){ |
ocomeni | 88:7ffa053be662 | 651 | printf("HTTP_GET -- ignoring body\n"); |
ocomeni | 89:45f6db09a76d | 652 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 653 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 654 | //setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 655 | //setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 656 | http_response = https_request->send(NULL, 0); |
ocomeni | 88:7ffa053be662 | 657 | } |
ocomeni | 88:7ffa053be662 | 658 | else{ |
ocomeni | 89:45f6db09a76d | 659 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 660 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 90:ed0267eca7b5 | 661 | setHttpsHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 90:ed0267eca7b5 | 662 | setHttpsHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 88:7ffa053be662 | 663 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 664 | } |
ocomeni | 88:7ffa053be662 | 665 | #else |
ocomeni | 89:45f6db09a76d | 666 | setHttpsHeader("Host", http_req_cfg->hostName); |
ocomeni | 89:45f6db09a76d | 667 | setHttpsHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 88:7ffa053be662 | 668 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 88:7ffa053be662 | 669 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 87:99b37d26ff2a | 670 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 88:7ffa053be662 | 671 | #endif |
ocomeni | 88:7ffa053be662 | 672 | |
ocomeni | 88:7ffa053be662 | 673 | //_wmutex.unlock(); |
ocomeni | 90:ed0267eca7b5 | 674 | //free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 675 | if (!http_response) { |
ocomeni | 87:99b37d26ff2a | 676 | char buf[100]; |
ocomeni | 87:99b37d26ff2a | 677 | mbedtls_strerror(https_request->get_error(), buf, 100); |
ocomeni | 88:7ffa053be662 | 678 | printf("HttpsRequest failed (error code %s)\n", buf); |
ocomeni | 87:99b37d26ff2a | 679 | delete https_request; // free the memory |
ocomeni | 88:7ffa053be662 | 680 | https_connection_active = false; // reset true whenever connection fails |
ocomeni | 88:7ffa053be662 | 681 | delete socket; |
ocomeni | 90:ed0267eca7b5 | 682 | free_DataMsg(); |
ocomeni | 87:99b37d26ff2a | 683 | return; |
ocomeni | 87:99b37d26ff2a | 684 | } |
ocomeni | 88:7ffa053be662 | 685 | https_connection_active = true; // set true whenever connection succeeds |
ocomeni | 87:99b37d26ff2a | 686 | printf("\n----- HTTPS POST response -----\r\n"); |
ocomeni | 87:99b37d26ff2a | 687 | } |
ocomeni | 90:ed0267eca7b5 | 688 | if(http_response != NULL){ |
ocomeni | 90:ed0267eca7b5 | 689 | //return_response(http_response); |
ocomeni | 90:ed0267eca7b5 | 690 | delete http_response; // free the response memory |
ocomeni | 90:ed0267eca7b5 | 691 | } |
ocomeni | 90:ed0267eca7b5 | 692 | free_DataMsg(); |
ocomeni | 90:ed0267eca7b5 | 693 | //delete https_request; // free the request memory |
ocomeni | 79:a2187bbfa407 | 694 | } |
ocomeni | 79:a2187bbfa407 | 695 | |
ocomeni | 79:a2187bbfa407 | 696 | void WiFiManager::createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 697 | const char* url, |
ocomeni | 79:a2187bbfa407 | 698 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 699 | { |
ocomeni | 84:7c7add00f4bf | 700 | http_request = new HttpRequest(network, |
ocomeni | 84:7c7add00f4bf | 701 | method, url, body_callback);; |
ocomeni | 79:a2187bbfa407 | 702 | } |
ocomeni | 79:a2187bbfa407 | 703 | |
ocomeni | 79:a2187bbfa407 | 704 | void WiFiManager::setHttpHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 705 | { |
ocomeni | 79:a2187bbfa407 | 706 | http_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 707 | } |
ocomeni | 79:a2187bbfa407 | 708 | |
ocomeni | 79:a2187bbfa407 | 709 | void WiFiManager::setHttpsHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 710 | { |
ocomeni | 79:a2187bbfa407 | 711 | https_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 712 | } |
ocomeni | 79:a2187bbfa407 | 713 | |
ocomeni | 79:a2187bbfa407 | 714 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 715 | { |
ocomeni | 78:07bb86e3ce14 | 716 | } |
ocomeni | 79:a2187bbfa407 | 717 | |
ocomeni | 79:a2187bbfa407 | 718 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 719 | { |
ocomeni | 78:07bb86e3ce14 | 720 | } |
ocomeni | 79:a2187bbfa407 | 721 |