Handle StopPairing more cleanly in DeviceController (#28939)
* Handle StopPairing more cleanly in DeviceController
This ensures the controller is not left in a state where further
commissioning operations after a StopPairing call are rejected because
pairing is already "in progress".
* Comment typo
Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
---------
Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp
index ee2c36d..3333a65 100644
--- a/src/controller/CHIPDeviceController.cpp
+++ b/src/controller/CHIPDeviceController.cpp
@@ -918,16 +918,27 @@
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(remoteDeviceId != kUndefinedNodeId, CHIP_ERROR_INVALID_ARGUMENT);
- bool stopped = mSetUpCodePairer.StopPairing(remoteDeviceId);
+ ChipLogProgress(Controller, "StopPairing called for node ID 0x" ChipLogFormatX64, ChipLogValueX64(remoteDeviceId));
- CommissioneeDeviceProxy * device = FindCommissioneeDevice(remoteDeviceId);
- if (device != nullptr)
+ // If we're still in the process of discovering the device, just stop the SetUpCodePairer
+ if (mSetUpCodePairer.StopPairing(remoteDeviceId))
{
- ReleaseCommissioneeDevice(device);
- stopped = true;
+ return CHIP_NO_ERROR;
}
- return (stopped) ? CHIP_NO_ERROR : CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR;
+ // Otherwise we might be pairing and / or commissioning it.
+ CommissioneeDeviceProxy * device = FindCommissioneeDevice(remoteDeviceId);
+ VerifyOrReturnError(device != nullptr, CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR);
+
+ if (mDeviceBeingCommissioned == device)
+ {
+ CommissioningStageComplete(CHIP_ERROR_CANCELLED);
+ }
+ else
+ {
+ ReleaseCommissioneeDevice(device);
+ }
+ return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceCommissioner::UnpairDevice(NodeId remoteDeviceId)