@@ -44,24 +44,57 @@ int arduino::WiFiClass::begin(const char* ssid, const char *passphrase) {
4444
4545int 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
63+ nsapi_error_t registrationResult;
64+ softAPInterface->unregister_event_handler ();
65+ registrationResult = softAPInterface->register_event_handler (arduino::WiFiClass::handleAPEvents);
66+
67+ if (registrationResult != NSAPI_ERROR_OK) {
68+ return (_currentNetworkStatus = WL_AP_FAILED);
69+ }
70+
6171 _currentNetworkStatus = (result == NSAPI_ERROR_OK && setSSID (ssid)) ? WL_AP_LISTENING : WL_AP_FAILED;
6272 return _currentNetworkStatus;
6373}
6474
75+ void * arduino::WiFiClass::handleAPEvents (whd_interface_t ifp, const whd_event_header_t *event_header, const uint8_t *event_data, void *handler_user_data){
76+ if (event_header->event_type == WLC_E_ASSOC_IND){
77+ WiFi._currentNetworkStatus = WL_AP_CONNECTED;
78+ } else if (event_header->event_type == WLC_E_DISASSOC_IND){
79+ WiFi._currentNetworkStatus = WL_AP_LISTENING;
80+ }
81+
82+ // Default Event Handler
83+ whd_driver_t whd_driver = ifp->whd_driver ;
84+ WHD_IOCTL_LOG_ADD_EVENT (whd_driver, event_header->event_type , event_header->flags , event_header->reason );
85+
86+ if ((event_header->event_type == (whd_event_num_t )WLC_E_LINK) || (event_header->event_type == WLC_E_IF)) {
87+ if (osSemaphoreGetCount (whd_driver->ap_info .whd_wifi_sleep_flag ) < 1 ) {
88+ osStatus_t result = osSemaphoreRelease (whd_driver->ap_info .whd_wifi_sleep_flag );
89+ if (result != osOK) {
90+ printf (" Release whd_wifi_sleep_flag ERROR: %d" , result);
91+ }
92+ }
93+ }
94+
95+ return handler_user_data;
96+ }
97+
6598void arduino::WiFiClass::ensureDefaultAPNetworkConfiguration () {
6699 if (_ip == nullptr ){
67100 _ip = SocketAddress (DEFAULT_IP_ADDRESS);
@@ -80,10 +113,15 @@ void arduino::WiFiClass::end() {
80113
81114int arduino::WiFiClass::disconnect () {
82115 if (_softAP != nullptr ) {
83- return static_cast <WhdSoftAPInterface*>(_softAP)->stop ();
116+ WhdSoftAPInterface* softAPInterface = static_cast <WhdSoftAPInterface*>(_softAP);
117+ softAPInterface->unregister_event_handler ();
118+ _currentNetworkStatus = (softAPInterface->stop () == NSAPI_ERROR_OK ? WL_DISCONNECTED : WL_AP_FAILED);
84119 } else {
85- return wifi_if->disconnect ();
120+ wifi_if->disconnect ();
121+ _currentNetworkStatus = WL_DISCONNECTED;
86122 }
123+
124+ return _currentNetworkStatus;
87125}
88126
89127void arduino::WiFiClass::config (arduino::IPAddress local_ip){
0 commit comments