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
3331using namespace chip ;
3432using namespace chip ::app;
3533using 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-
11284DataModel::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
0 commit comments