From b2a2516c95a3994755fc56e29d1b966a88a524c7 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Mon, 21 Nov 2022 16:10:18 +0100 Subject: [PATCH] Do not start the PASE Session establishement before device scan succeeds (#23638) --- src/controller/CHIPDeviceController.cpp | 40 ++++++++++++++++++++++++- src/controller/CHIPDeviceController.h | 6 ++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 0e6c0d544d3ee9..9c20c7728cac52 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -703,9 +703,15 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re } else if (params.HasDiscriminator()) { + // The RendezvousParameters argument needs to be recovered if the search succeed, so save them + // for later. + mRendezvousParametersForDeviceDiscoveredOverBle = params; + SetupDiscriminator discriminator; discriminator.SetLongValue(params.GetDiscriminator()); - SuccessOrExit(err = mSystemState->BleLayer()->NewBleConnectionByDiscriminator(discriminator)); + SuccessOrExit(err = mSystemState->BleLayer()->NewBleConnectionByDiscriminator( + discriminator, this, OnDiscoveredDeviceOverBleSuccess, OnDiscoveredDeviceOverBleError)); + ExitNow(CHIP_NO_ERROR); } else { @@ -740,6 +746,38 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re return err; } +#if CONFIG_NETWORK_LAYER_BLE +void DeviceCommissioner::OnDiscoveredDeviceOverBleSuccess(void * appState, BLE_CONNECTION_OBJECT connObj) +{ + auto self = static_cast(appState); + auto device = self->mDeviceInPASEEstablishment; + + if (nullptr != device && device->GetDeviceTransportType() == Transport::Type::kBle) + { + auto remoteId = device->GetDeviceId(); + + auto params = self->mRendezvousParametersForDeviceDiscoveredOverBle; + params.SetConnectionObject(connObj); + self->mRendezvousParametersForDeviceDiscoveredOverBle = RendezvousParameters(); + + self->ReleaseCommissioneeDevice(device); + LogErrorOnFailure(self->EstablishPASEConnection(remoteId, params)); + } +} + +void DeviceCommissioner::OnDiscoveredDeviceOverBleError(void * appState, CHIP_ERROR err) +{ + auto self = static_cast(appState); + auto device = self->mDeviceInPASEEstablishment; + + if (nullptr != device && device->GetDeviceTransportType() == Transport::Type::kBle) + { + self->ReleaseCommissioneeDevice(device); + self->mRendezvousParametersForDeviceDiscoveredOverBle = RendezvousParameters(); + } +} +#endif // CONFIG_NETWORK_LAYER_BLE + CHIP_ERROR DeviceCommissioner::Commission(NodeId remoteDeviceId, CommissioningParameters & params) { if (mDefaultCommissioner == nullptr) diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index aa5e2936216604..f5494802134c16 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -688,6 +688,12 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, uint16_t mUdcListenPort = CHIP_UDC_PORT; #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY +#if CONFIG_NETWORK_LAYER_BLE + static void OnDiscoveredDeviceOverBleSuccess(void * appState, BLE_CONNECTION_OBJECT connObj); + static void OnDiscoveredDeviceOverBleError(void * appState, CHIP_ERROR err); + RendezvousParameters mRendezvousParametersForDeviceDiscoveredOverBle; +#endif + CHIP_ERROR LoadKeyId(PersistentStorageDelegate * delegate, uint16_t & out); /* This function sends a Device Attestation Certificate chain request to the device.