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
…missioning window
  • Loading branch information
tianfeng-yang committed Jul 6, 2023
1 parent 79cebcf commit 181eccf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ class DLL_EXPORT DeviceController : public AbstractDnssdDiscoveryController
return nullptr;
}

System::Layer * GetSystemLayer()
{
if (mSystemState)
{
return mSystemState->SystemLayer();
}

return nullptr;
}

CHIP_ERROR GetPeerAddressAndPort(NodeId peerId, Inet::IPAddress & addr, uint16_t & port);

/**
Expand Down
5 changes: 4 additions & 1 deletion src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ PyChipError pychip_DeviceController_OnNetworkCommission(chip::Controller::Device
return ToPyChipError(CHIP_ERROR_INVALID_ARGUMENT);
}

sPairingDeviceDiscoveryDelegate.Init(nodeId, setupPasscode, sCommissioningParameters, &sPairingDelegate, devCtrl);
sPairingDelegate.SetExpectingPairingComplete(true);
CHIP_ERROR err =
sPairingDeviceDiscoveryDelegate.Init(nodeId, setupPasscode, sCommissioningParameters, &sPairingDelegate, devCtrl);
VerifyOrReturnError(err == CHIP_NO_ERROR, ToPyChipError(err));
devCtrl->RegisterDeviceDiscoveryDelegate(&sPairingDeviceDiscoveryDelegate);
return ToPyChipError(devCtrl->DiscoverCommissionableNodes(filter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void ScriptPairingDeviceDiscoveryDelegate::OnDiscoveredDevice(const Dnssd::Disco
nodeData.resolutionData.ipAddress[0].ToString(buf);
ChipLogProgress(chipTool, "Discovered Device: %s:%u", buf, port);

// Cancel discovery timer.
if (mActiveDeviceCommissioner->GetSystemLayer() != nullptr)
mActiveDeviceCommissioner->GetSystemLayer()->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 <system/SystemClock.h>

constexpr uint32_t kDeviceDiscoveredTimeout = CHIP_CONFIG_ON_NETWORK_PAIRER_DISCOVERY_TIMEOUT_SECS * chip::kMillisecondsPerSecond;

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)
{
mNodeId = nodeId;
mSetupPasscode = setupPasscode;
mParams = commissioningParams;
mPairingDelegate = pairingDelegate;
mActiveDeviceCommissioner = activeDeviceCommissioner;
VerifyOrReturnError(mActiveDeviceCommissioner != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(mActiveDeviceCommissioner->GetSystemLayer() != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
return mActiveDeviceCommissioner->GetSystemLayer()->StartTimer(System::Clock::Milliseconds32(kDeviceDiscoveredTimeout),
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
12 changes: 12 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,18 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
#define CHIP_CONFIG_SETUP_CODE_PAIRER_DISCOVERY_TIMEOUT_SECS 30
#endif // CHIP_CONFIG_SETUP_CODE_PAIRER_DISCOVERY_TIMEOUT_SECS

/**
* @def CHIP_CONFIG_ON_NETWORK_PAIRER_DISCOVERY_TIMEOUT_SECS
*
* @brief
* This is the default timeout for the discovery of devices by
* the on network pairer.
*
*/
#ifndef CHIP_CONFIG_ON_NETWORK_PAIRER_DISCOVERY_TIMEOUT_SECS
#define CHIP_CONFIG_ON_NETWORK_PAIRER_DISCOVERY_TIMEOUT_SECS 30
#endif // CHIP_CONFIG_ON_NETWORK_PAIRER_DISCOVERY_TIMEOUT_SECS

/**
* @def CHIP_CONFIG_NUM_CD_KEY_SLOTS
*
Expand Down

0 comments on commit 181eccf

Please sign in to comment.