forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattribute-storage.cpp
467 lines (402 loc) · 17.1 KB
/
attribute-storage.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
*
* Copyright (c) 2021 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.
*/
/**
* @file
* This file contains the mock implementation for the generated attribute-storage.cpp
* - It contains three endpoints, 0xFFFE, 0xFFFD, 0xFFFC
* - It contains four clusters: 0xFFF1'0001 to 0xFFF1'0004
* - All cluster has two global attribute (0x0000'FFFC, 0x0000'FFFD)
* - Some clusters has some cluster-specific attributes, with 0xFFF1 prefix.
*
* Note: The ember's attribute-storage.cpp will include some app specific generated files. So we cannot use it directly. This
* might be fixed with a mock endpoint-config.h
*/
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app/MessageDef/AttributeDataIB.h>
#include <app/MessageDef/AttributeReportIB.h>
#include <app/MessageDef/AttributeStatusIB.h>
#include <app/util/att-storage.h>
#include <app/util/attribute-storage.h>
#include <app/util/endpoint-config-api.h>
#include <app/util/mock/Constants.h>
#include <app/util/mock/MockNodeConfig.h>
#include <app/AttributeValueEncoder.h>
#include <app/ConcreteAttributePath.h>
#include <app/EventManagement.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPEncoding.h>
#include <lib/core/TLVDebug.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DLLUtil.h>
#include <lib/support/logging/CHIPLogging.h>
#include <app/util/af-types.h>
#include <app/util/attribute-metadata.h>
typedef uint8_t EmberAfClusterMask;
using namespace chip;
using namespace chip::Test;
using namespace chip::app;
using namespace Clusters::Globals::Attributes;
namespace {
DataVersion dataVersion = 0;
const MockNodeConfig * mockConfig = nullptr;
const MockNodeConfig & DefaultMockNodeConfig()
{
// clang-format off
static const MockNodeConfig config({
MockEndpointConfig(kMockEndpoint1, {
MockClusterConfig(MockClusterId(1), {
ClusterRevision::Id, FeatureMap::Id,
}, {
MockEventId(1), MockEventId(2),
}),
MockClusterConfig(MockClusterId(2), {
ClusterRevision::Id, FeatureMap::Id, MockAttributeId(1),
}),
}),
MockEndpointConfig(kMockEndpoint2, {
MockClusterConfig(MockClusterId(1), {
ClusterRevision::Id, FeatureMap::Id,
}),
MockClusterConfig(MockClusterId(2), {
ClusterRevision::Id, FeatureMap::Id, MockAttributeId(1), MockAttributeId(2),
}),
MockClusterConfig(MockClusterId(3), {
ClusterRevision::Id, FeatureMap::Id, MockAttributeId(1), MockAttributeId(2), MockAttributeId(3),
}),
}),
MockEndpointConfig(kMockEndpoint3, {
MockClusterConfig(MockClusterId(1), {
ClusterRevision::Id, FeatureMap::Id, MockAttributeId(1),
}),
MockClusterConfig(MockClusterId(2), {
ClusterRevision::Id, FeatureMap::Id, MockAttributeId(1), MockAttributeId(2), MockAttributeId(3), MockAttributeId(4),
}),
MockClusterConfig(MockClusterId(3), {
ClusterRevision::Id, FeatureMap::Id,
}),
MockClusterConfig(MockClusterId(4), {
ClusterRevision::Id, FeatureMap::Id,
}),
}),
});
// clang-format on
return config;
}
const MockNodeConfig & GetMockNodeConfig()
{
return (mockConfig != nullptr) ? *mockConfig : DefaultMockNodeConfig();
}
uint16_t mockClusterRevision = 1;
uint32_t mockFeatureMap = 0x1234;
bool mockAttribute1 = true;
int16_t mockAttribute2 = 42;
uint64_t mockAttribute3 = 0xdeadbeef0000cafe;
uint8_t mockAttribute4[256] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
};
} // namespace
uint16_t emberAfEndpointCount()
{
return static_cast<uint16_t>(GetMockNodeConfig().endpoints.size());
}
uint16_t emberAfIndexFromEndpoint(EndpointId endpointId)
{
ptrdiff_t index;
auto endpoint = GetMockNodeConfig().endpointById(endpointId, &index);
VerifyOrReturnValue(endpoint != nullptr, kEmberInvalidEndpointIndex);
return static_cast<uint16_t>(index);
}
uint8_t emberAfGetClusterCountForEndpoint(EndpointId endpointId)
{
auto endpoint = GetMockNodeConfig().endpointById(endpointId);
VerifyOrReturnValue(endpoint != nullptr, 0);
return static_cast<uint8_t>(endpoint->clusters.size());
}
const EmberAfAttributeMetadata * emberAfLocateAttributeMetadata(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId)
{
auto ep = GetMockNodeConfig().endpointById(endpointId);
VerifyOrReturnValue(ep != nullptr, nullptr);
auto cluster = ep->clusterById(clusterId);
VerifyOrReturnValue(cluster != nullptr, nullptr);
auto attr = cluster->attributeById(attributeId);
VerifyOrReturnValue(attr != nullptr, nullptr);
return &attr->attributeMetaData;
}
const EmberAfCluster * emberAfFindClusterInType(const EmberAfEndpointType * endpointType, ClusterId clusterId,
EmberAfClusterMask mask, uint8_t * index)
{
// This is a copy & paste implementation from ember attribute storage
// TODO: this hard-codes ember logic and is duplicated code.
uint8_t scopedIndex = 0;
for (uint8_t i = 0; i < endpointType->clusterCount; i++)
{
const EmberAfCluster * cluster = &(endpointType->cluster[i]);
if (mask == 0 || ((cluster->mask & mask) != 0))
{
if (cluster->clusterId == clusterId)
{
if (index)
{
*index = scopedIndex;
}
return cluster;
}
scopedIndex++;
}
}
return nullptr;
}
uint8_t emberAfClusterCount(chip::EndpointId endpoint, bool server)
{
return (server) ? emberAfGetClusterCountForEndpoint(endpoint) : 0;
}
uint16_t emberAfGetServerAttributeCount(chip::EndpointId endpointId, chip::ClusterId clusterId)
{
auto cluster = GetMockNodeConfig().clusterByIds(endpointId, clusterId);
VerifyOrReturnValue(cluster != nullptr, 0);
return static_cast<uint16_t>(cluster->attributes.size());
}
uint16_t emberAfGetServerAttributeIndexByAttributeId(chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::AttributeId attributeId)
{
auto cluster = GetMockNodeConfig().clusterByIds(endpointId, clusterId);
VerifyOrReturnValue(cluster != nullptr, kEmberInvalidEndpointIndex);
ptrdiff_t index;
auto attribute = cluster->attributeById(attributeId, &index);
VerifyOrReturnValue(attribute != nullptr, kEmberInvalidEndpointIndex);
return static_cast<uint16_t>(index);
}
bool emberAfContainsAttribute(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId)
{
return emberAfGetServerAttributeIndexByAttributeId(endpoint, clusterId, attributeId) != kEmberInvalidEndpointIndex;
}
chip::EndpointId emberAfEndpointFromIndex(uint16_t index)
{
auto & config = GetMockNodeConfig();
VerifyOrDie(index < config.endpoints.size());
return config.endpoints[index].id;
}
chip::Optional<chip::ClusterId> emberAfGetNthClusterId(chip::EndpointId endpointId, uint8_t n, bool server)
{
VerifyOrReturnValue(server, NullOptional); // only server clusters supported
auto endpoint = GetMockNodeConfig().endpointById(endpointId);
VerifyOrReturnValue(endpoint != nullptr && n < endpoint->clusters.size(), NullOptional);
return MakeOptional(endpoint->clusters[n].id);
}
// Returns number of clusters put into the passed cluster list
// for the given endpoint and client/server polarity
uint8_t emberAfGetClustersFromEndpoint(EndpointId endpoint, ClusterId * clusterList, uint8_t listLen, bool server)
{
uint8_t cluster_count = emberAfClusterCount(endpoint, server);
uint8_t i;
if (cluster_count > listLen)
{
cluster_count = listLen;
}
for (i = 0; i < cluster_count; i++)
{
clusterList[i] = emberAfGetNthClusterId(endpoint, i, server).Value();
}
return cluster_count;
}
chip::Optional<chip::AttributeId> emberAfGetServerAttributeIdByIndex(chip::EndpointId endpointId, chip::ClusterId clusterId,
uint16_t index)
{
auto cluster = GetMockNodeConfig().clusterByIds(endpointId, clusterId);
VerifyOrReturnValue(cluster != nullptr && index < cluster->attributes.size(), NullOptional);
return MakeOptional(cluster->attributes[index].id);
}
uint8_t emberAfClusterIndex(chip::EndpointId endpointId, chip::ClusterId clusterId, EmberAfClusterMask mask)
{
VerifyOrReturnValue(mask == 0 || (mask & CLUSTER_MASK_SERVER) != 0, UINT8_MAX); // only server clusters supported
ptrdiff_t index;
auto cluster = GetMockNodeConfig().clusterByIds(endpointId, clusterId, &index);
VerifyOrReturnValue(cluster != nullptr, UINT8_MAX);
return static_cast<uint8_t>(index);
}
bool emberAfEndpointIndexIsEnabled(uint16_t index)
{
return index < GetMockNodeConfig().endpoints.size();
}
// This will find the first server that has the clusterId given from the index of endpoint.
bool emberAfContainsServerFromIndex(uint16_t index, ClusterId clusterId)
{
auto config = GetMockNodeConfig();
VerifyOrReturnValue(index < config.endpoints.size(), false);
return true; // TODO: TestSceneTable relies on returning true here: https://github.com/project-chip/connectedhomeip/issues/30696
// return config.endpoints[index].clusterById(clusterId) != nullptr;
}
const EmberAfEndpointType * emberAfFindEndpointType(EndpointId endpointId)
{
auto endpoint = GetMockNodeConfig().endpointById(endpointId);
VerifyOrReturnValue(endpoint != nullptr, nullptr);
return endpoint->emberEndpoint();
}
const EmberAfCluster * emberAfFindServerCluster(EndpointId endpointId, ClusterId clusterId)
{
auto cluster = GetMockNodeConfig().clusterByIds(endpointId, clusterId);
VerifyOrReturnValue(cluster != nullptr, nullptr);
return cluster->emberCluster();
}
DataVersion * emberAfDataVersionStorage(const chip::app::ConcreteClusterPath & aConcreteClusterPath)
{
// shared data version storage
return &dataVersion;
}
namespace chip {
namespace app {
EndpointId EnabledEndpointsWithServerCluster::operator*() const
{
return emberAfEndpointFromIndex(mEndpointIndex);
}
EnabledEndpointsWithServerCluster::EnabledEndpointsWithServerCluster(ClusterId clusterId) :
mEndpointCount(emberAfEndpointCount()), mClusterId(clusterId)
{
EnsureMatchingEndpoint();
}
EnabledEndpointsWithServerCluster & EnabledEndpointsWithServerCluster::operator++()
{
++mEndpointIndex;
EnsureMatchingEndpoint();
return *this;
}
void EnabledEndpointsWithServerCluster::EnsureMatchingEndpoint()
{
for (; mEndpointIndex < mEndpointCount; ++mEndpointIndex)
{
if (!emberAfEndpointIndexIsEnabled(mEndpointIndex))
{
continue;
}
if (emberAfContainsServerFromIndex(mEndpointIndex, mClusterId))
{
break;
}
}
}
} // namespace app
namespace Test {
void ResetVersion()
{
dataVersion = 0;
}
void BumpVersion()
{
dataVersion++;
}
DataVersion GetVersion()
{
return dataVersion;
}
CHIP_ERROR ReadSingleMockClusterData(FabricIndex aAccessingFabricIndex, const ConcreteAttributePath & aPath,
AttributeReportIBs::Builder & aAttributeReports, AttributeEncodeState * apEncoderState)
{
bool dataExists =
(emberAfGetServerAttributeIndexByAttributeId(aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId) != UINT16_MAX);
ChipLogDetail(DataManagement, "Reading Mock Endpoint %x Mock Cluster %" PRIx32 ", Field %" PRIx32 " is dirty",
aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId);
if (!dataExists)
{
AttributeReportIB::Builder & attributeReport = aAttributeReports.CreateAttributeReport();
ReturnErrorOnFailure(aAttributeReports.GetError());
AttributeStatusIB::Builder & attributeStatus = attributeReport.CreateAttributeStatus();
ReturnErrorOnFailure(attributeReport.GetError());
AttributePathIB::Builder & attributePath = attributeStatus.CreatePath();
ReturnErrorOnFailure(attributeStatus.GetError());
attributePath.Endpoint(aPath.mEndpointId).Cluster(aPath.mClusterId).Attribute(aPath.mAttributeId).EndOfAttributePathIB();
ReturnErrorOnFailure(attributePath.GetError());
StatusIB::Builder & errorStatus = attributeStatus.CreateErrorStatus();
ReturnErrorOnFailure(attributeStatus.GetError());
errorStatus.EncodeStatusIB(StatusIB(Protocols::InteractionModel::Status::UnsupportedAttribute));
ReturnErrorOnFailure(errorStatus.GetError());
ReturnErrorOnFailure(attributeStatus.EndOfAttributeStatusIB());
return attributeReport.EndOfAttributeReportIB();
}
// Attribute 4 acts as a large attribute to trigger chunking.
if (aPath.mAttributeId == MockAttributeId(4))
{
AttributeEncodeState state(apEncoderState);
Access::SubjectDescriptor subject;
subject.fabricIndex = aAccessingFabricIndex;
AttributeValueEncoder valueEncoder(aAttributeReports, subject, aPath, dataVersion, /* aIsFabricFiltered = */ false, state);
CHIP_ERROR err = valueEncoder.EncodeList([](const auto & encoder) -> CHIP_ERROR {
for (int i = 0; i < 6; i++)
{
ReturnErrorOnFailure(encoder.Encode(chip::ByteSpan(mockAttribute4, sizeof(mockAttribute4))));
}
return CHIP_NO_ERROR;
});
if (apEncoderState != nullptr)
{
*apEncoderState = valueEncoder.GetState();
}
return err;
}
AttributeReportIB::Builder & attributeReport = aAttributeReports.CreateAttributeReport();
ReturnErrorOnFailure(aAttributeReports.GetError());
AttributeDataIB::Builder & attributeData = attributeReport.CreateAttributeData();
ReturnErrorOnFailure(attributeReport.GetError());
attributeData.DataVersion(dataVersion);
AttributePathIB::Builder & attributePath = attributeData.CreatePath();
ReturnErrorOnFailure(attributeData.GetError());
attributePath.Endpoint(aPath.mEndpointId).Cluster(aPath.mClusterId).Attribute(aPath.mAttributeId).EndOfAttributePathIB();
ReturnErrorOnFailure(attributePath.GetError());
TLV::TLVWriter * writer = attributeData.GetWriter();
switch (aPath.mAttributeId)
{
case Clusters::Globals::Attributes::ClusterRevision::Id:
ReturnErrorOnFailure(writer->Put(TLV::ContextTag(AttributeDataIB::Tag::kData), mockClusterRevision));
break;
case Clusters::Globals::Attributes::FeatureMap::Id:
ReturnErrorOnFailure(writer->Put(TLV::ContextTag(AttributeDataIB::Tag::kData), mockFeatureMap));
break;
case MockAttributeId(1):
ReturnErrorOnFailure(writer->Put(TLV::ContextTag(AttributeDataIB::Tag::kData), mockAttribute1));
break;
case MockAttributeId(2):
ReturnErrorOnFailure(writer->Put(TLV::ContextTag(AttributeDataIB::Tag::kData), mockAttribute2));
break;
case MockAttributeId(3):
ReturnErrorOnFailure(writer->Put(TLV::ContextTag(AttributeDataIB::Tag::kData), mockAttribute3));
break;
default:
// The key should found since we have checked above.
return CHIP_ERROR_KEY_NOT_FOUND;
}
ReturnErrorOnFailure(attributeData.EndOfAttributeDataIB());
return attributeReport.EndOfAttributeReportIB();
}
void SetMockNodeConfig(const MockNodeConfig & config)
{
mockConfig = &config;
}
/// Resets the mock attribute storage to the default configuration.
void ResetMockNodeConfig()
{
mockConfig = nullptr;
}
} // namespace Test
} // namespace chip