Skip to content

Commit

Permalink
Stop the SetupCodePairer when StopPairing is called (project-chip#28881)
Browse files Browse the repository at this point in the history
Prior to this change the SetupCodePairer was left running, and in
particular its discovery timeout timer was not correctly cancelled.
  • Loading branch information
ksperling-apple authored Aug 28, 2023
1 parent 2d5fe2c commit 1084dd5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
14 changes: 10 additions & 4 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void DeviceCommissioner::Shutdown()

ChipLogDetail(Controller, "Shutting down the commissioner");

mSetUpCodePairer.CommissionerShuttingDown();
mSetUpCodePairer.StopPairing();

// Check to see if pairing in progress before shutting down
CommissioneeDeviceProxy * device = mDeviceInPASEEstablishment;
Expand Down Expand Up @@ -916,12 +916,18 @@ DeviceCommissioner::ContinueCommissioningAfterDeviceAttestation(DeviceProxy * de
CHIP_ERROR DeviceCommissioner::StopPairing(NodeId remoteDeviceId)
{
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(remoteDeviceId != kUndefinedNodeId, CHIP_ERROR_INVALID_ARGUMENT);

bool stopped = mSetUpCodePairer.StopPairing(remoteDeviceId);

CommissioneeDeviceProxy * device = FindCommissioneeDevice(remoteDeviceId);
VerifyOrReturnError(device != nullptr, CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);
if (device != nullptr)
{
ReleaseCommissioneeDevice(device);
stopped = true;
}

ReleaseCommissioneeDevice(device);
return CHIP_NO_ERROR;
return (stopped) ? CHIP_NO_ERROR : CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR;
}

CHIP_ERROR DeviceCommissioner::UnpairDevice(NodeId remoteDeviceId)
Expand Down
35 changes: 20 additions & 15 deletions src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode,
DiscoveryType discoveryType, Optional<Dnssd::CommonResolutionData> resolutionData)
{
VerifyOrReturnError(mSystemLayer != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(remoteId != kUndefinedNodeId, CHIP_ERROR_INVALID_ARGUMENT);

SetupPayload payload;
ReturnErrorOnFailure(GetPayload(setUpCode, payload));
Expand Down Expand Up @@ -405,9 +406,19 @@ void SetUpCodePairer::NotifyCommissionableDeviceDiscovered(const Dnssd::CommonRe
ConnectToDiscoveredDevice();
}

void SetUpCodePairer::CommissionerShuttingDown()
bool SetUpCodePairer::StopPairing(NodeId remoteId)
{
VerifyOrReturnValue(mRemoteId != kUndefinedNodeId, false);
VerifyOrReturnValue(remoteId == kUndefinedNodeId || remoteId == mRemoteId, false);

if (mWaitingForPASE)
{
PASEEstablishmentComplete();
}

ResetDiscoveryState();
mRemoteId = kUndefinedNodeId;
return true;
}

bool SetUpCodePairer::TryNextRendezvousParameters()
Expand Down Expand Up @@ -452,32 +463,26 @@ void SetUpCodePairer::ResetDiscoveryState()
waiting = false;
}

while (!mDiscoveredParameters.empty())
{
mDiscoveredParameters.pop_front();
}

mDiscoveredParameters.clear();
mCurrentPASEParameters.ClearValue();
mLastPASEError = CHIP_NO_ERROR;

mSystemLayer->CancelTimer(OnDeviceDiscoveredTimeoutCallback, this);
}

void SetUpCodePairer::ExpectPASEEstablishment()
{
VerifyOrDie(!mWaitingForPASE);
mWaitingForPASE = true;
auto * delegate = mCommissioner->GetPairingDelegate();
if (this == delegate)
{
// This should really not happen, but if it does, do nothing, to avoid
// delegate loops.
return;
}

VerifyOrDie(delegate != this);
mPairingDelegate = delegate;
mCommissioner->RegisterPairingDelegate(this);
}

void SetUpCodePairer::PASEEstablishmentComplete()
{
VerifyOrDie(mWaitingForPASE);
mWaitingForPASE = false;
mCommissioner->RegisterPairingDelegate(mPairingDelegate);
mPairingDelegate = nullptr;
Expand Down Expand Up @@ -524,9 +529,9 @@ void SetUpCodePairer::OnPairingComplete(CHIP_ERROR error)

if (CHIP_NO_ERROR == error)
{
mSystemLayer->CancelTimer(OnDeviceDiscoveredTimeoutCallback, this);

ChipLogProgress(Controller, "Pairing with commissionee successful, stopping discovery");
ResetDiscoveryState();
mRemoteId = kUndefinedNodeId;
if (pairingDelegate != nullptr)
{
pairingDelegate->OnPairingComplete(error);
Expand Down
14 changes: 7 additions & 7 deletions src/controller/SetUpCodePairer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ enum class DiscoveryType : uint8_t
class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
{
public:
SetUpCodePairer(DeviceCommissioner * commissioner) : mCommissioner(commissioner) { ResetDiscoveryState(); }
SetUpCodePairer(DeviceCommissioner * commissioner) : mCommissioner(commissioner) {}
virtual ~SetUpCodePairer() {}

CHIP_ERROR PairDevice(chip::NodeId remoteId, const char * setUpCode,
Expand All @@ -93,9 +93,9 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
void SetBleLayer(Ble::BleLayer * bleLayer) { mBleLayer = bleLayer; };
#endif // CONFIG_NETWORK_LAYER_BLE

// Called to notify us that the DeviceCommissioner is shutting down and we
// should not try to do any more new work.
void CommissionerShuttingDown();
// Stop ongoing discovery / pairing of the specified node, or of
// whichever node we're pairing if kUndefinedNodeId is passed.
bool StopPairing(NodeId remoteId = kUndefinedNodeId);

private:
// DevicePairingDelegate implementation.
Expand Down Expand Up @@ -177,9 +177,9 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
uint16_t mPayloadVendorID = kNotAvailable;
uint16_t mPayloadProductID = kNotAvailable;

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

0 comments on commit 1084dd5

Please sign in to comment.