15
15
#endif
16
16
#include < functional>
17
17
#include " freertos/FreeRTOS.h"
18
- #include " freertos/task.h"
19
- #include " freertos/queue.h"
20
- #include " freertos/semphr.h"
21
18
#include " freertos/event_groups.h"
22
19
#if defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
23
20
#include < mutex>
@@ -39,8 +36,11 @@ constexpr int WIFI_SCAN_DONE_BIT = BIT1;
39
36
#define NET_HAS_IP6_GLOBAL_BIT 0
40
37
41
38
ESP_EVENT_DECLARE_BASE (ARDUINO_EVENTS);
39
+ // backward compatibility
40
+ #define ARDUINO_EVENT_MAX ARDUINO_EVENT_ANY
42
41
43
42
typedef enum {
43
+ ARDUINO_EVENT_ANY = ESP_EVENT_ANY_ID, // make all ARDUINO events compatibile with ANY ESP_EVENT
44
44
ARDUINO_EVENT_NONE = 0 ,
45
45
ARDUINO_EVENT_ETH_START,
46
46
ARDUINO_EVENT_ETH_STOP,
@@ -93,9 +93,9 @@ typedef enum {
93
93
ARDUINO_EVENT_PPP_GOT_IP,
94
94
ARDUINO_EVENT_PPP_LOST_IP,
95
95
ARDUINO_EVENT_PPP_GOT_IP6,
96
- ARDUINO_EVENT_MAX
97
96
} arduino_event_id_t ;
98
97
98
+
99
99
typedef union {
100
100
ip_event_ap_staipassigned_t wifi_ap_staipassigned;
101
101
ip_event_got_ip_t got_ip;
@@ -148,7 +148,7 @@ using network_event_handle_t = size_t;
148
148
*/
149
149
class NetworkEvents {
150
150
public:
151
- NetworkEvents ();
151
+ NetworkEvents (){} ;
152
152
~NetworkEvents ();
153
153
154
154
/* *
@@ -159,7 +159,7 @@ class NetworkEvents {
159
159
* @param event event to process, any event by default
160
160
* @return network_event_handle_t
161
161
*/
162
- network_event_handle_t onEvent (NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
162
+ network_event_handle_t onEvent (NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
163
163
164
164
/* *
165
165
* @brief register functional callback to be executed on arduino event(s)
@@ -170,7 +170,7 @@ class NetworkEvents {
170
170
* @param event event to process, any event by default
171
171
* @return network_event_handle_t
172
172
*/
173
- network_event_handle_t onEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
173
+ network_event_handle_t onEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
174
174
175
175
/* *
176
176
* @brief register static system callback to be executed on arduino event(s)
@@ -182,7 +182,7 @@ class NetworkEvents {
182
182
* @param event event to process, any event by default
183
183
* @return network_event_handle_t
184
184
*/
185
- network_event_handle_t onEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
185
+ network_event_handle_t onEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
186
186
187
187
/* *
188
188
* @brief unregister static function callback
@@ -191,7 +191,7 @@ class NetworkEvents {
191
191
* @param cbEvent static callback function
192
192
* @param event event to process, any event by default
193
193
*/
194
- void removeEvent (NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
194
+ void removeEvent (NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
195
195
196
196
/* *
197
197
* @brief unregister functional callback
@@ -201,7 +201,7 @@ class NetworkEvents {
201
201
* @param cbEvent functional callback
202
202
* @param event event to process, any event by default
203
203
*/
204
- void removeEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX )
204
+ void removeEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY )
205
205
__attribute__((deprecated(" removing functional callbacks via pointer is deprecated, use removeEvent(network_event_handle_t) instead" )));
206
206
207
207
/* *
@@ -211,21 +211,19 @@ class NetworkEvents {
211
211
* @param cbEvent static callback function
212
212
* @param event event to process, any event by default
213
213
*/
214
- void removeEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
214
+ void removeEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
215
215
216
216
/* *
217
217
* @brief unregister event callback via handler
218
218
*
219
- * @param cbEvent static callback function
220
- * @param event event to process, any event by default
219
+ * @param event_handle a handler to remove
221
220
*/
222
221
void removeEvent (network_event_handle_t event_handle);
223
222
224
223
/* *
225
224
* @brief get a human-readable name of an event by it's id
226
225
*
227
226
* @param id event id code
228
- * @return const char* event name string
229
227
*/
230
228
static const char *eventName (arduino_event_id_t id);
231
229
@@ -238,7 +236,7 @@ class NetworkEvents {
238
236
* @return true if event was queued susccessfuly
239
237
* @return false on memrory allocation error or queue is full
240
238
*/
241
- bool postEvent (const arduino_event_t *event);
239
+ bool postEvent (const arduino_event_t *event, TickType_t timeout = portMAX_DELAY );
242
240
243
241
int getStatusBits () const ;
244
242
int waitStatusBits (int bits, uint32_t timeout_ms);
@@ -257,11 +255,11 @@ class NetworkEvents {
257
255
protected:
258
256
bool initNetworkEvents ();
259
257
// same as onEvent() but places newly added handler at the beginning of registered events list
260
- network_event_handle_t onSysEvent (NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
258
+ network_event_handle_t onSysEvent (NetworkEventCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
261
259
// same as onEvent() but places newly added handler at the beginning of registered events list
262
- network_event_handle_t onSysEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
260
+ network_event_handle_t onSysEvent (NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
263
261
// same as onEvent() but places newly added handler at the beginning of registered events list
264
- network_event_handle_t onSysEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX );
262
+ network_event_handle_t onSysEvent (NetworkEventSysCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_ANY );
265
263
266
264
private:
267
265
/* *
@@ -280,17 +278,20 @@ class NetworkEvents {
280
278
281
279
explicit NetworkEventCbList_t (
282
280
network_event_handle_t id, NetworkEventCb cb = nullptr , NetworkEventFuncCb fcb = nullptr , NetworkEventSysCb scb = nullptr ,
283
- arduino_event_id_t event = ARDUINO_EVENT_MAX
281
+ arduino_event_id_t event = ARDUINO_EVENT_ANY
284
282
)
285
283
: id(id), cb(cb), fcb(fcb), scb(scb), event(event) {}
286
284
};
287
285
288
286
// define initial id's value
289
287
network_event_handle_t _current_id{0 };
288
+ // EventGroup i.c.
289
+ uint32_t _initial_bits{0 };
290
+
291
+ // ESP event bus handler
292
+ esp_event_handler_instance_t _evt_handler{nullptr };
290
293
291
- EventGroupHandle_t _arduino_event_group;
292
- QueueHandle_t _arduino_event_queue;
293
- TaskHandle_t _arduino_event_task_handle;
294
+ EventGroupHandle_t _arduino_event_group{nullptr };
294
295
295
296
// registered events callbacks container
296
297
std::vector<NetworkEventCbList_t> _cbEventList;
@@ -300,9 +301,6 @@ class NetworkEvents {
300
301
std::mutex _mtx;
301
302
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
302
303
303
- /* *
304
- * @brief task function that picks events from an event queue and calls registered callbacks
305
- *
306
- */
307
- void _checkForEvent ();
304
+ // ESP event bus callback function, picks events from an event queue and calls registered callbacks
305
+ void _evt_picker (int32_t id, arduino_event_info_t *info);
308
306
};
0 commit comments