From 2418688a8e1a210aa044a711dc5e7d8981aac9a5 Mon Sep 17 00:00:00 2001 From: Kamil Kasperczyk <66371704+kkasperczyk-no@users.noreply.github.com> Date: Fri, 25 Jun 2021 00:33:52 +0200 Subject: [PATCH] Integrated Zephyr shell with the CHIP lib shell (#7848) Currently there was a standalone CHIP shell example using CHIP lib shell and there was a Zephyr shell used only by nrfconnect platform with some specific commands. The goal is to use for Zephyr based platforms the CHIP lib shell but with Zephyr shell backend integrated and be able to use it in every example, not only standalone shell app. For nrfconnect: * Removed custom Zephyr-based ChipShell.cpp and CONFIG_CHIP_ZEPHYR_SHELL option. * Renamed CONFIG_CHIP_STANDALONE_SHELL to the CONFIG_CHIP_LIB_SHELL. * Aligned console docs to the new commands. For all: * Extended existing shell commands by ble adv , onboardingcodes , nfc and device factoryreset. * Added MainLoopZephyr.cpp file for Zephyr-based platforms, that enables Zephyr shell and is kind of integration layer between CHIP shell and Zephyr shell. * Removed from streamer_zephyr.cpp part not using Zephyr shell. --- BUILD.gn | 1 - config/nrfconnect/chip-gn/BUILD.gn | 5 - config/nrfconnect/chip-module/CMakeLists.txt | 18 +- config/zephyr/Kconfig | 11 +- docs/guides/nrfconnect_examples_cli.md | 200 ++++++++++++++----- examples/shell/nrfconnect/prj.conf | 5 +- src/lib/shell/BUILD.gn | 2 + src/lib/shell/Commands.h | 18 ++ src/lib/shell/Engine.cpp | 5 + src/lib/shell/MainLoopZephyr.cpp | 49 +++++ src/lib/shell/commands/BLE.cpp | 15 +- src/lib/shell/commands/BUILD.gn | 14 +- src/lib/shell/commands/Device.cpp | 75 +++++++ src/lib/shell/commands/NFC.cpp | 97 +++++++++ src/lib/shell/commands/OnboardingCodes.cpp | 133 ++++++++++++ src/lib/shell/streamer_zephyr.cpp | 44 ---- src/platform/nrfconnect/shell/BUILD.gn | 26 --- src/platform/nrfconnect/shell/ChipShell.cpp | 197 ------------------ 18 files changed, 568 insertions(+), 347 deletions(-) create mode 100644 src/lib/shell/MainLoopZephyr.cpp create mode 100644 src/lib/shell/commands/Device.cpp create mode 100644 src/lib/shell/commands/NFC.cpp create mode 100644 src/lib/shell/commands/OnboardingCodes.cpp delete mode 100644 src/platform/nrfconnect/shell/BUILD.gn delete mode 100644 src/platform/nrfconnect/shell/ChipShell.cpp diff --git a/BUILD.gn b/BUILD.gn index 496d109b9887be..a24dfeefe04255 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -73,7 +73,6 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") { "${chip_root}/src/lib", "${chip_root}/src/lib/asn1", "${chip_root}/src/lib/core", - "${chip_root}/src/lib/shell", "${chip_root}/src/lib/support", "${chip_root}/src/messaging", "${chip_root}/src/protocols", diff --git a/config/nrfconnect/chip-gn/BUILD.gn b/config/nrfconnect/chip-gn/BUILD.gn index d72432012f41cc..b9a71c9d34334d 100644 --- a/config/nrfconnect/chip-gn/BUILD.gn +++ b/config/nrfconnect/chip-gn/BUILD.gn @@ -21,7 +21,6 @@ assert(current_os == "zephyr") declare_args() { chip_build_pw_rpc_lib = false - chip_build_zephyr_shell = false } group("nrfconnect") { @@ -31,10 +30,6 @@ group("nrfconnect") { deps += [ "${chip_root}/src:tests" ] } - if (chip_build_zephyr_shell) { - deps += [ "${chip_root}/src/platform/nrfconnect/shell:chip-zephyr-shell" ] - } - # Building PW_RPC lib with GN may go obsolete after getting full CMake # support in Pigweed. if (chip_build_pw_rpc_lib) { diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt index 24168590ecdc22..89b21be3aa4c79 100644 --- a/config/nrfconnect/chip-module/CMakeLists.txt +++ b/config/nrfconnect/chip-module/CMakeLists.txt @@ -119,14 +119,6 @@ if (NOT CHIP_LIBRARIES) set(CHIP_LIBRARIES -lCHIP) endif() -if (CONFIG_CHIP_STANDALONE_SHELL) - list(APPEND CHIP_LIBRARIES -lCHIPShell) -endif() - -if (CONFIG_CHIP_ZEPHYR_SHELL) - set(CHIP_ZEPHYR_SHELL_LIBRARY -lCHIPZephyrShell) -endif() - if (CONFIG_CHIP_PW_RPC) list(APPEND CHIP_LIBRARIES -lPwRpc) endif() @@ -206,8 +198,7 @@ chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TE chip_gn_arg_bool ("chip_inet_config_enable_raw_endpoint" CONFIG_CHIP_BUILD_TESTS) chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) chip_gn_arg_bool ("chip_inet_config_enable_dns_resolver" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_STANDALONE_SHELL) -chip_gn_arg_bool ("chip_build_zephyr_shell" CONFIG_CHIP_ZEPHYR_SHELL) +chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) chip_gn_arg_bool ("chip_build_pw_rpc_lib" CONFIG_CHIP_PW_RPC) if (CONFIG_CHIP_ENABLE_DNSSD_SRP) @@ -266,7 +257,12 @@ target_include_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/gen/include ) target_link_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib) -target_link_libraries(chip INTERFACE -Wl,--whole-archive ${CHIP_ZEPHYR_SHELL_LIBRARY} -Wl,--no-whole-archive,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) +if (CONFIG_CHIP_LIB_SHELL) + target_link_options(chip INTERFACE -Wl,--whole-archive -lCHIPShell -Wl,--no-whole-archive) +endif() + +target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) + add_dependencies(chip chip-gn) endif() # CONFIG_CHIP diff --git a/config/zephyr/Kconfig b/config/zephyr/Kconfig index 1dc5c8c47bb602..32f548f455bd84 100644 --- a/config/zephyr/Kconfig +++ b/config/zephyr/Kconfig @@ -47,17 +47,10 @@ config CHIP_OPENTHREAD_CONFIG This option can be used to replace Zephyr-supplied OpenThread configuration file with a custom one. -config CHIP_ZEPHYR_SHELL - bool "Enable Zephyr-based CHIP shell" - default n - imply SHELL - help - Add CHIP commands to the Zephyr shell. - -config CHIP_STANDALONE_SHELL +config CHIP_LIB_SHELL bool "Enable CHIP shell library" default n - depends on !CHIP_ZEPHYR_SHELL + imply SHELL help Link the application with the library containing CHIP shell commands. diff --git a/docs/guides/nrfconnect_examples_cli.md b/docs/guides/nrfconnect_examples_cli.md index a0bf5ddf41dd18..8d35aa1be12149 100644 --- a/docs/guides/nrfconnect_examples_cli.md +++ b/docs/guides/nrfconnect_examples_cli.md @@ -90,114 +90,220 @@ Done The nRF Connect SDK examples let you use several CHIP-specific CLI commands. -These commands are not available in the standard Zephyr shell. +These commands are not available by default and to enable using them, set the +`CONFIG_CHIP_LIB_SHELL=y` Kconfig option in the `prj.conf` file of the given +example. -They are currently used for testing purposes and allow only to get some -information about CHIP stack state, but not to modify it. +Every invoked command must be preceded by the `matter` prefix. -To enable using CHIP commands, set the `CONFIG_CHIP_ZEPHYR_SHELL=y` Kconfig -option in the `prj.conf` file of the given example. +See the following subsections for the description of each CHIP-specific command. + +### device -### Listing CHIP-specific commands +Handles a group of commands that are used to manage the device. You must use +this command together with one of the additional subcommands listed below. -To list all available CHIP-specific commands, enter `chip` in the command line -and press the Tab key. This will list the available commands: +#### factoryreset + +Performs device factory reset that is hardware reset preceded by erasing of the +whole CHIP settings stored in a non-volatile memory. ```shell -uart:~$ chip - qrcode qrcodeurl setuppincode discriminator - vendorid productid manualpairingcode bleadvertising - nfcemulation +uart:~$ matter factoryreset +Performing factory reset ... ``` -See the following subsections for the description of each CHIP-specific command. +### onboardingcodes + +Handles a group of commands that are used to view information about device +onboarding codes. You can use this command without any subcommand to print all +available onboarding codes or to add a specific subcommand. + +```shell +uart:~$ matter onboardingcodes +QRCode: MT:W0GU2OTB00KA0648G00 +QRCodeUrl: https://dhrishi.github.io/connectedhomeip/qrcode.html?data=MT%3AW0GU2OTB00KA0648G00 +ManualPairingCode: 34970112332 +``` -#### `qrcode` +The `onboardingcodes` command can also take the subcommands listed below. + +#### qrcode Prints the device [onboarding QR code payload](https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/nrfconnect_android_commissioning.md#preparing-accessory-device). Takes no arguments. ```shell -uart:~$ chip qrcode +uart:~$ matter onboardingcodes qrcode MT:W0GU2OTB00KA0648G00 ``` -#### `qrcodeurl` +#### qrcodeurl Prints the URL to view the [device onboarding QR code](https://github.com/project-chip/connectedhomeip/blob/master/docs/guides/nrfconnect_android_commissioning.md#preparing-accessory-device) in a web browser. Takes no arguments. ```shell -uart:~$ chip qrcodeurl -https://dhrishi.github.io/connectedhomeip/qrcode.html?data=CH%3AH34.GHY00%200C9SS0 +uart:~$ matter onboardingcodes qrcodeurl +https://dhrishi.github.io/connectedhomeip/qrcode.html?data=MT%3AW0GU2OTB00KA0648G00 +``` + +#### manualpairingcode + +Prints the pairing code for the manual onboarding of a device. Takes no +arguments. + +```shell +uart:~$ matter onboardingcodes manualpairingcode +34970112332 +``` + +### config + +Handles a group of commands that are used to view device configuration +information. You can use this command without any subcommand to print all +available configuration data or to add a specific subcommand. + +```shell +VendorId: 9050 (0x235A) +ProductId: 20043 (0x4E4B) +ProductRevision: 1 (0x1) +FabricId: +PinCode: 020202021 +Discriminator: f00 +DeviceId: ``` -#### `setuppincode` +The `config` command can also take the subcommands listed below. + +#### pincode Prints the PIN code for device setup. Takes no arguments. ```shell -uart:~$ chip setuppincode -12345678 +uart:~$ matter config pincode +020202021 ``` -#### `discriminator` +#### discriminator Prints the device setup discriminator. Takes no arguments. ```shell -uart:~$ chip discriminator -3840 +uart:~$ matter config discriminator +f00 ``` -#### `vendorid` +#### vendorid Prints the vendor ID of the device. Takes no arguments. ```shell -uart:~$ chip vendorid -9050 +uart:~$ matter config vendorid +9050 (0x235A) ``` -#### `productid` +#### productid Prints the product ID of the device. Takes no arguments. ```shell -uart:~$ chip productid -20043 +uart:~$ matter config productid +20043 (0x4E4B) ``` -#### `manualpairingcode` +#### productrev -Prints the pairing code for the manual onboarding of a device. Takes no -arguments. +Prints the product revision of the device. Takes no arguments. ```shell -uart:~$ chip manualpairingcode -35767807533 +uart:~$ matter config productrev +1 (0x1) ``` -#### `bleadvertising` +#### deviceid -Prints the information about the Bluetooth LE advertising status, either `0` if -the advertising is disabled on the device or `1` if it is enabled. Takes no -arguments. +Prints the device identifier. Takes no arguments. + +#### fabricid + +Prints the fabric identifier. Takes no arguments. + +### ble + +Handles a group of commands that are used to control the device Bluetooth LE +transport state. You must use this command together with one of the additional +subcommands listed below. + +#### help + +Prints help information about `ble` commands group. ```shell -uart:~$ chip bleadvertising -0 +uart:~$ matter ble help + help Usage: ble + adv Enable or disable advertisement. Usage: ble adv ``` -#### `nfcemulation` +#### adv start -Prints the information about the NFC tag emulation status, either `0` if the -emulation is disabled on the device or `1` if it is enabled (1). Takes no -arguments. +Enables Bluetooth LE advertising. + +```shell +uart:~$ matter ble adv start +Starting BLE advertising +``` + +#### adv stop + +Disables Bluetooth LE advertising. + +```shell +uart:~$ matter ble adv stop +Stopping BLE advertising +``` + +#### adv status + +Prints the information about the current Bluetooth LE advertising status. + +```shell +uart:~$ matter ble adv state +BLE advertising is disabled + +``` + +### nfc + +Handles a group of commands that are used to control the device NFC tag +emulation state. You must use this command together with one of the additional +subcommands listed below. + +#### start + +Starts the NFC tag emulation. + +```shell +uart:~$ matter nfc start +NFC tag emulation started +``` + +#### stop + +Stops the NFC tag emulation. + +```shell +uart:~$ matter nfc stop +NFC tag emulation stopped +``` + +#### state + +Prints the information about the NFC tag emulation status. ```shell -uart:~$ chip nfcemulation -0 +uart:~$ matter nfc state +NFC tag emulation is disabled ``` diff --git a/examples/shell/nrfconnect/prj.conf b/examples/shell/nrfconnect/prj.conf index 3d74a32b3942fa..351d4507bdc905 100644 --- a/examples/shell/nrfconnect/prj.conf +++ b/examples/shell/nrfconnect/prj.conf @@ -22,10 +22,7 @@ CONFIG_DK_LIBRARY=y # Configure CHIP shell -CONFIG_CHIP_STANDALONE_SHELL=y -CONFIG_CONSOLE_SUBSYS=y -CONFIG_CONSOLE_GETCHAR=y -CONFIG_SHELL=n +CONFIG_CHIP_LIB_SHELL=y CONFIG_OPENTHREAD_SHELL=n # Some shell commands require OpenThread FTD configuration diff --git a/src/lib/shell/BUILD.gn b/src/lib/shell/BUILD.gn index d94104278d4df8..c085639113d792 100644 --- a/src/lib/shell/BUILD.gn +++ b/src/lib/shell/BUILD.gn @@ -58,6 +58,8 @@ static_library("shell") { "MainLoopDefault.cpp", "streamer_k32w.cpp", ] + } else if (current_os == "zephyr") { + sources += [ "MainLoopZephyr.cpp" ] } else { sources += [ "MainLoopDefault.cpp" ] } diff --git a/src/lib/shell/Commands.h b/src/lib/shell/Commands.h index 762df6ac822db7..84a325b4279f6c 100644 --- a/src/lib/shell/Commands.h +++ b/src/lib/shell/Commands.h @@ -44,11 +44,29 @@ void RegisterMetaCommands(); */ void RegisterConfigCommands(); +/** + * This function registers the device management commands. + * + */ +void RegisterDeviceCommands(); + +/** + * This function registers the device onboarding codes commands. + * + */ +void RegisterOnboardingCodesCommands(); + /** * This function registers the wifi commands. * */ void RegisterWiFiCommands(); +/** + * This function registers the NFC commands. + * + */ +void RegisterNFCCommands(); + } // namespace Shell } // namespace chip diff --git a/src/lib/shell/Engine.cpp b/src/lib/shell/Engine.cpp index 616cf528c9fdad..2f2fcd58bc3830 100644 --- a/src/lib/shell/Engine.cpp +++ b/src/lib/shell/Engine.cpp @@ -105,6 +105,11 @@ void Engine::RegisterDefaultCommands() #endif #if CONFIG_DEVICE_LAYER RegisterConfigCommands(); + RegisterDeviceCommands(); + RegisterOnboardingCodesCommands(); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_NFC + RegisterNFCCommands(); #endif } diff --git a/src/lib/shell/MainLoopZephyr.cpp b/src/lib/shell/MainLoopZephyr.cpp new file mode 100644 index 00000000000000..58bc4c2aa63bd6 --- /dev/null +++ b/src/lib/shell/MainLoopZephyr.cpp @@ -0,0 +1,49 @@ +/* + * + * 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 + +using chip::Shell::Engine; + +static int cmd_matter(const struct shell * shell, size_t argc, char ** argv) +{ + return (Engine::Root().ExecCommand(argc - 1, argv + 1) == CHIP_NO_ERROR) ? 0 : -ENOEXEC; +} + +static int RegisterCommands(const struct device * dev) +{ + Engine::Root().RegisterDefaultCommands(); + return 0; +} + +SYS_INIT(RegisterCommands, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); + +SHELL_CMD_ARG_REGISTER(matter, NULL, "Matter commands", cmd_matter, 1, 4); + +namespace chip { +namespace Shell { + +void Engine::RunMainLoop() +{ + // Intentionally empty as Zephyr has own thread handling shell +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/BLE.cpp b/src/lib/shell/commands/BLE.cpp index 0843c454192d62..b652638c93138a 100644 --- a/src/lib/shell/commands/BLE.cpp +++ b/src/lib/shell/commands/BLE.cpp @@ -72,12 +72,23 @@ int BLEAdvertiseHandler(int argc, char ** argv) streamer_printf(sout, "BLE advertising already stopped\r\n"); } } + else if (strcmp(argv[0], "state") == 0) + { + if (adv_enabled) + { + streamer_printf(sout, "BLE advertising is enabled\r\n"); + } + else + { + streamer_printf(sout, "BLE advertising is disabled\r\n"); + } + } else { return CHIP_ERROR_INVALID_ARGUMENT; } - return CHIP_NO_ERROR; + return error; } int BLEDispatch(int argc, char ** argv) @@ -94,7 +105,7 @@ void RegisterBLECommands() { static const shell_command_t sBLESubCommands[] = { { &BLEHelpHandler, "help", "Usage: ble " }, - { &BLEAdvertiseHandler, "adv", "Enable or disable advertisement. Usage: ble adv " }, + { &BLEAdvertiseHandler, "adv", "Enable or disable advertisement. Usage: ble adv " }, }; static const shell_command_t sBLECommand = { &BLEDispatch, "ble", "BLE transport commands" }; diff --git a/src/lib/shell/commands/BUILD.gn b/src/lib/shell/commands/BUILD.gn index 31717149136288..b4ec8c1efabc2d 100644 --- a/src/lib/shell/commands/BUILD.gn +++ b/src/lib/shell/commands/BUILD.gn @@ -26,7 +26,11 @@ source_set("commands") { ] if (chip_device_platform != "none") { - sources += [ "Config.cpp" ] + sources += [ + "Config.cpp", + "Device.cpp", + "OnboardingCodes.cpp", + ] } if (chip_enable_wifi) { @@ -37,5 +41,13 @@ source_set("commands") { sources += [ "BLE.cpp" ] } + if (chip_enable_nfc && chip_device_platform != "none") { + sources += [ "NFC.cpp" ] + } + public_deps = [ "${chip_root}/src/lib/shell:shell_core" ] + + if (chip_device_platform != "none") { + public_deps += [ "${chip_root}/src/app/server" ] + } } diff --git a/src/lib/shell/commands/Device.cpp b/src/lib/shell/commands/Device.cpp new file mode 100644 index 00000000000000..6b176292880f1f --- /dev/null +++ b/src/lib/shell/commands/Device.cpp @@ -0,0 +1,75 @@ +/* + * + * 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::Engine sShellDeviceSubcommands; + +int DeviceHelpHandler(int argc, char ** argv) +{ + sShellDeviceSubcommands.ForEachCommand(PrintCommandHelp, nullptr); + return 0; +} + +static int FactoryResetHandler(int argc, char ** argv) +{ + streamer_printf(streamer_get(), "Performing factory reset ... \r\n"); + DeviceLayer::ConfigurationMgr().InitiateFactoryReset(); + return 0; +} + +static int DeviceHandler(int argc, char ** argv) +{ + if (argc == 0) + { + DeviceHelpHandler(argc, argv); + return CHIP_NO_ERROR; + } + return sShellDeviceSubcommands.ExecCommand(argc, argv); +} + +void RegisterDeviceCommands() +{ + static const shell_command_t sDeviceSubCommands[] = { + { &FactoryResetHandler, "factoryreset", "Performs device factory reset" }, + }; + + static const shell_command_t sDeviceComand = { &DeviceHandler, "device", "Device management commands" }; + + // Register `device` subcommands with the local shell dispatcher. + sShellDeviceSubcommands.RegisterCommands(sDeviceSubCommands, ArraySize(sDeviceSubCommands)); + + // Register the root `device` command with the top-level shell. + Engine::Root().RegisterCommands(&sDeviceComand, 1); +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/NFC.cpp b/src/lib/shell/commands/NFC.cpp new file mode 100644 index 00000000000000..ba101b0863d39c --- /dev/null +++ b/src/lib/shell/commands/NFC.cpp @@ -0,0 +1,97 @@ +/* + * + * 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 +#include + +using chip::DeviceLayer::ConnectivityMgr; + +namespace chip { +namespace Shell { + +static int NFCHandler(int argc, char ** argv) +{ + CHIP_ERROR error = CHIP_NO_ERROR; + streamer_t * sout = streamer_get(); + bool nfcEnabled; + + VerifyOrReturnError(argc == 1, error = CHIP_ERROR_INVALID_ARGUMENT); + + nfcEnabled = chip::DeviceLayer::NFCMgr().IsTagEmulationStarted(); + + if (strcmp(argv[0], "start") == 0) + { + if (nfcEnabled) + { + streamer_printf(sout, "NFC tag emulation is already enabled\r\n"); + } + else + { + ShareQRCodeOverNFC(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); + streamer_printf(sout, "NFC tag emulation started\r\n"); + } + } + else if (strcmp(argv[0], "stop") == 0) + { + if (nfcEnabled) + { + chip::DeviceLayer::NFCMgr().StopTagEmulation(); + streamer_printf(sout, "NFC tag emulation stopped\r\n"); + } + else + { + streamer_printf(sout, "NFC tag emulation is already disabled\r\n"); + } + } + else if (strcmp(argv[0], "state") == 0) + { + if (nfcEnabled) + { + streamer_printf(sout, "NFC tag emulation is enabled\r\n"); + } + else + { + streamer_printf(sout, "NFC tag emulation is disabled\r\n"); + } + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + return error; +} + +void RegisterNFCCommands() +{ + static const shell_command_t sDeviceComand = { &NFCHandler, "nfc", + "Start, stop or get nfc emulation state. Usage: nfc " }; + + // Register the root `device` command with the top-level shell. + Engine::Root().RegisterCommands(&sDeviceComand, 1); +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/commands/OnboardingCodes.cpp b/src/lib/shell/commands/OnboardingCodes.cpp new file mode 100644 index 00000000000000..2d7d82ab901cf4 --- /dev/null +++ b/src/lib/shell/commands/OnboardingCodes.cpp @@ -0,0 +1,133 @@ +/* + * + * 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 + +#define CHIP_SHELL_MAX_BUFFER_SIZE 128 + +using chip::DeviceLayer::ConfigurationMgr; + +namespace chip { +namespace Shell { + +static CHIP_ERROR GetOnboardingQRCode(bool printHeader) +{ + streamer_t * sout = streamer_get(); + std::string QRCode; + + if (printHeader) + { + streamer_printf(sout, "QRCode: "); + } + ReturnErrorOnFailure(GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE))); + streamer_printf(sout, "%s\r\n", QRCode.c_str()); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR GetOnboardingQRCodeUrl(bool printHeader) +{ + streamer_t * sout = streamer_get(); + std::string QRCode; + + if (printHeader) + { + streamer_printf(sout, "QRCodeUrl: "); + } + ReturnErrorOnFailure(GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE))); + + char qrCodeBuffer[CHIP_SHELL_MAX_BUFFER_SIZE]; + + ReturnErrorOnFailure(GetQRCodeUrl(qrCodeBuffer, sizeof(qrCodeBuffer), QRCode)); + streamer_printf(sout, "%s\r\n", qrCodeBuffer); + return CHIP_NO_ERROR; +} + +static CHIP_ERROR GetOnboardingManualPairingCode(bool printHeader) +{ + streamer_t * sout = streamer_get(); + std::string manualPairingCode; + + if (printHeader) + { + streamer_printf(sout, "ManualPairingCode: "); + } + ReturnErrorOnFailure( + GetManualPairingCode(manualPairingCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE))); + streamer_printf(sout, "%s\r\n", manualPairingCode.c_str()); + return CHIP_NO_ERROR; +} + +static int PrintAllOnboardingCodes() +{ + CHIP_ERROR error = CHIP_NO_ERROR; + + error |= GetOnboardingQRCode(true); + error |= GetOnboardingQRCodeUrl(true); + error |= GetOnboardingManualPairingCode(true); + + return error; +} + +static int OnboardingHandler(int argc, char ** argv) +{ + CHIP_ERROR error = CHIP_NO_ERROR; + + if (argc == 0) + { + return PrintAllOnboardingCodes(); + } + + if (strcmp(argv[0], "qrcode") == 0) + { + return GetOnboardingQRCode(false); + } + else if (strcmp(argv[0], "qrcodeurl") == 0) + { + return error = GetOnboardingQRCodeUrl(false); + } + else if (strcmp(argv[0], "manualpairingcode") == 0) + { + return error = GetOnboardingManualPairingCode(false); + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } +} + +void RegisterOnboardingCodesCommands() +{ + + static const shell_command_t sDeviceComand = { &OnboardingHandler, "onboardingcodes", + "Dump device onboarding codes. Usage: onboardingcodes [param_name]" }; + + // Register the root `device` command with the top-level shell. + Engine::Root().RegisterCommands(&sDeviceComand, 1); + return; +} + +} // namespace Shell +} // namespace chip diff --git a/src/lib/shell/streamer_zephyr.cpp b/src/lib/shell/streamer_zephyr.cpp index 00f8c462ca6729..8f93e8252ab175 100644 --- a/src/lib/shell/streamer_zephyr.cpp +++ b/src/lib/shell/streamer_zephyr.cpp @@ -24,24 +24,13 @@ #include -#if CONFIG_SHELL #include #include -#else -#include -#endif namespace chip { namespace Shell { namespace { -#if CONFIG_SHELL - -// NOTE: -// We assume that if Zephyr shell is enabled it will be responsible for reading and parsing -// a command line. Therefore, we only have to provide implentation for streamer_write() and -// may omit streamer_read() part. - int streamer_zephyr_init(streamer_t * streamer) { ARG_UNUSED(streamer); @@ -63,39 +52,6 @@ ssize_t streamer_zephyr_write(streamer_t * streamer, const char * buffer, size_t return length; } -#else // CONFIG_SHELL - -int streamer_zephyr_init(streamer_t * streamer) -{ - ARG_UNUSED(streamer); - return console_init(); -} - -ssize_t streamer_zephyr_read(streamer_t * streamer, char * buffer, size_t length) -{ - ARG_UNUSED(streamer); - const ssize_t rc = console_read(nullptr, buffer, length); - return rc >= 0 ? rc : 0; -} - -ssize_t streamer_zephyr_write(streamer_t * streamer, const char * buffer, size_t length) -{ - ARG_UNUSED(streamer); - size_t written = 0; - - while (written < length) - { - const ssize_t rc = console_write(nullptr, buffer + written, length - written); - if (rc <= 0) // error - break; - written += static_cast(rc); - } - - return written; -} - -#endif // CONFIG_SHELL - static streamer_t streamer_zephyr = { .init_cb = streamer_zephyr_init, .read_cb = streamer_zephyr_read, diff --git a/src/platform/nrfconnect/shell/BUILD.gn b/src/platform/nrfconnect/shell/BUILD.gn deleted file mode 100644 index 04874cd17e6930..00000000000000 --- a/src/platform/nrfconnect/shell/BUILD.gn +++ /dev/null @@ -1,26 +0,0 @@ -# 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") - -static_library("chip-zephyr-shell") { - output_name = "libCHIPZephyrShell" - output_dir = "${root_out_dir}/lib" - - sources = [ "ChipShell.cpp" ] - - cflags = [ "-Wconversion" ] - - public_deps = [ "${chip_root}/src/app/server" ] -} diff --git a/src/platform/nrfconnect/shell/ChipShell.cpp b/src/platform/nrfconnect/shell/ChipShell.cpp deleted file mode 100644 index f81e71478b548a..00000000000000 --- a/src/platform/nrfconnect/shell/ChipShell.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * All rights reserved. - * - * 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 "shell/shell.h" - -#define CHIP_SHELL_MAX_BUFFER_SIZE 128 - -using namespace chip::DeviceLayer; - -static int cmd_chip_qrcode(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - std::string QRCode; - - error = GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); - - if (error == CHIP_NO_ERROR) - { - shell_print(shell, "%s", QRCode.c_str()); - } - else - { - shell_print(shell, "Getting QR code failed with error: %d", error); - } - - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_qrcodeurl(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - std::string QRCode; - - error = GetQRCode(QRCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); - VerifyOrExit(error == CHIP_NO_ERROR, shell_print(shell, "Getting QR code failed with error: %d", error)); - - char qrCodeBuffer[CHIP_SHELL_MAX_BUFFER_SIZE]; - - error = GetQRCodeUrl(qrCodeBuffer, sizeof(qrCodeBuffer), QRCode); - VerifyOrExit(error == CHIP_NO_ERROR, shell_print(shell, "Getting QR code url failed with error: %d", error)); - - shell_print(shell, "%s", qrCodeBuffer); - -exit: - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_setuppincode(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - uint32_t setupPinCode; - - error = ConfigurationMgr().GetSetupPinCode(setupPinCode); - if (error == CHIP_NO_ERROR) - { - shell_print(shell, "%d", setupPinCode); - } - else - { - shell_print(shell, "Getting Setup Pin Code failed with error: %d", error); - } - - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_discriminator(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - uint16_t discriminator; - - error = ConfigurationMgr().GetSetupDiscriminator(discriminator); - if (error == CHIP_NO_ERROR) - { - shell_print(shell, "%d", discriminator); - } - else - { - shell_print(shell, "Getting Setup Discriminator failed with error: %d", error); - } - - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_vendorid(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - uint16_t vendorId; - - error = ConfigurationMgr().GetVendorId(vendorId); - if (error == CHIP_NO_ERROR) - { - shell_print(shell, "%d", vendorId); - } - else - { - shell_print(shell, "Getting Vendor ID failed with error: %d", error); - } - - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_productid(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - uint16_t productId; - - error = ConfigurationMgr().GetProductId(productId); - if (error == CHIP_NO_ERROR) - { - shell_print(shell, "%d", productId); - } - else - { - shell_print(shell, "Getting Product ID failed with error: %d", error); - } - - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_manualpairingcode(const struct shell * shell, size_t argc, char ** argv) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - std::string manualPairingCode; - - error = GetManualPairingCode(manualPairingCode, chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); - if (error == CHIP_NO_ERROR) - { - shell_print(shell, "%s", manualPairingCode.c_str()); - } - else - { - shell_print(shell, "Getting Manual Pairing Code failed with error: %d", error); - } - - return (error == CHIP_NO_ERROR) ? 0 : -ENOEXEC; -} - -static int cmd_chip_bleadvertising(const struct shell * shell, size_t argc, char ** argv) -{ - if (ConnectivityMgr().IsBLEAdvertisingEnabled()) - { - shell_print(shell, "1"); - } - else - { - shell_print(shell, "0"); - } - - return 0; -} - -static int cmd_chip_nfcemulation(const struct shell * shell, size_t argc, char ** argv) -{ - if (NFCMgr().IsTagEmulationStarted()) - { - shell_print(shell, "1"); - } - else - { - shell_print(shell, "0"); - } - - return 0; -} - -SHELL_STATIC_SUBCMD_SET_CREATE(chipCommands, SHELL_CMD_ARG(qrcode, NULL, "qrcode", cmd_chip_qrcode, 0, 0), - SHELL_CMD_ARG(qrcodeurl, NULL, "qrcodeurl", cmd_chip_qrcodeurl, 0, 0), - SHELL_CMD_ARG(setuppincode, NULL, "setuppincode", cmd_chip_setuppincode, 0, 0), - SHELL_CMD_ARG(discriminator, NULL, "discriminator", cmd_chip_discriminator, 0, 0), - SHELL_CMD_ARG(vendorid, NULL, "vendorid", cmd_chip_vendorid, 0, 0), - SHELL_CMD_ARG(productid, NULL, "productid", cmd_chip_productid, 0, 0), - SHELL_CMD_ARG(manualpairingcode, NULL, "manualpairingcode", cmd_chip_manualpairingcode, 0, 0), - SHELL_CMD_ARG(bleadvertising, NULL, "bleadvertising", cmd_chip_bleadvertising, 0, 0), - SHELL_CMD_ARG(nfcemulation, NULL, "nfcemulation", cmd_chip_nfcemulation, 0, 0), - SHELL_SUBCMD_SET_END /* Array terminated. */ -); - -SHELL_CMD_REGISTER(chip, &chipCommands, "CHIP commands", NULL);