diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 35c616ac0426db..9d59a237ae1fe7 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -118,14 +118,24 @@ 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.fabricIndex.HasValue()) + { + VerifyOrReturnError(params.systemState->Fabrics()->FabricCount() > 0, CHIP_ERROR_INVALID_ARGUMENT); + if (params.systemState->Fabrics()->FindFabricWithIndex(params.fabricIndex.Value()) != nullptr) + { + mFabricIndex = params.fabricIndex.Value(); + } + else + { + ChipLogError(Controller, "There is no fabric corresponding to the given fabricIndex"); + return CHIP_ERROR_INVALID_ARGUMENT; + } + } mSystemState = params.systemState->Retain(); mState = State::Initialized; @@ -143,9 +153,8 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params) return CHIP_NO_ERROR; } -CHIP_ERROR DeviceController::InitControllerNOCChain(const ControllerInitParams & params) +CHIP_ERROR DeviceController::InitControllerNOCChain(const ControllerInitParams & params, bool UpdateNOCOnly) { - FabricInfo newFabric; constexpr uint32_t chipCertAllocatedLen = kMaxCHIPCertLength; chip::Platform::ScopedMemoryBuffer rcacBuf; chip::Platform::ScopedMemoryBuffer icacBuf; @@ -195,8 +204,18 @@ CHIP_ERROR DeviceController::InitControllerNOCChain(const ControllerInitParams & ReturnErrorOnFailure(ConvertX509CertToChipCert(params.controllerNOC, nocSpan)); ReturnErrorOnFailure(ExtractNodeIdFabricIdFromOpCert(nocSpan, &nodeId, &fabricId)); - - auto * fabricTable = params.systemState->Fabrics(); + FabricTable * fabricTable = nullptr; + if (!UpdateNOCOnly) + { + fabricTable = params.systemState->Fabrics(); + } + else + { + VerifyOrReturnError(mFabricIndex != kUndefinedFabricIndex, CHIP_ERROR_INTERNAL); + VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INTERNAL); + fabricTable = mSystemState->Fabrics(); + VerifyOrReturnError(fabricTable != nullptr, CHIP_ERROR_INTERNAL); + } const FabricInfo * fabricInfo = nullptr; // @@ -224,6 +243,11 @@ CHIP_ERROR DeviceController::InitControllerNOCChain(const ControllerInitParams & FabricIndex fabricIndex = fabricFoundInTable ? fabricInfo->GetFabricIndex() : kUndefinedFabricIndex; + if (UpdateNOCOnly) + { + VerifyOrReturnError(fabricIndex == mFabricIndex, CHIP_ERROR_INTERNAL); + } + CHIP_ERROR err = CHIP_NO_ERROR; auto advertiseOperational = @@ -404,6 +428,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 ee4ac09ac84ebe..28177c87c9da46 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -136,6 +136,13 @@ struct ControllerInitParams */ bool removeFromFabricTableOnShutdown = true; + /** + * Specifies whether to utilize the fabric table entry for the given FabricIndex + * for initialization. If provided and neither the operational key pair nor the NOC + * chain are provided, then attempt to locate a fabric corresponding to the given FabricIndex. + */ + chip::Optional fabricIndex; + chip::VendorId controllerVendorId; }; @@ -349,7 +356,7 @@ class DLL_EXPORT DeviceController : public AbstractDnssdDiscoveryController * It can be used for fine-grained dependency injection of a controller's * NOC and operational keypair. */ - CHIP_ERROR InitControllerNOCChain(const ControllerInitParams & params); + CHIP_ERROR InitControllerNOCChain(const ControllerInitParams & params, bool UpdateNOCOnly = false); protected: enum class State diff --git a/src/controller/CHIPDeviceControllerFactory.cpp b/src/controller/CHIPDeviceControllerFactory.cpp index 25bcd12b190a09..115d865e0ac0cb 100644 --- a/src/controller/CHIPDeviceControllerFactory.cpp +++ b/src/controller/CHIPDeviceControllerFactory.cpp @@ -310,6 +310,10 @@ void DeviceControllerFactory::PopulateInitParams(ControllerInitParams & controll controllerParams.controllerVendorId = params.controllerVendorId; controllerParams.enableServerInteractions = params.enableServerInteractions; + if (params.fabricIndex.HasValue()) + { + controllerParams.fabricIndex.SetValue(params.fabricIndex.Value()); + } } void DeviceControllerFactory::ControllerInitialized(const DeviceController & controller) diff --git a/src/controller/CHIPDeviceControllerFactory.h b/src/controller/CHIPDeviceControllerFactory.h index 6b4aa77fdc00c8..94585d4fbf877f 100644 --- a/src/controller/CHIPDeviceControllerFactory.h +++ b/src/controller/CHIPDeviceControllerFactory.h @@ -102,6 +102,13 @@ struct SetupParams */ bool removeFromFabricTableOnShutdown = true; + /** + * Specifies whether to utilize the fabric table entry for the given FabricIndex + * for initialization. If provided and neither the operational key pair nor the NOC + * chain are provided, then attempt to locate a fabric corresponding to the given FabricIndex. + */ + chip::Optional fabricIndex; + Credentials::DeviceAttestationVerifier * deviceAttestationVerifier = nullptr; CommissioningDelegate * defaultCommissioner = nullptr; };