|
4 | 4 | * Created on: Feb 25, 2017
|
5 | 5 | * Author: kolban
|
6 | 6 | */
|
| 7 | +#include "sdkconfig.h" |
| 8 | +#if defined(CONFIG_WIFI_ENABLED) |
| 9 | +#define _GLIBCXX_USE_C99 |
7 | 10 |
|
8 | 11 | #include "WiFi.h"
|
9 | 12 | #include <esp_event.h>
|
|
20 | 23 | #include <string.h>
|
21 | 24 | #include <Task.h>
|
22 | 25 |
|
23 |
| -#include "sdkconfig.h" |
| 26 | + |
24 | 27 |
|
25 | 28 | static char tag[]= "WiFi";
|
26 | 29 |
|
@@ -139,6 +142,51 @@ struct in_addr WiFi::getHostByName(std::string hostName) {
|
139 | 142 | } // getHostByName
|
140 | 143 |
|
141 | 144 |
|
| 145 | +/** |
| 146 | + * @brief Perform a WiFi scan looking for access points. |
| 147 | + * |
| 148 | + * An access point scan is performed and a vector of WiFi access point records |
| 149 | + * is built and returned with one record per found scan instance. The scan is |
| 150 | + * performed in a blocking fashion and will not return until the set of scanned |
| 151 | + * access points has been built. |
| 152 | + * |
| 153 | + * @return A vector of WiFiAPRecord instances. |
| 154 | + */ |
| 155 | +std::vector<WiFiAPRecord> WiFi::scan() { |
| 156 | + ::nvs_flash_init(); |
| 157 | + ::tcpip_adapter_init(); |
| 158 | + ESP_ERROR_CHECK(esp_event_loop_init(wifiEventHandler->getEventHandler(), wifiEventHandler)); |
| 159 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 160 | + ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); |
| 161 | + ESP_ERROR_CHECK(::esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
| 162 | + ESP_ERROR_CHECK(::esp_wifi_set_mode(WIFI_MODE_STA)); |
| 163 | + ESP_ERROR_CHECK( esp_wifi_start() ); |
| 164 | + wifi_scan_config_t conf; |
| 165 | + memset(&conf, 0, sizeof(conf)); |
| 166 | + conf.show_hidden = true; |
| 167 | + esp_err_t rc = ::esp_wifi_scan_start(&conf, true); |
| 168 | + if (rc != ESP_OK) { |
| 169 | + ESP_LOGE(tag, "esp_wifi_scan_start: %d", rc); |
| 170 | + } |
| 171 | + uint16_t apCount; |
| 172 | + rc = ::esp_wifi_scan_get_ap_num(&apCount); |
| 173 | + ESP_LOGD(tag, "Count of found access points: %d", apCount); |
| 174 | + wifi_ap_record_t *list = |
| 175 | + (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount); |
| 176 | + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, list)); |
| 177 | + std::vector<WiFiAPRecord> apRecords; |
| 178 | + for (auto i=0; i<apCount; i++) { |
| 179 | + WiFiAPRecord apR; |
| 180 | + memcpy(apR.bssid, list[i].bssid, 6); |
| 181 | + apR.ssid = std::string((char *)list[i].ssid); |
| 182 | + apR.authMode = list[i].authmode; |
| 183 | + apRecords.push_back(apR); |
| 184 | + } |
| 185 | + free(list); |
| 186 | + return apRecords; |
| 187 | +} // scan |
| 188 | + |
| 189 | + |
142 | 190 | /**
|
143 | 191 | * Start being an access point.
|
144 | 192 | * @param[in] ssid The SSID to use to advertize for stations.
|
@@ -185,3 +233,35 @@ void WiFi::setIPInfo(std::string ip, std::string gw, std::string netmask) {
|
185 | 233 | this->gw = gw;
|
186 | 234 | this->netmask = netmask;
|
187 | 235 | }
|
| 236 | + |
| 237 | + |
| 238 | +/** |
| 239 | + * @brief Return a string representation of the WiFi access point record. |
| 240 | + * |
| 241 | + * @return A string representation of the WiFi access point record. |
| 242 | + */ |
| 243 | +std::string WiFiAPRecord::toString() { |
| 244 | + std::string auth; |
| 245 | + switch(getAuthMode()) { |
| 246 | + case WIFI_AUTH_OPEN: |
| 247 | + auth = "WIFI_AUTH_OPEN"; |
| 248 | + break; |
| 249 | + case WIFI_AUTH_WEP: |
| 250 | + auth = "WIFI_AUTH_WEP"; |
| 251 | + break; |
| 252 | + case WIFI_AUTH_WPA_PSK: |
| 253 | + auth = "WIFI_AUTH_WPA_PSK"; |
| 254 | + break; |
| 255 | + case WIFI_AUTH_WPA2_PSK: |
| 256 | + auth = "WIFI_AUTH_WPA2_PSK"; |
| 257 | + break; |
| 258 | + case WIFI_AUTH_WPA_WPA2_PSK: |
| 259 | + auth = "WIFI_AUTH_WPA_WPA2_PSK"; |
| 260 | + break; |
| 261 | + default: |
| 262 | + auth = "<unknown>"; |
| 263 | + break; |
| 264 | + } |
| 265 | + return "ssid: " + ssid + ", auth: " + auth + ", rssi: " + std::to_string(rssi); |
| 266 | +} // toString |
| 267 | +#endif // CONFIG_WIFI_ENABLED |
0 commit comments