Skip to content

Commit

Permalink
Disallow registering conflicting attribute access overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Sep 15, 2021
1 parent a1dc89e commit 8bdbe09
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/app/AttributeAccessInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ class AttributeAccessInterface
*/
bool MatchesExactly(EndpointId aEndpointId) const { return mEndpointId.HasValue() && mEndpointId.Value() == aEndpointId; }

/**
* Check whether another AttributeAccessInterface wants to handle the same set of
* attributes as we do.
*/
bool Matches(const AttributeAccessInterface & aOther) const
{
return mClusterId == aOther.mClusterId &&
(!mEndpointId.HasValue() || !aOther.mEndpointId.HasValue() || mEndpointId.Value() == aOther.mEndpointId.Value());
}

private:
Optional<EndpointId> mEndpointId;
ClusterId mClusterId;
Expand Down
12 changes: 11 additions & 1 deletion src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "app/util/common.h"
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/support/logging/CHIPLogging.h>

#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/callback.h>
Expand Down Expand Up @@ -1444,10 +1445,19 @@ bool emberAfExtractCommandIds(bool outgoing, EmberAfClusterCommand * cmd, Cluste
}
#endif

void registerAttributeAccessOverride(app::AttributeAccessInterface * attrOverride)
bool registerAttributeAccessOverride(app::AttributeAccessInterface * attrOverride)
{
for (auto * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext())
{
if (cur->Matches(*attrOverride))
{
ChipLogError(Zcl, "Duplicate attribute override registration failed");
return false;
}
}
attrOverride->SetNext(gAttributeAccessOverrides);
gAttributeAccessOverrides = attrOverride;
return true;
}

app::AttributeAccessInterface * findAttributeAccessOverride(EndpointId endpointId, ClusterId clusterId)
Expand Down
9 changes: 7 additions & 2 deletions src/app/util/attribute-storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,14 @@ uint16_t emberAfGetDynamicIndexFromEndpoint(chip::EndpointId id);
/**
* 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).
* registered for all endpoints). 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.
*/
void registerAttributeAccessOverride(chip::app::AttributeAccessInterface * attrOverride);
bool registerAttributeAccessOverride(chip::app::AttributeAccessInterface * attrOverride);

/**
* Find an attribute access override, if any, that is registered for the given
Expand Down

0 comments on commit 8bdbe09

Please sign in to comment.