Skip to content

Commit c6fbf7c

Browse files
DeviceControllerFactory: Add InitAndRetainSystemState (#32998)
* 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
1 parent 01facfd commit c6fbf7c

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

src/controller/CHIPDeviceControllerFactory.cpp

+27-29
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,36 @@ CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)
7373
return err;
7474
}
7575

76-
CHIP_ERROR DeviceControllerFactory::InitSystemState()
76+
CHIP_ERROR DeviceControllerFactory::ReinitSystemStateIfNecessary()
7777
{
78+
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
79+
VerifyOrReturnError(mSystemState->IsShutdown(), CHIP_NO_ERROR);
80+
7881
FactoryInitParams params;
79-
if (mSystemState != nullptr)
80-
{
81-
params.systemLayer = mSystemState->SystemLayer();
82-
params.udpEndPointManager = mSystemState->UDPEndPointManager();
82+
params.systemLayer = mSystemState->SystemLayer();
83+
params.udpEndPointManager = mSystemState->UDPEndPointManager();
8384
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
84-
params.tcpEndPointManager = mSystemState->TCPEndPointManager();
85+
params.tcpEndPointManager = mSystemState->TCPEndPointManager();
8586
#endif
8687
#if CONFIG_NETWORK_LAYER_BLE
87-
params.bleLayer = mSystemState->BleLayer();
88+
params.bleLayer = mSystemState->BleLayer();
8889
#endif
89-
params.listenPort = mListenPort;
90-
params.fabricIndependentStorage = mFabricIndependentStorage;
91-
params.enableServerInteractions = mEnableServerInteractions;
92-
params.groupDataProvider = mSystemState->GetGroupDataProvider();
93-
params.sessionKeystore = mSystemState->GetSessionKeystore();
94-
params.fabricTable = mSystemState->Fabrics();
95-
params.operationalKeystore = mOperationalKeystore;
96-
params.opCertStore = mOpCertStore;
97-
params.certificateValidityPolicy = mCertificateValidityPolicy;
98-
params.sessionResumptionStorage = mSessionResumptionStorage;
99-
}
90+
params.listenPort = mListenPort;
91+
params.fabricIndependentStorage = mFabricIndependentStorage;
92+
params.enableServerInteractions = mEnableServerInteractions;
93+
params.groupDataProvider = mSystemState->GetGroupDataProvider();
94+
params.sessionKeystore = mSystemState->GetSessionKeystore();
95+
params.fabricTable = mSystemState->Fabrics();
96+
params.operationalKeystore = mOperationalKeystore;
97+
params.opCertStore = mOpCertStore;
98+
params.certificateValidityPolicy = mCertificateValidityPolicy;
99+
params.sessionResumptionStorage = mSessionResumptionStorage;
100100

101101
return InitSystemState(params);
102102
}
103103

104104
CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
105105
{
106-
if (mSystemState != nullptr && mSystemState->IsInitialized())
107-
{
108-
return CHIP_NO_ERROR;
109-
}
110-
111106
if (mSystemState != nullptr)
112107
{
113108
Platform::Delete(mSystemState);
@@ -331,10 +326,8 @@ void DeviceControllerFactory::ControllerInitialized(const DeviceController & con
331326

332327
CHIP_ERROR DeviceControllerFactory::SetupController(SetupParams params, DeviceController & controller)
333328
{
334-
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
335329
VerifyOrReturnError(params.controllerVendorId != VendorId::Unspecified, CHIP_ERROR_INVALID_ARGUMENT);
336-
337-
ReturnErrorOnFailure(InitSystemState());
330+
ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
338331

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

352345
CHIP_ERROR DeviceControllerFactory::SetupCommissioner(SetupParams params, DeviceCommissioner & commissioner)
353346
{
354-
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
355347
VerifyOrReturnError(params.controllerVendorId != VendorId::Unspecified, CHIP_ERROR_INVALID_ARGUMENT);
356-
357-
ReturnErrorOnFailure(InitSystemState());
348+
ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
358349

359350
CommissionerInitParams commissionerParams;
360351

@@ -397,6 +388,13 @@ bool DeviceControllerFactory::ReleaseSystemState()
397388
return mSystemState->Release();
398389
}
399390

391+
CHIP_ERROR DeviceControllerFactory::EnsureAndRetainSystemState()
392+
{
393+
ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
394+
RetainSystemState();
395+
return CHIP_NO_ERROR;
396+
}
397+
400398
DeviceControllerFactory::~DeviceControllerFactory()
401399
{
402400
Shutdown();

src/controller/CHIPDeviceControllerFactory.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ class DeviceControllerFactory
209209
//
210210
bool ReleaseSystemState();
211211

212+
// Like RetainSystemState(), but will re-initialize the system state first if necessary.
213+
CHIP_ERROR EnsureAndRetainSystemState();
214+
212215
//
213216
// Retrieve a read-only pointer to the system state object that contains pointers to key stack
214217
// singletons. If the pointer is null, it indicates that the DeviceControllerFactory has yet to
@@ -272,7 +275,7 @@ class DeviceControllerFactory
272275
DeviceControllerFactory() {}
273276
void PopulateInitParams(ControllerInitParams & controllerParams, const SetupParams & params);
274277
CHIP_ERROR InitSystemState(FactoryInitParams params);
275-
CHIP_ERROR InitSystemState();
278+
CHIP_ERROR ReinitSystemStateIfNecessary();
276279
void ControllerInitialized(const DeviceController & controller);
277280

278281
uint16_t mListenPort;

0 commit comments

Comments
 (0)