From 38cdefa51aa38a9b5148781ac60b7efef10f329a Mon Sep 17 00:00:00 2001 From: Arash Zandi Date: Sun, 30 Nov 2025 14:40:32 +0330 Subject: [PATCH] Wifi DeviceName added --- .../sys_dev_wifi_native.cpp | 4 +- src/System.Device.Wifi/sys_dev_wifi_native.h | 1 + ..._native_System_Device_Wifi_WifiAdapter.cpp | 105 ++++++++++-------- ..._native_System_Device_Wifi_WifiAdapter.cpp | 39 +++++++ 4 files changed, 100 insertions(+), 49 deletions(-) diff --git a/src/System.Device.Wifi/sys_dev_wifi_native.cpp b/src/System.Device.Wifi/sys_dev_wifi_native.cpp index 8c894e93d7..a0d4c36d19 100644 --- a/src/System.Device.Wifi/sys_dev_wifi_native.cpp +++ b/src/System.Device.Wifi/sys_dev_wifi_native.cpp @@ -33,12 +33,14 @@ static const CLR_RT_MethodHandler method_lookup[] = NULL, NULL, NULL, + NULL, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::DisposeNative___VOID, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeInit___VOID, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeConnect___SystemDeviceWifiWifiConnectionStatus__STRING__STRING__SystemDeviceWifiWifiReconnectionKind, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeDisconnect___VOID, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeScanAsync___VOID, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::GetNativeScanReport___SZARRAY_U1, + Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeSetDeviceName___VOID__STRING, NULL, Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeFindWirelessAdapters___STATIC__SZARRAY_U1, NULL, @@ -81,7 +83,7 @@ static const CLR_RT_MethodHandler method_lookup[] = const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_System_Device_Wifi = { "System.Device.Wifi", - 0x00A058C6, + 0x030E2768, method_lookup, { 100, 0, 6, 4 } }; diff --git a/src/System.Device.Wifi/sys_dev_wifi_native.h b/src/System.Device.Wifi/sys_dev_wifi_native.h index 878d8a5e8d..2535275a65 100644 --- a/src/System.Device.Wifi/sys_dev_wifi_native.h +++ b/src/System.Device.Wifi/sys_dev_wifi_native.h @@ -66,6 +66,7 @@ struct Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter NANOCLR_NATIVE_DECLARE(NativeDisconnect___VOID); NANOCLR_NATIVE_DECLARE(NativeScanAsync___VOID); NANOCLR_NATIVE_DECLARE(GetNativeScanReport___SZARRAY_U1); + NANOCLR_NATIVE_DECLARE(NativeSetDeviceName___VOID__STRING); NANOCLR_NATIVE_DECLARE(NativeFindWirelessAdapters___STATIC__SZARRAY_U1); //--// diff --git a/targets/AzureRTOS/ST/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp b/targets/AzureRTOS/ST/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp index 770ac22532..3ed86eac02 100644 --- a/targets/AzureRTOS/ST/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp +++ b/targets/AzureRTOS/ST/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp @@ -6,7 +6,6 @@ #include #include -//#include /////////////////////////////////////////////////////////////////////////////////////// // !!! KEEP IN SYNC WITH System.Device.WiFi (in managed code) !!! // @@ -20,7 +19,6 @@ struct ScanRecord uint8_t cypherType; }; - // // // // Stores Ap records to target string. // // if pTarget == 0 then just calculates length of target @@ -59,6 +57,16 @@ struct ScanRecord // return (recordCount * sizeof(ScanRecord) + sizeof(uint16_t)); // } +HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeSetDeviceName___VOID__STRING( + CLR_RT_StackFrame &stack) +{ + NANOCLR_HEADER(); + { + NANOCLR_SET_AND_LEAVE(CLR_E_NOTIMPL); + } + NANOCLR_NOCLEANUP(); +} + HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::DisposeNative___VOID(CLR_RT_StackFrame &stack) { NANOCLR_HEADER(); @@ -113,7 +121,7 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter:: int reconnectionKind; int netIndex; CLR_RT_HeapBlock hbTimeout; - //CLR_INT64 *timeout; + // CLR_INT64 *timeout; bool eventResult = true; WifiConnectionStatus Status = WifiConnectionStatus_UnspecifiedFailure; @@ -147,49 +155,50 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter:: // Wait for connect to finish while (eventResult) { - // int connectResult = Network_Interface_Connect_Result(netIndex); - // if (connectResult >= 0) - // { - // // Map ESP32 wifi reason code to WifiConnectionStatus - // switch (connectResult) - // { - // case 0: - // Status = WifiConnectionStatus_Success; - // break; - - // case WIFI_REASON_NO_AP_FOUND: - // Status = WifiConnectionStatus_NetworkNotAvailable; - // break; - - // case WIFI_REASON_AUTH_EXPIRE: - // case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: - // case WIFI_REASON_BEACON_TIMEOUT: - // case WIFI_REASON_AUTH_FAIL: - // case WIFI_REASON_ASSOC_FAIL: - // case WIFI_REASON_HANDSHAKE_TIMEOUT: - // Status = WifiConnectionStatus_InvalidCredential; - // break; - - // default: - // Status = WifiConnectionStatus_UnspecifiedFailure; - // break; - // } - // break; - // } - - // // Get timeout - // NANOCLR_CHECK_HRESULT(stack.SetupTimeoutFromTicks(hbTimeout, timeout)); - - // // non-blocking wait allowing other threads to run while we wait for the Spi transaction to complete - // NANOCLR_CHECK_HRESULT( - // g_CLR_RT_ExecutionEngine.WaitEvents(stack.m_owningThread, *timeout, Event_Wifi_Station, eventResult)); - - // if (!eventResult) - // { - // // Timeout - // Status = WifiConnectionStatus_Timeout; - // break; - // } + // int connectResult = Network_Interface_Connect_Result(netIndex); + // if (connectResult >= 0) + // { + // // Map ESP32 wifi reason code to WifiConnectionStatus + // switch (connectResult) + // { + // case 0: + // Status = WifiConnectionStatus_Success; + // break; + + // case WIFI_REASON_NO_AP_FOUND: + // Status = WifiConnectionStatus_NetworkNotAvailable; + // break; + + // case WIFI_REASON_AUTH_EXPIRE: + // case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: + // case WIFI_REASON_BEACON_TIMEOUT: + // case WIFI_REASON_AUTH_FAIL: + // case WIFI_REASON_ASSOC_FAIL: + // case WIFI_REASON_HANDSHAKE_TIMEOUT: + // Status = WifiConnectionStatus_InvalidCredential; + // break; + + // default: + // Status = WifiConnectionStatus_UnspecifiedFailure; + // break; + // } + // break; + // } + + // // Get timeout + // NANOCLR_CHECK_HRESULT(stack.SetupTimeoutFromTicks(hbTimeout, timeout)); + + // // non-blocking wait allowing other threads to run while we wait for the Spi transaction to complete + // NANOCLR_CHECK_HRESULT( + // g_CLR_RT_ExecutionEngine.WaitEvents(stack.m_owningThread, *timeout, Event_Wifi_Station, + // eventResult)); + + // if (!eventResult) + // { + // // Timeout + // Status = WifiConnectionStatus_Timeout; + // break; + // } } // Return value to the managed application @@ -241,7 +250,7 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::GetNativeSca // // Temporary ap record storage // wifi_ap_record_t *ap_records = 0; // { - //CLR_RT_HeapBlock &top = stack.PushValueAndClear(); + // CLR_RT_HeapBlock &top = stack.PushValueAndClear(); // CLR_RT_HeapBlock_Array *array; // CLR_UINT8 *buf; // uint16_t number = 0; @@ -288,7 +297,7 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::GetNativeSca // platform_free(ap_records); // } - //NANOCLR_CLEANUP_END(); + // NANOCLR_CLEANUP_END(); NANOCLR_NOCLEANUP_NOLABEL(); } 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 382be93810..afd15d7a2d 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 @@ -8,6 +8,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////////// // !!! KEEP IN SYNC WITH System.Device.Wifi (in managed code) !!! // @@ -21,6 +22,44 @@ struct ScanRecord uint8_t cypherType; }; +HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeSetDeviceName___VOID__STRING( + CLR_RT_StackFrame &stack) +{ + NANOCLR_HEADER(); + { +#if defined(CONFIG_SOC_WIFI_SUPPORTED) || defined(CONFIG_SOC_WIRELESS_HOST_SUPPORTED) + { + // Get deviceName from args + const char *hostname = stack.Arg1().RecoverString(); + FAULT_ON_NULL(hostname); + + // Get adapter index to check it's valid. + int netIndex; + NANOCLR_CHECK_HRESULT(GetNetInterfaceIndex(stack, &netIndex)); + + // Get the default Wi-Fi STA esp_netif + // This is the default key used by ESP-IDF for the station interface + esp_netif_t *sta_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); + if (sta_netif == nullptr) + { + NANOCLR_SET_AND_LEAVE(CLR_E_FAIL); + } + + esp_err_t err = esp_netif_set_hostname(sta_netif, hostname); + if (err != ESP_OK) + { + NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION); + } + } +#else + { + NANOCLR_SET_AND_LEAVE(CLR_E_NOT_SUPPORTED); + } +#endif + } + NANOCLR_NOCLEANUP(); +} + HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::DisposeNative___VOID(CLR_RT_StackFrame &stack) { NANOCLR_HEADER();