Skip to content

Commit

Permalink
Group Data Provider: PersistentStorageDelegate passed as init() argum…
Browse files Browse the repository at this point in the history
…ent.
  • Loading branch information
rcasallas-silabs committed Mar 10, 2022
1 parent 244969f commit aa5013d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ uint32_t InteractionModelEngine::GetNumActiveWriteHandlers() const
}

void InteractionModelEngine::CloseTransactionsFromFabricIndex(FabricIndex aFabricIndex)
{
{
//
// Walk through all existing subscriptions and shut down those whose subscriber matches
// that which just came in.
Expand Down
4 changes: 2 additions & 2 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Server::Server() :
.devicePool = &mDevicePool,
.dnsResolver = nullptr,
}),
mCommissioningWindowManager(this), mGroupsProvider(mDeviceStorage), mAttributePersister(mDeviceStorage),
mCommissioningWindowManager(this), mGroupsProvider(), mAttributePersister(mDeviceStorage),
mAccessControl(Access::Examples::GetAccessControlDelegate(&mDeviceStorage))
{}

Expand Down Expand Up @@ -138,7 +138,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint
app::DnssdServer::Instance().SetCommissioningModeProvider(&mCommissioningWindowManager);

// Group data provider must be initialized after mDeviceStorage
err = mGroupsProvider.Init();
err = mGroupsProvider.Init(&mDeviceStorage);
SuccessOrExit(err);
SetGroupDataProvider(&mGroupsProvider);

Expand Down
5 changes: 3 additions & 2 deletions src/credentials/GroupDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app/util/basic-types.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>

namespace chip {
namespace Credentials {
Expand Down Expand Up @@ -227,8 +228,8 @@ class GroupDataProvider
* @retval #CHIP_ERROR_INCORRECT_STATE if called when already initialized.
* @retval #CHIP_NO_ERROR on success
*/
virtual CHIP_ERROR Init() = 0;
virtual void Finish() = 0;
virtual CHIP_ERROR Init(PersistentStorageDelegate * storage) = 0;
virtual void Finish() = 0;

//
// Group Table
Expand Down
48 changes: 29 additions & 19 deletions src/credentials/GroupDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ struct PersistentData
virtual CHIP_ERROR Deserialize(TLV::TLVReader & reader) = 0;
virtual void Clear() = 0;

virtual CHIP_ERROR Save(chip::PersistentStorageDelegate & storage)
virtual CHIP_ERROR Save(PersistentStorageDelegate * storage)
{
VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);

uint8_t buffer[kMaxSerializedSize] = { 0 };
DefaultStorageKeyAllocator key;
// Update storage key
Expand All @@ -58,11 +60,13 @@ struct PersistentData
ReturnErrorOnFailure(Serialize(writer));

// Save serialized data
return storage.SyncSetKeyValue(key.KeyName(), buffer, static_cast<uint16_t>(writer.GetLengthWritten()));
return storage->SyncSetKeyValue(key.KeyName(), buffer, static_cast<uint16_t>(writer.GetLengthWritten()));
}

CHIP_ERROR Load(chip::PersistentStorageDelegate & storage)
CHIP_ERROR Load(PersistentStorageDelegate * storage)
{
VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);

uint8_t buffer[kMaxSerializedSize] = { 0 };
DefaultStorageKeyAllocator key;

Expand All @@ -74,7 +78,7 @@ struct PersistentData

// Load the serialized data
uint16_t size = static_cast<uint16_t>(sizeof(buffer));
CHIP_ERROR err = storage.SyncGetKeyValue(key.KeyName(), buffer, size);
CHIP_ERROR err = storage->SyncGetKeyValue(key.KeyName(), buffer, size);
VerifyOrReturnError(CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND != err, CHIP_ERROR_NOT_FOUND);
ReturnErrorOnFailure(err);

Expand All @@ -84,13 +88,15 @@ struct PersistentData
return Deserialize(reader);
}

virtual CHIP_ERROR Delete(chip::PersistentStorageDelegate & storage)
virtual CHIP_ERROR Delete(PersistentStorageDelegate * storage)
{
VerifyOrReturnError(nullptr != storage, CHIP_ERROR_INVALID_ARGUMENT);

DefaultStorageKeyAllocator key;
// Update storage key
ReturnErrorOnFailure(UpdateKey(key));
// Delete stored data
return storage.SyncDeleteKeyValue(key.KeyName());
return storage->SyncDeleteKeyValue(key.KeyName());
}
};

Expand Down Expand Up @@ -248,7 +254,7 @@ struct FabricData : public PersistentData<kPersistentBufferMax>
}

