9
9
#include " esp32-hal.h"
10
10
11
11
12
- static void _network_event_task (void *arg) {
13
- for (;;) {
14
- ((NetworkEvents *)arg)->checkForEvent ();
15
- }
16
- vTaskDelete (NULL );
17
- }
18
-
19
12
NetworkEvents::NetworkEvents () : _arduino_event_group(NULL ), _arduino_event_queue(NULL ), _arduino_event_task_handle(NULL ) {}
20
13
21
14
NetworkEvents::~NetworkEvents () {
@@ -64,7 +57,13 @@ bool NetworkEvents::initNetworkEvents() {
64
57
}
65
58
66
59
if (!_arduino_event_task_handle) {
67
- xTaskCreateUniversal (_network_event_task, " arduino_events" , 4096 , this , ESP_TASKD_EVENT_PRIO - 1 , &_arduino_event_task_handle, ARDUINO_EVENT_RUNNING_CORE);
60
+ xTaskCreateUniversal ( [](void * self){ static_cast <NetworkEvents*>(self)->_checkForEvent (); },
61
+ " arduino_events" , // label
62
+ 4096 , // event task's stack size
63
+ this ,
64
+ ESP_TASKD_EVENT_PRIO - 1 ,
65
+ &_arduino_event_task_handle,
66
+ ARDUINO_EVENT_RUNNING_CORE);
68
67
if (!_arduino_event_task_handle) {
69
68
log_e (" Network Event Task Start Failed!" );
70
69
return false ;
@@ -91,33 +90,49 @@ bool NetworkEvents::postEvent(arduino_event_t *data) {
91
90
return true ;
92
91
}
93
92
94
- void NetworkEvents::checkForEvent () {
95
- arduino_event_t *event = NULL ;
93
+ void NetworkEvents::_checkForEvent () {
94
+ // this task can't run without the queue
96
95
if (_arduino_event_queue == NULL ) {
96
+ _arduino_event_task_handle = NULL ;
97
+ vTaskDelete (NULL );
97
98
return ;
98
99
}
99
- if (xQueueReceive (_arduino_event_queue, &event, portMAX_DELAY) != pdTRUE) {
100
- return ;
101
- }
102
- if (event == NULL ) {
103
- return ;
104
- }
105
- log_v (" Network Event: %d - %s" , event->event_id , eventName (event->event_id ));
106
- for (uint32_t i = 0 ; i < cbEventList.size (); i++) {
107
- NetworkEventCbList_t entry = cbEventList[i];
108
- if (entry.cb || entry.fcb || entry.scb ) {
109
- if (entry.event == (arduino_event_id_t )event->event_id || entry.event == ARDUINO_EVENT_MAX) {
110
- if (entry.cb ) {
111
- entry.cb ((arduino_event_id_t )event->event_id );
112
- } else if (entry.fcb ) {
113
- entry.fcb ((arduino_event_id_t )event->event_id , (arduino_event_info_t )event->event_info );
114
- } else {
115
- entry.scb (event);
100
+
101
+ for (;;) {
102
+ arduino_event_t *event = NULL ;
103
+ // wait for an event on a queue
104
+ if (xQueueReceive (_arduino_event_queue, &event, portMAX_DELAY) != pdTRUE) {
105
+ continue ;
106
+ }
107
+ if (event == NULL ) {
108
+ continue ;
109
+ }
110
+ log_v (" Network Event: %d - %s" , event->event_id , eventName (event->event_id ));
111
+
112
+ // iterate over registered callbacks
113
+ for (auto &i : cbEventList){
114
+ if (i.cb || i.fcb || i.scb ) {
115
+ if (i.event == (arduino_event_id_t )event->event_id || i.event == ARDUINO_EVENT_MAX) {
116
+ if (i.cb ) {
117
+ i.cb ((arduino_event_id_t )event->event_id );
118
+ continue ;
119
+ }
120
+
121
+ if (i.fcb ) {
122
+ i.fcb ((arduino_event_id_t )event->event_id , (arduino_event_info_t )event->event_info );
123
+ continue ;
124
+ }
125
+
126
+ i.scb (event);
116
127
}
117
128
}
118
129
}
130
+
131
+ // release the event object's memory
132
+ free (event);
119
133
}
120
- free (event);
134
+
135
+ vTaskDelete (NULL );
121
136
}
122
137
123
138
template <typename T, typename ... U> static size_t getStdFunctionAddress (std::function<T(U...)> f) {
0 commit comments