Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviceControllerFactory: Add InitAndRetainSystemState #32998

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
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
Loading