Skip to content

Conversation

@zandiarash
Copy link

@zandiarash zandiarash commented Nov 30, 2025

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

  • NanoSamples
  • nf-interpreter
  • System.Device.Wifi

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 :
x
After :
x

Types of changes

  • Improvement (non-breaking change that improves a sample)
  • Bug fix (fixes an issue with a current sample)
  • New Sample (adds a new sample)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Documentation/comment (fixes and improvements documentation related)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Summary by CodeRabbit

  • New Features
    • Added capability to set the device hostname via the WifiAdapter API, allowing custom device names for network identification and easier discovery on Wi‑Fi networks.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 30, 2025

Walkthrough

Adds 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

Cohort / File(s) Summary
Native interface declaration
src/System.Device.Wifi/sys_dev_wifi_native.h
Added NativeSetDeviceName___VOID__STRING declaration to Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.
Method registration & assembly identity
src/System.Device.Wifi/sys_dev_wifi_native.cpp
Inserted NativeSetDeviceName___VOID__STRING into method_lookup (placed after GetNativeScanReport___SZARRAY_U1), added an extra NULL slot earlier in the table, and updated g_CLR_AssemblyNative_System_Device_Wifi CRC from 0x00A058C6 to 0x030E2768.
ESP32 platform implementation
targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp
Implemented NativeSetDeviceName___VOID__STRING(CLR_RT_StackFrame&): read/validate hostname, obtain STA netif via esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), call esp_netif_set_hostname, return failure on error; added #include <esp_netif.h>.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Managed as Managed (C#)
participant CLR as nanoCLR native entry
participant ESP as esp_netif / ESP-IDF
Managed->>CLR: Invoke NativeSetDeviceName(string hostname)
CLR->>CLR: Parse & validate hostname; get net interface index
CLR->>ESP: esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")
ESP-->>CLR: netif handle or NULL
alt netif found
CLR->>ESP: esp_netif_set_hostname(netif, hostname)
ESP-->>CLR: success / error
else netif not found
CLR-->>CLR: return failure
end
CLR-->>Managed: return (void) or propagate error

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify esp_netif lookup key ("WIFI_STA_DEF") and null handling.
  • Confirm hostname validation meets ESP-IDF constraints.
  • Ensure method registration order and CRC update are consistent across targets.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Wifi DeviceName has been added' clearly describes the main change—adding the ability to set WiFi device name. It directly relates to the changeset's primary objective.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 63b0b13 and b1deeab.

📒 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)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/System.Device.Wifi/sys_dev_wifi_native.h
  • targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-09-25T11:28:38.536Z
Learnt from: josesimoes
Repo: nanoframework/nf-interpreter PR: 3023
File: targets/netcore/nanoFramework.nanoCLR/nanoCLR_native.cpp:191-225
Timestamp: 2024-09-25T11:28:38.536Z
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.cpp
🧬 Code graph analysis (1)
src/System.Device.Wifi/sys_dev_wifi_native.cpp (1)
targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp (2)
  • NativeSetDeviceName___VOID__STRING (25-53)
  • NativeSetDeviceName___VOID__STRING (25-26)
🔇 Additional comments (2)
src/System.Device.Wifi/sys_dev_wifi_native.cpp (2)

36-45: Verify method table indices match the updated native header

The extra NULL at Line 36 and the new NativeSetDeviceName___VOID__STRING entry at Line 43 look consistent with the surrounding WifiAdapter methods, but the method_lookup array must stay in perfect lockstep with the IDs/order defined in sys_dev_wifi_native.h and the managed assembly.

Please double‑check that:

  • The position of NativeSetDeviceName___VOID__STRING matches its method index in the generated native header.
  • The additional NULL at Line 36 corresponds to a real method slot in that header (i.e., you’re not accidentally shifting everything by one).

If you haven’t already, I recommend regenerating this table using the usual nanoFramework tooling rather than editing it by hand, to avoid subtle index mismatches.


83-89: Confirm the updated Wi‑Fi native assembly CRC and version alignment

The CRC has been changed to 0x030E2768 while keeping the version at { 100, 0, 6, 4 }. For this to be safe at runtime, the following must hold:

  • The CRC here must match the one computed from the updated System.Device.Wifi managed assembly.
  • The version tuple must stay in sync with the managed assembly version used by the other related PRs.

Please confirm that this CRC came from the official nanoFramework native assembly generation process and that it matches the managed side you intend to ship with this firmware.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between 8f92554 and b7a2467.

📒 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.h
  • src/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.h include is necessary for the hostname configuration functionality.

Comment on lines +31 to +50
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 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:

  1. ESP-IDF esp_netif_set_hostname API documentation. [link] [1]
  2. RFC 1123 (host name length requirements). [link] [2]
  3. RFC 1034 (label character rules). [link] [3]
  4. 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.

@Ellerbach
Copy link
Member

@zandiarash please also use the proper PR template.

@Ellerbach
Copy link
Member

Also, please adjust all the native devices. It's find to throw an exception but the build of other devices than ESP32 will break.

@Ellerbach
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants