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
This commit is contained in:
Tim P 2018-05-14 21:05:49 +10:00 committed by Me No Dev
parent 7bf1f47c97
commit 6bf7619ccc
5 changed files with 98 additions and 43 deletions

View File

@ -71,7 +71,7 @@ typedef unsigned long prog_uint32_t;
#define memcpy_P memcpy #define memcpy_P memcpy
#define strcpy_P strcpy #define strcpy_P strcpy
#define strncpy_P strncpy #define strncpy_P strncpy
#define strcat_p strcat #define strcat_P strcat
#define strncat_P strncat #define strncat_P strncat
#define strcmp_P strcmp #define strcmp_P strcmp
#define strncmp_P strncmp #define strncmp_P strncmp

View File

@ -1,7 +1,37 @@
/* /*
* This sketch shows the WiFi event usage * This sketch shows the WiFi event usage
* *
*/ */
/*
* WiFi Events
SYSTEM_EVENT_WIFI_READY < ESP32 WiFi ready
SYSTEM_EVENT_SCAN_DONE < ESP32 finish scanning AP
SYSTEM_EVENT_STA_START < ESP32 station start
SYSTEM_EVENT_STA_STOP < ESP32 station stop
SYSTEM_EVENT_STA_CONNECTED < ESP32 station connected to AP
SYSTEM_EVENT_STA_DISCONNECTED < ESP32 station disconnected from AP
SYSTEM_EVENT_STA_AUTHMODE_CHANGE < the auth mode of AP connected by ESP32 station changed
SYSTEM_EVENT_STA_GOT_IP < ESP32 station got IP from connected AP
SYSTEM_EVENT_STA_LOST_IP < ESP32 station lost IP and the IP is reset to 0
SYSTEM_EVENT_STA_WPS_ER_SUCCESS < ESP32 station wps succeeds in enrollee mode
SYSTEM_EVENT_STA_WPS_ER_FAILED < ESP32 station wps fails in enrollee mode
SYSTEM_EVENT_STA_WPS_ER_TIMEOUT < ESP32 station wps timeout in enrollee mode
SYSTEM_EVENT_STA_WPS_ER_PIN < ESP32 station wps pin code in enrollee mode
SYSTEM_EVENT_AP_START < ESP32 soft-AP start
SYSTEM_EVENT_AP_STOP < ESP32 soft-AP stop
SYSTEM_EVENT_AP_STACONNECTED < a station connected to ESP32 soft-AP
SYSTEM_EVENT_AP_STADISCONNECTED < a station disconnected from ESP32 soft-AP
SYSTEM_EVENT_AP_PROBEREQRECVED < Receive probe request packet in soft-AP interface
SYSTEM_EVENT_GOT_IP6 < ESP32 station or ap or ethernet interface v6IP addr is preferred
SYSTEM_EVENT_ETH_START < ESP32 ethernet start
SYSTEM_EVENT_ETH_STOP < ESP32 ethernet stop
SYSTEM_EVENT_ETH_CONNECTED < ESP32 ethernet phy link up
SYSTEM_EVENT_ETH_DISCONNECTED < ESP32 ethernet phy link down
SYSTEM_EVENT_ETH_GOT_IP < ESP32 ethernet got IP from connected AP
SYSTEM_EVENT_MAX
*/
#include <WiFi.h> #include <WiFi.h>
@ -13,7 +43,8 @@ void WiFiEvent(WiFiEvent_t event)
{ {
Serial.printf("[WiFi-event] event: %d\n", event); Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) { switch (event)
{
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected"); Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println("IP address: ");
@ -25,6 +56,13 @@ void WiFiEvent(WiFiEvent_t event)
} }
} }
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
{
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(IPAddress(info.got_ip.ip_info.ip.addr));
}
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -34,7 +72,18 @@ void setup()
delay(1000); delay(1000);
// Examples of diffrent ways to register wifi events
WiFi.onEvent(WiFiEvent); WiFi.onEvent(WiFiEvent);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.disconnected.reason);
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
// Remove WiFi event
Serial.print("WiFi Event ID: ");
Serial.println(eventID);
// WiFi.removeEvent(eventID);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@ -43,9 +92,7 @@ void setup()
Serial.println("Wait for WiFi... "); Serial.println("Wait for WiFi... ");
} }
void loop() void loop()
{ {
delay(1000); delay(1000);
} }

