Skip to content

Commit

Permalink
GroupDataProvider: DEBUG IN PROGRESS.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasallas-silabs committed Mar 27, 2023
1 parent fb41c57 commit 86c9ee3
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 215 deletions.
2 changes: 1 addition & 1 deletion src/credentials/GroupDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class GroupDataProvider
// return *this;
// }

bool operator==(const GroupInfo & other)
bool operator==(const GroupInfo & other) const
{
return (this->group_id == other.group_id) && !strncmp(this->name, other.name, kGroupNameMax);
}
Expand Down
133 changes: 59 additions & 74 deletions src/credentials/GroupDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,89 +43,82 @@ enum class GroupTags : uint8_t
kName,
};

void FabricEntry::Clear()
void FabricList::UpdateKey(StorageKeyName & key)
{
key = DefaultStorageKeyAllocator::GroupFabricList();
}

void FabricList::Clear(FabricIndex & entry)
{
index = kUndefinedFabricIndex;
entry = kUndefinedFabricIndex;
}

CHIP_ERROR FabricEntry::Serialize(TLV::TLVWriter & writer) const
CHIP_ERROR FabricList::Serialize(TLV::TLVWriter & writer, const FabricIndex & entry) const
{
// Fabric
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(GroupTags::kFabric), static_cast<uint16_t>(index)));
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(GroupTags::kFabric), static_cast<uint16_t>(entry)));
return CHIP_NO_ERROR;
}

CHIP_ERROR FabricEntry::Deserialize(TLV::TLVReader & reader)
CHIP_ERROR FabricList::Deserialize(TLV::TLVReader & reader, FabricIndex & entry)
{
// Fabric
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(GroupTags::kFabric)));
ReturnErrorOnFailure(reader.Get(index));
ReturnErrorOnFailure(reader.Get(entry));
return CHIP_NO_ERROR;
}

void FabricList::UpdateKey(StorageKeyName & key)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void GroupList::UpdateKey(StorageKeyName & key)
{
key = DefaultStorageKeyAllocator::GroupFabricList();
key = DefaultStorageKeyAllocator::FabricGroups(this->fabric);
}

bool FabricList::Compare(const FabricEntry & a, const FabricEntry & b) const
void GroupList::Clear(GroupDataProvider::GroupInfo & entry)
{
return a.index == b.index;
entry.group_id = kUndefinedGroupId;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void GroupEntry::Clear()
bool GroupList::Compare(const GroupDataProvider::GroupInfo & a, const GroupDataProvider::GroupInfo & b) const
{
group_id = kUndefinedGroupId;
return a.group_id == b.group_id;
}

CHIP_ERROR GroupEntry::Serialize(TLV::TLVWriter & writer) const
CHIP_ERROR GroupList::Serialize(TLV::TLVWriter & writer, const GroupDataProvider::GroupInfo & entry) const
{
// group_id
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(GroupTags::kGroup), static_cast<uint16_t>(this->group_id)));
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(GroupTags::kGroup), static_cast<uint16_t>(entry.group_id)));
// name
size_t name_size = strnlen(this->name, GroupDataProvider::GroupInfo::kGroupNameMax);
ReturnErrorOnFailure(writer.PutString(TLV::ContextTag(GroupTags::kName), this->name, static_cast<uint32_t>(name_size)));
size_t name_size = strnlen(entry.name, GroupDataProvider::GroupInfo::kGroupNameMax);
ReturnErrorOnFailure(writer.PutString(TLV::ContextTag(GroupTags::kName), entry.name, static_cast<uint32_t>(name_size)));
return CHIP_NO_ERROR;
}

CHIP_ERROR GroupEntry::Deserialize(TLV::TLVReader & reader)
CHIP_ERROR GroupList::Deserialize(TLV::TLVReader & reader, GroupDataProvider::GroupInfo & entry)
{
// group_id
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(GroupTags::kGroup)));
ReturnErrorOnFailure(reader.Get(this->group_id));
ReturnErrorOnFailure(reader.Get(entry.group_id));
// name
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(GroupTags::kName)));
ReturnErrorOnFailure(reader.GetString(this->name, sizeof(this->name)));
size_t size = strnlen(this->name, GroupDataProvider::GroupInfo::kGroupNameMax);
this->name[size] = 0;
ReturnErrorOnFailure(reader.GetString(entry.name, sizeof(entry.name)));
size_t size = strnlen(entry.name, GroupDataProvider::GroupInfo::kGroupNameMax);
entry.name[size] = 0;
return CHIP_NO_ERROR;
}

void GroupList::UpdateKey(StorageKeyName & key)
{
key = DefaultStorageKeyAllocator::FabricGroups(this->fabric.id);
}

