Skip to content

Commit

Permalink
Update src/controller/SetUpCodePairer to timeout after a certain amou…
Browse files Browse the repository at this point in the history
…nt of time
  • Loading branch information
vivien-apple committed Mar 31, 2022
1 parent 428008c commit 6001bb4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/app/tests/suites/TestMultiAdmin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ tests:
- name: "nodeId"
value: nodeId

- label: "Commission from alpha when the commissioning window is not opened"
identity: "alpha"
cluster: "CommissionerCommands"
command: "PairWithQRCode"
arguments:
values:
- name: "nodeId"
value: nodeIdForDuplicateCommissioning
- name: "payload"
value: payload
response:
error: FAILURE

- label: "Open Commissioning Window from alpha"
cluster: "AdministratorCommissioning"
command: "OpenBasicCommissioningWindow"
Expand Down
2 changes: 2 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,11 @@ CHIP_ERROR DeviceCommissioner::Init(CommissionerInitParams params)
mUdcServer->SetInstanceNameResolver(this);
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY

mSetUpCodePairer.SetSystemLayer(mSystemState->SystemLayer());
#if CONFIG_NETWORK_LAYER_BLE
mSetUpCodePairer.SetBleLayer(mSystemState->BleLayer());
#endif // CONFIG_NETWORK_LAYER_BLE

return CHIP_NO_ERROR;
}

Expand Down
21 changes: 20 additions & 1 deletion src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
#include <controller/CHIPDeviceController.h>
#include <lib/dnssd/Resolver.h>
#include <lib/support/CodeUtils.h>
#include <system/SystemClock.h>

constexpr uint32_t kDeviceDiscoveredTimeout = 30 * chip::kMillisecondsPerSecond;

namespace chip {
namespace Controller {

CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode, SetupCodePairerBehaviour commission)
{
VerifyOrReturnError(mSystemLayer != nullptr, CHIP_ERROR_INCORRECT_STATE);

SetupPayload payload;
mConnectionType = commission;

Expand All @@ -45,7 +50,10 @@ CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode,
mRemoteId = remoteId;
mSetUpPINCode = payload.setUpPINCode;

return Connect(payload);
ReturnErrorOnFailure(Connect(payload));

return mSystemLayer->StartTimer(System::Clock::Milliseconds32(kDeviceDiscoveredTimeout), OnDeviceDiscoveredTimeoutCallback,
this);
}

CHIP_ERROR SetUpCodePairer::Connect(SetupPayload & payload)
Expand Down Expand Up @@ -135,6 +143,8 @@ CHIP_ERROR SetUpCodePairer::StopConnectOverSoftAP()

void SetUpCodePairer::OnDeviceDiscovered(RendezvousParameters & params)
{
mSystemLayer->CancelTimer(OnDeviceDiscoveredTimeoutCallback, this);

if (mConnectionType == SetupCodePairerBehaviour::kCommission)
{
LogErrorOnFailure(mCommissioner->PairDevice(mRemoteId, params.SetSetupPINCode(mSetUpPINCode)));
Expand Down Expand Up @@ -202,5 +212,14 @@ void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::Discover
OnDeviceDiscovered(params);
}

void SetUpCodePairer::OnDeviceDiscoveredTimeoutCallback(System::Layer * layer, void * context)
{
auto * pairer = static_cast<SetUpCodePairer *>(context);
LogErrorOnFailure(pairer->StopConnectOverBle());
LogErrorOnFailure(pairer->StopConnectOverIP());
LogErrorOnFailure(pairer->StopConnectOverSoftAP());
pairer->mCommissioner->OnSessionEstablishmentError(CHIP_ERROR_TIMEOUT);
}

} // namespace Controller
} // namespace chip
5 changes: 5 additions & 0 deletions src/controller/SetUpCodePairer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class DLL_EXPORT SetUpCodePairer
// Called by the DeviceCommissioner to notify that we have discovered a new device.
void NotifyCommissionableDeviceDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData);

void SetSystemLayer(System::Layer * systemLayer) { mSystemLayer = systemLayer; };

#if CONFIG_NETWORK_LAYER_BLE
void SetBleLayer(Ble::BleLayer * bleLayer) { mBleLayer = bleLayer; };
#endif // CONFIG_NETWORK_LAYER_BLE
Expand All @@ -77,6 +79,8 @@ class DLL_EXPORT SetUpCodePairer

void OnDeviceDiscovered(RendezvousParameters & params);

static void OnDeviceDiscoveredTimeoutCallback(System::Layer * layer, void * context);

#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * mBleLayer = nullptr;
void OnDiscoveredDeviceOverBle(BLE_CONNECTION_OBJECT connObj);
Expand All @@ -89,6 +93,7 @@ class DLL_EXPORT SetUpCodePairer
Dnssd::DiscoveryFilter currentFilter;

DeviceCommissioner * mCommissioner = nullptr;
System::Layer * mSystemLayer = nullptr;
chip::NodeId mRemoteId;
uint32_t mSetUpPINCode = 0;
SetupCodePairerBehaviour mConnectionType = SetupCodePairerBehaviour::kCommission;
Expand Down

0 comments on commit 6001bb4

Please sign in to comment.