Skip to content

Commit

Permalink
Mdns device controller update device (#5956)
Browse files Browse the repository at this point in the history
* Add update command to the discover module of chip-tool

* Add DeviceController::UpdateDevice to update device information
  • Loading branch information
vivien-apple authored Apr 13, 2021
1 parent e9c15e3 commit e047d27
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 29 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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
67 changes: 38 additions & 29 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,44 @@ class Discover : public Command, public chip::Mdns::ResolverDelegate
ChipLogProgress(chipTool, "NodeId Resolution: failed!");
SetCommandExitStatus(false);
};
};

private:
uint16_t mWaitDurationInSeconds = 30;
ChipDeviceCommissioner mCommissioner;
chip::NodeId mNodeId;
uint64_t mFabricId;
class Update : public DiscoverCommand
{
public:
Update() : DiscoverCommand("update") {}

/////////// DiscoverCommand Interface /////////
CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override
{
ChipDevice * device;
ReturnErrorOnFailure(mCommissioner.GetDevice(remoteId, &device));
return mCommissioner.UpdateDevice(device, fabricId);
}

/////////// DeviceAddressUpdateDelegate Interface /////////
void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override
{
if (CHIP_NO_ERROR == error)
{
ChipLogProgress(chipTool, "Device address updated successfully");
}
else
{
ChipLogError(chipTool, "Failed to update the device address: %s", chip::ErrorStr(error));
}

SetCommandExitStatus(CHIP_NO_ERROR == error);
}
};

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
45 changes: 45 additions & 0 deletions examples/chip-tool/commands/discover/DiscoverCommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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)
{
chip::Controller::ControllerInitParams params{
.storageDelegate = &storage,
.mDeviceAddressUpdateDelegate = this,
};

ReturnErrorOnFailure(mCommissioner.SetUdpListenPort(storage.GetListenPort()));
ReturnErrorOnFailure(mCommissioner.Init(localId, params));
ReturnErrorOnFailure(mCommissioner.ServiceEvents());

ReturnErrorOnFailure(RunCommand(mNodeId, mFabricId));

UpdateWaitForResponse(true);
WaitForResponse(kWaitDurationInSeconds);

mCommissioner.ServiceEventSignal();
mCommissioner.Shutdown();

VerifyOrReturnError(GetCommandExitStatus(), CHIP_ERROR_INTERNAL);

return CHIP_NO_ERROR;
}
47 changes: 47 additions & 0 deletions examples/chip-tool/commands/discover/DiscoverCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 chip::Controller::DeviceAddressUpdateDelegate
{
public:
DiscoverCommand(const char * commandName) : Command(commandName)
{
AddArgument("nodeid", 0, UINT64_MAX, &mNodeId);
AddArgument("fabricid", 0, UINT64_MAX, &mFabricId);
}

/////////// DeviceAddressUpdateDelegate Interface /////////
void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override{};

/////////// 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;
};
47 changes: 47 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ CHIP_ERROR DeviceController::Init(NodeId localDeviceId, ControllerInitParams par

mExchangeMgr->SetDelegate(this);

#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
err = Mdns::Resolver::Instance().SetResolverDelegate(this);
SuccessOrExit(err);

if (params.mDeviceAddressUpdateDelegate != nullptr)
{
mDeviceAddressUpdateDelegate = params.mDeviceAddressUpdateDelegate;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS

InitDataModelHandler();

mState = State::Initialized;
Expand Down Expand Up @@ -329,6 +339,15 @@ CHIP_ERROR DeviceController::GetDevice(NodeId deviceId, Device ** out_device)
return err;
}

CHIP_ERROR DeviceController::UpdateDevice(Device * device, uint64_t fabricId)
{
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
return Mdns::Resolver::Instance().ResolveNodeId(device->GetDeviceId(), fabricId, chip::Inet::kIPAddressType_Any);
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS
}

void DeviceController::PersistDevice(Device * device)
{
// mStorageDelegate would not be null for a typical pairing scenario, as Pair()
Expand Down Expand Up @@ -548,6 +567,34 @@ CHIP_ERROR DeviceController::SetPairedDeviceList(const char * serialized)

void DeviceController::OnPersistentStorageStatus(const char * key, Operation op, CHIP_ERROR err) {}

#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
void DeviceController::OnNodeIdResolved(NodeId nodeId, const chip::Mdns::ResolvedNodeData & nodeData)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Device * device = nullptr;

err = GetDevice(nodeId, &device);
SuccessOrExit(err);

err = device->UpdateAddress(Transport::PeerAddress::UDP(nodeData.mAddress, nodeData.mPort, nodeData.mInterfaceId));
SuccessOrExit(err);

PersistDevice(device);

exit:
if (mDeviceAddressUpdateDelegate != nullptr)
{
mDeviceAddressUpdateDelegate->OnAddressUpdateComplete(nodeId, err);
}
return;
};

void DeviceController::OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error)
{
ChipLogError(Controller, "Error resolving node %" PRIu64 ": %s", ErrorStr(error));
};
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS

