Skip to content

Commit

Permalink
Group Data Provider: RemoveFabric method added.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasallas-silabs committed Sep 30, 2021
1 parent e24aae9 commit 8f9e56d
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 27 deletions.
33 changes: 18 additions & 15 deletions src/credentials/GroupDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,32 @@ class GroupDataProvider
virtual void Finish() = 0;

// Endpoints
virtual bool GroupMappingExists(chip::FabricIndex fabric_index, GroupMapping & mapping) = 0;
virtual CHIP_ERROR AddGroupMapping(chip::FabricIndex fabric_index, GroupMapping & mapping, const char * name) = 0;
virtual CHIP_ERROR RemoveGroupMapping(chip::FabricIndex fabric_index, GroupMapping & mapping) = 0;
virtual CHIP_ERROR RemoveAllGroupMappings(chip::FabricIndex fabric_index) = 0;
virtual GroupMappingIterator * IterateGroupMappings(chip::FabricIndex fabric_index, EndpointId endpoint) = 0;
virtual bool GroupMappingExists(chip::FabricIndex fabric_index, const GroupMapping & mapping) = 0;
virtual CHIP_ERROR AddGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping, const char * name) = 0;
virtual CHIP_ERROR RemoveGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping) = 0;
virtual CHIP_ERROR RemoveAllGroupMappings(chip::FabricIndex fabric_index) = 0;
virtual GroupMappingIterator * IterateGroupMappings(chip::FabricIndex fabric_index, EndpointId endpoint) = 0;

// States
virtual CHIP_ERROR SetGroupState(uint16_t state_index, const GroupState & state) = 0;
virtual CHIP_ERROR GetGroupState(uint16_t state_index, GroupState & state) = 0;
virtual CHIP_ERROR RemoveGroupState(uint16_t state_index) = 0;
virtual GroupStateIterator * IterateGroupStates() = 0;
virtual GroupStateIterator * IterateGroupStates(chip::FabricIndex fabric_index) = 0;
virtual CHIP_ERROR SetGroupState(size_t state_index, const GroupState & state) = 0;
virtual CHIP_ERROR GetGroupState(size_t state_index, GroupState & state) = 0;
virtual CHIP_ERROR RemoveGroupState(size_t state_index) = 0;
virtual GroupStateIterator * IterateGroupStates() = 0;
virtual GroupStateIterator * IterateGroupStates(chip::FabricIndex fabric_index) = 0;

// Keys
virtual CHIP_ERROR SetKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index, KeySet & keys) = 0;
virtual CHIP_ERROR GetKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index, KeySet & keys) = 0;
virtual CHIP_ERROR RemoveKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index) = 0;
virtual KeySetIterator * IterateKeySets(chip::FabricIndex fabric_index) = 0;
virtual CHIP_ERROR SetKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index, const KeySet & keys) = 0;
virtual CHIP_ERROR GetKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index, KeySet & keys) = 0;
virtual CHIP_ERROR RemoveKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index) = 0;
virtual KeySetIterator * IterateKeySets(chip::FabricIndex fabric_index) = 0;

// Fabrics
virtual CHIP_ERROR RemoveFabric(chip::FabricIndex fabric_index) = 0;

// Listener
void SetListener(GroupListener * listener) { mListener = listener; };
void RemoveListener() { mListener = nullptr; };

// TODO: handle fabric deletion (reindex fabric entries!)
protected:
GroupListener * mListener = nullptr;
};
Expand Down
56 changes: 47 additions & 9 deletions src/credentials/examples/GroupDataProviderExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class StaticGroupsProvider : public GroupDataProvider
size_t states_count;
// Group key sets for fabric
KeysEntry keys[kKeyEntriesMax];

void Clear()
{
in_use = false;
fabric_index = 0;
states_count = 0;
memset(endpoints, 0x00, sizeof(endpoints));
memset(keys, 0x00, sizeof(keys));
}
};

struct StateEntry : public GroupState
Expand All @@ -65,11 +74,11 @@ class StaticGroupsProvider : public GroupDataProvider
bool in_use = false;
void Clear()
{
in_use = false;
fabric_index = 0;
group = 0;
key_set_index = 0;
fabric = nullptr;
in_use = false;
}
};

