@@ -43,15 +43,21 @@ extern "C" {
43
43
44
44
#include " debug.h"
45
45
46
+ #undef min
47
+ #undef max
48
+ #include < vector>
49
+
46
50
extern " C" void esp_schedule ();
47
51
extern " C" void esp_yield ();
48
52
49
53
// -----------------------------------------------------------------------------------------------------------------------
50
54
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
51
55
// -----------------------------------------------------------------------------------------------------------------------
52
56
57
+ // arduino dont like std::vectors move static here
58
+ static std::vector<WiFiEventCbList_t> cbEventList;
59
+
53
60
bool ESP8266WiFiGenericClass::_persistent = true ;
54
- WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL ;
55
61
WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF;
56
62
57
63
ESP8266WiFiGenericClass::ESP8266WiFiGenericClass () {
@@ -61,9 +67,34 @@ ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
61
67
/* *
62
68
* set callback function
63
69
* @param cbEvent WiFiEventCb
70
+ * @param event optional filter (WIFI_EVENT_MAX is all events)
64
71
*/
65
- void ESP8266WiFiGenericClass::onEvent (WiFiEventCb cbEvent) {
66
- _cbEvent = cbEvent;
72
+ void ESP8266WiFiGenericClass::onEvent (WiFiEventCb cbEvent, WiFiEvent_t event) {
73
+ if (!cbEvent) {
74
+ return ;
75
+ }
76
+ WiFiEventCbList_t newEventHandler;
77
+ newEventHandler.cb = cbEvent;
78
+ newEventHandler.event = event;
79
+ cbEventList.push_back (newEventHandler);
80
+ }
81
+
82
+ /* *
83
+ * removes a callback form event handler
84
+ * @param cbEvent WiFiEventCb
85
+ * @param event optional filter (WIFI_EVENT_MAX is all events)
86
+ */
87
+ void ESP8266WiFiGenericClass::removeEvent (WiFiEventCb cbEvent, WiFiEvent_t event) {
88
+ if (!cbEvent) {
89
+ return ;
90
+ }
91
+
92
+ for (uint32_t i = 0 ; i < cbEventList.size (); i++) {
93
+ WiFiEventCbList_t entry = cbEventList[i];
94
+ if (entry.cb == cbEvent && entry.event == event) {
95
+ cbEventList.erase (cbEventList.begin () + i);
96
+ }
97
+ }
67
98
}
68
99
69
100
/* *
@@ -78,8 +109,13 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) {
78
109
WiFiClient::stopAll ();
79
110
}
80
111
81
- if (_cbEvent) {
82
- _cbEvent ((WiFiEvent_t) event->event );
112
+ for (uint32_t i = 0 ; i < cbEventList.size (); i++) {
113
+ WiFiEventCbList_t entry = cbEventList[i];
114
+ if (entry.cb ) {
115
+ if (entry.event == (WiFiEvent_t) event->event || entry.event == WIFI_EVENT_MAX) {
116
+ entry.cb ((WiFiEvent_t) event->event );
117
+ }
118
+ }
83
119
}
84
120
}
85
121
0 commit comments