Skip to content

Commit 373da3d

Browse files
committed
allow hook WiFi events from sketch
1 parent fd443d4 commit 373da3d

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ extern "C" void esp_yield();
5151
// -----------------------------------------------------------------------------------------------------------------------
5252

5353
bool ESP8266WiFiGenericClass::_persistent = true;
54-
55-
54+
WiFiEventCb ESP8266WiFiGenericClass::_cbEvent = NULL;
5655

5756
ESP8266WiFiGenericClass::ESP8266WiFiGenericClass() {
5857
wifi_set_event_handler_cb((wifi_event_handler_cb_t) &ESP8266WiFiGenericClass::_eventCallback);
5958
}
6059

60+
/**
61+
* set callback function
62+
* @param cbEvent WiFiEventCb
63+
*/
64+
void ESP8266WiFiGenericClass::onEvent(WiFiEventCb cbEvent) {
65+
_cbEvent = cbEvent;
66+
}
67+
6168
/**
6269
* callback for WiFi events
6370
* @param arg
@@ -70,11 +77,11 @@ void ESP8266WiFiGenericClass::_eventCallback(void* arg) {
7077
WiFiClient::stopAll();
7178
}
7279

73-
//TODO allow user to hook this event
80+
if(_cbEvent) {
81+
_cbEvent((WiFiEvent_t) event->event);
82+
}
7483
}
7584

76-
77-
7885
/**
7986
* Return the current channel associated with the network
8087
* @return channel (1-13)
@@ -236,8 +243,9 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
236243
* @param callback_arg
237244
*/
238245
void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) {
239-
if(ipaddr)
246+
if(ipaddr) {
240247
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->addr;
248+
}
241249
esp_schedule(); // resume the hostByName function
242250
}
243251

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@
2525

2626
#include "ESP8266WiFiType.h"
2727

28+
typedef void (*WiFiEventCb)(WiFiEvent_t event);
29+
2830
class ESP8266WiFiGenericClass {
2931
// ----------------------------------------------------------------------------------------------
3032
// -------------------------------------- Generic WiFi function ---------------------------------
3133
// ----------------------------------------------------------------------------------------------
3234

3335
public:
36+
3437
ESP8266WiFiGenericClass();
3538

39+
void onEvent(WiFiEventCb cbEvent);
40+
3641
int32_t channel(void);
3742

3843
bool setSleepMode(WiFiSleepType_t type);
@@ -50,9 +55,10 @@ class ESP8266WiFiGenericClass {
5055
bool enableAP(bool enable);
5156

5257
protected:
53-
static bool _persistent;
58+
static bool _persistent;
59+
static WiFiEventCb _cbEvent;
5460

55-
static void _eventCallback(void *event);
61+
static void _eventCallback(void *event);
5662

5763
// ----------------------------------------------------------------------------------------------
5864
// ------------------------------------ Generic Network function --------------------------------
@@ -69,5 +75,4 @@ class ESP8266WiFiGenericClass {
6975
friend class ESP8266WiFiAPClass;
7076
};
7177

72-
7378
#endif /* ESP8266WIFIGENERIC_H_ */

libraries/ESP8266WiFi/src/ESP8266WiFiScan.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class ESP8266WiFiScanClass {
6060
static void* _scanResult;
6161

6262
static void _scanDone(void* result, int status);
63-
void * _getScanInfoByIndex(int i);
64-
65-
friend class ESP8266WiFiClass;
63+
static void * _getScanInfoByIndex(int i);
6664

6765
};
6866

libraries/ESP8266WiFi/src/ESP8266WiFiType.h

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ typedef enum {
4444
} WiFiSleepType_t;
4545

4646

47+
typedef enum {
48+
WIFI_EVENT_STAMODE_CONNECTED = 0,
49+
WIFI_EVENT_STAMODE_DISCONNECTED,
50+
WIFI_EVENT_STAMODE_AUTHMODE_CHANGE,
51+
WIFI_EVENT_STAMODE_GOT_IP,
52+
WIFI_EVENT_STAMODE_DHCP_TIMEOUT,
53+
WIFI_EVENT_SOFTAPMODE_STACONNECTED,
54+
WIFI_EVENT_SOFTAPMODE_STADISCONNECTED,
55+
WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED,
56+
WIFI_EVENT_MAX
57+
} WiFiEvent_t;
58+
59+
60+
4761
extern "C" {
4862
typedef STAILQ_HEAD(, bss_info)
4963
bss_info_head_t;

0 commit comments

Comments
 (0)