Skip to content

Commit 2b8d9a2

Browse files
committed
Network events handling improvements
- use handlers to avoid dublicate event subscriptions - replace ARDUINO_EVENT_MAX with ARDUINO_EVENT_ANY to match with ESP event loop
1 parent 900848e commit 2b8d9a2

File tree

9 files changed

+74
-43
lines changed

9 files changed

+74
-43
lines changed

Diff for: docs/en/api/wifi.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Function pointer callback taking the event ID:
6161
.. code-block:: arduino
6262
6363
typedef void (*WiFiEventCb)(arduino_event_id_t);
64-
wifi_event_id_t onEvent(WiFiEventCb, arduino_event_id_t = ARDUINO_EVENT_MAX);
64+
wifi_event_id_t onEvent(WiFiEventCb, arduino_event_id_t = ARDUINO_EVENT_ANY);
6565
6666
Function pointer callback taking an event-ID-and-info struct:
6767

@@ -73,26 +73,26 @@ Function pointer callback taking an event-ID-and-info struct:
7373
} arduino_event_t;
7474
7575
typedef void (*WiFiEventSysCb)(arduino_event_t *);
76-
wifi_event_id_t onEvent(WiFiEventSysCb, arduino_event_id_t = ARDUINO_EVENT_MAX);
76+
wifi_event_id_t onEvent(WiFiEventSysCb, arduino_event_id_t = ARDUINO_EVENT_ANY);
7777
7878
Callback using ``std::function`` taking event ID and info separately:
7979

8080
.. code-block:: arduino
8181
8282
typedef std::function<void(arduino_event_id_t, arduino_event_info_t)> WiFiEventFuncCb;
83-
wifi_event_id_t onEvent(WiFiEventFuncCb, arduino_event_id_t = ARDUINO_EVENT_MAX);
83+
wifi_event_id_t onEvent(WiFiEventFuncCb, arduino_event_id_t = ARDUINO_EVENT_ANY);
8484
8585
A similar set of functions are available to remove callbacks:
8686

8787
.. code-block:: arduino
8888
89-
void removeEvent(WiFiEventCb, arduino_event_id_t = ARDUINO_EVENT_MAX);
90-
void removeEvent(WiFiEventSysCb, arduino_event_id_t = ARDUINO_EVENT_MAX);
91-
void removeEvent(wifi_event_id_t = ARDUINO_EVENT_MAX);
89+
void removeEvent(WiFiEventCb, arduino_event_id_t = ARDUINO_EVENT_ANY);
90+
void removeEvent(WiFiEventSysCb, arduino_event_id_t = ARDUINO_EVENT_ANY);
91+
void removeEvent(wifi_event_id_t = ARDUINO_EVENT_ANY);
9292
9393
In all cases, the subscribing function accepts an optional event type to
9494
invoke the callback only for that specific event; with the default
95-
``ARDUINO_EVENT_MAX``, the callback will be invoked for all Wi-Fi events.
95+
``ARDUINO_EVENT_ANY``, the callback will be invoked for all Wi-Fi events.
9696

9797
Any callback function is given the event type in a parameter.
9898
Some of the possible callback function formats also take an

Diff for: libraries/Ethernet/src/ETH.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ esp_eth_handle_t ETHClass::handle() const {
9494

9595
void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
9696
arduino_event_t arduino_event;
97-
arduino_event.event_id = ARDUINO_EVENT_MAX;
97+
arduino_event.event_id = ARDUINO_EVENT_ANY;
9898

9999
if (event_id == ETHERNET_EVENT_CONNECTED) {
100100
log_v("%s Connected", desc());
@@ -118,7 +118,7 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
118118
);
119119
}
120120

121-
if (arduino_event.event_id < ARDUINO_EVENT_MAX) {
121+
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
122122
Network.postEvent(&arduino_event);
123123
}
124124
}

Diff for: libraries/Network/src/NetworkEvents.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ bool NetworkEvents::postEvent(const arduino_event_t *data, TickType_t timeout) {
6161
return false;
6262
}
6363

