Skip to content

Commit

Permalink
Update logic for IsDescentantof
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Jan 17, 2025
1 parent 9447bfe commit 36e048e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/app/clusters/descriptor/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,31 @@ using namespace chip::app::Clusters::Descriptor::Attributes;
namespace {

/// Figures out if `childId` is a descendant of `parentId` given some specific endpoint entries
bool IsDescendantOf(EndpointId childId, const EndpointId parentId, Span<const DataModel::EndpointEntry> allEndpoints)
bool IsDescendantOf(const DataModel::EndpointEntry * childEndpoint, const EndpointId parentId,
Span<const DataModel::EndpointEntry> allEndpoints)
{
// NOTE: this is not very efficient, however most compositions should be of small depth
while (childId != kInvalidEndpointId)
// NOTE: this is not very efficient as we loop through all endpoints for each parent search
// however endpoint depth should not be as large.
while (true)
{

VerifyOrReturnValue(childEndpoint != nullptr, false);
VerifyOrReturnValue(childEndpoint->parentId != parentId, true);
VerifyOrReturnValue(childEndpoint->parentId != kInvalidEndpointId, false);

const auto lookupId = childEndpoint->parentId;
childEndpoint = nullptr; // we will look it up again

// find the requested value in the array to get its parent
for (auto & ep : allEndpoints)
{
if (ep.id != childId)
{
continue;
}

if (ep.parentId == parentId)
if (ep.id == lookupId)
{
return true;
childEndpoint = &ep;
break;
}
childId = ep.parentId; // see if parent is a descendant
}
}

return false;
}

class DescriptorAttrAccess : public AttributeAccessInterface
Expand Down Expand Up @@ -148,7 +151,7 @@ CHIP_ERROR DescriptorAttrAccess::ReadPartsAttribute(EndpointId endpoint, Attribu
return aEncoder.EncodeList([&endpoints, endpoint](const auto & encoder) -> CHIP_ERROR {
for (auto & ep : endpoints.GetSpanValidForLifetime())
{
if (IsDescendantOf(ep.id, endpoint, endpoints.GetSpanValidForLifetime()))
if (IsDescendantOf(&ep, endpoint, endpoints.GetSpanValidForLifetime()))
{
ReturnErrorOnFailure(encoder.Encode(ep.id));
}
Expand Down

0 comments on commit 36e048e

Please sign in to comment.