Skip to content

Commit 526a6f3

Browse files
committed
feat(openthread): support openthread cli console command register
1 parent efb4d57 commit 526a6f3

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

components/openthread/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ menu "OpenThread"
4444
default y
4545
help
4646
Select this option to enable Command-Line Interface in OpenThread.
47+
48+
config OPENTHREAD_CONSOLE_COMMAND_PREFIX
49+
string "The prefix of the openthread CLI command registered on the esp console"
50+
default "ot"
51+
help
52+
A prefix string used before a Thread CLI command, allowing the ESP console to identify
53+
it and delegate the remaining command to the OpenThread callback for processing.
4754
endmenu
4855

4956
menu "Thread Core Features"

components/openthread/include/esp_openthread_cli.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -13,6 +13,14 @@ extern "C" {
1313
/**
1414
* @brief This function initializes the OpenThread command line interface(CLI).
1515
*
16+
* @note There are two ways to initialize the OpenThread CLI:
17+
* 1. By creating a dedicated task via `esp_openthread_cli_create_task`
18+
*
19+
* 2. By registering a console command with the ESP console via
20+
* `esp_openthread_cli_console_command_register`
21+
* If using this approach, the user must initialize the interface used
22+
* by the console and also initialize esp_console manually. Additionally,
23+
* the `host_connection_mode` should be set to `HOST_CONNECTION_MODE_NONE`.
1624
*/
1725
void esp_openthread_cli_init(void);
1826

@@ -34,11 +42,27 @@ esp_err_t esp_openthread_cli_input(const char *line);
3442
/**
3543
* @brief This function launches an exclusive loop for the OpenThread CLI.
3644
*
37-
* @param[in] priority The priority of the created task.
38-
*
3945
*/
4046
void esp_openthread_cli_create_task(void);
4147

48+
/**
49+
* @brief This function registers an ESP Console command for the OpenThread CLI.
50+
*
51+
* @return
52+
* - ESP_OK on success
53+
* - ESP_ERR_NO_MEM if allocation has failed
54+
*/
55+
esp_err_t esp_openthread_cli_console_command_register(void);
56+
57+
/**
58+
* @brief This function deregisters the ESP Console command for the OpenThread CLI.
59+
*
60+
* @return
61+
* - ESP_OK on success
62+
* - ESP_ERR_INVALID_ARG if command is not registered
63+
*/
64+
esp_err_t esp_openthread_cli_console_command_unregister(void);
65+
4266
#ifdef __cplusplus
4367
}
4468
#endif

components/openthread/src/esp_openthread_cli.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include "esp_openthread_cli.h"
87

98
#include <stdio.h>
109
#include <string.h>
11-
10+
#include "sdkconfig.h"
1211
#include "openthread/cli.h"
1312

1413
#include "esp_check.h"
1514
#include "esp_err.h"
1615
#include "esp_log.h"
1716
#include "esp_openthread.h"
1817
#include "esp_openthread_common_macro.h"
18+
#include "esp_openthread_cli.h"
1919
#include "esp_openthread_task_queue.h"
2020
#include "freertos/FreeRTOS.h"
2121
#include "freertos/task.h"
@@ -63,6 +63,40 @@ esp_err_t esp_openthread_cli_input(const char *line)
6363
return esp_openthread_task_queue_post(line_handle_task, line_copy);
6464
}
6565

66+
static int ot_cli_console_callback(int argc, char **argv)
67+
{
68+
char cli_cmd[OT_CLI_MAX_LINE_LENGTH] = {0};
69+
strncpy(cli_cmd, argv[1], sizeof(cli_cmd) - strlen(cli_cmd) - 1);
70+
for (int i = 2; i < argc; i++) {
71+
strncat(cli_cmd, " ", sizeof(cli_cmd) - strlen(cli_cmd) - 1);
72+
strncat(cli_cmd, argv[i], sizeof(cli_cmd) - strlen(cli_cmd) - 1);
73+
}
74+
s_cli_task = xTaskGetCurrentTaskHandle();
75+
if (esp_openthread_cli_input(cli_cmd) == ESP_OK) {
76+
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
77+
} else {
78+
printf("Openthread task is busy, failed to run command: %s\n", cli_cmd);
79+
}
80+
s_cli_task = NULL;
81+
return 0;
82+
}
83+
84+
esp_err_t esp_openthread_cli_console_command_register(void)
85+
{
86+
esp_console_cmd_t cmd = {
87+
.command = CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX,
88+
.help = "Execute `"CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX" ...` to run openthread cli",
89+
.hint = NULL,
90+
.func = ot_cli_console_callback,
91+
};
92+
return esp_console_cmd_register(&cmd);
93+
}
94+
95+
esp_err_t esp_openthread_cli_console_command_unregister(void)
96+
{
97+
return esp_console_cmd_deregister(CONFIG_OPENTHREAD_CONSOLE_COMMAND_PREFIX);
98+
}
99+
66100
static void ot_cli_loop(void *context)
67101
{
68102
int ret = 0;

0 commit comments

Comments
 (0)