diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 2d7947d5033915..a255731bd8c1b3 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -44,6 +44,8 @@ config("config") { static_library("chip-tool-utils") { sources = [ "commands/clusters/ModelCommand.cpp", + "commands/common/CHIPCommand.cpp", + "commands/common/CHIPCommand.h", "commands/common/Command.cpp", "commands/common/Commands.cpp", "commands/discover/DiscoverCommand.cpp", diff --git a/examples/chip-tool/commands/clusters/ModelCommand.cpp b/examples/chip-tool/commands/clusters/ModelCommand.cpp index 35946ec6ed97be..818db0862d7a6c 100644 --- a/examples/chip-tool/commands/clusters/ModelCommand.cpp +++ b/examples/chip-tool/commands/clusters/ModelCommand.cpp @@ -23,16 +23,14 @@ using namespace ::chip; -CHIP_ERROR ModelCommand::Run() +CHIP_ERROR ModelCommand::Run(NodeId remoteId) { CHIP_ERROR err = CHIP_NO_ERROR; - auto * ctx = GetExecContext(); - - err = ctx->commissioner->GetConnectedDevice(ctx->remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); + err = mController.GetConnectedDevice(remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %" CHIP_ERROR_FORMAT, - ctx->remoteId, err.Format())); + remoteId, err.Format())); exit: return err; diff --git a/examples/chip-tool/commands/clusters/ModelCommand.h b/examples/chip-tool/commands/clusters/ModelCommand.h index 27b67febe68a67..2fdb9b73a42136 100644 --- a/examples/chip-tool/commands/clusters/ModelCommand.h +++ b/examples/chip-tool/commands/clusters/ModelCommand.h @@ -19,7 +19,7 @@ #pragma once #include "../../config/PersistentStorage.h" -#include "../common/Command.h" +#include "../common/CHIPCommand.h" #include #include #include @@ -28,18 +28,20 @@ #define CHIP_ZCL_ENDPOINT_MIN 0x00 #define CHIP_ZCL_ENDPOINT_MAX 0xF0 -class ModelCommand : public Command +class ModelCommand : public CHIPCommand { public: + using ChipDevice = ::chip::Controller::Device; + ModelCommand(const char * commandName) : - Command(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), + CHIPCommand(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this) {} void AddArguments() { AddArgument("endpoint-id", CHIP_ZCL_ENDPOINT_MIN, CHIP_ZCL_ENDPOINT_MAX, &mEndPointId); } - /////////// Command Interface ///////// - CHIP_ERROR Run() override; + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return 10; } virtual CHIP_ERROR SendCommand(ChipDevice * device, uint8_t endPointId) = 0; @@ -47,7 +49,7 @@ class ModelCommand : public Command private: uint8_t mEndPointId; - static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device); + static void OnDeviceConnectedFn(void * context, ChipDevice * device); static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error); chip::Callback::Callback mOnDeviceConnectedCallback; diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp new file mode 100644 index 00000000000000..dd6d2932f1050a --- /dev/null +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -0,0 +1,144 @@ +/* + * 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 "CHIPCommand.h" + +#include +#include +#include +#include +#include +#include +#include + +using DeviceControllerFactory = chip::Controller::DeviceControllerFactory; + +CHIP_ERROR CHIPCommand::Run() +{ +#if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE + // By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central. + ReturnLogErrorOnFailure(chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(0, true)); +#endif + + ReturnLogErrorOnFailure(mStorage.Init()); + ReturnLogErrorOnFailure(mOpCredsIssuer.Initialize(mStorage)); + + chip::Platform::ScopedMemoryBuffer noc; + chip::Platform::ScopedMemoryBuffer icac; + chip::Platform::ScopedMemoryBuffer rcac; + + chip::Credentials::SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider()); + chip::Credentials::SetDeviceAttestationVerifier(chip::Credentials::Examples::GetExampleDACVerifier()); + + VerifyOrReturnError(noc.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY); + VerifyOrReturnError(icac.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY); + VerifyOrReturnError(rcac.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY); + + chip::MutableByteSpan nocSpan(noc.Get(), chip::Controller::kMaxCHIPDERCertLength); + chip::MutableByteSpan icacSpan(icac.Get(), chip::Controller::kMaxCHIPDERCertLength); + chip::MutableByteSpan rcacSpan(rcac.Get(), chip::Controller::kMaxCHIPDERCertLength); + + chip::Crypto::P256Keypair ephemeralKey; + ReturnLogErrorOnFailure(ephemeralKey.Initialize()); + + // TODO - OpCreds should only be generated for pairing command + // store the credentials in persistent storage, and + // generate when not available in the storage. + ReturnLogErrorOnFailure(mOpCredsIssuer.GenerateNOCChainAfterValidation(mStorage.GetLocalNodeId(), 0, ephemeralKey.Pubkey(), + rcacSpan, icacSpan, nocSpan)); + + chip::Controller::FactoryInitParams factoryInitParams; + factoryInitParams.storageDelegate = &mStorage; + factoryInitParams.listenPort = mStorage.GetListenPort(); + + chip::Controller::SetupParams commissionerParams; + commissionerParams.operationalCredentialsDelegate = &mOpCredsIssuer; + commissionerParams.ephemeralKeypair = &ephemeralKey; + commissionerParams.controllerRCAC = rcacSpan; + commissionerParams.controllerICAC = icacSpan; + commissionerParams.controllerNOC = nocSpan; + + ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryInitParams)); + ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().SetupCommissioner(commissionerParams, mController)); + + chip::DeviceLayer::PlatformMgr().ScheduleWork(RunQueuedCommand, reinterpret_cast(this)); + ReturnLogErrorOnFailure(StartWaiting(GetWaitDurationInSeconds())); + + Shutdown(); + + // + // We can call DeviceController::Shutdown() safely without grabbing the stack lock + // since the CHIP thread and event queue have been stopped, preventing any thread + // races. + // + ReturnLogErrorOnFailure(mController.Shutdown()); + + return CHIP_NO_ERROR; +} + +void CHIPCommand::RunQueuedCommand(intptr_t commandArg) +{ + auto * command = reinterpret_cast(commandArg); + CHIP_ERROR err = command->Run(command->mStorage.GetRemoteNodeId()); + if (err != CHIP_NO_ERROR) + { + command->SetCommandExitStatus(err); + } +} + +#if !CONFIG_USE_SEPARATE_EVENTLOOP +static void OnResponseTimeout(chip::System::Layer *, void * appState) +{ + (reinterpret_cast(appState))->SetCommandExitStatus(CHIP_ERROR_TIMEOUT); +} +#endif // !CONFIG_USE_SEPARATE_EVENTLOOP + +CHIP_ERROR CHIPCommand::StartWaiting(uint16_t seconds) +{ +#if CONFIG_USE_SEPARATE_EVENTLOOP + // ServiceEvents() calls StartEventLoopTask(), which is paired with the StopEventLoopTask() below. + ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().ServiceEvents()); + auto waitingUntil = std::chrono::system_clock::now() + std::chrono::seconds(seconds); + { + std::unique_lock lk(cvWaitingForResponseMutex); + if (!cvWaitingForResponse.wait_until(lk, waitingUntil, [this]() { return !this->mWaitingForResponse; })) + { + mCommandExitStatus = CHIP_ERROR_TIMEOUT; + } + } + LogErrorOnFailure(chip::DeviceLayer::PlatformMgr().StopEventLoopTask()); +#else + ReturnLogErrorOnFailure(chip::DeviceLayer::SystemLayer().StartTimer(seconds * 1000, OnResponseTimeout, this)); + chip::DeviceLayer::PlatformMgr().RunEventLoop(); +#endif // CONFIG_USE_SEPARATE_EVENTLOOP + + return mCommandExitStatus; +} + +void CHIPCommand::StopWaiting() +{ +#if CONFIG_USE_SEPARATE_EVENTLOOP + { + std::lock_guard lk(cvWaitingForResponseMutex); + mWaitingForResponse = false; + } + cvWaitingForResponse.notify_all(); +#else // CONFIG_USE_SEPARATE_EVENTLOOP + LogErrorOnFailure(chip::DeviceLayer::PlatformMgr().StopEventLoopTask()); +#endif // CONFIG_USE_SEPARATE_EVENTLOOP +} diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h new file mode 100644 index 00000000000000..ab916a49679032 --- /dev/null +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -0,0 +1,83 @@ +/* + * 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. + * + */ + +#pragma once + +#include "../../config/PersistentStorage.h" +#include "Command.h" + +#pragma once + +class PersistentStorage; + +class CHIPCommand : public Command +{ +public: + using ChipDevice = ::chip::Controller::Device; + using ChipDeviceCommissioner = ::chip::Controller::DeviceCommissioner; + using ChipDeviceController = ::chip::Controller::DeviceController; + using ChipSerializedDevice = ::chip::Controller::SerializedDevice; + using IPAddress = ::chip::Inet::IPAddress; + using NodeId = ::chip::NodeId; + using PeerAddress = ::chip::Transport::PeerAddress; + + CHIPCommand(const char * commandName) : Command(commandName) {} + + /////////// Command Interface ///////// + CHIP_ERROR Run() override; + + void SetCommandExitStatus(CHIP_ERROR status) + { + mCommandExitStatus = status; + StopWaiting(); + } + +protected: + // Will be called in a setting in which it's safe to touch the CHIP + // stack. The rules for Run() are as follows: + // + // 1) If error is returned, Run() must not call SetCommandExitStatus. + // 2) If success is returned Run() must either have called + // SetCommandExitStatus() or scheduled async work that will do that. + virtual CHIP_ERROR Run(NodeId remoteId) = 0; + + // Get the wait duration, in seconds, before the command times out. + virtual uint16_t GetWaitDurationInSeconds() const = 0; + + // Shut down the command, in case any work needs to be done after the event + // loop has been stopped. + virtual void Shutdown() {} + + ChipDeviceCommissioner mController; + PersistentStorage mStorage; + +private: + static void RunQueuedCommand(intptr_t commandArg); + + CHIP_ERROR mCommandExitStatus = CHIP_ERROR_INTERNAL; + chip::Controller::ExampleOperationalCredentialsIssuer mOpCredsIssuer; + + CHIP_ERROR StartWaiting(uint16_t seconds); + void StopWaiting(); + +#if CONFIG_USE_SEPARATE_EVENTLOOP + std::condition_variable cvWaitingForResponse; + std::mutex cvWaitingForResponseMutex; + bool mWaitingForResponse{ true }; +#endif // CONFIG_USE_SEPARATE_EVENTLOOP +}; diff --git a/examples/chip-tool/commands/common/Command.cpp b/examples/chip-tool/commands/common/Command.cpp index cfbc3c10e42ed1..9b0b1bdd190e18 100644 --- a/examples/chip-tool/commands/common/Command.cpp +++ b/examples/chip-tool/commands/common/Command.cpp @@ -98,13 +98,20 @@ bool Command::InitArgument(size_t argIndex, char * argValue) break; } - case ArgumentType::CharString: { + case ArgumentType::String: { const char ** value = reinterpret_cast(arg.value); *value = argValue; isValidArgument = true; break; } + case ArgumentType::CharString: { + auto * value = static_cast *>(arg.value); + *value = chip::Span(argValue, strlen(argValue)); + isValidArgument = true; + break; + } + case ArgumentType::OctetString: { auto * value = static_cast(arg.value); // We support two ways to pass an octet string argument. If it happens @@ -314,6 +321,17 @@ size_t Command::AddArgument(const char * name, char ** value) return mArgs.size(); } +size_t Command::AddArgument(const char * name, chip::CharSpan * value) +{ + Argument arg; + arg.type = ArgumentType::CharString; + arg.name = name; + arg.value = reinterpret_cast(value); + + mArgs.emplace_back(arg); + return mArgs.size(); +} + size_t Command::AddArgument(const char * name, chip::ByteSpan * value) { Argument arg; @@ -386,58 +404,3 @@ const char * Command::GetAttribute(void) const return nullptr; } - -void Command::UpdateWaitForResponse(bool value) -{ -#if CONFIG_USE_SEPARATE_EVENTLOOP - { - std::lock_guard lk(cvWaitingForResponseMutex); - mWaitingForResponse = value; - } - cvWaitingForResponse.notify_all(); -#else // CONFIG_USE_SEPARATE_EVENTLOOP - if (value == false) - { - if (mCommandExitStatus != CHIP_NO_ERROR) - { - ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(mCommandExitStatus)); - } - - chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); - } -#endif // CONFIG_USE_SEPARATE_EVENTLOOP -} - -#if CONFIG_USE_SEPARATE_EVENTLOOP - -void Command::WaitForResponse(uint16_t seconds) -{ - std::chrono::seconds waitingForResponseTimeout(seconds); - std::unique_lock lk(cvWaitingForResponseMutex); - auto waitingUntil = std::chrono::system_clock::now() + waitingForResponseTimeout; - if (!cvWaitingForResponse.wait_until(lk, waitingUntil, [this]() { return !this->mWaitingForResponse; })) - { - ChipLogError(chipTool, "No response from device"); - } -} - -#else // CONFIG_USE_SEPARATE_EVENTLOOP - -static void OnResponseTimeout(chip::System::Layer *, void *) -{ - ChipLogError(chipTool, "No response from device"); - - chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); -} - -CHIP_ERROR Command::ScheduleWaitForResponse(uint16_t seconds) -{ - CHIP_ERROR err = chip::DeviceLayer::SystemLayer().StartTimer(seconds * 1000, OnResponseTimeout, this); - if (err != CHIP_NO_ERROR) - { - ChipLogError(chipTool, "Failed to allocate timer %" CHIP_ERROR_FORMAT, err.Format()); - } - return err; -} - -#endif // CONFIG_USE_SEPARATE_EVENTLOOP diff --git a/examples/chip-tool/commands/common/Command.h b/examples/chip-tool/commands/common/Command.h index fa3a6076456b53..757c67a7818fdc 100644 --- a/examples/chip-tool/commands/common/Command.h +++ b/examples/chip-tool/commands/common/Command.h @@ -31,7 +31,6 @@ #include class Command; -class PersistentStorage; template std::unique_ptr make_unique(Args &&... args) @@ -58,8 +57,9 @@ enum ArgumentType Number_int16, Number_int32, Number_int64, - CharString, Boolean, + String, + CharString, OctetString, Attribute, Address @@ -77,15 +77,6 @@ struct Argument class Command { public: - using ChipDeviceCommissioner = ::chip::Controller::DeviceCommissioner; - using ChipDeviceController = ::chip::Controller::DeviceController; - using ChipSerializedDevice = ::chip::Controller::SerializedDevice; - using ChipDevice = ::chip::Controller::Device; - using PeerAddress = ::chip::Transport::PeerAddress; - using IPAddress = ::chip::Inet::IPAddress; - using PacketBufferHandle = ::chip::System::PacketBufferHandle; - using NodeId = ::chip::NodeId; - struct AddressWithInterface { ::chip::Inet::IPAddress address; @@ -109,8 +100,6 @@ class Command Command(const char * commandName) : mName(commandName) {} virtual ~Command() {} - void SetExecutionContext(ExecutionContext & execContext) { mExecContext = execContext; } - const char * GetName(void) const { return mName; } const char * GetAttribute(void) const; const char * GetArgumentName(size_t index) const; @@ -131,6 +120,7 @@ class Command * Add an octet string command argument */ size_t AddArgument(const char * name, chip::ByteSpan * value); + size_t AddArgument(const char * name, chip::Span * value); size_t AddArgument(const char * name, AddressWithInterface * out); size_t AddArgument(const char * name, int64_t min, uint64_t max, bool * out) { @@ -169,59 +159,13 @@ class Command return AddArgument(name, min, max, reinterpret_cast(out), Number_uint64); } - // Will be called in a setting in which it's safe to touch the CHIP - // stack. The rules for Run() are as follows: - // - // 1) If error is returned, Run() must not call SetCommandExitStatus. - // 2) If success is returned Run() must either have called - // SetCommandExitStatus() or scheduled async work that will do that. virtual CHIP_ERROR Run() = 0; - // Get the wait duration, in seconds, before the command times out. - virtual uint16_t GetWaitDurationInSeconds() const = 0; - - // Shut down the command, in case any work needs to be done after the event - // loop has been stopped. - virtual void Shutdown() {} - - CHIP_ERROR GetCommandExitStatus() const { return mCommandExitStatus; } - void SetCommandExitStatus(CHIP_ERROR status) - { - mCommandExitStatus = status; - UpdateWaitForResponse(false); - } - - void UpdateWaitForResponse(bool value); - - // There is a certain symmetry between the single-event-loop and - // separate-event-loop approaches. With a separate event loop, we schedule - // our work on that event loop and synchronously wait (block) waiting for a - // response. When using a single event loop, we ask for an async response - // notification and then block processing work on the event loop - // synchronously until that notification happens. -#if CONFIG_USE_SEPARATE_EVENTLOOP - void WaitForResponse(uint16_t seconds); -#else // CONFIG_USE_SEPARATE_EVENTLOOP - CHIP_ERROR ScheduleWaitForResponse(uint16_t seconds); -#endif // CONFIG_USE_SEPARATE_EVENTLOOP - -protected: - ExecutionContext * GetExecContext() { return &mExecContext; } - private: bool InitArgument(size_t argIndex, char * argValue); size_t AddArgument(const char * name, int64_t min, uint64_t max, void * out, ArgumentType type); size_t AddArgument(const char * name, int64_t min, uint64_t max, void * out); - CHIP_ERROR mCommandExitStatus = CHIP_ERROR_INTERNAL; - const char * mName = nullptr; + const char * mName = nullptr; std::vector mArgs; - - ExecutionContext mExecContext; - -#if CONFIG_USE_SEPARATE_EVENTLOOP - std::condition_variable cvWaitingForResponse; - std::mutex cvWaitingForResponseMutex; - bool mWaitingForResponse{ false }; -#endif // CONFIG_USE_SEPARATE_EVENTLOOP }; diff --git a/examples/chip-tool/commands/common/Commands.cpp b/examples/chip-tool/commands/common/Commands.cpp index 108ab78b9f8043..6d0ba7e265c1dc 100644 --- a/examples/chip-tool/commands/common/Commands.cpp +++ b/examples/chip-tool/commands/common/Commands.cpp @@ -30,9 +30,6 @@ #include #include #include -#include - -using DeviceControllerFactory = chip::Controller::DeviceControllerFactory; void Commands::Register(const char * clusterName, commands_list commandsList) { @@ -45,24 +42,10 @@ void Commands::Register(const char * clusterName, commands_list commandsList) int Commands::Run(int argc, char ** argv) { CHIP_ERROR err = CHIP_NO_ERROR; - chip::Controller::FactoryInitParams factoryInitParams; - chip::Controller::SetupParams commissionerParams; - Command * command = nullptr; - NodeId localId; - NodeId remoteId; - - chip::Platform::ScopedMemoryBuffer noc; - chip::Platform::ScopedMemoryBuffer icac; - chip::Platform::ScopedMemoryBuffer rcac; err = chip::Platform::MemoryInit(); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Memory failure: %s", chip::ErrorStr(err))); -#if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE - // By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central. - SuccessOrExit(err = chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(/* BLE adapter ID */ 0, /* BLE central */ true)); -#endif - err = mStorage.Init(); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Storage failure: %s", chip::ErrorStr(err))); @@ -131,37 +114,11 @@ int Commands::Run(int argc, char ** argv) #endif // !CONFIG_USE_SEPARATE_EVENTLOOP exit: -#if CONFIG_USE_SEPARATE_EVENTLOOP - chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); -#endif // CONFIG_USE_SEPARATE_EVENTLOOP - - if ((err == CHIP_NO_ERROR) && (command != nullptr)) - { - err = command->GetCommandExitStatus(); - } - if (err != CHIP_NO_ERROR) - { - ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err)); - } - - if (command) - { - command->Shutdown(); - } - - // - // We can call DeviceController::Shutdown() safely without grabbing the stack lock - // since the CHIP thread and event queue have been stopped, preventing any thread - // races. - // - mController.Shutdown(); - return (err == CHIP_NO_ERROR) ? EXIT_SUCCESS : EXIT_FAILURE; } -CHIP_ERROR Commands::RunCommand(NodeId localId, NodeId remoteId, int argc, char ** argv, Command ** ranCommand) +CHIP_ERROR Commands::RunCommand(int argc, char ** argv) { - CHIP_ERROR err = CHIP_NO_ERROR; std::map::iterator cluster; Command * command = nullptr; @@ -169,7 +126,7 @@ CHIP_ERROR Commands::RunCommand(NodeId localId, NodeId remoteId, int argc, char { ChipLogError(chipTool, "Missing cluster name"); ShowClusters(argv[0]); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } cluster = GetCluster(argv[1]); @@ -177,14 +134,14 @@ CHIP_ERROR Commands::RunCommand(NodeId localId, NodeId remoteId, int argc, char { ChipLogError(chipTool, "Unknown cluster: %s", argv[1]); ShowClusters(argv[0]); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } if (argc <= 2) { ChipLogError(chipTool, "Missing command name"); ShowCluster(argv[0], argv[1], cluster->second); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } if (!IsGlobalCommand(argv[2])) @@ -194,7 +151,7 @@ CHIP_ERROR Commands::RunCommand(NodeId localId, NodeId remoteId, int argc, char { ChipLogError(chipTool, "Unknown command: %s", argv[2]); ShowCluster(argv[0], argv[1], cluster->second); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } } else @@ -203,7 +160,7 @@ CHIP_ERROR Commands::RunCommand(NodeId localId, NodeId remoteId, int argc, char { ChipLogError(chipTool, "Missing attribute name"); ShowClusterAttributes(argv[0], argv[1], argv[2], cluster->second); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } command = GetGlobalCommand(cluster->second, argv[2], argv[3]); @@ -211,14 +168,14 @@ CHIP_ERROR Commands::RunCommand(NodeId localId, NodeId remoteId, int argc, char { ChipLogError(chipTool, "Unknown attribute: %s", argv[3]); ShowClusterAttributes(argv[0], argv[1], argv[2], cluster->second); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } } if (!command->InitArguments(argc - 3, &argv[3])) { ShowCommand(argv[0], argv[1], command); - ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT); + return CHIP_ERROR_INVALID_ARGUMENT; } { diff --git a/examples/chip-tool/commands/common/Commands.h b/examples/chip-tool/commands/common/Commands.h index e0ba57511fc7d3..d7068690a5f4d0 100644 --- a/examples/chip-tool/commands/common/Commands.h +++ b/examples/chip-tool/commands/common/Commands.h @@ -37,11 +37,7 @@ class Commands virtual ~Commands() {} private: - // *ranCommand will be set to the command we ran if we get as far as running - // it. If it's not null, we need to call Shutdown() on the command after we - // shut down the event loop. - CHIP_ERROR RunCommand(NodeId localId, NodeId remoteId, int argc, char ** argv, Command ** ranCommand); - static void RunQueuedCommand(intptr_t commandArg); + CHIP_ERROR RunCommand(int argc, char ** argv); std::map::iterator GetCluster(std::string clusterName); Command * GetCommand(CommandsVector & commands, std::string commandName); Command * GetGlobalCommand(CommandsVector & commands, std::string commandName, std::string attributeName); diff --git a/examples/chip-tool/commands/discover/Commands.h b/examples/chip-tool/commands/discover/Commands.h index 424b331dc97404..8de5d290e644eb 100644 --- a/examples/chip-tool/commands/discover/Commands.h +++ b/examples/chip-tool/commands/discover/Commands.h @@ -78,8 +78,8 @@ class Update : public DiscoverCommand CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override { ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " Compressed FabricId: %" PRIx64 " ...", remoteId, - GetExecContext()->commissioner->GetCompressedFabricId()); - return GetExecContext()->commissioner->UpdateDevice(remoteId); + mController.GetCompressedFabricId()); + return mController.UpdateDevice(remoteId); } /////////// DeviceAddressUpdateDelegate Interface ///////// diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommand.cpp index fc3f6449a14a7c..80c93942cc504c 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommand.cpp +++ b/examples/chip-tool/commands/discover/DiscoverCommand.cpp @@ -18,8 +18,8 @@ #include "DiscoverCommand.h" -CHIP_ERROR DiscoverCommand::Run() +CHIP_ERROR DiscoverCommand::Run(NodeId remoteId) { - GetExecContext()->commissioner->RegisterDeviceAddressUpdateDelegate(this); + mController.RegisterDeviceAddressUpdateDelegate(this); return RunCommand(mNodeId, mFabricId); } diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.h b/examples/chip-tool/commands/discover/DiscoverCommand.h index f580fb2fb4cf84..1a35deb2d9e398 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommand.h +++ b/examples/chip-tool/commands/discover/DiscoverCommand.h @@ -19,13 +19,13 @@ #pragma once #include "../../config/PersistentStorage.h" -#include "../common/Command.h" +#include "../common/CHIPCommand.h" #include -class DiscoverCommand : public Command, public chip::Controller::DeviceAddressUpdateDelegate +class DiscoverCommand : public CHIPCommand, public chip::Controller::DeviceAddressUpdateDelegate { public: - DiscoverCommand(const char * commandName) : Command(commandName) + DiscoverCommand(const char * commandName) : CHIPCommand(commandName) { AddArgument("nodeid", 0, UINT64_MAX, &mNodeId); AddArgument("fabricid", 0, UINT64_MAX, &mFabricId); @@ -34,8 +34,8 @@ class DiscoverCommand : public Command, public chip::Controller::DeviceAddressUp /////////// DeviceAddressUpdateDelegate Interface ///////// void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override{}; - /////////// Command Interface ///////// - CHIP_ERROR Run() override; + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return 30; } virtual CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) = 0; diff --git a/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp index 356e7ea65c512e..c9841736fe13f3 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp +++ b/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp @@ -21,12 +21,11 @@ using namespace ::chip; -CHIP_ERROR DiscoverCommissionablesCommand::Run() +CHIP_ERROR DiscoverCommissionablesCommand::Run(NodeId remoteId) { - GetExecContext()->commissioner->RegisterDeviceDiscoveryDelegate(this); - + mController.RegisterDeviceDiscoveryDelegate(this); Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, (uint64_t) 0); - return GetExecContext()->commissioner->DiscoverCommissionableNodes(filter); + return mController.DiscoverCommissionableNodes(filter); } void DiscoverCommissionablesCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) diff --git a/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.h b/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.h index d2a84c2c907eef..6e7a25a2bf4eb9 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.h +++ b/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.h @@ -18,17 +18,17 @@ #pragma once -#include "../common/Command.h" +#include "../common/CHIPCommand.h" -class DiscoverCommissionablesCommand : public Command, public chip::Controller::DeviceDiscoveryDelegate +class DiscoverCommissionablesCommand : public CHIPCommand, public chip::Controller::DeviceDiscoveryDelegate { public: - DiscoverCommissionablesCommand() : Command("commissionables") {} + DiscoverCommissionablesCommand() : CHIPCommand("commissionables") {} /////////// DeviceDiscoveryDelegate Interface ///////// void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override; - /////////// Command Interface ///////// - CHIP_ERROR Run() override; + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return 30; } }; diff --git a/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.cpp index 4412b7145fbb1b..c65e61677b617b 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.cpp +++ b/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.cpp @@ -21,7 +21,7 @@ using namespace ::chip; -CHIP_ERROR DiscoverCommissionersCommand::Run() +CHIP_ERROR DiscoverCommissionersCommand::Run(NodeId remoteId) { return mCommissionableNodeController.DiscoverCommissioners(); } diff --git a/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.h b/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.h index 9fab66db6c39ed..a9b454c60af3e9 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.h +++ b/examples/chip-tool/commands/discover/DiscoverCommissionersCommand.h @@ -18,14 +18,16 @@ #pragma once -#include "../common/Command.h" +#include "../common/CHIPCommand.h" #include -class DiscoverCommissionersCommand : public Command +class DiscoverCommissionersCommand : public CHIPCommand { public: - DiscoverCommissionersCommand() : Command("commissioners") {} - CHIP_ERROR Run() override; + DiscoverCommissionersCommand() : CHIPCommand("commissioners") {} + + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return 3; } void Shutdown() override; diff --git a/examples/chip-tool/commands/pairing/PairingCommand.cpp b/examples/chip-tool/commands/pairing/PairingCommand.cpp index 21e81978086c2a..cb2126bc9ea094 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.cpp +++ b/examples/chip-tool/commands/pairing/PairingCommand.cpp @@ -32,12 +32,12 @@ using namespace ::chip; constexpr uint64_t kBreadcrumb = 0; constexpr uint32_t kTimeoutMs = 6000; -CHIP_ERROR PairingCommand::Run() +CHIP_ERROR PairingCommand::Run(NodeId remoteId) { CHIP_ERROR err = CHIP_NO_ERROR; - GetExecContext()->commissioner->RegisterDeviceAddressUpdateDelegate(this); - GetExecContext()->commissioner->RegisterPairingDelegate(this); + mController.RegisterDeviceAddressUpdateDelegate(this); + mController.RegisterPairingDelegate(this); if (mPairingMode != PairingMode::OpenCommissioningWindow) { @@ -52,16 +52,16 @@ CHIP_ERROR PairingCommand::Run() ChipLogProgress(Controller, "Generated random node id: 0x" ChipLogFormatX64, ChipLogValueX64(randomId)); - ReturnErrorOnFailure(GetExecContext()->storage->SetRemoteNodeId(randomId)); - GetExecContext()->remoteId = randomId; + ReturnErrorOnFailure(mStorage.SetRemoteNodeId(randomId)); + remoteId = randomId; #else // CONFIG_PAIR_WITH_RANDOM_ID // Use the default id, not whatever happens to be in our storage, since this // is a new pairing. - GetExecContext()->remoteId = kTestDeviceNodeId; + remoteId = kTestDeviceNodeId; #endif // CONFIG_PAIR_WITH_RANDOM_ID } - err = RunInternal(GetExecContext()->remoteId); + err = RunInternal(remoteId); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Init Failure! PairDevice: %s", ErrorStr(err))); exit: @@ -103,12 +103,11 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId) err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort)); break; case PairingMode::OpenCommissioningWindow: - err = GetExecContext()->commissioner->GetConnectedDevice(GetExecContext()->remoteId, &mOnDeviceConnectedCallback, - &mOnDeviceConnectionFailureCallback); + err = mController.GetConnectedDevice(remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); if (err != CHIP_NO_ERROR) { ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %" CHIP_ERROR_FORMAT, - GetExecContext()->remoteId, err.Format()); + remoteId, err.Format()); } break; @@ -131,12 +130,12 @@ void PairingCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId) { - return GetExecContext()->commissioner->PairDevice(remoteId, mOnboardingPayload); + return mController.PairDevice(remoteId, mOnboardingPayload); } CHIP_ERROR PairingCommand::PairWithManualCode(NodeId remoteId) { - return GetExecContext()->commissioner->PairDevice(remoteId, mOnboardingPayload); + return mController.PairDevice(remoteId, mOnboardingPayload); } CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address) @@ -144,7 +143,7 @@ CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address) RendezvousParameters params = RendezvousParameters().SetSetupPINCode(mSetupPINCode).SetDiscriminator(mDiscriminator).SetPeerAddress(address); - return GetExecContext()->commissioner->PairDevice(remoteId, params); + return mController.PairDevice(remoteId, params); } CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId) @@ -172,27 +171,27 @@ CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId) break; } - GetExecContext()->commissioner->RegisterDeviceDiscoveryDelegate(this); - return GetExecContext()->commissioner->DiscoverCommissionableNodes(filter); + mController.RegisterDeviceDiscoveryDelegate(this); + return mController.DiscoverCommissionableNodes(filter); } CHIP_ERROR PairingCommand::PairWithoutSecurity(NodeId remoteId, PeerAddress address) { ChipSerializedDevice serializedTestDevice; - return GetExecContext()->commissioner->PairTestDeviceWithoutSecurity(remoteId, address, serializedTestDevice); + return mController.PairTestDeviceWithoutSecurity(remoteId, address, serializedTestDevice); } CHIP_ERROR PairingCommand::Unpair(NodeId remoteId) { - CHIP_ERROR err = GetExecContext()->commissioner->UnpairDevice(remoteId); + CHIP_ERROR err = mController.UnpairDevice(remoteId); SetCommandExitStatus(err); return err; } CHIP_ERROR PairingCommand::OpenCommissioningWindow() { - CHIP_ERROR err = GetExecContext()->commissioner->OpenCommissioningWindow(GetExecContext()->remoteId, mTimeout, mIteration, - mDiscriminator, mCommissioningWindowOption); + CHIP_ERROR err = + mController.OpenCommissioningWindow(mRemoteId, mTimeout, mIteration, mDiscriminator, mCommissioningWindowOption); SetCommandExitStatus(err); return err; } @@ -272,7 +271,7 @@ CHIP_ERROR PairingCommand::SetupNetwork() break; case PairingNetworkType::WiFi: case PairingNetworkType::Thread: - err = GetExecContext()->commissioner->GetDevice(mRemoteId, &mDevice); + err = mController.GetDevice(mRemoteId, &mDevice); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Setup failure! No pairing for device: %" PRIu64, mRemoteId)); mCluster.Associate(mDevice, mEndpointId); @@ -438,8 +437,8 @@ void PairingCommand::OnEnableNetworkResponse(void * context, uint8_t errorCode, CHIP_ERROR PairingCommand::UpdateNetworkAddress() { ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " Compressed FabricId: %" PRIx64 " ...", mRemoteId, - GetExecContext()->commissioner->GetCompressedFabricId()); - return GetExecContext()->commissioner->UpdateDevice(mRemoteId); + mController.GetCompressedFabricId()); + return mController.UpdateDevice(mRemoteId); } void PairingCommand::OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR err) @@ -461,7 +460,7 @@ void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & ChipLogProgress(chipTool, "Discovered Device: %s:%u", buf, port); // Stop Mdns discovery. Is it the right method ? - GetExecContext()->commissioner->RegisterDeviceDiscoveryDelegate(nullptr); + mController.RegisterDeviceDiscoveryDelegate(nullptr); Inet::InterfaceId interfaceId = nodeData.ipAddress[0].IsIPv6LinkLocal() ? nodeData.interfaceId[0] : INET_NULL_INTERFACEID; PeerAddress peerAddress = PeerAddress::UDP(nodeData.ipAddress[0], port, interfaceId); diff --git a/examples/chip-tool/commands/pairing/PairingCommand.h b/examples/chip-tool/commands/pairing/PairingCommand.h index 61525da3f6263d..d577bd98ee98b1 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.h +++ b/examples/chip-tool/commands/pairing/PairingCommand.h @@ -19,7 +19,7 @@ #pragma once #include "../../config/PersistentStorage.h" -#include "../common/Command.h" +#include "../common/CHIPCommand.h" #include #include @@ -49,7 +49,7 @@ enum class PairingNetworkType Ethernet, }; -class PairingCommand : public Command, +class PairingCommand : public CHIPCommand, public chip::Controller::DevicePairingDelegate, public chip::Controller::DeviceAddressUpdateDelegate, public chip::Controller::DeviceDiscoveryDelegate @@ -57,7 +57,7 @@ class PairingCommand : public Command, public: PairingCommand(const char * commandName, PairingMode mode, PairingNetworkType networkType, chip::Dnssd::DiscoveryFilterType filterType = chip::Dnssd::DiscoveryFilterType::kNone) : - Command(commandName), + CHIPCommand(commandName), mPairingMode(mode), mNetworkType(networkType), mFilterType(filterType), mRemoteAddr{ IPAddress::Any, INET_NULL_INTERFACEID }, mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this) @@ -145,8 +145,8 @@ class PairingCommand : public Command, } } - /////////// Command Interface ///////// - CHIP_ERROR Run() override; + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return 120; } void Shutdown() override; @@ -215,7 +215,6 @@ class PairingCommand : public Command, ChipDevice * mDevice; chip::Controller::NetworkCommissioningCluster mCluster; chip::EndpointId mEndpointId = 0; - chip::Controller::ExampleOperationalCredentialsIssuer mOpCredsIssuer; static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device); static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error); diff --git a/examples/chip-tool/commands/payload/AdditionalDataParseCommand.cpp b/examples/chip-tool/commands/payload/AdditionalDataParseCommand.cpp index 7b6e61c607a55a..0a5f6c68145ee3 100644 --- a/examples/chip-tool/commands/payload/AdditionalDataParseCommand.cpp +++ b/examples/chip-tool/commands/payload/AdditionalDataParseCommand.cpp @@ -23,32 +23,21 @@ #include using namespace ::chip; +using namespace ::chip::Encoding; using namespace ::chip::SetupPayloadData; CHIP_ERROR AdditionalDataParseCommand::Run() { - AdditionalDataPayload resultPayload; - CHIP_ERROR err = CHIP_NO_ERROR; + VerifyOrReturnError(strlen(mPayload) % 2 == 0, CHIP_ERROR_INVALID_STRING_LENGTH); - if (strlen(mPayload) % 2 != 0) - { - return CHIP_ERROR_INVALID_STRING_LENGTH; - } size_t additionalDataPayloadBytesLength = strlen(mPayload) / 2; std::unique_ptr additionalDataPayloadBytes(new uint8_t[additionalDataPayloadBytesLength]); - size_t bufferSize = - chip::Encoding::HexToBytes(mPayload, strlen(mPayload), additionalDataPayloadBytes.get(), additionalDataPayloadBytesLength); - - err = AdditionalDataPayloadParser(additionalDataPayloadBytes.get(), bufferSize).populatePayload(resultPayload); - SuccessOrExit(err); + AdditionalDataPayload resultPayload; + size_t bufferSize = HexToBytes(mPayload, strlen(mPayload), additionalDataPayloadBytes.get(), additionalDataPayloadBytesLength); + ReturnErrorOnFailure(AdditionalDataPayloadParser(additionalDataPayloadBytes.get(), bufferSize).populatePayload(resultPayload)); ChipLogProgress(chipTool, "AdditionalDataParseCommand, RotatingDeviceId=%s", resultPayload.rotatingDeviceId.c_str()); -exit: - if (err == CHIP_NO_ERROR) - { - SetCommandExitStatus(CHIP_NO_ERROR); - } - return err; + return CHIP_NO_ERROR; } diff --git a/examples/chip-tool/commands/payload/AdditionalDataParseCommand.h b/examples/chip-tool/commands/payload/AdditionalDataParseCommand.h index aeaa9b2b39e06a..0f1fb3ab448e7b 100644 --- a/examples/chip-tool/commands/payload/AdditionalDataParseCommand.h +++ b/examples/chip-tool/commands/payload/AdditionalDataParseCommand.h @@ -25,7 +25,6 @@ class AdditionalDataParseCommand : public Command public: AdditionalDataParseCommand() : Command("parse-additional-data-payload") { AddArgument("payload", &mPayload); } CHIP_ERROR Run() override; - uint16_t GetWaitDurationInSeconds() const override { return 5; } private: char * mPayload; diff --git a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp index 88c118e70e2949..bedfab03136530 100644 --- a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp +++ b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp @@ -17,7 +17,6 @@ */ #include "SetupPayloadParseCommand.h" -#include #include #include #include @@ -29,26 +28,24 @@ CHIP_ERROR SetupPayloadParseCommand::Run() std::string codeString(mCode); SetupPayload payload; - CHIP_ERROR err = CHIP_NO_ERROR; - err = Parse(codeString, payload); - SuccessOrExit(err); + ReturnErrorOnFailure(Parse(codeString, payload)); + ReturnErrorOnFailure(Print(payload)); - err = Print(payload); - SuccessOrExit(err); -exit: - if (err == CHIP_NO_ERROR) - { - SetCommandExitStatus(CHIP_NO_ERROR); - } - return err; + return CHIP_NO_ERROR; } -CHIP_ERROR SetupPayloadParseCommand::Print(chip::SetupPayload payload) +CHIP_ERROR SetupPayloadParseCommand::Parse(std::string codeString, chip::SetupPayload & payload) { - std::string serialNumber; - std::vector optionalVendorData; - CHIP_ERROR err = CHIP_NO_ERROR; + bool isQRCode = IsQRCode(codeString); + + ChipLogDetail(SetupPayload, "Parsing %sRepresentation: %s", isQRCode ? "base38" : "decimal", codeString.c_str()); + + return isQRCode ? QRCodeSetupPayloadParser(codeString).populatePayload(payload) + : ManualSetupPayloadParser(codeString).populatePayload(payload); +} +CHIP_ERROR SetupPayloadParseCommand::Print(chip::SetupPayload payload) +{ ChipLogProgress(SetupPayload, "CommissioningFlow: %" PRIu8, to_underlying(payload.commissioningFlow)); ChipLogProgress(SetupPayload, "VendorID: %u", payload.vendorID); ChipLogProgress(SetupPayload, "Version: %u", payload.version); @@ -57,50 +54,30 @@ CHIP_ERROR SetupPayloadParseCommand::Print(chip::SetupPayload payload) ChipLogProgress(SetupPayload, "SetUpPINCode: %u", payload.setUpPINCode); ChipLogProgress(SetupPayload, "RendezvousInformation: %u", payload.rendezvousInformation.Raw()); + std::string serialNumber; if (payload.getSerialNumber(serialNumber) == CHIP_NO_ERROR) { ChipLogProgress(SetupPayload, "SerialNumber: %s", serialNumber.c_str()); } - optionalVendorData = payload.getAllOptionalVendorData(); + std::vector optionalVendorData = payload.getAllOptionalVendorData(); for (const OptionalQRCodeInfo & info : optionalVendorData) { - if (info.type == optionalQRCodeInfoTypeString) + bool isTypeString = info.type == optionalQRCodeInfoTypeString; + bool isTypeInt32 = info.type == optionalQRCodeInfoTypeInt32; + VerifyOrReturnError(isTypeString || isTypeInt32, CHIP_ERROR_INVALID_ARGUMENT); + + if (isTypeString) { ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,string value=%s", info.tag, info.data.c_str()); } - else if (info.type == optionalQRCodeInfoTypeInt32) - { - ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,int value=%u", info.tag, info.int32); - } else { - err = CHIP_ERROR_INVALID_ARGUMENT; + ChipLogProgress(SetupPayload, "OptionalQRCodeInfo: tag=%u,int value=%u", info.tag, info.int32); } } - SuccessOrExit(err); - -exit: - return err; -} - -CHIP_ERROR SetupPayloadParseCommand::Parse(std::string codeString, chip::SetupPayload & payload) -{ - - CHIP_ERROR err = CHIP_NO_ERROR; - if (IsQRCode(codeString)) - { - ChipLogDetail(SetupPayload, "Parsing base38Representation: %s", codeString.c_str()); - err = QRCodeSetupPayloadParser(codeString).populatePayload(payload); - } - else - { - ChipLogDetail(SetupPayload, "Parsing decimalRepresentation: %s", codeString.c_str()); - err = ManualSetupPayloadParser(codeString).populatePayload(payload); - } - - return err; + return CHIP_NO_ERROR; } bool SetupPayloadParseCommand::IsQRCode(std::string codeString) diff --git a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h index 620151f6d06409..83193f909e35b8 100644 --- a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h +++ b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.h @@ -26,7 +26,6 @@ class SetupPayloadParseCommand : public Command public: SetupPayloadParseCommand() : Command("parse-setup-payload") { AddArgument("payload", &mCode); } CHIP_ERROR Run() override; - uint16_t GetWaitDurationInSeconds() const override { return 5; } private: char * mCode; diff --git a/examples/chip-tool/commands/reporting/ReportingCommand.cpp b/examples/chip-tool/commands/reporting/ReportingCommand.cpp index 440335d044162a..3f8a4a65c4febf 100644 --- a/examples/chip-tool/commands/reporting/ReportingCommand.cpp +++ b/examples/chip-tool/commands/reporting/ReportingCommand.cpp @@ -24,15 +24,14 @@ using namespace ::chip; -CHIP_ERROR ReportingCommand::Run() +CHIP_ERROR ReportingCommand::Run(NodeId remoteId) { - auto * ctx = GetExecContext(); + CHIP_ERROR err = mController.GetConnectedDevice(remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); + VerifyOrExit( + err == CHIP_NO_ERROR, + ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %s", remoteId, ErrorStr(err))); - CHIP_ERROR err = - ctx->commissioner->GetConnectedDevice(ctx->remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); - VerifyOrExit(err == CHIP_NO_ERROR, - ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %s", ctx->remoteId, - ErrorStr(err))); + mRemoteId = remoteId; exit: return err; @@ -47,7 +46,7 @@ void ReportingCommand::OnDeviceConnectedFn(void * context, chip::Controller::Dev chip::Controller::BasicCluster cluster; cluster.Associate(device, command->mEndPointId); - command->AddReportCallbacks(command->mEndPointId); + command->AddReportCallbacks(command->mRemoteId, command->mEndPointId); CHIP_ERROR err = cluster.MfgSpecificPing(nullptr, nullptr); if (err != CHIP_NO_ERROR) diff --git a/examples/chip-tool/commands/reporting/ReportingCommand.h b/examples/chip-tool/commands/reporting/ReportingCommand.h index d868e5390001cf..b4633ddaf955bc 100644 --- a/examples/chip-tool/commands/reporting/ReportingCommand.h +++ b/examples/chip-tool/commands/reporting/ReportingCommand.h @@ -19,7 +19,7 @@ #pragma once #include "../../config/PersistentStorage.h" -#include "../common/Command.h" +#include "../common/CHIPCommand.h" #include @@ -27,23 +27,24 @@ #define CHIP_ZCL_ENDPOINT_MIN 0x00 #define CHIP_ZCL_ENDPOINT_MAX 0xF0 -class ReportingCommand : public Command +class ReportingCommand : public CHIPCommand { public: ReportingCommand(const char * commandName) : - Command(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), + CHIPCommand(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this) { AddArgument("endpoint-id", CHIP_ZCL_ENDPOINT_MIN, CHIP_ZCL_ENDPOINT_MAX, &mEndPointId); } - /////////// Command Interface ///////// - CHIP_ERROR Run() override; + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return UINT16_MAX; } - virtual void AddReportCallbacks(uint8_t endPointId) = 0; + virtual void AddReportCallbacks(NodeId remoteId, uint8_t endPointId) = 0; private: + NodeId mRemoteId; uint8_t mEndPointId; static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device); diff --git a/examples/chip-tool/commands/tests/TestCommand.cpp b/examples/chip-tool/commands/tests/TestCommand.cpp index 3078290f5c6493..81921248c0c62a 100644 --- a/examples/chip-tool/commands/tests/TestCommand.cpp +++ b/examples/chip-tool/commands/tests/TestCommand.cpp @@ -18,16 +18,9 @@ #include "TestCommand.h" -CHIP_ERROR TestCommand::Run() +CHIP_ERROR TestCommand::Run(NodeId remoteId) { - CHIP_ERROR err = CHIP_NO_ERROR; - - auto * ctx = GetExecContext(); - - err = ctx->commissioner->GetConnectedDevice(ctx->remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); - ReturnErrorOnFailure(err); - - return CHIP_NO_ERROR; + return mController.GetConnectedDevice(remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); } void TestCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device * device) diff --git a/examples/chip-tool/commands/tests/TestCommand.h b/examples/chip-tool/commands/tests/TestCommand.h index 5946fc0b9b6e3d..5670c464b4efb2 100644 --- a/examples/chip-tool/commands/tests/TestCommand.h +++ b/examples/chip-tool/commands/tests/TestCommand.h @@ -18,20 +18,20 @@ #pragma once -#include "../common/Command.h" +#include "../common/CHIPCommand.h" #include #include -class TestCommand : public Command +class TestCommand : public CHIPCommand { public: TestCommand(const char * commandName) : - Command(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), + CHIPCommand(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this) {} - /////////// Command Interface ///////// - CHIP_ERROR Run() override; + /////////// CHIPCommand Interface ///////// + CHIP_ERROR Run(NodeId remoteId) override; uint16_t GetWaitDurationInSeconds() const override { return 30; } virtual void NextTest() = 0; diff --git a/examples/chip-tool/templates/commands.zapt b/examples/chip-tool/templates/commands.zapt index 1dfd0625ba0539..9958ac44a9b48b 100644 --- a/examples/chip-tool/templates/commands.zapt +++ b/examples/chip-tool/templates/commands.zapt @@ -207,7 +207,7 @@ public: chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster; cluster.Associate(device, endpointId); - return cluster.{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(){{#chip_cluster_command_arguments}}, {{#if (isCharString type)}} chip::ByteSpan(chip::Uint8::from_char(m{{asUpperCamelCase label}}), strlen(m{{asUpperCamelCase label}})){{else}}m{{asUpperCamelCase label}}{{/if}}{{/chip_cluster_command_arguments}}); + return cluster.{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(){{#chip_cluster_command_arguments}}, m{{asUpperCamelCase label}}{{/chip_cluster_command_arguments}}); } private: @@ -218,11 +218,7 @@ private: {{/if}} chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); {{#chip_cluster_command_arguments}} - {{#if (isCharString type)}} - char * m{{asUpperCamelCase label}}; - {{else}} - {{chipType}} m{{asUpperCamelCase label}}; - {{/if}} + {{#if (isCharString type)}}chip::ByteSpan{{else}}{{chipType}}{{/if}} m{{asUpperCamelCase label}}; {{/chip_cluster_command_arguments}} }; @@ -292,17 +288,13 @@ public: chip::Controller::{{asUpperCamelCase parent.name}}Cluster cluster; cluster.Associate(device, endpointId); - return cluster.WriteAttribute{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), {{#if (isCharString type)}} chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue)){{else}}mValue{{/if}}); + return cluster.WriteAttribute{{asUpperCamelCase name}}(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue); } private: chip::Callback::Callback * onSuccessCallback = new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - {{#if (isCharString type)}} - char * mValue; - {{else}} - {{chipType}} mValue; - {{/if}} + {{#if (isCharString type)}}chip::ByteSpan{{else}}{{chipType}}{{/if}} mValue; }; {{/if}} diff --git a/examples/chip-tool/templates/reporting-commands.zapt b/examples/chip-tool/templates/reporting-commands.zapt index c9c05e691fb22f..25d0808300a888 100644 --- a/examples/chip-tool/templates/reporting-commands.zapt +++ b/examples/chip-tool/templates/reporting-commands.zapt @@ -25,13 +25,13 @@ public: {{/chip_client_clusters}} } - void AddReportCallbacks(uint8_t endpointId) override + void AddReportCallbacks(NodeId remoteId, uint8_t endpointId) override { chip::app::CHIPDeviceCallbacksMgr & callbacksMgr = chip::app::CHIPDeviceCallbacksMgr::GetInstance(); {{#chip_client_clusters}} {{#chip_server_cluster_attributes}} {{#if isReportableAttribute}} - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, {{asHex parent.code 4}}, {{asHex code 4}}, onReport{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}Callback->Cancel(), BasicAttributeFilter<{{chipCallback.name}}AttributeCallback>); + callbacksMgr.AddReportCallback(remoteId, endpointId, {{asHex parent.code 4}}, {{asHex code 4}}, onReport{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}Callback->Cancel(), BasicAttributeFilter<{{chipCallback.name}}AttributeCallback>); {{/if}} {{/chip_server_cluster_attributes}} {{/chip_client_clusters}} diff --git a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp index c3d61bcf4842a2..583af995e0f664 100644 --- a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp +++ b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include #include #include @@ -50,6 +52,7 @@ using namespace chip; using namespace ::chip::DeviceLayer; using namespace ::chip::Transport; +using namespace chip::app; using namespace chip::app::Clusters::OperationalCredentials; namespace { @@ -57,7 +60,65 @@ namespace { constexpr uint8_t kDACCertificate = 1; constexpr uint8_t kPAICertificate = 2; -} // namespace +class OperationalCredentialsAttrAccess : public AttributeAccessInterface +{ +public: + // Register for the OperationalCredentials cluster on all endpoints. + OperationalCredentialsAttrAccess() : + AttributeAccessInterface(Optional::Missing(), Clusters::OperationalCredentials::Id) + {} + + CHIP_ERROR Read(ClusterInfo & aClusterInfo, AttributeValueEncoder & aEncoder) override; + +private: + CHIP_ERROR ReadFabricsList(EndpointId endpoint, AttributeValueEncoder & aEncoder); +}; + +CHIP_ERROR OperationalCredentialsAttrAccess::ReadFabricsList(EndpointId endpoint, AttributeValueEncoder & aEncoder) +{ + return aEncoder.EncodeList([](const TagBoundEncoder & encoder) -> CHIP_ERROR { + for (auto & fabricInfo : Server::GetInstance().GetFabricTable()) + { + if (!fabricInfo.IsInitialized()) + continue; + + Clusters::OperationalCredentials::Structs::FabricDescriptor::Type fabricDescriptor; + + fabricDescriptor.fabricIndex = fabricInfo.GetFabricIndex(); + fabricDescriptor.nodeId = fabricInfo.GetPeerId().GetNodeId(); + fabricDescriptor.vendorId = fabricInfo.GetVendorId(); + fabricDescriptor.fabricId = fabricInfo.GetFabricId(); + + // TODO: The type of 'label' should be 'CharSpan', need to fix the XML definition for broken member type. + fabricDescriptor.label = + ByteSpan(Uint8::from_const_char(fabricInfo.GetFabricLabel().data()), fabricInfo.GetFabricLabel().size()); + fabricDescriptor.rootPublicKey = fabricInfo.GetRootPubkey(); + + ReturnErrorOnFailure(encoder.Encode(fabricDescriptor)); + } + + return CHIP_NO_ERROR; + }); +} + +OperationalCredentialsAttrAccess gAttrAccess; + +CHIP_ERROR OperationalCredentialsAttrAccess::Read(ClusterInfo & aClusterInfo, AttributeValueEncoder & aEncoder) +{ + VerifyOrDie(aClusterInfo.mClusterId == Clusters::OperationalCredentials::Id); + + switch (aClusterInfo.mFieldId) + { + case Attributes::FabricsList::Id: { + return ReadFabricsList(aClusterInfo.mEndpointId, aEncoder); + } + default: + break; + } + + return CHIP_NO_ERROR; +} +} // anonymous namespace // As per specifications section 11.22.5.1. Constant RESP_MAX constexpr uint16_t kMaxRspLen = 900; @@ -244,6 +305,11 @@ OpCredsFabricTableDelegate gFabricDelegate; void MatterOperationalCredentialsPluginServerInitCallback(void) { emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: Initiating OpCreds cluster by writing fabrics list from fabric table."); + +#if CHIP_CLUSTER_CONFIG_ENABLE_COMPLEX_ATTRIBUTE_READ + registerAttributeAccessOverride(&gAttrAccess); +#endif + Server::GetInstance().GetFabricTable().SetFabricDelegate(&gFabricDelegate); writeFabricsIntoFabricsListAttribute(); } diff --git a/zzz_generated/chip-tool/zap-generated/cluster/Commands.h b/zzz_generated/chip-tool/zap-generated/cluster/Commands.h index a968cc06d72135..782c718cad6cc6 100644 --- a/zzz_generated/chip-tool/zap-generated/cluster/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/cluster/Commands.h @@ -1247,8 +1247,7 @@ class AccountLoginGetSetupPIN : public ModelCommand chip::Controller::AccountLoginCluster cluster; cluster.Associate(device, endpointId); - return cluster.GetSetupPIN(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mTempAccountIdentifier), strlen(mTempAccountIdentifier))); + return cluster.GetSetupPIN(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mTempAccountIdentifier); } private: @@ -1257,7 +1256,7 @@ class AccountLoginGetSetupPIN : public ModelCommand this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mTempAccountIdentifier; + chip::ByteSpan mTempAccountIdentifier; }; /* @@ -1284,9 +1283,7 @@ class AccountLoginLogin : public ModelCommand chip::Controller::AccountLoginCluster cluster; cluster.Associate(device, endpointId); - return cluster.Login(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mTempAccountIdentifier), strlen(mTempAccountIdentifier)), - chip::ByteSpan(chip::Uint8::from_char(mSetupPIN), strlen(mSetupPIN))); + return cluster.Login(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mTempAccountIdentifier, mSetupPIN); } private: @@ -1294,8 +1291,8 @@ class AccountLoginLogin : public ModelCommand new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mTempAccountIdentifier; - char * mSetupPIN; + chip::ByteSpan mTempAccountIdentifier; + chip::ByteSpan mSetupPIN; }; /* @@ -1848,9 +1845,7 @@ class ApplicationLauncherLaunchApp : public ModelCommand chip::Controller::ApplicationLauncherCluster cluster; cluster.Associate(device, endpointId); - return cluster.LaunchApp(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mData), strlen(mData)), mCatalogVendorId, - chip::ByteSpan(chip::Uint8::from_char(mApplicationId), strlen(mApplicationId))); + return cluster.LaunchApp(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mData, mCatalogVendorId, mApplicationId); } private: @@ -1859,9 +1854,9 @@ class ApplicationLauncherLaunchApp : public ModelCommand OnApplicationLauncherClusterLaunchAppResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mData; + chip::ByteSpan mData; uint16_t mCatalogVendorId; - char * mApplicationId; + chip::ByteSpan mApplicationId; }; /* @@ -2038,8 +2033,7 @@ class AudioOutputRenameOutput : public ModelCommand chip::Controller::AudioOutputCluster cluster; cluster.Associate(device, endpointId); - return cluster.RenameOutput(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mIndex, - chip::ByteSpan(chip::Uint8::from_char(mName), strlen(mName))); + return cluster.RenameOutput(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mIndex, mName); } private: @@ -2048,7 +2042,7 @@ class AudioOutputRenameOutput : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); uint8_t mIndex; - char * mName; + chip::ByteSpan mName; }; /* @@ -2719,8 +2713,7 @@ class WriteBasicUserLabel : public ModelCommand chip::Controller::BasicCluster cluster; cluster.Associate(device, endpointId); - return cluster.WriteAttributeUserLabel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue))); + return cluster.WriteAttributeUserLabel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue); } private: @@ -2728,7 +2721,7 @@ class WriteBasicUserLabel : public ModelCommand new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mValue; + chip::ByteSpan mValue; }; /* @@ -2787,8 +2780,7 @@ class WriteBasicLocation : public ModelCommand chip::Controller::BasicCluster cluster; cluster.Associate(device, endpointId); - return cluster.WriteAttributeLocation(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue))); + return cluster.WriteAttributeLocation(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue); } private: @@ -2796,7 +2788,7 @@ class WriteBasicLocation : public ModelCommand new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mValue; + chip::ByteSpan mValue; }; /* @@ -3852,8 +3844,7 @@ class WriteBridgedDeviceBasicUserLabel : public ModelCommand chip::Controller::BridgedDeviceBasicCluster cluster; cluster.Associate(device, endpointId); - return cluster.WriteAttributeUserLabel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue))); + return cluster.WriteAttributeUserLabel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue); } private: @@ -3861,7 +3852,7 @@ class WriteBridgedDeviceBasicUserLabel : public ModelCommand new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mValue; + chip::ByteSpan mValue; }; /* @@ -7626,8 +7617,7 @@ class ContentLauncherLaunchContent : public ModelCommand chip::Controller::ContentLauncherCluster cluster; cluster.Associate(device, endpointId); - return cluster.LaunchContent(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mAutoPlay, - chip::ByteSpan(chip::Uint8::from_char(mData), strlen(mData))); + return cluster.LaunchContent(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mAutoPlay, mData); } private: @@ -7637,7 +7627,7 @@ class ContentLauncherLaunchContent : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); bool mAutoPlay; - char * mData; + chip::ByteSpan mData; }; /* @@ -7664,9 +7654,7 @@ class ContentLauncherLaunchURL : public ModelCommand chip::Controller::ContentLauncherCluster cluster; cluster.Associate(device, endpointId); - return cluster.LaunchURL(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mContentURL), strlen(mContentURL)), - chip::ByteSpan(chip::Uint8::from_char(mDisplayString), strlen(mDisplayString))); + return cluster.LaunchURL(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mContentURL, mDisplayString); } private: @@ -7675,8 +7663,8 @@ class ContentLauncherLaunchURL : public ModelCommand this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mContentURL; - char * mDisplayString; + chip::ByteSpan mContentURL; + chip::ByteSpan mDisplayString; }; /* @@ -10216,9 +10204,8 @@ class GeneralCommissioningSetRegulatoryConfig : public ModelCommand chip::Controller::GeneralCommissioningCluster cluster; cluster.Associate(device, endpointId); - return cluster.SetRegulatoryConfig(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mLocation, - chip::ByteSpan(chip::Uint8::from_char(mCountryCode), strlen(mCountryCode)), mBreadcrumb, - mTimeoutMs); + return cluster.SetRegulatoryConfig(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mLocation, mCountryCode, + mBreadcrumb, mTimeoutMs); } private: @@ -10228,7 +10215,7 @@ class GeneralCommissioningSetRegulatoryConfig : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); uint8_t mLocation; - char * mCountryCode; + chip::ByteSpan mCountryCode; uint64_t mBreadcrumb; uint32_t mTimeoutMs; }; @@ -10743,8 +10730,7 @@ class GroupsAddGroup : public ModelCommand chip::Controller::GroupsCluster cluster; cluster.Associate(device, endpointId); - return cluster.AddGroup(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mGroupId, - chip::ByteSpan(chip::Uint8::from_char(mGroupName), strlen(mGroupName))); + return cluster.AddGroup(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mGroupId, mGroupName); } private: @@ -10753,7 +10739,7 @@ class GroupsAddGroup : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); uint16_t mGroupId; - char * mGroupName; + chip::ByteSpan mGroupName; }; /* @@ -10780,8 +10766,7 @@ class GroupsAddGroupIfIdentifying : public ModelCommand chip::Controller::GroupsCluster cluster; cluster.Associate(device, endpointId); - return cluster.AddGroupIfIdentifying(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mGroupId, - chip::ByteSpan(chip::Uint8::from_char(mGroupName), strlen(mGroupName))); + return cluster.AddGroupIfIdentifying(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mGroupId, mGroupName); } private: @@ -10790,7 +10775,7 @@ class GroupsAddGroupIfIdentifying : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); uint16_t mGroupId; - char * mGroupName; + chip::ByteSpan mGroupName; }; /* @@ -11895,8 +11880,7 @@ class MediaInputRenameInput : public ModelCommand chip::Controller::MediaInputCluster cluster; cluster.Associate(device, endpointId); - return cluster.RenameInput(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mIndex, - chip::ByteSpan(chip::Uint8::from_char(mName), strlen(mName))); + return cluster.RenameInput(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mIndex, mName); } private: @@ -11905,7 +11889,7 @@ class MediaInputRenameInput : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); uint8_t mIndex; - char * mName; + chip::ByteSpan mName; }; /* @@ -13308,9 +13292,7 @@ class OtaSoftwareUpdateProviderQueryImage : public ModelCommand chip::Controller::OtaSoftwareUpdateProviderCluster cluster; cluster.Associate(device, endpointId); return cluster.QueryImage(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mVendorId, mProductId, mHardwareVersion, - mSoftwareVersion, mProtocolsSupported, - chip::ByteSpan(chip::Uint8::from_char(mLocation), strlen(mLocation)), mRequestorCanConsent, - mMetadataForProvider); + mSoftwareVersion, mProtocolsSupported, mLocation, mRequestorCanConsent, mMetadataForProvider); } private: @@ -13324,7 +13306,7 @@ class OtaSoftwareUpdateProviderQueryImage : public ModelCommand uint16_t mHardwareVersion; uint32_t mSoftwareVersion; uint8_t mProtocolsSupported; - char * mLocation; + chip::ByteSpan mLocation; bool mRequestorCanConsent; chip::ByteSpan mMetadataForProvider; }; @@ -14783,8 +14765,7 @@ class OperationalCredentialsUpdateFabricLabel : public ModelCommand chip::Controller::OperationalCredentialsCluster cluster; cluster.Associate(device, endpointId); - return cluster.UpdateFabricLabel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mLabel), strlen(mLabel))); + return cluster.UpdateFabricLabel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mLabel); } private: @@ -14793,7 +14774,7 @@ class OperationalCredentialsUpdateFabricLabel : public ModelCommand this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mLabel; + chip::ByteSpan mLabel; }; /* @@ -16170,8 +16151,7 @@ class ScenesAddScene : public ModelCommand chip::Controller::ScenesCluster cluster; cluster.Associate(device, endpointId); return cluster.AddScene(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mGroupId, mSceneId, mTransitionTime, - chip::ByteSpan(chip::Uint8::from_char(mSceneName), strlen(mSceneName)), mClusterId, mLength, - mValue); + mSceneName, mClusterId, mLength, mValue); } private: @@ -16182,7 +16162,7 @@ class ScenesAddScene : public ModelCommand uint16_t mGroupId; uint8_t mSceneId; uint16_t mTransitionTime; - char * mSceneName; + chip::ByteSpan mSceneName; chip::ClusterId mClusterId; uint8_t mLength; uint8_t mValue; @@ -16982,8 +16962,7 @@ class TvChannelChangeChannel : public ModelCommand chip::Controller::TvChannelCluster cluster; cluster.Associate(device, endpointId); - return cluster.ChangeChannel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mMatch), strlen(mMatch))); + return cluster.ChangeChannel(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mMatch); } private: @@ -16991,7 +16970,7 @@ class TvChannelChangeChannel : public ModelCommand new chip::Callback::Callback(OnTvChannelClusterChangeChannelResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mMatch; + chip::ByteSpan mMatch; }; /* @@ -17236,8 +17215,7 @@ class TargetNavigatorNavigateTarget : public ModelCommand chip::Controller::TargetNavigatorCluster cluster; cluster.Associate(device, endpointId); - return cluster.NavigateTarget(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mTarget, - chip::ByteSpan(chip::Uint8::from_char(mData), strlen(mData))); + return cluster.NavigateTarget(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mTarget, mData); } private: @@ -17247,7 +17225,7 @@ class TargetNavigatorNavigateTarget : public ModelCommand chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); uint8_t mTarget; - char * mData; + chip::ByteSpan mData; }; /* @@ -19005,8 +18983,7 @@ class WriteTestClusterCharString : public ModelCommand chip::Controller::TestClusterCluster cluster; cluster.Associate(device, endpointId); - return cluster.WriteAttributeCharString(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue))); + return cluster.WriteAttributeCharString(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue); } private: @@ -19014,7 +18991,7 @@ class WriteTestClusterCharString : public ModelCommand new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mValue; + chip::ByteSpan mValue; }; /* @@ -19073,8 +19050,7 @@ class WriteTestClusterLongCharString : public ModelCommand chip::Controller::TestClusterCluster cluster; cluster.Associate(device, endpointId); - return cluster.WriteAttributeLongCharString(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), - chip::ByteSpan(chip::Uint8::from_char(mValue), strlen(mValue))); + return cluster.WriteAttributeLongCharString(onSuccessCallback->Cancel(), onFailureCallback->Cancel(), mValue); } private: @@ -19082,7 +19058,7 @@ class WriteTestClusterLongCharString : public ModelCommand new chip::Callback::Callback(OnDefaultSuccessResponse, this); chip::Callback::Callback * onFailureCallback = new chip::Callback::Callback(OnDefaultFailureResponse, this); - char * mValue; + chip::ByteSpan mValue; }; /* diff --git a/zzz_generated/chip-tool/zap-generated/reporting/Commands.h b/zzz_generated/chip-tool/zap-generated/reporting/Commands.h index 2728c84b7e5c10..c15d74a16694b3 100644 --- a/zzz_generated/chip-tool/zap-generated/reporting/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/reporting/Commands.h @@ -57,81 +57,70 @@ class Listen : public ReportingCommand delete onReportWindowCoveringSafetyStatusCallback; } - void AddReportCallbacks(uint8_t endpointId) override + void AddReportCallbacks(NodeId remoteId, uint8_t endpointId) override { chip::app::CHIPDeviceCallbacksMgr & callbacksMgr = chip::app::CHIPDeviceCallbacksMgr::GetInstance(); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x000F, 0x0055, - onReportBinaryInputBasicPresentValueCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x000F, 0x0055, onReportBinaryInputBasicPresentValueCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x000F, 0x006F, - onReportBinaryInputBasicStatusFlagsCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x000F, 0x006F, onReportBinaryInputBasicStatusFlagsCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0300, 0x0000, - onReportColorControlCurrentHueCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0300, 0x0000, onReportColorControlCurrentHueCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0300, 0x0001, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0300, 0x0001, onReportColorControlCurrentSaturationCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0300, 0x0003, - onReportColorControlCurrentXCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0300, 0x0003, onReportColorControlCurrentXCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0300, 0x0004, - onReportColorControlCurrentYCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0300, 0x0004, onReportColorControlCurrentYCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0300, 0x0007, - onReportColorControlColorTemperatureCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0300, 0x0007, onReportColorControlColorTemperatureCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0101, 0x0000, - onReportDoorLockLockStateCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0008, 0x0000, - onReportLevelControlCurrentLevelCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0101, 0x0000, onReportDoorLockLockStateCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0406, 0x0000, - onReportOccupancySensingOccupancyCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0008, 0x0000, onReportLevelControlCurrentLevelCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0006, 0x0000, - onReportOnOffOnOffCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0403, 0x0000, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0406, 0x0000, onReportOccupancySensingOccupancyCallback->Cancel(), + BasicAttributeFilter); + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0006, 0x0000, onReportOnOffOnOffCallback->Cancel(), + BasicAttributeFilter); + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0403, 0x0000, onReportPressureMeasurementMeasuredValueCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0200, 0x0013, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0200, 0x0013, onReportPumpConfigurationAndControlCapacityCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0405, 0x0000, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0405, 0x0000, onReportRelativeHumidityMeasurementMeasuredValueCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x003B, 0x0001, - onReportSwitchCurrentPositionCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x003B, 0x0001, onReportSwitchCurrentPositionCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0402, 0x0000, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0402, 0x0000, onReportTemperatureMeasurementMeasuredValueCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0201, 0x0000, - onReportThermostatLocalTemperatureCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0201, 0x0000, onReportThermostatLocalTemperatureCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x0008, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x0008, onReportWindowCoveringCurrentPositionLiftPercentageCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x0009, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x0009, onReportWindowCoveringCurrentPositionTiltPercentageCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x000A, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x000A, onReportWindowCoveringOperationalStatusCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x000B, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x000B, onReportWindowCoveringTargetPositionLiftPercent100thsCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x000C, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x000C, onReportWindowCoveringTargetPositionTiltPercent100thsCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x000E, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x000E, onReportWindowCoveringCurrentPositionLiftPercent100thsCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x000F, + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x000F, onReportWindowCoveringCurrentPositionTiltPercent100thsCallback->Cancel(), BasicAttributeFilter); - callbacksMgr.AddReportCallback(GetExecContext()->storage->GetRemoteNodeId(), endpointId, 0x0102, 0x001A, - onReportWindowCoveringSafetyStatusCallback->Cancel(), + callbacksMgr.AddReportCallback(remoteId, endpointId, 0x0102, 0x001A, onReportWindowCoveringSafetyStatusCallback->Cancel(), BasicAttributeFilter); }