Skip to content

Commit

Permalink
Add update command to the discover module of chip-tool
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Apr 12, 2021
1 parent 7ef91f9 commit b1848f4
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 28 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
64 changes: 36 additions & 28 deletions examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,23 @@

#pragma once

#include "Command.h"
#include "DiscoverCommand.h"
#include <controller/DeviceAddressUpdater.h>
#include <mdns/Resolver.h>

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];
Expand All @@ -63,20 +48,43 @@ 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)
{
const char * clusterName = "Discover";

commands_list clusterCommands = {
make_unique<Discover>(),
make_unique<Resolve>(),
make_unique<Update>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
40 changes: 40 additions & 0 deletions examples/chip-tool/commands/discover/DiscoverCommand.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
44 changes: 44 additions & 0 deletions examples/chip-tool/commands/discover/DiscoverCommand.h
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit b1848f4

Please sign in to comment.