feat(openthread): support openthread cli console command register

This commit is contained in:
zwx
2025-06-04 12:28:06 +08:00
committed by Xu Si Yu
parent 747f9448bc
commit f9dc7e7491
3 changed files with 71 additions and 6 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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 <stdio.h>
#include <string.h>
#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;