From f9dc7e7491ad7db9870800e33faed2f8ab79d3b8 Mon Sep 17 00:00:00 2001 From: zwx Date: Wed, 4 Jun 2025 12:28:06 +0800 Subject: [PATCH] feat(openthread): support openthread cli console command register --- components/openthread/Kconfig | 7 ++++ .../openthread/include/esp_openthread_cli.h | 30 ++++++++++++-- .../openthread/src/esp_openthread_cli.c | 40 +++++++++++++++++-- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/components/openthread/Kconfig b/components/openthread/Kconfig index bca02ec1fb..afbd187b93 100644 --- a/components/openthread/Kconfig +++ b/components/openthread/Kconfig @@ -44,6 +44,13 @@ menu "OpenThread" default y help Select this option to enable Command-Line Interface in OpenThread. + + config OPENTHREAD_CONSOLE_COMMAND_PREFIX + string "The prefix of the openthread CLI command registered on the esp console" + default "ot" + help + A prefix string used before a Thread CLI command, allowing the ESP console to identify + it and delegate the remaining command to the OpenThread callback for processing. endmenu menu "Thread Core Features" diff --git a/components/openthread/include/esp_openthread_cli.h b/components/openthread/include/esp_openthread_cli.h index 554d0c755c..0bc7741a9c 100644 --- a/components/openthread/include/esp_openthread_cli.h +++ b/components/openthread/include/esp_openthread_cli.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -13,6 +13,14 @@ extern "C" { /** * @brief This function initializes the OpenThread command line interface(CLI). * + * @note There are two ways to initialize the OpenThread CLI: + * 1. By creating a dedicated task via `esp_openthread_cli_create_task` + * + * 2. By registering a console command with the ESP console via + * `esp_openthread_cli_console_command_register` + * If using this approach, the user must initialize the interface used + * by the console and also initialize esp_console manually. Additionally, + * the `host_connection_mode` should be set to `HOST_CONNECTION_MODE_NONE`. */ void esp_openthread_cli_init(void); @@ -34,11 +42,27 @@ esp_err_t esp_openthread_cli_input(const char *line); /** * @brief This function launches an exclusive loop for the OpenThread CLI. * - * @param[in] priority The priority of the created task. - * */ void esp_openthread_cli_create_task(void); +/** + * @brief This function registers an ESP Console command for the OpenThread CLI. + * + * @return + * - ESP_OK on success + * - ESP_ERR_NO_MEM if allocation has failed + */ +esp_err_t esp_openthread_cli_console_command_register(void); + +/** + * @brief This function deregisters the ESP Console command for the OpenThread CLI. + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG if command is not registered + */ +esp_err_t esp_openthread_cli_console_command_unregister(void); + #ifdef __cplusplus } #endif diff --git a/components/openthread/src/esp_openthread_cli.c b/components/openthread/src/esp_openthread_cli.c index b2a76a4df0..b7e1b933f9 100644 --- a/components/openthread/src/esp_openthread_cli.c +++ b/components/openthread/src/esp_openthread_cli.c @@ -1,14 +1,13 @@ /* - * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ -#include "esp_openthread_cli.h" #include #include - +#include "sdkconfig.h" #include "openthread/cli.h" #include "esp_check.h" @@ -16,6 +15,7 @@ #include "esp_log.h" #include "esp_openthread.h" #include "esp_openthread_common_macro.h" +#include "esp_openthread_cli.h" #include "esp_openthread_task_queue.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -63,6 +63,40 @@ esp_err_t esp_openthread_cli_input(const char *line) return esp_openthread_task_queue_post(line_handle_task, line_copy); } +static int ot_cli_console_callback(int argc, char **argv) +{ + char cli_cmd[OT_CLI_MAX_LINE_LENGTH] = {0}; + strncpy(cli_cmd, argv[1], sizeof(cli_cmd) - strlen(cli_cmd) - 1); + for (int i = 2; i < argc; i++) { + strncat(cli_cmd, " ", sizeof(cli_cmd) - strlen(cli_cmd) - 1); + strncat(cli_cmd, argv[i], sizeof(cli_cmd) - strlen(cli_cmd) - 1); + } + s_cli_task = xTaskGetCurrentTaskHandle(); + if (esp_openthread_cli_input(cli_cmd) == ESP_OK) { + xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); + } else { + printf("Openthread task is busy, failed to run command: %s\n", cli_cmd); + } + s_cli_task = NULL; + return 0; +} + +esp_err_t esp_openthread_cli_console_command_register(void) +{ + esp_console_cmd_t cmd = { + .command = CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX, + .help = "Execute `"CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX" ...` to run openthread cli", + .hint = NULL, + .func = ot_cli_console_callback, + }; + return esp_console_cmd_register(&cmd); +} + +esp_err_t esp_openthread_cli_console_command_unregister(void) +{ + return esp_console_cmd_deregister(CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX); +} + static void ot_cli_loop(void *context) { int ret = 0;