From e1cb7fa6ce67569b95be237c7379f3d199ce9afc Mon Sep 17 00:00:00 2001 From: chendejin Date: Sun, 18 Feb 2024 17:10:53 +0800 Subject: [PATCH] Setup controller by storage fabric, without providing NOC chain --- src/controller/CHIPDeviceController.cpp | 16 +++++++++++--- src/controller/CHIPDeviceController.h | 23 ++++++++++++++++++++ src/controller/CHIPDeviceControllerFactory.h | 14 ++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 57c75cd0d813ae..7a484001c5df8f 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -118,14 +118,22 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params) mDNSResolver.SetCommissioningDelegate(this); RegisterDeviceDiscoveryDelegate(params.deviceDiscoveryDelegate); - VerifyOrReturnError(params.operationalCredentialsDelegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - mOperationalCredentialsDelegate = params.operationalCredentialsDelegate; - mVendorId = params.controllerVendorId; if (params.operationalKeypair != nullptr || !params.controllerNOC.empty() || !params.controllerRCAC.empty()) { ReturnErrorOnFailure(InitControllerNOCChain(params)); } + else if (params.enableUseStorageFabric && params.useStorageFabricIndex != kUndefinedFabricIndex) + { + VerifyOrReturnError(params.systemState->Fabrics()->FabricCount() >= kMinValidFabricIndex, CHIP_ERROR_INVALID_ARGUMENT); + const FabricInfo * fabricInfo = nullptr; + fabricInfo = params.systemState->Fabrics()->FindFabricWithIndex(params.useStorageFabricIndex); + if (fabricInfo != nullptr) + { + // ensure the fabric is existed + mFabricIndex = fabricInfo->GetFabricIndex(); + } + } mSystemState = params.systemState->Retain(); mState = State::Initialized; @@ -408,6 +416,8 @@ DeviceCommissioner::DeviceCommissioner() : CHIP_ERROR DeviceCommissioner::Init(CommissionerInitParams params) { + VerifyOrReturnError(params.operationalCredentialsDelegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + mOperationalCredentialsDelegate = params.operationalCredentialsDelegate; ReturnErrorOnFailure(DeviceController::Init(params)); mPairingDelegate = params.pairingDelegate; diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index a7e6f6dac4bcb9..94f5ab2db3318e 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -141,6 +141,20 @@ struct ControllerInitParams */ bool removeFromFabricTableOnShutdown = true; + /** + * Controls whether use the storage fabric table. If operational key pair + * or NOC chain are not passed, the fabric index are not available even if + * there is a fabric in storage. This provides a way to SetupController when + * the device has NOC installed and reboots, by providing a storage fabric + * index instead of operational key pair and NOC chain. + */ + bool enableUseStorageFabric = false; + + /** + * Use a storage fabric index to find the fabric table. + */ + FabricIndex useStorageFabricIndex = kUndefinedFabricIndex; + chip::VendorId controllerVendorId; }; @@ -356,6 +370,15 @@ class DLL_EXPORT DeviceController : public AbstractDnssdDiscoveryController */ CHIP_ERROR InitControllerNOCChain(const ControllerInitParams & params); + /** + * @brief Update the operational credentials after controller Init. + */ + CHIP_ERROR UpdateControllerNOCChain(ControllerInitParams params) + { + params.systemState = mSystemState; + return InitControllerNOCChain(params); + } + protected: enum class State { diff --git a/src/controller/CHIPDeviceControllerFactory.h b/src/controller/CHIPDeviceControllerFactory.h index 6b4aa77fdc00c8..d3185a67e31e34 100644 --- a/src/controller/CHIPDeviceControllerFactory.h +++ b/src/controller/CHIPDeviceControllerFactory.h @@ -102,6 +102,20 @@ struct SetupParams */ bool removeFromFabricTableOnShutdown = true; + /** + * Controls whether use the storage fabric table. If operational key pair + * or NOC chain are not passed, the fabric index are not available even if + * there is a fabric in storage. This provides a way to SetupController when + * the device has NOC installed and reboots, by providing a storage fabric + * index instead of operational key pair and NOC chain. + */ + bool enableUseStorageFabric = false; + + /** + * Use a storage fabric index to find the fabric table. + */ + FabricIndex useStorageFabricIndex = kUndefinedFabricIndex; + Credentials::DeviceAttestationVerifier * deviceAttestationVerifier = nullptr; CommissioningDelegate * defaultCommissioner = nullptr; };