From 73fbfc33f827c8a7e9a29edca0e1ca7b3f099e6a Mon Sep 17 00:00:00 2001
From: Karsten Sperling <ksperling@apple.com>
Date: Tue, 9 Apr 2024 16:35:59 +1200
Subject: [PATCH 1/2] 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.
---
 .../CHIPDeviceControllerFactory.cpp           | 56 +++++++++----------
 src/controller/CHIPDeviceControllerFactory.h  |  5 +-
 2 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/src/controller/CHIPDeviceControllerFactory.cpp b/src/controller/CHIPDeviceControllerFactory.cpp
index e4e419978d8a2d..1cd2c07ca66130 100644
--- a/src/controller/CHIPDeviceControllerFactory.cpp
+++ b/src/controller/CHIPDeviceControllerFactory.cpp
@@ -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);
@@ -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);
@@ -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;
 
@@ -397,6 +388,13 @@ bool DeviceControllerFactory::ReleaseSystemState()
     return mSystemState->Release();
 }
 
+CHIP_ERROR DeviceControllerFactory::InitAndRetainSystemState()
+{
+    ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
+    RetainSystemState();
+    return CHIP_NO_ERROR;
+}
+
 DeviceControllerFactory::~DeviceControllerFactory()
 {
     Shutdown();
diff --git a/src/controller/CHIPDeviceControllerFactory.h b/src/controller/CHIPDeviceControllerFactory.h
index 19b19e54eab414..725c326baf667c 100644
--- a/src/controller/CHIPDeviceControllerFactory.h
+++ b/src/controller/CHIPDeviceControllerFactory.h
@@ -209,6 +209,9 @@ class DeviceControllerFactory
     //
     bool ReleaseSystemState();
 
+    // Like RetainSystemState(), but will re-initialize the system state first if necessary.
+    CHIP_ERROR InitAndRetainSystemState();
+
     //
     // 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
@@ -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;

From 53795d3b60253179db2d05a07c08edb6af38c4f6 Mon Sep 17 00:00:00 2001
From: Karsten Sperling <ksperling@apple.com>
Date: Thu, 18 Apr 2024 08:29:19 +1200
Subject: [PATCH 2/2] Rename to EnsureAndRetainSystemState

---
 src/controller/CHIPDeviceControllerFactory.cpp | 2 +-
 src/controller/CHIPDeviceControllerFactory.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/controller/CHIPDeviceControllerFactory.cpp b/src/controller/CHIPDeviceControllerFactory.cpp
index 1cd2c07ca66130..248e96cf237f3a 100644
--- a/src/controller/CHIPDeviceControllerFactory.cpp
+++ b/src/controller/CHIPDeviceControllerFactory.cpp
@@ -388,7 +388,7 @@ bool DeviceControllerFactory::ReleaseSystemState()
     return mSystemState->Release();
 }
 
-CHIP_ERROR DeviceControllerFactory::InitAndRetainSystemState()
+CHIP_ERROR DeviceControllerFactory::EnsureAndRetainSystemState()
 {
     ReturnErrorOnFailure(ReinitSystemStateIfNecessary());
     RetainSystemState();
diff --git a/src/controller/CHIPDeviceControllerFactory.h b/src/controller/CHIPDeviceControllerFactory.h
index 725c326baf667c..d4e50e9c44b57a 100644
--- a/src/controller/CHIPDeviceControllerFactory.h
+++ b/src/controller/CHIPDeviceControllerFactory.h
@@ -210,7 +210,7 @@ class DeviceControllerFactory
     bool ReleaseSystemState();
 
     // Like RetainSystemState(), but will re-initialize the system state first if necessary.
-    CHIP_ERROR InitAndRetainSystemState();
+    CHIP_ERROR EnsureAndRetainSystemState();
 
     //
     // Retrieve a read-only pointer to the system state object that contains pointers to key stack