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);