ControllerDeviceInitParams DeviceController::GetControllerDeviceInitParams()
{
return ControllerDeviceInitParams{
Expand Down
31 changes: 31 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <transport/TransportMgr.h>
#include <transport/raw/UDP.h>

#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
#include <controller/DeviceAddressUpdater.h>
#endif

namespace chip {

namespace Controller {
Expand All @@ -59,6 +63,9 @@ struct ControllerInitParams
#if CHIP_ENABLE_INTERACTION_MODEL
app::InteractionModelDelegate * imDelegate = nullptr;
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
DeviceAddressUpdateDelegate * mDeviceAddressUpdateDelegate = nullptr;
#endif
};

class DLL_EXPORT DevicePairingDelegate
Expand Down Expand Up @@ -121,6 +128,9 @@ class DLL_EXPORT DevicePairingDelegate
class DLL_EXPORT DeviceController : public Messaging::ExchangeDelegate,
public Messaging::ExchangeMgrDelegate,
public PersistentStorageResultDelegate,
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
public Mdns::ResolverDelegate,
#endif
public app::InteractionModelDelegate
{
public:
Expand Down Expand Up @@ -166,6 +176,18 @@ class DLL_EXPORT DeviceController : public Messaging::ExchangeDelegate,
*/
CHIP_ERROR GetDevice(NodeId deviceId, Device ** device);

/**
* @brief
* This function update the device informations asynchronously using mdns.
* If new device informations has been found, it will be persisted.
*
* @param[in] device The input device object to update
* @param[in] fabricId The fabricId used for mdns resolution
*
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error code.
*/
CHIP_ERROR UpdateDevice(Device * device, uint64_t fabricId);

void PersistDevice(Device * device);

CHIP_ERROR SetUdpListenPort(uint16_t listenPort);
Expand Down Expand Up @@ -211,6 +233,9 @@ class DLL_EXPORT DeviceController : public Messaging::ExchangeDelegate,
SecureSessionMgr * mSessionMgr;
Messaging::ExchangeManager * mExchangeMgr;
PersistentStorageDelegate * mStorageDelegate;
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
DeviceAddressUpdateDelegate * mDeviceAddressUpdateDelegate = nullptr;
#endif
Inet::InetLayer * mInetLayer;
System::Layer * mSystemLayer;

Expand Down Expand Up @@ -240,6 +265,12 @@ class DLL_EXPORT DeviceController : public Messaging::ExchangeDelegate,
//////////// PersistentStorageResultDelegate Implementation ///////////////
void OnPersistentStorageStatus(const char * key, Operation op, CHIP_ERROR err) override;

#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
//////////// ResolverDelegate Implementation ///////////////
void OnNodeIdResolved(NodeId nodeId, const chip::Mdns::ResolvedNodeData & nodeData) override;
void OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error) override;
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS

void ReleaseAllDevices();
};

Expand Down

0 comments on commit e047d27

Please sign in to comment.