bool GroupList::Compare(const GroupEntry & a, const GroupEntry & b) const
{
return a.group_id == b.group_id;
}

void GroupList::OnEntryAdded(const GroupEntry & entry)
void GroupList::OnEntryAdded(const GroupDataProvider::GroupInfo & entry)
{
if (_listener)
{
_listener->OnGroupAdded(fabric.index, entry);
// _listener->OnGroupAdded(fabric.index, entry);
}
}

void GroupList::OnEntryRemoved(const GroupEntry & entry)
void GroupList::OnEntryRemoved(const GroupDataProvider::GroupInfo & entry)
{
if (_listener)
{
_listener->OnGroupRemoved(fabric.index, entry);
// _listener->OnGroupRemoved(fabric.index, entry);
}
}

Expand Down Expand Up @@ -190,12 +183,11 @@ CHIP_ERROR GroupDataProviderImpl::SetGroupInfo(FabricIndex fabric_index, const G
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricList fabrics(mStorage);
FabricEntry fabric(fabric_index);
ReturnErrorOnFailure(fabrics.Get(fabric, true));
PersistentId fid = 0;
ReturnErrorOnFailure(fabrics.Get(fabric_index, fid, true));

GroupList groups(mStorage, mListener, fabric);
GroupEntry group(info.group_id, info.name);
return groups.Set(group);
GroupList groups(mStorage, mListener, fid);
return groups.Set(info, true);
}

CHIP_ERROR GroupDataProviderImpl::GetGroupInfo(FabricIndex fabric_index, GroupId group_id, GroupInfo & info)
Expand All @@ -204,15 +196,13 @@ CHIP_ERROR GroupDataProviderImpl::GetGroupInfo(FabricIndex fabric_index, GroupId
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricList fabrics(mStorage);
FabricEntry fabric(fabric_index);
ReturnErrorOnFailure(fabrics.Get(fabric));
PersistentId fid = 0;
ReturnErrorOnFailure(fabrics.Get(fabric_index, fid));

GroupList groups(mStorage, mListener, fabric);
GroupEntry group(info.group_id);
ReturnErrorOnFailure(groups.Get(group));

info.group_id = group.group_id;
info.SetName(group.name);
GroupList groups(mStorage, mListener, fid);
PersistentId gid = 0;
info.group_id = group_id;
ReturnErrorOnFailure(groups.Get(info, gid));
return CHIP_NO_ERROR;
}

