From 12230381b4189633ac6b684546110f1ea8e62a45 Mon Sep 17 00:00:00 2001 From: tianfeng-yang <130436698+tianfeng-yang@users.noreply.github.com> Date: Wed, 19 Jul 2023 10:16:33 +0800 Subject: [PATCH] [Python] CommissionOnNetwork blocked if DUT does not have an open commissoning window (#27646) * [Python] CommissionOnNetwork blocked if DUT does not have an open commissioning window * optimize interfaces * optimize --- .../ChipDeviceController-ScriptBinding.cpp | 12 ++++++---- ...r-ScriptPairingDeviceDiscoveryDelegate.cpp | 3 +++ ...ler-ScriptPairingDeviceDiscoveryDelegate.h | 23 +++++++++++++++++-- src/controller/python/chip/ChipDeviceCtrl.py | 7 +++--- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index f58ee12e592216..65c8e086fae385 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -165,7 +165,8 @@ PyChipError pychip_DeviceController_DiscoverCommissionableNodesDeviceType(chip:: PyChipError pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Controller::DeviceCommissioner * devCtrl); PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, - uint32_t setupPasscode, const uint8_t filterType, const char * filterParam); + uint32_t setupPasscode, const uint8_t filterType, const char * filterParam, + uint32_t discoveryTimeoutMsec); PyChipError pychip_DeviceController_PostTaskOnChipThread(ChipThreadTaskRunnerFunct callback, void * pythonContext); @@ -440,7 +441,8 @@ PyChipError pychip_DeviceController_UnpairDevice(chip::Controller::DeviceCommiss } PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::DeviceCommissioner * devCtrl, uint64_t nodeId, - uint32_t setupPasscode, const uint8_t filterType, const char * filterParam) + uint32_t setupPasscode, const uint8_t filterType, const char * filterParam, + uint32_t discoveryTimeoutMsec) { Dnssd::DiscoveryFilter filter(static_cast(filterType)); switch (static_cast(filterType)) @@ -475,8 +477,10 @@ PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::Device return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT); } - sPairingDeviceDiscoveryDelegate.Init(nodeId, setupPasscode, sCommissioningParameters, &sPairingDelegate, devCtrl); - devCtrl->RegisterDeviceDiscoveryDelegate(&sPairingDeviceDiscoveryDelegate); + sPairingDelegate.SetExpectingPairingComplete(true); + CHIP_ERROR err = sPairingDeviceDiscoveryDelegate.Init(nodeId, setupPasscode, sCommissioningParameters, &sPairingDelegate, + devCtrl, discoveryTimeoutMsec); + VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err)); return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter)); } diff --git a/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.cpp b/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.cpp index 1752232b786167..850ec208ac3e4b 100644 --- a/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.cpp +++ b/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.cpp @@ -32,6 +32,9 @@ void ScriptPairingDeviceDiscoveryDelegate::OnDiscoveredDevice(const Dnssd::Disco nodeData.resolutionData.ipAddress[0].ToString(buf); ChipLogProgress(chipTool, "Discovered Device: %s:%u", buf, port); + // Cancel discovery timer. + chip::DeviceLayer::SystemLayer().CancelTimer(OnDiscoveredTimeout, this); + // Stop Mdns discovery. mActiveDeviceCommissioner->RegisterDeviceDiscoveryDelegate(nullptr); diff --git a/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.h b/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.h index d829f261aa8701..7745bb13de4c15 100644 --- a/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.h +++ b/src/controller/python/ChipDeviceController-ScriptPairingDeviceDiscoveryDelegate.h @@ -21,6 +21,8 @@ #include "ChipDeviceController-ScriptDevicePairingDelegate.h" #include +#include +#include namespace chip { namespace Controller { @@ -28,18 +30,35 @@ namespace Controller { class ScriptPairingDeviceDiscoveryDelegate : public DeviceDiscoveryDelegate { public: - void Init(NodeId nodeId, uint32_t setupPasscode, CommissioningParameters commissioningParams, - ScriptDevicePairingDelegate * pairingDelegate, DeviceCommissioner * activeDeviceCommissioner) + CHIP_ERROR Init(NodeId nodeId, uint32_t setupPasscode, CommissioningParameters commissioningParams, + ScriptDevicePairingDelegate * pairingDelegate, DeviceCommissioner * activeDeviceCommissioner, + uint32_t discoveryTimeoutMsec) { mNodeId = nodeId; mSetupPasscode = setupPasscode; mParams = commissioningParams; mPairingDelegate = pairingDelegate; mActiveDeviceCommissioner = activeDeviceCommissioner; + VerifyOrReturnError(mActiveDeviceCommissioner != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + mActiveDeviceCommissioner->RegisterDeviceDiscoveryDelegate(this); + return chip::DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds32(discoveryTimeoutMsec), OnDiscoveredTimeout, + this); } void OnDiscoveredDevice(const Dnssd::DiscoveredNodeData & nodeData); private: + static void OnDiscoveredTimeout(System::Layer * layer, void * context) + { + ChipLogError(Controller, "Mdns discovery timed out"); + auto * self = static_cast(context); + + // Stop Mdns discovery. + self->mActiveDeviceCommissioner->RegisterDeviceDiscoveryDelegate(nullptr); + + if (self->mPairingDelegate != nullptr) + self->mPairingDelegate->OnPairingComplete(CHIP_ERROR_TIMEOUT); + } + ScriptDevicePairingDelegate * mPairingDelegate; DeviceCommissioner * mActiveDeviceCommissioner = nullptr; diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py index e162df18d9fb07..5062102d3073e1 100644 --- a/src/controller/python/chip/ChipDeviceCtrl.py +++ b/src/controller/python/chip/ChipDeviceCtrl.py @@ -1355,7 +1355,8 @@ def _InitLib(self): c_void_p, c_uint64] self._dmLib.pychip_DeviceController_Commission.restype = PyChipError - self._dmLib.pychip_DeviceController_OnNetworkCommission.argtypes = [c_void_p, c_uint64, c_uint32, c_uint8, c_char_p] + self._dmLib.pychip_DeviceController_OnNetworkCommission.argtypes = [ + c_void_p, c_uint64, c_uint32, c_uint8, c_char_p, c_uint32] self._dmLib.pychip_DeviceController_OnNetworkCommission.restype = PyChipError self._dmLib.pychip_DeviceController_DiscoverCommissionableNodes.argtypes = [ @@ -1662,7 +1663,7 @@ def SetTrustedTimeSource(self, nodeId: int, endpoint: int): ).raise_on_error() def CommissionOnNetwork(self, nodeId: int, setupPinCode: int, - filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None) -> PyChipError: + filterType: DiscoveryFilterType = DiscoveryFilterType.NONE, filter: typing.Any = None, discoveryTimeoutMsec: int = 30000) -> PyChipError: ''' Does the routine for OnNetworkCommissioning, with a filter for mDNS discovery. Supported filters are: @@ -1693,7 +1694,7 @@ def CommissionOnNetwork(self, nodeId: int, setupPinCode: int, self._ChipStack.CallAsync( lambda: self._dmLib.pychip_DeviceController_OnNetworkCommission( - self.devCtrl, nodeId, setupPinCode, int(filterType), str(filter).encode("utf-8") + b"\x00" if filter is not None else None) + self.devCtrl, nodeId, setupPinCode, int(filterType), str(filter).encode("utf-8") + b"\x00" if filter is not None else None, discoveryTimeoutMsec) ) if not self._ChipStack.commissioningCompleteEvent.isSet(): # Error 50 is a timeout