Skip to content

Commit

Permalink
DeviceControllerFactory: Add InitAndRetainSystemState (#32998)
Browse files Browse the repository at this point in the history
* DeviceControllerFactory: Add InitAndRetainSystemState

This does a RetainSystemState, but re-initializes if necessary.

Also rename no-arg InitSystemState() to ReinitSystemStateIfNecessary() and
check for the case where no work is necessary before creating the
FactoryInitParams and calling InitSystemState(params).

This means InitSystemState(params) doesn't have to handle that case anymore
since it's only called from Init() or after ReinitSystemStateIfNecessary() has
already done the check.

* Rename to EnsureAndRetainSystemState
  • Loading branch information
ksperling-apple authored Apr 17, 2024
1 parent 01facfd commit c6fbf7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
56 changes: 27 additions & 29 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,36 @@ CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)
return err;
}

CHIP_ERROR DeviceControllerFactory::InitSystemState()
CHIP_ERROR DeviceControllerFactory::ReinitSystemStateIfNecessary()
{
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mSystemState->IsShutdown(), CHIP_NO_ERROR);

FactoryInitParams params;
if (mSystemState != nullptr)
{
params.systemLayer = mSystemState->SystemLayer();
params.udpEndPointManager = mSystemState->UDPEndPointManager();
params.systemLayer = mSystemState->SystemLayer();
params.udpEndPointManager = mSystemState->UDPEndPointManager();
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
params.tcpEndPointManager = mSystemState->TCPEndPointManager();
params.tcpEndPointManager = mSystemState->TCPEndPointManager();
#endif
#if CONFIG_NETWORK_LAYER_BLE
params.bleLayer = mSystemState->BleLayer();
params.bleLayer = mSystemState->BleLayer();
#endif
params.listenPort = mListenPort;
params.fabricIndependentStorage = mFabricIndependentStorage;
params.enableServerInteractions = mEnableServerInteractions;
params.groupDataProvider = mSystemState->GetGroupDataProvider();
params.sessionKeystore = mSystemState->GetSessionKeystore();
params.fabricTable = mSystemState->Fabrics();
params.operationalKeystore = mOperationalKeystore;
params.opCertStore = mOpCertStore;
params.certificateValidityPolicy = mCertificateValidityPolicy;
params.sessionResumptionStorage = mSessionResumptionStorage;
}
params.listenPort = mListenPort;
params.fabricIndependentStorage = mFabricIndependentStorage;
params.enableServerInteractions = mEnableServerInteractions;
params.groupDataProvider = mSystemState->GetGroupDataProvider();
params.sessionKeystore = mSystemState->GetSessionKeystore();
params.fabricTable = mSystemState->Fabrics();
params.operationalKeystore = mOperationalKeystore;
params.opCertStore = mOpCertStore;
params.certificateValidityPolicy = mCertificateValidityPolicy;
params.sessionResumptionStorage = mSessionResumptionStorage;

return InitSystemState(params);
}

CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
{
if (mSystemState != nullptr && mSystemState->IsInitialized())
{
return CHIP_NO_ERROR;
}

if (mSystemState != nullptr)
{
Platform::Delete(mSystemState);
Expand Down Expand Up @@ -331,10 +326,8 @@ void DeviceControllerFactory::ControllerInitialized(const DeviceController & con

CHIP_ERROR DeviceControllerFactory::SetupController(SetupParams params, DeviceController & controller)
{
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(params.controllerVendorId != VendorId::Unspecified, CHIP_ERROR_INVALID_ARGUMENT);

ReturnErrorOnFailure(InitSystemState());
ReturnErrorOnFailure(ReinitSystemStateIfNecessary());

ControllerInitParams controllerParams;
PopulateInitParams(controllerParams, params);
Expand All @@ -351,10 +344,8 @@ CHIP_ERROR DeviceControllerFactory::SetupController(SetupParams params, DeviceCo

CHIP_ERROR DeviceControllerFactory::SetupCommissioner(SetupParams params, DeviceCommissioner & commissioner)
{
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(params.controllerVendorId != VendorId::Unspecified, CHIP_ERROR_INVALID_ARGUMENT);

ReturnErrorOnFailure(InitSystemState());
ReturnErrorOnFailure(ReinitSystemStateIfNecessary());

CommissionerInitParams commissionerParams;

Expand Down Expand Up @@ -397,6 +388,13 @@ bool DeviceControllerFactory::ReleaseSystemState()
return mSystemState->Release();
}

CHIP_ERROR DeviceControllerFactory::EnsureAndRetainSystemState()
{
ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
RetainSystemState();
return CHIP_NO_ERROR;
}

DeviceControllerFactory::~DeviceControllerFactory()
{
Shutdown();
Expand Down
5 changes: 4 additions & 1 deletion src/controller/CHIPDeviceControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class DeviceControllerFactory
//
bool ReleaseSystemState();

// Like RetainSystemState(), but will re-initialize the system state first if necessary.
CHIP_ERROR EnsureAndRetainSystemState();

//
// Retrieve a read-only pointer to the system state object that contains pointers to key stack
// singletons. If the pointer is null, it indicates that the DeviceControllerFactory has yet to
Expand Down Expand Up @@ -272,7 +275,7 @@ class DeviceControllerFactory
DeviceControllerFactory() {}
void PopulateInitParams(ControllerInitParams & controllerParams, const SetupParams & params);
CHIP_ERROR InitSystemState(FactoryInitParams params);
CHIP_ERROR InitSystemState();
CHIP_ERROR ReinitSystemStateIfNecessary();
void ControllerInitialized(const DeviceController & controller);

uint16_t mListenPort;
Expand Down

0 comments on commit c6fbf7c

Please sign in to comment.