Expand All @@ -222,12 +212,12 @@ CHIP_ERROR GroupDataProviderImpl::RemoveGroupInfo(FabricIndex fabric_index, Grou
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricList fabrics(mStorage);
FabricEntry fabric(fabric_index);
ReturnErrorOnFailure(fabrics.Get(fabric));
PersistentId fid = 0;
ReturnErrorOnFailure(fabrics.Get(fabric_index, fid));

GroupList groups(mStorage, mListener, fabric);
GroupEntry group(group_id);
return groups.Remove(group);
GroupList groups(mStorage, mListener, fid);
GroupInfo info(group_id, nullptr);
return groups.Remove(info);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -239,12 +229,11 @@ CHIP_ERROR GroupDataProviderImpl::SetGroupInfoAt(FabricIndex fabric_index, size_
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricList fabrics(mStorage);
FabricEntry fabric(fabric_index);
ReturnErrorOnFailure(fabrics.Get(fabric, true));
PersistentId fid = 0;
ReturnErrorOnFailure(fabrics.Get(fabric_index, fid, true));

GroupList groups(mStorage, mListener, fabric);
GroupEntry group(info.group_id, info.name);
return groups.Set(index, group);
GroupList groups(mStorage, mListener, fid);
return groups.Set(index, info);
}

CHIP_ERROR GroupDataProviderImpl::GetGroupInfoAt(FabricIndex fabric_index, size_t index, GroupInfo & info)
Expand All @@ -253,15 +242,12 @@ CHIP_ERROR GroupDataProviderImpl::GetGroupInfoAt(FabricIndex fabric_index, size_
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricList fabrics(mStorage);
FabricEntry fabric(fabric_index);
ReturnErrorOnFailure(fabrics.Get(fabric));

GroupList groups(mStorage, mListener, fabric);
GroupEntry group;
ReturnErrorOnFailure(groups.Get(index, group));
PersistentId fid = 0;
ReturnErrorOnFailure(fabrics.Get(fabric_index, fid));

info.group_id = group.group_id;
info.SetName(group.name);
GroupList groups(mStorage, mListener, fid);
PersistentId gid = 0;
ReturnErrorOnFailure(groups.Get(index, info, gid));
return CHIP_NO_ERROR;
}

Expand All @@ -271,14 +257,13 @@ CHIP_ERROR GroupDataProviderImpl::RemoveGroupInfoAt(FabricIndex fabric_index, si
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

FabricList fabrics(mStorage);
FabricEntry fabric(fabric_index);
ReturnErrorOnFailure(fabrics.Get(fabric));
PersistentId fid = 0;
ReturnErrorOnFailure(fabrics.Get(fabric_index, fid));

GroupList groups(mStorage, mListener, fabric);
GroupList groups(mStorage, mListener, fid);
return groups.Remove(index);
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Endpoints

Expand Down
73 changes: 18 additions & 55 deletions src/credentials/GroupDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,72 +31,35 @@ constexpr size_t kPersistentBufferMax = 1024;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

struct FabricEntry: public PersistentEntry {
struct FabricList: public PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, FabricIndex> {

FabricEntry() = default;
FabricEntry(FabricIndex index): index(index) {}
void Clear() override;
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override;
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override;

FabricIndex index = kUndefinedFabricIndex;
};

struct FabricList: public PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, FabricEntry> {

FabricList(PersistentStorageDelegate * storage): PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, FabricEntry>(storage) {}
FabricList(PersistentStorageDelegate * storage): PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, FabricIndex>(storage) {}
void UpdateKey(StorageKeyName & key) override;
bool Compare(const FabricEntry & a, const FabricEntry & b) const override;

void Clear(FabricIndex & entry) override;
CHIP_ERROR Serialize(TLV::TLVWriter & writer, const FabricIndex & entry) const override;
CHIP_ERROR Deserialize(TLV::TLVReader & reader, FabricIndex & entry) override;
};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

struct GroupEntry: public PersistentEntry, GroupDataProvider::GroupInfo {

GroupEntry() = default;
GroupEntry(GroupId gid): GroupDataProvider::GroupInfo(gid, nullptr) {}
GroupEntry(GroupId gid, const char * name): GroupDataProvider::GroupInfo(gid, name) {}
GroupEntry(const GroupEntry & info): GroupEntry(info.group_id, info.name) {}
// GroupEntry(const GroupDataProvider::GroupInfo & info): GroupDataProvider::GroupInfo(info.group_id, info.name) {}

// GroupEntry() = default;
// GroupEntry(GroupId id): GroupDataProvider::GroupInfo(id, nullptr) {}
// GroupEntry(const GroupEntry & entry): PersistentEntry(entry.id), GroupDataProvider::GroupInfo(entry.group_id, entry.name) {}

void Clear() override;
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override;
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override;

// GroupEntry& operator=(const GroupEntry & other)
// {
// if (this != &other) // not a self-assignment
// {
// id = other.id;
// group_id = other.group_id;
// SetName(other.name);
// }
// return *this;
// }
};

struct GroupList: public PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, GroupEntry> {
struct GroupList: public PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, GroupDataProvider::GroupInfo> {

GroupList(PersistentStorageDelegate * storage,
GroupDataProvider::GroupListener * listener,
FabricEntry & fabric):
PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, GroupEntry>(storage),
PersistentId fabric):
PersistentArray<kPersistentBufferMax, CHIP_CONFIG_MAX_FABRICS, GroupDataProvider::GroupInfo>(storage),
fabric(fabric), _listener(listener) {}
void UpdateKey(StorageKeyName & key) override;
bool Compare(const GroupEntry & a, const GroupEntry & b) const override;
void OnEntryAdded(const GroupEntry & entry) override;
void OnEntryRemoved(const GroupEntry & entry) override;

// bool operator==(const GroupInfo & other)
// {
// return (this->group_id == other.group_id) && !strncmp(this->name, other.name, kGroupNameMax);
// }

FabricEntry & fabric;
void UpdateKey(StorageKeyName & key) override;
void Clear(GroupDataProvider::GroupInfo & entry) override;
bool Compare(const GroupDataProvider::GroupInfo & a, const GroupDataProvider::GroupInfo & b) const override;
CHIP_ERROR Serialize(TLV::TLVWriter & writer, const GroupDataProvider::GroupInfo & entry) const override;
CHIP_ERROR Deserialize(TLV::TLVReader & reader, GroupDataProvider::GroupInfo & entry) override;
void OnEntryAdded(const GroupDataProvider::GroupInfo & entry) override;
void OnEntryRemoved(const GroupDataProvider::GroupInfo & entry) override;

PersistentId fabric;
private:
GroupDataProvider::GroupListener *_listener = nullptr;
};
Expand Down
Loading

0 comments on commit 86c9ee3

Please sign in to comment.