forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttributePathExpandIterator-DataModel.cpp
242 lines (205 loc) · 8.44 KB
/
AttributePathExpandIterator-DataModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/support/CodeUtils.h"
#include <app/AttributePathExpandIterator-DataModel.h>
#include <app/GlobalAttributes.h>
using namespace chip::app::InteractionModel;
namespace chip {
namespace app {
AttributePathExpandIteratorDataModel::AttributePathExpandIteratorDataModel(
InteractionModel::DataModel * dataModel, SingleLinkedListNode<AttributePathParams> * attributePath) :
mDataModel(dataModel),
mpAttributePath(attributePath), mOutputPath(kInvalidEndpointId, kInvalidClusterId, kInvalidAttributeId)
{
mOutputPath.mExpanded = true; // this is reset in 'next' if needed
// Make the iterator ready to emit the first valid path in the list.
// TODO: the bool return value here is completely unchecked
Next();
}
bool AttributePathExpandIteratorDataModel::IsValidAttributeId(AttributeId attributeId)
{
switch (attributeId)
{
case Clusters::Globals::Attributes::GeneratedCommandList::Id:
case Clusters::Globals::Attributes::AcceptedCommandList::Id:
#if CHIP_CONFIG_ENABLE_EVENTLIST_ATTRIBUTE
case Clusters::Globals::Attributes::EventList::Id:
#endif // CHIP_CONFIG_ENABLE_EVENTLIST_ATTRIBUTE
case Clusters::Globals::Attributes::AttributeList::Id:
return true;
default:
break;
}
const ConcreteAttributePath attributePath(mOutputPath.mEndpointId, mOutputPath.mClusterId, attributeId);
return mDataModel->GetAttributeInfo(attributePath).has_value();
}
std::optional<AttributeId> AttributePathExpandIteratorDataModel::NextAttributeId()
{
if (mOutputPath.mAttributeId == kInvalidAttributeId)
{
if (mpAttributePath->mValue.HasWildcardAttributeId())
{
AttributeEntry entry = mDataModel->FirstAttribute(mOutputPath);
return entry.IsValid() //
? entry.path.mAttributeId //
: Clusters::Globals::Attributes::GeneratedCommandList::Id; //
}
// We allow fixed attribute IDs if and only if they are valid:
// - they may be GLOBAL attributes OR
// - they are valid attributes for this cluster
if (IsValidAttributeId(mpAttributePath->mValue.mAttributeId))
{
return mpAttributePath->mValue.mAttributeId;
}
return std::nullopt;
}
// advance the existing attribute id if it can be advanced
VerifyOrReturnValue(mpAttributePath->mValue.HasWildcardAttributeId(), std::nullopt);
// Ensure (including ordering) that GlobalAttributesNotInMetadata is reported as needed
for (unsigned i = 0; i < ArraySize(GlobalAttributesNotInMetadata); i++)
{
if (GlobalAttributesNotInMetadata[i] != mOutputPath.mAttributeId)
{
continue;
}
unsigned nextAttributeIndex = i + 1;
if (nextAttributeIndex < ArraySize(GlobalAttributesNotInMetadata))
{
return GlobalAttributesNotInMetadata[nextAttributeIndex];
}
// reached the end of global attributes
return std::nullopt;
}
AttributeEntry entry = mDataModel->NextAttribute(mOutputPath);
if (entry.IsValid())
{
return entry.path.mAttributeId;
}
// Finished the data model, start with global attributes
static_assert(ArraySize(GlobalAttributesNotInMetadata) > 0);
return GlobalAttributesNotInMetadata[0];
}
std::optional<ClusterId> AttributePathExpandIteratorDataModel::NextClusterId()
{
if (mOutputPath.mClusterId == kInvalidClusterId)
{
if (mpAttributePath->mValue.HasWildcardClusterId())
{
ClusterEntry entry = mDataModel->FirstCluster(mOutputPath.mEndpointId);
return entry.IsValid() ? std::make_optional(entry.path.mClusterId) : std::nullopt;
}
// only return a cluster if it is valid
const ConcreteClusterPath clusterPath(mOutputPath.mEndpointId, mpAttributePath->mValue.mClusterId);
if (!mDataModel->GetClusterInfo(clusterPath).has_value())
{
return std::nullopt;
}
return mpAttributePath->mValue.mClusterId;
}
VerifyOrReturnValue(mpAttributePath->mValue.HasWildcardClusterId(), std::nullopt);
ClusterEntry entry = mDataModel->NextCluster(mOutputPath);
return entry.IsValid() ? std::make_optional(entry.path.mClusterId) : std::nullopt;
}
std::optional<ClusterId> AttributePathExpandIteratorDataModel::NextEndpointId()
{
if (mOutputPath.mEndpointId == kInvalidEndpointId)
{
if (mpAttributePath->mValue.HasWildcardEndpointId())
{
EndpointId id = mDataModel->FirstEndpoint();
return (id != kInvalidEndpointId) ? std::make_optional(id) : std::nullopt;
}
return mpAttributePath->mValue.mEndpointId;
}
VerifyOrReturnValue(mpAttributePath->mValue.HasWildcardEndpointId(), std::nullopt);
EndpointId id = mDataModel->NextEndpoint(mOutputPath.mEndpointId);
return (id != kInvalidEndpointId) ? std::make_optional(id) : std::nullopt;
}
void AttributePathExpandIteratorDataModel::ResetCurrentCluster()
{
// If this is a null iterator, or the attribute id of current cluster info is not a wildcard attribute id, then this function
// will do nothing, since we won't be expanding the wildcard attribute ids under a cluster.
VerifyOrReturn(mpAttributePath != nullptr && mpAttributePath->mValue.HasWildcardAttributeId());
// Proceed path expansion to ask for the first attribute of the current cluster
mOutputPath.mAttributeId = kInvalidAttributeId;
mOutputPath.mExpanded = true; // we know this is a wildcard attribute
Next();
}
bool AttributePathExpandIteratorDataModel::AdvanceOutputPath()
{
if (!mpAttributePath->mValue.IsWildcardPath())
{
if (mOutputPath.mEndpointId != kInvalidEndpointId)
{
return false; // cannot expand non-wildcard path
}
mOutputPath.mEndpointId = mpAttributePath->mValue.mEndpointId;
mOutputPath.mClusterId = mpAttributePath->mValue.mClusterId;
mOutputPath.mAttributeId = mpAttributePath->mValue.mAttributeId;
mOutputPath.mExpanded = false;
return true;
}
while (true)
{
if (mOutputPath.mClusterId != kInvalidClusterId)
{
std::optional<AttributeId> nextAttribute = NextAttributeId();
if (nextAttribute.has_value())
{
mOutputPath.mAttributeId = *nextAttribute;
return true;
}
}
// no valid attribute, try to advance the cluster, see if a suitable one exists
if (mOutputPath.mEndpointId != kInvalidEndpointId)
{
std::optional<ClusterId> nextCluster = NextClusterId();
if (nextCluster.has_value())
{
mOutputPath.mClusterId = *nextCluster;
mOutputPath.mAttributeId = kInvalidAttributeId; // restarts attributes
continue;
}
}
// no valid cluster, try advance the endpoint, see if a suitable on exists
std::optional<EndpointId> nextEndpoint = NextEndpointId();
if (nextEndpoint.has_value())
{
mOutputPath.mEndpointId = *nextEndpoint;
mOutputPath.mClusterId = kInvalidClusterId; // restarts clusters
continue;
}
return false;
}
}
bool AttributePathExpandIteratorDataModel::Next()
{
while (mpAttributePath != nullptr)
{
if (AdvanceOutputPath())
{
return true;
}
mpAttributePath = mpAttributePath->mpNext;
mOutputPath = ConcreteReadAttributePath(kInvalidEndpointId, kInvalidClusterId, kInvalidAttributeId);
mOutputPath.mExpanded = true; // this is reset to false on advancement if needed
}
mOutputPath = ConcreteReadAttributePath();
return false;
}
} // namespace app
} // namespace chip