diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 46424f21a01a5d..d727c7385cae36 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -72,7 +72,7 @@ if (NOT CONFIG_USE_MINIMAL_MDNS) endif() if (CONFIG_ENABLE_CHIP_SHELL) - chip_gn_arg_append("chip_build_shell_lib" "true") + chip_gn_arg_append("chip_build_libshell" "true") endif() diff --git a/config/nrfconnect/chip-gn/BUILD.gn b/config/nrfconnect/chip-gn/BUILD.gn index 79852ad6bfc266..d72432012f41cc 100644 --- a/config/nrfconnect/chip-gn/BUILD.gn +++ b/config/nrfconnect/chip-gn/BUILD.gn @@ -20,7 +20,6 @@ import("${chip_root}/build/chip/tests.gni") assert(current_os == "zephyr") declare_args() { - chip_build_libshell = false chip_build_pw_rpc_lib = false chip_build_zephyr_shell = false } @@ -32,10 +31,6 @@ group("nrfconnect") { deps += [ "${chip_root}/src:tests" ] } - if (chip_build_libshell) { - deps += [ "${chip_root}/src/lib/shell" ] - } - if (chip_build_zephyr_shell) { deps += [ "${chip_root}/src/platform/nrfconnect/shell:chip-zephyr-shell" ] } diff --git a/examples/lock-app/esp32/.gitignore b/examples/lock-app/esp32/.gitignore new file mode 100644 index 00000000000000..a90ab6f1e57d5a --- /dev/null +++ b/examples/lock-app/esp32/.gitignore @@ -0,0 +1,3 @@ +/build/ +/sdkconfig +/sdkconfig.old diff --git a/examples/shell/esp32/main/main.cpp b/examples/shell/esp32/main/main.cpp index 882fc8d6c5eabf..a02cacc376a9d1 100644 --- a/examples/shell/esp32/main/main.cpp +++ b/examples/shell/esp32/main/main.cpp @@ -25,12 +25,44 @@ #include "linenoise/linenoise.h" #include "nvs_flash.h" #include "support/CHIPMem.h" +#include "support/ErrorStr.h" #include -#include +#include #include #include +class ShellLineArgs +{ +public: + ShellLineArgs(char * line, TaskHandle_t source_task) : m_line(line), m_source_task(source_task) {} + char * GetLine() { return m_line; } + void WaitShellProcessDone() { ulTaskNotifyTake(pdTRUE, portMAX_DELAY); } + void NotifyShellProcessDone() { xTaskNotifyGive(m_source_task); } + +private: + char * m_line; + TaskHandle_t m_source_task; +}; + +static void process_shell_line(intptr_t context) +{ + ShellLineArgs * shellArgs = reinterpret_cast(context); + int ret; + esp_console_run(shellArgs->GetLine(), &ret); + if (ret) + { + printf("Error: %s\r\n", chip::ErrorStr(ret)); + } + else + { + printf("Done\r\n"); + } + + linenoiseFree(shellArgs->GetLine()); + shellArgs->NotifyShellProcessDone(); +} + static void chip_shell_task(void * args) { chip::Platform::MemoryInit(); @@ -43,34 +75,20 @@ static void chip_shell_task(void * args) { const char * prompt = LOG_COLOR_I "> " LOG_RESET_COLOR; char * line = linenoise(prompt); + printf("\r\n"); if (line == NULL || strlen(line) == 0) { continue; } + ShellLineArgs shellArgs(line, xTaskGetCurrentTaskHandle()); linenoiseHistoryAdd(line); - int ret; - esp_console_run(line, &ret); - if (ret) - { - char errorStr[160]; - bool errorStrFound = chip::FormatCHIPError(errorStr, sizeof(errorStr), ret); - if (!errorStrFound) - { - errorStr[0] = 0; - } - printf("Error: %s\n", errorStr); - } - else - { - printf("Done\n"); - } - - linenoiseFree(line); + chip::DeviceLayer::PlatformMgr().ScheduleWork(process_shell_line, reinterpret_cast(&shellArgs)); + shellArgs.WaitShellProcessDone(); } } extern "C" void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - xTaskCreate(&chip_shell_task, "chip_shell", 8192, NULL, 5, NULL); + xTaskCreate(&chip_shell_task, "chip_shell", 4096, NULL, 5, NULL); } diff --git a/examples/shell/nrfconnect/CMakeLists.txt b/examples/shell/nrfconnect/CMakeLists.txt index ed1de6326726c1..83cf26b62d5029 100644 --- a/examples/shell/nrfconnect/CMakeLists.txt +++ b/examples/shell/nrfconnect/CMakeLists.txt @@ -35,13 +35,10 @@ target_include_directories(app PRIVATE target_sources(app PRIVATE ${APP_ROOT}/shell_common/globals.cpp - ${APP_ROOT}/shell_common/cmd_base64.cpp - ${APP_ROOT}/shell_common/cmd_device.cpp ${APP_ROOT}/shell_common/cmd_misc.cpp ${APP_ROOT}/shell_common/cmd_otcli.cpp ${APP_ROOT}/shell_common/cmd_ping.cpp ${APP_ROOT}/shell_common/cmd_send.cpp - ${APP_ROOT}/shell_common/cmd_btp.cpp ${APP_ROOT}/standalone/main.cpp ) diff --git a/examples/shell/shell_common/BUILD.gn b/examples/shell/shell_common/BUILD.gn index 28c55b1e6a266a..e2d95419545fab 100644 --- a/examples/shell/shell_common/BUILD.gn +++ b/examples/shell/shell_common/BUILD.gn @@ -32,9 +32,6 @@ config("shell_common_config") { static_library("shell_common") { sources = [ - "cmd_base64.cpp", - "cmd_btp.cpp", - "cmd_device.cpp", "cmd_misc.cpp", "cmd_otcli.cpp", "cmd_ping.cpp", @@ -43,14 +40,10 @@ static_library("shell_common") { ] public_deps = [ - "${chip_root}/src/lib", - "${chip_root}/src/lib/core", "${chip_root}/src/lib/shell", "${chip_root}/src/lib/support", "${chip_root}/src/platform", "${chip_root}/src/protocols", - "${chip_root}/src/setup_payload", - "${chip_root}/src/system", ] if (chip_enable_openthread && diff --git a/examples/shell/shell_common/cmd_base64.cpp b/examples/shell/shell_common/cmd_base64.cpp deleted file mode 100644 index 504d94804dd0c5..00000000000000 --- a/examples/shell/shell_common/cmd_base64.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * - * Copyright (c) 2020 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 - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -using namespace chip; -using namespace chip::Shell; -using namespace chip::Logging; -using namespace chip::ArgParser; - -chip::Shell::Shell theShellBase64; - -int cmd_base64_help_iterator(shell_command_t * command, void * arg) -{ - streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help); - return 0; -} - -int cmd_base64_help(int argc, char ** argv) -{ - theShellBase64.ForEachCommand(cmd_base64_help_iterator, nullptr); - return 0; -} - -int cmd_base64_decode(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint32_t binarySize; - uint8_t binary[256]; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - binarySize = Base64Decode(argv[0], strlen(argv[0]), binary); - streamer_print_hex(sout, binary, binarySize); - streamer_printf(sout, "\n\r"); - -exit: - return error; -} - -int cmd_base64_encode(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - char base64[256]; - uint8_t binary[256]; - uint32_t binarySize, base64Size; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - ParseHexString(argv[0], strlen(argv[0]), binary, sizeof(binary), binarySize); - base64Size = Base64Encode(binary, binarySize, base64); - streamer_printf(sout, "%.*s\n\r", base64Size, base64); - -exit: - return error; -} - -int cmd_base64_dispatch(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - error = theShellBase64.ExecCommand(argc, argv); - -exit: - return error; -} - -static const shell_command_t cmds_base64_root = { &cmd_base64_dispatch, "base64", "Base64 encode / decode utilities" }; - -/// Subcommands for root command: `base64 ` -static const shell_command_t cmds_base64[] = { - { &cmd_base64_help, "help", "Usage: base64 " }, - { &cmd_base64_encode, "encode", "Encode a hex sting as base64. Usage: base64 encode " }, - { &cmd_base64_decode, "decode", "Decode a base64 sting as hex. Usage: base64 decode " }, -}; - -void cmd_base64_init() -{ - // Register `base64` subcommands with the local shell dispatcher. - theShellBase64.RegisterCommands(cmds_base64, ArraySize(cmds_base64)); - - // Register the root `base64` command with the top-level shell. - shell_register(&cmds_base64_root, 1); -} diff --git a/examples/shell/shell_common/cmd_btp.cpp b/examples/shell/shell_common/cmd_btp.cpp deleted file mode 100644 index 632e8ae4620d7e..00000000000000 --- a/examples/shell/shell_common/cmd_btp.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/* - * - * Copyright (c) 2020 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 "ChipShellCollection.h" - -#include - -#if CONFIG_NETWORK_LAYER_BLE - -#if CONFIG_DEVICE_LAYER -#include -#endif - -#include -#include -#include -#include - -using namespace chip; -using namespace chip::Shell; -using namespace chip::Platform; -using namespace chip::DeviceLayer; -using namespace chip::Logging; -using namespace chip::ArgParser; - -static chip::Shell::Shell sShellDeviceSubcommands; - -int cmd_btp_help_iterator(shell_command_t * command, void * arg) -{ - streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help); - return 0; -} - -int cmd_btp_help(int argc, char ** argv) -{ - sShellDeviceSubcommands.ForEachCommand(cmd_btp_help_iterator, nullptr); - return 0; -} - -int cmd_btp_adv(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - bool adv_enabled; - - if (argc == 0) - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - - adv_enabled = ConnectivityMgr().IsBLEAdvertisingEnabled(); - if (strcmp(argv[0], "start") == 0) - { - if (adv_enabled) - { - streamer_printf(sout, "BLE advertising already enabled"); - } - else - { - streamer_printf(sout, "Starting BLE advertising"); - ConnectivityMgr().SetBLEAdvertisingEnabled(true); - } - } - else if (strcmp(argv[0], "stop") == 0) - { - if (adv_enabled) - { - streamer_printf(sout, "Stopping BLE advertising"); - ConnectivityMgr().SetBLEAdvertisingEnabled(false); - } - else - { - streamer_printf(sout, "BLE advertising already stopped"); - } - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - return error; -} - -int cmd_btp_scan(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - - if (argc == 0) - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - - if (strcmp(argv[0], "start") == 0) - { - streamer_printf(sout, "Starting scanning over BLE"); - // TODO: start scanning - } - else if (strcmp(argv[0], "stop") == 0) - { - streamer_printf(sout, "Stopping scanning over BLE"); - // TODO: stop scanning - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - return error; -} - -int cmd_btp_connect(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - - if (argc == 0) - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - - if (strcmp(argv[0], "start") == 0) - { - streamer_printf(sout, "Connecting to the device over BLE"); - // connecting - } - else if (strcmp(argv[0], "stop") == 0) - { - streamer_printf(sout, "Disconnecting from the device"); - // disconnecting - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - return error; -} - -int cmd_btp_send(int argc, char ** argv) -{ - return CHIP_ERROR_NOT_IMPLEMENTED; -} - -int cmd_btp_dispatch(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - error = sShellDeviceSubcommands.ExecCommand(argc, argv); - -exit: - return error; -} - -static const shell_command_t cmds_btp_root = { &cmd_btp_dispatch, "btp", "BLE transport commands" }; - -/// Subcommands for root command: `btp ` -static const shell_command_t cmds_btp[] = { - { &cmd_btp_help, "help", "Usage: btp " }, - { &cmd_btp_scan, "scan", "Enable or disable scan. Usage: btp scan " }, - { &cmd_btp_connect, "connect", "Connect or disconnect to a device. Usage: btp connect " }, - { &cmd_btp_adv, "adv", "Enable or disable advertisement. Usage: btp adv " }, - { &cmd_btp_send, "send", "Send binary data. Usage: device dump" }, -}; - -#endif // CONFIG_NETWORK_LAYER_BLE - -void cmd_btp_init() -{ -#if CONFIG_NETWORK_LAYER_BLE - // CHIP_ERROR error = CHIP_NO_ERROR; - - // Register `device` subcommands with the local shell dispatcher. - sShellDeviceSubcommands.RegisterCommands(cmds_btp, ArraySize(cmds_btp)); - - // Register the root `btp` command with the top-level shell. - shell_register(&cmds_btp_root, 1); - -#endif // CONFIG_NETWORK_LAYER_BLE -} diff --git a/examples/shell/shell_common/cmd_device.cpp b/examples/shell/shell_common/cmd_device.cpp deleted file mode 100644 index 9925c6ea8f6e08..00000000000000 --- a/examples/shell/shell_common/cmd_device.cpp +++ /dev/null @@ -1,978 +0,0 @@ -/* - * - * Copyright (c) 2020 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 - -#if CONFIG_DEVICE_LAYER - -#include - -#include -#include -#include -#include -#include -#if CHIP_ENABLE_OPENTHREAD -#include -#endif - -#include - -using namespace chip; -using namespace chip::Shell; -using namespace chip::Platform; -using namespace chip::DeviceLayer; -using namespace chip::Logging; -using namespace chip::ArgParser; - -static chip::Shell::Shell sShellDeviceSubcommands; - -int cmd_device_help_iterator(shell_command_t * command, void * arg) -{ - streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help); - return 0; -} - -int cmd_device_help(int argc, char ** argv) -{ - sShellDeviceSubcommands.ForEachCommand(cmd_device_help_iterator, nullptr); - return 0; -} - -static CHIP_ERROR ConfigGetDone(streamer_t * sout, CHIP_ERROR error) -{ - if (error) - { - streamer_printf(sout, ""); - } - streamer_printf(sout, "\r\n"); - return error; -} - -static CHIP_ERROR ConfigGetVendorId(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint16_t value16; - - if (printHeader) - { - streamer_printf(sout, "VendorId: "); - } - SuccessOrExit(error = ConfigurationMgr().GetVendorId(value16)); - streamer_printf(sout, "%" PRIu16 " (0x%" PRIX16 ")", value16, value16); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetProductId(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint16_t value16; - - if (printHeader) - { - streamer_printf(sout, "ProductId: "); - } - SuccessOrExit(error = ConfigurationMgr().GetProductId(value16)); - streamer_printf(sout, "%" PRIu16 " (0x%" PRIX16 ")", value16, value16); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetProductRevision(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint16_t value16; - - if (printHeader) - { - streamer_printf(sout, "ProductRevision: "); - } - SuccessOrExit(error = ConfigurationMgr().GetProductRevision(value16)); - streamer_printf(sout, "%" PRIu16 " (0x%" PRIX16 ")", value16, value16); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetSerialNumber(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - char buf[ConfigurationManager::kMaxSerialNumberLength + 1]; - size_t bufSize; - - if (printHeader) - { - streamer_printf(sout, "SerialNumber: "); - } - SuccessOrExit(error = ConfigurationMgr().GetSerialNumber(buf, sizeof(buf), bufSize)); - buf[bufSize] = '\0'; - streamer_printf(sout, "%s", buf); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetDeviceId(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint64_t value64; - - if (printHeader) - { - streamer_printf(sout, "DeviceId: "); - } - SuccessOrExit(error = ConfigurationMgr().GetDeviceId(value64)); - streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")", value64, value64); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetDeviceCert(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint8_t * certBuf = nullptr; - size_t certLen; - - if (printHeader) - { - streamer_printf(sout, "DeviceCert: "); - } - // Determine the length of the device certificate. - error = ConfigurationMgr().GetDeviceCertificate(nullptr, 0, certLen); - SuccessOrExit(error); - - // Fail if no certificate has been configured. - VerifyOrExit(certLen != 0, error = CHIP_ERROR_CERT_NOT_FOUND); - - // Create a temporary buffer to hold the certificate. - certBuf = static_cast(MemoryAlloc(certLen)); - VerifyOrExit(certBuf != nullptr, error = CHIP_ERROR_NO_MEMORY); - - // Read the certificate - error = ConfigurationMgr().GetDeviceCertificate(certBuf, certLen, certLen); - SuccessOrExit(error); - - streamer_print_hex(sout, const_cast(certBuf), certLen); - -exit: - if (certBuf != nullptr) - { - MemoryFree(certBuf); - } - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetDeviceCaCerts(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint8_t * certBuf = nullptr; - size_t certLen; - - if (printHeader) - { - streamer_printf(sout, "DeviceCaCerts: "); - } - // Determine the length of the device certificate. - error = ConfigurationMgr().GetDeviceIntermediateCACerts(nullptr, 0, certLen); - SuccessOrExit(error); - - // Fail if no certificate has been configured. - VerifyOrExit(certLen != 0, error = CHIP_ERROR_CERT_NOT_FOUND); - - // Create a temporary buffer to hold the certificate. - certBuf = static_cast(MemoryAlloc(certLen)); - VerifyOrExit(certBuf != nullptr, error = CHIP_ERROR_NO_MEMORY); - - // Read the certificate - error = ConfigurationMgr().GetDeviceIntermediateCACerts(certBuf, certLen, certLen); - SuccessOrExit(error); - - streamer_print_hex(sout, const_cast(certBuf), certLen); - -exit: - if (certBuf != nullptr) - { - MemoryFree(certBuf); - } - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetManufacturerDeviceId(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint64_t value64; - - if (printHeader) - { - streamer_printf(sout, "MfrDeviceId: "); - } - SuccessOrExit(error = ConfigurationMgr().GetManufacturerDeviceId(value64)); - streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")", value64, value64); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetManufacturerDeviceCert(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint8_t * certBuf = nullptr; - size_t certLen; - - if (printHeader) - { - streamer_printf(sout, "MfrDeviceCert: "); - } - // Determine the length of the device certificate. - error = ConfigurationMgr().GetManufacturerDeviceCertificate(nullptr, 0, certLen); - SuccessOrExit(error); - - // Fail if no certificate has been configured. - VerifyOrExit(certLen != 0, error = CHIP_ERROR_CERT_NOT_FOUND); - - // Create a temporary buffer to hold the certificate. - certBuf = static_cast(MemoryAlloc(certLen)); - VerifyOrExit(certBuf != nullptr, error = CHIP_ERROR_NO_MEMORY); - - // Read the certificate - error = ConfigurationMgr().GetManufacturerDeviceCertificate(certBuf, certLen, certLen); - SuccessOrExit(error); - - streamer_print_hex(sout, const_cast(certBuf), certLen); - -exit: - if (certBuf != nullptr) - { - MemoryFree(certBuf); - } - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetManufacturerDeviceCaCerts(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint8_t * certBuf = nullptr; - size_t certLen; - - if (printHeader) - { - streamer_printf(sout, "MfgDeviceCaCerts:"); - } - // Determine the length of the device certificate. - error = ConfigurationMgr().GetManufacturerDeviceIntermediateCACerts(nullptr, 0, certLen); - SuccessOrExit(error); - - // Fail if no certificate has been configured. - VerifyOrExit(certLen != 0, error = CHIP_ERROR_CERT_NOT_FOUND); - - // Create a temporary buffer to hold the certificate. - certBuf = static_cast(MemoryAlloc(certLen)); - VerifyOrExit(certBuf != nullptr, error = CHIP_ERROR_NO_MEMORY); - - // Read the certificate - error = ConfigurationMgr().GetManufacturerDeviceIntermediateCACerts(certBuf, certLen, certLen); - SuccessOrExit(error); - - streamer_print_hex(sout, const_cast(certBuf), certLen); - -exit: - if (certBuf != nullptr) - { - MemoryFree(certBuf); - } - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetSetupPinCode(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint32_t setupPinCode; - - if (printHeader) - { - streamer_printf(sout, "PinCode: "); - } - SuccessOrExit(error = ConfigurationMgr().GetSetupPinCode(setupPinCode)); - streamer_printf(sout, "%09u", setupPinCode); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetSetupDiscriminator(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint16_t setupDiscriminator; - - if (printHeader) - { - streamer_printf(sout, "Discriminator: "); - } - SuccessOrExit(error = ConfigurationMgr().GetSetupDiscriminator(setupDiscriminator)); - streamer_printf(sout, "%03x", setupDiscriminator & 0xFFF); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetServiceId(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint64_t value64; - - if (printHeader) - { - streamer_printf(sout, "ServiceId: "); - } - SuccessOrExit(error = ConfigurationMgr().GetServiceId(value64)); - streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")", value64, value64); - -exit: - return ConfigGetDone(sout, error); -} - -static CHIP_ERROR ConfigGetFabricId(bool printHeader) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - uint64_t value64; - - if (printHeader) - { - streamer_printf(sout, "FabricId: "); - } - SuccessOrExit(error = ConfigurationMgr().GetFabricId(value64)); - streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")", value64, value64); - -exit: - return ConfigGetDone(sout, error); -} - -int cmd_device_config(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc == 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - error |= ConfigGetVendorId(true); - error |= ConfigGetProductId(true); - error |= ConfigGetProductRevision(true); - error |= ConfigGetSerialNumber(true); - - error |= ConfigGetServiceId(true); - error |= ConfigGetFabricId(true); - error |= ConfigGetSetupPinCode(true); - error |= ConfigGetSetupDiscriminator(true); - - error |= ConfigGetDeviceId(true); - error |= ConfigGetDeviceCert(true); - error |= ConfigGetDeviceCaCerts(true); - - error |= ConfigGetManufacturerDeviceId(true); - error |= ConfigGetManufacturerDeviceCert(true); - error |= ConfigGetManufacturerDeviceCaCerts(true); - -exit: - return (error) ? CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND : CHIP_NO_ERROR; -} - -int cmd_device_get(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - - if (argc == 0) - { - return cmd_device_config(argc, argv); - } - - if (strcmp(argv[0], "vendorid") == 0) - { - SuccessOrExit(error = ConfigGetVendorId(false)); - } - else if (strcmp(argv[0], "productid") == 0) - { - SuccessOrExit(error = ConfigGetProductId(false)); - } - else if (strcmp(argv[0], "productrev") == 0) - { - SuccessOrExit(error = ConfigGetProductRevision(false)); - } - else if (strcmp(argv[0], "serial") == 0) - { - SuccessOrExit(error = ConfigGetSerialNumber(false)); - } - else if (strcmp(argv[0], "deviceid") == 0) - { - SuccessOrExit(error = ConfigGetDeviceId(false)); - } - else if (strcmp(argv[0], "cert") == 0) - { - SuccessOrExit(error = ConfigGetDeviceCert(false)); - } - else if (strcmp(argv[0], "cacerts") == 0) - { - SuccessOrExit(error = ConfigGetDeviceCaCerts(false)); - } - else if (strcmp(argv[0], "mfrdeviceid") == 0) - { - SuccessOrExit(error = ConfigGetManufacturerDeviceId(false)); - } - else if (strcmp(argv[0], "mfrcert") == 0) - { - SuccessOrExit(error = ConfigGetManufacturerDeviceCert(false)); - } - else if (strcmp(argv[0], "mfrcacerts") == 0) - { - SuccessOrExit(error = ConfigGetManufacturerDeviceCaCerts(false)); - } - else if (strcmp(argv[0], "pincode") == 0) - { - SuccessOrExit(error = ConfigGetSetupPinCode(false)); - } - else if (strcmp(argv[0], "discriminator") == 0) - { - SuccessOrExit(error = ConfigGetSetupDiscriminator(false)); - } - else if (strcmp(argv[0], "serviceid") == 0) - { - SuccessOrExit(error = ConfigGetServiceId(false)); - } - else if (strcmp(argv[0], "fabricid") == 0) - { - SuccessOrExit(error = ConfigGetFabricId(false)); - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - return error; -} - -#if CHIP_DEVICE_CONFIG_ENABLE_WPA -int cmd_device_start_wifi(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - streamer_t * sout = streamer_get(); - - VerifyOrExit(argc == 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - streamer_printf(sout, "Starting WiFi management\r\n"); - ConnectivityMgrImpl().StartWiFiManagement(); - -exit: - return error; -} - -int cmd_device_sta(int argc, char ** argv) -{ - streamer_t * sout = streamer_get(); - - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - VerifyOrExit(PlatformMgr().TryLockChipStack(), error = CHIP_ERROR_INVALID_ARGUMENT); - - if (strcmp(argv[0], "mode") == 0) - { - const char * typeStr = "Unknown"; - ConnectivityManager::WiFiStationMode mode = ConnectivityMgr().GetWiFiStationMode(); - switch (mode) - { - case ConnectivityManager::WiFiStationMode::kWiFiStationMode_NotSupported: - typeStr = "NotSupported"; - break; - case ConnectivityManager::WiFiStationMode::kWiFiStationMode_ApplicationControlled: - typeStr = "ApplicationControlled"; - break; - case ConnectivityManager::WiFiStationMode::kWiFiStationMode_Disabled: - typeStr = "Disabled"; - break; - case ConnectivityManager::WiFiStationMode::kWiFiStationMode_Enabled: - typeStr = "Enabled"; - break; - } - streamer_printf(sout, "%s\r\n", typeStr); - } - else if (strcmp(argv[0], "set_mode") == 0) - { - if (argc < 2) - { - streamer_printf(sout, - "Invalid command: needs to specify mode:\r\n" - "disable\r\n" - "enable\r\n" - "app_ctrl\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - else - { - ConnectivityManager::WiFiStationMode mode = ConnectivityManager::WiFiStationMode::kWiFiStationMode_NotSupported; - - if (strcmp(argv[1], "disable") == 0) - { - mode = ConnectivityManager::WiFiStationMode::kWiFiStationMode_Disabled; - } - else if (strcmp(argv[1], "enable") == 0) - { - mode = ConnectivityManager::WiFiStationMode::kWiFiStationMode_Enabled; - } - else if (strcmp(argv[1], "app_ctrl") == 0) - { - mode = ConnectivityManager::WiFiStationMode::kWiFiStationMode_ApplicationControlled; - } - - if (mode != ConnectivityManager::WiFiStationMode::kWiFiStationMode_NotSupported) - { - error = ConnectivityMgr().SetWiFiStationMode(mode); - } - else - { - streamer_printf(sout, - "Invalid command: needs to be one of the following modes:\r\n" - "disable\r\n" - "enable\r\n" - "app_ctrl\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - } - } - else if (strcmp(argv[0], "enabled") == 0) - { - bool isState = ConnectivityMgr().IsWiFiStationEnabled(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "provisioned") == 0) - { - bool isState = ConnectivityMgr().IsWiFiStationProvisioned(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "clear_provision") == 0) - { - ConnectivityMgr().ClearWiFiStationProvision(); - streamer_printf(sout, "Clear WiFi Station provision\r\n"); - } - else if (strcmp(argv[0], "controlled") == 0) - { - bool isState = ConnectivityMgr().IsWiFiStationApplicationControlled(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "connected") == 0) - { - bool isState = ConnectivityMgr().IsWiFiStationConnected(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "reconnect_interval") == 0) - { - uint32_t interval = ConnectivityMgr().GetWiFiStationReconnectIntervalMS(); - streamer_printf(sout, "WiFi Station Reconnect Interval (in seconds): %d\r\n", interval / 1000); - } - else if (strcmp(argv[0], "set_reconnect_interval") == 0) - { - if (argc < 2) - { - streamer_printf(sout, "Invalid command: needs to specify Station Reconnect Interval (in seconds):\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - else - { - uint32_t interval = std::stoi(argv[1]) * 1000; - - ConnectivityMgr().SetWiFiStationReconnectIntervalMS(interval); - } - } - else if (strcmp(argv[0], "stats") == 0) - { - SuccessOrExit(error = ConnectivityMgr().GetAndLogWifiStatsCounters()); - streamer_printf(sout, "WiFi statistics written to log\r\n"); - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - PlatformMgr().UnlockChipStack(); - return error; -} - -int cmd_device_ap(int argc, char ** argv) -{ - streamer_t * sout = streamer_get(); - - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - VerifyOrExit(PlatformMgr().TryLockChipStack(), error = CHIP_ERROR_INVALID_ARGUMENT); - - if (strcmp(argv[0], "mode") == 0) - { - const char * typeStr = "Unknown"; - ConnectivityManager::WiFiAPMode mode = ConnectivityMgr().GetWiFiAPMode(); - switch (mode) - { - case ConnectivityManager::WiFiAPMode::kWiFiAPMode_NotSupported: - typeStr = "NotSupported"; - break; - case ConnectivityManager::WiFiAPMode::kWiFiAPMode_ApplicationControlled: - typeStr = "ApplicationControlled"; - break; - case ConnectivityManager::WiFiAPMode::kWiFiAPMode_Disabled: - typeStr = "Disabled"; - break; - case ConnectivityManager::WiFiAPMode::kWiFiAPMode_Enabled: - typeStr = "Enabled"; - break; - case ConnectivityManager::WiFiAPMode::kWiFiAPMode_OnDemand: - typeStr = "OnDemand"; - break; - case ConnectivityManager::WiFiAPMode::kWiFiAPMode_OnDemand_NoStationProvision: - typeStr = "OnDemand-NoStation"; - break; - } - streamer_printf(sout, "%s\r\n", typeStr); - } - else if (strcmp(argv[0], "set_mode") == 0) - { - if (argc < 2) - { - streamer_printf(sout, - "Invalid command: needs to specify mode:\r\n" - "disable\r\n" - "enable\r\n" - "on_demand\r\n" - "on_demand_no_sta\r\n" - "app_ctrl\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - else - { - ConnectivityManager::WiFiAPMode mode = ConnectivityManager::WiFiAPMode::kWiFiAPMode_NotSupported; - - if (strcmp(argv[1], "disable") == 0) - { - mode = ConnectivityManager::WiFiAPMode::kWiFiAPMode_Disabled; - } - else if (strcmp(argv[1], "enable") == 0) - { - mode = ConnectivityManager::WiFiAPMode::kWiFiAPMode_Enabled; - } - else if (strcmp(argv[1], "on_demand") == 0) - { - mode = ConnectivityManager::WiFiAPMode::kWiFiAPMode_OnDemand; - } - else if (strcmp(argv[1], "on_demand_no_sta") == 0) - { - mode = ConnectivityManager::WiFiAPMode::kWiFiAPMode_OnDemand_NoStationProvision; - } - else if (strcmp(argv[1], "app_ctrl") == 0) - { - mode = ConnectivityManager::WiFiAPMode::kWiFiAPMode_ApplicationControlled; - } - - if (mode != ConnectivityManager::WiFiAPMode::kWiFiAPMode_NotSupported) - { - error = ConnectivityMgr().SetWiFiAPMode(mode); - } - else - { - streamer_printf(sout, - "Invalid command: needs to be one of the following modes:\r\n" - "disable\r\n" - "enable\r\n" - "on_demand\r\n" - "on_demand_no_sta\r\n" - "app_ctrl\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - } - } - else if (strcmp(argv[0], "active") == 0) - { - bool isState = ConnectivityMgr().IsWiFiAPActive(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "controlled") == 0) - { - bool isState = ConnectivityMgr().IsWiFiAPApplicationControlled(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "start") == 0) - { - ConnectivityMgr().DemandStartWiFiAP(); - streamer_printf(sout, "start AP mode\r\n"); - } - else if (strcmp(argv[0], "stop") == 0) - { - ConnectivityMgr().StopOnDemandWiFiAP(); - streamer_printf(sout, "stop AP mode\r\n"); - } - else if (strcmp(argv[0], "idle_timeout") == 0) - { - uint32_t interval = ConnectivityMgr().GetWiFiAPIdleTimeoutMS(); - streamer_printf(sout, "WiFi AP Idle Timeout (in seconds): %d\r\n", interval / 1000); - } - else if (strcmp(argv[0], "set_idle") == 0) - { - if (argc < 2) - { - streamer_printf(sout, "Invalid command: needs to specify AP Idle Timeout (in seconds):\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - else - { - uint32_t timeout = std::stoi(argv[1]) * 1000; - - ConnectivityMgr().SetWiFiAPIdleTimeoutMS(timeout); - } - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - PlatformMgr().UnlockChipStack(); - return error; -} - -int cmd_device_connect(int argc, char ** argv) -{ - streamer_t * sout = streamer_get(); - - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - VerifyOrExit(PlatformMgr().TryLockChipStack(), error = CHIP_ERROR_INVALID_ARGUMENT); - - if (argc < 2) - { - streamer_printf(sout, - "Invalid command: needs two arguments " - "(network ssid and password)\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - else if (strlen(argv[0]) > 32) - { - streamer_printf(sout, "Invalid network SSID\r\n"); - error = CHIP_ERROR_INVALID_ARGUMENT; - } - else - { - streamer_printf(sout, "Connect to WiFi network: SSID: %s\r\n", argv[0]); - - error = ConnectivityMgrImpl().ProvisionWiFiNetwork(argv[0], argv[1]); - - if (error != CHIP_NO_ERROR) - { - streamer_printf(sout, "Failed to connect to WiFi network: %s\r\n", chip::ErrorStr(error)); - } - } - -exit: - PlatformMgr().UnlockChipStack(); - return error; -} -#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA - -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD -int cmd_device_thread(int argc, char ** argv) -{ - streamer_t * sout = streamer_get(); - - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - VerifyOrExit(PlatformMgr().TryLockChipStack(), error = CHIP_ERROR_INVALID_ARGUMENT); - - if (strcmp(argv[0], "mac") == 0) - { - uint64_t mac; - SuccessOrExit(error = ThreadStackMgr().GetPrimary802154MACAddress(reinterpret_cast(&mac))); - streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")", mac, mac); - } - else if (strcmp(argv[0], "start") == 0) - { - SuccessOrExit(error = ThreadStackMgr().StartThreadTask()); - } - else if (strcmp(argv[0], "role") == 0) - { - const char * typeStr = "Unknown"; - ConnectivityManager::ThreadDeviceType type; - type = ConnectivityMgr().GetThreadDeviceType(); - switch (type) - { - case ConnectivityManager::kThreadDeviceType_NotSupported: - typeStr = "NotSupported"; - break; - case ConnectivityManager::kThreadDeviceType_Router: - typeStr = "Router"; - break; - case ConnectivityManager::kThreadDeviceType_FullEndDevice: - typeStr = "FullEndDevice"; - break; - case ConnectivityManager::kThreadDeviceType_MinimalEndDevice: - typeStr = "MinimalEndDevice"; - break; - case ConnectivityManager::kThreadDeviceType_SleepyEndDevice: - typeStr = "SleepyEndDevice"; - break; - } - streamer_printf(sout, "%d: %s\r\n", static_cast(type), typeStr); - } - else if (strcmp(argv[0], "enabled") == 0) - { - bool isState = ConnectivityMgr().IsThreadEnabled(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "attached") == 0) - { - bool isState = ConnectivityMgr().IsThreadAttached(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "provisioned") == 0) - { - bool isState = ConnectivityMgr().IsThreadProvisioned(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "controlled") == 0) - { - bool isState = ConnectivityMgr().IsThreadApplicationControlled(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "connected") == 0) - { - bool isState = ConnectivityMgr().HaveServiceConnectivityViaThread(); - streamer_printf(sout, "%s\r\n", (isState) ? "true" : "false"); - } - else if (strcmp(argv[0], "erase") == 0) - { - ConnectivityMgr().ErasePersistentInfo(); - streamer_printf(sout, "Thread Factory Reset\r\n"); - } - else if (strcmp(argv[0], "stats") == 0) - { - SuccessOrExit(error = ThreadStackMgr().GetAndLogThreadStatsCounters()); - streamer_printf(sout, "Thread statistics written to log\r\n"); - } - else if (strcmp(argv[0], "topology") == 0) - { - SuccessOrExit(error = ThreadStackMgr().GetAndLogThreadTopologyFull()); - streamer_printf(sout, "Thread topology written to log\r\n"); - } - else - { - ExitNow(error = CHIP_ERROR_INVALID_ARGUMENT); - } - -exit: - PlatformMgr().UnlockChipStack(); - return error; -} -#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD - -int cmd_device_dispatch(int argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - - VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT); - - error = sShellDeviceSubcommands.ExecCommand(argc, argv); - -exit: - return error; -} - -static const shell_command_t cmds_device_root = { &cmd_device_dispatch, "device", "Device Layer commands" }; - -/// Subcommands for root command: `device ` -static const shell_command_t cmds_device[] = { - { &cmd_device_help, "help", "Usage: device " }, - { &cmd_device_get, "get", "Get configuration value. Usage: device get " }, - { &cmd_device_config, "config", "Dump entire configuration of device. Usage: device config" }, -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - { &cmd_device_thread, "thread", "Control the Thread interface. Usage: device thread " }, -#endif -#if CHIP_DEVICE_CONFIG_ENABLE_WPA - { &cmd_device_start_wifi, "start_wifi", "Start WiFi management. Usage: device start_wifi" }, - { &cmd_device_sta, "sta", "Control the WiFi STA interface. Usage: device sta " }, - { &cmd_device_ap, "ap", "Control the WiFi AP interface. Usage: device ap " }, - { &cmd_device_connect, "connect", "Join the network with the given SSID and PSK. Usage: device connect " }, -#endif -}; - -#endif // CONFIG_DEVICE_LAYER - -void cmd_device_init() -{ -#if CONFIG_DEVICE_LAYER - CHIP_ERROR error = CHIP_NO_ERROR; - - // Register `device` subcommands with the local shell dispatcher. - sShellDeviceSubcommands.RegisterCommands(cmds_device, ArraySize(cmds_device)); - - // Register the root `device` command with the top-level shell. - shell_register(&cmds_device_root, 1); - - streamer_t * sout = streamer_get(); - - error = chip::Platform::MemoryInit(); - VerifyOrExit(error == CHIP_NO_ERROR, - streamer_printf(sout, "Failed to init CHIP platform memory with error: %s\r\n", ErrorStr(error))); - - streamer_printf(sout, "Init CHIP Stack\r\n"); - error = PlatformMgr().InitChipStack(); - VerifyOrExit(error == CHIP_NO_ERROR, streamer_printf(sout, "Failed to init CHIP Stack with error: %s\r\n", ErrorStr(error))); - -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - streamer_printf(sout, "Init Thread stack\r\n"); - if (ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR) - { - streamer_printf(sout, "ThreadStackMgr().InitThreadStack() failed\r\n"); - } -#endif - - // Starting Platform Manager Event Loop; - error = PlatformMgr().StartEventLoopTask(); - VerifyOrExit(error == CHIP_NO_ERROR, streamer_printf(sout, "Failed to start platform event loop\r\n")); - -exit: - return; -#endif // CONFIG_DEVICE_LAYER -} diff --git a/examples/shell/shell_common/cmd_misc.cpp b/examples/shell/shell_common/cmd_misc.cpp index 1b4169d4e97a7a..45a1f29e5a4e9e 100644 --- a/examples/shell/shell_common/cmd_misc.cpp +++ b/examples/shell/shell_common/cmd_misc.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include +#include #include #include diff --git a/examples/shell/shell_common/cmd_otcli.cpp b/examples/shell/shell_common/cmd_otcli.cpp index f1058c0bc1ae9d..01d516b015563f 100644 --- a/examples/shell/shell_common/cmd_otcli.cpp +++ b/examples/shell/shell_common/cmd_otcli.cpp @@ -27,7 +27,7 @@ #include -#include +#include #include #include #include diff --git a/examples/shell/shell_common/cmd_ping.cpp b/examples/shell/shell_common/cmd_ping.cpp index b2c4bc20eb33a3..b91b73796e0dcd 100644 --- a/examples/shell/shell_common/cmd_ping.cpp +++ b/examples/shell/shell_common/cmd_ping.cpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include #include diff --git a/examples/shell/shell_common/cmd_send.cpp b/examples/shell/shell_common/cmd_send.cpp index 42ab92dab69998..d6a89e61cd4e1d 100644 --- a/examples/shell/shell_common/cmd_send.cpp +++ b/examples/shell/shell_common/cmd_send.cpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include #include @@ -230,7 +230,7 @@ void ProcessCommand(streamer_t * stream, char * destination) Transport::PeerAddress peerAddress; Transport::AdminPairingInfo * adminInfo = nullptr; - if (!Inet::IPAddress::FromString(destination, gDestAddr)) + if (!chip::Inet::IPAddress::FromString(destination, gDestAddr)) { streamer_printf(stream, "Invalid CHIP Server IP address: %s\n", destination); ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); diff --git a/examples/shell/shell_common/include/ChipShellCollection.h b/examples/shell/shell_common/include/ChipShellCollection.h index 0bfc41bd92edf5..ba54bad94391af 100644 --- a/examples/shell/shell_common/include/ChipShellCollection.h +++ b/examples/shell/shell_common/include/ChipShellCollection.h @@ -19,9 +19,6 @@ extern "C" { // A list of shell commands provided by ChipShell -void cmd_base64_init(void); -void cmd_btp_init(void); -void cmd_device_init(void); void cmd_misc_init(void); void cmd_otcli_init(void); void cmd_ping_init(void); diff --git a/examples/shell/standalone/main.cpp b/examples/shell/standalone/main.cpp index 5710c661d2329e..62fb5f78ba7aa8 100644 --- a/examples/shell/standalone/main.cpp +++ b/examples/shell/standalone/main.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include +#include #include #include @@ -25,12 +25,21 @@ #include #include +#include +#include +#include using namespace chip; using namespace chip::Shell; int main() { + chip::Platform::MemoryInit(); + chip::DeviceLayer::PlatformMgr().InitChipStack(); + chip::DeviceLayer::PlatformMgr().StartEventLoopTask(); +#if CHIP_DEVICE_CONFIG_ENABLE_WPA + chip::DeviceLayer::ConnectivityManagerImpl().StartWiFiManagement(); +#endif // Initialize the default streamer that was linked. const int rc = streamer_init(streamer_get()); @@ -41,9 +50,6 @@ int main() } cmd_misc_init(); - cmd_base64_init(); - cmd_device_init(); - cmd_btp_init(); cmd_otcli_init(); cmd_ping_init(); cmd_send_init(); diff --git a/src/lib/BUILD.gn b/src/lib/BUILD.gn index 685704e8cc7e67..b63e040d2bac76 100644 --- a/src/lib/BUILD.gn +++ b/src/lib/BUILD.gn @@ -15,7 +15,7 @@ import("//build_overrides/chip.gni") declare_args() { - chip_build_shell_lib = false + chip_build_libshell = false } config("includes") { @@ -40,7 +40,7 @@ static_library("lib") { "${chip_root}/src/transport", ] - if (chip_build_shell_lib) { + if (chip_build_libshell) { public_deps += [ "${chip_root}/src/lib/shell" ] } diff --git a/src/lib/shell/BUILD.gn b/src/lib/shell/BUILD.gn index 8392dd3f0312e1..8122c29898aef2 100644 --- a/src/lib/shell/BUILD.gn +++ b/src/lib/shell/BUILD.gn @@ -17,19 +17,28 @@ import("//build_overrides/chip.gni") import("${chip_root}/src/lib/core/core.gni") import("${chip_root}/src/platform/device.gni") -static_library("shell") { - output_name = "libCHIPShell" - output_dir = "${root_out_dir}/lib" - +source_set("shell_core") { sources = [ - "commands.cpp", - "commands.h", - "shell_core.cpp", - "shell_core.h", + "Commands.h", + "Shell.cpp", + "Shell.h", "streamer.cpp", "streamer.h", ] + public_deps = [ + "${chip_root}/src/lib/core", + "${chip_root}/src/lib/support", + "${chip_root}/src/platform", + ] +} + +static_library("shell") { + output_name = "libCHIPShell" + output_dir = "${root_out_dir}/lib" + + sources = [ "Commands.cpp" ] + if (chip_target_style == "unix") { sources += [ "streamer_stdio.cpp" ] } @@ -45,7 +54,7 @@ static_library("shell") { cflags = [ "-Wconversion" ] public_deps = [ - "${chip_root}/src/lib/core", - "${chip_root}/src/lib/support", + ":shell_core", + "${chip_root}/src/lib/shell/commands", ] } diff --git a/src/lib/shell/Commands.cpp b/src/lib/shell/Commands.cpp new file mode 100644 index 00000000000000..cbd0edcac3e533 --- /dev/null +++ b/src/lib/shell/Commands.cpp @@ -0,0 +1,41 @@ +/* + * + * 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 +#include +#include + +namespace chip { +namespace Shell { + +void Shell::RegisterDefaultCommands() +{ + RegisterBase64Commands(); + RegisterMetaCommands(); +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE + RegisterBLECommands(); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION || CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP + RegisterWiFiCommands(); +#endif +#if CONFIG_DEVICE_LAYER + RegisterConfigCommands(); +#endif +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/Commands.h b/src/lib/shell/Commands.h new file mode 100644 index 00000000000000..762df6ac822db7 --- /dev/null +++ b/src/lib/shell/Commands.h @@ -0,0 +1,54 @@ +/* + * + * 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 { +namespace Shell { + +/** + * This function registers the base64 encode/decode commands. + * + */ +void RegisterBase64Commands(); + +/** + * This function registers the BLE commands. + * + */ +void RegisterBLECommands(); + +/** + * This function registers the common commands. + * + */ +void RegisterMetaCommands(); + +/** + * This function registers the device configuration commands. + * + */ +void RegisterConfigCommands(); + +/** + * This function registers the wifi commands. + * + */ +void RegisterWiFiCommands(); + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/shell_core.cpp b/src/lib/shell/Shell.cpp similarity index 71% rename from src/lib/shell/shell_core.cpp rename to src/lib/shell/Shell.cpp index 4fefb296061c91..1f839c0dee25e7 100644 --- a/src/lib/shell/shell_core.cpp +++ b/src/lib/shell/Shell.cpp @@ -21,10 +21,11 @@ * Source implementation for a generic shell API for CHIP examples. */ -#include "shell_core.h" -#include "commands.h" +#include "Shell.h" #include +#include +#include #include #include @@ -130,6 +131,7 @@ int Shell::ExecCommand(int argc, char * argv[]) { int retval = CHIP_ERROR_INVALID_ARGUMENT; + VerifyOrReturnError(argc > 0, retval); // Find the command for (unsigned i = 0; i < _commandSetCount; i++) { @@ -202,46 +204,83 @@ int Shell::TokenizeLine(char * buffer, char ** tokens, int max_tokens) return cursor; } -void Shell::TaskLoop(void * arg) +void Shell::ProcessShellLineTask(intptr_t context) { + char * line = reinterpret_cast(context); int retval; int argc; char * argv[CHIP_SHELL_MAX_TOKENS]; - char line[CHIP_SHELL_MAX_LINE_SIZE]; - theShellRoot.RegisterDefaultCommands(); + argc = shell_line_tokenize(line, argv, CHIP_SHELL_MAX_TOKENS); - while (true) + if (argc > 0) { - streamer_printf(streamer_get(), CHIP_SHELL_PROMPT); + retval = theShellRoot.ExecCommand(argc, argv); - shell_line_read(line, sizeof(line)); - argc = shell_line_tokenize(line, argv, CHIP_SHELL_MAX_TOKENS); - - if (argc > 0) + if (retval) { - retval = theShellRoot.ExecCommand(argc, argv); - - if (retval) - { - char errorStr[160]; - bool errorStrFound = FormatCHIPError(errorStr, sizeof(errorStr), retval); - if (!errorStrFound) - { - errorStr[0] = 0; - } - streamer_printf(streamer_get(), "Error %s: %s\r\n", argv[0], errorStr); - } - else + char errorStr[160]; + bool errorStrFound = FormatCHIPError(errorStr, sizeof(errorStr), retval); + if (!errorStrFound) { - streamer_printf(streamer_get(), "Done\r\n", argv[0]); + errorStr[0] = 0; } + streamer_printf(streamer_get(), "Error %s: %s\r\n", argv[0], errorStr); } else { - // Empty input has no output -- just display prompt + streamer_printf(streamer_get(), "Done\r\n", argv[0]); } } + else + { + // Empty input has no output -- just display prompt + } + Platform::MemoryFree(line); + streamer_printf(streamer_get(), CHIP_SHELL_PROMPT); +} + +void Shell::TaskLoop(void * arg) +{ + // char line[CHIP_SHELL_MAX_LINE_SIZE]; + + theShellRoot.RegisterDefaultCommands(); + streamer_printf(streamer_get(), CHIP_SHELL_PROMPT); + + while (true) + { + char * line = static_cast(Platform::MemoryAlloc(CHIP_SHELL_MAX_LINE_SIZE)); + shell_line_read(line, CHIP_SHELL_MAX_LINE_SIZE); +#if CONFIG_DEVICE_LAYER + DeviceLayer::PlatformMgr().ScheduleWork(ProcessShellLineTask, reinterpret_cast(line)); +#else + ProcessShellLineTask(reinterpret_cast(line)); +#endif + } +} + +/** Utility function for running ForEachCommand on root shell. */ +void shell_command_foreach(shell_command_iterator_t * on_command, void * arg) +{ + return Shell::Root().ForEachCommand(on_command, arg); +} + +/** Utility function for running ForEachCommand on Root shell. */ +void shell_register(shell_command_t * command_set, unsigned count) +{ + return Shell::Root().RegisterCommands(command_set, count); +} + +/** Utility function for to tokenize an input line. */ +int shell_line_tokenize(char * buffer, char ** tokens, int max_tokens) +{ + return Shell::TokenizeLine(buffer, tokens, max_tokens); +} + +/** Utility function to run main shell task loop. */ +void shell_task(void * arg) +{ + return Shell::TaskLoop(arg); } } // namespace Shell diff --git a/src/lib/shell/shell_core.h b/src/lib/shell/Shell.h similarity index 87% rename from src/lib/shell/shell_core.h rename to src/lib/shell/Shell.h index 555ee57163cece..383492762f9bfb 100644 --- a/src/lib/shell/shell_core.h +++ b/src/lib/shell/Shell.h @@ -161,31 +161,22 @@ class Shell * @param arg Unused context block for shell task to comply with task function syntax. */ static void TaskLoop(void * arg); + +private: + static void ProcessShellLineTask(intptr_t context); }; -/** Utility macro for running ForEachCommand on root shell. */ -static inline void shell_command_foreach(shell_command_iterator_t * on_command, void * arg) -{ - return Shell::Root().ForEachCommand(on_command, arg); -} +/** Utility function for running ForEachCommand on root shell. */ +void shell_command_foreach(shell_command_iterator_t * on_command, void * arg); -/** Utility macro for running ForEachCommand on Root shell. */ -static inline void shell_register(shell_command_t * command_set, unsigned count) -{ - return Shell::Root().RegisterCommands(command_set, count); -} +/** Utility function for running ForEachCommand on Root shell. */ +void shell_register(shell_command_t * command_set, unsigned count); -/** Utility macro for to tokenize an input line. */ -static inline int shell_line_tokenize(char * buffer, char ** tokens, int max_tokens) -{ - return Shell::TokenizeLine(buffer, tokens, max_tokens); -} +/** Utility function for to tokenize an input line. */ +int shell_line_tokenize(char * buffer, char ** tokens, int max_tokens); -/** Utility macro to run main shell task loop. */ -static inline void shell_task(void * arg) -{ - return Shell::TaskLoop(arg); -} +/** Utility function to run main shell task loop. */ +void shell_task(void * arg); } // namespace Shell } // namespace chip diff --git a/src/lib/shell/commands/BLE.cpp b/src/lib/shell/commands/BLE.cpp new file mode 100644 index 00000000000000..09f2351b58f49a --- /dev/null +++ b/src/lib/shell/commands/BLE.cpp @@ -0,0 +1,110 @@ +/* + * + * 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 +#include +#if CONFIG_DEVICE_LAYER +#include +#endif +#include +#include +#include +#include +#include + +using chip::DeviceLayer::ConnectivityMgr; + +namespace chip { +namespace Shell { + +static chip::Shell::Shell sShellDeviceSubcommands; + +int BLEHelpHandler(int argc, char ** argv) +{ + sShellDeviceSubcommands.ForEachCommand(PrintCommandHelp, nullptr); + return 0; +} + +int BLEAdvertiseHandler(int argc, char ** argv) +{ + CHIP_ERROR error = CHIP_NO_ERROR; + streamer_t * sout = streamer_get(); + bool adv_enabled; + + VerifyOrReturnError(argc == 1, error = CHIP_ERROR_INVALID_ARGUMENT); + + adv_enabled = ConnectivityMgr().IsBLEAdvertisingEnabled(); + if (strcmp(argv[0], "start") == 0) + { + if (adv_enabled) + { + streamer_printf(sout, "BLE advertising already enabled\r\n"); + } + else + { + streamer_printf(sout, "Starting BLE advertising\r\n"); + return ConnectivityMgr().SetBLEAdvertisingEnabled(true); + } + } + else if (strcmp(argv[0], "stop") == 0) + { + if (adv_enabled) + { + streamer_printf(sout, "Stopping BLE advertising\r\n"); + return ConnectivityMgr().SetBLEAdvertisingEnabled(false); + } + else + { + streamer_printf(sout, "BLE advertising already stopped\r\n"); + } + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + + return CHIP_NO_ERROR; +} + +int BLEDispatch(int argc, char ** argv) +{ + if (argc == 0) + { + BLEHelpHandler(argc, argv); + return CHIP_NO_ERROR; + } + return sShellDeviceSubcommands.ExecCommand(argc, argv); +} + +void RegisterBLECommands() +{ + static const shell_command_t sBLESubCommands[] = { + { &BLEHelpHandler, "help", "Usage: ble " }, + { &BLEAdvertiseHandler, "adv", "Enable or disable advertisement. Usage: ble adv " }, + }; + + static const shell_command_t sBLECommand = { &BLEDispatch, "ble", "BLE transport commands" }; + + // Register `device` subcommands with the local shell dispatcher. + sShellDeviceSubcommands.RegisterCommands(sBLESubCommands, ArraySize(sBLESubCommands)); + + // Register the root `btp` command with the top-level shell. + shell_register(&sBLECommand, 1); +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/BUILD.gn b/src/lib/shell/commands/BUILD.gn new file mode 100644 index 00000000000000..31717149136288 --- /dev/null +++ b/src/lib/shell/commands/BUILD.gn @@ -0,0 +1,41 @@ +# 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. + +import("//build_overrides/chip.gni") + +import("${chip_root}/src/lib/core/core.gni") +import("${chip_root}/src/platform/device.gni") + +source_set("commands") { + sources = [ + "Base64.cpp", + "Help.cpp", + "Help.h", + "Meta.cpp", + ] + + if (chip_device_platform != "none") { + sources += [ "Config.cpp" ] + } + + if (chip_enable_wifi) { + sources += [ "WiFi.cpp" ] + } + + if (chip_enable_ble && chip_device_platform != "none") { + sources += [ "BLE.cpp" ] + } + + public_deps = [ "${chip_root}/src/lib/shell:shell_core" ] +} diff --git a/src/lib/shell/commands/Base64.cpp b/src/lib/shell/commands/Base64.cpp new file mode 100644 index 00000000000000..0f1ff0523ef38a --- /dev/null +++ b/src/lib/shell/commands/Base64.cpp @@ -0,0 +1,103 @@ +/* + * + * 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 +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +chip::Shell::Shell sShellBase64Commands; + +namespace chip { +namespace Shell { + +static int Base64HelpHandler(int argc, char ** argv) +{ + sShellBase64Commands.ForEachCommand(PrintCommandHelp, nullptr); + return 0; +} + +static int Base64DecodeHandler(int argc, char ** argv) +{ + streamer_t * sout = streamer_get(); + uint32_t binarySize; + uint8_t binary[256]; + + VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT); + binarySize = Base64Decode(argv[0], strlen(argv[0]), binary); + streamer_print_hex(sout, binary, binarySize); + streamer_printf(sout, "\r\n"); + return CHIP_NO_ERROR; +} + +static int Base64EncodeHandler(int argc, char ** argv) +{ + streamer_t * sout = streamer_get(); + char base64[256]; + uint8_t binary[256]; + uint32_t binarySize, base64Size; + + VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT); + ArgParser::ParseHexString(argv[0], strlen(argv[0]), binary, sizeof(binary), binarySize); + base64Size = Base64Encode(binary, binarySize, base64); + streamer_printf(sout, "%.*s\r\n", base64Size, base64); + return CHIP_NO_ERROR; +} + +static int Base64Dispatch(int argc, char ** argv) +{ + CHIP_ERROR error = CHIP_NO_ERROR; + + if (argc == 0) + { + Base64HelpHandler(argc, argv); + return error; + } + error = sShellBase64Commands.ExecCommand(argc, argv); + return error; +} + +void RegisterBase64Commands() +{ + /// Subcommands for root command: `base64 ` + static const shell_command_t sBase64SubCommands[] = { + { &Base64HelpHandler, "help", "Usage: base64 " }, + { &Base64EncodeHandler, "encode", "Encode a hex sting as base64. Usage: base64 encode " }, + { &Base64DecodeHandler, "decode", "Decode a base64 sting as hex. Usage: base64 decode " }, + }; + + static const shell_command_t sBase64Command = { &Base64Dispatch, "base64", "Base64 encode / decode utilities" }; + + // Register `base64` subcommands with the local shell dispatcher. + sShellBase64Commands.RegisterCommands(sBase64SubCommands, ArraySize(sBase64SubCommands)); + + // Register the root `base64` command with the top-level shell. + shell_register(&sBase64Command, 1); +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/Config.cpp b/src/lib/shell/commands/Config.cpp new file mode 100644 index 00000000000000..b26aa69e390cda --- /dev/null +++ b/src/lib/shell/commands/Config.cpp @@ -0,0 +1,204 @@ +/* + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include + +using chip::DeviceLayer::ConfigurationMgr; + +namespace chip { +namespace Shell { + +static CHIP_ERROR ConfigGetVendorId(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint16_t value16; + + if (printHeader) + { + streamer_printf(sout, "VendorId: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetVendorId(value16)); + streamer_printf(sout, "%" PRIu16 " (0x%" PRIX16 ")\r\n", value16, value16); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR ConfigGetProductId(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint16_t value16; + + if (printHeader) + { + streamer_printf(sout, "ProductId: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetProductId(value16)); + streamer_printf(sout, "%" PRIu16 " (0x%" PRIX16 ")\r\n", value16, value16); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR ConfigGetProductRevision(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint16_t value16; + + if (printHeader) + { + streamer_printf(sout, "ProductRevision: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetProductRevision(value16)); + streamer_printf(sout, "%" PRIu16 " (0x%" PRIX16 ")\r\n", value16, value16); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR ConfigGetDeviceId(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint64_t value64; + + if (printHeader) + { + streamer_printf(sout, "DeviceId: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetDeviceId(value64)); + streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")\r\n", value64, value64); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR ConfigGetSetupPinCode(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint32_t setupPinCode; + + if (printHeader) + { + streamer_printf(sout, "PinCode: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetSetupPinCode(setupPinCode)); + streamer_printf(sout, "%09u\r\n", setupPinCode); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR ConfigGetSetupDiscriminator(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint16_t setupDiscriminator; + + if (printHeader) + { + streamer_printf(sout, "Discriminator: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetSetupDiscriminator(setupDiscriminator)); + streamer_printf(sout, "%03x\r\n", setupDiscriminator & 0xFFF); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR ConfigGetFabricId(bool printHeader) +{ + streamer_t * sout = streamer_get(); + uint64_t value64; + + if (printHeader) + { + streamer_printf(sout, "FabricId: "); + } + ReturnErrorOnFailure(ConfigurationMgr().GetFabricId(value64)); + streamer_printf(sout, "%" PRIu64 " (0x%" PRIX64 ")\r\n", value64, value64); + return CHIP_NO_ERROR; +} + +static int PrintAllConfigs() +{ + CHIP_ERROR error = CHIP_NO_ERROR; + + error |= ConfigGetVendorId(true); + error |= ConfigGetProductId(true); + error |= ConfigGetProductRevision(true); + + error |= ConfigGetFabricId(true); + error |= ConfigGetSetupPinCode(true); + error |= ConfigGetSetupDiscriminator(true); + + error |= ConfigGetDeviceId(true); + + return (error) ? CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND : CHIP_NO_ERROR; +} + +static int ConfigHandler(int argc, char ** argv) +{ + CHIP_ERROR error = CHIP_NO_ERROR; + + if (argc == 0) + { + return PrintAllConfigs(); + } + + if (strcmp(argv[0], "vendorid") == 0) + { + return ConfigGetVendorId(false); + } + else if (strcmp(argv[0], "productid") == 0) + { + return error = ConfigGetProductId(false); + } + else if (strcmp(argv[0], "productrev") == 0) + { + return error = ConfigGetProductRevision(false); + } + else if (strcmp(argv[0], "deviceid") == 0) + { + return error = ConfigGetDeviceId(false); + } + else if (strcmp(argv[0], "pincode") == 0) + { + return error = ConfigGetSetupPinCode(false); + } + else if (strcmp(argv[0], "discriminator") == 0) + { + return error = ConfigGetSetupDiscriminator(false); + } + else if (strcmp(argv[0], "fabricid") == 0) + { + return error = ConfigGetFabricId(false); + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } +} + +void RegisterConfigCommands() +{ + + static const shell_command_t sDeviceComand = { &ConfigHandler, "config", + "Dump device configuration. Usage: config [param_name]" }; + + // Register the root `device` command with the top-level shell. + shell_register(&sDeviceComand, 1); + + return; +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/Help.cpp b/src/lib/shell/commands/Help.cpp new file mode 100644 index 00000000000000..b44abca94be7ef --- /dev/null +++ b/src/lib/shell/commands/Help.cpp @@ -0,0 +1,37 @@ +/* + * + * 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. + */ + +/** + * @file + * Header that defines default shell commands for CHIP examples + */ + +#include +#include +#include + +namespace chip { +namespace Shell { + +int PrintCommandHelp(shell_command_t * command, void * arg) +{ + streamer_printf(streamer_get(), " %-15s %s\r\n", command->cmd_name, command->cmd_help); + return 0; +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands.h b/src/lib/shell/commands/Help.h similarity index 85% rename from src/lib/shell/commands.h rename to src/lib/shell/commands/Help.h index 79e3969101dc78..d3876fcf38fc3a 100644 --- a/src/lib/shell/commands.h +++ b/src/lib/shell/commands/Help.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2020 Project CHIP Authors + * 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. @@ -22,10 +22,12 @@ #pragma once -#include "shell_core.h" +#include namespace chip { namespace Shell { +int PrintCommandHelp(shell_command_t * command, void * arg); + } // namespace Shell } // namespace chip diff --git a/src/lib/shell/commands.cpp b/src/lib/shell/commands/Meta.cpp similarity index 52% rename from src/lib/shell/commands.cpp rename to src/lib/shell/commands/Meta.cpp index 14afe5d5e8c74f..ec4f048b3bee64 100644 --- a/src/lib/shell/commands.cpp +++ b/src/lib/shell/commands/Meta.cpp @@ -17,14 +17,9 @@ /** * @file - * Source implementation of default shell commands for CHIP examples. + * Source implementation of meta shell commands for CHIP examples. */ -#include "CHIPVersion.h" - -#include "shell_core.h" -#include - #include #include #include @@ -32,43 +27,43 @@ #include #include +#include +#include +#include +#include +#include + namespace chip { namespace Shell { -int cmd_exit(int argc, char ** argv) +static int ExitHandler(int argc, char ** argv) { - streamer_printf(streamer_get(), "Goodbye\n\r"); + streamer_printf(streamer_get(), "Goodbye\r\n"); exit(0); return 0; } -int cmd_help_iterator(shell_command_t * command, void * arg) +static int HelpHandler(int argc, char ** argv) { - streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help); + shell_command_foreach(PrintCommandHelp, nullptr); return 0; } -int cmd_help(int argc, char ** argv) +static int VersionHandler(int argc, char ** argv) { - shell_command_foreach(cmd_help_iterator, nullptr); + streamer_printf(streamer_get(), "CHIP %s\r\n", CHIP_VERSION_STRING); return 0; } -int cmd_version(int argc, char ** argv) +void RegisterMetaCommands() { - streamer_printf(streamer_get(), "CHIP %s\n\r", CHIP_VERSION_STRING); - return 0; -} + static shell_command_t sCmds[] = { + { &ExitHandler, "exit", "Exit the shell application" }, + { &HelpHandler, "help", "List out all top level commands" }, + { &VersionHandler, "version", "Output the software version" }, + }; -static shell_command_t cmds[] = { - { &cmd_exit, "exit", "Exit the shell application" }, - { &cmd_help, "help", "List out all top level commands" }, - { &cmd_version, "version", "Output the software version" }, -}; - -void Shell::RegisterDefaultCommands() -{ - RegisterCommands(cmds, ArraySize(cmds)); + shell_register(sCmds, ArraySize(sCmds)); } } // namespace Shell diff --git a/src/lib/shell/commands/WiFi.cpp b/src/lib/shell/commands/WiFi.cpp new file mode 100644 index 00000000000000..c201779f07f5b1 --- /dev/null +++ b/src/lib/shell/commands/WiFi.cpp @@ -0,0 +1,146 @@ +/* + * + * 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 +#include +#include +#include +#include +#include + +// Include DeviceNetworkProvisioningDelegateImpl for WiFi provisioning. +// TODO: Enable wifi network should be done by ConnectivityManager. (Or other platform neutral interfaces) +#if defined(CHIP_DEVICE_LAYER_TARGET) +#define DEVICENETWORKPROVISIONING_HEADER +#include DEVICENETWORKPROVISIONING_HEADER +#endif + +using chip::DeviceLayer::ConnectivityManager; +using chip::DeviceLayer::ConnectivityMgr; + +namespace chip { +namespace Shell { + +static chip::Shell::Shell sShellWifiSubCommands; + +static int WiFiHelpHandler(int argc, char ** argv) +{ + sShellWifiSubCommands.ForEachCommand(PrintCommandHelp, nullptr); + return 0; +} + +static int PrintWifiMode() +{ + streamer_t * sout = streamer_get(); + ConnectivityManager::WiFiAPMode apMode = ConnectivityMgr().GetWiFiAPMode(); + ConnectivityManager::WiFiStationMode staMode = ConnectivityMgr().GetWiFiStationMode(); + bool apEnabled = (apMode == ConnectivityManager::kWiFiAPMode_Enabled); + bool staEnabled = (staMode == ConnectivityManager::kWiFiStationMode_Enabled); + + if (apEnabled && !staEnabled) + { + streamer_printf(sout, "ap\r\n"); + } + else if (!apEnabled && staEnabled) + { + streamer_printf(sout, "sta\r\n"); + } + else if (!apEnabled && !staEnabled) + { + streamer_printf(sout, "disable\r\n"); + } + else + { + streamer_printf(sout, "mode not supported\r\n"); + } + + return 0; +} + +static int SetWifiMode(const char * mode) +{ + if (strcmp(mode, "disable") == 0) + { + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Disabled)); + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled)); + } + else if (strcmp(mode, "ap") == 0) + { + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled)); + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled)); + } + else if (strcmp(mode, "sta") == 0) + { + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Disabled)); + ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled)); + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + + return 0; +} + +static int WiFiModeHandler(int argc, char ** argv) +{ + if (argc == 0) + { + return PrintWifiMode(); + } + else if (argc != 1) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + return SetWifiMode(argv[0]); +} + +static int WiFiConnectHandler(int argc, char ** argv) +{ + if (argc != 2) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + DeviceLayer::DeviceNetworkProvisioningDelegateImpl deviceDelegate; + return deviceDelegate.ProvisionWiFi(argv[0], argv[1]); +} + +static int WiFiDispatch(int argc, char ** argv) +{ + if (argc == 0) + { + return WiFiHelpHandler(argc, argv); + } + return sShellWifiSubCommands.ExecCommand(argc, argv); +} + +void RegisterWiFiCommands() +{ + /// Subcommands for root command: `device ` + static const shell_command_t sWifiSubCommands[] = { + { &WiFiHelpHandler, "help", "" }, + { &WiFiModeHandler, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]." }, + { &WiFiConnectHandler, "connect", "Connect to AP. Usage: wifi connect ssid psk." }, + }; + static const shell_command_t sWifiCommand = { &WiFiDispatch, "wifi", "Usage: wifi " }; + + sShellWifiSubCommands.RegisterCommands(sWifiSubCommands, ArraySize(sWifiSubCommands)); + shell_register(&sWifiCommand, 1); +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/streamer_esp32.cpp b/src/lib/shell/streamer_esp32.cpp index 2f250b16262c3e..9dd63fc891ce2b 100644 --- a/src/lib/shell/streamer_esp32.cpp +++ b/src/lib/shell/streamer_esp32.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "shell_core.h" +#include "Shell.h" #include "streamer.h" #include "driver/uart.h" @@ -64,7 +64,7 @@ int streamer_esp32_init(streamer_t * streamer) esp_vfs_dev_uart_use_driver(0); esp_console_config_t console_config = { .max_cmdline_length = 256, - .max_cmdline_args = 8, + .max_cmdline_args = 32, }; ESP_ERROR_CHECK(esp_console_init(&console_config)); linenoiseSetMultiLine(1); diff --git a/src/lib/shell/streamer_stdio.cpp b/src/lib/shell/streamer_stdio.cpp index 7c7d871b8989d1..a0d3b481cc1151 100644 --- a/src/lib/shell/streamer_stdio.cpp +++ b/src/lib/shell/streamer_stdio.cpp @@ -20,7 +20,7 @@ * Source implementation of an input / output stream for stdio targets. */ -#include "shell_core.h" +#include "Shell.h" #include #include diff --git a/src/lib/shell/streamer_zephyr.cpp b/src/lib/shell/streamer_zephyr.cpp index 6192c06c74208e..2ba0a66dc38e3e 100644 --- a/src/lib/shell/streamer_zephyr.cpp +++ b/src/lib/shell/streamer_zephyr.cpp @@ -20,7 +20,7 @@ * Source implementation of an input / output stream for zehpyr targets. */ -#include "shell_core.h" +#include "Shell.h" #include diff --git a/src/lib/shell/tests/TestShell.cpp b/src/lib/shell/tests/TestShell.cpp index f06420b9597e2c..ca5d662176945c 100644 --- a/src/lib/shell/tests/TestShell.cpp +++ b/src/lib/shell/tests/TestShell.cpp @@ -17,7 +17,7 @@ #include "TestShell.h" -#include +#include #include #include diff --git a/src/lib/shell/tests/TestStreamerStdio.cpp b/src/lib/shell/tests/TestStreamerStdio.cpp index f54a09b465e60d..3e57ef9956271b 100644 --- a/src/lib/shell/tests/TestStreamerStdio.cpp +++ b/src/lib/shell/tests/TestStreamerStdio.cpp @@ -17,7 +17,7 @@ #include "TestShell.h" -#include +#include #include #include diff --git a/src/platform/device.gni b/src/platform/device.gni index d50d6956675ec2..485f7d1f7f5e6d 100644 --- a/src/platform/device.gni +++ b/src/platform/device.gni @@ -38,13 +38,11 @@ declare_args() { chip_device_platform == "cc13x2_26x2" || chip_device_platform == "k32w" # Enable wifi support. - chip_enable_wifi = chip_device_platform == "linux" + chip_enable_wifi = + chip_device_platform == "linux" || chip_device_platform == "esp32" # Enable ble support. - chip_enable_ble = - chip_config_network_layer_ble && - (chip_device_platform == "linux" || chip_device_platform == "darwin" || - chip_device_platform == "cc13x2_26x2") + chip_enable_ble = chip_config_network_layer_ble # Enable NFC support chip_enable_nfc = false diff --git a/src/platform/tests/BUILD.gn b/src/platform/tests/BUILD.gn index 9b2ebb04be23a2..1c3b534a677feb 100644 --- a/src/platform/tests/BUILD.gn +++ b/src/platform/tests/BUILD.gn @@ -57,7 +57,8 @@ if (chip_device_platform != "none") { # test_sources += [ "TestThreadStackMgr.cpp" ] } - if (chip_enable_ble) { + if (chip_enable_ble && + (chip_device_platform == "linux" || chip_device_platform == "darwin")) { # FIXME: TestCHIPoBLEStackMgr requires bluetoothd daemon to be running # # TODO: Driver code has a check for only running the test