// Register the fabric in the fabrics' linked-list
CHIP_ERROR Register(chip::PersistentStorageDelegate & storage)
CHIP_ERROR Register(PersistentStorageDelegate * storage)
{
FabricList fabric_list;
CHIP_ERROR err = fabric_list.Load(storage);
Expand Down Expand Up @@ -285,7 +291,7 @@ struct FabricData : public PersistentData<kPersistentBufferMax>
}

// Remove the fabric from the fabrics' linked list
CHIP_ERROR Unregister(chip::PersistentStorageDelegate & storage)
CHIP_ERROR Unregister(PersistentStorageDelegate * storage)
{
FabricList fabric_list;
CHIP_ERROR err = fabric_list.Load(storage);
Expand Down Expand Up @@ -328,7 +334,7 @@ struct FabricData : public PersistentData<kPersistentBufferMax>
}

// Check the fabric is registered in the fabrics' linked list
CHIP_ERROR Validate(chip::PersistentStorageDelegate & storage)
CHIP_ERROR Validate(PersistentStorageDelegate * storage)
{
FabricList fabric_list;
ReturnErrorOnFailure(fabric_list.Load(storage));
Expand All @@ -349,13 +355,13 @@ struct FabricData : public PersistentData<kPersistentBufferMax>
return CHIP_ERROR_NOT_FOUND;
}

CHIP_ERROR Save(chip::PersistentStorageDelegate & storage) override
CHIP_ERROR Save(PersistentStorageDelegate * storage) override
{
ReturnErrorOnFailure(Register(storage));
return PersistentData::Save(storage);
}

CHIP_ERROR Delete(chip::PersistentStorageDelegate & storage) override
CHIP_ERROR Delete(PersistentStorageDelegate * storage) override
{
ReturnErrorOnFailure(Unregister(storage));
return PersistentData::Delete(storage);
Expand Down Expand Up @@ -434,7 +440,7 @@ struct GroupData : public GroupDataProvider::GroupInfo, PersistentData<kPersiste
return reader.ExitContainer(container);
}

bool Get(chip::PersistentStorageDelegate & storage, const FabricData & fabric, size_t target_index)
bool Get(PersistentStorageDelegate * storage, const FabricData & fabric, size_t target_index)
{
fabric_index = fabric.fabric_index;
group_id = fabric.first_group;
Expand Down Expand Up @@ -462,7 +468,7 @@ struct GroupData : public GroupDataProvider::GroupInfo, PersistentData<kPersiste
return false;
}

bool Find(chip::PersistentStorageDelegate & storage, const FabricData & fabric, chip::GroupId target_group)
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, chip::GroupId target_group)
{
fabric_index = fabric.fabric_index;
group_id = fabric.first_group;
Expand Down Expand Up @@ -545,7 +551,7 @@ struct KeyMapData : public GroupDataProvider::GroupKey, LinkedData
return reader.ExitContainer(container);
}

bool Get(chip::PersistentStorageDelegate & storage, const FabricData & fabric, size_t target_index)
bool Get(PersistentStorageDelegate * storage, const FabricData & fabric, size_t target_index)
{
fabric_index = fabric.fabric_index;
id = fabric.first_map;
Expand Down Expand Up @@ -575,7 +581,7 @@ struct KeyMapData : public GroupDataProvider::GroupKey, LinkedData
return false;
}

bool Find(chip::PersistentStorageDelegate & storage, const FabricData & fabric, const GroupKey & map)
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, const GroupKey & map)
{
fabric_index = fabric.fabric_index;
id = fabric.first_map;
Expand Down Expand Up @@ -661,8 +667,7 @@ struct EndpointData : GroupDataProvider::GroupEndpoint, PersistentData<kPersiste
return reader.ExitContainer(container);
}

bool Find(chip::PersistentStorageDelegate & storage, const FabricData & fabric, const GroupData & group,
chip::EndpointId target_id)
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, const GroupData & group, chip::EndpointId target_id)
{
fabric_index = fabric.fabric_index;
group_id = group.group_id;
Expand Down Expand Up @@ -841,7 +846,7 @@ struct KeySetData : PersistentData<kPersistentBufferMax>
return reader.ExitContainer(container);
}

