Skip to content

Commit 6bf7619

Browse files
timpurme-no-dev
authored andcommitted
std::functioanl for WFIF event + Minor fix (#1366)
* add missing bits from esp8266 to help porting other libs * Clean Up of Wifi event * Exampl of Wifi Events
1 parent 7bf1f47 commit 6bf7619

File tree

5 files changed

+92
-37
lines changed

5 files changed

+92
-37
lines changed

Diff for: cores/esp32/pgmspace.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ typedef unsigned long prog_uint32_t;
7171
#define memcpy_P memcpy
7272
#define strcpy_P strcpy
7373
#define strncpy_P strncpy
74-
#define strcat_p strcat
74+
#define strcat_P strcat
7575
#define strncat_P strncat
7676
#define strcmp_P strcmp
7777
#define strncmp_P strncmp

Diff for: libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
/*
22
* This sketch shows the WiFi event usage
33
*
4-
*/
4+
*/
5+
6+
/*
7+
* WiFi Events
8+
9+
SYSTEM_EVENT_WIFI_READY < ESP32 WiFi ready
10+
SYSTEM_EVENT_SCAN_DONE < ESP32 finish scanning AP
11+
SYSTEM_EVENT_STA_START < ESP32 station start
12+
SYSTEM_EVENT_STA_STOP < ESP32 station stop
13+
SYSTEM_EVENT_STA_CONNECTED < ESP32 station connected to AP
14+
SYSTEM_EVENT_STA_DISCONNECTED < ESP32 station disconnected from AP
15+
SYSTEM_EVENT_STA_AUTHMODE_CHANGE < the auth mode of AP connected by ESP32 station changed
16+
SYSTEM_EVENT_STA_GOT_IP < ESP32 station got IP from connected AP
17+
SYSTEM_EVENT_STA_LOST_IP < ESP32 station lost IP and the IP is reset to 0
18+
SYSTEM_EVENT_STA_WPS_ER_SUCCESS < ESP32 station wps succeeds in enrollee mode
19+
SYSTEM_EVENT_STA_WPS_ER_FAILED < ESP32 station wps fails in enrollee mode
20+
SYSTEM_EVENT_STA_WPS_ER_TIMEOUT < ESP32 station wps timeout in enrollee mode
21+
SYSTEM_EVENT_STA_WPS_ER_PIN < ESP32 station wps pin code in enrollee mode
22+
SYSTEM_EVENT_AP_START < ESP32 soft-AP start
23+
SYSTEM_EVENT_AP_STOP < ESP32 soft-AP stop
24+
SYSTEM_EVENT_AP_STACONNECTED < a station connected to ESP32 soft-AP
25+
SYSTEM_EVENT_AP_STADISCONNECTED < a station disconnected from ESP32 soft-AP
26+
SYSTEM_EVENT_AP_PROBEREQRECVED < Receive probe request packet in soft-AP interface
27+
SYSTEM_EVENT_GOT_IP6 < ESP32 station or ap or ethernet interface v6IP addr is preferred
28+
SYSTEM_EVENT_ETH_START < ESP32 ethernet start
29+
SYSTEM_EVENT_ETH_STOP < ESP32 ethernet stop
30+
SYSTEM_EVENT_ETH_CONNECTED < ESP32 ethernet phy link up
31+
SYSTEM_EVENT_ETH_DISCONNECTED < ESP32 ethernet phy link down
32+
SYSTEM_EVENT_ETH_GOT_IP < ESP32 ethernet got IP from connected AP
33+
SYSTEM_EVENT_MAX
34+
*/
535

636
#include <WiFi.h>
737

@@ -13,7 +43,8 @@ void WiFiEvent(WiFiEvent_t event)
1343
{
1444
Serial.printf("[WiFi-event] event: %d\n", event);
1545

16-
switch(event) {
46+
switch (event)
47+
{
1748
case SYSTEM_EVENT_STA_GOT_IP:
1849
Serial.println("WiFi connected");
1950
Serial.println("IP address: ");
@@ -25,6 +56,13 @@ void WiFiEvent(WiFiEvent_t event)
2556
}
2657
}
2758

59+
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
60+
{
61+
Serial.println("WiFi connected");
62+
Serial.println("IP address: ");
63+
Serial.println(IPAddress(info.got_ip.ip_info.ip.addr));
64+
}
65+
2866
void setup()
2967
{
3068
Serial.begin(115200);
@@ -34,7 +72,18 @@ void setup()
3472

3573
delay(1000);
3674

75+
// Examples of diffrent ways to register wifi events
3776
WiFi.onEvent(WiFiEvent);
77+
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
78+
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
79+
Serial.print("WiFi lost connection. Reason: ");
80+
Serial.println(info.disconnected.reason);
81+
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
82+
83+
// Remove WiFi event
84+
Serial.print("WiFi Event ID: ");
85+
Serial.println(eventID);
86+
// WiFi.removeEvent(eventID);
3887

3988
WiFi.begin(ssid, password);
4089

@@ -43,9 +92,7 @@ void setup()
4392
Serial.println("Wait for WiFi... ");
4493
}
4594

46-
4795
void loop()
4896
{
4997
delay(1000);
5098
}
51-

Diff for: libraries/WiFi/src/WiFiGeneric.cpp

+23-18
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,18 @@ static bool espWiFiStop(){
170170
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
171171
// -----------------------------------------------------------------------------------------------------------------------
172172

173-
typedef struct {
173+
typedef struct WiFiEventCbList {
174+
static wifi_event_id_t current_id;
175+
wifi_event_id_t id;
174176
WiFiEventCb cb;
175-
WiFiEventFullCb fcb;
177+
WiFiEventFuncCb fcb;
176178
WiFiEventSysCb scb;
177179
system_event_id_t event;
180+
181+
WiFiEventCbList() : id(current_id++) {}
178182
} WiFiEventCbList_t;
183+
wifi_event_id_t WiFiEventCbList::current_id = 1;
184+
179185

180186
// arduino dont like std::vectors move static here
181187
static std::vector<WiFiEventCbList_t> cbEventList;
@@ -193,43 +199,46 @@ WiFiGenericClass::WiFiGenericClass()
193199
* @param cbEvent WiFiEventCb
194200
* @param event optional filter (WIFI_EVENT_MAX is all events)
195201
*/
196-
void WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
202+
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
197203
{
198204
if(!cbEvent) {
199-
return;
205+
return 0;
200206
}
201207
WiFiEventCbList_t newEventHandler;
202208
newEventHandler.cb = cbEvent;
203209
newEventHandler.fcb = NULL;
204210
newEventHandler.scb = NULL;
205211
newEventHandler.event = event;
206212
cbEventList.push_back(newEventHandler);
213+
return newEventHandler.id;
207214
}
208215

209-
void WiFiGenericClass::onEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
216+
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event)
210217
{
211218
if(!cbEvent) {
212-
return;
219+
return 0;
213220
}
214221
WiFiEventCbList_t newEventHandler;
215222
newEventHandler.cb = NULL;
216223
newEventHandler.fcb = cbEvent;
217224
newEventHandler.scb = NULL;
218225
newEventHandler.event = event;
219226
cbEventList.push_back(newEventHandler);
227+
return newEventHandler.id;
220228
}
221229

222-
void WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
230+
wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
223231
{
224232
if(!cbEvent) {
225-
return;
233+
return 0;
226234
}
227235
WiFiEventCbList_t newEventHandler;
228236
newEventHandler.cb = NULL;
229237
newEventHandler.fcb = NULL;
230238
newEventHandler.scb = cbEvent;
231239
newEventHandler.event = event;
232240
cbEventList.push_back(newEventHandler);
241+
return newEventHandler.id;
233242
}
234243

235244
/**
@@ -251,29 +260,25 @@ void WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, system_event_id_t event)
251260
}
252261
}
253262

254-
void WiFiGenericClass::removeEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
263+
void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
255264
{
256265
if(!cbEvent) {
257266
return;
258267
}
259268

260269
for(uint32_t i = 0; i < cbEventList.size(); i++) {
261270
WiFiEventCbList_t entry = cbEventList[i];
262-
if(entry.fcb == cbEvent && entry.event == event) {
271+
if(entry.scb == cbEvent && entry.event == event) {
263272
cbEventList.erase(cbEventList.begin() + i);
264273
}
265274
}
266275
}
267276

268-
void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
277+
void WiFiGenericClass::removeEvent(wifi_event_id_t id)
269278
{
270-
if(!cbEvent) {
271-
return;
272-
}
273-
274279
for(uint32_t i = 0; i < cbEventList.size(); i++) {
275280
WiFiEventCbList_t entry = cbEventList[i];
276-
if(entry.scb == cbEvent && entry.event == event) {
281+
if(entry.id == id) {
277282
cbEventList.erase(cbEventList.begin() + i);
278283
}
279284
}
@@ -329,9 +334,9 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
329334
WiFiEventCbList_t entry = cbEventList[i];
330335
if(entry.cb || entry.fcb || entry.scb) {
331336
if(entry.event == (system_event_id_t) event->event_id || entry.event == SYSTEM_EVENT_MAX) {
332-
if(entry.cb){
337+
if(entry.cb) {
333338
entry.cb((system_event_id_t) event->event_id);
334-
} else if(entry.fcb){
339+
} else if(entry.fcb) {
335340
entry.fcb((system_event_id_t) event->event_id, (system_event_info_t) event->event_info);
336341
} else {
337342
entry.scb(event);

Diff for: libraries/WiFi/src/WiFiGeneric.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@
2323
#ifndef ESP32WIFIGENERIC_H_
2424
#define ESP32WIFIGENERIC_H_
2525

26-
#include "WiFiType.h"
2726
#include <esp_err.h>
2827
#include <esp_event_loop.h>
28+
#include <functional>
29+
#include "WiFiType.h"
2930

3031
typedef void (*WiFiEventCb)(system_event_id_t event);
31-
typedef void (*WiFiEventFullCb)(system_event_id_t event, system_event_info_t info);
32+
typedef std::function<void(system_event_id_t event, system_event_info_t info)> WiFiEventFuncCb;
3233
typedef void (*WiFiEventSysCb)(system_event_t *event);
3334

35+
typedef size_t wifi_event_id_t;
36+
3437
class WiFiGenericClass
3538
{
36-
public:
37-
39+
public:
3840
WiFiGenericClass();
3941

40-
void onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
41-
void onEvent(WiFiEventFullCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
42-
void onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
42+
wifi_event_id_t onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
43+
wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
44+
wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
4345
void removeEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
44-
void removeEvent(WiFiEventFullCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
4546
void removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
47+
void removeEvent(wifi_event_id_t id);
4648

4749
int32_t channel(void);
4850

@@ -56,16 +58,14 @@ class WiFiGenericClass
5658

5759
static esp_err_t _eventCallback(void *arg, system_event_t *event);
5860

59-
protected:
61+
protected:
6062
static bool _persistent;
6163
static wifi_mode_t _forceSleepLastMode;
6264

63-
public:
64-
65-
int hostByName(const char* aHostname, IPAddress& aResult);
66-
67-
protected:
65+
public:
66+
int hostByName(const char *aHostname, IPAddress &aResult);
6867

68+
protected:
6969
friend class WiFiSTAClass;
7070
friend class WiFiScanClass;
7171
friend class WiFiAPClass;

Diff for: libraries/WiFi/src/WiFiType.h

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define WIFI_AP_STA WIFI_MODE_APSTA
3434

3535
#define WiFiEvent_t system_event_id_t
36+
#define WiFiEventInfo_t system_event_info_t
37+
#define WiFiEventId_t wifi_event_id_t
38+
3639

3740
typedef enum {
3841
WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library

0 commit comments

Comments
 (0)