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