From 4824bfba181f72db8955a259d81456a0aacaed2c Mon Sep 17 00:00:00 2001 From: chendejin Date: Thu, 31 Aug 2023 15:59:23 +0800 Subject: [PATCH 1/4] ESP32: Add otcli command in matter console --- examples/lighting-app/esp32/main/main.cpp | 4 ++ .../esp32/sdkconfig.defaults.esp32h2 | 2 +- .../openthread_cli_register.cpp | 70 +++++++++++++++++++ .../shell_extension/openthread_cli_register.h | 24 +++++++ src/platform/ESP32/OpenthreadLauncher.c | 68 ++++++++++++++++++ src/platform/ESP32/OpenthreadLauncher.h | 1 + 6 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 examples/platform/esp32/shell_extension/openthread_cli_register.cpp create mode 100644 examples/platform/esp32/shell_extension/openthread_cli_register.h diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 280131263efa96..790bd9b7867dff 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -32,6 +32,7 @@ #include "freertos/task.h" #include "nvs_flash.h" #include "shell_extension/launch.h" +#include "shell_extension/openthread_cli_register.h" #include #include #include @@ -156,6 +157,9 @@ extern "C" void app_main() ESP_LOGI(TAG, "=================================================="); #if CONFIG_ENABLE_CHIP_SHELL +#if CONFIG_OPENTHREAD_CLI + chip::RegisterOpenThreadCliCommands(); +#endif chip::LaunchShell(); #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI diff --git a/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 b/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 index 11d5991c353b20..7a1b53cba9a474 100644 --- a/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 +++ b/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 @@ -30,7 +30,7 @@ CONFIG_OPENTHREAD_ENABLED=y CONFIG_OPENTHREAD_SRP_CLIENT=y CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y -CONFIG_OPENTHREAD_CLI=n +# CONFIG_OPENTHREAD_CLI=n CONFIG_OPENTHREAD_DNS_CLIENT=y # Disable lwip ipv6 autoconfig diff --git a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp new file mode 100644 index 00000000000000..c82bd33849edd2 --- /dev/null +++ b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp @@ -0,0 +1,70 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "sdkconfig.h" +#if CONFIG_OPENTHREAD_CLI +#include "esp_check.h" +#include "esp_err.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "openthread_cli_register.h" +#include "platform/ESP32/OpenthreadLauncher.h" +#include + +#define CLI_INPUT_BUFF_LENGTH 256u +namespace chip { + +static CHIP_ERROR OtcliHandler(int argc, char * argv[]) +{ + /* the beginning of command "matter esp ot cli" has already been removed */ + char * cli_str = (char *) malloc(CLI_INPUT_BUFF_LENGTH); + memset(cli_str, 0, CLI_INPUT_BUFF_LENGTH); + uint8_t len = 0; + for (size_t i = 0; i < (size_t) argc; ++i) + { + len = len + strlen(argv[i]) + 1; + if (len > CLI_INPUT_BUFF_LENGTH - 1) + { + free(cli_str); + return CHIP_ERROR_INTERNAL; + } + strcat(cli_str, argv[i]); + strcat(cli_str, " "); + } + + QueueHandle_t cli_trans_queue = get_cli_transmit_task_queue(); + if (!cli_trans_queue || xQueueSend(cli_trans_queue, &cli_str, portMAX_DELAY) != pdTRUE) + { + free(cli_str); + return CHIP_ERROR_INTERNAL; + } + return CHIP_NO_ERROR; +} + +void RegisterOpenThreadCliCommands() +{ + static const chip::Shell::shell_command_t cmds[] = { + { + .cmd_func = OtcliHandler, + .cmd_name = "otcli", + .cmd_help = "OpenThread cli commands", + }, + }; + int cmds_num = sizeof(cmds) / sizeof(chip::Shell::shell_command_t); + chip::Shell::Engine::Root().RegisterCommands(cmds, cmds_num); +} +} // namespace chip +#endif // CONFIG_OPENTHREAD_CLI diff --git a/examples/platform/esp32/shell_extension/openthread_cli_register.h b/examples/platform/esp32/shell_extension/openthread_cli_register.h new file mode 100644 index 00000000000000..4811146415c593 --- /dev/null +++ b/examples/platform/esp32/shell_extension/openthread_cli_register.h @@ -0,0 +1,24 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +namespace chip { + +void RegisterOpenThreadCliCommands(); + +} // namespace chip diff --git a/src/platform/ESP32/OpenthreadLauncher.c b/src/platform/ESP32/OpenthreadLauncher.c index 4eaa674e4d8c48..72bc6ece63cb76 100644 --- a/src/platform/ESP32/OpenthreadLauncher.c +++ b/src/platform/ESP32/OpenthreadLauncher.c @@ -20,18 +20,82 @@ #include "esp_netif.h" #include "esp_netif_types.h" #include "esp_openthread.h" +#include "esp_openthread_cli.h" #include "esp_openthread_lock.h" #include "esp_openthread_netif_glue.h" #include "esp_openthread_types.h" #include "esp_vfs_eventfd.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "openthread/cli.h" #include "openthread/instance.h" #include "openthread/logging.h" #include "openthread/tasklet.h" static esp_netif_t * openthread_netif = NULL; static esp_openthread_platform_config_t * s_platform_config = NULL; +static TaskHandle_t cli_transmit_task = NULL; +static QueueHandle_t cli_transmit_task_queue = NULL; + +QueueHandle_t get_cli_transmit_task_queue(void) +{ + return cli_transmit_task_queue; +} + +static int cli_output_callback(void * context, const char * format, va_list args) +{ + int ret = 0; + char prompt_check[3]; + vsnprintf(prompt_check, sizeof(prompt_check), format, args); + if (!strncmp(prompt_check, "> ", sizeof(prompt_check)) && cli_transmit_task) + { + xTaskNotifyGive(cli_transmit_task); + } + else + { + ret = vprintf(format, args); + } + return ret; +} + +static void esp_openthread_matter_cli_init(void) +{ + otCliInit(esp_openthread_get_instance(), cli_output_callback, NULL); +} + +static void cli_transmit_worker(void * context) +{ + cli_transmit_task_queue = xQueueCreate(8, sizeof(char **)); + if (!cli_transmit_task_queue) + { + return; + } + char * command_line = NULL; + while (true) + { + if (xQueueReceive(cli_transmit_task_queue, &command_line, portMAX_DELAY) == pdTRUE) + { + if (command_line) + { + esp_openthread_cli_input(command_line); + free(command_line); + } + else + { + continue; + } + xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); + } + } + vQueueDelete(cli_transmit_task_queue); + vTaskDelete(NULL); +} + +static esp_err_t cli_command_transmit_task(void) +{ + xTaskCreate(cli_transmit_worker, "ocli_trans", 3072, xTaskGetCurrentTaskHandle(), 4, &cli_transmit_task); + return ESP_OK; +} static esp_netif_t * init_openthread_netif(const esp_openthread_platform_config_t * config) { @@ -84,6 +148,10 @@ esp_err_t openthread_init_stack(void) assert(s_platform_config); // Initialize the OpenThread stack ESP_ERROR_CHECK(esp_openthread_init(s_platform_config)); +#if CONFIG_OPENTHREAD_CLI + esp_openthread_matter_cli_init(); + cli_command_transmit_task(); +#endif // Initialize the esp_netif bindings openthread_netif = init_openthread_netif(s_platform_config); free(s_platform_config); diff --git a/src/platform/ESP32/OpenthreadLauncher.h b/src/platform/ESP32/OpenthreadLauncher.h index a7f4ef02747eb2..033a55518fd56b 100644 --- a/src/platform/ESP32/OpenthreadLauncher.h +++ b/src/platform/ESP32/OpenthreadLauncher.h @@ -27,6 +27,7 @@ extern "C" { esp_err_t set_openthread_platform_config(esp_openthread_platform_config_t * config); esp_err_t openthread_init_stack(void); esp_err_t openthread_launch_task(void); +QueueHandle_t get_cli_transmit_task_queue(void); #ifdef __cplusplus } From 13f09f42e90d03ad6438f78a01769a6ab313dea6 Mon Sep 17 00:00:00 2001 From: chendejin Date: Wed, 6 Sep 2023 15:18:39 +0800 Subject: [PATCH 2/4] add function cli_transmit_task_post --- .../openthread_cli_register.cpp | 15 +++++++------ src/platform/ESP32/BUILD.gn | 2 +- ...hreadLauncher.c => OpenthreadLauncher.cpp} | 21 ++++++++++++------- src/platform/ESP32/OpenthreadLauncher.h | 12 +++-------- 4 files changed, 25 insertions(+), 25 deletions(-) rename src/platform/ESP32/{OpenthreadLauncher.c => OpenthreadLauncher.cpp} (88%) diff --git a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp index c82bd33849edd2..46484a4916013f 100644 --- a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp +++ b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp @@ -23,6 +23,7 @@ #include "openthread_cli_register.h" #include "platform/ESP32/OpenthreadLauncher.h" #include +#include #define CLI_INPUT_BUFF_LENGTH 256u namespace chip { @@ -30,27 +31,25 @@ namespace chip { static CHIP_ERROR OtcliHandler(int argc, char * argv[]) { /* the beginning of command "matter esp ot cli" has already been removed */ - char * cli_str = (char *) malloc(CLI_INPUT_BUFF_LENGTH); - memset(cli_str, 0, CLI_INPUT_BUFF_LENGTH); + std::unique_ptr cli_str(new char[CLI_INPUT_BUFF_LENGTH]); + memset(cli_str.get(), 0, CLI_INPUT_BUFF_LENGTH); uint8_t len = 0; for (size_t i = 0; i < (size_t) argc; ++i) { len = len + strlen(argv[i]) + 1; if (len > CLI_INPUT_BUFF_LENGTH - 1) { - free(cli_str); return CHIP_ERROR_INTERNAL; } - strcat(cli_str, argv[i]); - strcat(cli_str, " "); + strcat(cli_str.get(), argv[i]); + strcat(cli_str.get(), " "); } - QueueHandle_t cli_trans_queue = get_cli_transmit_task_queue(); - if (!cli_trans_queue || xQueueSend(cli_trans_queue, &cli_str, portMAX_DELAY) != pdTRUE) + if (cli_transmit_task_post(cli_str) != CHIP_NO_ERROR) { - free(cli_str); return CHIP_ERROR_INTERNAL; } + cli_str.release(); return CHIP_NO_ERROR; } diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 1d27940db829fa..a9c0879a0e8a76 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -153,7 +153,7 @@ static_library("ESP32") { "../OpenThread/GenericNetworkCommissioningThreadDriver.cpp", "../OpenThread/GenericNetworkCommissioningThreadDriver.h", "../OpenThread/OpenThreadUtils.cpp", - "OpenthreadLauncher.c", + "OpenthreadLauncher.cpp", "OpenthreadLauncher.h", "ThreadStackManagerImpl.cpp", "ThreadStackManagerImpl.h", diff --git a/src/platform/ESP32/OpenthreadLauncher.c b/src/platform/ESP32/OpenthreadLauncher.cpp similarity index 88% rename from src/platform/ESP32/OpenthreadLauncher.c rename to src/platform/ESP32/OpenthreadLauncher.cpp index 72bc6ece63cb76..81d81de0683d41 100644 --- a/src/platform/ESP32/OpenthreadLauncher.c +++ b/src/platform/ESP32/OpenthreadLauncher.cpp @@ -31,15 +31,21 @@ #include "openthread/instance.h" #include "openthread/logging.h" #include "openthread/tasklet.h" +#include +#include static esp_netif_t * openthread_netif = NULL; static esp_openthread_platform_config_t * s_platform_config = NULL; static TaskHandle_t cli_transmit_task = NULL; static QueueHandle_t cli_transmit_task_queue = NULL; -QueueHandle_t get_cli_transmit_task_queue(void) +CHIP_ERROR cli_transmit_task_post(std::unique_ptr & cli_str) { - return cli_transmit_task_queue; + if (!cli_transmit_task_queue || xQueueSend(cli_transmit_task_queue, &cli_str, portMAX_DELAY) != pdTRUE) + { + return CHIP_ERROR_INTERNAL; + } + return CHIP_NO_ERROR; } static int cli_output_callback(void * context, const char * format, va_list args) @@ -65,20 +71,21 @@ static void esp_openthread_matter_cli_init(void) static void cli_transmit_worker(void * context) { - cli_transmit_task_queue = xQueueCreate(8, sizeof(char **)); + cli_transmit_task_queue = xQueueCreate(8, sizeof(std::unique_ptr)); if (!cli_transmit_task_queue) { + vTaskDelete(NULL); return; } - char * command_line = NULL; + while (true) { + std::unique_ptr command_line(nullptr); if (xQueueReceive(cli_transmit_task_queue, &command_line, portMAX_DELAY) == pdTRUE) { if (command_line) { - esp_openthread_cli_input(command_line); - free(command_line); + esp_openthread_cli_input(command_line.get()); } else { @@ -93,7 +100,7 @@ static void cli_transmit_worker(void * context) static esp_err_t cli_command_transmit_task(void) { - xTaskCreate(cli_transmit_worker, "ocli_trans", 3072, xTaskGetCurrentTaskHandle(), 4, &cli_transmit_task); + xTaskCreate(cli_transmit_worker, "ocli_trans", 3072, xTaskGetCurrentTaskHandle(), 5, &cli_transmit_task); return ESP_OK; } diff --git a/src/platform/ESP32/OpenthreadLauncher.h b/src/platform/ESP32/OpenthreadLauncher.h index 033a55518fd56b..ba1b115a7355d6 100644 --- a/src/platform/ESP32/OpenthreadLauncher.h +++ b/src/platform/ESP32/OpenthreadLauncher.h @@ -19,16 +19,10 @@ #include #include - -#ifdef __cplusplus -extern "C" { -#endif +#include +#include esp_err_t set_openthread_platform_config(esp_openthread_platform_config_t * config); esp_err_t openthread_init_stack(void); esp_err_t openthread_launch_task(void); -QueueHandle_t get_cli_transmit_task_queue(void); - -#ifdef __cplusplus -} -#endif +CHIP_ERROR cli_transmit_task_post(std::unique_ptr & cli_str); From 23b31a7f29829055e273087a8123dde4e7c73e81 Mon Sep 17 00:00:00 2001 From: chendejin Date: Fri, 22 Sep 2023 12:09:37 +0800 Subject: [PATCH 3/4] use char ptr in xQueueSend --- .../shell_extension/openthread_cli_register.cpp | 3 +-- src/platform/ESP32/OpenthreadLauncher.cpp | 17 ++++++++++------- src/platform/ESP32/OpenthreadLauncher.h | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp index 46484a4916013f..146783b96b9886 100644 --- a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp +++ b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp @@ -45,11 +45,10 @@ static CHIP_ERROR OtcliHandler(int argc, char * argv[]) strcat(cli_str.get(), " "); } - if (cli_transmit_task_post(cli_str) != CHIP_NO_ERROR) + if (cli_transmit_task_post(std::move(cli_str)) != CHIP_NO_ERROR) { return CHIP_ERROR_INTERNAL; } - cli_str.release(); return CHIP_NO_ERROR; } diff --git a/src/platform/ESP32/OpenthreadLauncher.cpp b/src/platform/ESP32/OpenthreadLauncher.cpp index 81d81de0683d41..45367f7ffef491 100644 --- a/src/platform/ESP32/OpenthreadLauncher.cpp +++ b/src/platform/ESP32/OpenthreadLauncher.cpp @@ -39,12 +39,14 @@ static esp_openthread_platform_config_t * s_platform_config = NULL; static TaskHandle_t cli_transmit_task = NULL; static QueueHandle_t cli_transmit_task_queue = NULL; -CHIP_ERROR cli_transmit_task_post(std::unique_ptr & cli_str) +CHIP_ERROR cli_transmit_task_post(std::unique_ptr && cli_str) { - if (!cli_transmit_task_queue || xQueueSend(cli_transmit_task_queue, &cli_str, portMAX_DELAY) != pdTRUE) + char * cmd = cli_str.get(); + if (!cli_transmit_task_queue || xQueueSend(cli_transmit_task_queue, &cmd, portMAX_DELAY) != pdTRUE) { return CHIP_ERROR_INTERNAL; } + cli_str.release(); return CHIP_NO_ERROR; } @@ -71,7 +73,7 @@ static void esp_openthread_matter_cli_init(void) static void cli_transmit_worker(void * context) { - cli_transmit_task_queue = xQueueCreate(8, sizeof(std::unique_ptr)); + cli_transmit_task_queue = xQueueCreate(8, sizeof(char *)); if (!cli_transmit_task_queue) { vTaskDelete(NULL); @@ -80,12 +82,13 @@ static void cli_transmit_worker(void * context) while (true) { - std::unique_ptr command_line(nullptr); - if (xQueueReceive(cli_transmit_task_queue, &command_line, portMAX_DELAY) == pdTRUE) + char * cmd = NULL; + if (xQueueReceive(cli_transmit_task_queue, &cmd, portMAX_DELAY) == pdTRUE) { - if (command_line) + if (cmd) { - esp_openthread_cli_input(command_line.get()); + std::unique_ptr cmd_ptr(cmd); + esp_openthread_cli_input(cmd_ptr.get()); } else { diff --git a/src/platform/ESP32/OpenthreadLauncher.h b/src/platform/ESP32/OpenthreadLauncher.h index ba1b115a7355d6..0a10b486d74afc 100644 --- a/src/platform/ESP32/OpenthreadLauncher.h +++ b/src/platform/ESP32/OpenthreadLauncher.h @@ -25,4 +25,4 @@ esp_err_t set_openthread_platform_config(esp_openthread_platform_config_t * config); esp_err_t openthread_init_stack(void); esp_err_t openthread_launch_task(void); -CHIP_ERROR cli_transmit_task_post(std::unique_ptr & cli_str); +CHIP_ERROR cli_transmit_task_post(std::unique_ptr && cli_str); From f9b1e2a6fa4a0932982b9dd34fb9df62db591fbb Mon Sep 17 00:00:00 2001 From: chendejin Date: Sat, 7 Oct 2023 11:23:25 +0800 Subject: [PATCH 4/4] add constant for otcli transmit task --- examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 | 2 +- .../esp32/shell_extension/openthread_cli_register.cpp | 2 +- src/platform/ESP32/OpenthreadLauncher.cpp | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 b/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 index 7a1b53cba9a474..11d5991c353b20 100644 --- a/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 +++ b/examples/lighting-app/esp32/sdkconfig.defaults.esp32h2 @@ -30,7 +30,7 @@ CONFIG_OPENTHREAD_ENABLED=y CONFIG_OPENTHREAD_SRP_CLIENT=y CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y -# CONFIG_OPENTHREAD_CLI=n +CONFIG_OPENTHREAD_CLI=n CONFIG_OPENTHREAD_DNS_CLIENT=y # Disable lwip ipv6 autoconfig diff --git a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp index 146783b96b9886..bc8ccae7e2af00 100644 --- a/examples/platform/esp32/shell_extension/openthread_cli_register.cpp +++ b/examples/platform/esp32/shell_extension/openthread_cli_register.cpp @@ -30,7 +30,7 @@ namespace chip { static CHIP_ERROR OtcliHandler(int argc, char * argv[]) { - /* the beginning of command "matter esp ot cli" has already been removed */ + /* the beginning of command "matter otcli" has already been removed */ std::unique_ptr cli_str(new char[CLI_INPUT_BUFF_LENGTH]); memset(cli_str.get(), 0, CLI_INPUT_BUFF_LENGTH); uint8_t len = 0; diff --git a/src/platform/ESP32/OpenthreadLauncher.cpp b/src/platform/ESP32/OpenthreadLauncher.cpp index 45367f7ffef491..8c76c1db809082 100644 --- a/src/platform/ESP32/OpenthreadLauncher.cpp +++ b/src/platform/ESP32/OpenthreadLauncher.cpp @@ -38,6 +38,8 @@ static esp_netif_t * openthread_netif = NULL; static esp_openthread_platform_config_t * s_platform_config = NULL; static TaskHandle_t cli_transmit_task = NULL; static QueueHandle_t cli_transmit_task_queue = NULL; +static constexpr uint16_t OTCLI_TRANSMIT_TASK_STACK_SIZE = 1024; +static constexpr UBaseType_t OTCLI_TRANSMIT_TASK_PRIORITY = 5; CHIP_ERROR cli_transmit_task_post(std::unique_ptr && cli_str) { @@ -103,7 +105,8 @@ static void cli_transmit_worker(void * context) static esp_err_t cli_command_transmit_task(void) { - xTaskCreate(cli_transmit_worker, "ocli_trans", 3072, xTaskGetCurrentTaskHandle(), 5, &cli_transmit_task); + xTaskCreate(cli_transmit_worker, "otcli_trans", OTCLI_TRANSMIT_TASK_STACK_SIZE, xTaskGetCurrentTaskHandle(), + OTCLI_TRANSMIT_TASK_PRIORITY, &cli_transmit_task); return ESP_OK; }