Skip to content

Commit

Permalink
Extract AP event handler into static function
Browse files Browse the repository at this point in the history
  • Loading branch information
sebromero committed Oct 27, 2020
1 parent f3a2d60 commit 300a533
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
47 changes: 24 additions & 23 deletions libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,7 @@ int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_

nsapi_error_t registrationResult;
softAPInterface->unregister_event_handler();
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*{

if(event_header->event_type == WLC_E_ASSOC_IND){
WiFi._currentNetworkStatus = WL_AP_CONNECTED;
} else if(event_header->event_type == WLC_E_DISASSOC_IND){
WiFi._currentNetworkStatus = WL_AP_LISTENING;
}

// Default Event Handler
whd_driver_t whd_driver = ifp->whd_driver;
WHD_IOCTL_LOG_ADD_EVENT(whd_driver, event_header->event_type, event_header->flags, event_header->reason);

if ((event_header->event_type == (whd_event_num_t)WLC_E_LINK) || (event_header->event_type == WLC_E_IF)) {
if (osSemaphoreGetCount(whd_driver->ap_info.whd_wifi_sleep_flag) < 1) {
osStatus_t result = osSemaphoreRelease(whd_driver->ap_info.whd_wifi_sleep_flag);
if (result != osOK) {
printf("Release whd_wifi_sleep_flag ERROR: %d", result);
}
}
}

return handler_user_data;
});
registrationResult = softAPInterface->register_event_handler(arduino::WiFiClass::handleAPEvents);

if (registrationResult != NSAPI_ERROR_OK) {
return (_currentNetworkStatus = WL_AP_FAILED);
Expand All @@ -94,6 +72,29 @@ int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_
return _currentNetworkStatus;
}

void * arduino::WiFiClass::handleAPEvents(whd_interface_t ifp, const whd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data){
if(event_header->event_type == WLC_E_ASSOC_IND){
WiFi._currentNetworkStatus = WL_AP_CONNECTED;
} else if(event_header->event_type == WLC_E_DISASSOC_IND){
WiFi._currentNetworkStatus = WL_AP_LISTENING;
}

// Default Event Handler
whd_driver_t whd_driver = ifp->whd_driver;
WHD_IOCTL_LOG_ADD_EVENT(whd_driver, event_header->event_type, event_header->flags, event_header->reason);

if ((event_header->event_type == (whd_event_num_t)WLC_E_LINK) || (event_header->event_type == WLC_E_IF)) {
if (osSemaphoreGetCount(whd_driver->ap_info.whd_wifi_sleep_flag) < 1) {
osStatus_t result = osSemaphoreRelease(whd_driver->ap_info.whd_wifi_sleep_flag);
if (result != osOK) {
printf("Release whd_wifi_sleep_flag ERROR: %d", result);
}
}
}

return handler_user_data;
}

void arduino::WiFiClass::ensureDefaultAPNetworkConfiguration() {
if(_ip == nullptr){
_ip = SocketAddress(DEFAULT_IP_ADDRESS);
Expand Down
1 change: 1 addition & 0 deletions libraries/WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class WiFiClass
uint8_t connected_ap;
int setSSID(const char* ssid);
void ensureDefaultAPNetworkConfiguration();
static void * handleAPEvents(whd_interface_t ifp, const whd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data);
bool isVisible(const char* ssid);
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
Expand Down

0 comments on commit 300a533

Please sign in to comment.