diff --git a/examples/lock-app/esp32/.gitignore b/examples/lock-app/esp32/.gitignore new file mode 100644 index 00000000000000..234526a082ad26 --- /dev/null +++ b/examples/lock-app/esp32/.gitignore @@ -0,0 +1,5 @@ +*.vscode + +/build/ +/sdkconfig +/sdkconfig.old diff --git a/examples/shell/esp32/main/main.cpp b/examples/shell/esp32/main/main.cpp index 882fc8d6c5eabf..0d2610692bed5d 100644 --- a/examples/shell/esp32/main/main.cpp +++ b/examples/shell/esp32/main/main.cpp @@ -43,6 +43,7 @@ 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; diff --git a/examples/shell/shell_common/BUILD.gn b/examples/shell/shell_common/BUILD.gn index 28c55b1e6a266a..4bc46a583caa51 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", 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_send.cpp b/examples/shell/shell_common/cmd_send.cpp index ee30d2fab899ff..62aa900c677bfe 100644 --- a/examples/shell/shell_common/cmd_send.cpp +++ b/examples/shell/shell_common/cmd_send.cpp @@ -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..3a708ca14c2584 100644 --- a/examples/shell/standalone/main.cpp +++ b/examples/shell/standalone/main.cpp @@ -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/shell/BUILD.gn b/src/lib/shell/BUILD.gn index 8392dd3f0312e1..202373ff42cb94 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", + "Commands.h", "shell_core.cpp", "shell_core.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..d84248051a2c25 --- /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() +{ + CommandBase64Init(); + CommandCommonInit(); +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE + CommandBLEInit(); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION || CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP + CommandWifiInit(); +#endif +#if CONFIG_DEVICE_LAYER + CommandConfigInit(); +#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..f060a23d9204d6 --- /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 CommandBase64Init(); + +/** + * This function registers the BLE commands. + * + */ +void CommandBLEInit(); + +/** + * This function registers the common commands. + * + */ +void CommandCommonInit(); + +/** + * This function registers the device configuration commands. + * + */ +void CommandConfigInit(); + +/** + * This function registers the wifi commands. + * + */ +void CommandWifiInit(); + +} // 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..2ef5bbff88a803 --- /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 = [ + "CommandBase64.cpp", + "CommandCommon.cpp", + "CommandsHelp.cpp", + "CommandsHelp.h", + ] + + if (chip_device_platform != "none") { + sources += [ "CommandConfig.cpp" ] + } + + if (chip_enable_wifi) { + sources += [ "CommandWiFi.cpp" ] + } + + if (chip_enable_ble) { + sources += [ "CommandBLE.cpp" ] + } + + public_deps = [ "${chip_root}/src/lib/shell:shell_core" ] +} diff --git a/src/lib/shell/commands/CommandBLE.cpp b/src/lib/shell/commands/CommandBLE.cpp new file mode 100644 index 00000000000000..1ca3271db82537 --- /dev/null +++ b/src/lib/shell/commands/CommandBLE.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 CommandBLEHelp(int argc, char ** argv) +{ + sShellDeviceSubcommands.ForEachCommand(PrintCommandHelp, nullptr); + return 0; +} + +int CommandBLEAdvertise(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 CommandBLEDispatch(int argc, char ** argv) +{ + if (argc == 0) + { + CommandBLEHelp(argc, argv); + return CHIP_NO_ERROR; + } + return sShellDeviceSubcommands.ExecCommand(argc, argv); +} + +void CommandBLEInit() +{ + static const shell_command_t sBLESubCommands[] = { + { &CommandBLEHelp, "help", "Usage: ble " }, + { &CommandBLEAdvertise, "adv", "Enable or disable advertisement. Usage: ble adv " }, + }; + + static const shell_command_t sBLECommand = { &CommandBLEDispatch, "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/CommandBase64.cpp b/src/lib/shell/commands/CommandBase64.cpp new file mode 100644 index 00000000000000..377824552c3901 --- /dev/null +++ b/src/lib/shell/commands/CommandBase64.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 { + +int CommandBase64Help(int argc, char ** argv) +{ + sShellBase64Commands.ForEachCommand(PrintCommandHelp, nullptr); + return 0; +} + +int CommandBase64Decode(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; +} + +int CommandBase64Encode(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; +} + +int CommandBase64Dispatch(int argc, char ** argv) +{ + CHIP_ERROR error = CHIP_NO_ERROR; + + if (argc == 0) + { + CommandBase64Help(argc, argv); + return error; + } + error = sShellBase64Commands.ExecCommand(argc, argv); + return error; +} + +void CommandBase64Init() +{ + /// Subcommands for root command: `base64 ` + static const shell_command_t sBase64SubCommands[] = { + { &CommandBase64Help, "help", "Usage: base64 " }, + { &CommandBase64Encode, "encode", "Encode a hex sting as base64. Usage: base64 encode " }, + { &CommandBase64Decode, "decode", "Decode a base64 sting as hex. Usage: base64 decode " }, + }; + + static const shell_command_t sBase64Command = { &CommandBase64Dispatch, "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.cpp b/src/lib/shell/commands/CommandCommon.cpp similarity index 56% rename from src/lib/shell/commands.cpp rename to src/lib/shell/commands/CommandCommon.cpp index 14afe5d5e8c74f..3c8735293ddf30 100644 --- a/src/lib/shell/commands.cpp +++ b/src/lib/shell/commands/CommandCommon.cpp @@ -20,11 +20,6 @@ * Source implementation of default 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 CommandExit(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 CommandHelp(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 CommandVersion(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 CommandCommonInit() { - streamer_printf(streamer_get(), "CHIP %s\n\r", CHIP_VERSION_STRING); - return 0; -} + static shell_command_t sCmds[] = { + { &CommandExit, "exit", "Exit the shell application" }, + { &CommandHelp, "help", "List out all top level commands" }, + { &CommandVersion, "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/CommandConfig.cpp b/src/lib/shell/commands/CommandConfig.cpp new file mode 100644 index 00000000000000..1ee445a81bb176 --- /dev/null +++ b/src/lib/shell/commands/CommandConfig.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 CommandDeviceConfig(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 CommandConfigInit() +{ + + static const shell_command_t sDeviceComand = { &CommandDeviceConfig, "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/CommandWiFi.cpp b/src/lib/shell/commands/CommandWiFi.cpp new file mode 100644 index 00000000000000..8dfde74304e870 --- /dev/null +++ b/src/lib/shell/commands/CommandWiFi.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 CommandWifiHelp(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 CommandWifiMode(int argc, char ** argv) +{ + if (argc == 0) + { + return PrintWifiMode(); + } + else if (argc != 1) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + return SetWifiMode(argv[0]); +} + +static int CommandWifiConnect(int argc, char ** argv) +{ + if (argc != 2) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + DeviceLayer::DeviceNetworkProvisioningDelegateImpl deviceDelegate; + return deviceDelegate.ProvisionWiFi(argv[0], argv[1]); +} + +static int CommandWifiDispatch(int argc, char ** argv) +{ + if (argc == 0) + { + return CommandWifiHelp(argc, argv); + } + return sShellWifiSubCommands.ExecCommand(argc, argv); +} + +void CommandWifiInit() +{ + /// Subcommands for root command: `device ` + static const shell_command_t sWifiSubCommands[] = { + { &CommandWifiHelp, "help", "" }, + { &CommandWifiMode, "mode", "Get/Set wifi mode. Usage: wifi mode [disable|ap|sta]." }, + { &CommandWifiConnect, "connect", "Connect to AP. Usage: wifi connect ssid psk." }, + }; + static const shell_command_t sWifiCommand = { &CommandWifiDispatch, "wifi", "Usage: wifi " }; + + sShellWifiSubCommands.RegisterCommands(sWifiSubCommands, ArraySize(sWifiSubCommands)); + shell_register(&sWifiCommand, 1); +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/CommandsHelp.cpp b/src/lib/shell/commands/CommandsHelp.cpp new file mode 100644 index 00000000000000..912df511e8ac27 --- /dev/null +++ b/src/lib/shell/commands/CommandsHelp.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/CommandsHelp.h similarity index 84% rename from src/lib/shell/commands.h rename to src/lib/shell/commands/CommandsHelp.h index 79e3969101dc78..c67802c1df8019 100644 --- a/src/lib/shell/commands.h +++ b/src/lib/shell/commands/CommandsHelp.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/shell_core.cpp b/src/lib/shell/shell_core.cpp index 4fefb296061c91..3048a5e5d9059c 100644 --- a/src/lib/shell/shell_core.cpp +++ b/src/lib/shell/shell_core.cpp @@ -22,7 +22,6 @@ */ #include "shell_core.h" -#include "commands.h" #include #include @@ -130,6 +129,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++) { @@ -244,5 +244,29 @@ void Shell::TaskLoop(void * arg) } } +/** 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 } // namespace chip diff --git a/src/lib/shell/shell_core.h b/src/lib/shell/shell_core.h index 555ee57163cece..2b9b46817b5bb7 100644 --- a/src/lib/shell/shell_core.h +++ b/src/lib/shell/shell_core.h @@ -163,29 +163,17 @@ class Shell static void TaskLoop(void * arg); }; -/** 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/platform/device.gni b/src/platform/device.gni index d50d6956675ec2..9c006080669a8d 100644 --- a/src/platform/device.gni +++ b/src/platform/device.gni @@ -38,13 +38,10 @@ 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