Skip to content

Commit f8abfa6

Browse files
committed
WiFiGeneric make _eventCallback accept events by id and optional event_info_t ptr
1 parent 63a2641 commit f8abfa6

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

libraries/WiFi/src/WiFiGeneric.cpp

+40-39
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ esp_netif_t *get_esp_interface_netif(esp_interface_t interface) {
7575
}
7676

7777
static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
78-
arduino_event_t arduino_event;
79-
arduino_event.event_id = ARDUINO_EVENT_ANY;
80-
8178
if (event_base == WIFI_EVENT){
8279
switch (event_id){
8380
case WIFI_EVENT_STA_WPS_ER_SUCCESS :
@@ -94,22 +91,29 @@ static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t ev
9491
wifi_event_sta_scan_done_t *event = (wifi_event_sta_scan_done_t *)event_data;
9592
log_v("SCAN Done: ID: %u, Status: %u, Results: %u", event->scan_id, event->status, event->number);
9693
#endif
97-
arduino_event.event_id = ARDUINO_EVENT_WIFI_SCAN_DONE;
98-
memcpy(&arduino_event.event_info.wifi_scan_done, event_data, sizeof(wifi_event_sta_scan_done_t));
99-
break;
94+
arduino_event_info_t i;
95+
memcpy(&i.wifi_scan_done, event_data, sizeof(wifi_event_sta_scan_done_t));
96+
Network.postEvent(ARDUINO_EVENT_WIFI_SCAN_DONE, &i);
97+
return;
98+
}
99+
case WIFI_EVENT_STA_WPS_ER_FAILED : {
100+
arduino_event_info_t i;
101+
memcpy(&i.wps_fail_reason, event_data, sizeof(wifi_event_sta_wps_fail_reason_t));
102+
Network.postEvent(ARDUINO_EVENT_WPS_ER_FAILED, &i);
103+
return;
104+
}
105+
case WIFI_EVENT_STA_WPS_ER_PIN : {
106+
arduino_event_info_t i;
107+
memcpy(&i.wps_fail_reason, event_data, sizeof(wifi_event_sta_wps_er_pin_t));
108+
Network.postEvent(ARDUINO_EVENT_WPS_ER_PIN, &i);
109+
return;
110+
}
111+
case WIFI_EVENT_FTM_REPORT : {
112+
arduino_event_info_t i;
113+
memcpy(&i.wifi_ftm_report, event_data, sizeof(wifi_event_ftm_report_t));
114+
Network.postEvent(ARDUINO_EVENT_WIFI_FTM_REPORT, &i);
115+
return;
100116
}
101-
case WIFI_EVENT_STA_WPS_ER_FAILED :
102-
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_FAILED;
103-
memcpy(&arduino_event.event_info.wps_fail_reason, event_data, sizeof(wifi_event_sta_wps_fail_reason_t));
104-
break;
105-
case WIFI_EVENT_STA_WPS_ER_PIN :
106-
arduino_event.event_id = ARDUINO_EVENT_WPS_ER_PIN;
107-
memcpy(&arduino_event.event_info.wps_fail_reason, event_data, sizeof(wifi_event_sta_wps_er_pin_t));
108-
break;
109-
case WIFI_EVENT_FTM_REPORT :
110-
arduino_event.event_id = ARDUINO_EVENT_WIFI_FTM_REPORT;
111-
memcpy(&arduino_event.event_info.wifi_ftm_report, event_data, sizeof(wifi_event_ftm_report_t));
112-
break;
113117
default:
114118
return;
115119
}
@@ -131,9 +135,10 @@ static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t ev
131135
smartconfig_event_got_ssid_pswd_t *event = (smartconfig_event_got_ssid_pswd_t *)event_data;
132136
log_v("SC: SSID: %s, Password: %s", (const char *)event->ssid, (const char *)event->password);
133137
#endif
134-
arduino_event.event_id = ARDUINO_EVENT_SC_GOT_SSID_PSWD;
135-
memcpy(&arduino_event.event_info.sc_got_ssid_pswd, event_data, sizeof(smartconfig_event_got_ssid_pswd_t));
136-
break;
138+
arduino_event_info_t i;
139+
memcpy(&i.sc_got_ssid_pswd, event_data, sizeof(smartconfig_event_got_ssid_pswd_t));
140+
Network.postEvent(ARDUINO_EVENT_SC_GOT_SSID_PSWD, &i);
141+
return;
137142
}
138143
case SC_EVENT_SEND_ACK_DONE :
139144
log_v("SC Send Ack Done");
@@ -172,18 +177,20 @@ static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t ev
172177
wifi_sta_config_t *event = (wifi_sta_config_t *)event_data;
173178
log_v("Provisioned Credentials: SSID: %s, Password: %s", (const char *)event->ssid, (const char *)event->password);
174179
#endif
175-
arduino_event.event_id = ARDUINO_EVENT_PROV_CRED_RECV;
176-
memcpy(&arduino_event.event_info.prov_cred_recv, event_data, sizeof(wifi_sta_config_t));
177-
break;
180+
arduino_event_info_t i;
181+
memcpy(&i.prov_cred_recv, event_data, sizeof(wifi_sta_config_t));
182+
Network.postEvent(ARDUINO_EVENT_PROV_CRED_RECV, &i);
183+
return;
178184
}
179185
case NETWORK_PROV_WIFI_CRED_FAIL : {
180186
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
181187
network_prov_wifi_sta_fail_reason_t *reason = (network_prov_wifi_sta_fail_reason_t *)event_data;
182188
log_e("Provisioning Failed: Reason : %s", (*reason == NETWORK_PROV_WIFI_STA_AUTH_ERROR) ? "Authentication Failed" : "AP Not Found");
183189
#endif
184-
arduino_event.event_id = ARDUINO_EVENT_PROV_CRED_FAIL;
185-
memcpy(&arduino_event.event_info.prov_fail_reason, event_data, sizeof(network_prov_wifi_sta_fail_reason_t));
186-
break;
190+
arduino_event_info_t i;
191+
memcpy(&i.prov_fail_reason, event_data, sizeof(network_prov_wifi_sta_fail_reason_t));
192+
Network.postEvent(ARDUINO_EVENT_PROV_CRED_FAIL, &i);
193+
return;
187194
}
188195
case NETWORK_PROV_WIFI_CRED_SUCCESS :
189196
log_v("Provisioning Success!");
@@ -195,8 +202,6 @@ static void _arduino_event_cb(void *arg, esp_event_base_t event_base, int32_t ev
195202
}
196203
#endif // CONFIG_NETWORK_PROV_NETWORK_TYPE_WIFI
197204
#endif //!CONFIG_ESP_WIFI_REMOTE_ENABLED
198-
199-
Network.postEvent(&arduino_event);
200205
}
201206

