@@ -27,7 +27,6 @@ static void _network_event_task(void * arg){
27
27
28
28
ESP_Network_Events::ESP_Network_Events ()
29
29
: _arduino_event_group(NULL )
30
- , _arduino_event_group_h(NULL )
31
30
, _arduino_event_queue(NULL )
32
31
, _arduino_event_task_handle(NULL )
33
32
{}
@@ -41,10 +40,6 @@ ESP_Network_Events::~ESP_Network_Events(){
41
40
vEventGroupDelete (_arduino_event_group);
42
41
_arduino_event_group = NULL ;
43
42
}
44
- if (_arduino_event_group_h != NULL ){
45
- vEventGroupDelete (_arduino_event_group_h);
46
- _arduino_event_group_h = NULL ;
47
- }
48
43
if (_arduino_event_queue != NULL ){
49
44
arduino_event_t *event = NULL ;
50
45
while (xQueueReceive (_arduino_event_queue, &event, 0 ) == pdTRUE){
@@ -66,13 +61,6 @@ bool ESP_Network_Events::initNetworkEvents(){
66
61
}
67
62
xEventGroupSetBits (_arduino_event_group, _initial_bits);
68
63
}
69
- if (!_arduino_event_group_h){
70
- _arduino_event_group_h = xEventGroupCreate ();
71
- if (!_arduino_event_group_h){
72
- log_e (" Network Event Group 2 Create Failed!" );
73
- return false ;
74
- }
75
- }
76
64
77
65
if (!_arduino_event_queue){
78
66
_arduino_event_queue = xQueueCreate (32 , sizeof (arduino_event_t *));
@@ -189,6 +177,48 @@ network_event_handle_t ESP_Network_Events::onEvent(NetworkEventSysCb cbEvent, ar
189
177
return newEventHandler.id ;
190
178
}
191
179
180
+ network_event_handle_t ESP_Network_Events::onSysEvent (NetworkEventCb cbEvent, arduino_event_id_t event)
181
+ {
182
+ if (!cbEvent) {
183
+ return 0 ;
184
+ }
185
+ NetworkEventCbList_t newEventHandler;
186
+ newEventHandler.cb = cbEvent;
187
+ newEventHandler.fcb = NULL ;
188
+ newEventHandler.scb = NULL ;
189
+ newEventHandler.event = event;
190
+ cbEventList.insert (cbEventList.begin (), newEventHandler);
191
+ return newEventHandler.id ;
192
+ }
193
+
194
+ network_event_handle_t ESP_Network_Events::onSysEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event)
195
+ {
196
+ if (!cbEvent) {
197
+ return 0 ;
198
+ }
199
+ NetworkEventCbList_t newEventHandler;
200
+ newEventHandler.cb = NULL ;
201
+ newEventHandler.fcb = cbEvent;
202
+ newEventHandler.scb = NULL ;
203
+ newEventHandler.event = event;
204
+ cbEventList.insert (cbEventList.begin (), newEventHandler);
205
+ return newEventHandler.id ;
206
+ }
207
+
208
+ network_event_handle_t ESP_Network_Events::onSysEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event)
209
+ {
210
+ if (!cbEvent) {
211
+ return 0 ;
212
+ }
213
+ NetworkEventCbList_t newEventHandler;
214
+ newEventHandler.cb = NULL ;
215
+ newEventHandler.fcb = NULL ;
216
+ newEventHandler.scb = cbEvent;
217
+ newEventHandler.event = event;
218
+ cbEventList.insert (cbEventList.begin (), newEventHandler);
219
+ return newEventHandler.id ;
220
+ }
221
+
192
222
void ESP_Network_Events::removeEvent (NetworkEventCb cbEvent, arduino_event_id_t event)
193
223
{
194
224
if (!cbEvent) {
@@ -203,6 +233,27 @@ void ESP_Network_Events::removeEvent(NetworkEventCb cbEvent, arduino_event_id_t
203
233
}
204
234
}
205
235
236
+ template <typename T, typename ... U>
237
+ static size_t getStdFunctionAddress (std::function<T(U...)> f) {
238
+ typedef T (fnType)(U...);
239
+ fnType ** fnPointer = f.template target <fnType*>();
240
+ return (size_t ) *fnPointer;
241
+ }
242
+
243
+ void ESP_Network_Events::removeEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event)
244
+ {
245
+ if (!cbEvent) {
246
+ return ;
247
+ }
248
+
249
+ for (uint32_t i = 0 ; i < cbEventList.size (); i++) {
250
+ NetworkEventCbList_t entry = cbEventList[i];
251
+ if (getStdFunctionAddress (entry.fcb ) == getStdFunctionAddress (cbEvent) && entry.event == event) {
252
+ cbEventList.erase (cbEventList.begin () + i);
253
+ }
254
+ }
255
+ }
256
+
206
257
void ESP_Network_Events::removeEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event)
207
258
{
208
259
if (!cbEvent) {
@@ -232,62 +283,42 @@ int ESP_Network_Events::setStatusBits(int bits){
232
283
_initial_bits |= bits;
233
284
return _initial_bits;
234
285
}
235
- int l=bits & 0x00FFFFFF , h=(bits & 0xFF000000 ) >> 24 ;
236
- if (l){
237
- l = xEventGroupSetBits (_arduino_event_group, l);
238
- }
239
- if (h){
240
- h = xEventGroupSetBits (_arduino_event_group_h, h);
241
- }
242
- return l | (h << 24 );
286
+ return xEventGroupSetBits (_arduino_event_group, bits);
243
287
}
244
288
245
289
int ESP_Network_Events::clearStatusBits (int bits){
246
290
if (!_arduino_event_group){
247
291
_initial_bits &= ~bits;
248
292
return _initial_bits;
249
293
}
250
- int l=bits & 0x00FFFFFF , h=(bits & 0xFF000000 ) >> 24 ;
251
- if (l){
252
- l = xEventGroupClearBits (_arduino_event_group, l);
253
- }
254
- if (h){
255
- h = xEventGroupClearBits (_arduino_event_group_h, h);
256
- }
257
- return l | (h << 24 );
294
+ return xEventGroupClearBits (_arduino_event_group, bits);
258
295
}
259
296
260
297
int ESP_Network_Events::getStatusBits (){
261
298
if (!_arduino_event_group){
262
299
return _initial_bits;
263
300
}
264
- return xEventGroupGetBits (_arduino_event_group) | ( xEventGroupGetBits (_arduino_event_group_h) << 24 ) ;
301
+ return xEventGroupGetBits (_arduino_event_group);
265
302
}
266
303
267
304
int ESP_Network_Events::waitStatusBits (int bits, uint32_t timeout_ms){
268
305
if (!_arduino_event_group){
269
306
return 0 ;
270
307
}
271
- int l=bits & 0x00FFFFFF , h=(bits & 0xFF000000 ) >> 24 ;
272
- if (l){
273
- l = xEventGroupWaitBits (
274
- _arduino_event_group, // The event group being tested.
275
- l, // The bits within the event group to wait for.
276
- pdFALSE, // bits should be cleared before returning.
277
- pdTRUE, // Don't wait for all bits, any bit will do.
278
- timeout_ms / portTICK_PERIOD_MS ) & l; // Wait a maximum of timeout_ms for any bit to be set.
279
- }
280
- if (h){
281
- h = xEventGroupWaitBits (
282
- _arduino_event_group, // The event group being tested.
283
- h, // The bits within the event group to wait for.
284
- pdFALSE, // bits should be cleared before returning.
285
- pdTRUE, // Don't wait for all bits, any bit will do.
286
- timeout_ms / portTICK_PERIOD_MS ) & h; // Wait a maximum of timeout_ms for any bit to be set.
287
- }
288
- return l | (h << 24 );
308
+ return xEventGroupWaitBits (
309
+ _arduino_event_group, // The event group being tested.
310
+ bits, // The bits within the event group to wait for.
311
+ pdFALSE, // bits should be cleared before returning.
312
+ pdTRUE, // Don't wait for all bits, any bit will do.
313
+ timeout_ms / portTICK_PERIOD_MS ) & bits; // Wait a maximum of timeout_ms for any bit to be set.
289
314
}
290
315
316
+ /* *
317
+ * @brief Convert arduino_event_id_t to a C string.
318
+ * @param [in] id The event id to be converted.
319
+ * @return A string representation of the event id.
320
+ * @note: arduino_event_id_t values as of Mar 2023 (arduino-esp32 r2.0.7) are: 0-39 (ARDUINO_EVENT_MAX=40) and are defined in WiFiGeneric.h.
321
+ */
291
322
const char * ESP_Network_Events::eventName (arduino_event_id_t id) {
292
323
switch (id) {
293
324
case ARDUINO_EVENT_ETH_START: return " ETH_START" ;
@@ -306,6 +337,7 @@ const char * ESP_Network_Events::eventName(arduino_event_id_t id) {
306
337
// case ARDUINO_EVENT_PPP_LOST_IP: return "PPP_LOST_IP";
307
338
// case ARDUINO_EVENT_PPP_GOT_IP6: return "PPP_GOT_IP6";
308
339
#if SOC_WIFI_SUPPORTED
340
+ case ARDUINO_EVENT_WIFI_OFF: return " WIFI_OFF" ;
309
341
case ARDUINO_EVENT_WIFI_READY: return " WIFI_READY" ;
310
342
case ARDUINO_EVENT_WIFI_SCAN_DONE: return " SCAN_DONE" ;
311
343
case ARDUINO_EVENT_WIFI_STA_START: return " STA_START" ;
0 commit comments