-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAttributePathExpandIterator.cpp
323 lines (283 loc) · 11.6 KB
/
AttributePathExpandIterator.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* 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 <app/AttributePathExpandIterator.h>
#include <app/GlobalAttributes.h>
#include <app/data-model-provider/MetadataList.h>
#include <app/data-model-provider/MetadataLookup.h>
#include <app/data-model-provider/MetadataTypes.h>
#include <lib/core/DataModelTypes.h>
#include <lib/support/CodeUtils.h>
#include <optional>
using namespace chip::app::DataModel;
namespace chip {
namespace app {
AttributePathExpandIterator::AttributePathExpandIterator(DataModel::Provider * dataModel, Position & position) :
mDataModelProvider(dataModel), mPosition(position)
{}
bool AttributePathExpandIterator::AdvanceOutputPath()
{
/// Output path invariants
/// - kInvalid* constants are used to define "no value available (yet)" and
/// iteration loop will fill the first value when such a value is seen (fixed for non-wildcard
/// or iteration-based in case of wildcards).
/// - Iteration of the output path is done in order: first endpoint, then cluster, then attribute.
/// Processing works like:
/// - Initial state is kInvalidEndpointId/kInvalidClusterId/kInvalidAttributeId
/// - First loop pass fills-in endpointID, followed by clusterID, followed by attributeID
/// - Whenever one level is done iterating (there is no "next") the following
/// "higher path component" is updated:
/// - once a valid path exists, try to advance attributeID
/// - if attributeID fails to advance, try to advance clusterID (and restart attributeID)
/// - if clusterID fails to advance, try to advance endpointID (and restart clusterID)
/// - if endpointID fails to advance, iteration is done
while (true)
{
if (mPosition.mOutputPath.mClusterId != kInvalidClusterId)
{
std::optional<AttributeId> nextAttribute = NextAttributeId();
if (nextAttribute.has_value())
{
mPosition.mOutputPath.mAttributeId = *nextAttribute;
mPosition.mOutputPath.mExpanded = mPosition.mAttributePath->mValue.IsWildcardPath();
return true;
}
}
// no valid attribute, try to advance the cluster, see if a suitable one exists
if (mPosition.mOutputPath.mEndpointId != kInvalidEndpointId)
{
std::optional<ClusterId> nextCluster = NextClusterId();
if (nextCluster.has_value())
{
// A new cluster ID is to be processed. This sets the cluster ID to the new value and
// ALSO resets the attribute ID to "invalid", to trigger an attribute set/expansion from
// the beginning.
mPosition.mOutputPath.mClusterId = *nextCluster;
mPosition.mOutputPath.mAttributeId = kInvalidAttributeId;
continue;
}
}
// No valid cluster, try advance the endpoint, see if a suitable one exists.
std::optional<EndpointId> nextEndpoint = NextEndpointId();
if (nextEndpoint.has_value())
{
// A new endpoint ID is to be processed. This sets the endpoint ID to the new value and
// ALSO resets the cluster ID to "invalid", to trigger a cluster set/expansion from
// the beginning.
mPosition.mOutputPath.mEndpointId = *nextEndpoint;
mPosition.mOutputPath.mClusterId = kInvalidClusterId;
continue;
}
return false;
}
}
bool AttributePathExpandIterator::Next(ConcreteAttributePath & path)
{
while (mPosition.mAttributePath != nullptr)
{
if (AdvanceOutputPath())
{
path = mPosition.mOutputPath;
return true;
}
mPosition.mAttributePath = mPosition.mAttributePath->mpNext;
mPosition.mOutputPath = ConcreteReadAttributePath(kInvalidEndpointId, kInvalidClusterId, kInvalidAttributeId);
}
return false;
}
bool AttributePathExpandIterator::IsValidAttributeId(AttributeId attributeId)
{
switch (attributeId)
{
case Clusters::Globals::Attributes::GeneratedCommandList::Id:
case Clusters::Globals::Attributes::AcceptedCommandList::Id:
case Clusters::Globals::Attributes::AttributeList::Id:
return true;
default:
break;
}
DataModel::AttributeFinder finder(mDataModelProvider);
const ConcreteAttributePath attributePath(mPosition.mOutputPath.mEndpointId, mPosition.mOutputPath.mClusterId, attributeId);
return finder.Find(attributePath).has_value();
}
std::optional<AttributeId> AttributePathExpandIterator::NextAttributeId()
{
if (mPosition.mOutputPath.mAttributeId == kInvalidAttributeId)
{
// Attribute ID is tied to attribute index. If no attribute id is available yet
// this means the index is invalid. Processing logic in output advance only resets
// attribute ID to invalid when resetting iteration.
mAttributeIndex = kInvalidIndex;
}
if (mAttributeIndex == kInvalidIndex)
{
// start a new iteration of attributes on the current cluster path.
mAttributes = mDataModelProvider->AttributesIgnoreError(mPosition.mOutputPath);
if (mPosition.mOutputPath.mAttributeId != kInvalidAttributeId)
{
// Position on the correct attribute if we have a start point
mAttributeIndex = 0;
while ((mAttributeIndex < mAttributes.size()) &&
(mAttributes[mAttributeIndex].attributeId != mPosition.mOutputPath.mAttributeId))
{
mAttributeIndex++;
}
}
}
if (mPosition.mOutputPath.mAttributeId == kInvalidAttributeId)
{
if (!mPosition.mAttributePath->mValue.HasWildcardAttributeId())
{
// The attributeID is NOT a wildcard (i.e. it is fixed).
//
// For wildcard expansion, we validate that this is a valid attribute for the given
// cluster on the given endpoint. If not a wildcard expansion, return it as-is.
if (mPosition.mAttributePath->mValue.IsWildcardPath())
{
if (!IsValidAttributeId(mPosition.mAttributePath->mValue.mAttributeId))
{
return std::nullopt;
}
}
return mPosition.mAttributePath->mValue.mAttributeId;
}
mAttributeIndex = 0;
}
else
{
mAttributeIndex++;
}
// Advance the existing attribute id if it can be advanced.
VerifyOrReturnValue(mPosition.mAttributePath->mValue.HasWildcardAttributeId(), std::nullopt);
// Ensure (including ordering) that GlobalAttributesNotInMetadata is reported as needed
for (unsigned i = 0; i < ArraySize(GlobalAttributesNotInMetadata); i++)
{
if (GlobalAttributesNotInMetadata[i] != mPosition.mOutputPath.mAttributeId)
{
continue;
}
unsigned nextAttributeIndex = i + 1;
if (nextAttributeIndex < ArraySize(GlobalAttributesNotInMetadata))
{
return GlobalAttributesNotInMetadata[nextAttributeIndex];
}
// Reached the end of global attributes. Since global attributes are
// reported last, finishing global attributes means everything completed.
return std::nullopt;
}
if (mAttributeIndex < mAttributes.size())
{
return mAttributes[mAttributeIndex].attributeId;
}
// Finished the data model, start with global attributes
static_assert(ArraySize(GlobalAttributesNotInMetadata) > 0);
return GlobalAttributesNotInMetadata[0];
}
std::optional<ClusterId> AttributePathExpandIterator::NextClusterId()
{
if (mPosition.mOutputPath.mClusterId == kInvalidClusterId)
{
// Cluster ID is tied to cluster index. If no cluster id available yet
// this means index is invalid. Processing logic in output advance only resets
// cluster ID to invalid when resetting iteration.
mClusterIndex = kInvalidIndex;
}
if (mClusterIndex == kInvalidIndex)
{
// start a new iteration on the current endpoint
mClusters = mDataModelProvider->ServerClustersIgnoreError(mPosition.mOutputPath.mEndpointId);
if (mPosition.mOutputPath.mClusterId != kInvalidClusterId)
{
// Position on the correct cluster if we have a start point
mClusterIndex = 0;
while ((mClusterIndex < mClusters.size()) && (mClusters[mClusterIndex].clusterId != mPosition.mOutputPath.mClusterId))
{
mClusterIndex++;
}
}
}
if (mPosition.mOutputPath.mClusterId == kInvalidClusterId)
{
if (!mPosition.mAttributePath->mValue.HasWildcardClusterId())
{
// The clusterID is NOT a wildcard (i.e. is fixed).
//
// For wildcard expansion, we validate that this is a valid cluster for the endpoint.
// If non-wildcard expansion, we return as-is.
if (mPosition.mAttributePath->mValue.IsWildcardPath())
{
const ClusterId clusterId = mPosition.mAttributePath->mValue.mClusterId;
bool found = false;
for (auto & entry : mClusters)
{
if (entry.clusterId == clusterId)
{
found = true;
break;
}
}
if (!found)
{
return std::nullopt;
}
}
return mPosition.mAttributePath->mValue.mClusterId;
}
mClusterIndex = 0;
}
else
{
mClusterIndex++;
}
VerifyOrReturnValue(mPosition.mAttributePath->mValue.HasWildcardClusterId(), std::nullopt);
VerifyOrReturnValue(mClusterIndex < mClusters.size(), std::nullopt);
return mClusters[mClusterIndex].clusterId;
}
std::optional<EndpointId> AttributePathExpandIterator::NextEndpointId()
{
if (mEndpointIndex == kInvalidIndex)
{
// index is missing, have to start a new iteration
mEndpoints = mDataModelProvider->EndpointsIgnoreError();
if (mPosition.mOutputPath.mEndpointId != kInvalidEndpointId)
{
// Position on the correct endpoint if we have a start point
mEndpointIndex = 0;
while ((mEndpointIndex < mEndpoints.size()) && (mEndpoints[mEndpointIndex].id != mPosition.mOutputPath.mEndpointId))
{
mEndpointIndex++;
}
}
}
if (mPosition.mOutputPath.mEndpointId == kInvalidEndpointId)
{
if (!mPosition.mAttributePath->mValue.HasWildcardEndpointId())
{
return mPosition.mAttributePath->mValue.mEndpointId;
}
// start from the beginning
mEndpointIndex = 0;
}
else
{
mEndpointIndex++;
}
VerifyOrReturnValue(mPosition.mAttributePath->mValue.HasWildcardEndpointId(), std::nullopt);
VerifyOrReturnValue(mEndpointIndex < mEndpoints.size(), std::nullopt);
return mEndpoints[mEndpointIndex].id;
}
} // namespace app
} // namespace chip