Skip to content

Commit 07f4b35

Browse files
Separate out attribute access interface registry/cache, remove free functions from InteractionModelEngine.h (#32954)
* Move GetAttributeAccessOverride to the right place * A first pass at decoupling the AttributeAccessOverride * Start adding a bit of registry, update attribute-storage.h * Add the registry include everywhere it was used to register things * Restyle * More definition and usage cleanup * Add include in ember compatibility layer * Cleanup a TODO * Fix linter too * Fix dynamic dispatcher compile * Darwin connectivity manager does not use TLV * Fix include dependency for ESP32 * Remove usage of chip::tlv from connectivity manager: no TLV is used and this was just copy and paste * Fix tizen includes: if you need Wifi enums, cluster-enums.h need including * Fix esp32 build * Fix mbed compile: its impl header had no includes for some reason * Add needed include to InteractiveServer.cpp to fix darwin build * Also include vectore because we use it * Also fix boufallolab includes * Update src/darwin/Framework/CHIP/ServerEndpoint/MTRServerCluster.mm Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Apply rename suggestion for unregisterAll...ForEndpoint * Apply code review comment: log category switch --------- Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent ce52971 commit 07f4b35

File tree

133 files changed

+337
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+337
-194
lines changed

.github/workflows/lint.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ jobs:
9696
--known-failure app/app-platform/ContentAppPlatform.h \
9797
--known-failure controller/ExamplePersistentStorage.cpp \
9898
--known-failure controller/ExamplePersistentStorage.h \
99-
--known-failure app/AttributeAccessInterface.h \
10099
--known-failure app/AttributeAccessToken.h \
101100
--known-failure app/CommandHandler.h \
102101
--known-failure app/CommandHandlerInterface.h \
@@ -107,11 +106,11 @@ jobs:
107106
--known-failure app/reporting/tests/MockReportScheduler.cpp \
108107
--known-failure app/reporting/tests/MockReportScheduler.h \
109108
--known-failure app/util/attribute-storage.cpp \
110-
--known-failure app/util/attribute-storage.h \
111109
--known-failure app/util/attribute-storage-detail.h \
110+
--known-failure app/util/attribute-storage.h \
112111
--known-failure app/util/attribute-table.cpp \
113-
--known-failure app/util/attribute-table.h \
114112
--known-failure app/util/attribute-table-detail.h \
113+
--known-failure app/util/attribute-table.h \
115114
--known-failure app/util/binding-table.cpp \
116115
--known-failure app/util/binding-table.h \
117116
--known-failure app/util/config.h \
@@ -123,6 +122,7 @@ jobs:
123122
--known-failure app/util/generic-callbacks.h \
124123
--known-failure app/util/generic-callback-stubs.cpp \
125124
--known-failure app/util/im-client-callbacks.h \
125+
--known-failure app/util/IMClusterCommandHandler.h \
126126
--known-failure app/util/util.cpp \
127127
--known-failure app/util/util.h \
128128
--known-failure app/WriteHandler.h \

examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <app-common/zap-generated/ids/Attributes.h>
2020
#include <app-common/zap-generated/ids/Clusters.h>
2121
#include <app/AttributeAccessInterface.h>
22+
#include <app/AttributeAccessInterfaceRegistry.h>
2223
#include <app/util/attribute-storage.h>
2324
#include <lib/support/CodeUtils.h>
2425
#include <lib/support/logging/CHIPLogging.h>

examples/all-clusters-app/all-clusters-common/src/fan-stub.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <app-common/zap-generated/ids/Attributes.h>
2121
#include <app-common/zap-generated/ids/Clusters.h>
2222
#include <app/AttributeAccessInterface.h>
23+
#include <app/AttributeAccessInterfaceRegistry.h>
2324
#include <app/clusters/fan-control-server/fan-control-server.h>
2425
#include <app/util/attribute-storage.h>
2526
#include <lib/support/CodeUtils.h>

examples/bridge-app/asr/src/bridged-actions-stub.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <app-common/zap-generated/ids/Attributes.h>
1919
#include <app-common/zap-generated/ids/Clusters.h>
2020
#include <app/AttributeAccessInterface.h>
21+
#include <app/AttributeAccessInterfaceRegistry.h>
2122
#include <app/util/attribute-storage.h>
2223
#include <lib/support/CodeUtils.h>
2324
#include <lib/support/logging/CHIPLogging.h>

examples/bridge-app/esp32/main/DeviceCallbacks.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <app-common/zap-generated/ids/Attributes.h>
2121
#include <app-common/zap-generated/ids/Clusters.h>
2222
#include <app/AttributeAccessInterface.h>
23+
#include <app/AttributeAccessInterfaceRegistry.h>
2324
#include <app/util/attribute-storage.h>
2425
#include <lib/support/CodeUtils.h>
2526
#include <lib/support/logging/CHIPLogging.h>

examples/bridge-app/linux/bridged-actions-stub.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <app-common/zap-generated/ids/Attributes.h>
2020
#include <app-common/zap-generated/ids/Clusters.h>
2121
#include <app/AttributeAccessInterface.h>
22+
#include <app/AttributeAccessInterfaceRegistry.h>
2223
#include <app/util/attribute-storage.h>
2324
#include <lib/support/CodeUtils.h>
2425
#include <lib/support/logging/CHIPLogging.h>

examples/bridge-app/linux/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <app-common/zap-generated/ids/Attributes.h>
2424
#include <app-common/zap-generated/ids/Clusters.h>
25+
#include <app/AttributeAccessInterfaceRegistry.h>
2526
#include <app/ConcreteAttributePath.h>
2627
#include <app/EventLogging.h>
2728
#include <app/reporting/reporting.h>

examples/bridge-app/telink/src/DeviceCallbacks.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <app-common/zap-generated/ids/Attributes.h>
2121
#include <app-common/zap-generated/ids/Clusters.h>
2222
#include <app/AttributeAccessInterface.h>
23+
#include <app/AttributeAccessInterfaceRegistry.h>
2324
#include <app/util/attribute-storage.h>
2425
#include <lib/support/CodeUtils.h>
2526
#include <lib/support/logging/CHIPLogging.h>

examples/chef/common/chef-fan-control-manager.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <app-common/zap-generated/ids/Attributes.h>
2121
#include <app-common/zap-generated/ids/Clusters.h>
2222
#include <app/AttributeAccessInterface.h>
23+
#include <app/AttributeAccessInterfaceRegistry.h>
2324
#include <app/clusters/fan-control-server/fan-control-server.h>
2425
#include <app/util/attribute-storage.h>
2526
#include <lib/support/CodeUtils.h>

examples/placeholder/linux/InteractiveServer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#include "InteractiveServer.h"
2020

2121
#include <json/json.h>
22+
#include <lib/core/CHIPSafeCasts.h>
2223
#include <lib/support/Base64.h>
2324
#include <platform/CHIPDeviceLayer.h>
2425
#include <platform/logging/LogV.h>
2526

2627
#include <string>
28+
#include <vector>
2729

2830
using namespace chip::DeviceLayer;
2931

examples/placeholder/linux/src/bridged-actions-stub.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <app-common/zap-generated/ids/Attributes.h>
2020
#include <app-common/zap-generated/ids/Clusters.h>
2121
#include <app/AttributeAccessInterface.h>
22+
#include <app/AttributeAccessInterfaceRegistry.h>
2223
#include <app/util/attribute-storage.h>
2324
#include <lib/support/CodeUtils.h>
2425
#include <lib/support/logging/CHIPLogging.h>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <app/AttributeAccessInterfaceRegistry.h>
17+
18+
#include <app/AttributeAccessInterfaceCache.h>
19+
20+
using namespace chip::app;
21+
22+
namespace {
23+
24+
AttributeAccessInterface * gAttributeAccessOverrides = nullptr;
25+
AttributeAccessInterfaceCache gAttributeAccessInterfaceCache;
26+
27+
// shouldUnregister returns true if the given AttributeAccessInterface should be
28+
// unregistered.
29+
template <typename F>
30+
void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister)
31+
{
32+
AttributeAccessInterface * prev = nullptr;
33+
AttributeAccessInterface * cur = gAttributeAccessOverrides;
34+
while (cur)
35+
{
36+
AttributeAccessInterface * next = cur->GetNext();
37+
if (shouldUnregister(cur))
38+
{
39+
// Remove it from the list
40+
if (prev)
41+
{
42+
prev->SetNext(next);
43+
}
44+
else
45+
{
46+
gAttributeAccessOverrides = next;
47+
}
48+
49+
cur->SetNext(nullptr);
50+
51+
// Do not change prev in this case.
52+
}
53+
else
54+
{
55+
prev = cur;
56+
}
57+
cur = next;
58+
}
59+
}
60+
61+
} // namespace
62+
63+
void unregisterAttributeAccessOverride(AttributeAccessInterface * attrOverride)
64+
{
65+
gAttributeAccessInterfaceCache.Invalidate();
66+
UnregisterMatchingAttributeAccessInterfaces([attrOverride](AttributeAccessInterface * entry) { return entry == attrOverride; });
67+
}
68+
69+
void unregisterAllAttributeAccessOverridesForEndpoint(EmberAfDefinedEndpoint * definedEndpoint)
70+
{
71+
UnregisterMatchingAttributeAccessInterfaces(
72+
[endpoint = definedEndpoint->endpoint](AttributeAccessInterface * entry) { return entry->MatchesEndpoint(endpoint); });
73+
}
74+
75+
bool registerAttributeAccessOverride(AttributeAccessInterface * attrOverride)
76+
{
77+
gAttributeAccessInterfaceCache.Invalidate();
78+
for (auto * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext())
79+
{
80+
if (cur->Matches(*attrOverride))
81+
{
82+
ChipLogError(InteractionModel, "Duplicate attribute override registration failed");
83+
return false;
84+
}
85+
}
86+
attrOverride->SetNext(gAttributeAccessOverrides);
87+
gAttributeAccessOverrides = attrOverride;
88+
return true;
89+
}
90+
91+
namespace chip {
92+
namespace app {
93+
94+
app::AttributeAccessInterface * GetAttributeAccessOverride(EndpointId endpointId, ClusterId clusterId)
95+
{
96+
using CacheResult = AttributeAccessInterfaceCache::CacheResult;
97+
98+
AttributeAccessInterface * cached = nullptr;
99+
CacheResult result = gAttributeAccessInterfaceCache.Get(endpointId, clusterId, &cached);
100+
switch (result)
101+
{
102+
case CacheResult::kDefinitelyUnused:
103+
return nullptr;
104+
case CacheResult::kDefinitelyUsed:
105+
return cached;
106+
case CacheResult::kCacheMiss:
107+
default:
108+
// Did not cache yet, search set of AAI registered, and cache if found.
109+
for (app::AttributeAccessInterface * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext())
110+
{
111+
if (cur->Matches(endpointId, clusterId))
112+
{
113+
gAttributeAccessInterfaceCache.MarkUsed(endpointId, clusterId, cur);
114+
return cur;
115+
}
116+
}
117+
118+
// Did not find AAI registered: mark as definitely not using.
119+
gAttributeAccessInterfaceCache.MarkUnused(endpointId, clusterId);
120+
}
121+
122+
return nullptr;
123+
}
124+
125+
} // namespace app
126+
} // namespace chip
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <app/AttributeAccessInterface.h>
19+
#include <app/util/af-types.h>
20+
21+
/**
22+
* Register an attribute access override. It will remain registered until the
23+
* endpoint it's registered for is disabled (or until shutdown if it's
24+
* registered for all endpoints) or until it is explicitly unregistered.
25+
* Registration will fail if there is an already-registered override for the
26+
* same set of attributes.
27+
*
28+
* @return false if there is an existing override that the new one would
29+
* conflict with. In this case the override is not registered.
30+
* @return true if registration was successful.
31+
*/
32+
bool registerAttributeAccessOverride(chip::app::AttributeAccessInterface * attrOverride);
33+
34+
/**
35+
* Unregister an attribute access override (for example if the object
36+
* implementing AttributeAccessInterface is being destroyed).
37+
*/
38+
void unregisterAttributeAccessOverride(chip::app::AttributeAccessInterface * attrOverride);
39+
40+
/**
41+
* Unregister all attribute access interfaces that match this given endpoint.
42+
*/
43+
void unregisterAllAttributeAccessOverridesForEndpoint(EmberAfDefinedEndpoint * definedEndpoint);
44+
45+
namespace chip {
46+
namespace app {
47+
48+
/**
49+
* Get the registered attribute access override. nullptr when attribute access override is not found.
50+
*/
51+
AttributeAccessInterface * GetAttributeAccessOverride(EndpointId aEndpointId, ClusterId aClusterId);
52+
53+
} // namespace app
54+
} // namespace chip

