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