diff --git a/components/esp_matter_console/CMakeLists.txt b/components/esp_matter_console/CMakeLists.txt index 017e44fed..6b24e6aa5 100644 --- a/components/esp_matter_console/CMakeLists.txt +++ b/components/esp_matter_console/CMakeLists.txt @@ -1,6 +1,7 @@ set(srcs_list) if (CONFIG_ENABLE_CHIP_SHELL) - list(APPEND srcs_list esp_matter_console.cpp esp_matter_console_diagnostics.cpp esp_matter_console_wifi.cpp) + list(APPEND srcs_list esp_matter_console.cpp esp_matter_console_diagnostics.cpp + esp_matter_console_wifi.cpp esp_matter_console_otcli.cpp) endif() idf_component_register(SRCS ${srcs_list} INCLUDE_DIRS . diff --git a/components/esp_matter_console/esp_matter_console.h b/components/esp_matter_console/esp_matter_console.h index 60a4728af..b5dc52236 100644 --- a/components/esp_matter_console/esp_matter_console.h +++ b/components/esp_matter_console/esp_matter_console.h @@ -137,5 +137,14 @@ esp_err_t diagnostics_register_commands(); */ esp_err_t wifi_register_commands(); +/** Add Thread Cli Commands + * + * Adds the default Thread Cli commands. + * + * @return ESP_OK on success. + * @return error in case of failure. + */ +esp_err_t otcli_register_commands(); + } // namespace console } // namespace esp_matter diff --git a/components/esp_matter_console/esp_matter_console_otcli.cpp b/components/esp_matter_console/esp_matter_console_otcli.cpp new file mode 100644 index 000000000..9ac204cb1 --- /dev/null +++ b/components/esp_matter_console/esp_matter_console_otcli.cpp @@ -0,0 +1,62 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// 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 +#if CONFIG_OPENTHREAD_CLI +#include +#include +#include +#include +#include +#include + +#define CLI_INPUT_BUFF_LENGTH 256u +namespace esp_matter { +namespace console { + +static esp_err_t otcli_handler(int argc, char *argv[]) +{ + /* the beginning of command "matter esp 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; + for (size_t i = 0; i < (size_t)argc; ++i) { + len = len + strlen(argv[i]) + 1; + if (len > CLI_INPUT_BUFF_LENGTH - 1) { + return ESP_FAIL; + } + strcat(cli_str.get(), argv[i]); + if (i < (size_t)argc - 1) { + strcat(cli_str.get(), " "); + } + } + + if (cli_transmit_task_post(std::move(cli_str)) != CHIP_NO_ERROR) { + return ESP_FAIL; + } + return ESP_OK; +} + +esp_err_t otcli_register_commands() +{ + static const command_t command = { + .name = "ot_cli", + .description = "Openthread commands. Usage: matter esp otcli .", + .handler = otcli_handler, + }; + return add_commands(&command, 1); +} +} // namespace console +} // namespace esp_matter +#endif // CONFIG_OPENTHREAD_CLI diff --git a/examples/light/main/app_main.cpp b/examples/light/main/app_main.cpp index 3958b0666..f4fbdc373 100644 --- a/examples/light/main/app_main.cpp +++ b/examples/light/main/app_main.cpp @@ -218,6 +218,9 @@ extern "C" void app_main() #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); esp_matter::console::wifi_register_commands(); +#if CONFIG_OPENTHREAD_CLI + esp_matter::console::otcli_register_commands(); +#endif esp_matter::console::init(); #endif }