From 3b56f22d72c0f60c08b3bdf9eb40cf8189ac876c Mon Sep 17 00:00:00 2001 From: Xu Si Yu Date: Thu, 23 Oct 2025 17:57:29 +0800 Subject: [PATCH 1/2] feat(openthread): support custom netif config during initialization --- components/openthread/include/esp_openthread.h | 2 +- .../openthread/include/esp_openthread_types.h | 10 ++++++++++ components/openthread/src/esp_openthread.cpp | 16 ++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/components/openthread/include/esp_openthread.h b/components/openthread/include/esp_openthread.h index ce96d186569..7ce38a58844 100644 --- a/components/openthread/include/esp_openthread.h +++ b/components/openthread/include/esp_openthread.h @@ -102,7 +102,7 @@ esp_err_t esp_openthread_mainloop_exit(void); * - ESP_ERR_INVALID_STATE if already initialized * */ -esp_err_t esp_openthread_start(esp_openthread_platform_config_t *config); +esp_err_t esp_openthread_start(const esp_openthread_config_t *config); /** * @brief This function performs OpenThread stack and platform driver deinitialization and delete the handle task. diff --git a/components/openthread/include/esp_openthread_types.h b/components/openthread/include/esp_openthread_types.h index 364d596100b..cd3f75f5a6d 100644 --- a/components/openthread/include/esp_openthread_types.h +++ b/components/openthread/include/esp_openthread_types.h @@ -11,6 +11,7 @@ #include #include "esp_event_base.h" +#include "esp_netif_types.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "driver/spi_slave.h" @@ -198,6 +199,15 @@ typedef struct { esp_openthread_port_config_t port_config; /*!< The port configuration */ } esp_openthread_platform_config_t; +/** + * @brief The OpenThread configuration + * + */ +typedef struct { + esp_netif_config_t netif_config; /*!< The netif configuration */ + esp_openthread_platform_config_t platform_config; /*!< The platform configuration */ +} esp_openthread_config_t; + /** * @brief The OpenThread rcp failure handler * diff --git a/components/openthread/src/esp_openthread.cpp b/components/openthread/src/esp_openthread.cpp index d3de546eff2..22802fc3e23 100644 --- a/components/openthread/src/esp_openthread.cpp +++ b/components/openthread/src/esp_openthread.cpp @@ -235,15 +235,14 @@ esp_err_t esp_openthread_deinit(void) static void ot_task_worker(void *aContext) { - esp_openthread_platform_config_t* config = (esp_openthread_platform_config_t *)aContext; + const esp_openthread_config_t* config = *(esp_openthread_config_t **)aContext; // Initialize the OpenThread stack - ESP_ERROR_CHECK(esp_openthread_init(config)); + ESP_ERROR_CHECK(esp_openthread_init(&(config->platform_config))); #if CONFIG_OPENTHREAD_FTD || CONFIG_OPENTHREAD_MTD - esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD(); - esp_netif_t *openthread_netif = esp_netif_new(&cfg); + esp_netif_t *openthread_netif = esp_netif_new(&(config->netif_config)); assert(openthread_netif != NULL); - ESP_ERROR_CHECK(esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(config))); + ESP_ERROR_CHECK(esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&(config->platform_config)))); #endif #if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC @@ -256,10 +255,10 @@ static void ot_task_worker(void *aContext) esp_openthread_cli_console_command_register(); #endif // CONFIG_OPENTHREAD_CLI - xSemaphoreGive(s_ot_syn_semaphore); #if CONFIG_OPENTHREAD_RADIO otAppNcpInit(esp_openthread_get_instance()); #endif + xSemaphoreGive(s_ot_syn_semaphore); // Run the main loop esp_openthread_launch_mainloop(); @@ -283,12 +282,13 @@ static void ot_task_worker(void *aContext) vTaskDelay(portMAX_DELAY); } -esp_err_t esp_openthread_start(esp_openthread_platform_config_t *config) +esp_err_t esp_openthread_start(const esp_openthread_config_t *config) { + assert(config); ESP_RETURN_ON_FALSE(s_ot_syn_semaphore == NULL, ESP_ERR_INVALID_STATE, OT_PLAT_LOG_TAG, "OpenThread has been initialized"); s_ot_syn_semaphore = xSemaphoreCreateBinary(); ESP_RETURN_ON_FALSE(s_ot_syn_semaphore != NULL, ESP_ERR_INVALID_STATE, OT_PLAT_LOG_TAG, "Failed to create s_ot_syn_semaphore"); - assert(xTaskCreate(ot_task_worker, CONFIG_OPENTHREAD_TASK_NAME, CONFIG_OPENTHREAD_TASK_SIZE, config, CONFIG_OPENTHREAD_TASK_PRIORITY, &s_ot_task_handle) == pdPASS); + assert(xTaskCreate(ot_task_worker, CONFIG_OPENTHREAD_TASK_NAME, CONFIG_OPENTHREAD_TASK_SIZE, &config, CONFIG_OPENTHREAD_TASK_PRIORITY, &s_ot_task_handle) == pdPASS); xSemaphoreTake(s_ot_syn_semaphore, portMAX_DELAY); return ESP_OK; } From 709101d86ac254a16d7a718ab63f3ac4ac98f636 Mon Sep 17 00:00:00 2001 From: Xu Si Yu Date: Fri, 24 Oct 2025 14:16:58 +0800 Subject: [PATCH 2/2] feat(openthread): update examples for API changes --- examples/openthread/ot_br/main/esp_ot_br.c | 11 +++++++---- examples/openthread/ot_cli/main/esp_ot_cli.c | 12 ++++++++---- examples/openthread/ot_rcp/main/esp_ot_rcp.c | 11 +++++++---- .../deep_sleep/main/esp_ot_sleepy_device.c | 11 +++++++---- .../light_sleep/main/esp_ot_sleepy_device.c | 11 +++++++---- examples/openthread/ot_trel/main/esp_ot_trel.c | 11 +++++++---- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/examples/openthread/ot_br/main/esp_ot_br.c b/examples/openthread/ot_br/main/esp_ot_br.c index 10da75c855a..7cfcca642bb 100644 --- a/examples/openthread/ot_br/main/esp_ot_br.c +++ b/examples/openthread/ot_br/main/esp_ot_br.c @@ -103,10 +103,13 @@ void app_main(void) ot_external_coexist_init(); #endif - static esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + static esp_openthread_config_t config = { + .netif_config = ESP_NETIF_DEFAULT_OPENTHREAD(), + .platform_config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }, }; ESP_ERROR_CHECK(esp_openthread_start(&config)); diff --git a/examples/openthread/ot_cli/main/esp_ot_cli.c b/examples/openthread/ot_cli/main/esp_ot_cli.c index c2717a4c211..ee83bcc22de 100644 --- a/examples/openthread/ot_cli/main/esp_ot_cli.c +++ b/examples/openthread/ot_cli/main/esp_ot_cli.c @@ -24,6 +24,7 @@ #include "esp_netif_types.h" #include "esp_openthread.h" #include "esp_openthread_lock.h" +#include "esp_openthread_netif_glue.h" #include "esp_openthread_types.h" #include "esp_ot_config.h" #include "esp_vfs_eventfd.h" @@ -59,10 +60,13 @@ void app_main(void) ot_console_start(); #endif - esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + static esp_openthread_config_t config = { + .netif_config = ESP_NETIF_DEFAULT_OPENTHREAD(), + .platform_config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }, }; ESP_ERROR_CHECK(esp_openthread_start(&config)); diff --git a/examples/openthread/ot_rcp/main/esp_ot_rcp.c b/examples/openthread/ot_rcp/main/esp_ot_rcp.c index 765fef58a24..fddd7766846 100644 --- a/examples/openthread/ot_rcp/main/esp_ot_rcp.c +++ b/examples/openthread/ot_rcp/main/esp_ot_rcp.c @@ -50,10 +50,13 @@ void app_main(void) ot_external_coexist_init(); #endif - static esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + static esp_openthread_config_t config = { + .netif_config = {0}, + .platform_config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }, }; ESP_ERROR_CHECK(esp_openthread_start(&config)); diff --git a/examples/openthread/ot_sleepy_device/deep_sleep/main/esp_ot_sleepy_device.c b/examples/openthread/ot_sleepy_device/deep_sleep/main/esp_ot_sleepy_device.c index ad633b9bd02..4c5670e94de 100644 --- a/examples/openthread/ot_sleepy_device/deep_sleep/main/esp_ot_sleepy_device.c +++ b/examples/openthread/ot_sleepy_device/deep_sleep/main/esp_ot_sleepy_device.c @@ -166,10 +166,13 @@ void app_main(void) ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config)); ot_deep_sleep_init(); - esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + static esp_openthread_config_t config = { + .netif_config = ESP_NETIF_DEFAULT_OPENTHREAD(), + .platform_config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }, }; ESP_ERROR_CHECK(esp_openthread_start(&config)); esp_netif_set_default_netif(esp_openthread_get_netif()); diff --git a/examples/openthread/ot_sleepy_device/light_sleep/main/esp_ot_sleepy_device.c b/examples/openthread/ot_sleepy_device/light_sleep/main/esp_ot_sleepy_device.c index 633f40b094d..b54b2680424 100644 --- a/examples/openthread/ot_sleepy_device/light_sleep/main/esp_ot_sleepy_device.c +++ b/examples/openthread/ot_sleepy_device/light_sleep/main/esp_ot_sleepy_device.c @@ -163,10 +163,13 @@ void app_main(void) ot_console_start(); #endif - static esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + static esp_openthread_config_t config = { + .netif_config = ESP_NETIF_DEFAULT_OPENTHREAD(), + .platform_config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }, }; ESP_ERROR_CHECK(esp_openthread_start(&config)); esp_netif_set_default_netif(esp_openthread_get_netif()); diff --git a/examples/openthread/ot_trel/main/esp_ot_trel.c b/examples/openthread/ot_trel/main/esp_ot_trel.c index fc59497c2aa..d45b211b7b6 100644 --- a/examples/openthread/ot_trel/main/esp_ot_trel.c +++ b/examples/openthread/ot_trel/main/esp_ot_trel.c @@ -70,10 +70,13 @@ void app_main(void) ot_console_start(); #endif - static esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + static esp_openthread_config_t config = { + .netif_config = ESP_NETIF_DEFAULT_OPENTHREAD(), + .platform_config = { + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), + .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), + .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), + }, }; ESP_ERROR_CHECK(esp_openthread_start(&config)); esp_netif_set_default_netif(esp_openthread_get_netif());