From 52e8c2f0f77acba53d7db600f07f4622095fffb6 Mon Sep 17 00:00:00 2001 From: chendejin Date: Sun, 18 Feb 2024 15:41:02 +0800 Subject: [PATCH] Setup controller by storage fabric, without providing NOC chain --- src/controller/CHIPDeviceController.cpp | 17 +++++++++++--- src/controller/CHIPDeviceController.h | 24 ++++++++++++++++++++ src/controller/CHIPDeviceControllerFactory.h | 14 ++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 57c75cd0d813ae..c88a9dfa3e7ae4 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -118,14 +118,23 @@ 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(); + printf("useStorageFabric: %d\n", (int) mFabricIndex); + } + } mSystemState = params.systemState->Retain(); mState = State::Initialized; @@ -408,6 +417,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..932a3d94d1a81d 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,16 @@ 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..abf66996a980f7 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; };