Skip to content

Commit

Permalink
Refactor ICDManager init to leverage a builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mkardous-silabs committed Jul 26, 2024
1 parent ebca97f commit f1484c0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
34 changes: 11 additions & 23 deletions src/app/icd/server/ICDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ using chip::Protocols::InteractionModel::Status;
static_assert(UINT8_MAX >= CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS,
"ICDManager::mOpenExchangeContextCount cannot hold count for the max exchange count");

void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricTable, Crypto::SymmetricKeystore * symmetricKeystore,
Messaging::ExchangeManager * exchangeManager, SubscriptionsInfoProvider * subInfoProvider,
ICDCheckInBackOffStrategy * strategy)
void ICDManager::Init()
{
#if CHIP_CONFIG_ENABLE_ICD_CIP
VerifyOrDie(storage != nullptr);
VerifyOrDie(fabricTable != nullptr);
VerifyOrDie(symmetricKeystore != nullptr);
VerifyOrDie(exchangeManager != nullptr);
VerifyOrDie(subInfoProvider != nullptr);
VerifyOrDie(strategy != nullptr);
VerifyOrDie(mStorage != nullptr);
VerifyOrDie(mFabricTable != nullptr);
VerifyOrDie(mSymmetricKeystore != nullptr);
VerifyOrDie(mExchangeManager != nullptr);
VerifyOrDie(mSubInfoProvider != nullptr);
VerifyOrDie(mICDCheckInBackOffStrategy != nullptr);

VerifyOrDie(ICDConfigurationData::GetInstance().GetICDCounter().Init(mStorage, DefaultStorageKeyAllocator::ICDCheckInCounter(),
ICDConfigurationData::kICDCounterPersistenceIncrement) ==
CHIP_NO_ERROR);
#endif // CHIP_CONFIG_ENABLE_ICD_CIP

#if CHIP_CONFIG_ENABLE_ICD_LIT
Expand All @@ -84,19 +86,6 @@ void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricT

VerifyOrDie(ICDNotifier::GetInstance().Subscribe(this) == CHIP_NO_ERROR);

#if CHIP_CONFIG_ENABLE_ICD_CIP
mStorage = storage;
mFabricTable = fabricTable;
mSymmetricKeystore = symmetricKeystore;
mExchangeManager = exchangeManager;
mSubInfoProvider = subInfoProvider;
mICDCheckInBackOffStrategy = strategy;

VerifyOrDie(ICDConfigurationData::GetInstance().GetICDCounter().Init(mStorage, DefaultStorageKeyAllocator::ICDCheckInCounter(),
ICDConfigurationData::kICDCounterPersistenceIncrement) ==
CHIP_NO_ERROR);
#endif // CHIP_CONFIG_ENABLE_ICD_CIP

UpdateICDMode();
UpdateOperationState(OperationalState::IdleMode);
}
Expand Down Expand Up @@ -197,7 +186,6 @@ void ICDManager::SendCheckInMsgs()
continue;
}

