From cf4cf8a6df6430fca12d564b6fcc2adeaf4df59f Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 25 Aug 2022 19:18:25 +0200 Subject: [PATCH] [SetUpCodePairer] Only search on network if no credentials are provided to chip-tool pairing code commands (#22104) --- src/controller/CHIPDeviceController.cpp | 11 +++++++++- src/controller/SetUpCodePairer.cpp | 27 +++++++++++++++---------- src/controller/SetUpCodePairer.h | 1 + 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index afdc4609fd8581..ec8e126137510b 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -571,7 +571,16 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * se return CHIP_ERROR_INCORRECT_STATE; } ReturnErrorOnFailure(mDefaultCommissioner->SetCommissioningParameters(params)); - return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, SetupCodePairerBehaviour::kCommission); + + auto threadCredentials = params.GetThreadOperationalDataset(); + auto wiFiCredentials = params.GetWiFiCredentials(); + bool hasCredentials = threadCredentials.HasValue() || wiFiCredentials.HasValue(); + + // If there is no network credentials, we assume that the pairing command as been issued to pair with a + // device that is already on the network. + auto pairerBehaviour = hasCredentials ? SetupCodePairerBehaviour::kCommission : SetupCodePairerBehaviour::kCommissionOnNetwork; + + return mSetUpCodePairer.PairDevice(remoteDeviceId, setUpCode, pairerBehaviour); } CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, const char * setUpCode) diff --git a/src/controller/SetUpCodePairer.cpp b/src/controller/SetUpCodePairer.cpp index 3ab62afe8a075e..e261e2d0a78eab 100644 --- a/src/controller/SetUpCodePairer.cpp +++ b/src/controller/SetUpCodePairer.cpp @@ -72,22 +72,26 @@ CHIP_ERROR SetUpCodePairer::Connect(SetupPayload & payload) bool isRunning = false; bool searchOverAll = !payload.rendezvousInformation.HasValue(); - if (searchOverAll || payload.rendezvousInformation.Value().Has(RendezvousInformationFlag::kBLE)) + + if (mConnectionType != SetupCodePairerBehaviour::kCommissionOnNetwork) { - if (CHIP_NO_ERROR == (err = StartDiscoverOverBle(payload))) + if (searchOverAll || payload.rendezvousInformation.Value().Has(RendezvousInformationFlag::kBLE)) { - isRunning = true; + if (CHIP_NO_ERROR == (err = StartDiscoverOverBle(payload))) + { + isRunning = true; + } + VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err || CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE == err, err); } - VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err || CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE == err, err); - } - if (searchOverAll || payload.rendezvousInformation.Value().Has(RendezvousInformationFlag::kSoftAP)) - { - if (CHIP_NO_ERROR == (err = StartDiscoverOverSoftAP(payload))) + if (searchOverAll || payload.rendezvousInformation.Value().Has(RendezvousInformationFlag::kSoftAP)) { - isRunning = true; + if (CHIP_NO_ERROR == (err = StartDiscoverOverSoftAP(payload))) + { + isRunning = true; + } + VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err || CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE == err, err); } - VerifyOrReturnError(searchOverAll || CHIP_NO_ERROR == err || CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE == err, err); } // We always want to search on network because any node that has already been commissioned will use on-network regardless of the @@ -225,7 +229,8 @@ bool SetUpCodePairer::ConnectToDiscoveredDevice() ExpectPASEEstablishment(); CHIP_ERROR err; - if (mConnectionType == SetupCodePairerBehaviour::kCommission) + if (mConnectionType == SetupCodePairerBehaviour::kCommission || + mConnectionType == SetupCodePairerBehaviour::kCommissionOnNetwork) { err = mCommissioner->PairDevice(mRemoteId, params); } diff --git a/src/controller/SetUpCodePairer.h b/src/controller/SetUpCodePairer.h index fdf54dbc10be09..19a6cebc914e74 100644 --- a/src/controller/SetUpCodePairer.h +++ b/src/controller/SetUpCodePairer.h @@ -51,6 +51,7 @@ class DeviceCommissioner; enum class SetupCodePairerBehaviour : uint8_t { kCommission, + kCommissionOnNetwork, kPaseOnly, };