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 Apr 5, 2022
1 parent 637d5bf commit 9ece421
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 @@ -463,9 +463,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 @@ -47,7 +52,10 @@ CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode,

ResetDiscoveryState();

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 @@ -162,6 +170,8 @@ CHIP_ERROR SetUpCodePairer::StopConnectOverSoftAP()

bool SetUpCodePairer::ConnectToDiscoveredDevice()
{
mSystemLayer->CancelTimer(OnDeviceDiscoveredTimeoutCallback, this);

if (mWaitingForPASE)
{
// Nothing to do. Just wait until we either succeed or fail at that
Expand Down Expand Up @@ -407,5 +417,14 @@ void SetUpCodePairer::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR error)
}
}

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 @@ -64,6 +64,8 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
// 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 Down Expand Up @@ -120,6 +122,8 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
kTransportTypeCount,
};

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 @@ -133,6 +137,7 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
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 9ece421

Please sign in to comment.