Expand Down Expand Up @@ -292,7 +301,13 @@ class StaticGroupsProvider : public GroupDataProvider
public:
CHIP_ERROR Init() override
{
// Clear-out all fabric entries
for (size_t i = 0; i < kNumFabrics; ++i)
{
mFabrics[i].Clear();
}
// Clear-out all entries of index mapping.
mGroupStatesCount = 0;
for (size_t i = 0; i < kMaxNumGroupStates; ++i)
{
mGroupStates[i].Clear();
Expand All @@ -304,7 +319,7 @@ class StaticGroupsProvider : public GroupDataProvider
void Finish() override { mInitialized = false; }

// Endpoints
bool GroupMappingExists(chip::FabricIndex fabric_index, GroupMapping & mapping) override
bool GroupMappingExists(chip::FabricIndex fabric_index, const GroupMapping & mapping) override
{
VerifyOrReturnError(mInitialized, false);

Expand All @@ -322,7 +337,7 @@ class StaticGroupsProvider : public GroupDataProvider
return false;
}

CHIP_ERROR AddGroupMapping(chip::FabricIndex fabric_index, GroupMapping & mapping, const char * name) override
CHIP_ERROR AddGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping, const char * name) override
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);

Expand Down Expand Up @@ -356,7 +371,7 @@ class StaticGroupsProvider : public GroupDataProvider
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR RemoveGroupMapping(chip::FabricIndex fabric_index, GroupMapping & mapping) override
CHIP_ERROR RemoveGroupMapping(chip::FabricIndex fabric_index, const GroupMapping & mapping) override
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);

Expand Down Expand Up @@ -403,7 +418,7 @@ class StaticGroupsProvider : public GroupDataProvider

// States

CHIP_ERROR SetGroupState(uint16_t state_index, const GroupState & state) override
CHIP_ERROR SetGroupState(size_t state_index, const GroupState & state) override
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);

Expand Down Expand Up @@ -444,7 +459,7 @@ class StaticGroupsProvider : public GroupDataProvider
return CHIP_NO_ERROR;
}

CHIP_ERROR GetGroupState(uint16_t state_index, GroupState & state) override
CHIP_ERROR GetGroupState(size_t state_index, GroupState & state) override
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);
VerifyOrReturnError(static_cast<size_t>(state_index) < mGroupStatesCount, CHIP_ERROR_KEY_NOT_FOUND);
Expand All @@ -460,7 +475,7 @@ class StaticGroupsProvider : public GroupDataProvider
return CHIP_NO_ERROR;
}

CHIP_ERROR RemoveGroupState(uint16_t state_index) override
CHIP_ERROR RemoveGroupState(size_t state_index) override
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);
VerifyOrReturnError(static_cast<size_t>(state_index) < mGroupStatesCount, CHIP_ERROR_KEY_NOT_FOUND);
Expand Down Expand Up @@ -516,13 +531,15 @@ class StaticGroupsProvider : public GroupDataProvider
void Release(AllGroupStateIterator * iterator) { mAllStateIterators.ReleaseObject(iterator); }

// Keys
CHIP_ERROR SetKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index, KeySet & keys) override
CHIP_ERROR SetKeySet(chip::FabricIndex fabric_index, uint16_t key_set_index, const KeySet & keys) override
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);

Fabric * fabric = GetExistingFabric(fabric_index);
Fabric * fabric = GetExistingFabricOrAllocateNew(fabric_index);
KeysEntry * entry = nullptr;

VerifyOrReturnError(fabric, CHIP_ERROR_INVALID_FABRIC_ID);

// Search for existing, or unused entry
for (uint16_t i = 0; fabric && i < kKeyEntriesMax; ++i)
{
Expand Down Expand Up @@ -557,6 +574,8 @@ class StaticGroupsProvider : public GroupDataProvider
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);
Fabric * fabric = GetExistingFabric(fabric_index);
VerifyOrReturnError(fabric, CHIP_ERROR_INVALID_FABRIC_ID);

// Search for existing keys
for (uint16_t i = 0; fabric && i < kKeyEntriesMax; ++i)
{
Expand All @@ -577,6 +596,8 @@ class StaticGroupsProvider : public GroupDataProvider
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INTERNAL);
Fabric * fabric = GetExistingFabric(fabric_index);
VerifyOrReturnError(fabric, CHIP_ERROR_INVALID_FABRIC_ID);

// Search for existing keys
for (uint16_t i = 0; fabric && i < kKeyEntriesMax; ++i)
{
Expand All @@ -599,6 +620,23 @@ class StaticGroupsProvider : public GroupDataProvider

void Release(KeysIterator * iterator) { return mKeyIterators.ReleaseObject(iterator); }

CHIP_ERROR RemoveFabric(chip::FabricIndex fabric_index) override
{
Fabric * fabric = GetExistingFabric(fabric_index);
VerifyOrReturnError(fabric, CHIP_ERROR_INVALID_FABRIC_ID);
// Remove group states
for (size_t i = 0; i < kMaxNumGroupStates; ++i)
{
if (mGroupStates[i].fabric == fabric)
{
RemoveGroupState(i);
}
}
// Release fabric entry
fabric->Clear();
return CHIP_NO_ERROR;
}

private:
bool mInitialized = false;
Fabric mFabrics[kNumFabrics];
Expand Down
Loading

0 comments on commit 8f9e56d

Please sign in to comment.