From 1014776ec13116809bbe1f42691ccd7e0e69f740 Mon Sep 17 00:00:00 2001 From: Karsten Sperling <113487422+ksperling-apple@users.noreply.github.com> Date: Thu, 31 Aug 2023 09:51:32 +1200 Subject: [PATCH] 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 --------- Co-authored-by: Boris Zbarsky --- src/controller/CHIPDeviceController.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index ee2c36d8bd761e..3333a6554f3c59 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -918,16 +918,27 @@ 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); + ChipLogProgress(Controller, "StopPairing called for node ID 0x" ChipLogFormatX64, ChipLogValueX64(remoteDeviceId)); + // If we're still in the process of discovering the device, just stop the SetUpCodePairer + if (mSetUpCodePairer.StopPairing(remoteDeviceId)) + { + return CHIP_NO_ERROR; + } + + // Otherwise we might be pairing and / or commissioning it. CommissioneeDeviceProxy * device = FindCommissioneeDevice(remoteDeviceId); - if (device != nullptr) + VerifyOrReturnError(device != nullptr, CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR); + + if (mDeviceBeingCommissioned == device) + { + CommissioningStageComplete(CHIP_ERROR_CANCELLED); + } + else { ReleaseCommissioneeDevice(device); - stopped = true; } - - return (stopped) ? CHIP_NO_ERROR : CHIP_ERROR_INVALID_DEVICE_DESCRIPTOR; + return CHIP_NO_ERROR; } CHIP_ERROR DeviceCommissioner::UnpairDevice(NodeId remoteDeviceId)