Skip to content

Commit aafc841

Browse files
committed
lib WiFi AP refactor event handling
- make _onApArduinoEvent event handler as class members, remove static pointer to APClass class instance - replace legacy esp_event_handler_unregister() with instance/hamdler via esp_event_handler_instance_unregister() - replace a bunch of if's with switch/case for better readability - removed mandatory struct arduino_event_t when sending arduino events, use post(event_id,*data)
1 parent f8abfa6 commit aafc841

File tree

2 files changed

+88
-78
lines changed

2 files changed

+88
-78
lines changed

libraries/WiFi/src/AP.cpp

+77-76
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
#include "dhcpserver/dhcpserver_options.h"
2323
#include "esp_netif.h"
2424

25-
// a static handle for event callback
26-
static network_event_handle_t evt_handle{0};
27-
2825
esp_netif_t *get_esp_interface_netif(esp_interface_t interface);
2926

3027
static size_t _wifi_strncpy(char *dst, const char *src, size_t dst_len) {
@@ -75,101 +72,105 @@ static bool softap_config_equal(const wifi_config_t &lhs, const wifi_config_t &r
7572
return true;
7673
}
7774

78-
static APClass *_ap_network_if = NULL;
79-
80-
static esp_event_handler_instance_t _ap_ev_instance = NULL;
81-
static void _ap_event_cb(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
82-
if (event_base == WIFI_EVENT) {
83-
((APClass *)arg)->_onApEvent(event_id, event_data);
84-
}
85-
}
86-
87-
static void _onApArduinoEvent(arduino_event_t *ev) {
88-
if (_ap_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_AP_START || ev->event_id > ARDUINO_EVENT_WIFI_AP_GOT_IP6) {
75+
void APClass::_onApArduinoEvent(arduino_event_id_t event, const arduino_event_info_t *info) {
76+
if (event < ARDUINO_EVENT_WIFI_AP_START || event > ARDUINO_EVENT_WIFI_AP_GOT_IP6) {
8977
return;
9078
}
91-
log_v("Arduino AP Event: %d - %s", ev->event_id, NetworkEvents::eventName(ev->event_id));
92-
if (ev->event_id == ARDUINO_EVENT_WIFI_AP_START) {
79+
log_v("Arduino AP Event: %d - %s", event, NetworkEvents::eventName(event));
80+
if (event == ARDUINO_EVENT_WIFI_AP_START) {
9381
#if CONFIG_LWIP_IPV6
94-
if (_ap_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT) {
95-
esp_err_t err = esp_netif_create_ip6_linklocal(_ap_network_if->netif());
82+
if (getStatusBits() & ESP_NETIF_WANT_IP6_BIT) {
83+
esp_err_t err = esp_netif_create_ip6_linklocal(netif());
9684
if (err != ESP_OK) {
9785
log_e("Failed to enable IPv6 Link Local on AP: 0x%x: %s", err, esp_err_to_name(err));
9886
} else {
99-
log_v("Enabled IPv6 Link Local on %s", _ap_network_if->desc());
87+
log_v("Enabled IPv6 Link Local on %s", desc());
10088
}
10189
}
10290
#endif
10391
}
10492
}
10593

10694
void APClass::_onApEvent(int32_t event_id, void *event_data) {
107-
arduino_event_t arduino_event;
108-
arduino_event.event_id = ARDUINO_EVENT_ANY;
109-
110-
if (event_id == WIFI_EVENT_AP_START) {
111-
log_v("AP Started");
112-
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_START;
113-
setStatusBits(ESP_NETIF_STARTED_BIT);
114-
} else if (event_id == WIFI_EVENT_AP_STOP) {
115-
log_v("AP Stopped");
116-
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STOP;
117-
clearStatusBits(ESP_NETIF_STARTED_BIT | ESP_NETIF_CONNECTED_BIT | ESP_NETIF_HAS_IP_BIT | ESP_NETIF_HAS_LOCAL_IP6_BIT | ESP_NETIF_HAS_GLOBAL_IP6_BIT);
118-
} else if (event_id == WIFI_EVENT_AP_PROBEREQRECVED) {
119-
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
120-
wifi_event_ap_probe_req_rx_t *event = (wifi_event_ap_probe_req_rx_t *)event_data;
121-
log_v("AP Probe Request: RSSI: %d, MAC: " MACSTR, event->rssi, MAC2STR(event->mac));
122-
#endif
123-
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED;
124-
memcpy(&arduino_event.event_info.wifi_ap_probereqrecved, event_data, sizeof(wifi_event_ap_probe_req_rx_t));
125-
} else if (event_id == WIFI_EVENT_AP_STACONNECTED) {
126-
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
127-
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
128-
log_v("AP Station Connected: MAC: " MACSTR ", AID: %d", MAC2STR(event->mac), event->aid);
129-
#endif
130-
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STACONNECTED;
131-
memcpy(&arduino_event.event_info.wifi_ap_staconnected, event_data, sizeof(wifi_event_ap_staconnected_t));
132-
setStatusBits(ESP_NETIF_CONNECTED_BIT);
133-
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
134-
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
135-
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
136-
log_v("AP Station Disconnected: MAC: " MACSTR ", AID: %d", MAC2STR(event->mac), event->aid);
137-
#endif
138-
arduino_event.event_id = ARDUINO_EVENT_WIFI_AP_STADISCONNECTED;
139-
memcpy(&arduino_event.event_info.wifi_ap_stadisconnected, event_data, sizeof(wifi_event_ap_stadisconnected_t));
140-
// If no more clients are left
141-
wifi_sta_list_t clients;
142-
if (esp_wifi_ap_get_sta_list(&clients) != ESP_OK || clients.num == 0) {
143-
clearStatusBits(ESP_NETIF_CONNECTED_BIT);
95+
switch (event_id){
96+
case WIFI_EVENT_AP_START :
97+
log_v("AP Started");
98+
setStatusBits(ESP_NETIF_STARTED_BIT);
99+
Network.postEvent(ARDUINO_EVENT_WIFI_AP_START);
100+
return;
101+
case WIFI_EVENT_AP_STOP :
102+
log_v("AP Stopped");
103+
clearStatusBits(ESP_NETIF_STARTED_BIT | ESP_NETIF_CONNECTED_BIT | ESP_NETIF_HAS_IP_BIT | ESP_NETIF_HAS_LOCAL_IP6_BIT | ESP_NETIF_HAS_GLOBAL_IP6_BIT);
104+
Network.postEvent(ARDUINO_EVENT_WIFI_AP_STOP);
105+
return;
106+
case WIFI_EVENT_AP_PROBEREQRECVED : {
107+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
108+
wifi_event_ap_probe_req_rx_t *event = (wifi_event_ap_probe_req_rx_t *)event_data;
109+
log_v("AP Probe Request: RSSI: %d, MAC: " MACSTR, event->rssi, MAC2STR(event->mac));
110+
#endif
111+
arduino_event_info_t i;
112+
memcpy(&i.wifi_ap_probereqrecved, event_data, sizeof(wifi_event_ap_probe_req_rx_t));
113+
Network.postEvent(ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED, &i);
114+
return;
144115
}
145-
} else {
146-
return;
147-
}
148-
149-
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
150-
Network.postEvent(&arduino_event);
116+
case WIFI_EVENT_AP_STACONNECTED : {
117+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
118+
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
119+
log_v("AP Station Connected: MAC: " MACSTR ", AID: %d", MAC2STR(event->mac), event->aid);
120+
#endif
121+
setStatusBits(ESP_NETIF_CONNECTED_BIT);
122+
arduino_event_info_t i;
123+
memcpy(&i.wifi_ap_staconnected, event_data, sizeof(wifi_event_ap_staconnected_t));
124+
Network.postEvent(ARDUINO_EVENT_WIFI_AP_STACONNECTED, &i);
125+
return;
126+
}
127+
case WIFI_EVENT_AP_STADISCONNECTED : {
128+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
129+
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
130+
log_v("AP Station Disconnected: MAC: " MACSTR ", AID: %d", MAC2STR(event->mac), event->aid);
131+
#endif
132+
// If no more clients are left
133+
wifi_sta_list_t clients;
134+
if (esp_wifi_ap_get_sta_list(&clients) != ESP_OK || clients.num == 0) {
135+
clearStatusBits(ESP_NETIF_CONNECTED_BIT);
136+
}
137+
arduino_event_info_t i;
138+
memcpy(&i.wifi_ap_stadisconnected, event_data, sizeof(wifi_event_ap_stadisconnected_t));
139+
Network.postEvent(ARDUINO_EVENT_WIFI_AP_STADISCONNECTED, &i);
140+
return;
141+
}
142+
default :
143+
return;
151144
}
152145
}
153146

154-
APClass::APClass() {
155-
_ap_network_if = this;
156-
}
147+
APClass::APClass() {}
157148

158149
APClass::~APClass() {
159150
end();
160-
_ap_network_if = NULL;
161-
Network.removeEvent(evt_handle);
162-
evt_handle = 0;
151+
Network.removeEvent(_evt_handle);
152+
if (_ap_ev_instance != NULL) {
153+
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, _ap_ev_instance);
154+
_ap_ev_instance = NULL;
155+
}
163156
}
164157

165158
bool APClass::onEnable() {
166-
if (_ap_ev_instance == NULL && esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_ap_event_cb, this, &_ap_ev_instance)) {
167-
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
168-
return false;
159+
if (!_ap_ev_instance){
160+
esp_err_t err = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
161+
[](void* self, esp_event_base_t base, int32_t id, void* data) { static_cast<APClass*>(self)->_onApEvent(id, data); },
162+
this,
163+
&_ap_ev_instance
164+
);
165+
if (err){
166+
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
167+
return false;
168+
}
169169
}
170+
170171
if (_esp_netif == NULL) {
171-
if (!evt_handle)
172-
evt_handle = Network.onSysEvent(_onApArduinoEvent);
172+
if (!_evt_handle)
173+
_evt_handle = Network.onSysEvent([this](arduino_event_id_t event, const arduino_event_info_t *info){_onApArduinoEvent(event, info);});
173174
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_AP);
174175
/* attach to receive events */
175176
initNetif(ESP_NETIF_ID_AP);
@@ -178,14 +179,14 @@ bool APClass::onEnable() {
178179
}
179180

180181
bool APClass::onDisable() {
181-
Network.removeEvent(evt_handle);
182-
evt_handle = 0;
182+
Network.removeEvent(_evt_handle);
183+
_evt_handle = 0;
183184
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
184185
// That would be done by WiFi.enableAP(false) if STA is not enabled, or when it gets disabled
185186
_esp_netif = NULL;
186187
destroyNetif();
187188
if (_ap_ev_instance != NULL) {
188-
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &_ap_event_cb);
189+
esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, _ap_ev_instance);
189190
_ap_ev_instance = NULL;
190191
}
191192
return true;

libraries/WiFi/src/WiFiAP.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@ class APClass : public NetworkInterface {
5757
String SSID(void) const;
5858
uint8_t stationCount();
5959

60-
void _onApEvent(int32_t event_id, void *event_data);
61-
6260
protected:
6361
size_t printDriverInfo(Print &out) const;
6462

6563
friend class WiFiGenericClass;
6664
bool onEnable();
6765
bool onDisable();
66+
67+
private:
68+
// Arduino events
69+
network_event_handle_t _evt_handle{0};
70+
// esp AP events
71+
esp_event_handler_instance_t _ap_ev_instance{NULL};
72+
73+
// Arduino events
74+
void _onApArduinoEvent(arduino_event_id_t event, const arduino_event_info_t *info);
75+
// esp AP events
76+
void _onApEvent(int32_t event_id, void *event_data);
6877
};
6978

7079
// ----------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)