From 8894005b07c51cfee24dea1ed6c70a0397c222e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Mon, 14 Nov 2022 23:57:51 +0000 Subject: [PATCH 1/4] Improvements in Wifi StartScan - Rework Network_Interface_Start_Scan to return detailed success/error code. - Update caller accordingly. (cherry picked from commit d68004e5d976659ad9b820562b4010f51b3e14c4) --- src/PAL/Include/nanoPAL_Sockets.h | 2 +- targets/ESP32/_Network/NF_ESP32_Wireless.cpp | 4 +- targets/ESP32/_common/Target_Network.cpp | 13 +++---- targets/ESP32/_include/NF_ESP32_Network.h | 14 ++++++- ..._native_System_Device_Wifi_WifiAdapter.cpp | 38 +++++++++++++++---- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/PAL/Include/nanoPAL_Sockets.h b/src/PAL/Include/nanoPAL_Sockets.h index 3bf451825b..d345a3cded 100644 --- a/src/PAL/Include/nanoPAL_Sockets.h +++ b/src/PAL/Include/nanoPAL_Sockets.h @@ -522,7 +522,7 @@ bool Network_Interface_Close(int index); int Network_Interface_Disconnect(int index); int Network_Interface_Start_Connect(int index, const char *ssid, const char *passphase, int options); int Network_Interface_Connect_Result(int configIndex); -bool Network_Interface_Start_Scan(int index); +int Network_Interface_Start_Scan(int index); // Wireless AP methods void Network_Interface_Add_Station(uint16_t index, uint8_t *macAddress); diff --git a/targets/ESP32/_Network/NF_ESP32_Wireless.cpp b/targets/ESP32/_Network/NF_ESP32_Wireless.cpp index 32042f7c70..5638f4ff88 100644 --- a/targets/ESP32/_Network/NF_ESP32_Wireless.cpp +++ b/targets/ESP32/_Network/NF_ESP32_Wireless.cpp @@ -349,9 +349,7 @@ int NF_ESP32_Wireless_Scan() // Start a Wi-Fi scan // When complete a Scan Complete event will be fired - esp_err_t res = esp_wifi_scan_start(&config, false); - - return (int)res; + return esp_wifi_scan_start(&config, false); } wifi_auth_mode_t MapAuthentication(AuthenticationType type) diff --git a/targets/ESP32/_common/Target_Network.cpp b/targets/ESP32/_common/Target_Network.cpp index 4e06a30e97..ca65d610d2 100644 --- a/targets/ESP32/_common/Target_Network.cpp +++ b/targets/ESP32/_common/Target_Network.cpp @@ -97,7 +97,7 @@ bool Network_Interface_Close(int index) return false; } -bool Network_Interface_Start_Scan(int index) +int Network_Interface_Start_Scan(int index) { HAL_Configuration_NetworkInterface networkConfiguration; @@ -107,18 +107,17 @@ bool Network_Interface_Start_Scan(int index) DeviceConfigurationOption_Network, index)) { - // failed to load configuration - // FIXME output error? - return SOCK_SOCKET_ERROR; + // failed to get configuration + return StartScanOutcome_FailedToGetConfiguration; } // can only do this is this is STA - if (networkConfiguration.InterfaceType == NetworkInterfaceType_Wireless80211) + if (networkConfiguration.InterfaceType != NetworkInterfaceType_Wireless80211) { - return (NF_ESP32_Wireless_Scan() == 0); + return StartScanOutcome_WrongInterfaceType; } - return false; + return NF_ESP32_Wireless_Scan(); } bool GetWirelessConfig(int index, HAL_Configuration_Wireless80211 **wirelessConfig) diff --git a/targets/ESP32/_include/NF_ESP32_Network.h b/targets/ESP32/_include/NF_ESP32_Network.h index 1986c9cd48..6fffb99abc 100644 --- a/targets/ESP32/_include/NF_ESP32_Network.h +++ b/targets/ESP32/_include/NF_ESP32_Network.h @@ -7,7 +7,6 @@ #define NF_ESP32_NETWORK_H #include -#include #include // ESP IDF 4.0 it's using an abstraction layer (esp_netif) that hides the netif index @@ -17,6 +16,19 @@ #define IDF_WIFI_AP_DEF 1 #define IDF_ETH_DEF 2 +typedef enum __nfpack StartScanOutcome +{ + StartScanOutcome_Success = 0, + StartScanOutcome_FailedToGetConfiguration = 10, + StartScanOutcome_WrongInterfaceType = 20, + // these are the same as the IDF error codes + StartScanOutcome_Esp32WifiNotInit = ESP_ERR_WIFI_NOT_INIT, + StartScanOutcome_Esp32WifiNotStarted = ESP_ERR_WIFI_NOT_STARTED, + StartScanOutcome_Esp32WifiTimeout = ESP_ERR_WIFI_TIMEOUT, + StartScanOutcome_Esp32WifiState = ESP_ERR_WIFI_STATE, + +} StartScanOutcome; + extern bool NF_ESP32_ConnectInProgress; extern int NF_ESP32_ConnectResult; // flag to signal if connect is to happen diff --git a/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp b/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp index 5cd4d436e5..73c9e62520 100644 --- a/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp +++ b/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp @@ -7,8 +7,9 @@ #include #include #include +#include -/////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////// // !!! KEEP IN SYNC WITH System.Device.Wifi (in managed code) !!! // /////////////////////////////////////////////////////////////////////////////////////// struct ScanRecord @@ -180,17 +181,38 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeDiscon HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeScanAsync___VOID(CLR_RT_StackFrame &stack) { NANOCLR_HEADER(); - { - int netIndex; - NANOCLR_CHECK_HRESULT(GetNetInterfaceIndex(stack, &netIndex)); + int netIndex; + int startScanResult; - // Start scan - if (Network_Interface_Start_Scan(netIndex) == false) - { + NANOCLR_CHECK_HRESULT(GetNetInterfaceIndex(stack, &netIndex)); + + // Start scan + startScanResult = Network_Interface_Start_Scan(netIndex); + + switch (startScanResult) + { + case StartScanOutcome_FailedToGetConfiguration: + NANOCLR_SET_AND_LEAVE(CLR_E_FAIL); + break; + + case StartScanOutcome_WrongInterfaceType: + case StartScanOutcome_Esp32WifiNotInit: + case StartScanOutcome_Esp32WifiNotStarted: + case StartScanOutcome_Esp32WifiState: NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION); - } + break; + + case StartScanOutcome_Esp32WifiTimeout: + NANOCLR_SET_AND_LEAVE(CLR_E_TIMEOUT); + break; + + default: + debug_printf("START SCAN error %d", startScanResult); + NANOCLR_SET_AND_LEAVE(CLR_E_UNKNOWN_TYPE); + break; } + NANOCLR_NOCLEANUP(); } From ff9d1aa8a31e56f9af01972c8812b7cb9aca11ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 15 Nov 2022 14:57:32 +0000 Subject: [PATCH 2/4] Improvements in Wifi scan exceptions (cherry picked from commit 9be414f8faf7d6986f67ea43dbafff2f7c206a57) --- ...ev_wifi_native_System_Device_Wifi_WifiAdapter.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp b/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp index 73c9e62520..029db4429e 100644 --- a/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp +++ b/targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp @@ -192,24 +192,24 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeScanAs switch (startScanResult) { - case StartScanOutcome_FailedToGetConfiguration: - NANOCLR_SET_AND_LEAVE(CLR_E_FAIL); - break; case StartScanOutcome_WrongInterfaceType: case StartScanOutcome_Esp32WifiNotInit: case StartScanOutcome_Esp32WifiNotStarted: - case StartScanOutcome_Esp32WifiState: NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION); break; + case StartScanOutcome_Esp32WifiState: + NANOCLR_SET_AND_LEAVE(CLR_E_BUSY); + break; + case StartScanOutcome_Esp32WifiTimeout: NANOCLR_SET_AND_LEAVE(CLR_E_TIMEOUT); break; + case StartScanOutcome_FailedToGetConfiguration: default: - debug_printf("START SCAN error %d", startScanResult); - NANOCLR_SET_AND_LEAVE(CLR_E_UNKNOWN_TYPE); + NANOCLR_SET_AND_LEAVE(CLR_E_FAIL); break; } From 3d5b6b46afafb49ab1bfc95a9b80a12c9a459a26 Mon Sep 17 00:00:00 2001 From: nfbot Date: Tue, 15 Nov 2022 15:12:46 +0000 Subject: [PATCH 3/4] Code style fixes Automated fixes for code style. --- targets/ESP32/_include/NF_ESP32_Network.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/targets/ESP32/_include/NF_ESP32_Network.h b/targets/ESP32/_include/NF_ESP32_Network.h index 6fffb99abc..c74fee9dd4 100644 --- a/targets/ESP32/_include/NF_ESP32_Network.h +++ b/targets/ESP32/_include/NF_ESP32_Network.h @@ -12,14 +12,14 @@ // ESP IDF 4.0 it's using an abstraction layer (esp_netif) that hides the netif index // we need that for our interface with lwIP, so we have to clone those here // not very elegant but will have to work for now -#define IDF_WIFI_STA_DEF 0 -#define IDF_WIFI_AP_DEF 1 -#define IDF_ETH_DEF 2 +#define IDF_WIFI_STA_DEF 0 +#define IDF_WIFI_AP_DEF 1 +#define IDF_ETH_DEF 2 typedef enum __nfpack StartScanOutcome { StartScanOutcome_Success = 0, - StartScanOutcome_FailedToGetConfiguration = 10, + StartScanOutcome_FailedToGetConfiguration = 10, StartScanOutcome_WrongInterfaceType = 20, // these are the same as the IDF error codes StartScanOutcome_Esp32WifiNotInit = ESP_ERR_WIFI_NOT_INIT, From afd617bda2fd6fb8d502d8b940a1d0db6870895c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 15 Nov 2022 15:40:43 +0000 Subject: [PATCH 4/4] Fix implementation for Azure RTOS --- targets/AzureRTOS/_common/target_network.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/targets/AzureRTOS/_common/target_network.cpp b/targets/AzureRTOS/_common/target_network.cpp index 3d5660db77..f58cdad789 100644 --- a/targets/AzureRTOS/_common/target_network.cpp +++ b/targets/AzureRTOS/_common/target_network.cpp @@ -111,7 +111,7 @@ bool Network_Interface_Close(int index) return false; } -bool Network_Interface_Start_Scan(int index) +int Network_Interface_Start_Scan(int index) { HAL_Configuration_NetworkInterface networkConfiguration; @@ -121,18 +121,21 @@ bool Network_Interface_Start_Scan(int index) DeviceConfigurationOption_Network, index)) { - // failed to load configuration - // FIXME output error? - return SOCK_SOCKET_ERROR; + // failed to get configuration + // TODO include error code enum + return 7777; // StartScanOutcome_FailedToGetConfiguration; } // can only do this is this is STA - if (networkConfiguration.InterfaceType == NetworkInterfaceType_Wireless80211) + if (networkConfiguration.InterfaceType != NetworkInterfaceType_Wireless80211) { - // return (NF_ESP32_Wireless_Scan() == 0); + // TODO include error code enum + return 8888; // StartScanOutcome_WrongInterfaceType; } - return false; + // TODO return NF_ESP32_Wireless_Scan(); + // TODO include error code enum + return 9999; } bool GetWirelessConfig(int index, HAL_Configuration_Wireless80211 **wirelessConfig)