-
-
Notifications
You must be signed in to change notification settings - Fork 187
Wifi DeviceName has been added #3243
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughAdds a native Wi‑Fi adapter method to set the device hostname: header declaration, insertion into the platform-neutral native method table with an updated assembly CRC, and an ESP32 implementation that calls esp_netif_set_hostname on the STA netif. Changes
Sequence Diagram(s)mermaid Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
🧰 Additional context used🧠 Learnings (1)📚 Learning: 2024-09-25T11:28:38.536ZApplied to files:
🧬 Code graph analysis (1)src/System.Device.Wifi/sys_dev_wifi_native.cpp (1)
🔇 Additional comments (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/System.Device.Wifi/sys_dev_wifi_native.cpp(2 hunks)src/System.Device.Wifi/sys_dev_wifi_native.h(1 hunks)targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp(2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2024-10-12T19:00:39.000Z
Learnt from: josesimoes
Repo: nanoframework/nf-interpreter PR: 3023
File: targets/netcore/nanoFramework.nanoCLR/nanoCLR_native.cpp:191-225
Timestamp: 2024-10-12T19:00:39.000Z
Learning: When working with `nanoCLR_GetNativeAssemblyInformation`, fixed-size assembly names are required, so code that deals with variable-length names cannot be used.
Applied to files:
src/System.Device.Wifi/sys_dev_wifi_native.hsrc/System.Device.Wifi/sys_dev_wifi_native.cpp
📚 Learning: 2024-10-08T15:52:09.445Z
Learnt from: josesimoes
Repo: nanoframework/nf-interpreter PR: 3023
File: targets/netcore/nanoFramework.nanoCLR/nanoCLR_native.cpp:191-225
Timestamp: 2024-10-08T15:52:09.445Z
Learning: In `nanoCLR_GetNativeAssemblyInformation`, there is no need to return the number of bytes written, as the memory buffer is zeroed, making the string buffer terminated.
Applied to files:
src/System.Device.Wifi/sys_dev_wifi_native.h
🔇 Additional comments (3)
src/System.Device.Wifi/sys_dev_wifi_native.h (1)
69-69: LGTM! Method declaration follows the correct pattern.The native method declaration is properly formatted and consistently placed with other WifiAdapter methods.
src/System.Device.Wifi/sys_dev_wifi_native.cpp (1)
36-43: LGTM! Method lookup table correctly updated.The new method has been properly wired into the method lookup table. The CRC update at line 86 correctly reflects the assembly changes.
targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp (1)
11-11: LGTM! Required include for ESP-NETIF APIs.The
esp_netif.hinclude is necessary for the hostname configuration functionality.
...ets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp
Outdated
Show resolved
Hide resolved
| const char *hostname = stack.Arg1().RecoverString(); | ||
| FAULT_ON_NULL(hostname); | ||
|
|
||
| // Optional: ensure this adapter index is 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_FAIL); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
ESP-IDF esp_netif_set_hostname hostname length limits and character requirements
💡 Result:
-
Maximum length: esp_netif_set_hostname accepts a hostname up to 32 bytes (API docs). [1]
-
Character rules: esp_netif_set_hostname documentation does not specify character validation; in practice hostnames used on IP networks should follow the usual DNS/host rules (RFCs) — each label ≤ 63 octets, full name ≤ 255 octets, and labels normally contain only ASCII letters (A–Z, a–z), digits (0–9) and hyphen (“-”), and must not begin or end with a hyphen (RFC 1034 / RFC 1123). Some protocols (mDNS, DHCP, NetBIOS) may impose additional rules. [2][3][4]
Notes:
- Because esp_netif limits the stored hostname to 32 bytes, you must keep within that size even though DNS allows longer names. See the ESP-IDF docs for the 32-byte limit. [1]
Sources:
- ESP-IDF esp_netif_set_hostname API documentation. [link] [1]
- RFC 1123 (host name length requirements). [link] [2]
- RFC 1034 (label character rules). [link] [3]
- RFC 6762 (mDNS name length note). [link] [4]
Add hostname validation for length and empty string before calling ESP-IDF API.
The hostname string is passed directly to esp_netif_set_hostname without validation. ESP-IDF enforces a maximum hostname length of 32 bytes; exceeding this limit could cause the API to fail silently or produce unexpected behavior. Additionally, an empty string should be rejected before making the API call.
Add validation for:
- Maximum length: 32 bytes (ESP-IDF limit)
- Non-empty string
🤖 Prompt for AI Agents
In
targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp
around lines 31 to 50, validate the hostname before calling
esp_netif_set_hostname: after FAULT_ON_NULL(hostname) compute the byte length
(use strlen to measure bytes), reject empty strings (length == 0) and reject
lengths greater than 32 bytes (ESP-IDF limit); if invalid, return a clear error
(e.g., NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER)) instead of calling
esp_netif_set_hostname, otherwise proceed as before.
|
@zandiarash please also use the proper PR template. |
|
Also, please adjust all the native devices. It's find to throw an exception but the build of other devices than ESP32 will break. |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Description
I have an ESP32-32D N4 chip
When it connects to the hotspot as a client the device (default) name is nanodevice_31708
I want to change that but I did not found any way to do so (I read the documentation and samples).
I decided to implement that feature as Arduino has.
I’ve changed these projects
Motivation and Context
Now we can set the device name before it connects to any hotspot, router, access point and etc.
Please read these :
nanoframework/System.Device.Wifi#322
nanoframework/Samples#446
How Has This Been Tested?
I should mention that I am not familiar with C++ and after I changed nf-interpreter project I flashed my chip like this
nanoff --platform esp32 --target ESP32_REV3 --serialport COM6 --update --clrfile "D:\Sources Electronic\Nanoframework - Copy\nf-interpreter\build\nanoCLR.bin"Screenshots
Before :


After :
Types of changes
Checklist:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.