From e38b5491f452df2ff5e09209ea0859f3444d4158 Mon Sep 17 00:00:00 2001 From: Isaac Benitez Date: Fri, 7 Apr 2023 11:45:13 -0700 Subject: [PATCH] Add device name env variable for web workflow --- docs/environment.rst | 4 ++++ docs/workflows.md | 4 +++- supervisor/shared/web_workflow/web_workflow.c | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/environment.rst b/docs/environment.rst index 58ada402c6..f569d5e6a4 100644 --- a/docs/environment.rst +++ b/docs/environment.rst @@ -74,6 +74,10 @@ CIRCUITPY_WEB_API_PORT ~~~~~~~~~~~~~~~~~~~~~~ TCP port number used for the web HTTP API. Defaults to 80 when omitted. +CIRCUITPY_WEB_INSTANCE_NAME +~~~~~~~~~~~~~~~~~~~ +Name the board advertises as for the WEB workflow. Defaults to human readable board name if omitted. + CIRCUITPY_WIFI_PASSWORD ~~~~~~~~~~~~~~~~~~~~~~~ Wi-Fi password used to auto connect to CIRCUITPY_WIFI_SSID. diff --git a/docs/workflows.md b/docs/workflows.md index 29229bd00e..453b425491 100644 --- a/docs/workflows.md +++ b/docs/workflows.md @@ -72,7 +72,8 @@ Read-only characteristic that returns the UTF-8 encoded version string. The web workflow is depends on adding Wi-Fi credentials into the `settings.toml` file. The keys are `CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD`. Once these are defined, CircuitPython will automatically connect to the network and start the webserver used for the workflow. The webserver -is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS. +is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS. The name +of the board as advertised to the network can be overridden by `CIRCUITPY_WEB_INSTANCE_NAME`. Here is an example `/settings.toml`: @@ -86,6 +87,7 @@ CIRCUITPY_WIFI_PASSWORD="secretpassword" CIRCUITPY_WEB_API_PASSWORD="passw0rd" CIRCUITPY_WEB_API_PORT=80 +CIRCUITPY_WEB_INSTANCE_NAME="" ``` MDNS is used to resolve [`circuitpython.local`](http://circuitpython.local) to a device specific diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index 468787269a..b640578e20 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -269,6 +269,7 @@ void supervisor_start_web_workflow(void) { char ssid[33]; char password[64]; + char web_instance_name[50]; os_getenv_err_t result = common_hal_os_getenv_str("CIRCUITPY_WIFI_SSID", ssid, sizeof(ssid)); if (result != GETENV_OK) { @@ -283,6 +284,11 @@ void supervisor_start_web_workflow(void) { return; } + result = common_hal_os_getenv_str("CIRCUITPY_WEB_INSTANCE_NAME", web_instance_name, sizeof(web_instance_name)); + if (result != GETENV_OK || web_instance_name[0] == '\0') { + strcpy(web_instance_name, MICROPY_HW_BOARD_NAME); + } + if (!common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) { common_hal_wifi_init(false); common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true); @@ -329,7 +335,8 @@ void supervisor_start_web_workflow(void) { mdns_server_construct(&mdns, true); mdns.base.type = &mdns_server_type; if (!common_hal_mdns_server_deinited(&mdns)) { - common_hal_mdns_server_set_instance_name(&mdns, MICROPY_HW_BOARD_NAME); + char *instance_name = strndup(web_instance_name, strlen(web_instance_name)); + common_hal_mdns_server_set_instance_name(&mdns, instance_name); } } if (!common_hal_mdns_server_deinited(&mdns)) { @@ -796,9 +803,11 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request * mp_print_t _socket_print = {socket, _print_chunk}; const char *hostname = ""; + const char *instance_name = ""; #if CIRCUITPY_MDNS if (!common_hal_mdns_server_deinited(&mdns)) { hostname = common_hal_mdns_server_get_hostname(&mdns); + instance_name = common_hal_mdns_server_get_instance_name(&mdns); } #endif _update_encoded_ip(); @@ -807,13 +816,13 @@ static void _reply_with_version_json(socketpool_socket_obj_t *socket, _request * "{\"web_api_version\": 2, " "\"version\": \"" MICROPY_GIT_TAG "\", " "\"build_date\": \"" MICROPY_BUILD_DATE "\", " - "\"board_name\": \"" MICROPY_HW_BOARD_NAME "\", " + "\"board_name\": \"%s\", " "\"mcu_name\": \"" MICROPY_HW_MCU_NAME "\", " "\"board_id\": \"" CIRCUITPY_BOARD_ID "\", " "\"creator_id\": %u, " "\"creation_id\": %u, " "\"hostname\": \"%s\", " - "\"port\": %d, ", CIRCUITPY_CREATOR_ID, CIRCUITPY_CREATION_ID, hostname, web_api_port, _our_ip_encoded); + "\"port\": %d, ", instance_name, CIRCUITPY_CREATOR_ID, CIRCUITPY_CREATION_ID, hostname, web_api_port, _our_ip_encoded); #if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH > 0 uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH]; common_hal_mcu_processor_get_uid(raw_id);