From b1848f4fea98b263522c5d41fbe7f78d4763598b Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Mon, 12 Apr 2021 17:46:07 +0200 Subject: [PATCH] Add update command to the discover module of chip-tool --- examples/chip-tool/BUILD.gn | 1 + .../chip-tool/commands/discover/Commands.h | 64 +++++++++++-------- .../commands/discover/DiscoverCommand.cpp | 40 ++++++++++++ .../commands/discover/DiscoverCommand.h | 44 +++++++++++++ 4 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 examples/chip-tool/commands/discover/DiscoverCommand.cpp create mode 100644 examples/chip-tool/commands/discover/DiscoverCommand.h diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index c9b7d8f58a3e71..e18caac5e9220a 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -37,6 +37,7 @@ executable("chip-tool") { "commands/clusters/ModelCommand.cpp", "commands/common/Command.cpp", "commands/common/Commands.cpp", + "commands/discover/DiscoverCommand.cpp", "commands/pairing/PairingCommand.cpp", "commands/payload/AdditionalDataParseCommand.cpp", "commands/payload/SetupPayloadParseCommand.cpp", diff --git a/examples/chip-tool/commands/discover/Commands.h b/examples/chip-tool/commands/discover/Commands.h index f9f3f41df7ebf8..e1bec28686623f 100644 --- a/examples/chip-tool/commands/discover/Commands.h +++ b/examples/chip-tool/commands/discover/Commands.h @@ -18,38 +18,23 @@ #pragma once -#include "Command.h" +#include "DiscoverCommand.h" +#include #include -class Discover : public Command, public chip::Mdns::ResolverDelegate +class Resolve : public DiscoverCommand, public chip::Mdns::ResolverDelegate { public: - Discover() : Command("resolve-node-id") - { - AddArgument("nodeid", 0, UINT64_MAX, &mNodeId); - AddArgument("fabricid", 0, UINT64_MAX, &mFabricId); - } + Resolve() : DiscoverCommand("resolve") {} - CHIP_ERROR Run(PersistentStorage & storage, NodeId localId, NodeId remoteId) override + /////////// DiscoverCommand Interface ///////// + CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override { - ReturnErrorOnFailure(mCommissioner.SetUdpListenPort(storage.GetListenPort())); - ReturnErrorOnFailure(mCommissioner.Init(localId, &storage)); - ReturnErrorOnFailure(mCommissioner.ServiceEvents()); - ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().SetResolverDelegate(this)); - ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().ResolveNodeId(mNodeId, mFabricId, chip::Inet::kIPAddressType_Any)); - - UpdateWaitForResponse(true); - WaitForResponse(mWaitDurationInSeconds); - - mCommissioner.ServiceEventSignal(); - mCommissioner.Shutdown(); - - VerifyOrReturnError(GetCommandExitStatus(), CHIP_ERROR_INTERNAL); - - return CHIP_NO_ERROR; + return chip::Mdns::Resolver::Instance().ResolveNodeId(remoteId, fabricId, chip::Inet::kIPAddressType_Any); } + /////////// ResolverDelegate Interface ///////// void OnNodeIdResolved(NodeId nodeId, const chip::Mdns::ResolvedNodeData & nodeData) override { char addrBuffer[chip::Transport::PeerAddress::kMaxToStringSize]; @@ -63,12 +48,34 @@ class Discover : public Command, public chip::Mdns::ResolverDelegate ChipLogProgress(chipTool, "NodeId Resolution: failed!"); SetCommandExitStatus(false); }; +}; + +class Update : public DiscoverCommand, public chip::Controller::DeviceAddressUpdateDelegate +{ +public: + Update() : DiscoverCommand("update") {} + + /////////// DiscoverCommand Interface ///////// + CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override + { + ReturnErrorOnFailure(mAddressUpdater.Init(&mCommissioner, this)); + ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().SetResolverDelegate(&mAddressUpdater)); + return chip::Mdns::Resolver::Instance().ResolveNodeId(remoteId, fabricId, chip::Inet::kIPAddressType_Any); + } + + /////////// DeviceAddressUpdateDelegate Interface ///////// + void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override + { + if (CHIP_NO_ERROR != error) + { + ChipLogError(chipTool, "Failed to update the device address: %s", chip::ErrorStr(error)); + } + + SetCommandExitStatus(CHIP_NO_ERROR == error); + } private: - uint16_t mWaitDurationInSeconds = 30; - ChipDeviceCommissioner mCommissioner; - chip::NodeId mNodeId; - uint64_t mFabricId; + chip::Controller::DeviceAddressUpdater mAddressUpdater; }; void registerCommandsDiscover(Commands & commands) @@ -76,7 +83,8 @@ void registerCommandsDiscover(Commands & commands) const char * clusterName = "Discover"; commands_list clusterCommands = { - make_unique(), + make_unique(), + make_unique(), }; commands.Register(clusterName, clusterCommands); diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommand.cpp new file mode 100644 index 00000000000000..f76624639b5efe --- /dev/null +++ b/examples/chip-tool/commands/discover/DiscoverCommand.cpp @@ -0,0 +1,40 @@ +/* + * 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 "DiscoverCommand.h" + +constexpr uint16_t kWaitDurationInSeconds = 30; + +CHIP_ERROR DiscoverCommand::Run(PersistentStorage & storage, NodeId localId, NodeId remoteId) +{ + ReturnErrorOnFailure(mCommissioner.SetUdpListenPort(storage.GetListenPort())); + ReturnErrorOnFailure(mCommissioner.Init(localId, &storage)); + ReturnErrorOnFailure(mCommissioner.ServiceEvents()); + + ReturnErrorOnFailure(RunCommand(mNodeId, mFabricId)); + + UpdateWaitForResponse(true); + WaitForResponse(kWaitDurationInSeconds); + + mCommissioner.ServiceEventSignal(); + mCommissioner.Shutdown(); + + VerifyOrReturnError(GetCommandExitStatus(), CHIP_ERROR_INTERNAL); + + return CHIP_NO_ERROR; +} diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.h b/examples/chip-tool/commands/discover/DiscoverCommand.h new file mode 100644 index 00000000000000..fc53bb35b02372 --- /dev/null +++ b/examples/chip-tool/commands/discover/DiscoverCommand.h @@ -0,0 +1,44 @@ +/* + * 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 "../common/Command.h" + +class DiscoverCommand : public Command +{ +public: + DiscoverCommand(const char * commandName) : Command(commandName) + { + AddArgument("nodeid", 0, UINT64_MAX, &mNodeId); + AddArgument("fabricid", 0, UINT64_MAX, &mFabricId); + } + + /////////// Command Interface ///////// + CHIP_ERROR Run(PersistentStorage & storage, NodeId localId, NodeId remoteId) override; + + virtual CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) = 0; + +protected: + ChipDeviceCommissioner mCommissioner; + +private: + chip::NodeId mNodeId; + uint64_t mFabricId; +};