Skip to content

Commit 3b49dcb

Browse files
committed
Add default AP event handler
1 parent b5170bd commit 3b49dcb

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

libraries/WiFi/src/WiFi.cpp

+32-17
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,50 @@ int arduino::WiFiClass::begin(const char* ssid, const char *passphrase) {
4444

4545
int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_t channel) {
4646

47-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
48-
_softAP = WhdSoftAPInterface::get_default_instance();
49-
#endif
47+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
48+
_softAP = WhdSoftAPInterface::get_default_instance();
49+
#endif
5050

5151
if (_softAP == NULL) {
52-
return WL_AP_FAILED;
52+
return (_currentNetworkStatus = WL_AP_FAILED);
5353
}
5454

5555
ensureDefaultAPNetworkConfiguration();
5656

57+
WhdSoftAPInterface* softAPInterface = static_cast<WhdSoftAPInterface*>(_softAP);
58+
5759
//Set ap ssid, password and channel
58-
static_cast<WhdSoftAPInterface*>(_softAP)->set_network(_ip, _netmask, _gateway);
59-
nsapi_error_t result = static_cast<WhdSoftAPInterface*>(_softAP)->start(ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */, NULL, true /* cohexistance */);
60+
softAPInterface->set_network(_ip, _netmask, _gateway);
61+
nsapi_error_t result = softAPInterface->start(ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */, NULL, true /* cohexistance */);
6062

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;
63+
nsapi_error_t registrationResult;
64+
softAPInterface->unregister_event_handler();
65+
registrationResult = softAPInterface->register_event_handler([](whd_interface_t ifp, const whd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data) -> void*{
66+
67+
if(event_header->event_type == WLC_E_ASSOC_IND){
68+
WiFi._currentNetworkStatus = WL_AP_CONNECTED;
69+
} else if(event_header->event_type == WLC_E_DISASSOC_IND){
70+
WiFi._currentNetworkStatus = WL_AP_LISTENING;
71+
}
72+
73+
// Default Event Handler
74+
if ((event_header->event_type == (whd_event_num_t)WLC_E_LINK) || (event_header->event_type == WLC_E_IF)) {
75+
whd_driver_t whd_driver = ifp->whd_driver;
76+
if (osSemaphoreGetCount(whd_driver->ap_info.whd_wifi_sleep_flag) < 1) {
77+
osStatus_t result = osSemaphoreRelease(whd_driver->ap_info.whd_wifi_sleep_flag);
78+
if (result != osOK) {
79+
printf("Release whd_wifi_sleep_flag ERROR: %d", result);
80+
}
81+
}
6682
}
6783

68-
if(event_header->event_type == 12){ //12 = disconnect
69-
WiFi._currentNetworkStatus = WL_AP_LISTENING;
70-
return nullptr;
71-
}
72-
73-
return nullptr;
84+
return handler_user_data;
7485
});
7586

87+
if (registrationResult != NSAPI_ERROR_OK) {
88+
return (_currentNetworkStatus = WL_AP_FAILED);
89+
}
90+
7691
_currentNetworkStatus = (result == NSAPI_ERROR_OK && setSSID(ssid)) ? WL_AP_LISTENING : WL_AP_FAILED;
7792
return _currentNetworkStatus;
7893
}

0 commit comments

Comments
 (0)