From b5170bdff504b1e0f048c1460f917a6e8ffb9d16 Mon Sep 17 00:00:00 2001 From: Sebastian Hunkeler Date: Mon, 26 Oct 2020 17:46:28 +0100 Subject: [PATCH 1/5] Implement event handler for AP connections --- libraries/WiFi/src/WiFi.cpp | 17 +++++++++++++++++ libraries/WiFi/src/WiFi.h | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 85f2a16c3..e40d00f51 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -58,6 +58,21 @@ int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_ static_cast(_softAP)->set_network(_ip, _netmask, _gateway); nsapi_error_t result = static_cast(_softAP)->start(ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */, NULL, true /* cohexistance */); + static_cast(_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*{ + + if(event_header->event_type == 8){ //8 = connect + WiFi._currentNetworkStatus = WL_AP_CONNECTED; + return nullptr; + } + + if(event_header->event_type == 12){ //12 = disconnect + WiFi._currentNetworkStatus = WL_AP_LISTENING; + return nullptr; + } + + return nullptr; + }); + _currentNetworkStatus = (result == NSAPI_ERROR_OK && setSSID(ssid)) ? WL_AP_LISTENING : WL_AP_FAILED; return _currentNetworkStatus; } @@ -80,10 +95,12 @@ void arduino::WiFiClass::end() { int arduino::WiFiClass::disconnect() { if (_softAP != nullptr) { + static_cast(_softAP)->unregister_event_handler(); return static_cast(_softAP)->stop(); } else { return wifi_if->disconnect(); } + _currentNetworkStatus = WL_IDLE_STATUS; } void arduino::WiFiClass::config(arduino::IPAddress local_ip){ diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 394d1c7c4..4f013c677 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -303,7 +303,7 @@ class WiFiClass SocketAddress _dnsServer1 = nullptr; SocketAddress _dnsServer2 = nullptr; char* _ssid = nullptr; - wl_status_t _currentNetworkStatus = WL_IDLE_STATUS; + volatile wl_status_t _currentNetworkStatus = WL_IDLE_STATUS; WiFiInterface* wifi_if = nullptr; voidPrtFuncPtr _initializerCallback; WiFiAccessPoint* ap_list = nullptr; From 3b49dcbd30b28e709937e1867134cada6852700c Mon Sep 17 00:00:00 2001 From: Sebastian Hunkeler Date: Mon, 26 Oct 2020 23:29:19 +0100 Subject: [PATCH 2/5] Add default AP event handler --- libraries/WiFi/src/WiFi.cpp | 49 ++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index e40d00f51..c03769ecd 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -44,35 +44,50 @@ int arduino::WiFiClass::begin(const char* ssid, const char *passphrase) { int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_t channel) { -#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) - _softAP = WhdSoftAPInterface::get_default_instance(); -#endif + #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) + _softAP = WhdSoftAPInterface::get_default_instance(); + #endif if (_softAP == NULL) { - return WL_AP_FAILED; + return (_currentNetworkStatus = WL_AP_FAILED); } ensureDefaultAPNetworkConfiguration(); + WhdSoftAPInterface* softAPInterface = static_cast(_softAP); + //Set ap ssid, password and channel - static_cast(_softAP)->set_network(_ip, _netmask, _gateway); - nsapi_error_t result = static_cast(_softAP)->start(ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */, NULL, true /* cohexistance */); + softAPInterface->set_network(_ip, _netmask, _gateway); + nsapi_error_t result = softAPInterface->start(ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */, NULL, true /* cohexistance */); - static_cast(_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*{ - - if(event_header->event_type == 8){ //8 = connect - WiFi._currentNetworkStatus = WL_AP_CONNECTED; - return nullptr; + 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 + if ((event_header->event_type == (whd_event_num_t)WLC_E_LINK) || (event_header->event_type == WLC_E_IF)) { + whd_driver_t whd_driver = ifp->whd_driver; + 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); + } + } } - if(event_header->event_type == 12){ //12 = disconnect - WiFi._currentNetworkStatus = WL_AP_LISTENING; - return nullptr; - } - - return nullptr; + return handler_user_data; }); + if (registrationResult != NSAPI_ERROR_OK) { + return (_currentNetworkStatus = WL_AP_FAILED); + } + _currentNetworkStatus = (result == NSAPI_ERROR_OK && setSSID(ssid)) ? WL_AP_LISTENING : WL_AP_FAILED; return _currentNetworkStatus; } From 59e224de6c104a4c53f3eb51ee573ff087e08aed Mon Sep 17 00:00:00 2001 From: Sebastian Hunkeler Date: Mon, 26 Oct 2020 23:33:31 +0100 Subject: [PATCH 3/5] Add correct network status for disconnect --- libraries/WiFi/src/WiFi.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index c03769ecd..7104f8b85 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -110,12 +110,15 @@ void arduino::WiFiClass::end() { int arduino::WiFiClass::disconnect() { if (_softAP != nullptr) { - static_cast(_softAP)->unregister_event_handler(); - return static_cast(_softAP)->stop(); + WhdSoftAPInterface* softAPInterface = static_cast(_softAP); + softAPInterface->unregister_event_handler(); + _currentNetworkStatus = (softAPInterface->stop() == NSAPI_ERROR_OK ? WL_DISCONNECTED : WL_AP_FAILED); } else { - return wifi_if->disconnect(); + wifi_if->disconnect(); + _currentNetworkStatus = WL_DISCONNECTED; } - _currentNetworkStatus = WL_IDLE_STATUS; + + return _currentNetworkStatus; } void arduino::WiFiClass::config(arduino::IPAddress local_ip){ From f3a2d60b4b9aaecd2117762e56a3275d2aad6327 Mon Sep 17 00:00:00 2001 From: Sebastian Hunkeler Date: Tue, 27 Oct 2020 00:11:45 +0100 Subject: [PATCH 4/5] Add missing lines from default AP event handler --- libraries/WiFi/src/WiFi.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 7104f8b85..8812a74ee 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -71,8 +71,10 @@ int arduino::WiFiClass::beginAP(const char* ssid, const char *passphrase, uint8_ } // 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)) { - whd_driver_t whd_driver = ifp->whd_driver; 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) { From 300a53383bace1664f8d2ba0173cc12bb1468741 Mon Sep 17 00:00:00 2001 From: Sebastian Hunkeler Date: Tue, 27 Oct 2020 10:28:49 +0100 Subject: [PATCH 5/5] Extract AP event handler into static function --- libraries/WiFi/src/WiFi.cpp | 47 +++++++++++++++++++------------------ libraries/WiFi/src/WiFi.h | 1 + 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp index 8812a74ee..3dde521c4 100644 --- a/libraries/WiFi/src/WiFi.cpp +++ b/libraries/WiFi/src/WiFi.cpp @@ -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); @@ -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); diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h index 4f013c677..8231ae6e9 100644 --- a/libraries/WiFi/src/WiFi.h +++ b/libraries/WiFi/src/WiFi.h @@ -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);