99#include " esp32-hal.h"
1010
1111
12- static void _network_event_task (void *arg) {
13- for (;;) {
14- ((NetworkEvents *)arg)->checkForEvent ();
15- }
16- vTaskDelete (NULL );
17- }
18-
1912NetworkEvents::NetworkEvents () : _arduino_event_group(NULL ), _arduino_event_queue(NULL ), _arduino_event_task_handle(NULL ) {}
2013
2114NetworkEvents::~NetworkEvents () {
@@ -64,7 +57,13 @@ bool NetworkEvents::initNetworkEvents() {
6457 }
6558
6659 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);
6867 if (!_arduino_event_task_handle) {
6968 log_e (" Network Event Task Start Failed!" );
7069 return false ;
@@ -91,33 +90,49 @@ bool NetworkEvents::postEvent(arduino_event_t *data) {
9190 return true ;
9291}
9392
94- void NetworkEvents::checkForEvent () {
95- arduino_event_t *event = NULL ;
93+ void NetworkEvents::_checkForEvent () {
94+ // this task can't run without the queue
9695 if (_arduino_event_queue == NULL ) {
96+ _arduino_event_task_handle = NULL ;
97+ vTaskDelete (NULL );
9798 return ;
9899 }
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);
116127 }
117128 }
118129 }
130+
131+ // release the event object's memory
132+ free (event);
119133 }
120- free (event);
134+
135+ vTaskDelete (NULL );
121136}
122137
123138template <typename T, typename ... U> static size_t getStdFunctionAddress (std::function<T(U...)> f) {
0 commit comments