64+
bool NetworkEvents::postEvent(arduino_event_id_t event, TickType_t timeout){
65+
return esp_event_post(ARDUINO_EVENTS, static_cast<int32_t>(event), NULL, 0, timeout) == pdTRUE;
66+
}
67+
68+
6469
void NetworkEvents::_evt_picker(int32_t id, arduino_event_info_t *info){
6570
#if defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
6671
std::lock_guard<std::mutex> lock(_mtx);
@@ -75,14 +80,18 @@ void NetworkEvents::_evt_picker(int32_t id, arduino_event_info_t *info){
7580
}
7681

7782
if (i.fcb) {
78-
i.fcb(static_cast<arduino_event_id_t>(id), *info);
83+
if (info)
84+
i.fcb(static_cast<arduino_event_id_t>(id), *info);
85+
else
86+
i.fcb(static_cast<arduino_event_id_t>(id), {});
7987
continue;
8088
}
8189

82-
if (i.scb && info){
90+
if (i.scb){
8391
// system event callback needs a ptr to struct
8492
arduino_event_t event{static_cast<arduino_event_id_t>(id), {}};
85-
memcpy(&event.event_info, info, sizeof(arduino_event_info_t));
93+
if (info)
94+
memcpy(&event.event_info, info, sizeof(arduino_event_info_t));
8695
i.scb(&event);
8796
}
8897
}

Diff for: libraries/Network/src/NetworkInterface.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void _ip_event_cb(void *arg, esp_event_base_t event_base, int32_t event_i
7070

7171
void NetworkInterface::_onIpEvent(int32_t event_id, void *event_data) {
7272
arduino_event_t arduino_event;
73-
arduino_event.event_id = ARDUINO_EVENT_MAX;
73+
arduino_event.event_id = ARDUINO_EVENT_ANY;
7474
if (event_id == _got_ip_event_id) {
7575
setStatusBits(ESP_NETIF_HAS_IP_BIT);
7676
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
@@ -151,7 +151,7 @@ void NetworkInterface::_onIpEvent(int32_t event_id, void *event_data) {
151151
#endif
152152
}
153153

154-
if (arduino_event.event_id < ARDUINO_EVENT_MAX) {
154+
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
155155
Network.postEvent(&arduino_event);
156156
}
157157
}

Diff for: libraries/PPP/src/PPP.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void PPPClass::_onPppArduinoEvent(arduino_event_id_t event, arduino_event_info_t
128128
// PPP Driver Events Callback
129129
void PPPClass::_onPppEvent(int32_t event, void *event_data) {
130130
arduino_event_t arduino_event;
131-
arduino_event.event_id = ARDUINO_EVENT_MAX;
131+
arduino_event.event_id = ARDUINO_EVENT_ANY;
132132

133133
log_v("PPP Driver Event %ld: %s", event, _ppp_event_name(event));
134134

@@ -141,7 +141,7 @@ void PPPClass::_onPppEvent(int32_t event, void *event_data) {
141141
}
142142
}
143143

144-
if (arduino_event.event_id < ARDUINO_EVENT_MAX) {
144+
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
145145
Network.postEvent(&arduino_event);
146146
}
147147
}

Diff for: libraries/WiFi/src/AP.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
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+
2528
esp_netif_t *get_esp_interface_netif(esp_interface_t interface);
2629

2730
static size_t _wifi_strncpy(char *dst, const char *src, size_t dst_len) {
@@ -85,7 +88,7 @@ static void _onApArduinoEvent(arduino_event_t *ev) {
8588
if (_ap_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_AP_START || ev->event_id > ARDUINO_EVENT_WIFI_AP_GOT_IP6) {
8689
return;
8790
}
88-
log_v("Arduino AP Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
91+
log_v("Arduino AP Event: %d - %s", ev->event_id, NetworkEvents::eventName(ev->event_id));
8992
if (ev->event_id == ARDUINO_EVENT_WIFI_AP_START) {
9093
#if CONFIG_LWIP_IPV6
9194
if (_ap_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT) {
@@ -102,7 +105,7 @@ static void _onApArduinoEvent(arduino_event_t *ev) {
102105

103106
void APClass::_onApEvent(int32_t event_id, void *event_data) {
104107
arduino_event_t arduino_event;
105-
arduino_event.event_id = ARDUINO_EVENT_MAX;
108+
arduino_event.event_id = ARDUINO_EVENT_ANY;
106109

107110
if (event_id == WIFI_EVENT_AP_START) {
108111
log_v("AP Started");
@@ -143,7 +146,7 @@ void APClass::_onApEvent(int32_t event_id, void *event_data) {
143146
return;
144147
}
145148

146-
if (arduino_event.event_id < ARDUINO_EVENT_MAX) {
149+
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
147150
Network.postEvent(&arduino_event);
148151
}
149152
}
@@ -155,6 +158,8 @@ APClass::APClass() {
155158
APClass::~APClass() {
156159
end();
157160
_ap_network_if = NULL;
161+
Network.removeEvent(evt_handle);
162+
evt_handle = 0;
158163
}
159164

160165
bool APClass::onEnable() {
@@ -163,7 +168,8 @@ bool APClass::onEnable() {
163168
return false;
164169
}
165170
if (_esp_netif == NULL) {
166-
Network.onSysEvent(_onApArduinoEvent);
171+
if (!evt_handle)
172+
evt_handle = Network.onSysEvent(_onApArduinoEvent);
167173
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_AP);
168174
/* attach to receive events */
169175
initNetif(ESP_NETIF_ID_AP);
@@ -172,7 +178,8 @@ bool APClass::onEnable() {
172178
}
173179

174180
bool APClass::onDisable() {
175-
Network.removeEvent(_onApArduinoEvent);
181+
Network.removeEvent(evt_handle);
182+
evt_handle = 0;
176183
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
177184
// That would be done by WiFi.enableAP(false) if STA is not enabled, or when it gets disabled
178185
_esp_netif = NULL;

Diff for: libraries/WiFi/src/STA.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "esp_wpa2.h"
3131
#endif
3232

33+
// a static handle for event callback
34+
static network_event_handle_t evt_handle{0};
35+
3336
esp_netif_t *get_esp_interface_netif(esp_interface_t interface);
3437

3538
static size_t _wifi_strncpy(char *dst, const char *src, size_t dst_len) {
@@ -107,7 +110,7 @@ static void _onStaArduinoEvent(arduino_event_t *ev) {
107110
return;
108111
}
109112
static bool first_connect = true;
110-
log_v("Arduino STA Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
113+
log_v("Arduino STA Event: %d - %s", ev->event_id, NetworkEvents::eventName(ev->event_id));
111114

112115
if (ev->event_id == ARDUINO_EVENT_WIFI_STA_START) {
113116
_sta_network_if->_setStatus(WL_DISCONNECTED);
@@ -180,7 +183,7 @@ static void _onStaArduinoEvent(arduino_event_t *ev) {
180183

181184
void STAClass::_onStaEvent(int32_t event_id, void *event_data) {
182185
arduino_event_t arduino_event;
183-
arduino_event.event_id = ARDUINO_EVENT_MAX;
186+
arduino_event.event_id = ARDUINO_EVENT_ANY;
184187

185188
if (event_id == WIFI_EVENT_STA_START) {
186189
log_v("STA Started");
@@ -222,7 +225,7 @@ void STAClass::_onStaEvent(int32_t event_id, void *event_data) {
222225
return;
223226
}
224227

225-
if (arduino_event.event_id < ARDUINO_EVENT_MAX) {
228+
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
226229
Network.postEvent(&arduino_event);
227230
}
228231
}
@@ -235,6 +238,8 @@ STAClass::STAClass()
235238
STAClass::~STAClass() {
236239
end();
237240
_sta_network_if = NULL;
241+
Network.removeEvent(evt_handle);
242+
evt_handle = 0;
238243
}
239244

240245
wl_status_t STAClass::status() {
@@ -276,14 +281,16 @@ bool STAClass::onEnable() {
276281
return false;
277282
}
278283
/* attach to receive events */
279-
Network.onSysEvent(_onStaArduinoEvent);
284+
if (!evt_handle)
285+
evt_handle = Network.onSysEvent(_onStaArduinoEvent);
280286
initNetif(ESP_NETIF_ID_STA);
281287
}
282288
return true;
283289
}
284290

285291
bool STAClass::onDisable() {
286-
Network.removeEvent(_onStaArduinoEvent);
292+
Network.removeEvent(evt_handle);
293+
evt_handle = 0;
287294
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
288295
// That would be done by WiFi.enableSTA(false) if AP is not enabled, or when it gets disabled
289296
_esp_netif = NULL;

Diff for: libraries/WiFi/src/WiFiGeneric.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ extern "C" {
5757
#include "sdkconfig.h"
5858

5959
static esp_netif_t *esp_netifs[ESP_IF_MAX] = {NULL, NULL, NULL};
60+
// a static handle for event callback
61+
static network_event_handle_t evt_handle{0};
6062

6163
esp_netif_t *get_esp_interface_netif(esp_interface_t interface) {
6264
if (interface < ESP_IF_MAX) {
@@ -67,7 +69,7 @@ esp_netif_t *get_esp_interface_netif(esp_interface_t interface) {
6769

6870
static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
6971
arduino_event_t arduino_event;
70-
arduino_event.event_id = ARDUINO_EVENT_MAX;
72+
arduino_event.event_id = ARDUINO_EVENT_ANY;
7173

7274
/*
7375
* SCAN
@@ -163,7 +165,7 @@ static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t ev
163165
#endif
164166
}
165167

166-
if (arduino_event.event_id < ARDUINO_EVENT_MAX) {
168+
if (arduino_event.event_id != ARDUINO_EVENT_ANY) {
167169
Network.postEvent(&arduino_event);
168170
}
169171
}
@@ -409,12 +411,13 @@ wifi_ps_type_t WiFiGenericClass::_sleepEnabled = WIFI_PS_MIN_MODEM;
409411

410412
WiFiGenericClass::WiFiGenericClass() {}
411413

412-
const char *WiFiGenericClass::disconnectReasonName(wifi_err_reason_t reason) {
413-
return WiFi.STA.disconnectReasonName(reason);
414+
WiFiGenericClass::~WiFiGenericClass(){
415+
Network.removeEvent(evt_handle);
416+
evt_handle = 0;
414417
}
415418

416-
const char *WiFiGenericClass::eventName(arduino_event_id_t id) {
417-
return Network.eventName(id);
419+
const char *WiFiGenericClass::disconnectReasonName(wifi_err_reason_t reason) {
420+
return WiFi.STA.disconnectReasonName(reason);
418421
}
419422

420423
const char *WiFiGenericClass::getHostname() {
@@ -437,17 +440,18 @@ void WiFiGenericClass::_eventCallback(arduino_event_t *event) {
437440
// log_d("Arduino Event: %d - %s", event->event_id, WiFi.eventName(event->event_id));
438441
if (event->event_id == ARDUINO_EVENT_WIFI_SCAN_DONE) {
439442
WiFiScanClass::_scanDone();
443+
}
440444
#if !CONFIG_ESP_WIFI_REMOTE_ENABLED
441-
} else if (event->event_id == ARDUINO_EVENT_SC_GOT_SSID_PSWD) {
445+
else if (event->event_id == ARDUINO_EVENT_SC_GOT_SSID_PSWD) {
442446
WiFi.begin(
443447
(const char *)event->event_info.sc_got_ssid_pswd.ssid, (const char *)event->event_info.sc_got_ssid_pswd.password, 0,
444448
((event->event_info.sc_got_ssid_pswd.bssid_set == true) ? event->event_info.sc_got_ssid_pswd.bssid : NULL)
445449
);
446450
} else if (event->event_id == ARDUINO_EVENT_SC_SEND_ACK_DONE) {
447451
esp_smartconfig_stop();
448452
WiFiSTAClass::_smartConfigDone = true;
449-
#endif
450453
}
454+
#endif
451455
}
452456

453457
/**
@@ -527,7 +531,8 @@ bool WiFiGenericClass::mode(wifi_mode_t m) {
527531
if (!wifiLowLevelInit(_persistent)) {
528532
return false;
529533
}
530-
Network.onSysEvent(_eventCallback);
534+
if (!evt_handle)
535+
evt_handle = Network.onSysEvent(_eventCallback);
531536
}
532537

533538
if (((m & WIFI_MODE_STA) != 0) && ((cm & WIFI_MODE_STA) == 0)) {
@@ -552,7 +557,8 @@ bool WiFiGenericClass::mode(wifi_mode_t m) {
552557
// we are disabling AP interface
553558
WiFi.AP.onDisable();
554559
}
555-
Network.removeEvent(_eventCallback);
560+
Network.removeEvent(evt_handle);
561+
evt_handle = 0;
556562
return true;
557563
}
558564

Diff for: libraries/WiFi/src/WiFiGeneric.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ typedef enum {
8181
class WiFiGenericClass {
8282
public:
8383
WiFiGenericClass();
84+
virtual ~WiFiGenericClass();
8485

85-
wifi_event_id_t onEvent(WiFiEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
86-
wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
87-
wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
88-
void removeEvent(WiFiEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
89-
void removeEvent(WiFiEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX);
86+
wifi_event_id_t onEvent(WiFiEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY);
87+
wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY);
88+
wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY);
89+
void removeEvent(WiFiEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY);
90+
void removeEvent(WiFiEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY);
9091
void removeEvent(wifi_event_id_t id);
9192

9293
static int getStatusBits();
@@ -131,9 +132,8 @@ class WiFiGenericClass {
131132
static uint8_t calculateSubnetCIDR(IPAddress subnetMask);
132133

133134
const char *disconnectReasonName(wifi_err_reason_t reason);
134-
const char *eventName(arduino_event_id_t id);
135+
static const char *eventName(arduino_event_id_t id){ return NetworkEvents::eventName(id); };
135136

136-
static void _eventCallback(arduino_event_t *event);
137137

138138
protected:
139139
static bool _persistent;
@@ -145,6 +145,8 @@ class WiFiGenericClass {
145145
static int setStatusBits(int bits);
146146
static int clearStatusBits(int bits);
147147

148+
static void _eventCallback(arduino_event_t *event);
149+
148150
friend class WiFiSTAClass;
149151
friend class WiFiScanClass;
150152
friend class WiFiAPClass;

0 commit comments

Comments
 (0)