src/app/BUILD.gn

+21-7
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,32 @@ source_set("events") {
275275
]
276276
}
277277

278+
static_library("attribute-access") {
279+
sources = [
280+
"AttributeAccessInterface.cpp",
281+
"AttributeAccessInterface.h",
282+
"AttributeAccessInterfaceCache.h",
283+
"AttributeAccessInterfaceRegistry.cpp",
284+
"AttributeAccessInterfaceRegistry.h",
285+
]
286+
287+
deps = [
288+
":paths",
289+
"${chip_root}/src/access:types",
290+
"${chip_root}/src/app/MessageDef",
291+
"${chip_root}/src/app/data-model",
292+
"${chip_root}/src/app/util:af-types",
293+
"${chip_root}/src/lib/core",
294+
"${chip_root}/src/lib/support",
295+
]
296+
}
297+
278298
# Note to developpers, instead of continuously adding files in the app librabry, it is recommand to create smaller source_sets that app can depend on.
279299
# This way, we can have a better understanding of dependencies and other componenets can depend on the different source_sets without needing to depend on the entire app library.
280300
static_library("app") {
281301
output_name = "libCHIPDataModel"
282302

283303
sources = [
284-
"AttributeAccessInterface.cpp",
285-
"AttributeAccessInterfaceCache.h",
286304
"AttributePathExpandIterator.cpp",
287305
"AttributePathExpandIterator.h",
288306
"AttributePersistenceProvider.h",
@@ -315,15 +333,11 @@ static_library("app") {
315333
# "CommandHandler._h"
316334
# "ReadHandler._h",
317335
# "WriteHandler._h"
318-
319-
# TODO: the following items cannot be included due to platform includes not being
320-
# able to depend on src/app
321-
# Name with _ so that linter does not recognize it
322-
# "_AttributeAccessInterface._h",
323336
]
324337

325338
public_deps = [
326339
":app_config",
340+
":attribute-access",
327341
":constants",
328342
":global-attributes",
329343
":interaction-model",

src/app/InteractionModelEngine.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "access/SubjectDescriptor.h"
3232
#include <app/AppConfig.h>
3333
#include <app/RequiredPrivilege.h>
34+
#include <app/util/IMClusterCommandHandler.h>
3435
#include <app/util/af-types.h>
3536
#include <app/util/ember-compatibility-functions.h>
3637
#include <app/util/endpoint-config-api.h>

src/app/InteractionModelEngine.h

-10
Original file line numberDiff line numberDiff line change
@@ -712,15 +712,5 @@ class InteractionModelEngine : public Messaging::UnsolicitedMessageHandler,
712712
uint32_t mMagic = 0;
713713
};
714714

715-
void DispatchSingleClusterCommand(const ConcreteCommandPath & aCommandPath, chip::TLV::TLVReader & aReader,
716-
CommandHandler * apCommandObj);
717-
718-
/**
719-
* Get the registered attribute access override. nullptr when attribute access override is not found.
720-
*
721-
* TODO(#16806): This function and registerAttributeAccessOverride can be member functions of InteractionModelEngine.
722-
*/
723-
AttributeAccessInterface * GetAttributeAccessOverride(EndpointId aEndpointId, ClusterId aClusterId);
724-
725715
} // namespace app
726716
} // namespace chip

src/app/WriteHandler.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "messaging/ExchangeContext.h"
2020
#include <app/AppConfig.h>
21+
#include <app/AttributeAccessInterfaceRegistry.h>
2122
#include <app/InteractionModelEngine.h>
2223
#include <app/MessageDef/EventPathIB.h>
2324
#include <app/StatusResponse.h>

src/app/clusters/access-control-server/access-control-server.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <app-common/zap-generated/cluster-objects.h>
2121

2222
#include <app/AttributeAccessInterface.h>
23+
#include <app/AttributeAccessInterfaceRegistry.h>
2324
#include <app/CommandHandler.h>
2425
#include <app/ConcreteCommandPath.h>
2526
#include <app/EventLogging.h>

src/app/clusters/account-login-server/account-login-server.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <app-common/zap-generated/cluster-objects.h>
2828
#include <app/AttributeAccessInterface.h>
29+
#include <app/AttributeAccessInterfaceRegistry.h>
2930
#include <app/CommandHandler.h>
3031
#include <app/ConcreteCommandPath.h>
3132
#include <app/EventLogging.h>

0 commit comments

Comments
 (0)