Skip to content
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

Remove CONFIG_LWIP_LOCAL_HOSTNAME from S3 boards #9656

Merged
merged 5 commits into from
Oct 1, 2024

Conversation

RetiredWizard
Copy link

As discussed in #9583 this removes the CONFIG_LWIP_LOCAL_HOSTNAME parameter from the sdkconfig files of all ESP32-S3 boards.

I actually just commented out the configuration setting but can easily update this to completely remove the lines if that is preferred.

@anecdata
Copy link
Member

For completeness: I checked an S2 board with an empty sdkconfig file, and it looks like boards without CONFIG_LWIP_LOCAL_HOSTNAME defined will have the hostname espressif, unless changed in user code (wifi.radio.hostname). The hostname shows up in the router via DHCP, and may be used by some other protocols.

@RetiredWizard
Copy link
Author

I submitted this not really knowing as much as I should have about core Circuitpython WiFi. It seems to me if the variable is used somewhere that is visible via Circuitpython we should probably leave it alone.

Feel free to re-open if there's a reason to pursue this.

I made these changes using a script so it would be relatively painless, if for some reason we wanted to expand to other chips or make a different change to the files/default values.

@anecdata
Copy link
Member

anecdata commented Sep 24, 2024

I didn't mean to shut this down, only highlight what the new hostname would be. We haven't been consistent with having CONFIG_LWIP_LOCAL_HOSTNAME or not, or how to name it. I don't know if there's a use case to rely on an expected default where the old expectation wouldn't be met... I'd be a little surprised, especially since multiple of the same board would look identical anyway. I'm perfectly OK with Scott's and Dan's assessment that it isn't needed.

@tannewt
Copy link
Member

tannewt commented Sep 24, 2024

I do want to get rid of these settings so that our IDF builds are more uniform across boards. I think we should set it to the board id by default though. That's basically what folks use this setting for anyway.

@tannewt
Copy link
Member

tannewt commented Sep 24, 2024

Perhaps we should unify this with the MDNS name too. It could be cpy-board_name-MAC.

@RetiredWizard RetiredWizard reopened this Sep 24, 2024
@RetiredWizard
Copy link
Author

Any pointers as to where the initialization should take place. It looks like common_hal_wifi_init happens when wifi is imported which sounds too late to me.

@anecdata
Copy link
Member

anecdata commented Sep 24, 2024

I would think common_hal_wifi_init is fine. There is no wifi Station or AP, or wifi network access, possible until that time, and user code can't access the hostname without importing wifi. It should happen before web workflow or auto-connect start up, but that's enforced I think by web workflow calling common_hal_wifi_init.

@RetiredWizard
Copy link
Author

RetiredWizard commented Sep 30, 2024

I worked a while on trying to get ports\espressif\common-hal\wifi\__init__.c to work with mp_obj_t/Q_STR variables but wasn't making much progress so I thought I could get some help if I posted this as a proof of concept 😁

@RetiredWizard
Copy link
Author

RetiredWizard commented Sep 30, 2024

I was concerned that if someone set the hostname using wifi.radio.hostname that this default code would replace it when the interface was reset.

I haven't figured out if there is a way to reset the network interface in code yet, but I did some quick testing and it seems that wifi.radio.hostname survives a ctrl-d reset without being set back to this new default value. I haven't seen the host name set by wifi.radio.hostname show up on my router yet, but I'm thinking there's some sort of dhcp timeout that has to happen first.

@anecdata
Copy link
Member

anecdata commented Sep 30, 2024

I'm not sure what level of reset or deinit would clear the hostname, or if CircuitPython has such a path invokable by the user.

If there is a way to reset the interface, thereby ordinarily clearing or reverting the hostname, I can see pros and cons to preserving the custom hostname vs. letting it be lost. A user may want a clean interface and would expect to set a new hostname. OTOH, it's only one line to set a new hostname if the previous custom hostname survives. If it survives and isn't re-set by the user, anything expecting the previous custom hostname isn't disrupted.

So I don't think it's a big issue either way.

I do see the wifi.radio.hostname = blahblah to show up in the router right away once there's a new wifi connection...

Please note that when the hostname is altered after interface started/connected the changes would only be reflected once the interface restarts/reconnects

https://docs.espressif.com/projects/esp-idf/en/v5.3.1/esp32/api-reference/network/esp_netif.html#_CPPv422esp_netif_set_hostnameP11esp_netif_tPKc

@anecdata
Copy link
Member

anecdata commented Sep 30, 2024

Neither wifi.radio.enabled = False nor wifi.radio.stop_station() clear the custom hostname.

In 9.1.4, `supervisor.reload()` will clear the custom hostname back to `CONFIG_LWIP_LOCAL_HOSTNAME`...
import wifi
import time
import supervisor

print(wifi.radio.hostname)
wifi.radio.hostname = "blahblah"
print(wifi.radio.hostname)
time.sleep(5)
supervisor.reload()
Adafruit CircuitPython 9.1.4 on 2024-09-17; Adafruit QT Py ESP32-S3 4MB Flash 2MB PSRAM with ESP32S3
>>> import microcontroller ; microcontroller.on_next_reset(microcontroller.RunMode.NORMAL) ; microcontroller.reset()

[11:25:41.279] Disconnected
[11:25:42.282] Warning: Could not open tty device (No such file or directory)
[11:25:42.282] Waiting for tty device..
[11:25:44.311] Connected

Code done running.
soft reboot

Auto-reload is off.
code.py output:
espressif-esp32s3
blahblah

Code done running.
soft reboot

Auto-reload is off.
code.py output:
espressif-esp32s3
blahblah

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you!

@tannewt tannewt merged commit 66e289d into adafruit:main Oct 1, 2024
216 checks passed
@RetiredWizard
Copy link
Author

I didn't really think this was ready, it does work but the following code segment isn't optimized for Micropython/Circuitpython in terms of variable/memory use:

    char cpy_default_hostname[80];
    uint8_t mac[6];
    esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
    sprintf(cpy_default_hostname, "cpy_%s_%x", CIRCUITPY_BOARD_ID, (unsigned int)mac);
    const char *default_lwip_local_hostname = cpy_default_hostname;
    ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname));

@tannewt
Copy link
Member

tannewt commented Oct 1, 2024

Not-ready code should be in a draft PR then. I think that's fine because esp_netif_set_hostname will copy the string input.

@RetiredWizard
Copy link
Author

Sorry, I should have drafted it.... I'll see if there's anything I can do to optimize it and if so, submit another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants