roo_wifi
API Documentation for roo_wifi
Loading...
Searching...
No Matches
esp32_arduino_interface.cpp
Go to the documentation of this file.
2
3#include "WiFiGeneric.h"
4#include "WiFi.h"
5
6namespace roo_wifi {
7
8namespace {
9
10static AuthMode authMode(wifi_auth_mode_t mode) {
11 switch (mode) {
12 case ::WIFI_AUTH_OPEN:
13 return WIFI_AUTH_OPEN;
14 case ::WIFI_AUTH_WEP:
15 return WIFI_AUTH_WEP;
16 case ::WIFI_AUTH_WPA_PSK:
17 return WIFI_AUTH_WPA_PSK;
18 case ::WIFI_AUTH_WPA2_PSK:
19 return WIFI_AUTH_WPA2_PSK;
20 case ::WIFI_AUTH_WPA_WPA2_PSK:
22 case ::WIFI_AUTH_WPA2_ENTERPRISE:
24 default:
25 return WIFI_AUTH_UNKNOWN;
26 }
27}
28
29internal::Esp32ListenerListNode* head = nullptr;
30
31void attach(internal::Esp32ListenerListNode* n) {
32 if (head == nullptr) {
33 n->next = n->prev = n;
34 } else {
35 n->next = head->next;
36 n->prev = head;
37 head->next->prev = n;
38 head->next = n;
39 }
40 head = n;
41}
42
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;
48 new_head = n->next;
49 }
50 n->next = n->prev = nullptr;
51 head = new_head;
52}
53
54void dispatch(arduino_event_id_t event, arduino_event_info_t info) {
55 if (head == nullptr) return;
56 auto n = head;
57 do {
58 n->notify_fn(event, info);
59 n = n->next;
60 } while (n != head);
61}
62
63void init() {
64 static struct Init {
65 Init() { WiFi.onEvent(&dispatch); }
66 } init;
67}
68
69} // namespace
70
72 : event_relay_([&](arduino_event_id_t event, arduino_event_info_t info) {
73 dispatchEvent(event, info);
74 }),
75 scanning_(false) {}
76
78 detach(&event_relay_);
79}
80
82 init();
83 attach(&event_relay_);
84 WiFi.mode(WIFI_STA);
85 // // #ifdef ESP32
86 // WiFi.onEvent(
87 // [this](arduino_event_id_t event) {
88 // for (const auto& l : listeners_) {
89 // l->scanCompleted();
90 // }
91 // },
92 // arduino_EVENT_SCAN_DONE);
93 // // #endif
94}
95
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;
101 info->authmode = WIFI_AUTH_UNKNOWN; // authMode(WiFi.encryptionType());
102 info->rssi = WiFi.RSSI();
103 auto mac = WiFi.macAddress();
104 memcpy(info->bssid, mac.c_str(), 6);
105 info->primary = WiFi.channel();
108 info->use_11b = false;
109 info->use_11g = false;
110 info->use_11n = false;
111 info->supports_wps = false;
112
113 info->status = (ConnectionStatus)WiFi.status();
114 return true;
115}
116
118 scanning_ = (WiFi.scanNetworks(true, false) == WIFI_SCAN_RUNNING);
119 return scanning_;
120}
121
123 bool completed = WiFi.scanComplete() >= 0;
124 return completed;
125}
126
127bool Esp32ArduinoInterface::getScanResults(std::vector<NetworkDetails>* list,
128 int max_count) const {
129 int16_t result = WiFi.scanComplete();
130 if (result < 0) return false;
131 if (max_count > result) {
132 max_count = result;
133 }
134 list->clear();
135 for (int i = 0; i < max_count; ++i) {
136 NetworkDetails info;
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);
145 info.use_11b = false;
146 info.use_11g = false;
147 info.use_11n = false;
148 info.supports_wps = false;
149 list->push_back(std::move(info));
150 }
151 return true;
152}
153
154void Esp32ArduinoInterface::disconnect() { WiFi.disconnect(); }
155
156bool Esp32ArduinoInterface::connect(const std::string& ssid,
157 const std::string& passwd) {
158 WiFi.begin(ssid.c_str(), passwd.c_str());
159 return true;
160}
161
165
167 listeners_.insert(listener);
168}
169
171 listeners_.erase(listener);
172}
173
174namespace {
175
176Interface::EventType getEventType(arduino_event_id_t event, arduino_event_info_t info) {
177 switch (event) {
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:
191 default:
193 }
194 }
195 default:
197 }
198}
199
200} // namespace
201
202void Esp32ArduinoInterface::dispatchEvent(arduino_event_id_t event, arduino_event_info_t info) {
203 EventType type = getEventType(event, info);
204 if (type == Interface::EV_SCAN_COMPLETED) {
205 scanning_ = false;
206 }
207 for (const auto& l : listeners_) {
208 l->onEvent(type);
209 }
210}
211
212} // namespace roo_wifi
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.
Definition interface.h:84
EventType
Interface event types.
Definition interface.h:73
ConnectionStatus
Wi-Fi connection status.
Definition interface.h:42
AuthMode
Wi-Fi authentication modes.
Definition interface.h:11
@ WIFI_AUTH_WPA2_PSK
WPA2-PSK.
Definition interface.h:15
@ WIFI_AUTH_UNKNOWN
Unknown.
Definition interface.h:21
@ WIFI_AUTH_WPA2_ENTERPRISE
WPA2-Enterprise.
Definition interface.h:17
@ WIFI_AUTH_WPA_PSK
WPA-PSK.
Definition interface.h:14
@ WIFI_AUTH_OPEN
Open.
Definition interface.h:12
@ WIFI_AUTH_WPA_WPA2_PSK
WPA/WPA2-PSK.
Definition interface.h:16
@ WIFI_AUTH_WEP
WEP.
Definition interface.h:13
@ WIFI_CIPHER_TYPE_UNKNOWN
Unknown.
Definition interface.h:38
Detailed network information reported by the interface.
Definition interface.h:53
uint8_t ssid[33]
SSID of AP.
Definition interface.h:55
int8_t rssi
Signal strength of AP.
Definition interface.h:57
ConnectionStatus status
Definition interface.h:66
CipherType group_cipher
Group cipher of AP.
Definition interface.h:60
CipherType pairwise_cipher
Pairwise cipher of AP.
Definition interface.h:59
uint8_t primary
Channel of AP.
Definition interface.h:56
AuthMode authmode
Auth mode of AP.
Definition interface.h:58
uint8_t bssid[6]
MAC address of AP.
Definition interface.h:54