From 6bf7619cccc0c8ff8ee7dc53da4eda515382215d Mon Sep 17 00:00:00 2001 From: Tim P Date: Mon, 14 May 2018 21:05:49 +1000 Subject: [PATCH] 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 --- cores/esp32/pgmspace.h | 2 +- .../WiFiClientEvents/WiFiClientEvents.ino | 55 +++++++++++++++++-- libraries/WiFi/src/WiFiGeneric.cpp | 53 ++++++++++-------- libraries/WiFi/src/WiFiGeneric.h | 28 +++++----- libraries/WiFi/src/WiFiType.h | 3 + 5 files changed, 98 insertions(+), 43 deletions(-) diff --git a/cores/esp32/pgmspace.h b/cores/esp32/pgmspace.h index d1e81ffc..c2bee48c 100644 --- a/cores/esp32/pgmspace.h +++ b/cores/esp32/pgmspace.h @@ -71,7 +71,7 @@ typedef unsigned long prog_uint32_t; #define memcpy_P memcpy #define strcpy_P strcpy #define strncpy_P strncpy -#define strcat_p strcat +#define strcat_P strcat #define strncat_P strncat #define strcmp_P strcmp #define strncmp_P strncmp diff --git a/libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino b/libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino index eec5bee7..baccfb6e 100644 --- a/libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino +++ b/libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino @@ -1,7 +1,37 @@ /* * 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 @@ -13,7 +43,8 @@ void WiFiEvent(WiFiEvent_t event) { Serial.printf("[WiFi-event] event: %d\n", event); - switch(event) { + switch (event) + { case SYSTEM_EVENT_STA_GOT_IP: Serial.println("WiFi connected"); 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() { Serial.begin(115200); @@ -34,7 +72,18 @@ void setup() delay(1000); + // Examples of diffrent ways to register wifi events 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); @@ -43,9 +92,7 @@ void setup() Serial.println("Wait for WiFi... "); } - void loop() { delay(1000); } - diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index 82f8087c..a73de818 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -170,12 +170,18 @@ static bool espWiFiStop(){ // ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- -typedef struct { +typedef struct WiFiEventCbList { + static wifi_event_id_t current_id; + wifi_event_id_t id; WiFiEventCb cb; - WiFiEventFullCb fcb; + WiFiEventFuncCb fcb; WiFiEventSysCb scb; system_event_id_t event; + + WiFiEventCbList() : id(current_id++) {} } WiFiEventCbList_t; +wifi_event_id_t WiFiEventCbList::current_id = 1; + // arduino dont like std::vectors move static here static std::vector cbEventList; @@ -193,10 +199,10 @@ WiFiGenericClass::WiFiGenericClass() * @param cbEvent WiFiEventCb * @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) { - return; + return 0; } WiFiEventCbList_t newEventHandler; newEventHandler.cb = cbEvent; @@ -204,12 +210,13 @@ void WiFiGenericClass::onEvent(WiFiEventCb cbEvent, system_event_id_t event) newEventHandler.scb = NULL; newEventHandler.event = event; 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) { - return; + return 0; } WiFiEventCbList_t newEventHandler; newEventHandler.cb = NULL; @@ -217,12 +224,13 @@ void WiFiGenericClass::onEvent(WiFiEventFullCb cbEvent, system_event_id_t event) newEventHandler.scb = NULL; newEventHandler.event = event; 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) { - return; + return 0; } WiFiEventCbList_t newEventHandler; newEventHandler.cb = NULL; @@ -230,6 +238,7 @@ void WiFiGenericClass::onEvent(WiFiEventSysCb cbEvent, system_event_id_t event) newEventHandler.scb = cbEvent; newEventHandler.event = event; 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) { 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 * @param arg @@ -329,9 +334,9 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event) WiFiEventCbList_t entry = cbEventList[i]; if(entry.cb || entry.fcb || entry.scb) { 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); - } else if(entry.fcb){ + } else if(entry.fcb) { entry.fcb((system_event_id_t) event->event_id, (system_event_info_t) event->event_info); } else { entry.scb(event); diff --git a/libraries/WiFi/src/WiFiGeneric.h b/libraries/WiFi/src/WiFiGeneric.h index 83eb7f77..3bfbd21a 100644 --- a/libraries/WiFi/src/WiFiGeneric.h +++ b/libraries/WiFi/src/WiFiGeneric.h @@ -23,26 +23,28 @@ #ifndef ESP32WIFIGENERIC_H_ #define ESP32WIFIGENERIC_H_ -#include "WiFiType.h" #include #include +#include +#include "WiFiType.h" typedef void (*WiFiEventCb)(system_event_id_t event); -typedef void (*WiFiEventFullCb)(system_event_id_t event, system_event_info_t info); +typedef std::function WiFiEventFuncCb; typedef void (*WiFiEventSysCb)(system_event_t *event); +typedef size_t wifi_event_id_t; + class WiFiGenericClass { -public: - + public: WiFiGenericClass(); - void onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); - void onEvent(WiFiEventFullCb 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(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX); + wifi_event_id_t onEvent(WiFiEventFuncCb 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(WiFiEventFullCb 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); @@ -56,16 +58,14 @@ public: static esp_err_t _eventCallback(void *arg, system_event_t *event); -protected: + protected: static bool _persistent; static wifi_mode_t _forceSleepLastMode; -public: - - int hostByName(const char* aHostname, IPAddress& aResult); - -protected: + public: + int hostByName(const char *aHostname, IPAddress &aResult); + protected: friend class WiFiSTAClass; friend class WiFiScanClass; friend class WiFiAPClass; diff --git a/libraries/WiFi/src/WiFiType.h b/libraries/WiFi/src/WiFiType.h index 70d58ba2..cce29eea 100644 --- a/libraries/WiFi/src/WiFiType.h +++ b/libraries/WiFi/src/WiFiType.h @@ -33,6 +33,9 @@ #define WIFI_AP_STA WIFI_MODE_APSTA #define WiFiEvent_t system_event_id_t +#define WiFiEventInfo_t system_event_info_t +#define WiFiEventId_t wifi_event_id_t + typedef enum { WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library