// Validate Check-In BackOff strategy if we should send a Check-In message.
if (!mICDCheckInBackOffStrategy->ShouldSendCheckInMessage(entry))
{
// continue to next entry
Expand Down
49 changes: 46 additions & 3 deletions src/app/icd/server/ICDManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,52 @@ class ICDManager : public ICDListener, public TestEventTriggerHandler
ICDManager() = default;
~ICDManager() = default;

void Init(PersistentStorageDelegate * storage, FabricTable * fabricTable, Crypto::SymmetricKeystore * symmetricKeyStore,
Messaging::ExchangeManager * exchangeManager, SubscriptionsInfoProvider * subInfoProvider,
ICDCheckInBackOffStrategy * strategy);
/*
Builder function to set all necessary members for the ICDManager class
*/

#if CHIP_CONFIG_ENABLE_ICD_CIP
ICDManager & SetPersistentStorageDelegate(PersistentStorageDelegate * storage)
{
mStorage = storage;
return *this;
};

ICDManager & SetFabricTable(FabricTable * fabricTable)
{
mFabricTable = fabricTable;
return *this;
};

ICDManager & SetSymmetricKeyStore(Crypto::SymmetricKeystore * symmetricKeystore)
{
mSymmetricKeystore = symmetricKeystore;
return *this;
};

ICDManager & SetExchangeManager(Messaging::ExchangeManager * exchangeManager)
{
mExchangeManager = exchangeManager;
return *this;
};

ICDManager & SetSubscriptionsInfoProvider(SubscriptionsInfoProvider * subInfoProvider)
{
mSubInfoProvider = subInfoProvider;
return *this;
};

ICDManager & SetICDCheckInBackOffStrategy(ICDCheckInBackOffStrategy * strategy)
{
mICDCheckInBackOffStrategy = strategy;
return *this;
};
#endif // CHIP_CONFIG_ENABLE_ICD_CIP

/**
* @brief Validates that the ICDManager has all the necessary members to function and initializes the class
*/
void Init();
void Shutdown();

/**
Expand Down
25 changes: 22 additions & 3 deletions src/app/icd/server/tests/TestICDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ class TestICDManager : public Test::LoopbackMessagingContext

mICDStateObserver.ResetAll();
mICDManager.RegisterObserver(&mICDStateObserver);
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider, &mStrategy);

mICDManager.SetPersistentStorageDelegate(&testStorage)
.SetFabricTable(&GetFabricTable())
.SetSymmetricKeyStore(&mKeystore)
.SetExchangeManager(&GetExchangeManager())
.SetSubscriptionsInfoProvider(&mSubInfoProvider)
.SetICDCheckInBackOffStrategy(&mStrategy)
.Init();
}

// Performs teardown for each individual test in the test suite
Expand Down Expand Up @@ -570,7 +577,13 @@ TEST_F(TestICDManager, TestICDCounter)

// Shut down and reinit ICDManager to increment counter
mICDManager.Shutdown();
mICDManager.Init(&(testStorage), &GetFabricTable(), &(mKeystore), &GetExchangeManager(), &(mSubInfoProvider), &mStrategy);
mICDManager.SetPersistentStorageDelegate(&testStorage)
.SetFabricTable(&GetFabricTable())
.SetSymmetricKeyStore(&mKeystore)
.SetExchangeManager(&GetExchangeManager())
.SetSubscriptionsInfoProvider(&mSubInfoProvider)
.SetICDCheckInBackOffStrategy(&mStrategy)
.Init();
mICDManager.RegisterObserver(&(mICDStateObserver));

EXPECT_EQ(counter + ICDConfigurationData::kICDCounterPersistenceIncrement,
Expand Down Expand Up @@ -975,7 +988,13 @@ TEST_F(TestICDManager, TestICDStateObserverOnICDModeChangeOnInit)
// Shut down and reinit ICDManager - We should go to LIT mode since we have a registration
mICDManager.Shutdown();
mICDManager.RegisterObserver(&(mICDStateObserver));
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider, &mStrategy);
mICDManager.SetPersistentStorageDelegate(&testStorage)
.SetFabricTable(&GetFabricTable())
.SetSymmetricKeyStore(&mKeystore)
.SetExchangeManager(&GetExchangeManager())
.SetSubscriptionsInfoProvider(&mSubInfoProvider)
.SetICDCheckInBackOffStrategy(&mStrategy)
.Init();

// We have a registration, transition to LIT mode
EXPECT_TRUE(mICDStateObserver.mOnICDModeChangeCalled);
Expand Down
12 changes: 10 additions & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,16 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
mICDManager.RegisterObserver(mReportScheduler);
mICDManager.RegisterObserver(&app::DnssdServer::Instance());

mICDManager.Init(mDeviceStorage, &GetFabricTable(), mSessionKeystore, &mExchangeMgr,
chip::app::InteractionModelEngine::GetInstance(), initParams.icdCheckInBackOffStrategy);
#if CHIP_CONFIG_ENABLE_ICD_CIP
mICDManager.SetPersistentStorageDelegate(mDeviceStorage)
.SetFabricTable(&GetFabricTable())
.SetSymmetricKeyStore(mSessionKeystore)
.SetExchangeManager(&mExchangeMgr)
.SetSubscriptionsInfoProvider(chip::app::InteractionModelEngine::GetInstance())
.SetICDCheckInBackOffStrategy(initParams.icdCheckInBackOffStrategy);

#endif // CHIP_CONFIG_ENABLE_ICD_CIP
mICDManager.Init();

// Register Test Event Trigger Handler
if (mTestEventTriggerDelegate != nullptr)
Expand Down

0 comments on commit f1484c0

Please sign in to comment.