View File

@ -170,12 +170,18 @@ static bool espWiFiStop(){
// ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ------------------------------------------------- Generic WiFi function -----------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------------
typedef struct { typedef struct WiFiEventCbList {
static wifi_event_id_t current_id;
wifi_event_id_t id;
WiFiEventCb cb; WiFiEventCb cb;
WiFiEventFullCb fcb; WiFiEventFuncCb fcb;
WiFiEventSysCb scb; WiFiEventSysCb scb;
system_event_id_t event; system_event_id_t event;
WiFiEventCbList() : id(current_id++) {}
} WiFiEventCbList_t; } WiFiEventCbList_t;
wifi_event_id_t WiFiEventCbList::current_id = 1;
// arduino dont like std::vectors move static here // arduino dont like std::vectors move static here
static std::vector<WiFiEventCbList_t> cbEventList; static std::vector<WiFiEventCbList_t> cbEventList;
@ -193,10 +199,10 @@ WiFiGenericClass::WiFiGenericClass()
* @param cbEvent WiFiEventCb * @param cbEvent WiFiEventCb
* @param event optional filter (WIFI_EVENT_MAX is all events) * @param event optional filter (WIFI_EVENT_MAX is all events)
*/ */
void WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event) wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
{ {
if(!cbEvent) { if(!cbEvent) {
return; return 0;
} }
WiFiEventCbList_t newEventHandler; WiFiEventCbList_t newEventHandler;
newEventHandler.cb = cbEvent; newEventHandler.cb = cbEvent;
@ -204,12 +210,13 @@ void WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event)
newEventHandler.scb = NULL; newEventHandler.scb = NULL;
newEventHandler.event = event; newEventHandler.event = event;
cbEventList.push_back(newEventHandler); cbEventList.push_back(newEventHandler);
return newEventHandler.id;
} }
void WiFiGenericClass::onEvent(WiFiEventFullCb cbEvent, system_event_id_t event) wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event)
{ {
if(!cbEvent) { if(!cbEvent) {
return; return 0;
} }
WiFiEventCbList_t newEventHandler; WiFiEventCbList_t newEventHandler;
newEventHandler.cb = NULL; newEventHandler.cb = NULL;
@ -217,12 +224,13 @@ void WiFiGenericClass::onEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
newEventHandler.scb = NULL; newEventHandler.scb = NULL;
newEventHandler.event = event; newEventHandler.event = event;
cbEventList.push_back(newEventHandler); cbEventList.push_back(newEventHandler);
return newEventHandler.id;
} }
void WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event) wifi_event_id_t WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
{ {
if(!cbEvent) { if(!cbEvent) {
return; return 0;
} }
WiFiEventCbList_t newEventHandler; WiFiEventCbList_t newEventHandler;
newEventHandler.cb = NULL; newEventHandler.cb = NULL;
@ -230,6 +238,7 @@ void WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
newEventHandler.scb = cbEvent; newEventHandler.scb = cbEvent;
newEventHandler.event = event; newEventHandler.event = event;
cbEventList.push_back(newEventHandler); cbEventList.push_back(newEventHandler);
return newEventHandler.id;
} }
/** /**
@ -251,20 +260,6 @@ void WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, system_event_id_t event)
} }
} }
void WiFiGenericClass::removeEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
{
if(!cbEvent) {
return;
}
for(uint32_t i = 0; i < cbEventList.size(); i++) {
WiFiEventCbList_t entry = cbEventList[i];
if(entry.fcb == cbEvent && entry.event == event) {
cbEventList.erase(cbEventList.begin() + i);
}
}
}
void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event) void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
{ {
if(!cbEvent) { if(!cbEvent) {
@ -279,6 +274,16 @@ void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t eve
} }
} }
void WiFiGenericClass::removeEvent(wifi_event_id_t id)
{
for(uint32_t i = 0; i < cbEventList.size(); i++) {
WiFiEventCbList_t entry = cbEventList[i];
if(entry.id == id) {
cbEventList.erase(cbEventList.begin() + i);
}
}
}
/** /**
* callback for WiFi events * callback for WiFi events
* @param arg * @param arg
@ -329,9 +334,9 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
WiFiEventCbList_t entry = cbEventList[i]; WiFiEventCbList_t entry = cbEventList[i];
if(entry.cb || entry.fcb || entry.scb) { if(entry.cb || entry.fcb || entry.scb) {
if(entry.event == (system_event_id_t) event->event_id || entry.event == SYSTEM_EVENT_MAX) { if(entry.event == (system_event_id_t) event->event_id || entry.event == SYSTEM_EVENT_MAX) {
if(entry.cb){ if(entry.cb) {
entry.cb((system_event_id_t) event->event_id); entry.cb((system_event_id_t) event->event_id);
} else if(entry.fcb){ } else if(entry.fcb) {
entry.fcb((system_event_id_t) event->event_id, (system_event_info_t) event->event_info); entry.fcb((system_event_id_t) event->event_id, (system_event_info_t) event->event_info);
} else { } else {
entry.scb(event); entry.scb(event);

View File

@ -23,26 +23,28 @@
#ifndef ESP32WIFIGENERIC_H_ #ifndef ESP32WIFIGENERIC_H_
#define ESP32WIFIGENERIC_H_ #define ESP32WIFIGENERIC_H_
#include "WiFiType.h"
#include <esp_err.h> #include <esp_err.h>
#include <esp_event_loop.h> #include <esp_event_loop.h>
#include <functional>
#include "WiFiType.h"
typedef void (*WiFiEventCb)(system_event_id_t event); typedef void (*WiFiEventCb)(system_event_id_t event);
typedef void (*WiFiEventFullCb)(system_event_id_t event, system_event_info_t info); typedef std::function<void(system_event_id_t event, system_event_info_t info)> WiFiEventFuncCb;
typedef void (*WiFiEventSysCb)(system_event_t *event); typedef void (*WiFiEventSysCb)(system_event_t *event);
typedef size_t wifi_event_id_t;
class WiFiGenericClass class WiFiGenericClass
{ {
public: public:
WiFiGenericClass(); WiFiGenericClass();
void onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); wifi_event_id_t onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
void onEvent(WiFiEventFullCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
void onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
void removeEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); void removeEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
void removeEvent(WiFiEventFullCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
void removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); void removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
void removeEvent(wifi_event_id_t id);
int32_t channel(void); int32_t channel(void);
@ -56,16 +58,14 @@ public:
static esp_err_t _eventCallback(void *arg, system_event_t *event); static esp_err_t _eventCallback(void *arg, system_event_t *event);
protected: protected:
static bool _persistent; static bool _persistent;
static wifi_mode_t _forceSleepLastMode; static wifi_mode_t _forceSleepLastMode;
public: public:
int hostByName(const char *aHostname, IPAddress &aResult);
int hostByName(const char* aHostname, IPAddress& aResult);
protected:
protected:
friend class WiFiSTAClass; friend class WiFiSTAClass;
friend class WiFiScanClass; friend class WiFiScanClass;
friend class WiFiAPClass; friend class WiFiAPClass;

View File

@ -33,6 +33,9 @@
#define WIFI_AP_STA WIFI_MODE_APSTA #define WIFI_AP_STA WIFI_MODE_APSTA
#define WiFiEvent_t system_event_id_t #define WiFiEvent_t system_event_id_t
#define WiFiEventInfo_t system_event_info_t
#define WiFiEventId_t wifi_event_id_t
typedef enum { typedef enum {
WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library