Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.cpp@85:9f896e1e041c, 2019-03-28 (annotated)
- Committer:
- ocomeni
- Date:
- Thu Mar 28 23:03:33 2019 +0000
- Revision:
- 85:9f896e1e041c
- Parent:
- 84:7c7add00f4bf
- Child:
- 86:04fc2fcda7ec
http request getting to wifi manager
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 | 80:e8f0e92e3ac9 | 10 | MemoryPool<wifi_data_msg_t, 4> *aT2WiFiDatamPool, |
ocomeni | 80:e8f0e92e3ac9 | 11 | Queue<wifi_data_msg_t, 4> *aT2WiFiDataQueue, |
ocomeni | 81:637a87eb8170 | 12 | MemoryPool<at_data_msg_t, 4> *wiFi2ATDatamPool, |
ocomeni | 81:637a87eb8170 | 13 | Queue<at_data_msg_t, 4> *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 | 78:07bb86e3ce14 | 35 | } |
ocomeni | 78:07bb86e3ce14 | 36 | |
ocomeni | 78:07bb86e3ce14 | 37 | WiFiManager::~WiFiManager() |
ocomeni | 78:07bb86e3ce14 | 38 | { |
ocomeni | 78:07bb86e3ce14 | 39 | } |
ocomeni | 79:a2187bbfa407 | 40 | |
ocomeni | 81:637a87eb8170 | 41 | bool WiFiManager::queueATresponse(at_cmd_resp_t resp){ |
ocomeni | 81:637a87eb8170 | 42 | at_resp_message_t *atResp = _wiFi2ATmPool->alloc(); |
ocomeni | 81:637a87eb8170 | 43 | atResp->at_resp = resp; |
ocomeni | 81:637a87eb8170 | 44 | _wiFi2ATCmdQueue->put(atResp); |
ocomeni | 81:637a87eb8170 | 45 | return true; |
ocomeni | 81:637a87eb8170 | 46 | } |
ocomeni | 81:637a87eb8170 | 47 | |
ocomeni | 81:637a87eb8170 | 48 | |
ocomeni | 81:637a87eb8170 | 49 | bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){ |
ocomeni | 81:637a87eb8170 | 50 | at_data_msg_t *atData = _wiFi2ATDatamPool->alloc(); |
ocomeni | 81:637a87eb8170 | 51 | atData->at_resp = at_resp.at_resp; |
ocomeni | 81:637a87eb8170 | 52 | atData->dataLen = at_resp.dataLen; |
ocomeni | 81:637a87eb8170 | 53 | memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen); |
ocomeni | 81:637a87eb8170 | 54 | _wiFi2ATDataQueue->put(atData); |
ocomeni | 81:637a87eb8170 | 55 | return true; |
ocomeni | 81:637a87eb8170 | 56 | } |
ocomeni | 81:637a87eb8170 | 57 | |
ocomeni | 79:a2187bbfa407 | 58 | |
ocomeni | 79:a2187bbfa407 | 59 | |
ocomeni | 79:a2187bbfa407 | 60 | void WiFiManager::runMain(){ |
ocomeni | 81:637a87eb8170 | 61 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 62 | while(true){ |
ocomeni | 79:a2187bbfa407 | 63 | dequeueWiFiCommands(); |
ocomeni | 81:637a87eb8170 | 64 | dequeueATdataResponse(); |
ocomeni | 79:a2187bbfa407 | 65 | switch(wifiCmd){ |
ocomeni | 79:a2187bbfa407 | 66 | case WIFI_CMD_NONE: |
ocomeni | 79:a2187bbfa407 | 67 | // IDLE STATE |
ocomeni | 79:a2187bbfa407 | 68 | break; |
ocomeni | 79:a2187bbfa407 | 69 | case WIFI_CMD_SCAN: |
ocomeni | 79:a2187bbfa407 | 70 | error = scanNetworks(); |
ocomeni | 79:a2187bbfa407 | 71 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 72 | queueATresponse(AT_SCAN_RESP); |
ocomeni | 81:637a87eb8170 | 73 | break; |
ocomeni | 81:637a87eb8170 | 74 | case WIFI_CMD_DETAILED_SCAN: |
ocomeni | 81:637a87eb8170 | 75 | nsapi_size_or_error_t cnt_err; |
ocomeni | 81:637a87eb8170 | 76 | cnt_err = getAvailableAPs(lastScanCount); |
ocomeni | 81:637a87eb8170 | 77 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 78 | queueATresponse(AT_DETAILED_SCAN_RESP); |
ocomeni | 79:a2187bbfa407 | 79 | break; |
ocomeni | 79:a2187bbfa407 | 80 | case WIFI_CMD_CONNECT: |
ocomeni | 81:637a87eb8170 | 81 | error = connect(); |
ocomeni | 81:637a87eb8170 | 82 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 83 | queueATresponse(AT_CONNECT_RESP); |
ocomeni | 79:a2187bbfa407 | 84 | break; |
ocomeni | 79:a2187bbfa407 | 85 | case WIFI_CMD_DISCONNECT: |
ocomeni | 81:637a87eb8170 | 86 | error = disconnect(); |
ocomeni | 81:637a87eb8170 | 87 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 88 | queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 89 | break; |
ocomeni | 81:637a87eb8170 | 90 | case WIFI_CMD_CONFIG: |
ocomeni | 81:637a87eb8170 | 91 | set_WIFI_CONFIG(); |
ocomeni | 81:637a87eb8170 | 92 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 93 | queueATresponse(AT_CONFIG_RESP); |
ocomeni | 82:10072c1794d3 | 94 | break; |
ocomeni | 81:637a87eb8170 | 95 | case WIFI_CMD_INTERNET_CONFIG: |
ocomeni | 81:637a87eb8170 | 96 | set_internet_config(); |
ocomeni | 81:637a87eb8170 | 97 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 98 | queueATresponse(AT_INTERNET_CONFIG_RESP); |
ocomeni | 79:a2187bbfa407 | 99 | break; |
ocomeni | 79:a2187bbfa407 | 100 | case WIFI_CMD_SEND_HTTPS_REQ: |
ocomeni | 84:7c7add00f4bf | 101 | createHttpsRequest(); |
ocomeni | 84:7c7add00f4bf | 102 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 79:a2187bbfa407 | 103 | break; |
ocomeni | 79:a2187bbfa407 | 104 | case WIFI_CMD_SEND_HTTP_REQ: |
ocomeni | 79:a2187bbfa407 | 105 | break; |
ocomeni | 79:a2187bbfa407 | 106 | default: |
ocomeni | 79:a2187bbfa407 | 107 | break; |
ocomeni | 79:a2187bbfa407 | 108 | } |
ocomeni | 79:a2187bbfa407 | 109 | wait_ms(100); // |
ocomeni | 79:a2187bbfa407 | 110 | } |
ocomeni | 79:a2187bbfa407 | 111 | |
ocomeni | 78:07bb86e3ce14 | 112 | } |
ocomeni | 79:a2187bbfa407 | 113 | |
ocomeni | 79:a2187bbfa407 | 114 | bool WiFiManager::dequeueWiFiCommands(){ |
ocomeni | 81:637a87eb8170 | 115 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 116 | osEvent evt = _aT2WiFiCmdQueue->get(0); |
ocomeni | 79:a2187bbfa407 | 117 | if(evt.status == osEventMessage){ |
ocomeni | 79:a2187bbfa407 | 118 | wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p; |
ocomeni | 79:a2187bbfa407 | 119 | setNextCommand(cmd->wifi_cmd); |
ocomeni | 79:a2187bbfa407 | 120 | _aT2WiFimPool->free(cmd); |
ocomeni | 79:a2187bbfa407 | 121 | } |
ocomeni | 79:a2187bbfa407 | 122 | return true; |
ocomeni | 79:a2187bbfa407 | 123 | } |
ocomeni | 79:a2187bbfa407 | 124 | |
ocomeni | 79:a2187bbfa407 | 125 | |
ocomeni | 81:637a87eb8170 | 126 | bool WiFiManager::dequeueATdataResponse(){ |
ocomeni | 81:637a87eb8170 | 127 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 128 | osEvent evt = _aT2WiFiDataQueue->get(0); |
ocomeni | 81:637a87eb8170 | 129 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 130 | data_msg = (wifi_data_msg_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 131 | setNextCommand(data_msg->wifi_cmd); |
ocomeni | 81:637a87eb8170 | 132 | //_wiFi2ATDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 133 | } |
ocomeni | 81:637a87eb8170 | 134 | return true; |
ocomeni | 81:637a87eb8170 | 135 | } |
ocomeni | 81:637a87eb8170 | 136 | |
ocomeni | 81:637a87eb8170 | 137 | |
ocomeni | 79:a2187bbfa407 | 138 | bool WiFiManager::setNextCommand(wifi_cmd_t cmd) |
ocomeni | 78:07bb86e3ce14 | 139 | { |
ocomeni | 79:a2187bbfa407 | 140 | printf("\n [WIFI-MAN] About to set next WiFi manager command \n"); |
ocomeni | 79:a2187bbfa407 | 141 | if(wifiCmd == WIFI_CMD_NONE){ |
ocomeni | 79:a2187bbfa407 | 142 | wifiCmd = cmd; |
ocomeni | 79:a2187bbfa407 | 143 | return true; // success |
ocomeni | 79:a2187bbfa407 | 144 | } |
ocomeni | 79:a2187bbfa407 | 145 | return false; // wiFiManager busy |
ocomeni | 78:07bb86e3ce14 | 146 | } |
ocomeni | 79:a2187bbfa407 | 147 | |
ocomeni | 81:637a87eb8170 | 148 | const char * WiFiManager::sec2str(nsapi_security_t sec) |
ocomeni | 81:637a87eb8170 | 149 | { |
ocomeni | 81:637a87eb8170 | 150 | switch (sec) { |
ocomeni | 81:637a87eb8170 | 151 | case NSAPI_SECURITY_NONE: |
ocomeni | 81:637a87eb8170 | 152 | return "None"; |
ocomeni | 81:637a87eb8170 | 153 | case NSAPI_SECURITY_WEP: |
ocomeni | 81:637a87eb8170 | 154 | return "WEP"; |
ocomeni | 81:637a87eb8170 | 155 | case NSAPI_SECURITY_WPA: |
ocomeni | 81:637a87eb8170 | 156 | return "WPA"; |
ocomeni | 81:637a87eb8170 | 157 | case NSAPI_SECURITY_WPA2: |
ocomeni | 81:637a87eb8170 | 158 | return "WPA2"; |
ocomeni | 81:637a87eb8170 | 159 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 81:637a87eb8170 | 160 | return "WPA/WPA2"; |
ocomeni | 81:637a87eb8170 | 161 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 81:637a87eb8170 | 162 | default: |
ocomeni | 81:637a87eb8170 | 163 | return "Unknown"; |
ocomeni | 81:637a87eb8170 | 164 | } |
ocomeni | 81:637a87eb8170 | 165 | } |
ocomeni | 81:637a87eb8170 | 166 | |
ocomeni | 79:a2187bbfa407 | 167 | |
ocomeni | 79:a2187bbfa407 | 168 | nsapi_size_or_error_t WiFiManager::scanNetworks() |
ocomeni | 79:a2187bbfa407 | 169 | { |
ocomeni | 79:a2187bbfa407 | 170 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 171 | printf("\n [WIFI-MAN] About to start scan for WiFi networks\n"); |
ocomeni | 79:a2187bbfa407 | 172 | lastScanCount = network->scan(NULL, 0); |
ocomeni | 79:a2187bbfa407 | 173 | printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n"); |
ocomeni | 79:a2187bbfa407 | 174 | return lastScanCount; |
ocomeni | 79:a2187bbfa407 | 175 | } |
ocomeni | 79:a2187bbfa407 | 176 | |
ocomeni | 79:a2187bbfa407 | 177 | |
ocomeni | 81:637a87eb8170 | 178 | //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, |
ocomeni | 81:637a87eb8170 | 179 | // nsapi_size_t ncount) |
ocomeni | 81:637a87eb8170 | 180 | nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount) |
ocomeni | 79:a2187bbfa407 | 181 | { |
ocomeni | 81:637a87eb8170 | 182 | WiFiAccessPoint *ap; |
ocomeni | 81:637a87eb8170 | 183 | nsapi_size_or_error_t count; |
ocomeni | 81:637a87eb8170 | 184 | count = ncount; |
ocomeni | 81:637a87eb8170 | 185 | //count = wiFiManager->scanNetworks(); |
ocomeni | 81:637a87eb8170 | 186 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 187 | //_smutex.lock(); |
ocomeni | 81:637a87eb8170 | 188 | printf("[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 189 | //_smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 190 | return; |
ocomeni | 81:637a87eb8170 | 191 | } |
ocomeni | 81:637a87eb8170 | 192 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 81:637a87eb8170 | 193 | count = count < 15 ? count : 15; |
ocomeni | 81:637a87eb8170 | 194 | ap = new WiFiAccessPoint[count]; |
ocomeni | 81:637a87eb8170 | 195 | count = network->scan(ap, count); |
ocomeni | 81:637a87eb8170 | 196 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 197 | printf("[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 198 | return; |
ocomeni | 81:637a87eb8170 | 199 | } |
ocomeni | 81:637a87eb8170 | 200 | |
ocomeni | 81:637a87eb8170 | 201 | for (int i = 0; i < count; i++) { |
ocomeni | 81:637a87eb8170 | 202 | 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 | 203 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 81:637a87eb8170 | 204 | 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 | 205 | } |
ocomeni | 81:637a87eb8170 | 206 | printf("[WIFI-MAN] %d networks available.\n", count); |
ocomeni | 81:637a87eb8170 | 207 | |
ocomeni | 81:637a87eb8170 | 208 | delete[] ap; |
ocomeni | 79:a2187bbfa407 | 209 | return count; |
ocomeni | 79:a2187bbfa407 | 210 | } |
ocomeni | 79:a2187bbfa407 | 211 | |
ocomeni | 79:a2187bbfa407 | 212 | |
ocomeni | 81:637a87eb8170 | 213 | void WiFiManager::set_WIFI_CONFIG() |
ocomeni | 81:637a87eb8170 | 214 | { |
ocomeni | 81:637a87eb8170 | 215 | wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer; |
ocomeni | 82:10072c1794d3 | 216 | if(wifi_cfg->ssid[0] != NULL)set_WIFI_SSID(wifi_cfg->ssid); |
ocomeni | 82:10072c1794d3 | 217 | if(wifi_cfg->pass[0] != NULL)set_WIFI_PASSWORD(wifi_cfg->pass); |
ocomeni | 82:10072c1794d3 | 218 | if(wifi_cfg->security != NSAPI_SECURITY_UNKNOWN)set_WIFI_SECURITY(wifi_cfg->security); |
ocomeni | 81:637a87eb8170 | 219 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 220 | } |
ocomeni | 81:637a87eb8170 | 221 | |
ocomeni | 78:07bb86e3ce14 | 222 | void WiFiManager::set_WIFI_SSID(char * wifi_ssid) |
ocomeni | 78:07bb86e3ce14 | 223 | { |
ocomeni | 78:07bb86e3ce14 | 224 | strcpy(wifi_config.ssid, wifi_ssid); |
ocomeni | 82:10072c1794d3 | 225 | printf("[WIFI-MAN] wifi_ssid set to %s\n", wifi_config.ssid); |
ocomeni | 82:10072c1794d3 | 226 | |
ocomeni | 78:07bb86e3ce14 | 227 | } |
ocomeni | 79:a2187bbfa407 | 228 | |
ocomeni | 79:a2187bbfa407 | 229 | |
ocomeni | 78:07bb86e3ce14 | 230 | void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass) |
ocomeni | 78:07bb86e3ce14 | 231 | { |
ocomeni | 78:07bb86e3ce14 | 232 | strcpy(wifi_config.pass, wifi_pass); |
ocomeni | 82:10072c1794d3 | 233 | printf("[WIFI-MAN] wifi_pass set to %s\n", wifi_config.pass); |
ocomeni | 78:07bb86e3ce14 | 234 | } |
ocomeni | 79:a2187bbfa407 | 235 | |
ocomeni | 79:a2187bbfa407 | 236 | |
ocomeni | 78:07bb86e3ce14 | 237 | void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security) |
ocomeni | 78:07bb86e3ce14 | 238 | { |
ocomeni | 78:07bb86e3ce14 | 239 | wifi_config.security = wifi_security; |
ocomeni | 82:10072c1794d3 | 240 | printf("[WIFI-MAN] wifi_security set to %s\n", sec2str(wifi_config.security)); |
ocomeni | 78:07bb86e3ce14 | 241 | } |
ocomeni | 79:a2187bbfa407 | 242 | |
ocomeni | 79:a2187bbfa407 | 243 | |
ocomeni | 81:637a87eb8170 | 244 | |
ocomeni | 81:637a87eb8170 | 245 | void WiFiManager::set_internet_config() |
ocomeni | 81:637a87eb8170 | 246 | { |
ocomeni | 81:637a87eb8170 | 247 | internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer; |
ocomeni | 81:637a87eb8170 | 248 | internet_config.peer_id = internet_cfg->peer_id; |
ocomeni | 84:7c7add00f4bf | 249 | strncpy(internet_config.url,internet_cfg->url, strlen(internet_cfg->url)+1); |
ocomeni | 81:637a87eb8170 | 250 | internet_config.connectionScheme = internet_cfg->connectionScheme; |
ocomeni | 81:637a87eb8170 | 251 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 252 | printf("[WIFI MAN] Internet configuration setup completed\n"); |
ocomeni | 81:637a87eb8170 | 253 | printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, |
ocomeni | 84:7c7add00f4bf | 254 | internet_config.url, |
ocomeni | 81:637a87eb8170 | 255 | internet_config.connectionScheme); |
ocomeni | 81:637a87eb8170 | 256 | } |
ocomeni | 81:637a87eb8170 | 257 | |
ocomeni | 81:637a87eb8170 | 258 | void WiFiManager::free_DataMsg() |
ocomeni | 81:637a87eb8170 | 259 | { |
ocomeni | 81:637a87eb8170 | 260 | // free memory after processing |
ocomeni | 81:637a87eb8170 | 261 | _aT2WiFiDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 262 | } |
ocomeni | 81:637a87eb8170 | 263 | |
ocomeni | 81:637a87eb8170 | 264 | |
ocomeni | 81:637a87eb8170 | 265 | void WiFiManager::status_callback(nsapi_event_t status, intptr_t param) |
ocomeni | 81:637a87eb8170 | 266 | { |
ocomeni | 81:637a87eb8170 | 267 | //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) { |
ocomeni | 81:637a87eb8170 | 268 | //} |
ocomeni | 81:637a87eb8170 | 269 | switch(param) { |
ocomeni | 81:637a87eb8170 | 270 | case NSAPI_STATUS_LOCAL_UP: |
ocomeni | 81:637a87eb8170 | 271 | printf("[WIFI-MAN] Local IP address set!\r\n"); |
ocomeni | 81:637a87eb8170 | 272 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 273 | break; |
ocomeni | 81:637a87eb8170 | 274 | case NSAPI_STATUS_GLOBAL_UP: |
ocomeni | 81:637a87eb8170 | 275 | printf("Global IP address set!\r\n"); |
ocomeni | 81:637a87eb8170 | 276 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 277 | printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid); |
ocomeni | 81:637a87eb8170 | 278 | is_connected = true; |
ocomeni | 81:637a87eb8170 | 279 | break; |
ocomeni | 81:637a87eb8170 | 280 | case NSAPI_STATUS_DISCONNECTED: |
ocomeni | 81:637a87eb8170 | 281 | printf("No connection to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 282 | printf("\n [WIFI-MAN] No connection to network!\n"); |
ocomeni | 81:637a87eb8170 | 283 | is_connected = false; |
ocomeni | 81:637a87eb8170 | 284 | //queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 285 | // attempt reconnection if always connected scheme is set |
ocomeni | 81:637a87eb8170 | 286 | if(internet_config.connectionScheme == ALWAYS_CONNECTED) |
ocomeni | 81:637a87eb8170 | 287 | { |
ocomeni | 81:637a87eb8170 | 288 | nsapi_error_t error; |
ocomeni | 81:637a87eb8170 | 289 | error = scanNetworks(); |
ocomeni | 81:637a87eb8170 | 290 | queueATresponse(WIFI_RECONNECT_INFO); |
ocomeni | 81:637a87eb8170 | 291 | } |
ocomeni | 81:637a87eb8170 | 292 | break; |
ocomeni | 81:637a87eb8170 | 293 | case NSAPI_STATUS_CONNECTING: |
ocomeni | 81:637a87eb8170 | 294 | printf("Connecting to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 295 | break; |
ocomeni | 81:637a87eb8170 | 296 | default: |
ocomeni | 81:637a87eb8170 | 297 | printf("Not supported"); |
ocomeni | 81:637a87eb8170 | 298 | break; |
ocomeni | 81:637a87eb8170 | 299 | } |
ocomeni | 81:637a87eb8170 | 300 | } |
ocomeni | 81:637a87eb8170 | 301 | |
ocomeni | 81:637a87eb8170 | 302 | |
ocomeni | 81:637a87eb8170 | 303 | |
ocomeni | 79:a2187bbfa407 | 304 | // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */ |
ocomeni | 79:a2187bbfa407 | 305 | // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */ |
ocomeni | 79:a2187bbfa407 | 306 | // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */ |
ocomeni | 79:a2187bbfa407 | 307 | // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */ |
ocomeni | 79:a2187bbfa407 | 308 | // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED |
ocomeni | 79:a2187bbfa407 | 309 | |
ocomeni | 79:a2187bbfa407 | 310 | nsapi_error_t WiFiManager::connect() |
ocomeni | 79:a2187bbfa407 | 311 | { |
ocomeni | 79:a2187bbfa407 | 312 | nsapi_error_t error; |
ocomeni | 81:637a87eb8170 | 313 | printf("\n [WIFI-MAN] About to connect to WiFi network\n"); |
ocomeni | 81:637a87eb8170 | 314 | network->attach(callback(this, &WiFiManager::status_callback)); |
ocomeni | 79:a2187bbfa407 | 315 | error = network->set_blocking(false); |
ocomeni | 79:a2187bbfa407 | 316 | if(error) |
ocomeni | 79:a2187bbfa407 | 317 | { |
ocomeni | 79:a2187bbfa407 | 318 | printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n"); |
ocomeni | 79:a2187bbfa407 | 319 | return error; |
ocomeni | 79:a2187bbfa407 | 320 | } |
ocomeni | 81:637a87eb8170 | 321 | printf("[WIFI-MAN] Connecting to network %s\n", wifi_config.ssid); |
ocomeni | 79:a2187bbfa407 | 322 | error = network->connect(wifi_config.ssid, |
ocomeni | 79:a2187bbfa407 | 323 | wifi_config.pass, |
ocomeni | 79:a2187bbfa407 | 324 | wifi_config.security); |
ocomeni | 81:637a87eb8170 | 325 | return error; |
ocomeni | 81:637a87eb8170 | 326 | /* |
ocomeni | 79:a2187bbfa407 | 327 | if(error) |
ocomeni | 79:a2187bbfa407 | 328 | { |
ocomeni | 79:a2187bbfa407 | 329 | printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n"); |
ocomeni | 79:a2187bbfa407 | 330 | return error; |
ocomeni | 79:a2187bbfa407 | 331 | } |
ocomeni | 79:a2187bbfa407 | 332 | nsapi_connection_status_t conn_status = NSAPI_STATUS_CONNECTING; |
ocomeni | 79:a2187bbfa407 | 333 | int loopCount = 0; |
ocomeni | 79:a2187bbfa407 | 334 | while(conn_status == NSAPI_STATUS_CONNECTING){ |
ocomeni | 79:a2187bbfa407 | 335 | loopCount++; |
ocomeni | 79:a2187bbfa407 | 336 | conn_status = network->get_connection_status(); |
ocomeni | 79:a2187bbfa407 | 337 | printf("\n [WIFI-MAN] Waiting for WiFi network connect to complete asynchronously [status = %d] - %d\n", conn_status, loopCount); |
ocomeni | 79:a2187bbfa407 | 338 | wait(0.1); |
ocomeni | 79:a2187bbfa407 | 339 | } |
ocomeni | 79:a2187bbfa407 | 340 | if(conn_status < 0) |
ocomeni | 79:a2187bbfa407 | 341 | { |
ocomeni | 79:a2187bbfa407 | 342 | printf("\n [WIFI-MAN] Error connecting to Wifi -- %d!! - \n", conn_status); |
ocomeni | 79:a2187bbfa407 | 343 | return conn_status; |
ocomeni | 79:a2187bbfa407 | 344 | } |
ocomeni | 81:637a87eb8170 | 345 | printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid); |
ocomeni | 81:637a87eb8170 | 346 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 79:a2187bbfa407 | 347 | return conn_status; |
ocomeni | 81:637a87eb8170 | 348 | */ |
ocomeni | 79:a2187bbfa407 | 349 | } |
ocomeni | 79:a2187bbfa407 | 350 | |
ocomeni | 79:a2187bbfa407 | 351 | |
ocomeni | 79:a2187bbfa407 | 352 | nsapi_error_t WiFiManager::disconnect() |
ocomeni | 78:07bb86e3ce14 | 353 | { |
ocomeni | 79:a2187bbfa407 | 354 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 355 | error = network->disconnect(); |
ocomeni | 79:a2187bbfa407 | 356 | return error; |
ocomeni | 78:07bb86e3ce14 | 357 | } |
ocomeni | 79:a2187bbfa407 | 358 | |
ocomeni | 79:a2187bbfa407 | 359 | |
ocomeni | 79:a2187bbfa407 | 360 | /* |
ocomeni | 79:a2187bbfa407 | 361 | |
ocomeni | 79:a2187bbfa407 | 362 | HttpsRequest(NetworkInterface* network, |
ocomeni | 79:a2187bbfa407 | 363 | const char* ssl_ca_pem, |
ocomeni | 79:a2187bbfa407 | 364 | http_method method, |
ocomeni | 79:a2187bbfa407 | 365 | const char* url, |
ocomeni | 79:a2187bbfa407 | 366 | Callback<void(const char *at, uint32_t length)> body_callback = 0) |
ocomeni | 79:a2187bbfa407 | 367 | HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response); |
ocomeni | 79:a2187bbfa407 | 368 | HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0) |
ocomeni | 79:a2187bbfa407 | 369 | post_req->set_header("Content-Type", "application/json"); |
ocomeni | 79:a2187bbfa407 | 370 | HttpResponse* get_res = get_req->send(); |
ocomeni | 79:a2187bbfa407 | 371 | |
ocomeni | 79:a2187bbfa407 | 372 | const char body[] = "{\"hello\":\"world\"}"; |
ocomeni | 79:a2187bbfa407 | 373 | |
ocomeni | 79:a2187bbfa407 | 374 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
ocomeni | 79:a2187bbfa407 | 375 | |
ocomeni | 79:a2187bbfa407 | 376 | |
ocomeni | 79:a2187bbfa407 | 377 | */ |
ocomeni | 84:7c7add00f4bf | 378 | void WiFiManager::return_response(HttpResponse* res) { |
ocomeni | 84:7c7add00f4bf | 379 | |
ocomeni | 84:7c7add00f4bf | 380 | //queueWiFiDataResponse(at_data_msg_t at_data_resp); |
ocomeni | 84:7c7add00f4bf | 381 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
ocomeni | 84:7c7add00f4bf | 382 | printf("Headers:\n"); |
ocomeni | 84:7c7add00f4bf | 383 | at_data_msg_t at_data_resp; |
ocomeni | 84:7c7add00f4bf | 384 | //at_data_resp = &at_data; |
ocomeni | 84:7c7add00f4bf | 385 | at_data_resp.dataLen = sizeof(res); // start with minimum size of response |
ocomeni | 84:7c7add00f4bf | 386 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
ocomeni | 84:7c7add00f4bf | 387 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
ocomeni | 84:7c7add00f4bf | 388 | at_data_resp.dataLen+= res->get_headers_fields()[ix]->size(); |
ocomeni | 84:7c7add00f4bf | 389 | at_data_resp.dataLen+= res->get_headers_values()[ix]->size(); |
ocomeni | 84:7c7add00f4bf | 390 | } |
ocomeni | 84:7c7add00f4bf | 391 | at_data_resp.dataLen+= res->get_body_length(); |
ocomeni | 84:7c7add00f4bf | 392 | printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
ocomeni | 84:7c7add00f4bf | 393 | //printf("WIFI MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security)); |
ocomeni | 84:7c7add00f4bf | 394 | // package and send on wifi data queue |
ocomeni | 84:7c7add00f4bf | 395 | |
ocomeni | 84:7c7add00f4bf | 396 | at_data_resp.at_resp = AT_HTTPS_RESP; |
ocomeni | 84:7c7add00f4bf | 397 | //at_data_resp.at_data_resp = sizeof(wifi_config_t); |
ocomeni | 84:7c7add00f4bf | 398 | memcpy(at_data_resp.buffer, res, at_data_resp.dataLen); |
ocomeni | 84:7c7add00f4bf | 399 | queueWiFiDataResponse(at_data_resp); |
ocomeni | 79:a2187bbfa407 | 400 | |
ocomeni | 84:7c7add00f4bf | 401 | } |
ocomeni | 84:7c7add00f4bf | 402 | |
ocomeni | 84:7c7add00f4bf | 403 | |
ocomeni | 84:7c7add00f4bf | 404 | void WiFiManager::body_callback(const char *at, uint32_t length) { |
ocomeni | 84:7c7add00f4bf | 405 | |
ocomeni | 84:7c7add00f4bf | 406 | printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); |
ocomeni | 84:7c7add00f4bf | 407 | //device->printf("\n Try Print Header as string:\n\n "); |
ocomeni | 84:7c7add00f4bf | 408 | //device->printf("recv %d [%.*s]\n", length, strstr((char *)at, "\r\n")-(char *)at, (char *)at); |
ocomeni | 84:7c7add00f4bf | 409 | //if(false) |
ocomeni | 84:7c7add00f4bf | 410 | //if(chunkNum < 2) |
ocomeni | 84:7c7add00f4bf | 411 | //for(int i=0; i < length; i++){ |
ocomeni | 84:7c7add00f4bf | 412 | // |
ocomeni | 84:7c7add00f4bf | 413 | //putc((uint8_t)at[i]); |
ocomeni | 84:7c7add00f4bf | 414 | //int resp = write( (const uint8_t *)at, (int) length, &completed, SERIAL_EVENT_TX_COMPLETE); |
ocomeni | 84:7c7add00f4bf | 415 | //} |
ocomeni | 84:7c7add00f4bf | 416 | //if(false) |
ocomeni | 84:7c7add00f4bf | 417 | if(chunkNum < 2) |
ocomeni | 84:7c7add00f4bf | 418 | for (size_t ix = 0; ix < length; ix++) { |
ocomeni | 84:7c7add00f4bf | 419 | printf("%02X: ", (uint8_t)at[ix]); |
ocomeni | 84:7c7add00f4bf | 420 | if((ix % 32) == 0 and ix) |
ocomeni | 84:7c7add00f4bf | 421 | printf("\n"); |
ocomeni | 84:7c7add00f4bf | 422 | } |
ocomeni | 84:7c7add00f4bf | 423 | printf("\n\n"); |
ocomeni | 84:7c7add00f4bf | 424 | chunkNum++; |
ocomeni | 84:7c7add00f4bf | 425 | //device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
ocomeni | 84:7c7add00f4bf | 426 | } |
ocomeni | 84:7c7add00f4bf | 427 | |
ocomeni | 84:7c7add00f4bf | 428 | void WiFiManager::createHttpsRequest() |
ocomeni | 79:a2187bbfa407 | 429 | { |
ocomeni | 84:7c7add00f4bf | 430 | |
ocomeni | 84:7c7add00f4bf | 431 | //http_method method, |
ocomeni | 84:7c7add00f4bf | 432 | //Callback<void(const char *at, uint32_t length)> body_callback = 0 |
ocomeni | 84:7c7add00f4bf | 433 | printf("\n[WIFI MAN] Http Request received:"); |
ocomeni | 84:7c7add00f4bf | 434 | http_req_cfg = (http_request_t *) data_msg->buffer; |
ocomeni | 85:9f896e1e041c | 435 | printf("\n[WIFI MAN] uri = %s", http_req_cfg->request_URI.c_str()); |
ocomeni | 85:9f896e1e041c | 436 | strncat(internet_config.url, http_req_cfg->request_URI.c_str(), http_req_cfg->request_URI.size()); |
ocomeni | 84:7c7add00f4bf | 437 | printf("\n[WIFI MAN] server url = %s\n", internet_config.url); |
ocomeni | 84:7c7add00f4bf | 438 | //printf("\n[WIFI MAN] server url = %s\n", internet_config.url.c_str()); |
ocomeni | 84:7c7add00f4bf | 439 | //const char* url = internet_config.url; |
ocomeni | 84:7c7add00f4bf | 440 | |
ocomeni | 84:7c7add00f4bf | 441 | //https_request = new HttpsRequest(network, |
ocomeni | 84:7c7add00f4bf | 442 | http_request = new HttpRequest(network, |
ocomeni | 84:7c7add00f4bf | 443 | //SSL_CA_PEM, |
ocomeni | 84:7c7add00f4bf | 444 | http_req_cfg->method, |
ocomeni | 84:7c7add00f4bf | 445 | internet_config.url, |
ocomeni | 84:7c7add00f4bf | 446 | callback(this, &WiFiManager::body_callback)); |
ocomeni | 84:7c7add00f4bf | 447 | setHttpHeader("Host", http_req_cfg->hostName); |
ocomeni | 84:7c7add00f4bf | 448 | setHttpHeader("Accept", http_req_cfg->AcceptVal); |
ocomeni | 84:7c7add00f4bf | 449 | setHttpHeader("Content-Type", http_req_cfg->contentType); |
ocomeni | 84:7c7add00f4bf | 450 | setHttpHeader("Content-Length", http_req_cfg->contentLen); |
ocomeni | 84:7c7add00f4bf | 451 | int bodyLen; |
ocomeni | 84:7c7add00f4bf | 452 | sscanf(http_req_cfg->contentLen.c_str(), "%d", &bodyLen); |
ocomeni | 84:7c7add00f4bf | 453 | http_response = https_request->send(http_req_cfg->body, bodyLen); |
ocomeni | 84:7c7add00f4bf | 454 | free_DataMsg(); |
ocomeni | 84:7c7add00f4bf | 455 | if (!http_response) { |
ocomeni | 84:7c7add00f4bf | 456 | printf("HttpRequest failed (error code %d)\n", https_request->get_error()); |
ocomeni | 84:7c7add00f4bf | 457 | return; |
ocomeni | 84:7c7add00f4bf | 458 | } |
ocomeni | 84:7c7add00f4bf | 459 | |
ocomeni | 84:7c7add00f4bf | 460 | printf("\n----- HTTPS POST response -----\n"); |
ocomeni | 84:7c7add00f4bf | 461 | return_response(http_response); |
ocomeni | 79:a2187bbfa407 | 462 | } |
ocomeni | 79:a2187bbfa407 | 463 | |
ocomeni | 79:a2187bbfa407 | 464 | void WiFiManager::createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 465 | const char* url, |
ocomeni | 79:a2187bbfa407 | 466 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 467 | { |
ocomeni | 84:7c7add00f4bf | 468 | http_request = new HttpRequest(network, |
ocomeni | 84:7c7add00f4bf | 469 | method, url, body_callback);; |
ocomeni | 79:a2187bbfa407 | 470 | } |
ocomeni | 79:a2187bbfa407 | 471 | |
ocomeni | 79:a2187bbfa407 | 472 | void WiFiManager::setHttpHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 473 | { |
ocomeni | 79:a2187bbfa407 | 474 | http_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 475 | } |
ocomeni | 79:a2187bbfa407 | 476 | |
ocomeni | 79:a2187bbfa407 | 477 | void WiFiManager::setHttpsHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 478 | { |
ocomeni | 79:a2187bbfa407 | 479 | https_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 480 | } |
ocomeni | 79:a2187bbfa407 | 481 | |
ocomeni | 79:a2187bbfa407 | 482 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 483 | { |
ocomeni | 78:07bb86e3ce14 | 484 | } |
ocomeni | 79:a2187bbfa407 | 485 | |
ocomeni | 79:a2187bbfa407 | 486 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 487 | { |
ocomeni | 78:07bb86e3ce14 | 488 | } |
ocomeni | 79:a2187bbfa407 | 489 |