diff --git a/examples/chip-tool/commands/discover/Commands.h b/examples/chip-tool/commands/discover/Commands.h index e1bec28686623f..30ef4a12a7dc9c 100644 --- a/examples/chip-tool/commands/discover/Commands.h +++ b/examples/chip-tool/commands/discover/Commands.h @@ -50,7 +50,7 @@ class Resolve : public DiscoverCommand, public chip::Mdns::ResolverDelegate }; }; -class Update : public DiscoverCommand, public chip::Controller::DeviceAddressUpdateDelegate +class Update : public DiscoverCommand { public: Update() : DiscoverCommand("update") {} @@ -58,24 +58,25 @@ class Update : public DiscoverCommand, public chip::Controller::DeviceAddressUpd /////////// 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) diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommand.cpp index f76624639b5efe..3e75dbf07cdc46 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommand.cpp +++ b/examples/chip-tool/commands/discover/DiscoverCommand.cpp @@ -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)); diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.h b/examples/chip-tool/commands/discover/DiscoverCommand.h index fc53bb35b02372..724fc55b311e12 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommand.h +++ b/examples/chip-tool/commands/discover/DiscoverCommand.h @@ -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) @@ -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; diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index c1709e6e65ef27..a3d2a33567435d 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -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; @@ -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() @@ -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{ diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index 4cda9ccdc56373..2032813dc3875c 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -44,6 +44,10 @@ #include #include +#if CHIP_DEVICE_CONFIG_ENABLE_MDNS +#include +#endif + namespace chip { namespace Controller { @@ -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 @@ -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: @@ -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); @@ -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; @@ -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(); };