202207
static bool initWiFiEvents() {
@@ -463,22 +468,18 @@ bool WiFiGenericClass::setHostname(const char *hostname) {
463468
* callback for WiFi events
464469
* @param arg
465470
*/
466-
void WiFiGenericClass::_eventCallback(arduino_event_t *event) {
467-
if (!event) {
468-
return; //Null would crash this function
469-
}
470-
471+
void WiFiGenericClass::_eventCallback(arduino_event_id_t event, const arduino_event_info_t *info) {
471472
// log_d("Arduino Event: %d - %s", event->event_id, WiFi.eventName(event->event_id));
472-
if (event->event_id == ARDUINO_EVENT_WIFI_SCAN_DONE) {
473+
if (event == ARDUINO_EVENT_WIFI_SCAN_DONE) {
473474
WiFiScanClass::_scanDone();
474475
}
475476
#if !CONFIG_ESP_WIFI_REMOTE_ENABLED
476-
else if (event->event_id == ARDUINO_EVENT_SC_GOT_SSID_PSWD) {
477+
else if (event == ARDUINO_EVENT_SC_GOT_SSID_PSWD && info) {
477478
WiFi.begin(
478-
(const char *)event->event_info.sc_got_ssid_pswd.ssid, (const char *)event->event_info.sc_got_ssid_pswd.password, 0,
479-
((event->event_info.sc_got_ssid_pswd.bssid_set == true) ? event->event_info.sc_got_ssid_pswd.bssid : NULL)
479+
(const char *)info->sc_got_ssid_pswd.ssid, (const char *)info->sc_got_ssid_pswd.password, 0,
480+
((info->sc_got_ssid_pswd.bssid_set == true) ? info->sc_got_ssid_pswd.bssid : NULL)
480481
);
481-
} else if (event->event_id == ARDUINO_EVENT_SC_SEND_ACK_DONE) {
482+
} else if (event == ARDUINO_EVENT_SC_SEND_ACK_DONE) {
482483
esp_smartconfig_stop();
483484
WiFiSTAClass::_smartConfigDone = true;
484485
}

libraries/WiFi/src/WiFiGeneric.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class WiFiGenericClass {
145145
static int setStatusBits(int bits);
146146
static int clearStatusBits(int bits);
147147

148-
static void _eventCallback(arduino_event_t *event);
148+
static void _eventCallback(arduino_event_id_t event, const arduino_event_info_t *info);
149149

150150
friend class WiFiSTAClass;
151151
friend class WiFiScanClass;

0 commit comments

Comments
 (0)