8000 Thu Mar 23 19:53:28 CDT 2017 · programmer131/esp32-snippets@731a564 · GitHub
[go: up one dir, main page]

Skip to content

Commit 731a564

Browse files
committed
Thu Mar 23 19:53:28 CDT 2017
1 parent bf8649b commit 731a564

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

cpp_utils/WiFi.cpp

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* Created on: Feb 25, 2017
55
* Author: kolban
66
*/
7+
#include "sdkconfig.h"
8+
#if defined(CONFIG_WIFI_ENABLED)
9+
#define _GLIBCXX_USE_C99
710

811
#include "WiFi.h"
912
#include <esp_event.h>
@@ -20,7 +23,7 @@
2023
#include <string.h>
2124
#include <Task.h>
2225

23-
#include "sdkconfig.h"
26+
2427

2528
static char tag[]= "WiFi";
2629

@@ -139,6 +142,51 @@ struct in_addr WiFi::getHostByName(std::string hostName) {
139142
} // getHostByName
140143

141144

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+
142190
/**
143191
* Start being an access point.
144192
* @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) {
185233
this->gw = gw;
186234
this->netmask = netmask;
187235
}
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

cpp_utils/WiFi.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,36 @@
77

88
#ifndef MAIN_WIFI_H_
99
#define MAIN_WIFI_H_
10+
#include "sdkconfig.h"
11+
#if defined(CONFIG_WIFI_ENABLED)
1012
#include <string>
13+
#include <vector>
1114
#include "WiFiEventHandler.h"
15+
16+
17+
18+
class WiFiAPRecord {
19+
public:
20+
friend class WiFi;
21+
wifi_auth_mode_t getAuthMode() {
22+
return authMode;
23+
}
24+
25+
int8_t getRSSI() {
26+
return rssi;
27+
}
28+
29+
std::string getSSID() {
30+
return ssid;
31+
}
32+
33+
std::string toString();
34+
private:
35+
uint8_t bssid[6];
36+
int8_t rssi;
37+
std::string ssid;
38+
wifi_auth_mode_t authMode;
39+
};
1240
/**
1341
* @brief %WiFi driver.
1442
*
@@ -44,6 +72,7 @@ class WiFi {
4472
struct in_addr getHostByName(std::string hostName);
4573
void connectAP(std::string ssid, std::string passwd);
4674
void dump();
75+
std::vector<WiFiAPRecord> scan();
4776
void startAP(std::string ssid, std::string passwd);
4877
void setIPInfo(std::string ip, std::string gw, std::string netmask);
4978

@@ -61,4 +90,5 @@ class WiFi {
6190
char *dnsServer = nullptr;
6291
};
6392

93+
#endif // CONFIG_WIFI_ENABLED
6494
#endif /* MAIN_WIFI_H_ */

0 commit comments

Comments
 (0)
0