From 97cf4ff6000136a59f0b5515822704f9e974c579 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Tue, 8 Feb 2022 12:28:07 +0800 Subject: [PATCH] ESP: Add case command to establish CASESession to another node for TC-SC-4.9(RIO Support) verification. (#13900) * Add case command to establish CASESession to another node for ESP all-clusters-app * Restyled by clang-format * Use a single file ShellCommands.cpp to register the external shell commands * Auto Format * Add check for executing the case command before the last command done * Restyled by clang-format Co-authored-by: Restyled.io --- .../esp32/main/OnOffCommands.cpp | 102 --------- .../esp32/main/ShellCommands.cpp | 52 +++++ .../esp32/main/include/OnOffCommands.h | 47 ---- .../esp32/main/include/ShellCommands.h | 210 ++++++++++++++++++ examples/all-clusters-app/esp32/main/main.cpp | 7 +- 5 files changed, 265 insertions(+), 153 deletions(-) delete mode 100644 examples/all-clusters-app/esp32/main/OnOffCommands.cpp create mode 100644 examples/all-clusters-app/esp32/main/ShellCommands.cpp delete mode 100644 examples/all-clusters-app/esp32/main/include/OnOffCommands.h create mode 100644 examples/all-clusters-app/esp32/main/include/ShellCommands.h diff --git a/examples/all-clusters-app/esp32/main/OnOffCommands.cpp b/examples/all-clusters-app/esp32/main/OnOffCommands.cpp deleted file mode 100644 index 45a393548fcb3e..00000000000000 --- a/examples/all-clusters-app/esp32/main/OnOffCommands.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * - * Copyright (c) 2022 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 -#if CONFIG_DEVICE_LAYER -#include -#endif -#include -#include -#include -#include -#include -#include -#include - -namespace chip { -namespace Shell { -namespace { - -Shell::Engine sSubShell; - -CHIP_ERROR OnOffHandler(int argc, char ** argv) -{ - if (argc == 0) - { - sSubShell.ForEachCommand(PrintCommandHelp, nullptr); - return CHIP_NO_ERROR; - } - - CHIP_ERROR error = sSubShell.ExecCommand(argc, argv); - - if (error != CHIP_NO_ERROR) - { - streamer_printf(streamer_get(), "Error: %" CHIP_ERROR_FORMAT "\r\n", error.Format()); - } - - return error; -} - -static CHIP_ERROR OnLightHandler(int argc, char ** argv) -{ - if (argc != 1) - return CHIP_ERROR_INVALID_ARGUMENT; - chip::app::Clusters::OnOff::Attributes::OnOff::Set(atoi(argv[0]), 1); - return CHIP_NO_ERROR; -} - -static CHIP_ERROR OffLightHandler(int argc, char ** argv) -{ - if (argc != 1) - return CHIP_ERROR_INVALID_ARGUMENT; - chip::app::Clusters::OnOff::Attributes::OnOff::Set(atoi(argv[0]), 0); - return CHIP_NO_ERROR; -} - -static CHIP_ERROR ToggleLightHandler(int argc, char ** argv) -{ - if (argc != 1) - return CHIP_ERROR_INVALID_ARGUMENT; - bool value; - chip::app::Clusters::OnOff::Attributes::OnOff::Get(atoi(argv[0]), &value); - chip::app::Clusters::OnOff::Attributes::OnOff::Set(atoi(argv[0]), !value); - return CHIP_NO_ERROR; -} - -} // namespace - -void OnOffCommands::Register() -{ - static const shell_command_t subCommands[] = { { &OnLightHandler, "on", "Usage: OnOff on endpoint-id" }, - { &OffLightHandler, "off", "Usage: OnOff off endpoint-id" }, - { &ToggleLightHandler, "toggle", "Usage: OnOff toggle endpoint-id" } }; - sSubShell.RegisterCommands(subCommands, ArraySize(subCommands)); - - // Register the root `OnOff` command in the top-level shell. - static const shell_command_t onOffCommand = { &OnOffHandler, "OnOff", "OnOff commands" }; - - Engine::Root().RegisterCommands(&onOffCommand, 1); -} - -} // namespace Shell -} // namespace chip diff --git a/examples/all-clusters-app/esp32/main/ShellCommands.cpp b/examples/all-clusters-app/esp32/main/ShellCommands.cpp new file mode 100644 index 00000000000000..3c7b4822d21558 --- /dev/null +++ b/examples/all-clusters-app/esp32/main/ShellCommands.cpp @@ -0,0 +1,52 @@ +/* + * + * Copyright (c) 2022 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 + +namespace chip { +namespace Shell { + +Shell::Engine OnOffCommands::sSubShell; +void OnOffCommands::Register() +{ + static const shell_command_t subCommands[] = { { &OnLightHandler, "on", "Usage: OnOff on endpoint-id" }, + { &OffLightHandler, "off", "Usage: OnOff off endpoint-id" }, + { &ToggleLightHandler, "toggle", "Usage: OnOff toggle endpoint-id" } }; + sSubShell.RegisterCommands(subCommands, ArraySize(subCommands)); + + // Register the root `OnOff` command in the top-level shell. + static const shell_command_t onOffCommand = { &OnOffHandler, "OnOff", "OnOff commands" }; + + Engine::Root().RegisterCommands(&onOffCommand, 1); +} + +Callback::Callback CASECommands::sOnConnectedCallback(CASECommands::OnConnected, nullptr); +Callback::Callback CASECommands::sOnConnectionFailureCallback(CASECommands::OnConnectionFailure, + nullptr); +Shell::Engine CASECommands::sSubShell; +void CASECommands::Register() +{ + static const shell_command_t subCommands[] = { + { &ConnectToNodeHandler, "connect", "Establish CASESession to a node, Usage: case connect " }, + }; + sSubShell.RegisterCommands(subCommands, ArraySize(subCommands)); + + static const shell_command_t CASECommand = { &CASEHandler, "case", "Case Commands" }; + Engine::Root().RegisterCommands(&CASECommand, 1); +} +} // namespace Shell +} // namespace chip diff --git a/examples/all-clusters-app/esp32/main/include/OnOffCommands.h b/examples/all-clusters-app/esp32/main/include/OnOffCommands.h deleted file mode 100644 index 18674f01003c55..00000000000000 --- a/examples/all-clusters-app/esp32/main/include/OnOffCommands.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * Copyright (c) 2022 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 { - -class OnOffCommands -{ -public: - // delete the copy constructor - OnOffCommands(const OnOffCommands &) = delete; - // delete the move constructor - OnOffCommands(OnOffCommands &&) = delete; - // delete the assignment operator - OnOffCommands & operator=(const OnOffCommands &) = delete; - - static OnOffCommands & GetInstance() - { - static OnOffCommands instance; - return instance; - } - - // Register the OTA provider commands - void Register(); - -private: - OnOffCommands() {} -}; - -} // namespace Shell -} // namespace chip diff --git a/examples/all-clusters-app/esp32/main/include/ShellCommands.h b/examples/all-clusters-app/esp32/main/include/ShellCommands.h new file mode 100644 index 00000000000000..aea17cca138523 --- /dev/null +++ b/examples/all-clusters-app/esp32/main/include/ShellCommands.h @@ -0,0 +1,210 @@ +/* + * + * Copyright (c) 2022 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 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if CONFIG_DEVICE_LAYER +#include +#endif + +namespace chip { +namespace Shell { + +class OnOffCommands +{ +public: + // delete the copy constructor + OnOffCommands(const OnOffCommands &) = delete; + // delete the move constructor + OnOffCommands(OnOffCommands &&) = delete; + // delete the assignment operator + OnOffCommands & operator=(const OnOffCommands &) = delete; + + static OnOffCommands & GetInstance() + { + static OnOffCommands instance; + return instance; + } + + // Register the OnOff commands + void Register(); + +private: + OnOffCommands() {} + + static CHIP_ERROR OnOffHandler(int argc, char ** argv) + { + if (argc == 0) + { + sSubShell.ForEachCommand(PrintCommandHelp, nullptr); + return CHIP_NO_ERROR; + } + + CHIP_ERROR error = sSubShell.ExecCommand(argc, argv); + + if (error != CHIP_NO_ERROR) + { + streamer_printf(streamer_get(), "Error: %" CHIP_ERROR_FORMAT "\r\n", error.Format()); + } + + return error; + } + + static CHIP_ERROR OnLightHandler(int argc, char ** argv) + { + if (argc != 1) + return CHIP_ERROR_INVALID_ARGUMENT; + chip::app::Clusters::OnOff::Attributes::OnOff::Set(atoi(argv[0]), 1); + return CHIP_NO_ERROR; + } + + static CHIP_ERROR OffLightHandler(int argc, char ** argv) + { + if (argc != 1) + return CHIP_ERROR_INVALID_ARGUMENT; + chip::app::Clusters::OnOff::Attributes::OnOff::Set(atoi(argv[0]), 0); + return CHIP_NO_ERROR; + } + + static CHIP_ERROR ToggleLightHandler(int argc, char ** argv) + { + if (argc != 1) + return CHIP_ERROR_INVALID_ARGUMENT; + bool value; + chip::app::Clusters::OnOff::Attributes::OnOff::Get(atoi(argv[0]), &value); + chip::app::Clusters::OnOff::Attributes::OnOff::Set(atoi(argv[0]), !value); + return CHIP_NO_ERROR; + } + + static Shell::Engine sSubShell; +}; + +class CASECommands +{ +public: + // delete the copy constructor + CASECommands(const CASECommands &) = delete; + // delete the move constructor + CASECommands(CASECommands &&) = delete; + // delete the assignment operator + CASECommands & operator=(const CASECommands &) = delete; + + static CASECommands & GetInstance() + { + static CASECommands instance; + return instance; + } + + // Register the CASESession commands + void Register(); + + void SetFabricInfo(FabricInfo * fabricInfo) { mFabricInfo = fabricInfo; } + void SetNodeId(NodeId nodeId) { mNodeId = nodeId; } + void SetOnConnecting(bool onConnecting) { mOnConnecting = onConnecting; } + FabricInfo * GetFabricInfo(void) { return mFabricInfo; } + NodeId GetNodeId(void) { return mNodeId; } + bool GetOnConnecting(void) { return mOnConnecting; } + +private: + CASECommands() {} + static void OnConnected(void * context, OperationalDeviceProxy * deviceProxy) + { + streamer_printf(streamer_get(), "Establish CASESession Success!\r\n"); + GetInstance().SetOnConnecting(false); + } + + static void OnConnectionFailure(void * context, PeerId peerId, CHIP_ERROR error) + { + streamer_printf(streamer_get(), "Establish CASESession Failure!\r\n"); + GetInstance().SetOnConnecting(false); + } + + static void ConnectToNode(intptr_t arg) + { + CASECommands * caseCommand = reinterpret_cast(arg); + Server * server = &(chip::Server::GetInstance()); + CASESessionManager * caseSessionManager = server->GetCASESessionManager(); + if (caseSessionManager == nullptr) + { + ChipLogError(SecureChannel, "Can't get the CASESessionManager"); + return; + } + caseSessionManager->FindOrEstablishSession(caseCommand->GetFabricInfo()->GetPeerIdForNode(caseCommand->GetNodeId()), + &sOnConnectedCallback, &sOnConnectionFailureCallback); + } + + static CHIP_ERROR ConnectToNodeHandler(int argc, char ** argv) + { + if (GetInstance().GetOnConnecting()) + { + return CHIP_ERROR_INCORRECT_STATE; + } + const FabricIndex fabricIndex = static_cast(strtoul(argv[0], nullptr, 10)); + FabricInfo * fabricInfo = Server::GetInstance().GetFabricTable().FindFabricWithIndex(fabricIndex); + + if (fabricInfo == nullptr) + { + streamer_printf(streamer_get(), "Can't get fabric information from FabricIndex\r\n"); + return CHIP_ERROR_NOT_FOUND; + } + GetInstance().SetFabricInfo(fabricInfo); + GetInstance().SetNodeId(static_cast(strtoul(argv[1], nullptr, 10))); + streamer_printf(streamer_get(), "Try to establish CaseSession to NodeId:0x" ChipLogFormatX64 " on fabric index %d\r\n", + ChipLogValueX64(GetInstance().GetNodeId()), fabricIndex); + GetInstance().SetOnConnecting(true); + chip::DeviceLayer::PlatformMgr().ScheduleWork(ConnectToNode, reinterpret_cast(&GetInstance())); + return CHIP_NO_ERROR; + } + + static CHIP_ERROR CASEHandler(int argc, char ** argv) + { + if (argc == 0) + { + sSubShell.ForEachCommand(PrintCommandHelp, nullptr); + return CHIP_NO_ERROR; + } + CHIP_ERROR err = sSubShell.ExecCommand(argc, argv); + if (err != CHIP_NO_ERROR) + { + streamer_printf(streamer_get(), "Error: %" CHIP_ERROR_FORMAT "\r\n", err.Format()); + } + return err; + } + + static Callback::Callback sOnConnectedCallback; + static Callback::Callback sOnConnectionFailureCallback; + static Shell::Engine sSubShell; + FabricInfo * mFabricInfo = nullptr; + NodeId mNodeId = 0; + bool mOnConnecting = false; +}; + +} // namespace Shell +} // namespace chip diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 79c2e590c2f5f6..a50444821800a6 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -26,6 +26,7 @@ #include "OpenThreadLaunch.h" #include "QRCodeScreen.h" #include "ScreenManager.h" +#include "ShellCommands.h" #include "WiFiWidget.h" #include "esp_heap_caps_init.h" #include "esp_log.h" @@ -82,8 +83,6 @@ #include #endif -#include - using namespace ::chip; using namespace ::chip::Shell; using namespace ::chip::Credentials; @@ -614,8 +613,8 @@ extern "C" void app_main() #if CONFIG_ENABLE_CHIP_SHELL chip::LaunchShell(); - OnOffCommands & onOffCommands = OnOffCommands::GetInstance(); - onOffCommands.Register(); + OnOffCommands::GetInstance().Register(); + CASECommands::GetInstance().Register(); #endif // CONFIG_ENABLE_CHIP_SHELL #if CONFIG_OPENTHREAD_ENABLED