Skip to content

Commit

Permalink
Add DeviceController::UpdateDevice to update device informations
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Apr 12, 2021
1 parent b1848f4 commit 803912d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
17 changes: 9 additions & 8 deletions examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,33 @@ class Resolve : public DiscoverCommand, public chip::Mdns::ResolverDelegate
};
};

class Update : public DiscoverCommand, public chip::Controller::DeviceAddressUpdateDelegate
class Update : public DiscoverCommand
{
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);
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)
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);
}

private:
chip::Controller::DeviceAddressUpdater mAddressUpdater;
};

void registerCommandsDiscover(Commands & commands)
Expand Down
7 changes: 6 additions & 1 deletion examples/chip-tool/commands/discover/DiscoverCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ 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, &storage));
ReturnErrorOnFailure(mCommissioner.Init(localId, params));
ReturnErrorOnFailure(mCommissioner.ServiceEvents());

ReturnErrorOnFailure(RunCommand(mNodeId, mFabricId));
Expand Down
5 changes: 4 additions & 1 deletion examples/chip-tool/commands/discover/DiscoverCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "../../config/PersistentStorage.h"
#include "../common/Command.h"

class DiscoverCommand : public Command
class DiscoverCommand : public Command, public chip::Controller::DeviceAddressUpdateDelegate
{
public:
DiscoverCommand(const char * commandName) : Command(commandName)
Expand All @@ -30,6 +30,9 @@ class DiscoverCommand : public Command
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;

Expand Down
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 803912d

Please sign in to comment.