Skip to content

Commit

Permalink
Allow runtime init of some Server members (1/2) (#16065)
Browse files Browse the repository at this point in the history
* Allow runtime init of some `Server` members (1/2)

This PR is on the path towards having Server::Server no longer
statically initialize its members with storage, and instead
relying on Server::Init(). This will simplify organization
of unit tests and also the convergence of Controller/Server
storage to address issue #16028

Issue #16028

* Restyled by clang-format

* Move a comment to the correct place

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Nov 18, 2023
1 parent 21b2a93 commit 7519188
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/app/DefaultAttributePersistenceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace app {
CHIP_ERROR DefaultAttributePersistenceProvider::WriteValue(const ConcreteAttributePath & aPath,
const EmberAfAttributeMetadata * aMetadata, const ByteSpan & aValue)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);

// TODO: we may want to have a small cache for values that change a lot, so
// we only write them once a bunch of changes happen or on timer or
// shutdown.
Expand All @@ -33,15 +35,17 @@ CHIP_ERROR DefaultAttributePersistenceProvider::WriteValue(const ConcreteAttribu
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
return mStorage.SyncSetKeyValue(key.AttributeValue(aPath), aValue.data(), static_cast<uint16_t>(aValue.size()));
return mStorage->SyncSetKeyValue(key.AttributeValue(aPath), aValue.data(), static_cast<uint16_t>(aValue.size()));
}

CHIP_ERROR DefaultAttributePersistenceProvider::ReadValue(const ConcreteAttributePath & aPath,
const EmberAfAttributeMetadata * aMetadata, MutableByteSpan & aValue)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);

DefaultStorageKeyAllocator key;
uint16_t size = static_cast<uint16_t>(min(aValue.size(), static_cast<size_t>(UINT16_MAX)));
ReturnErrorOnFailure(mStorage.SyncGetKeyValue(key.AttributeValue(aPath), aValue.data(), size));
ReturnErrorOnFailure(mStorage->SyncGetKeyValue(key.AttributeValue(aPath), aValue.data(), size));
EmberAfAttributeType type = aMetadata->attributeType;
if (emberAfIsStringAttributeType(type))
{
Expand Down
20 changes: 16 additions & 4 deletions src/app/DefaultAttributePersistenceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,29 @@ namespace app {
class DefaultAttributePersistenceProvider : public AttributePersistenceProvider
{
public:
// aStorage must outlive this object.
DefaultAttributePersistenceProvider(PersistentStorageDelegate & aStorage) : mStorage(aStorage) {}
DefaultAttributePersistenceProvider() {}

// Passed-in storage must outlive this object.
CHIP_ERROR Init(PersistentStorageDelegate * storage)
{
if (storage == nullptr)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
mStorage = storage;
return CHIP_NO_ERROR;
}

void Shutdown() {}

// AttributePersistenceProvider implementation.
CHIP_ERROR WriteValue(const ConcreteAttributePath & aPath, const EmberAfAttributeMetadata * aMetadata,
const ByteSpan & aValue) override;
CHIP_ERROR ReadValue(const ConcreteAttributePath & aPath, const EmberAfAttributeMetadata * aMetadata,
MutableByteSpan & aValue) override;

private:
PersistentStorageDelegate & mStorage;
protected:
PersistentStorageDelegate * mStorage;
};

} // namespace app
Expand Down
12 changes: 11 additions & 1 deletion src/app/server/CommissioningWindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ class Server;
class CommissioningWindowManager : public SessionEstablishmentDelegate, public app::CommissioningModeProvider
{
public:
CommissioningWindowManager(Server * server) : mAppDelegate(nullptr), mServer(server) {}
CommissioningWindowManager() {}

CHIP_ERROR Init(Server * server)
{
if (server == nullptr)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
mServer = server;
return CHIP_NO_ERROR;
}

void SetAppDelegate(AppDelegate * delegate) { mAppDelegate = delegate; }

Expand Down
10 changes: 8 additions & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ Server::Server() :
.devicePool = &mDevicePool,
.dnsResolver = nullptr,
}),
mCommissioningWindowManager(this), mGroupsProvider(mDeviceStorage), mAttributePersister(mDeviceStorage),
mAccessControl(Access::Examples::GetAccessControlDelegate(&mDeviceStorage))
mGroupsProvider(mDeviceStorage), mAccessControl(Access::Examples::GetAccessControlDelegate(&mDeviceStorage))
{}

CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort,
Expand All @@ -120,13 +119,16 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint

CHIP_ERROR err = CHIP_NO_ERROR;

// TODO: Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code
chip::Platform::MemoryInit();

SuccessOrExit(err = mCommissioningWindowManager.Init(this));
mCommissioningWindowManager.SetAppDelegate(delegate);
mCommissioningWindowManager.SetSessionIDAllocator(&mSessionIDAllocator);

// Set up attribute persistence before we try to bring up the data model
// handler.
SuccessOrExit(mAttributePersister.Init(&mDeviceStorage));
SetAttributePersistenceProvider(&mAttributePersister);

InitDataModelHandler(&mExchangeMgr);
Expand Down Expand Up @@ -337,8 +339,12 @@ void Server::Shutdown()
}
mSessions.Shutdown();
mTransports.Close();

mAttributePersister.Shutdown();
mCommissioningWindowManager.Shutdown();
mCASESessionManager.Shutdown();

// TODO: Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code
chip::Platform::MemoryShutdown();
}

Expand Down

0 comments on commit 7519188

Please sign in to comment.