diff --git a/src/app/clusters/group-key-mgmt-server/group-key-mgmt-server.cpp b/src/app/clusters/group-key-mgmt-server/group-key-mgmt-server.cpp index 9878058eada76e..359176876220a3 100644 --- a/src/app/clusters/group-key-mgmt-server/group-key-mgmt-server.cpp +++ b/src/app/clusters/group-key-mgmt-server/group-key-mgmt-server.cpp @@ -84,15 +84,12 @@ struct GroupTableCodec TLV::TLVType inner; ReturnErrorOnFailure(writer.StartContainer(TagEndpoints(), TLV::kTLVType_Array, inner)); GroupDataProvider::GroupEndpoint mapping; - auto iter = mProvider->IterateEndpoints(mFabric); + auto iter = mProvider->IterateEndpoints(mFabric, MakeOptional(mInfo.group_id)); if (nullptr != iter) { while (iter->Next(mapping)) { - if (mapping.group_id == mInfo.group_id) - { - ReturnErrorOnFailure(writer.Put(TLV::AnonymousTag(), static_cast(mapping.endpoint_id))); - } + ReturnErrorOnFailure(writer.Put(TLV::AnonymousTag(), static_cast(mapping.endpoint_id))); } iter->Release(); } diff --git a/src/credentials/GroupDataProvider.h b/src/credentials/GroupDataProvider.h index 30d519d9b83dee..ca599296f6a659 100644 --- a/src/credentials/GroupDataProvider.h +++ b/src/credentials/GroupDataProvider.h @@ -249,10 +249,11 @@ class GroupDataProvider * Creates an iterator that may be used to obtain the list of (group, endpoint) pairs associated with the given fabric. * In order to release the allocated memory, the Release() method must be called after the iteration is finished. * Modifying the group table during the iteration is currently not supported, and may yield unexpected behaviour. + * If you wish to iterate only the endpoints of a particular group id you can provide the optional `group_id` to do so. * @retval An instance of EndpointIterator on success * @retval nullptr if no iterator instances are available. */ - virtual EndpointIterator * IterateEndpoints(FabricIndex fabric_index) = 0; + virtual EndpointIterator * IterateEndpoints(FabricIndex fabric_index, Optional group_id = NullOptional) = 0; // // Group-Key map diff --git a/src/credentials/GroupDataProviderImpl.cpp b/src/credentials/GroupDataProviderImpl.cpp index 36e9baaef398aa..90016d7d2f1085 100644 --- a/src/credentials/GroupDataProviderImpl.cpp +++ b/src/credentials/GroupDataProviderImpl.cpp @@ -1207,28 +1207,43 @@ void GroupDataProviderImpl::GroupInfoIteratorImpl::Release() mProvider.mGroupInfoIterators.ReleaseObject(this); } -GroupDataProvider::EndpointIterator * GroupDataProviderImpl::IterateEndpoints(chip::FabricIndex fabric_index) +GroupDataProvider::EndpointIterator * GroupDataProviderImpl::IterateEndpoints(chip::FabricIndex fabric_index, + Optional group_id) { VerifyOrReturnError(IsInitialized(), nullptr); - return mEndpointIterators.CreateObject(*this, fabric_index); + return mEndpointIterators.CreateObject(*this, fabric_index, group_id); } -GroupDataProviderImpl::EndpointIteratorImpl::EndpointIteratorImpl(GroupDataProviderImpl & provider, - chip::FabricIndex fabric_index) : +GroupDataProviderImpl::EndpointIteratorImpl::EndpointIteratorImpl(GroupDataProviderImpl & provider, chip::FabricIndex fabric_index, + Optional group_id) : mProvider(provider), mFabric(fabric_index) { FabricData fabric(fabric_index); VerifyOrReturn(CHIP_NO_ERROR == fabric.Load(provider.mStorage)); - GroupData group(fabric_index, fabric.first_group); - VerifyOrReturn(CHIP_NO_ERROR == group.Load(provider.mStorage)); + if (group_id.HasValue()) + { + GroupData group(fabric_index, group_id.Value()); + VerifyOrReturn(CHIP_NO_ERROR == group.Load(provider.mStorage)); + + mGroup = group_id.Value(); + mFirstGroup = group_id.Value(); + mGroupCount = 1; + mEndpoint = group.first_endpoint; + mEndpointCount = group.endpoint_count; + } + else + { + GroupData group(fabric_index, fabric.first_group); + VerifyOrReturn(CHIP_NO_ERROR == group.Load(provider.mStorage)); - mGroup = fabric.first_group; - mFirstGroup = fabric.first_group; - mGroupCount = fabric.group_count; - mEndpoint = group.first_endpoint; - mEndpointCount = group.endpoint_count; + mGroup = fabric.first_group; + mFirstGroup = fabric.first_group; + mGroupCount = fabric.group_count; + mEndpoint = group.first_endpoint; + mEndpointCount = group.endpoint_count; + } } size_t GroupDataProviderImpl::EndpointIteratorImpl::Count() diff --git a/src/credentials/GroupDataProviderImpl.h b/src/credentials/GroupDataProviderImpl.h index 5c1d0d67e3417f..fad1d87471bc4a 100644 --- a/src/credentials/GroupDataProviderImpl.h +++ b/src/credentials/GroupDataProviderImpl.h @@ -68,7 +68,7 @@ class GroupDataProviderImpl : public GroupDataProvider CHIP_ERROR RemoveEndpoint(FabricIndex fabric_index, EndpointId endpoint_id) override; // Iterators GroupInfoIterator * IterateGroupInfo(FabricIndex fabric_index) override; - EndpointIterator * IterateEndpoints(FabricIndex fabric_index) override; + EndpointIterator * IterateEndpoints(FabricIndex fabric_index, Optional group_id = NullOptional) override; // // Group-Key map @@ -133,7 +133,7 @@ class GroupDataProviderImpl : public GroupDataProvider class EndpointIteratorImpl : public EndpointIterator { public: - EndpointIteratorImpl(GroupDataProviderImpl & provider, FabricIndex fabric_index); + EndpointIteratorImpl(GroupDataProviderImpl & provider, FabricIndex fabric_index, Optional group_id); size_t Count() override; bool Next(GroupEndpoint & output) override; void Release() override;