feat(openthread): support custom netif config during initialization

This commit is contained in:
Xu Si Yu
2025-10-27 11:59:19 +08:00
parent e7f11e65a7
commit 3022c0ea51
3 changed files with 19 additions and 9 deletions
@@ -102,7 +102,7 @@ esp_err_t esp_openthread_mainloop_exit(void);
* - ESP_ERR_INVALID_STATE if already initialized * - 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. * @brief This function performs OpenThread stack and platform driver deinitialization and delete the handle task.
@@ -11,6 +11,7 @@
#include <sys/select.h> #include <sys/select.h>
#include "esp_event_base.h" #include "esp_event_base.h"
#include "esp_netif_types.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/spi_master.h" #include "driver/spi_master.h"
#include "driver/spi_slave.h" #include "driver/spi_slave.h"
@@ -199,6 +200,15 @@ typedef struct {
esp_openthread_port_config_t port_config; /*!< The port configuration */ esp_openthread_port_config_t port_config; /*!< The port configuration */
} esp_openthread_platform_config_t; } 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 * @brief The OpenThread rcp failure handler
* *
+8 -8
View File
@@ -235,15 +235,14 @@ esp_err_t esp_openthread_deinit(void)
static void ot_task_worker(void *aContext) 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 // 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 #if CONFIG_OPENTHREAD_FTD || CONFIG_OPENTHREAD_MTD
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD(); esp_netif_t *openthread_netif = esp_netif_new(&(config->netif_config));
esp_netif_t *openthread_netif = esp_netif_new(&cfg);
assert(openthread_netif != NULL); 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 #endif
#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC #if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
@@ -256,10 +255,10 @@ static void ot_task_worker(void *aContext)
esp_openthread_cli_console_command_register(); esp_openthread_cli_console_command_register();
#endif // CONFIG_OPENTHREAD_CLI #endif // CONFIG_OPENTHREAD_CLI
xSemaphoreGive(s_ot_syn_semaphore);
#if CONFIG_OPENTHREAD_RADIO #if CONFIG_OPENTHREAD_RADIO
otAppNcpInit(esp_openthread_get_instance()); otAppNcpInit(esp_openthread_get_instance());
#endif #endif
xSemaphoreGive(s_ot_syn_semaphore);
// Run the main loop // Run the main loop
esp_openthread_launch_mainloop(); esp_openthread_launch_mainloop();
@@ -284,12 +283,13 @@ static void ot_task_worker(void *aContext)
vTaskDelay(portMAX_DELAY); 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"); 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(); 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"); 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); xSemaphoreTake(s_ot_syn_semaphore, portMAX_DELAY);
return ESP_OK; return ESP_OK;
} }