Skip to content

Commit b5170bd

Browse files
committed
Implement event handler for AP connections
1 parent e3de5ff commit b5170bd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

libraries/WiFi/src/WiFi.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_
5858
static_cast<WhdSoftAPInterface*>(_softAP)->set_network(_ip, _netmask, _gateway);
5959
nsapi_error_t result = static_cast<WhdSoftAPInterface*>(_softAP)->start(ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */, NULL, true /* cohexistance */);
6060

61+
static_cast<WhdSoftAPInterface*>(_softAP)->register_event_handler([](whd_interface_t ifp, const whd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data) -> void*{
62+
63+
if(event_header->event_type == 8){ //8 = connect
64+
WiFi._currentNetworkStatus = WL_AP_CONNECTED;
65+
return nullptr;
66+
}
67+
68+
if(event_header->event_type == 12){ //12 = disconnect
69+
WiFi._currentNetworkStatus = WL_AP_LISTENING;
70+
return nullptr;
71+
}
72+
73+
return nullptr;
74+
});
75+
6176
_currentNetworkStatus = (result == NSAPI_ERROR_OK && setSSID(ssid)) ? WL_AP_LISTENING : WL_AP_FAILED;
6277
return _currentNetworkStatus;
6378
}
@@ -80,10 +95,12 @@ void arduino::WiFiClass::end() {
8095

8196
int arduino::WiFiClass::disconnect() {
8297
if (_softAP != nullptr) {
98+
static_cast<WhdSoftAPInterface*>(_softAP)->unregister_event_handler();
8399
return static_cast<WhdSoftAPInterface*>(_softAP)->stop();
84100
} else {
85101
return wifi_if->disconnect();
86102
}
103+
_currentNetworkStatus = WL_IDLE_STATUS;
87104
}
88105

89106
void arduino::WiFiClass::config(arduino::IPAddress local_ip){

libraries/WiFi/src/WiFi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class WiFiClass
303303
SocketAddress _dnsServer1 = nullptr;
304304
SocketAddress _dnsServer2 = nullptr;
305305
char* _ssid = nullptr;
306-
wl_status_t _currentNetworkStatus = WL_IDLE_STATUS;
306+
volatile wl_status_t _currentNetworkStatus = WL_IDLE_STATUS;
307307
WiFiInterface* wifi_if = nullptr;
308308
voidPrtFuncPtr _initializerCallback;
309309
WiFiAccessPoint* ap_list = nullptr;

0 commit comments

Comments
 (0)