Skip to content

Commit

Permalink
Add a way to unregister AttributeAccessInterface instances. (#28075)
Browse files Browse the repository at this point in the history
Fixes #28072
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Nov 7, 2023
1 parent dae7c4b commit 1156840
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
73 changes: 44 additions & 29 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,42 @@ DataVersion fixedEndpointDataVersions[ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT];
#define endpointTypeMacro(x) (&(generatedEmberAfEndpointTypes[fixedEmberAfEndpointTypes[x]]))
#endif

app::AttributeAccessInterface * gAttributeAccessOverrides = nullptr;
AttributeAccessInterface * gAttributeAccessOverrides = nullptr;

// shouldUnregister returns true if the given AttributeAccessInterface should be
// unregistered.
template <typename F>
void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister)
{
AttributeAccessInterface * prev = nullptr;
AttributeAccessInterface * cur = gAttributeAccessOverrides;
while (cur)
{
AttributeAccessInterface * next = cur->GetNext();
if (shouldUnregister(cur))
{
// Remove it from the list
if (prev)
{
prev->SetNext(next);
}
else
{
gAttributeAccessOverrides = next;
}

cur->SetNext(nullptr);

// Do not change prev in this case.
}
else
{
prev = cur;
}
cur = next;
}
}

} // anonymous namespace

// Initial configuration
Expand Down Expand Up @@ -383,33 +418,8 @@ static void shutdownEndpoint(EmberAfDefinedEndpoint * definedEndpoint)

// Clear out any attribute access overrides registered for this
// endpoint.
app::AttributeAccessInterface * prev = nullptr;
app::AttributeAccessInterface * cur = gAttributeAccessOverrides;
while (cur)
{
app::AttributeAccessInterface * next = cur->GetNext();
if (cur->MatchesEndpoint(definedEndpoint->endpoint))
{
// Remove it from the list
if (prev)
{
prev->SetNext(next);
}
else
{
gAttributeAccessOverrides = next;
}

cur->SetNext(nullptr);

// Do not change prev in this case.
}
else
{
prev = cur;
}
cur = next;
}
UnregisterMatchingAttributeAccessInterfaces(
[endpoint = definedEndpoint->endpoint](AttributeAccessInterface * entry) { return entry->MatchesEndpoint(endpoint); });
}

// Calls the init functions.
Expand Down Expand Up @@ -1378,7 +1388,7 @@ EmberAfGenericClusterFunction emberAfFindClusterFunction(const EmberAfCluster *
return cluster->functions[functionIndex];
}

bool registerAttributeAccessOverride(app::AttributeAccessInterface * attrOverride)
bool registerAttributeAccessOverride(AttributeAccessInterface * attrOverride)
{
for (auto * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext())
{
Expand All @@ -1393,6 +1403,11 @@ bool registerAttributeAccessOverride(app::AttributeAccessInterface * attrOverrid
return true;
}

void unregisterAttributeAccessOverride(AttributeAccessInterface * attrOverride)
{
UnregisterMatchingAttributeAccessInterfaces([attrOverride](AttributeAccessInterface * entry) { return entry == attrOverride; });
}

namespace chip {
namespace app {
app::AttributeAccessInterface * GetAttributeAccessOverride(EndpointId endpointId, ClusterId clusterId)
Expand Down
15 changes: 11 additions & 4 deletions src/app/util/attribute-storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,20 @@ chip::Optional<chip::AttributeId> emberAfGetServerAttributeIdByIndex(chip::Endpo
uint16_t attributeIndex);

/**
* Register an attribute access override. It will remain registered until
* the endpoint it's registered for is disabled (or until shutdown if it's
* registered for all endpoints). Registration will fail if there is an
* already-registered override for the same set of attributes.
* Register an attribute access override. It will remain registered until the
* endpoint it's registered for is disabled (or until shutdown if it's
* registered for all endpoints) or until it is explicitly unregistered.
* Registration will fail if there is an already-registered override for the
* same set of attributes.
*
* @return false if there is an existing override that the new one would
* conflict with. In this case the override is not registered.
* @return true if registration was successful.
*/
bool registerAttributeAccessOverride(chip::app::AttributeAccessInterface * attrOverride);

/**
* Unregister an attribute access override (for example if the object
* implementing AttributeAccessInterface is being destroyed).
*/
void unregisterAttributeAccessOverride(chip::app::AttributeAccessInterface * attrOverride);

0 comments on commit 1156840

Please sign in to comment.