Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.cpp@81:637a87eb8170, 2019-03-23 (annotated)
- Committer:
- ocomeni
- Date:
- Sat Mar 23 16:28:34 2019 +0000
- Revision:
- 81:637a87eb8170
- Parent:
- 80:e8f0e92e3ac9
- Child:
- 82:10072c1794d3
first data queue/dequeue implemented for internet configuration.
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 | 78:07bb86e3ce14 | 34 | } |
ocomeni | 78:07bb86e3ce14 | 35 | |
ocomeni | 78:07bb86e3ce14 | 36 | WiFiManager::~WiFiManager() |
ocomeni | 78:07bb86e3ce14 | 37 | { |
ocomeni | 78:07bb86e3ce14 | 38 | } |
ocomeni | 79:a2187bbfa407 | 39 | |
ocomeni | 81:637a87eb8170 | 40 | bool WiFiManager::queueATresponse(at_cmd_resp_t resp){ |
ocomeni | 81:637a87eb8170 | 41 | at_resp_message_t *atResp = _wiFi2ATmPool->alloc(); |
ocomeni | 81:637a87eb8170 | 42 | atResp->at_resp = resp; |
ocomeni | 81:637a87eb8170 | 43 | _wiFi2ATCmdQueue->put(atResp); |
ocomeni | 81:637a87eb8170 | 44 | return true; |
ocomeni | 81:637a87eb8170 | 45 | } |
ocomeni | 81:637a87eb8170 | 46 | |
ocomeni | 81:637a87eb8170 | 47 | |
ocomeni | 81:637a87eb8170 | 48 | bool WiFiManager::queueWiFiDataResponse(at_data_msg_t at_resp){ |
ocomeni | 81:637a87eb8170 | 49 | at_data_msg_t *atData = _wiFi2ATDatamPool->alloc(); |
ocomeni | 81:637a87eb8170 | 50 | atData->at_resp = at_resp.at_resp; |
ocomeni | 81:637a87eb8170 | 51 | atData->dataLen = at_resp.dataLen; |
ocomeni | 81:637a87eb8170 | 52 | memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen); |
ocomeni | 81:637a87eb8170 | 53 | _wiFi2ATDataQueue->put(atData); |
ocomeni | 81:637a87eb8170 | 54 | return true; |
ocomeni | 81:637a87eb8170 | 55 | } |
ocomeni | 81:637a87eb8170 | 56 | |
ocomeni | 79:a2187bbfa407 | 57 | |
ocomeni | 79:a2187bbfa407 | 58 | |
ocomeni | 79:a2187bbfa407 | 59 | void WiFiManager::runMain(){ |
ocomeni | 81:637a87eb8170 | 60 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 61 | while(true){ |
ocomeni | 79:a2187bbfa407 | 62 | dequeueWiFiCommands(); |
ocomeni | 81:637a87eb8170 | 63 | dequeueATdataResponse(); |
ocomeni | 79:a2187bbfa407 | 64 | switch(wifiCmd){ |
ocomeni | 79:a2187bbfa407 | 65 | case WIFI_CMD_NONE: |
ocomeni | 79:a2187bbfa407 | 66 | // IDLE STATE |
ocomeni | 79:a2187bbfa407 | 67 | break; |
ocomeni | 79:a2187bbfa407 | 68 | case WIFI_CMD_SCAN: |
ocomeni | 79:a2187bbfa407 | 69 | error = scanNetworks(); |
ocomeni | 79:a2187bbfa407 | 70 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 71 | queueATresponse(AT_SCAN_RESP); |
ocomeni | 81:637a87eb8170 | 72 | break; |
ocomeni | 81:637a87eb8170 | 73 | case WIFI_CMD_DETAILED_SCAN: |
ocomeni | 81:637a87eb8170 | 74 | nsapi_size_or_error_t cnt_err; |
ocomeni | 81:637a87eb8170 | 75 | cnt_err = getAvailableAPs(lastScanCount); |
ocomeni | 81:637a87eb8170 | 76 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 77 | queueATresponse(AT_DETAILED_SCAN_RESP); |
ocomeni | 79:a2187bbfa407 | 78 | break; |
ocomeni | 79:a2187bbfa407 | 79 | case WIFI_CMD_CONNECT: |
ocomeni | 81:637a87eb8170 | 80 | error = connect(); |
ocomeni | 81:637a87eb8170 | 81 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 82 | queueATresponse(AT_CONNECT_RESP); |
ocomeni | 79:a2187bbfa407 | 83 | break; |
ocomeni | 79:a2187bbfa407 | 84 | case WIFI_CMD_DISCONNECT: |
ocomeni | 81:637a87eb8170 | 85 | error = disconnect(); |
ocomeni | 81:637a87eb8170 | 86 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 87 | queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 88 | break; |
ocomeni | 81:637a87eb8170 | 89 | case WIFI_CMD_CONFIG: |
ocomeni | 81:637a87eb8170 | 90 | set_WIFI_CONFIG(); |
ocomeni | 81:637a87eb8170 | 91 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 92 | queueATresponse(AT_CONFIG_RESP); |
ocomeni | 81:637a87eb8170 | 93 | case WIFI_CMD_INTERNET_CONFIG: |
ocomeni | 81:637a87eb8170 | 94 | set_internet_config(); |
ocomeni | 81:637a87eb8170 | 95 | wifiCmd = WIFI_CMD_NONE; |
ocomeni | 81:637a87eb8170 | 96 | queueATresponse(AT_INTERNET_CONFIG_RESP); |
ocomeni | 79:a2187bbfa407 | 97 | break; |
ocomeni | 79:a2187bbfa407 | 98 | case WIFI_CMD_SEND_HTTPS_REQ: |
ocomeni | 79:a2187bbfa407 | 99 | break; |
ocomeni | 79:a2187bbfa407 | 100 | case WIFI_CMD_SEND_HTTP_REQ: |
ocomeni | 79:a2187bbfa407 | 101 | break; |
ocomeni | 79:a2187bbfa407 | 102 | default: |
ocomeni | 79:a2187bbfa407 | 103 | break; |
ocomeni | 79:a2187bbfa407 | 104 | } |
ocomeni | 79:a2187bbfa407 | 105 | wait_ms(100); // |
ocomeni | 79:a2187bbfa407 | 106 | } |
ocomeni | 79:a2187bbfa407 | 107 | |
ocomeni | 78:07bb86e3ce14 | 108 | } |
ocomeni | 79:a2187bbfa407 | 109 | |
ocomeni | 79:a2187bbfa407 | 110 | bool WiFiManager::dequeueWiFiCommands(){ |
ocomeni | 81:637a87eb8170 | 111 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 112 | osEvent evt = _aT2WiFiCmdQueue->get(0); |
ocomeni | 79:a2187bbfa407 | 113 | if(evt.status == osEventMessage){ |
ocomeni | 79:a2187bbfa407 | 114 | wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p; |
ocomeni | 79:a2187bbfa407 | 115 | setNextCommand(cmd->wifi_cmd); |
ocomeni | 79:a2187bbfa407 | 116 | _aT2WiFimPool->free(cmd); |
ocomeni | 79:a2187bbfa407 | 117 | } |
ocomeni | 79:a2187bbfa407 | 118 | return true; |
ocomeni | 79:a2187bbfa407 | 119 | } |
ocomeni | 79:a2187bbfa407 | 120 | |
ocomeni | 79:a2187bbfa407 | 121 | |
ocomeni | 81:637a87eb8170 | 122 | bool WiFiManager::dequeueATdataResponse(){ |
ocomeni | 81:637a87eb8170 | 123 | if(wifiCmd != WIFI_CMD_NONE) return false; // busy |
ocomeni | 81:637a87eb8170 | 124 | osEvent evt = _aT2WiFiDataQueue->get(0); |
ocomeni | 81:637a87eb8170 | 125 | if(evt.status == osEventMessage){ |
ocomeni | 81:637a87eb8170 | 126 | data_msg = (wifi_data_msg_t*)evt.value.p; |
ocomeni | 81:637a87eb8170 | 127 | setNextCommand(data_msg->wifi_cmd); |
ocomeni | 81:637a87eb8170 | 128 | //_wiFi2ATDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 129 | } |
ocomeni | 81:637a87eb8170 | 130 | return true; |
ocomeni | 81:637a87eb8170 | 131 | } |
ocomeni | 81:637a87eb8170 | 132 | |
ocomeni | 81:637a87eb8170 | 133 | |
ocomeni | 79:a2187bbfa407 | 134 | bool WiFiManager::setNextCommand(wifi_cmd_t cmd) |
ocomeni | 78:07bb86e3ce14 | 135 | { |
ocomeni | 79:a2187bbfa407 | 136 | printf("\n [WIFI-MAN] About to set next WiFi manager command \n"); |
ocomeni | 79:a2187bbfa407 | 137 | if(wifiCmd == WIFI_CMD_NONE){ |
ocomeni | 79:a2187bbfa407 | 138 | wifiCmd = cmd; |
ocomeni | 79:a2187bbfa407 | 139 | return true; // success |
ocomeni | 79:a2187bbfa407 | 140 | } |
ocomeni | 79:a2187bbfa407 | 141 | return false; // wiFiManager busy |
ocomeni | 78:07bb86e3ce14 | 142 | } |
ocomeni | 79:a2187bbfa407 | 143 | |
ocomeni | 81:637a87eb8170 | 144 | const char * WiFiManager::sec2str(nsapi_security_t sec) |
ocomeni | 81:637a87eb8170 | 145 | { |
ocomeni | 81:637a87eb8170 | 146 | switch (sec) { |
ocomeni | 81:637a87eb8170 | 147 | case NSAPI_SECURITY_NONE: |
ocomeni | 81:637a87eb8170 | 148 | return "None"; |
ocomeni | 81:637a87eb8170 | 149 | case NSAPI_SECURITY_WEP: |
ocomeni | 81:637a87eb8170 | 150 | return "WEP"; |
ocomeni | 81:637a87eb8170 | 151 | case NSAPI_SECURITY_WPA: |
ocomeni | 81:637a87eb8170 | 152 | return "WPA"; |
ocomeni | 81:637a87eb8170 | 153 | case NSAPI_SECURITY_WPA2: |
ocomeni | 81:637a87eb8170 | 154 | return "WPA2"; |
ocomeni | 81:637a87eb8170 | 155 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 81:637a87eb8170 | 156 | return "WPA/WPA2"; |
ocomeni | 81:637a87eb8170 | 157 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 81:637a87eb8170 | 158 | default: |
ocomeni | 81:637a87eb8170 | 159 | return "Unknown"; |
ocomeni | 81:637a87eb8170 | 160 | } |
ocomeni | 81:637a87eb8170 | 161 | } |
ocomeni | 81:637a87eb8170 | 162 | |
ocomeni | 79:a2187bbfa407 | 163 | |
ocomeni | 79:a2187bbfa407 | 164 | nsapi_size_or_error_t WiFiManager::scanNetworks() |
ocomeni | 79:a2187bbfa407 | 165 | { |
ocomeni | 79:a2187bbfa407 | 166 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 167 | printf("\n [WIFI-MAN] About to start scan for WiFi networks\n"); |
ocomeni | 79:a2187bbfa407 | 168 | lastScanCount = network->scan(NULL, 0); |
ocomeni | 79:a2187bbfa407 | 169 | printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n"); |
ocomeni | 79:a2187bbfa407 | 170 | return lastScanCount; |
ocomeni | 79:a2187bbfa407 | 171 | } |
ocomeni | 79:a2187bbfa407 | 172 | |
ocomeni | 79:a2187bbfa407 | 173 | |
ocomeni | 81:637a87eb8170 | 174 | //nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, |
ocomeni | 81:637a87eb8170 | 175 | // nsapi_size_t ncount) |
ocomeni | 81:637a87eb8170 | 176 | nsapi_size_or_error_t WiFiManager::getAvailableAPs(nsapi_size_t ncount) |
ocomeni | 79:a2187bbfa407 | 177 | { |
ocomeni | 81:637a87eb8170 | 178 | WiFiAccessPoint *ap; |
ocomeni | 81:637a87eb8170 | 179 | nsapi_size_or_error_t count; |
ocomeni | 81:637a87eb8170 | 180 | count = ncount; |
ocomeni | 81:637a87eb8170 | 181 | //count = wiFiManager->scanNetworks(); |
ocomeni | 81:637a87eb8170 | 182 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 183 | //_smutex.lock(); |
ocomeni | 81:637a87eb8170 | 184 | printf("[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 185 | //_smutex.unlock(); |
ocomeni | 81:637a87eb8170 | 186 | return; |
ocomeni | 81:637a87eb8170 | 187 | } |
ocomeni | 81:637a87eb8170 | 188 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 81:637a87eb8170 | 189 | count = count < 15 ? count : 15; |
ocomeni | 81:637a87eb8170 | 190 | ap = new WiFiAccessPoint[count]; |
ocomeni | 81:637a87eb8170 | 191 | count = network->scan(ap, count); |
ocomeni | 81:637a87eb8170 | 192 | if (count <= 0) { |
ocomeni | 81:637a87eb8170 | 193 | printf("[WIFI-MAN] scan() failed with return value: %d\n", count); |
ocomeni | 81:637a87eb8170 | 194 | return; |
ocomeni | 81:637a87eb8170 | 195 | } |
ocomeni | 81:637a87eb8170 | 196 | |
ocomeni | 81:637a87eb8170 | 197 | for (int i = 0; i < count; i++) { |
ocomeni | 81:637a87eb8170 | 198 | 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 | 199 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 81:637a87eb8170 | 200 | 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 | 201 | } |
ocomeni | 81:637a87eb8170 | 202 | printf("[WIFI-MAN] %d networks available.\n", count); |
ocomeni | 81:637a87eb8170 | 203 | |
ocomeni | 81:637a87eb8170 | 204 | delete[] ap; |
ocomeni | 79:a2187bbfa407 | 205 | return count; |
ocomeni | 79:a2187bbfa407 | 206 | } |
ocomeni | 79:a2187bbfa407 | 207 | |
ocomeni | 79:a2187bbfa407 | 208 | |
ocomeni | 81:637a87eb8170 | 209 | void WiFiManager::set_WIFI_CONFIG() |
ocomeni | 81:637a87eb8170 | 210 | { |
ocomeni | 81:637a87eb8170 | 211 | wifi_config_t *wifi_cfg= (wifi_config_t *) data_msg->buffer; |
ocomeni | 81:637a87eb8170 | 212 | set_WIFI_SSID(wifi_cfg->ssid); |
ocomeni | 81:637a87eb8170 | 213 | set_WIFI_PASSWORD(wifi_cfg->pass); |
ocomeni | 81:637a87eb8170 | 214 | set_WIFI_SECURITY(wifi_cfg->security); |
ocomeni | 81:637a87eb8170 | 215 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 216 | } |
ocomeni | 81:637a87eb8170 | 217 | |
ocomeni | 78:07bb86e3ce14 | 218 | void WiFiManager::set_WIFI_SSID(char * wifi_ssid) |
ocomeni | 78:07bb86e3ce14 | 219 | { |
ocomeni | 78:07bb86e3ce14 | 220 | strcpy(wifi_config.ssid, wifi_ssid); |
ocomeni | 78:07bb86e3ce14 | 221 | } |
ocomeni | 79:a2187bbfa407 | 222 | |
ocomeni | 79:a2187bbfa407 | 223 | |
ocomeni | 78:07bb86e3ce14 | 224 | void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass) |
ocomeni | 78:07bb86e3ce14 | 225 | { |
ocomeni | 78:07bb86e3ce14 | 226 | strcpy(wifi_config.pass, wifi_pass); |
ocomeni | 78:07bb86e3ce14 | 227 | } |
ocomeni | 79:a2187bbfa407 | 228 | |
ocomeni | 79:a2187bbfa407 | 229 | |
ocomeni | 78:07bb86e3ce14 | 230 | void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security) |
ocomeni | 78:07bb86e3ce14 | 231 | { |
ocomeni | 78:07bb86e3ce14 | 232 | wifi_config.security = wifi_security; |
ocomeni | 78:07bb86e3ce14 | 233 | } |
ocomeni | 79:a2187bbfa407 | 234 | |
ocomeni | 79:a2187bbfa407 | 235 | |
ocomeni | 81:637a87eb8170 | 236 | |
ocomeni | 81:637a87eb8170 | 237 | void WiFiManager::set_internet_config() |
ocomeni | 81:637a87eb8170 | 238 | { |
ocomeni | 81:637a87eb8170 | 239 | internet_config_t *internet_cfg = (internet_config_t *) data_msg->buffer; |
ocomeni | 81:637a87eb8170 | 240 | internet_config.peer_id = internet_cfg->peer_id; |
ocomeni | 81:637a87eb8170 | 241 | internet_config.url = internet_cfg->url; |
ocomeni | 81:637a87eb8170 | 242 | internet_config.connectionScheme = internet_cfg->connectionScheme; |
ocomeni | 81:637a87eb8170 | 243 | free_DataMsg(); |
ocomeni | 81:637a87eb8170 | 244 | printf("[WIFI MAN] Internet configuration setup completed\n"); |
ocomeni | 81:637a87eb8170 | 245 | printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, |
ocomeni | 81:637a87eb8170 | 246 | internet_config.url.c_str(), |
ocomeni | 81:637a87eb8170 | 247 | internet_config.connectionScheme); |
ocomeni | 81:637a87eb8170 | 248 | } |
ocomeni | 81:637a87eb8170 | 249 | |
ocomeni | 81:637a87eb8170 | 250 | void WiFiManager::free_DataMsg() |
ocomeni | 81:637a87eb8170 | 251 | { |
ocomeni | 81:637a87eb8170 | 252 | // free memory after processing |
ocomeni | 81:637a87eb8170 | 253 | _aT2WiFiDatamPool->free(data_msg); |
ocomeni | 81:637a87eb8170 | 254 | } |
ocomeni | 81:637a87eb8170 | 255 | |
ocomeni | 81:637a87eb8170 | 256 | |
ocomeni | 81:637a87eb8170 | 257 | void WiFiManager::status_callback(nsapi_event_t status, intptr_t param) |
ocomeni | 81:637a87eb8170 | 258 | { |
ocomeni | 81:637a87eb8170 | 259 | //if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) { |
ocomeni | 81:637a87eb8170 | 260 | //} |
ocomeni | 81:637a87eb8170 | 261 | switch(param) { |
ocomeni | 81:637a87eb8170 | 262 | case NSAPI_STATUS_LOCAL_UP: |
ocomeni | 81:637a87eb8170 | 263 | printf("[WIFI-MAN] Local IP address set!\r\n"); |
ocomeni | 81:637a87eb8170 | 264 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 265 | break; |
ocomeni | 81:637a87eb8170 | 266 | case NSAPI_STATUS_GLOBAL_UP: |
ocomeni | 81:637a87eb8170 | 267 | printf("Global IP address set!\r\n"); |
ocomeni | 81:637a87eb8170 | 268 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 81:637a87eb8170 | 269 | printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid); |
ocomeni | 81:637a87eb8170 | 270 | is_connected = true; |
ocomeni | 81:637a87eb8170 | 271 | break; |
ocomeni | 81:637a87eb8170 | 272 | case NSAPI_STATUS_DISCONNECTED: |
ocomeni | 81:637a87eb8170 | 273 | printf("No connection to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 274 | printf("\n [WIFI-MAN] No connection to network!\n"); |
ocomeni | 81:637a87eb8170 | 275 | is_connected = false; |
ocomeni | 81:637a87eb8170 | 276 | //queueATresponse(AT_DISCONNECT_RESP); |
ocomeni | 81:637a87eb8170 | 277 | // attempt reconnection if always connected scheme is set |
ocomeni | 81:637a87eb8170 | 278 | if(internet_config.connectionScheme == ALWAYS_CONNECTED) |
ocomeni | 81:637a87eb8170 | 279 | { |
ocomeni | 81:637a87eb8170 | 280 | nsapi_error_t error; |
ocomeni | 81:637a87eb8170 | 281 | error = scanNetworks(); |
ocomeni | 81:637a87eb8170 | 282 | queueATresponse(WIFI_RECONNECT_INFO); |
ocomeni | 81:637a87eb8170 | 283 | } |
ocomeni | 81:637a87eb8170 | 284 | break; |
ocomeni | 81:637a87eb8170 | 285 | case NSAPI_STATUS_CONNECTING: |
ocomeni | 81:637a87eb8170 | 286 | printf("Connecting to network!\r\n"); |
ocomeni | 81:637a87eb8170 | 287 | break; |
ocomeni | 81:637a87eb8170 | 288 | default: |
ocomeni | 81:637a87eb8170 | 289 | printf("Not supported"); |
ocomeni | 81:637a87eb8170 | 290 | break; |
ocomeni | 81:637a87eb8170 | 291 | } |
ocomeni | 81:637a87eb8170 | 292 | } |
ocomeni | 81:637a87eb8170 | 293 | |
ocomeni | 81:637a87eb8170 | 294 | |
ocomeni | 81:637a87eb8170 | 295 | |
ocomeni | 79:a2187bbfa407 | 296 | // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */ |
ocomeni | 79:a2187bbfa407 | 297 | // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */ |
ocomeni | 79:a2187bbfa407 | 298 | // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */ |
ocomeni | 79:a2187bbfa407 | 299 | // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */ |
ocomeni | 79:a2187bbfa407 | 300 | // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED |
ocomeni | 79:a2187bbfa407 | 301 | |
ocomeni | 79:a2187bbfa407 | 302 | nsapi_error_t WiFiManager::connect() |
ocomeni | 79:a2187bbfa407 | 303 | { |
ocomeni | 79:a2187bbfa407 | 304 | nsapi_error_t error; |
ocomeni | 81:637a87eb8170 | 305 | printf("\n [WIFI-MAN] About to connect to WiFi network\n"); |
ocomeni | 81:637a87eb8170 | 306 | network->attach(callback(this, &WiFiManager::status_callback)); |
ocomeni | 79:a2187bbfa407 | 307 | error = network->set_blocking(false); |
ocomeni | 79:a2187bbfa407 | 308 | if(error) |
ocomeni | 79:a2187bbfa407 | 309 | { |
ocomeni | 79:a2187bbfa407 | 310 | printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n"); |
ocomeni | 79:a2187bbfa407 | 311 | return error; |
ocomeni | 79:a2187bbfa407 | 312 | } |
ocomeni | 81:637a87eb8170 | 313 | printf("[WIFI-MAN] Connecting to network %s\n", wifi_config.ssid); |
ocomeni | 79:a2187bbfa407 | 314 | error = network->connect(wifi_config.ssid, |
ocomeni | 79:a2187bbfa407 | 315 | wifi_config.pass, |
ocomeni | 79:a2187bbfa407 | 316 | wifi_config.security); |
ocomeni | 81:637a87eb8170 | 317 | return error; |
ocomeni | 81:637a87eb8170 | 318 | /* |
ocomeni | 79:a2187bbfa407 | 319 | if(error) |
ocomeni | 79:a2187bbfa407 | 320 | { |
ocomeni | 79:a2187bbfa407 | 321 | printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n"); |
ocomeni | 79:a2187bbfa407 | 322 | return error; |
ocomeni | 79:a2187bbfa407 | 323 | } |
ocomeni | 79:a2187bbfa407 | 324 | nsapi_connection_status_t conn_status = NSAPI_STATUS_CONNECTING; |
ocomeni | 79:a2187bbfa407 | 325 | int loopCount = 0; |
ocomeni | 79:a2187bbfa407 | 326 | while(conn_status == NSAPI_STATUS_CONNECTING){ |
ocomeni | 79:a2187bbfa407 | 327 | loopCount++; |
ocomeni | 79:a2187bbfa407 | 328 | conn_status = network->get_connection_status(); |
ocomeni | 79:a2187bbfa407 | 329 | printf("\n [WIFI-MAN] Waiting for WiFi network connect to complete asynchronously [status = %d] - %d\n", conn_status, loopCount); |
ocomeni | 79:a2187bbfa407 | 330 | wait(0.1); |
ocomeni | 79:a2187bbfa407 | 331 | } |
ocomeni | 79:a2187bbfa407 | 332 | if(conn_status < 0) |
ocomeni | 79:a2187bbfa407 | 333 | { |
ocomeni | 79:a2187bbfa407 | 334 | printf("\n [WIFI-MAN] Error connecting to Wifi -- %d!! - \n", conn_status); |
ocomeni | 79:a2187bbfa407 | 335 | return conn_status; |
ocomeni | 79:a2187bbfa407 | 336 | } |
ocomeni | 81:637a87eb8170 | 337 | printf("[WIFI-MAN] Connected to the network %s\n", wifi_config.ssid); |
ocomeni | 81:637a87eb8170 | 338 | printf("[WIFI-MAN] IP address: %s\n", network->get_ip_address()); |
ocomeni | 79:a2187bbfa407 | 339 | return conn_status; |
ocomeni | 81:637a87eb8170 | 340 | */ |
ocomeni | 79:a2187bbfa407 | 341 | } |
ocomeni | 79:a2187bbfa407 | 342 | |
ocomeni | 79:a2187bbfa407 | 343 | |
ocomeni | 79:a2187bbfa407 | 344 | nsapi_error_t WiFiManager::disconnect() |
ocomeni | 78:07bb86e3ce14 | 345 | { |
ocomeni | 79:a2187bbfa407 | 346 | nsapi_error_t error; |
ocomeni | 79:a2187bbfa407 | 347 | error = network->disconnect(); |
ocomeni | 79:a2187bbfa407 | 348 | return error; |
ocomeni | 78:07bb86e3ce14 | 349 | } |
ocomeni | 79:a2187bbfa407 | 350 | |
ocomeni | 79:a2187bbfa407 | 351 | |
ocomeni | 79:a2187bbfa407 | 352 | /* |
ocomeni | 79:a2187bbfa407 | 353 | |
ocomeni | 79:a2187bbfa407 | 354 | HttpsRequest(NetworkInterface* network, |
ocomeni | 79:a2187bbfa407 | 355 | const char* ssl_ca_pem, |
ocomeni | 79:a2187bbfa407 | 356 | http_method method, |
ocomeni | 79:a2187bbfa407 | 357 | const char* url, |
ocomeni | 79:a2187bbfa407 | 358 | Callback<void(const char *at, uint32_t length)> body_callback = 0) |
ocomeni | 79:a2187bbfa407 | 359 | 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 | 360 | HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0) |
ocomeni | 79:a2187bbfa407 | 361 | post_req->set_header("Content-Type", "application/json"); |
ocomeni | 79:a2187bbfa407 | 362 | HttpResponse* get_res = get_req->send(); |
ocomeni | 79:a2187bbfa407 | 363 | |
ocomeni | 79:a2187bbfa407 | 364 | const char body[] = "{\"hello\":\"world\"}"; |
ocomeni | 79:a2187bbfa407 | 365 | |
ocomeni | 79:a2187bbfa407 | 366 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
ocomeni | 79:a2187bbfa407 | 367 | |
ocomeni | 79:a2187bbfa407 | 368 | |
ocomeni | 79:a2187bbfa407 | 369 | */ |
ocomeni | 79:a2187bbfa407 | 370 | |
ocomeni | 79:a2187bbfa407 | 371 | void WiFiManager::createHttpsRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 372 | const char* url, |
ocomeni | 79:a2187bbfa407 | 373 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 374 | { |
ocomeni | 79:a2187bbfa407 | 375 | https_request = new HttpsRequest(network, SSL_CA_PEM, method, url, body_callback); |
ocomeni | 79:a2187bbfa407 | 376 | } |
ocomeni | 79:a2187bbfa407 | 377 | |
ocomeni | 79:a2187bbfa407 | 378 | void WiFiManager::createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 379 | const char* url, |
ocomeni | 79:a2187bbfa407 | 380 | Callback<void(const char *at, uint32_t length)> body_callback) |
ocomeni | 79:a2187bbfa407 | 381 | { |
ocomeni | 79:a2187bbfa407 | 382 | http_request = new HttpRequest(network, method, url, body_callback);; |
ocomeni | 79:a2187bbfa407 | 383 | } |
ocomeni | 79:a2187bbfa407 | 384 | |
ocomeni | 79:a2187bbfa407 | 385 | void WiFiManager::setHttpHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 386 | { |
ocomeni | 79:a2187bbfa407 | 387 | http_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 388 | } |
ocomeni | 79:a2187bbfa407 | 389 | |
ocomeni | 79:a2187bbfa407 | 390 | void WiFiManager::setHttpsHeader(string key, string value) |
ocomeni | 79:a2187bbfa407 | 391 | { |
ocomeni | 79:a2187bbfa407 | 392 | https_request->set_header(key, value); |
ocomeni | 79:a2187bbfa407 | 393 | } |
ocomeni | 79:a2187bbfa407 | 394 | |
ocomeni | 79:a2187bbfa407 | 395 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 396 | { |
ocomeni | 78:07bb86e3ce14 | 397 | } |
ocomeni | 79:a2187bbfa407 | 398 | |
ocomeni | 79:a2187bbfa407 | 399 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
ocomeni | 78:07bb86e3ce14 | 400 | { |
ocomeni | 78:07bb86e3ce14 | 401 | } |
ocomeni | 79:a2187bbfa407 | 402 |