Skip to content

Implement event handler for AP connections #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,57 @@ 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<WhdSoftAPInterface*>(_softAP);

//Set ap ssid, password and channel
static_cast<WhdSoftAPInterface*>(_softAP)->set_network(_ip, _netmask, _gateway);
nsapi_error_t result = static_cast<WhdSoftAPInterface*>(_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 */);

nsapi_error_t registrationResult;
softAPInterface->unregister_event_handler();
registrationResult = softAPInterface->register_event_handler(arduino::WiFiClass::handleAPEvents);

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;
}

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 All @@ -80,10 +113,15 @@ void arduino::WiFiClass::end() {

int arduino::WiFiClass::disconnect() {
if (_softAP != nullptr) {
return static_cast<WhdSoftAPInterface*>(_softAP)->stop();
WhdSoftAPInterface* softAPInterface = static_cast<WhdSoftAPInterface*>(_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;
}

return _currentNetworkStatus;
}

void arduino::WiFiClass::config(arduino::IPAddress local_ip){
Expand Down
3 changes: 2 additions & 1 deletion libraries/WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,14 @@ 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;
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