Skip to content

std::functioanl for WFIF event + Minor fix #1366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cores/esp32/pgmspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 51 additions & 4 deletions libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino
Original file line number Diff line number Diff line change
@@ -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 <WiFi.h>

Expand All @@ -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: ");
Expand All @@ -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);
Expand All @@ -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);

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


void loop()
{
delay(1000);
}

41 changes: 23 additions & 18 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,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<WiFiEventCbList_t> cbEventList;
Expand All @@ -191,43 +197,46 @@ 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;
newEventHandler.fcb = NULL;
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;
newEventHandler.fcb = cbEvent;
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;
newEventHandler.fcb = NULL;
newEventHandler.scb = cbEvent;
newEventHandler.event = event;
cbEventList.push_back(newEventHandler);
return newEventHandler.id;
}

/**
Expand All @@ -249,29 +258,25 @@ void WiFiGenericClass::removeEvent(WiFiEventCb cbEvent, system_event_id_t event)
}
}

void WiFiGenericClass::removeEvent(WiFiEventFullCb cbEvent, system_event_id_t event)
void WiFiGenericClass::removeEvent(WiFiEventSysCb 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) {
if(entry.scb == cbEvent && entry.event == event) {
cbEventList.erase(cbEventList.begin() + i);
}
}
}

void WiFiGenericClass::removeEvent(WiFiEventSysCb cbEvent, system_event_id_t event)
void WiFiGenericClass::removeEvent(wifi_event_id_t id)
{
if(!cbEvent) {
return;
}

for(uint32_t i = 0; i < cbEventList.size(); i++) {
WiFiEventCbList_t entry = cbEventList[i];
if(entry.scb == cbEvent && entry.event == event) {
if(entry.id == id) {
cbEventList.erase(cbEventList.begin() + i);
}
}
Expand Down Expand Up @@ -327,9 +332,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);
Expand Down
28 changes: 14 additions & 14 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@
#ifndef ESP32WIFIGENERIC_H_
#define ESP32WIFIGENERIC_H_

#include "WiFiType.h"
#include <esp_err.h>
#include <esp_event_loop.h>
#include <functional>
#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<void(system_event_id_t event, system_event_info_t info)> 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);

Expand All @@ -56,16 +58,14 @@ class WiFiGenericClass

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;
Expand Down
3 changes: 3 additions & 0 deletions libraries/WiFi/src/WiFiType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down