Skip to content

Commit aca9215

Browse files
emessina-maxHayk10002restyled-commitsAniDashyanElen777300
authored
injecting dependencies for binding cluster. (project-chip#42418)
* injecting dependencies for binding cluster. * moving BindingManager CreateBindingEntry logic into BindingCluster. * recovering AddBingingEntry for BindingManager.cpp * Decouple `Binding::Manager` and `Binding::PendingNotificationMap` * Restyled by clang-format * Fix namespace issue * Restyled by clang-format * Finish decoupling * Fix namespace issue * Apply suggestion from @Elen777300 Co-authored-by: Elen777300 <43118262+Elen777300@users.noreply.github.com> * Replace `TEMPORARY_RETURN_INGORED` with `LogErrorOnFailure` * Fix namespace naming issues * Restyled by clang-format * Remove an unnecessary variable --------- Co-authored-by: Hayk10002 <hayk.g.khachatryan@gmail.com> Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: AniDashyan <ani.dashyan77@gmail.com> Co-authored-by: AniDashyan <45006450+AniDashyan@users.noreply.github.com> Co-authored-by: Elen777300 <43118262+Elen777300@users.noreply.github.com>
1 parent bf354ec commit aca9215

9 files changed

Lines changed: 92 additions & 56 deletions

File tree

examples/all-clusters-app/all-clusters-common/src/binding-handler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ static void BoundDeviceContextReleaseHandler(void * context)
116116
static void InitBindingHandlerInternal(intptr_t arg)
117117
{
118118
auto & server = chip::Server::GetInstance();
119-
TEMPORARY_RETURN_IGNORED Binding::Manager::GetInstance().Init(
120-
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
119+
LogErrorOnFailure(Binding::Manager::GetInstance().Init(
120+
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() }));
121121
Binding::Manager::GetInstance().RegisterBoundDeviceChangedHandler(BoundDeviceChangedHandler);
122122
Binding::Manager::GetInstance().RegisterBoundDeviceContextReleaseHandler(BoundDeviceContextReleaseHandler);
123123
}

examples/light-switch-app/asr/src/BindingHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ void BindingHandler::InitInternal(intptr_t arg)
242242
{
243243
ASR_LOG("Initialize binding Handler");
244244
auto & server = chip::Server::GetInstance();
245-
TEMPORARY_RETURN_IGNORED Binding::Manager::GetInstance().Init(
246-
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
245+
LogErrorOnFailure(Binding::Manager::GetInstance().Init(
246+
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() }));
247247
Binding::Manager::GetInstance().RegisterBoundDeviceChangedHandler(LightSwitchChangedHandler);
248248
Binding::Manager::GetInstance().RegisterBoundDeviceContextReleaseHandler(LightSwitchContextReleaseHandler);
249249
BindingHandler::GetInstance().PrintBindingTable();

examples/light-switch-app/silabs/src/BindingHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ void LightSwitchContextReleaseHandler(void * context)
391391
void InitBindingHandlerInternal(intptr_t arg)
392392
{
393393
auto & server = chip::Server::GetInstance();
394-
TEMPORARY_RETURN_IGNORED Binding::Manager::GetInstance().Init(
395-
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
394+
LogErrorOnFailure(Binding::Manager::GetInstance().Init(
395+
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() }));
396396
Binding::Manager::GetInstance().RegisterBoundDeviceChangedHandler(LightSwitchChangedHandler);
397397
Binding::Manager::GetInstance().RegisterBoundDeviceContextReleaseHandler(LightSwitchContextReleaseHandler);
398398
}

examples/light-switch-app/telink/src/binding-handler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ static void RegisterSwitchCommands()
435435
void InitBindingHandlerInternal(intptr_t arg)
436436
{
437437
auto & server = chip::Server::GetInstance();
438-
TEMPORARY_RETURN_IGNORED Binding::Manager::GetInstance().Init(
439-
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
438+
LogErrorOnFailure(Binding::Manager::GetInstance().Init(
439+
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() }));
440440
Binding::Manager::GetInstance().RegisterBoundDeviceChangedHandler(LightSwitchChangedHandler);
441441
Binding::Manager::GetInstance().RegisterBoundDeviceContextReleaseHandler(LightSwitchContextReleaseHandler);
442442
}

examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ CHIP_ERROR CastingServer::SetRotatingDeviceIdUniqueId(chip::Optional<chip::ByteS
111111
CHIP_ERROR CastingServer::InitBindingHandlers()
112112
{
113113
auto & server = chip::Server::GetInstance();
114-
TEMPORARY_RETURN_IGNORED app::Clusters::Binding::Manager::GetInstance().Init(
115-
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
114+
LogErrorOnFailure(app::Clusters::Binding::Manager::GetInstance().Init(
115+
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() }));
116116
return CHIP_NO_ERROR;
117117
}
118118

src/app/clusters/bindings/BindingCluster.cpp

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@
2525
#include <app/clusters/bindings/BindingCluster.h>
2626
#include <app/clusters/bindings/binding-table.h>
2727
#include <app/server-cluster/AttributeListBuilder.h>
28-
#include <clusters/Binding/Attributes.h>
2928
#include <clusters/Binding/Metadata.h>
30-
#include <clusters/Binding/Structs.h>
3129
#include <protocols/interaction_model/StatusCode.h>
3230

3331
using namespace chip;
3432
using namespace chip::app;
3533
using namespace chip::app::Clusters;
36-
using TargetStructType = Binding::Structs::TargetStruct::Type;
37-
using DecodableBindingListType = Binding::Attributes::Binding::TypeInfo::DecodableType;
3834

39-
// TODO: add binding table to the persistent storage
40-
namespace {
35+
namespace chip {
36+
namespace app {
37+
namespace Clusters {
4138

42-
bool IsValidBinding(const EndpointId localEndpoint, const TargetStructType & entry)
39+
bool BindingCluster::IsValidBinding(const EndpointId localEndpoint, const TargetStructType & entry)
4340
{
4441
// Entry has endpoint, node id and no group id
4542
if (!entry.group.HasValue() && entry.endpoint.HasValue() && entry.node.HasValue())
@@ -48,9 +45,7 @@ bool IsValidBinding(const EndpointId localEndpoint, const TargetStructType & ent
4845
// Valid node/endpoint/cluster binding
4946
ReadOnlyBufferBuilder<ClusterId> clientClusters;
5047
// TODO: is this a correct validation?
51-
VerifyOrReturnValue(InteractionModelEngine::GetInstance()->GetDataModelProvider()->ClientClusters(
52-
localEndpoint, clientClusters) == CHIP_NO_ERROR,
53-
false);
48+
VerifyOrReturnValue(mContext->provider.ClientClusters(localEndpoint, clientClusters) == CHIP_NO_ERROR, false);
5449
for (auto & client : clientClusters.TakeBuffer())
5550
{
5651
VerifyOrReturnValue(client != entry.cluster.Value(), true);
@@ -60,8 +55,8 @@ bool IsValidBinding(const EndpointId localEndpoint, const TargetStructType & ent
6055
return (!entry.endpoint.HasValue() && !entry.node.HasValue() && entry.group.HasValue());
6156
}
6257

63-
CHIP_ERROR CheckValidBindingList(const EndpointId localEndpoint, const DecodableBindingListType & bindingList,
64-
FabricIndex accessingFabricIndex)
58+
CHIP_ERROR BindingCluster::CheckValidBindingList(const EndpointId localEndpoint, const DecodableBindingListType & bindingList,
59+
FabricIndex accessingFabricIndex)
6560
{
6661
size_t listSize = 0;
6762
auto iter = bindingList.begin();
@@ -74,49 +69,26 @@ CHIP_ERROR CheckValidBindingList(const EndpointId localEndpoint, const Decodable
7469

7570
// Check binding table size
7671
uint8_t oldListSize = 0;
77-
for (const auto & entry : Binding::Table::GetInstance())
72+
for (const auto & entry : mClusterContext.bindingTable)
7873
{
7974
if (entry.local == localEndpoint && entry.fabricIndex == accessingFabricIndex)
8075
{
8176
oldListSize++;
8277
}
8378
}
84-
VerifyOrReturnError(Binding::Table::GetInstance().Size() - oldListSize + listSize <= Binding::Table::kMaxBindingEntries,
79+
VerifyOrReturnError(mClusterContext.bindingTable.Size() - oldListSize + listSize <= Binding::Table::kMaxBindingEntries,
8580
CHIP_IM_GLOBAL_STATUS(ResourceExhausted));
8681
return CHIP_NO_ERROR;
8782
}
8883

89-
CHIP_ERROR CreateBindingEntry(const TargetStructType & entry, EndpointId localEndpoint)
90-
{
91-
Binding::TableEntry bindingEntry;
92-
93-
if (entry.group.HasValue())
94-
{
95-
bindingEntry = Binding::TableEntry(entry.fabricIndex, entry.group.Value(), localEndpoint, entry.cluster.std_optional());
96-
}
97-
else
98-
{
99-
bindingEntry = Binding::TableEntry(entry.fabricIndex, entry.node.Value(), localEndpoint, entry.endpoint.Value(),
100-
entry.cluster.std_optional());
101-
}
102-
103-
return AddBindingEntry(bindingEntry);
104-
}
105-
106-
} // namespace
107-
108-
namespace chip {
109-
namespace app {
110-
namespace Clusters {
111-
11284
DataModel::ActionReturnStatus BindingCluster::ReadAttribute(const DataModel::ReadAttributeRequest & request,
11385
AttributeValueEncoder & encoder)
11486
{
11587
switch (request.path.mAttributeId)
11688
{
11789
case Binding::Attributes::Binding::Id: {
11890
return encoder.EncodeList([&](const auto & subEncoder) {
119-
for (auto & entry : Binding::Table::GetInstance())
91+
for (auto & entry : mClusterContext.bindingTable)
12092
{
12193
if (entry.local != request.path.mEndpointId)
12294
{
@@ -173,17 +145,17 @@ DataModel::ActionReturnStatus BindingCluster::WriteAttribute(const DataModel::Wr
173145
CheckValidBindingList(request.path.mEndpointId, newBindingList, request.GetAccessingFabricIndex()));
174146

175147
// Clear all entries for the current accessing fabric and endpoint
176-
auto bindingTableIter = Binding::Table::GetInstance().begin();
177-
while (bindingTableIter != Binding::Table::GetInstance().end())
148+
auto bindingTableIter = mClusterContext.bindingTable.begin();
149+
while (bindingTableIter != mClusterContext.bindingTable.end())
178150
{
179151
if (bindingTableIter->local == request.path.mEndpointId &&
180152
bindingTableIter->fabricIndex == request.GetAccessingFabricIndex())
181153
{
182154
if (bindingTableIter->type == Binding::MATTER_UNICAST_BINDING)
183155
{
184-
TEMPORARY_RETURN_IGNORED Binding::Manager::GetInstance().UnicastBindingRemoved(bindingTableIter.GetIndex());
156+
TEMPORARY_RETURN_IGNORED mClusterContext.bindingManager.UnicastBindingRemoved(bindingTableIter.GetIndex());
185157
}
186-
ReturnErrorOnFailure(Binding::Table::GetInstance().RemoveAt(bindingTableIter));
158+
ReturnErrorOnFailure(mClusterContext.bindingTable.RemoveAt(bindingTableIter));
187159
}
188160
else
189161
{
@@ -246,7 +218,24 @@ CHIP_ERROR BindingCluster::NotifyBindingsChanged(FabricIndex accessingFabricInde
246218
{
247219
DeviceLayer::ChipDeviceEvent event{ .Type = DeviceLayer::DeviceEventType::kBindingsChangedViaCluster,
248220
.BindingsChanged = { .fabricIndex = accessingFabricIndex } };
249-
return chip::DeviceLayer::PlatformMgr().PostEvent(&event);
221+
return mClusterContext.platformManager.PostEvent(&event);
222+
}
223+
224+
CHIP_ERROR BindingCluster::CreateBindingEntry(const TargetStructType & entry, EndpointId localEndpoint)
225+
{
226+
Binding::TableEntry bindingEntry;
227+
228+
if (entry.group.HasValue())
229+
{
230+
bindingEntry = Binding::TableEntry(entry.fabricIndex, entry.group.Value(), localEndpoint, entry.cluster.std_optional());
231+
}
232+
else
233+
{
234+
bindingEntry = Binding::TableEntry(entry.fabricIndex, entry.node.Value(), localEndpoint, entry.endpoint.Value(),
235+
entry.cluster.std_optional());
236+
}
237+
238+
return mClusterContext.bindingManager.AddBindingEntry(bindingEntry);
250239
}
251240

252241
} // namespace Clusters

src/app/clusters/bindings/BindingCluster.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,29 @@
1919
#include <app/clusters/bindings/BindingManager.h>
2020
#include <app/clusters/bindings/binding-table.h>
2121
#include <app/server-cluster/DefaultServerCluster.h>
22+
#include <clusters/Binding/Attributes.h>
23+
#include <clusters/Binding/Structs.h>
2224

2325
namespace chip {
2426
namespace app {
2527
namespace Clusters {
2628

29+
using TargetStructType = Binding::Structs::TargetStruct::Type;
30+
using DecodableBindingListType = Binding::Attributes::Binding::TypeInfo::DecodableType;
31+
2732
class BindingCluster : public DefaultServerCluster
2833
{
2934
public:
30-
constexpr BindingCluster(EndpointId endpointId) : DefaultServerCluster(ConcreteClusterPath::ConstExpr(endpointId, Binding::Id))
35+
/// Injected dependencies for this cluster
36+
struct Context
37+
{
38+
Binding::Table & bindingTable;
39+
Binding::Manager & bindingManager;
40+
DeviceLayer::PlatformManager & platformManager;
41+
};
42+
43+
constexpr BindingCluster(Context && context, EndpointId endpointId) :
44+
DefaultServerCluster(ConcreteClusterPath::ConstExpr(endpointId, Binding::Id)), mClusterContext(std::move(context))
3145
{}
3246

3347
DataModel::ActionReturnStatus ReadAttribute(const DataModel::ReadAttributeRequest & request,
@@ -42,7 +56,25 @@ class BindingCluster : public DefaultServerCluster
4256
CHIP_ERROR Attributes(const ConcreteClusterPath & path, ReadOnlyBufferBuilder<DataModel::AttributeEntry> & builder) override;
4357

4458
private:
59+
bool IsValidBinding(const EndpointId localEndpoint, const TargetStructType & entry);
60+
61+
CHIP_ERROR CheckValidBindingList(const EndpointId localEndpoint, const DecodableBindingListType & bindingList,
62+
FabricIndex accessingFabricIndex);
63+
4564
CHIP_ERROR NotifyBindingsChanged(FabricIndex accessingFabricIndex);
65+
66+
/**
67+
* @brief appends a binding to the list of bindings
68+
* This function is to be used when a device wants to add a binding to its own table
69+
* If entry is a unicast binding, BindingManager will be notified and will establish a case session with the peer device
70+
* Entry will be added to the binding table and persisted into storage
71+
* BindingManager will be notified and the binding added callback will be called if it has been set
72+
*
73+
* @param entry binding to add
74+
*/
75+
CHIP_ERROR CreateBindingEntry(const TargetStructType & entry, EndpointId localEndpoint);
76+
77+
Context mClusterContext;
4678
};
4779

4880
} // namespace Clusters

src/app/clusters/bindings/CodegenIntegration.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ class IntegrationDelegate : public CodegenClusterIntegration::Delegate
3939
ServerClusterRegistration & CreateRegistration(EndpointId endpointId, unsigned clusterInstanceIndex,
4040
uint32_t optionalAttributeBits, uint32_t featureMap) override
4141
{
42-
gServers[clusterInstanceIndex].Create(endpointId);
42+
gServers[clusterInstanceIndex].Create(
43+
BindingCluster::Context{
44+
.bindingTable = Binding::Table::GetInstance(),
45+
.bindingManager = Binding::Manager::GetInstance(),
46+
.platformManager = DeviceLayer::PlatformMgr(),
47+
},
48+
endpointId);
4349
return gServers[clusterInstanceIndex].Registration();
4450
}
4551

src/app/clusters/bindings/tests/TestBindingCluster.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ struct TestBindingCluster : public ::testing::Test
4242
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
4343
};
4444

45+
BindingCluster::Context CreateStandardContext()
46+
{
47+
return BindingCluster::Context{
48+
.bindingTable = Binding::Table::GetInstance(),
49+
.bindingManager = Binding::Manager::GetInstance(),
50+
.platformManager = chip::DeviceLayer::PlatformMgr(),
51+
};
52+
}
53+
4554
TEST_F(TestBindingCluster, TestAttributes)
4655
{
47-
BindingCluster cluster(1);
56+
BindingCluster cluster(CreateStandardContext(), 1);
4857

4958
ASSERT_TRUE(IsAttributesListEqualTo(cluster,
5059
{

0 commit comments

Comments
 (0)