Skip to content

Commit b85f076

Browse files
andy31415andreilitvinbzbarsky-apple
authored
Make AttributeAccessInterface and CommandHandlerInterface registries consistent as objects with singletons (#34462)
* Switch CHI registry to an object * Switch CHI registry to an object * Prepare the AAI registry to use an object * Replace unregister * Replace the register * Update the getter * Restyle * Fix unregister * Fix typo * Fix namespacing for instance * Do not make IME auto-clear command handlers * Make spellcheck happy * Remove chip::app prefix throughout * Fix compile error * One more compile fix * One more compile fix * make tv-app compile * Update examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update examples/tv-app/android/java/AppImpl.cpp Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Update register/unregister names * Fix endpoint id usage that was awkward in aai * Restyle * Code simplification and some namespace removal * Fix comment * re-order of methods * One more move to preserve previous order * Restyle * Add back the odd clear * Add missing invalidation of AAI cache * Fix compile * one more compile fix * more compile fixes --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent 13af869 commit b85f076

File tree

98 files changed

+305
-258
lines changed

Some content is hidden

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

98 files changed

+305
-258
lines changed

docs/cluster_and_device_type_dev/cluster_and_device_type_dev.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ ending in the cluster initialization code.
107107
EmberAfInitializeAttributes - ember attribute storage - for all attributes
108108
marked as “RAM” in the zap, sets defaults in the storage
109109
Matter<Cluster>PluginServerCallback - .h is a generated file, .cpp impl is done
110-
in the server cluster code. Use this to setup the cluster and do attribute
111-
overrides registerAttributeAccessOverride - use this if you want to handle
112-
attribute reads and writes externally
110+
in the server cluster code. Use this to setup the cluster and setup overrides in
111+
chip::app::AttributeAccessInterfaceRegistry::Instance().Register - use this if
112+
you want to handle attribute reads and writes externally
113113

114114
Blue sections can be overridden.
115115

docs/upgrading.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,26 @@ independent of the InteractionModelEngine class.
6969
The following replacements exist:
7070

7171
- `chip::app::InteractionModelEngine::RegisterCommandHandler` replaced by
72-
`chip::app::CommandHandlerInterfaceRegistry::RegisterCommandHandler`
72+
`chip::app::CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler`
7373
- `chip::app::InteractionModelEngine::UnregisterCommandHandler` replaced by
74-
`chip::app::CommandHandlerInterfaceRegistry::UnregisterCommandHandler`
74+
`chip::app::CommandHandlerInterfaceRegistry::Instance().UnregisterCommandHandler`
7575
- `chip::app::InteractionModelEngine::FindCommandHandler` replaced by
76-
`chip::app::CommandHandlerInterfaceRegistry::GetCommandHandler`
76+
`chip::app::CommandHandlerInterfaceRegistry::Instance().GetCommandHandler`
7777
- `chip::app::InteractionModelEngine::UnregisterCommandHandlers` replaced by
78-
`chip::app::CommandHandlerInterfaceRegistry::UnregisterAllCommandHandlersForEndpoint`
78+
`chip::app::CommandHandlerInterfaceRegistry::Instance().UnregisterAllCommandHandlersForEndpoint`
79+
80+
### AttributeAccessInterface registration and removal
81+
82+
A new object exists for the attribute access interface registry, accessible as
83+
`chip::app::AttributeHandlerInterfaceRegistry::Instance()`
84+
85+
Replacements for methods are:
86+
87+
- `registerAttributeAccessOverride` replaced by
88+
`chip::app::AttributeAccessInterfaceRegistry::Instance().Register`
89+
- `unregisterAttributeAccessOverride` replaced by
90+
`chip::app::AttributeAccessInterfaceRegistry::Instance().Unregister`
91+
- `unregisterAllAttributeAccessOverridesForEndpoint` replaced by
92+
`chip::app::AttributeAccessInterfaceRegistry::Instance().UnregisterAllForEndpoint`
93+
- `chip::app::GetAttributeAccessOverride` replaced by
94+
`chip::app::AttributeAccessInterfaceRegistry::Instance().Get`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attr
9898

9999
void MatterActionsPluginServerInitCallback()
100100
{
101-
registerAttributeAccessOverride(&gAttrAccess);
101+
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
102102
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,6 @@ void emberAfFanControlClusterInitCallback(EndpointId endpoint)
173173
{
174174
VerifyOrDie(mFanControlManager == nullptr);
175175
mFanControlManager = new FanControlManager(endpoint);
176-
registerAttributeAccessOverride(mFanControlManager);
176+
AttributeAccessInterfaceRegistry::Instance().Register(mFanControlManager);
177177
FanControl::SetDefaultDelegate(endpoint, mFanControlManager);
178178
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,5 @@ CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attr
9797

9898
void MatterActionsPluginServerInitCallback(void)
9999
{
100-
registerAttributeAccessOverride(&gAttrAccess);
100+
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
101101
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attr
113113

114114
void MatterActionsPluginServerInitCallback(void)
115115
{
116-
registerAttributeAccessOverride(&gAttrAccess);
116+
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
117117
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attr
133133

134134
void MatterActionsPluginServerInitCallback()
135135
{
136-
registerAttributeAccessOverride(&gAttrAccess);
136+
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
137137
}

examples/bridge-app/linux/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ void ApplicationInit()
998998
}
999999
}
10001000

1001-
registerAttributeAccessOverride(&gPowerAttrAccess);
1001+
AttributeAccessInterfaceRegistry::Instance().Register(&gPowerAttrAccess);
10021002
}
10031003

10041004
void ApplicationShutdown() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attr
101101

102102
void MatterActionsPluginServerInitCallback(void)
103103
{
104-
registerAttributeAccessOverride(&gAttrAccess);
104+
AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
105105
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,6 @@ void emberAfFanControlClusterInitCallback(EndpointId endpoint)
141141
{
142142
VerifyOrDie(!mFanControlManager);
143143
mFanControlManager = std::make_unique<ChefFanControlManager>(endpoint);
144-
registerAttributeAccessOverride(mFanControlManager.get());
144+
AttributeAccessInterfaceRegistry::Instance().Register(mFanControlManager.get());
145145
FanControl::SetDefaultDelegate(endpoint, mFanControlManager.get());
146146
}

examples/fabric-bridge-app/linux/main.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "RpcServer.h"
3333
#endif
3434

35-
#include <string>
3635
#include <sys/ioctl.h>
3736
#include <thread>
3837

@@ -186,8 +185,8 @@ void ApplicationInit()
186185
ChipLogDetail(NotSpecified, "Fabric-Bridge: ApplicationInit()");
187186

188187
MatterEcosystemInformationPluginServerInitCallback();
189-
CommandHandlerInterfaceRegistry::RegisterCommandHandler(&gAdministratorCommissioningCommandHandler);
190-
registerAttributeAccessOverride(&gBridgedDeviceBasicInformationAttributes);
188+
CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(&gAdministratorCommissioningCommandHandler);
189+
AttributeAccessInterfaceRegistry::Instance().Register(&gBridgedDeviceBasicInformationAttributes);
191190

192191
#if defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE
193192
InitRpcServer(kFabricBridgeServerPort);

examples/log-source-app/linux/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int main(int argc, char * argv[])
110110
// Initialize device attestation config
111111
SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
112112

113-
CommandHandlerInterfaceRegistry::RegisterCommandHandler(&GetLogProvider());
113+
CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(&GetLogProvider());
114114

115115
chip::DeviceLayer::PlatformMgr().RunEventLoop();
116116

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ CHIP_ERROR ActionsAttrAccess::Read(const ConcreteReadAttributePath & aPath, Attr
9898

9999
void MatterActionsPluginServerInitCallback(void)
100100
{
101-
registerAttributeAccessOverride(&gAttrAccess);
101+
chip::app::AttributeAccessInterfaceRegistry::Instance().Register(&gAttrAccess);
102102
}

examples/tv-app/android/java/AppImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ CHIP_ERROR InitVideoPlayerPlatform(jobject contentAppEndpointManager)
572572
{
573573
ContentAppCommandDelegate * delegate =
574574
new ContentAppCommandDelegate(contentAppEndpointManager, contentAppClusters[i].clusterId);
575-
chip::app::CommandHandlerInterfaceRegistry::RegisterCommandHandler(delegate);
575+
app::CommandHandlerInterfaceRegistry::Instance().RegisterCommandHandler(delegate);
576576
ChipLogProgress(AppServer, "Registered command handler delegate for cluster %d", contentAppClusters[i].clusterId);
577577
}
578578

src/app/AttributeAccessInterface.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* endpoint or for all endpoints.
3131
*
3232
* Instances of AttributeAccessInterface that are registered via
33-
* registerAttributeAccessOverride will be consulted before taking the normal
34-
* attribute access codepath and can use that codepath as a fallback if desired.
33+
* AttributeAccessInterfaceRegistry::Instance().Register will be consulted before taking the
34+
* normal attribute access codepath and can use that codepath as a fallback if desired.
3535
*/
3636
namespace chip {
3737
namespace app {

src/app/AttributeAccessInterfaceRegistry.cpp

+31-25
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include "app/AttributeAccessInterface.h"
1617
#include <app/AttributeAccessInterfaceRegistry.h>
1718

1819
#include <app/AttributeAccessInterfaceCache.h>
1920

20-
using namespace chip::app;
21-
2221
namespace {
2322

24-
AttributeAccessInterface * gAttributeAccessOverrides = nullptr;
25-
AttributeAccessInterfaceCache gAttributeAccessInterfaceCache;
23+
using chip::app::AttributeAccessInterface;
2624

2725
// shouldUnregister returns true if the given AttributeAccessInterface should be
2826
// unregistered.
2927
template <typename F>
30-
void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister)
28+
void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister, AttributeAccessInterface *& list_head)
3129
{
3230
AttributeAccessInterface * prev = nullptr;
33-
AttributeAccessInterface * cur = gAttributeAccessOverrides;
31+
AttributeAccessInterface * cur = list_head;
3432
while (cur)
3533
{
3634
AttributeAccessInterface * next = cur->GetNext();
@@ -43,7 +41,7 @@ void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister)
4341
}
4442
else
4543
{
46-
gAttributeAccessOverrides = next;
44+
list_head = next;
4745
}
4846

4947
cur->SetNext(nullptr);
@@ -60,43 +58,51 @@ void UnregisterMatchingAttributeAccessInterfaces(F shouldUnregister)
6058

6159
} // namespace
6260

63-
void unregisterAttributeAccessOverride(AttributeAccessInterface * attrOverride)
61+
namespace chip {
62+
namespace app {
63+
64+
AttributeAccessInterfaceRegistry & AttributeAccessInterfaceRegistry::Instance()
65+
{
66+
static AttributeAccessInterfaceRegistry instance;
67+
return instance;
68+
}
69+
70+
void AttributeAccessInterfaceRegistry::Unregister(AttributeAccessInterface * attrOverride)
6471
{
65-
gAttributeAccessInterfaceCache.Invalidate();
66-
UnregisterMatchingAttributeAccessInterfaces([attrOverride](AttributeAccessInterface * entry) { return entry == attrOverride; });
72+
mAttributeAccessInterfaceCache.Invalidate();
73+
UnregisterMatchingAttributeAccessInterfaces([attrOverride](AttributeAccessInterface * entry) { return entry == attrOverride; },
74+
mAttributeAccessOverrides);
6775
}
6876

69-
void unregisterAllAttributeAccessOverridesForEndpoint(EmberAfDefinedEndpoint * definedEndpoint)
77+
void AttributeAccessInterfaceRegistry::UnregisterAllForEndpoint(EndpointId endpointId)
7078
{
79+
mAttributeAccessInterfaceCache.Invalidate();
7180
UnregisterMatchingAttributeAccessInterfaces(
72-
[endpoint = definedEndpoint->endpoint](AttributeAccessInterface * entry) { return entry->MatchesEndpoint(endpoint); });
81+
[endpointId](AttributeAccessInterface * entry) { return entry->MatchesEndpoint(endpointId); }, mAttributeAccessOverrides);
7382
}
7483

75-
bool registerAttributeAccessOverride(AttributeAccessInterface * attrOverride)
84+
bool AttributeAccessInterfaceRegistry::Register(AttributeAccessInterface * attrOverride)
7685
{
77-
gAttributeAccessInterfaceCache.Invalidate();
78-
for (auto * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext())
86+
mAttributeAccessInterfaceCache.Invalidate();
87+
for (auto * cur = mAttributeAccessOverrides; cur; cur = cur->GetNext())
7988
{
8089
if (cur->Matches(*attrOverride))
8190
{
8291
ChipLogError(InteractionModel, "Duplicate attribute override registration failed");
8392
return false;
8493
}
8594
}
86-
attrOverride->SetNext(gAttributeAccessOverrides);
87-
gAttributeAccessOverrides = attrOverride;
95+
attrOverride->SetNext(mAttributeAccessOverrides);
96+
mAttributeAccessOverrides = attrOverride;
8897
return true;
8998
}
9099

91-
namespace chip {
92-
namespace app {
93-
94-
app::AttributeAccessInterface * GetAttributeAccessOverride(EndpointId endpointId, ClusterId clusterId)
100+
AttributeAccessInterface * AttributeAccessInterfaceRegistry::Get(EndpointId endpointId, ClusterId clusterId)
95101
{
96102
using CacheResult = AttributeAccessInterfaceCache::CacheResult;
97103

98104
AttributeAccessInterface * cached = nullptr;
99-
CacheResult result = gAttributeAccessInterfaceCache.Get(endpointId, clusterId, &cached);
105+
CacheResult result = mAttributeAccessInterfaceCache.Get(endpointId, clusterId, &cached);
100106
switch (result)
101107
{
102108
case CacheResult::kDefinitelyUnused:
@@ -106,17 +112,17 @@ app::AttributeAccessInterface * GetAttributeAccessOverride(EndpointId endpointId
106112
case CacheResult::kCacheMiss:
107113
default:
108114
// Did not cache yet, search set of AAI registered, and cache if found.
109-
for (app::AttributeAccessInterface * cur = gAttributeAccessOverrides; cur; cur = cur->GetNext())
115+
for (app::AttributeAccessInterface * cur = mAttributeAccessOverrides; cur; cur = cur->GetNext())
110116
{
111117
if (cur->Matches(endpointId, clusterId))
112118
{
113-
gAttributeAccessInterfaceCache.MarkUsed(endpointId, clusterId, cur);
119+
mAttributeAccessInterfaceCache.MarkUsed(endpointId, clusterId, cur);
114120
return cur;
115121
}
116122
}
117123

118124
// Did not find AAI registered: mark as definitely not using.
119-
gAttributeAccessInterfaceCache.MarkUnused(endpointId, clusterId);
125+
mAttributeAccessInterfaceCache.MarkUnused(endpointId, clusterId);
120126
}
121127

122128
return nullptr;

src/app/AttributeAccessInterfaceRegistry.h

+38-28
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,49 @@
1616
#pragma once
1717

1818
#include <app/AttributeAccessInterface.h>
19-
#include <app/util/af-types.h>
19+
#include <app/AttributeAccessInterfaceCache.h>
2020

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);
21+
namespace chip {
22+
namespace app {
3323

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);
24+
class AttributeAccessInterfaceRegistry
25+
{
26+
public:
27+
/**
28+
* Register an attribute access override. It will remain registered until the
29+
* endpoint it's registered for is disabled (or until shutdown if it's
30+
* registered for all endpoints) or until it is explicitly unregistered.
31+
* Registration will fail if there is an already-registered override for the
32+
* same set of attributes.
33+
*
34+
* @return false if there is an existing override that the new one would
35+
* conflict with. In this case the override is not registered.
36+
* @return true if registration was successful.
37+
*/
38+
bool Register(AttributeAccessInterface * attrOverride);
3939

40-
/**
41-
* Unregister all attribute access interfaces that match this given endpoint.
42-
*/
43-
void unregisterAllAttributeAccessOverridesForEndpoint(EmberAfDefinedEndpoint * definedEndpoint);
40+
/**
41+
* Unregister an attribute access override (for example if the object
42+
* implementing AttributeAccessInterface is being destroyed).
43+
*/
44+
void Unregister(AttributeAccessInterface * attrOverride);
4445

45-
namespace chip {
46-
namespace app {
46+
/**
47+
* Unregister all attribute access interfaces that match this given endpoint.
48+
*/
49+
void UnregisterAllForEndpoint(EndpointId endpointId);
4750

48-
/**
49-
* Get the registered attribute access override. nullptr when attribute access override is not found.
50-
*/
51-
AttributeAccessInterface * GetAttributeAccessOverride(EndpointId aEndpointId, ClusterId aClusterId);
51+
/**
52+
* Get the registered attribute access override. nullptr when attribute access override is not found.
53+
*/
54+
AttributeAccessInterface * Get(EndpointId aEndpointId, ClusterId aClusterId);
55+
56+
static AttributeAccessInterfaceRegistry & Instance();
57+
58+
private:
59+
AttributeAccessInterface * mAttributeAccessOverrides = nullptr;
60+
AttributeAccessInterfaceCache mAttributeAccessInterfaceCache;
61+
};
5262

5363
} // namespace app
5464
} // namespace chip

0 commit comments

Comments
 (0)