3#include "WiFiGeneric.h"
10static AuthMode authMode(wifi_auth_mode_t mode) {
12 case ::WIFI_AUTH_OPEN:
16 case ::WIFI_AUTH_WPA_PSK:
18 case ::WIFI_AUTH_WPA2_PSK:
20 case ::WIFI_AUTH_WPA_WPA2_PSK:
22 case ::WIFI_AUTH_WPA2_ENTERPRISE:
29internal::Esp32ListenerListNode* head =
nullptr;
31void attach(internal::Esp32ListenerListNode* n) {
32 if (head ==
nullptr) {
33 n->next = n->prev = n;
43void detach(internal::Esp32ListenerListNode* n) {
44 internal::Esp32ListenerListNode* new_head =
nullptr;
45 if (n->next != n->prev) {
46 n->next->prev = n->prev;
47 n->prev->next = n->next;
50 n->next = n->prev =
nullptr;
54void dispatch(arduino_event_id_t event, arduino_event_info_t info) {
55 if (head ==
nullptr)
return;
58 n->notify_fn(event, info);
65 Init() { WiFi.onEvent(&dispatch); }
72 : event_relay_([&](arduino_event_id_t event, arduino_event_info_t info) {
73 dispatchEvent(event, info);
78 detach(&event_relay_);
83 attach(&event_relay_);
97 const String& ssid = WiFi.SSID();
98 if (ssid.length() == 0)
return false;
99 memcpy(info->
ssid, ssid.c_str(), ssid.length());
100 info->
ssid[ssid.length()] = 0;
102 info->
rssi = WiFi.RSSI();
103 auto mac = WiFi.macAddress();
104 memcpy(info->
bssid, mac.c_str(), 6);
105 info->
primary = WiFi.channel();
118 scanning_ = (WiFi.scanNetworks(
true,
false) == WIFI_SCAN_RUNNING);
123 bool completed = WiFi.scanComplete() >= 0;
128 int max_count)
const {
129 int16_t result = WiFi.scanComplete();
130 if (result < 0)
return false;
131 if (max_count > result) {
135 for (
int i = 0; i < max_count; ++i) {
137 auto ssid = WiFi.SSID(i);
138 memcpy(info.
ssid, ssid.c_str(), ssid.length());
139 info.
ssid[ssid.length()] = 0;
140 info.
authmode = authMode(WiFi.encryptionType(i));
141 info.
rssi = WiFi.RSSI(i);
142 info.
primary = WiFi.channel(i);
149 list->push_back(std::move(info));
157 const std::string& passwd) {
158 WiFi.begin(ssid.c_str(), passwd.c_str());
167 listeners_.insert(listener);
171 listeners_.erase(listener);
178 case ARDUINO_EVENT_WIFI_SCAN_DONE:
180 case ARDUINO_EVENT_WIFI_STA_CONNECTED:
182 case ARDUINO_EVENT_WIFI_STA_GOT_IP:
184 case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: {
185 switch (info.wifi_sta_disconnected.reason) {
186 case WIFI_REASON_AUTH_FAIL:
188 case WIFI_REASON_BEACON_TIMEOUT:
189 case WIFI_REASON_HANDSHAKE_TIMEOUT:
202void Esp32ArduinoInterface::dispatchEvent(arduino_event_id_t event, arduino_event_info_t info) {
203 EventType type = getEventType(event, info);
207 for (
const auto& l : listeners_) {
bool startScan() override
Starts a scan.
void addEventListener(EventListener *listener) override
Registers an interface event listener.
bool getScanResults(std::vector< NetworkDetails > *list, int max_count) const override
Returns scan results, up to max_count entries.
bool connect(const std::string &ssid, const std::string &passwd) override
Connects to the specified SSID/password.
void removeEventListener(EventListener *listener) override
Unregisters an interface event listener.
bool getApInfo(NetworkDetails *info) const override
Returns current AP information; false if not connected.
ConnectionStatus getStatus() override
Returns the current connection status.
void disconnect() override
Disconnects from the current network.
bool scanCompleted() const override
Returns true if the scan has completed.
void begin()
Initializes the underlying Wi-Fi stack and registers callbacks.
Listener for interface events.
EventType
Interface event types.
ConnectionStatus
Wi-Fi connection status.
AuthMode
Wi-Fi authentication modes.
@ WIFI_AUTH_WPA2_PSK
WPA2-PSK.
@ WIFI_AUTH_UNKNOWN
Unknown.
@ WIFI_AUTH_WPA2_ENTERPRISE
WPA2-Enterprise.
@ WIFI_AUTH_WPA_PSK
WPA-PSK.
@ WIFI_AUTH_WPA_WPA2_PSK
WPA/WPA2-PSK.
@ WIFI_CIPHER_TYPE_UNKNOWN
Unknown.
Detailed network information reported by the interface.
uint8_t ssid[33]
SSID of AP.
int8_t rssi
Signal strength of AP.
CipherType group_cipher
Group cipher of AP.
CipherType pairwise_cipher
Pairwise cipher of AP.
uint8_t primary
Channel of AP.
AuthMode authmode
Auth mode of AP.
uint8_t bssid[6]
MAC address of AP.