Skip to content

Commit

Permalink
[Python] CommissionOnNetwork blocked if DUT does not have an open com…
Browse files Browse the repository at this point in the history
…missoning window (#27646)

* [Python] CommissionOnNetwork blocked if DUT does not have an open commissioning window

* optimize interfaces

* optimize
  • Loading branch information
tianfeng-yang authored and pull[bot] committed Feb 1, 2024
1 parent cadfcfb commit 1919077
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<Dnssd::DiscoveryFilterType>(filterType));
switch (static_cast<Dnssd::DiscoveryFilterType>(filterType))
Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,44 @@
#include "ChipDeviceController-ScriptDevicePairingDelegate.h"

#include <controller/CHIPDeviceController.h>
#include <platform/CHIPDeviceLayer.h>
#include <system/SystemClock.h>

namespace chip {
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<ScriptPairingDeviceDiscoveryDelegate *>(context);

// Stop Mdns discovery.
self->mActiveDeviceCommissioner->RegisterDeviceDiscoveryDelegate(nullptr);

if (self->mPairingDelegate != nullptr)
self->mPairingDelegate->OnPairingComplete(CHIP_ERROR_TIMEOUT);
}

ScriptDevicePairingDelegate * mPairingDelegate;
DeviceCommissioner * mActiveDeviceCommissioner = nullptr;

Expand Down
7 changes: 4 additions & 3 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1919077

Please sign in to comment.