Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop the SetupCodePairer when StopPairing is called #28881

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
}

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();
}
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved

ResetDiscoveryState();
mRemoteId = kUndefinedNodeId;
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
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);
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved

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