diff --git a/examples/chip-tool/commands/discover/Commands.h b/examples/chip-tool/commands/discover/Commands.h index 30ef4a12a7dc9c..387001c0da3d43 100644 --- a/examples/chip-tool/commands/discover/Commands.h +++ b/examples/chip-tool/commands/discover/Commands.h @@ -19,7 +19,7 @@ #pragma once #include "DiscoverCommand.h" -#include +#include #include class Resolve : public DiscoverCommand, public chip::Mdns::ResolverDelegate diff --git a/src/controller/BUILD.gn b/src/controller/BUILD.gn index 6cb30f86880a2c..70b91446ad6a98 100644 --- a/src/controller/BUILD.gn +++ b/src/controller/BUILD.gn @@ -27,8 +27,7 @@ static_library("controller") { "CHIPDevice.h", "CHIPDeviceController.cpp", "CHIPDeviceController.h", - "DeviceAddressUpdater.cpp", - "DeviceAddressUpdater.h", + "DeviceAddressUpdateDelegate.h", "EmptyDataModelHandler.cpp", ] diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index aa5688a261fcd3..ccd2b2db684375 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -591,7 +591,12 @@ void DeviceController::OnNodeIdResolved(NodeId nodeId, const chip::Mdns::Resolve void DeviceController::OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error) { - ChipLogError(Controller, "Error resolving node %" PRIu64 ": %s", ErrorStr(error)); + ChipLogError(Controller, "Error resolving node id: %s", ErrorStr(error)); + + if (mDeviceAddressUpdateDelegate != nullptr) + { + mDeviceAddressUpdateDelegate->OnAddressUpdateComplete(nodeId, error); + } }; #endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index fc794e74fcea75..427c18618b2df0 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -45,7 +45,8 @@ #include #if CHIP_DEVICE_CONFIG_ENABLE_MDNS -#include +#include +#include #endif namespace chip { diff --git a/src/controller/DeviceAddressUpdater.h b/src/controller/DeviceAddressUpdateDelegate.h similarity index 62% rename from src/controller/DeviceAddressUpdater.h rename to src/controller/DeviceAddressUpdateDelegate.h index 1ee3a31c8f7e5b..8af9dc602bd32e 100644 --- a/src/controller/DeviceAddressUpdater.h +++ b/src/controller/DeviceAddressUpdateDelegate.h @@ -18,15 +18,12 @@ #pragma once -#include #include #include namespace chip { namespace Controller { -class DeviceController; - /// Callbacks for CHIP device address resolution class DLL_EXPORT DeviceAddressUpdateDelegate { @@ -35,20 +32,5 @@ class DLL_EXPORT DeviceAddressUpdateDelegate virtual void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) = 0; }; -/// Class for updating CHIP devices' addresses based on responses from mDNS Resolver -class DLL_EXPORT DeviceAddressUpdater : public Mdns::ResolverDelegate -{ -public: - CHIP_ERROR Init(DeviceController * controller, DeviceAddressUpdateDelegate * delegate = nullptr); - -private: - // Mdns::ResolverDelegate Implementation - void OnNodeIdResolved(NodeId nodeId, const Mdns::ResolvedNodeData & nodeData) override; - void OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error) override; - - DeviceController * mController = nullptr; - DeviceAddressUpdateDelegate * mDelegate = nullptr; -}; - } // namespace Controller } // namespace chip diff --git a/src/controller/DeviceAddressUpdater.cpp b/src/controller/DeviceAddressUpdater.cpp deleted file mode 100644 index 7854cb0089b709..00000000000000 --- a/src/controller/DeviceAddressUpdater.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * - * 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 "DeviceAddressUpdater.h" - -#include - -namespace chip { -namespace Controller { - -CHIP_ERROR DeviceAddressUpdater::Init(DeviceController * controller, DeviceAddressUpdateDelegate * delegate) -{ - VerifyOrReturnError(mController == nullptr, CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(mDelegate == nullptr, CHIP_ERROR_INCORRECT_STATE); - - mController = controller; - mDelegate = delegate; - - return CHIP_NO_ERROR; -} - -void DeviceAddressUpdater::OnNodeIdResolved(NodeId nodeId, const Mdns::ResolvedNodeData & nodeData) -{ - CHIP_ERROR error = CHIP_NO_ERROR; - Device * device = nullptr; - - VerifyOrExit(nodeData.mAddress.Type() != Inet::kIPAddressType_Any, error = CHIP_ERROR_INVALID_ADDRESS); - VerifyOrExit(mController != nullptr, error = CHIP_ERROR_INCORRECT_STATE); - SuccessOrExit(error = mController->GetDevice(nodeId, &device)); - - device->UpdateAddress(Transport::PeerAddress::UDP(nodeData.mAddress, nodeData.mPort, nodeData.mInterfaceId)); - mController->PersistDevice(device); - -exit: - if (mDelegate != nullptr) - { - mDelegate->OnAddressUpdateComplete(nodeId, error); - } -} - -void DeviceAddressUpdater::OnNodeIdResolutionFailed(NodeId nodeId, CHIP_ERROR error) -{ - if (mDelegate != nullptr) - { - mDelegate->OnAddressUpdateComplete(nodeId, error); - } -} - -} // namespace Controller -} // namespace chip diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index eedd390c098f7d..ff7804afb94dec 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -49,7 +49,6 @@ #include #include #include -#include #include #include #include @@ -80,8 +79,7 @@ chip::NodeId kRemoteDeviceId = chip::kTestDeviceNodeId; extern "C" { CHIP_ERROR pychip_DeviceController_NewDeviceController(chip::Controller::DeviceCommissioner ** outDevCtrl, chip::NodeId localDeviceId); -CHIP_ERROR pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl, - chip::Controller::DeviceAddressUpdater * addressUpdater); +CHIP_ERROR pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl); CHIP_ERROR pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, char * outAddress, uint64_t maxAddressLen, uint16_t * outPort); @@ -103,10 +101,6 @@ CHIP_ERROR pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::DeviceCommissioner * devCtrl, chip::Controller::DevicePairingDelegate_OnPairingCompleteFunct callback); -// Discovery -CHIP_ERROR pychip_DeviceAddressUpdater_New(chip::Controller::DeviceAddressUpdater ** outAddressUpdater, - chip::Controller::DeviceCommissioner * devCtrl); -void pychip_DeviceAddressUpdater_Delete(chip::Controller::DeviceAddressUpdater * addressUpdater); void pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete( chip::Controller::DeviceAddressUpdateDelegate_OnUpdateComplete callback); CHIP_ERROR pychip_Resolver_ResolveNode(uint64_t fabricid, chip::NodeId nodeid); @@ -131,9 +125,8 @@ CHIP_ERROR pychip_DeviceController_NewDeviceController(chip::Controller::DeviceC chip::NodeId localDeviceId) { CHIP_ERROR err = CHIP_NO_ERROR; - ControllerInitParams initParams{ - .storageDelegate = &sStorageDelegate, - }; + ControllerInitParams initParams{ .storageDelegate = &sStorageDelegate, + .mDeviceAddressUpdateDelegate = &sDeviceAddressUpdateDelegate }; *outDevCtrl = new chip::Controller::DeviceCommissioner(); VerifyOrExit(*outDevCtrl != NULL, err = CHIP_ERROR_NO_MEMORY); @@ -164,8 +157,7 @@ CHIP_ERROR pychip_BLEMgrImpl_ConfigureBle(uint32_t bluetoothAdapterId) return CHIP_NO_ERROR; } -CHIP_ERROR pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl, - chip::Controller::DeviceAddressUpdater * addressUpdater) +CHIP_ERROR pychip_DeviceController_DeleteDeviceController(chip::Controller::DeviceCommissioner * devCtrl) { if (devCtrl != NULL) { @@ -285,28 +277,6 @@ pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback(chip::Controller::Devi return CHIP_NO_ERROR; } -CHIP_ERROR pychip_DeviceAddressUpdater_New(chip::Controller::DeviceAddressUpdater ** outAddressUpdater, - chip::Controller::DeviceCommissioner * devCtrl) -{ - auto addressUpdater = std::make_unique(); - - VerifyOrReturnError(addressUpdater.get() != nullptr, CHIP_ERROR_NO_MEMORY); - ReturnErrorOnFailure(addressUpdater->Init(devCtrl, &sDeviceAddressUpdateDelegate)); - ReturnErrorOnFailure(Mdns::Resolver::Instance().SetResolverDelegate(addressUpdater.get())); - - *outAddressUpdater = addressUpdater.release(); - return CHIP_NO_ERROR; -} - -void pychip_DeviceAddressUpdater_Delete(chip::Controller::DeviceAddressUpdater * addressUpdater) -{ - if (addressUpdater != nullptr) - { - Mdns::Resolver::Instance().SetResolverDelegate(nullptr); - delete addressUpdater; - } -} - void pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete( chip::Controller::DeviceAddressUpdateDelegate_OnUpdateComplete callback) { diff --git a/src/controller/python/ChipDeviceController-ScriptDeviceAddressUpdateDelegate.h b/src/controller/python/ChipDeviceController-ScriptDeviceAddressUpdateDelegate.h index cf6c9ffa6a1495..1a70bb2f93f2cc 100644 --- a/src/controller/python/ChipDeviceController-ScriptDeviceAddressUpdateDelegate.h +++ b/src/controller/python/ChipDeviceController-ScriptDeviceAddressUpdateDelegate.h @@ -17,7 +17,7 @@ #pragma once -#include +#include namespace chip { namespace Controller { diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py index a211b604630e07..08ec1986fc7d71 100644 --- a/src/controller/python/chip/ChipDeviceCtrl.py +++ b/src/controller/python/chip/ChipDeviceCtrl.py @@ -74,17 +74,11 @@ def __init__(self, startNetworkThread=True, controllerNodeId=0, bluetoothAdapter self._InitLib() devCtrl = c_void_p(None) - addressUpdater = c_void_p(None) res = self._dmLib.pychip_DeviceController_NewDeviceController(pointer(devCtrl), controllerNodeId) if res != 0: raise self._ChipStack.ErrorToException(res) - res = self._dmLib.pychip_DeviceAddressUpdater_New(pointer(addressUpdater), devCtrl) - if res != 0: - raise self._ChipStack.ErrorToException(res) - self.devCtrl = devCtrl - self.addressUpdater = addressUpdater self._ChipStack.devCtrl = devCtrl self._Cluster = ChipClusters(self._ChipStack) @@ -121,7 +115,6 @@ def HandleAddressUpdateComplete(nodeid, err): def __del__(self): if self.devCtrl != None: - self._dmLib.pychip_DeviceAddressUpdater_Delete(self.addressUpdater) self._dmLib.pychip_DeviceController_DeleteDeviceController(self.devCtrl) self.devCtrl = None @@ -252,12 +245,6 @@ def _InitLib(self): self._dmLib.pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback.argtypes = [c_void_p, _DevicePairingDelegate_OnPairingCompleteFunct] self._dmLib.pychip_ScriptDevicePairingDelegate_SetKeyExchangeCallback.restype = c_uint32 - self._dmLib.pychip_DeviceAddressUpdater_New.argtypes = [POINTER(c_void_p), c_void_p] - self._dmLib.pychip_DeviceAddressUpdater_New.restype = c_uint32 - - self._dmLib.pychip_DeviceAddressUpdater_Delete.argtypes = [c_void_p] - self._dmLib.pychip_DeviceAddressUpdater_Delete.restype = None - self._dmLib.pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete.argtypes = [_DeviceAddressUpdateDelegate_OnUpdateComplete] self._dmLib.pychip_ScriptDeviceAddressUpdateDelegate_SetOnAddressUpdateComplete.restype = None