bool Find(chip::PersistentStorageDelegate & storage, const FabricData & fabric, size_t target_id)
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, size_t target_id)
{
uint16_t count = 0;

Expand Down Expand Up @@ -878,8 +883,13 @@ struct KeySetData : PersistentData<kPersistentBufferMax>
constexpr size_t GroupDataProvider::GroupInfo::kGroupNameMax;
constexpr size_t GroupDataProviderImpl::kIteratorsMax;

CHIP_ERROR GroupDataProviderImpl::Init()
CHIP_ERROR GroupDataProviderImpl::Init(PersistentStorageDelegate * storage)
{
if (storage == nullptr)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
mStorage = storage;
mInitialized = true;
return CHIP_NO_ERROR;
}
Expand Down
15 changes: 6 additions & 9 deletions src/credentials/GroupDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#pragma once

#include <credentials/GroupDataProvider.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/support/Pool.h>

namespace chip {
Expand All @@ -28,15 +27,13 @@ class GroupDataProviderImpl : public GroupDataProvider
public:
static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_GROUP_CONCURRENT_ITERATORS;

GroupDataProviderImpl(chip::PersistentStorageDelegate & storage_delegate) : mStorage(storage_delegate) {}
GroupDataProviderImpl(chip::PersistentStorageDelegate & storage_delegate, uint16_t maxGroupsPerFabric,
uint16_t maxGroupKeysPerFabric) :
GroupDataProvider(maxGroupsPerFabric, maxGroupKeysPerFabric),
mStorage(storage_delegate)
GroupDataProviderImpl() = default;
GroupDataProviderImpl(uint16_t maxGroupsPerFabric, uint16_t maxGroupKeysPerFabric) :
GroupDataProvider(maxGroupsPerFabric, maxGroupKeysPerFabric)
{}
virtual ~GroupDataProviderImpl() {}

CHIP_ERROR Init() override;
CHIP_ERROR Init(PersistentStorageDelegate * storage) override;
void Finish() override;

//
Expand Down Expand Up @@ -215,8 +212,8 @@ class GroupDataProviderImpl : public GroupDataProvider
};
CHIP_ERROR RemoveEndpoints(FabricIndex fabric_index, GroupId group_id);

chip::PersistentStorageDelegate & mStorage;
bool mInitialized = false;
chip::PersistentStorageDelegate * mStorage = nullptr;
bool mInitialized = false;
ObjectPool<GroupInfoIteratorImpl, kIteratorsMax> mGroupInfoIterators;
ObjectPool<GroupKeyIteratorImpl, kIteratorsMax> mGroupKeyIterators;
ObjectPool<EndpointIteratorImpl, kIteratorsMax> mEndpointIterators;
Expand Down
8 changes: 4 additions & 4 deletions src/credentials/tests/TestGroupDataProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,7 @@ void TestGroupDecryption(nlTestSuite * apSuite, void * apContext)
namespace {

static chip::TestPersistentStorageDelegate sDelegate;
static GroupDataProviderImpl sProvider(sDelegate, chip::app::TestGroups::kMaxGroupsPerFabric,
chip::app::TestGroups::kMaxGroupKeysPerFabric);
static GroupDataProviderImpl sProvider(chip::app::TestGroups::kMaxGroupsPerFabric, chip::app::TestGroups::kMaxGroupKeysPerFabric);

static EpochKey kEpochKeys0[] = {
{ 0x0000000000000000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
Expand Down Expand Up @@ -1145,12 +1144,13 @@ static EpochKey kEpochKeys3[] = {
*/
int Test_Setup(void * inContext)
{
SetGroupDataProvider(&sProvider);
VerifyOrReturnError(CHIP_NO_ERROR == chip::Platform::MemoryInit(), FAILURE);
VerifyOrReturnError(CHIP_NO_ERROR == sProvider.Init(), FAILURE);

// Initialize Group Data Provider
VerifyOrReturnError(CHIP_NO_ERROR == sProvider.Init(&sDelegate), FAILURE);
// Event listener
sProvider.SetListener(&chip::app::TestGroups::sListener);
SetGroupDataProvider(&sProvider);

memcpy(chip::app::TestGroups::kKeySet0.epoch_keys, kEpochKeys0, sizeof(kEpochKeys0));
memcpy(chip::app::TestGroups::kKeySet1.epoch_keys, kEpochKeys1, sizeof(kEpochKeys1));
Expand Down
4 changes: 2 additions & 2 deletions src/lib/support/TestGroupData.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ constexpr uint16_t kMaxGroupsPerFabric = 5;
constexpr uint16_t kMaxGroupKeysPerFabric = 8;

static chip::TestPersistentStorageDelegate sDeviceStorage;
static chip::Credentials::GroupDataProviderImpl sGroupsProvider(sDeviceStorage, kMaxGroupsPerFabric, kMaxGroupKeysPerFabric);
static chip::Credentials::GroupDataProviderImpl sGroupsProvider(kMaxGroupsPerFabric, kMaxGroupKeysPerFabric);

static const chip::GroupId kGroup1 = 0x0101;
static const chip::GroupId kGroup2 = 0x0102;
Expand All @@ -42,7 +42,7 @@ namespace GroupTesting {

CHIP_ERROR InitProvider()
{
ReturnErrorOnFailure(sGroupsProvider.Init());
ReturnErrorOnFailure(sGroupsProvider.Init(&sDeviceStorage));
chip::Credentials::SetGroupDataProvider(&sGroupsProvider);
return CHIP_NO_ERROR;
}
Expand Down

0 comments on commit aa5013d

Please sign in to comment.