From 9ab34b8f39cfc81463b855c5f0ab722d4ffd15dd Mon Sep 17 00:00:00 2001 From: Xu Si Yu Date: Thu, 13 Aug 2026 12:47:09 +0800 Subject: [PATCH] feat(openthread): reorganize source file structure --- .../private_include/esp_openthread_instance.h | 56 +++++++++++++++++++ components/openthread/src/esp_openthread.cpp | 15 ++--- .../src/esp_openthread_instance.cpp | 47 ++++++++++++++++ .../src/esp_openthread_platform.cpp | 6 -- 4 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 components/openthread/private_include/esp_openthread_instance.h create mode 100644 components/openthread/src/esp_openthread_instance.cpp diff --git a/components/openthread/private_include/esp_openthread_instance.h b/components/openthread/private_include/esp_openthread_instance.h new file mode 100644 index 00000000000..c7ce7684b63 --- /dev/null +++ b/components/openthread/private_include/esp_openthread_instance.h @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#include "openthread/instance.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Initialize OpenThread instance(s) for the selected build (single or multiple). + * + * @return true on success, false otherwise. + */ +bool esp_openthread_instances_init(void); + +/** + * @brief Finalize OpenThread instance(s) for the selected build (single or multiple). + * + * @param[in] instance Instance provided by the caller (may be unused in multi-instance builds). + */ +void esp_openthread_instances_deinit(otInstance *instance); + +/** + * @brief Check whether tasklets are pending. + * + * @param[in] instance Instance provided by the caller (may be unused in multi-instance builds). + */ +bool esp_openthread_tasklets_are_pending(otInstance *instance); + +/** + * @brief Process pending tasklets. + * + * @param[in] instance Instance provided by the caller (may be unused in multi-instance builds). + */ +void esp_openthread_tasklets_process(otInstance *instance); + +/** + * @brief Initialize the NCP/RCP application for the selected instance mode. + * + * @param[in] instance Instance provided by the caller (may be unused in multi-instance builds). + * + * @note Only used when CONFIG_OPENTHREAD_RADIO is enabled. + */ +void esp_openthread_ncp_app_init(otInstance *instance); + +#ifdef __cplusplus +} +#endif diff --git a/components/openthread/src/esp_openthread.cpp b/components/openthread/src/esp_openthread.cpp index f7708a731aa..c0e1473ae75 100644 --- a/components/openthread/src/esp_openthread.cpp +++ b/components/openthread/src/esp_openthread.cpp @@ -12,8 +12,8 @@ #include "esp_openthread_common_macro.h" #include "esp_openthread_cli.h" #include "esp_openthread_dns64.h" +#include "esp_openthread_instance.h" #include "esp_openthread_lock.h" -#include "esp_openthread_ncp.h" #include "esp_openthread_netif_glue.h" #include "esp_openthread_platform.h" #include "esp_openthread_sleep.h" @@ -26,7 +26,6 @@ #include "openthread/instance.h" #include "openthread/logging.h" #include "openthread/netdata.h" -#include "openthread/tasklet.h" #include "openthread/thread.h" #if CONFIG_OPENTHREAD_FTD @@ -81,7 +80,7 @@ esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *config) "Failed to initialize OpenThread platform driver"); esp_openthread_lock_acquire(portMAX_DELAY); esp_err_t ret = ESP_OK; - ESP_GOTO_ON_FALSE(otInstanceInitSingle() != NULL, ESP_FAIL, exit, OT_PLAT_LOG_TAG, + ESP_GOTO_ON_FALSE(esp_openthread_instances_init(), ESP_FAIL, exit, OT_PLAT_LOG_TAG, "Failed to initialize OpenThread instance"); #if CONFIG_OPENTHREAD_DNS64_CLIENT ESP_GOTO_ON_ERROR(esp_openthread_dns64_client_init(), exit, OT_PLAT_LOG_TAG, @@ -203,7 +202,7 @@ esp_err_t esp_openthread_launch_mainloop(void) esp_openthread_lock_acquire(portMAX_DELAY); esp_openthread_platform_update(&mainloop); - if (otTaskletsArePending(instance)) { + if (esp_openthread_tasklets_are_pending(instance)) { mainloop.timeout.tv_sec = 0; mainloop.timeout.tv_usec = 0; } @@ -222,9 +221,7 @@ esp_err_t esp_openthread_launch_mainloop(void) if (result >= 0) { esp_openthread_lock_acquire(portMAX_DELAY); error = esp_openthread_platform_process(instance, &mainloop); - while (otTaskletsArePending(instance)) { - otTaskletsProcess(instance); - } + esp_openthread_tasklets_process(instance); esp_openthread_lock_release(); if (error != ESP_OK) { ESP_LOGE(OT_PLAT_LOG_TAG, "esp_openthread_platform_process failed"); @@ -244,7 +241,7 @@ esp_err_t esp_openthread_launch_mainloop(void) esp_err_t esp_openthread_deinit(void) { - otInstanceFinalize(esp_openthread_get_instance()); + esp_openthread_instances_deinit(esp_openthread_get_instance()); return esp_openthread_platform_deinit(); } @@ -271,7 +268,7 @@ static void ot_task_worker(void *aContext) #endif // CONFIG_OPENTHREAD_CLI #if CONFIG_OPENTHREAD_RADIO - otAppNcpInit(esp_openthread_get_instance()); + esp_openthread_ncp_app_init(esp_openthread_get_instance()); #endif xSemaphoreGive(s_ot_syn_semaphore); diff --git a/components/openthread/src/esp_openthread_instance.cpp b/components/openthread/src/esp_openthread_instance.cpp new file mode 100644 index 00000000000..c52daf509c7 --- /dev/null +++ b/components/openthread/src/esp_openthread_instance.cpp @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "sdkconfig.h" +#include "core/instance/instance.hpp" +#include "esp_openthread.h" +#include "esp_openthread_instance.h" +#include "esp_openthread_ncp.h" +#include "openthread/instance.h" +#include "openthread/tasklet.h" + +bool esp_openthread_instances_init(void) +{ + return otInstanceInitSingle() != NULL; +} + +void esp_openthread_instances_deinit(otInstance *instance) +{ + otInstanceFinalize(instance); +} + +bool esp_openthread_tasklets_are_pending(otInstance *instance) +{ + return otTaskletsArePending(instance); +} + +void esp_openthread_tasklets_process(otInstance *instance) +{ + while (otTaskletsArePending(instance)) { + otTaskletsProcess(instance); + } +} + +#if CONFIG_OPENTHREAD_RADIO +void esp_openthread_ncp_app_init(otInstance *instance) +{ + otAppNcpInit(instance); +} +#endif + +otInstance *esp_openthread_get_instance(void) +{ + return (otInstance *)&ot::Instance::Get(); +} diff --git a/components/openthread/src/esp_openthread_platform.cpp b/components/openthread/src/esp_openthread_platform.cpp index a2431f82d04..991ab07267d 100644 --- a/components/openthread/src/esp_openthread_platform.cpp +++ b/components/openthread/src/esp_openthread_platform.cpp @@ -20,7 +20,6 @@ #include "esp_partition.h" #include "common/code_utils.hpp" #include "common/logging.hpp" -#include "core/instance/instance.hpp" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "openthread/cli.h" @@ -166,11 +165,6 @@ exit: return ret; } -otInstance *esp_openthread_get_instance(void) -{ - return (otInstance *)&ot::Instance::Get(); -} - esp_err_t esp_openthread_platform_deinit(void) { ESP_RETURN_ON_FALSE(s_openthread_platform_initialized, ESP_ERR_INVALID_STATE, OT_PLAT_LOG_TAG,