@@ -54,15 +54,18 @@ bool NetworkEvents::initNetworkEvents() {
54
54
bool NetworkEvents::postEvent (const arduino_event_t *data, TickType_t timeout) {
55
55
if (!data) return false ;
56
56
esp_err_t err = esp_event_post (ARDUINO_EVENTS, static_cast <int32_t >(data->event_id ), &data->event_info , sizeof (data->event_info ), timeout);
57
- if (err == pdTRUE )
57
+ if (err == ESP_OK )
58
58
return true ;
59
59
60
60
log_e (" Arduino Event Send Failed!" );
61
61
return false ;
62
62
}
63
63
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;
64
+ bool NetworkEvents::postEvent (arduino_event_id_t event, const arduino_event_info_t *info, TickType_t timeout){
65
+ if (info)
66
+ return esp_event_post (ARDUINO_EVENTS, static_cast <int32_t >(event), info, sizeof (arduino_event_info_t ), timeout) == pdTRUE;
67
+ else
68
+ return esp_event_post (ARDUINO_EVENTS, static_cast <int32_t >(event), NULL , 0 , timeout) == pdTRUE;
66
69
}
67
70
68
71
@@ -73,27 +76,27 @@ void NetworkEvents::_evt_picker(int32_t id, arduino_event_info_t *info){
73
76
74
77
// iterate over registered callbacks
75
78
for (auto &i : _cbEventList) {
76
- if (i.event == static_cast < arduino_event_id_t >(id) || i.event == ARDUINO_EVENT_ANY) {
77
- if (i. cb ) {
78
- i. cb ( static_cast < arduino_event_id_t >(id));
79
- continue ;
80
- }
81
-
82
- if (i. fcb ) {
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), {} );
87
- continue ;
88
- }
89
-
90
- if (i. scb ){
91
- // system event callback needs a ptr to struct
92
- arduino_event_t event{ static_cast < arduino_event_id_t >(id), {}};
93
- if ( info)
94
- memcpy (&event. event_info , info, sizeof ( arduino_event_info_t ) );
95
- i. scb (&event);
96
- }
79
+ if (i.event == ARDUINO_EVENT_ANY || i.event == static_cast < arduino_event_id_t >(id)) {
80
+
81
+ std::visit ([id, info]( auto && arg){
82
+ using T = std:: decay_t < decltype (arg)> ;
83
+ if constexpr (std::is_same_v<T, NetworkEventReceiver>)
84
+ arg ( static_cast < arduino_event_id_t >(id), info);
85
+ else if constexpr (std::is_same_v<T, NetworkEventCb>)
86
+ arg ( static_cast < arduino_event_id_t >(id));
87
+ else if constexpr (std::is_same_v<T, NetworkEventFuncCb>)
88
+ if (info)
89
+ arg (static_cast <arduino_event_id_t >(id), *info );
90
+ else
91
+ arg ( static_cast < arduino_event_id_t >(id), {});
92
+ else if constexpr (std::is_same_v<T, NetworkEventSysCb>){
93
+ // system event callback needs a ptr to struct
94
+ arduino_event_t event{ static_cast < arduino_event_id_t >(id), {}};
95
+ if (info)
96
+ memcpy (&event. event_info , info, sizeof ( arduino_event_info_t ));
97
+ arg (&event);
98
+ }
99
+ }, i. cb_v );
97
100
}
98
101
}
99
102
}
@@ -116,7 +119,7 @@ network_event_handle_t NetworkEvents::onEvent(NetworkEventCb cbEvent, arduino_ev
116
119
std::lock_guard<std::mutex> lock (_mtx);
117
120
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
118
121
119
- _cbEventList.emplace_back (++_current_id, cbEvent, nullptr , nullptr , event);
122
+ _cbEventList.emplace_back (++_current_id, cbEvent, event);
120
123
return _cbEventList.back ().id ;
121
124
}
122
125
@@ -129,7 +132,7 @@ network_event_handle_t NetworkEvents::onEvent(NetworkEventFuncCb cbEvent, arduin
129
132
std::lock_guard<std::mutex> lock (_mtx);
130
133
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
131
134
132
- _cbEventList.emplace_back (++_current_id, nullptr , cbEvent, nullptr , event);
135
+ _cbEventList.emplace_back (++_current_id, cbEvent, event);
133
136
return _cbEventList.back ().id ;
134
137
}
135
138
@@ -142,7 +145,7 @@ network_event_handle_t NetworkEvents::onEvent(NetworkEventSysCb cbEvent, arduino
142
145
std::lock_guard<std::mutex> lock (_mtx);
143
146
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
144
147
145
- _cbEventList.emplace_back (++_current_id, nullptr , nullptr , cbEvent, event);
148
+ _cbEventList.emplace_back (++_current_id, cbEvent, event);
146
149
return _cbEventList.back ().id ;
147
150
}
148
151
@@ -155,7 +158,7 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventCb cbEvent, arduino
155
158
std::lock_guard<std::mutex> lock (_mtx);
156
159
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
157
160
158
- _cbEventList.emplace (_cbEventList.begin (), ++_current_id, cbEvent, nullptr , nullptr , event);
161
+ _cbEventList.emplace (_cbEventList.begin (), ++_current_id, cbEvent, event);
159
162
return _cbEventList.front ().id ;
160
163
}
161
164
@@ -168,7 +171,7 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventFuncCb cbEvent, ard
168
171
std::lock_guard<std::mutex> lock (_mtx);
169
172
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
170
173
171
- _cbEventList.emplace (_cbEventList.begin (), ++_current_id, nullptr , cbEvent, nullptr , event);
174
+ _cbEventList.emplace (_cbEventList.begin (), ++_current_id, cbEvent, event);
172
175
return _cbEventList.front ().id ;
173
176
}
174
177
@@ -181,7 +184,20 @@ network_event_handle_t NetworkEvents::onSysEvent(NetworkEventSysCb cbEvent, ardu
181
184
std::lock_guard<std::mutex> lock (_mtx);
182
185
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
183
186
184
- _cbEventList.emplace (_cbEventList.begin (), ++_current_id, nullptr , nullptr , cbEvent, event);
187
+ _cbEventList.emplace (_cbEventList.begin (), ++_current_id, cbEvent, event);
188
+ return _cbEventList.front ().id ;
189
+ }
190
+
191
+ network_event_handle_t NetworkEvents::onSysEvent (NetworkEventReceiver cbEvent, arduino_event_id_t event){
192
+ if (!cbEvent) {
193
+ return 0 ;
194
+ }
195
+
196
+ #if defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
197
+ std::lock_guard<std::mutex> lock (_mtx);
198
+ #endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
199
+
200
+ _cbEventList.emplace (_cbEventList.begin (), ++_current_id, cbEvent, event);
185
201
return _cbEventList.front ().id ;
186
202
}
187
203
@@ -198,7 +214,11 @@ void NetworkEvents::removeEvent(NetworkEventCb cbEvent, arduino_event_id_t event
198
214
std::remove_if (
199
215
_cbEventList.begin (), _cbEventList.end (),
200
216
[cbEvent, event](const NetworkEventCbList_t &e) {
201
- return e.cb == cbEvent && e.event == event;
217
+ return e.event == event && std::visit ([cbEvent](auto && arg) -> bool {
218
+ if constexpr (std::is_same_v<NetworkEventCb, std::decay_t <decltype (arg)>>)
219
+ return cbEvent == arg;
220
+ else return false ;
221
+ }, e.cb_v );
202
222
}
203
223
),
204
224
_cbEventList.end ()
@@ -213,12 +233,15 @@ void NetworkEvents::removeEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t e
213
233
#if defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
214
234
std::lock_guard<std::mutex> lock (_mtx);
215
235
#endif // defined NETWORK_EVENTS_MUTEX && SOC_CPU_CORES_NUM > 1
216
-
217
236
_cbEventList.erase (
218
237
std::remove_if (
219
238
_cbEventList.begin (), _cbEventList.end (),
220
239
[cbEvent, event](const NetworkEventCbList_t &e) {
221
- return getStdFunctionAddress (e.fcb ) == getStdFunctionAddress (cbEvent) && e.event == event;
240
+ return e.event == event && std::visit ([cbEvent](auto && arg) -> bool {
241
+ if constexpr (std::is_same_v<NetworkEventFuncCb, std::decay_t <decltype (arg)>>)
242
+ return getStdFunctionAddress (cbEvent) == getStdFunctionAddress (arg);
243
+ else return false ;
244
+ }, e.cb_v );
222
245
}
223
246
),
224
247
_cbEventList.end ()
@@ -238,7 +261,11 @@ void NetworkEvents::removeEvent(NetworkEventSysCb cbEvent, arduino_event_id_t ev
238
261
std::remove_if (
239
262
_cbEventList.begin (), _cbEventList.end (),
240
263
[cbEvent, event](const NetworkEventCbList_t &e) {
241
- return e.scb == cbEvent && e.event == event;
264
+ return e.event == event && std::visit ([cbEvent](auto && arg) -> bool {
265
+ if constexpr (std::is_same_v<NetworkEventSysCb, std::decay_t <decltype (arg)>>)
266
+ return cbEvent == arg;
267
+ else return false ;
268
+ }, e.cb_v );
242
269
}
243
270
),
244
271
_cbEventList.end ()
0 commit comments