-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
attribute-storage.cpp
1466 lines (1277 loc) · 52.9 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* Copyright (c) 2020-2023 Project CHIP Authors
*
* 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/util/attribute-storage.h>
#include <app/util/attribute-storage-detail.h>
#include <app/AttributeAccessInterfaceRegistry.h>
#include <app/AttributePersistenceProvider.h>
#include <app/InteractionModelEngine.h>
#include <app/reporting/reporting.h>
#include <app/util/config.h>
#include <app/util/ember-strings.h>
#include <app/util/endpoint-config-api.h>
#include <app/util/generic-callbacks.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/LockTracker.h>
#include <protocols/interaction_model/StatusCode.h>
using chip::Protocols::InteractionModel::Status;
// Attribute storage depends on knowing the current layout/setup of attributes
// and corresponding callbacks. Specifically:
// - zap-generated/callback.h is needed because endpoint_config will call the
// corresponding callbacks (via GENERATED_FUNCTION_ARRAYS) and the include
// for it is:
// util/config.h -> zap-generated/endpoint_config.h
#include <app-common/zap-generated/callback.h>
using namespace chip;
using namespace chip::app;
//------------------------------------------------------------------------------
// Globals
// This is not declared CONST in order to handle dynamic endpoint information
// retrieved from tokens.
EmberAfDefinedEndpoint emAfEndpoints[MAX_ENDPOINT_COUNT];
#if (ATTRIBUTE_MAX_SIZE == 0)
#define ACTUAL_ATTRIBUTE_SIZE 1
#else
#define ACTUAL_ATTRIBUTE_SIZE ATTRIBUTE_MAX_SIZE
#endif
uint8_t attributeData[ACTUAL_ATTRIBUTE_SIZE];
// ----- internal-only methods, not part of the external API -----
// Loads the attributes from built-in default and storage.
static void emAfLoadAttributeDefaults(chip::EndpointId endpoint, chip::Optional<chip::ClusterId> = chip::NullOptional);
static bool emAfMatchCluster(const EmberAfCluster * cluster, const EmberAfAttributeSearchRecord * attRecord);
static bool emAfMatchAttribute(const EmberAfCluster * cluster, const EmberAfAttributeMetadata * am,
const EmberAfAttributeSearchRecord * attRecord);
// If server == true, returns the number of server clusters,
// otherwise number of client clusters on the endpoint at the given index.
static uint8_t emberAfClusterCountForEndpointType(const EmberAfEndpointType * endpointType, bool server);
// If server == true, returns the number of server clusters,
// otherwise number of client clusters on the endpoint at the given index.
static uint8_t emberAfClusterCountByIndex(uint16_t endpointIndex, bool server);
// Check whether there is an endpoint defined with the given endpoint id that is
// enabled.
static bool emberAfEndpointIsEnabled(chip::EndpointId endpoint);
namespace {
#if (!defined(ATTRIBUTE_SINGLETONS_SIZE)) || (ATTRIBUTE_SINGLETONS_SIZE == 0)
#define ACTUAL_SINGLETONS_SIZE 1
#else
#define ACTUAL_SINGLETONS_SIZE ATTRIBUTE_SINGLETONS_SIZE
#endif
uint8_t singletonAttributeData[ACTUAL_SINGLETONS_SIZE];
uint16_t emberEndpointCount = 0;
// If we have attributes that are more than 4 bytes, then
// we need this data block for the defaults
#if (defined(GENERATED_DEFAULTS) && GENERATED_DEFAULTS_COUNT)
constexpr const uint8_t generatedDefaults[] = GENERATED_DEFAULTS;
#define ZAP_LONG_DEFAULTS_INDEX(index) \
{ \
&generatedDefaults[index] \
}
#endif // GENERATED_DEFAULTS
#if (defined(GENERATED_MIN_MAX_DEFAULTS) && GENERATED_MIN_MAX_DEFAULT_COUNT)
constexpr const EmberAfAttributeMinMaxValue minMaxDefaults[] = GENERATED_MIN_MAX_DEFAULTS;
#define ZAP_MIN_MAX_DEFAULTS_INDEX(index) \
{ \
&minMaxDefaults[index] \
}
#endif // GENERATED_MIN_MAX_DEFAULTS
#ifdef GENERATED_FUNCTION_ARRAYS
GENERATED_FUNCTION_ARRAYS
#endif
#ifdef GENERATED_COMMANDS
constexpr const chip::CommandId generatedCommands[] = GENERATED_COMMANDS;
#define ZAP_GENERATED_COMMANDS_INDEX(index) (&generatedCommands[index])
#endif // GENERATED_COMMANDS
#if (defined(GENERATED_EVENTS) && (GENERATED_EVENT_COUNT > 0))
constexpr const chip::EventId generatedEvents[] = GENERATED_EVENTS;
#define ZAP_GENERATED_EVENTS_INDEX(index) (&generatedEvents[index])
#endif // GENERATED_EVENTS
constexpr const EmberAfAttributeMetadata generatedAttributes[] = GENERATED_ATTRIBUTES;
#define ZAP_ATTRIBUTE_INDEX(index) (&generatedAttributes[index])
#ifdef GENERATED_CLUSTERS
constexpr const EmberAfCluster generatedClusters[] = GENERATED_CLUSTERS;
#define ZAP_CLUSTER_INDEX(index) (&generatedClusters[index])
#endif
#if FIXED_ENDPOINT_COUNT > 0
constexpr const EmberAfEndpointType generatedEmberAfEndpointTypes[] = GENERATED_ENDPOINT_TYPES;
constexpr const EmberAfDeviceType fixedDeviceTypeList[] = FIXED_DEVICE_TYPES;
// Not const, because these need to mutate.
DataVersion fixedEndpointDataVersions[ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT];
#endif // FIXED_ENDPOINT_COUNT > 0
bool emberAfIsThisDataTypeAListType(EmberAfAttributeType dataType)
{
return dataType == ZCL_ARRAY_ATTRIBUTE_TYPE;
}
uint16_t findIndexFromEndpoint(EndpointId endpoint, bool ignoreDisabledEndpoints)
{
if (endpoint == kInvalidEndpointId)
{
return kEmberInvalidEndpointIndex;
}
uint16_t epi;
for (epi = 0; epi < emberAfEndpointCount(); epi++)
{
if (emAfEndpoints[epi].endpoint == endpoint &&
(!ignoreDisabledEndpoints || emAfEndpoints[epi].bitmask.Has(EmberAfEndpointOptions::isEnabled)))
{
return epi;
}
}
return kEmberInvalidEndpointIndex;
}
// Returns the index of a given endpoint. Considers disabled endpoints.
uint16_t emberAfIndexFromEndpointIncludingDisabledEndpoints(EndpointId endpoint)
{
return findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */);
}
} // anonymous namespace
// Initial configuration
void emberAfEndpointConfigure()
{
uint16_t ep;
static_assert(FIXED_ENDPOINT_COUNT <= std::numeric_limits<decltype(ep)>::max(),
"FIXED_ENDPOINT_COUNT must not exceed the size of the endpoint data type");
emberEndpointCount = FIXED_ENDPOINT_COUNT;
#if FIXED_ENDPOINT_COUNT > 0
constexpr uint16_t fixedEndpoints[] = FIXED_ENDPOINT_ARRAY;
constexpr uint16_t fixedDeviceTypeListLengths[] = FIXED_DEVICE_TYPE_LENGTHS;
constexpr uint16_t fixedDeviceTypeListOffsets[] = FIXED_DEVICE_TYPE_OFFSETS;
constexpr uint8_t fixedEmberAfEndpointTypes[] = FIXED_ENDPOINT_TYPES;
constexpr EndpointId fixedParentEndpoints[] = FIXED_PARENT_ENDPOINTS;
#if ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT > 0
// Initialize our data version storage. If
// ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT == 0, gcc complains about a memset
// with size equal to number of elements without multiplication by element
// size, because the sizeof() is also 0 in that case...
if (Crypto::DRBG_get_bytes(reinterpret_cast<uint8_t *>(fixedEndpointDataVersions), sizeof(fixedEndpointDataVersions)) !=
CHIP_NO_ERROR)
{
// Now what? At least 0-init it.
memset(fixedEndpointDataVersions, 0, sizeof(fixedEndpointDataVersions));
}
#endif // ZAP_FIXED_ENDPOINT_DATA_VERSION_COUNT > 0
DataVersion * currentDataVersions = fixedEndpointDataVersions;
for (ep = 0; ep < FIXED_ENDPOINT_COUNT; ep++)
{
emAfEndpoints[ep].endpoint = fixedEndpoints[ep];
emAfEndpoints[ep].deviceTypeList =
Span<const EmberAfDeviceType>(&fixedDeviceTypeList[fixedDeviceTypeListOffsets[ep]], fixedDeviceTypeListLengths[ep]);
emAfEndpoints[ep].endpointType = &generatedEmberAfEndpointTypes[fixedEmberAfEndpointTypes[ep]];
emAfEndpoints[ep].dataVersions = currentDataVersions;
emAfEndpoints[ep].parentEndpointId = fixedParentEndpoints[ep];
emAfEndpoints[ep].bitmask.Set(EmberAfEndpointOptions::isEnabled);
emAfEndpoints[ep].bitmask.Set(EmberAfEndpointOptions::isFlatComposition);
// Increment currentDataVersions by 1 (slot) for every server cluster
// this endpoint has.
currentDataVersions += emberAfClusterCountByIndex(ep, /* server = */ true);
}
#endif // FIXED_ENDPOINT_COUNT > 0
#if CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT
if (MAX_ENDPOINT_COUNT > FIXED_ENDPOINT_COUNT)
{
//
// Reset instances tracking dynamic endpoints to safe defaults.
//
for (ep = FIXED_ENDPOINT_COUNT; ep < MAX_ENDPOINT_COUNT; ep++)
{
emAfEndpoints[ep] = EmberAfDefinedEndpoint();
}
}
#endif
}
void emberAfSetDynamicEndpointCount(uint16_t dynamicEndpointCount)
{
emberEndpointCount = static_cast<uint16_t>(FIXED_ENDPOINT_COUNT + dynamicEndpointCount);
}
uint16_t emberAfGetDynamicIndexFromEndpoint(EndpointId id)
{
if (id == kInvalidEndpointId)
{
return kEmberInvalidEndpointIndex;
}
uint16_t index;
for (index = FIXED_ENDPOINT_COUNT; index < MAX_ENDPOINT_COUNT; index++)
{
if (emAfEndpoints[index].endpoint == id)
{
return static_cast<uint8_t>(index - FIXED_ENDPOINT_COUNT);
}
}
return kEmberInvalidEndpointIndex;
}
CHIP_ERROR emberAfSetDynamicEndpoint(uint16_t index, EndpointId id, const EmberAfEndpointType * ep,
const chip::Span<chip::DataVersion> & dataVersionStorage,
chip::Span<const EmberAfDeviceType> deviceTypeList, EndpointId parentEndpointId)
{
auto realIndex = index + FIXED_ENDPOINT_COUNT;
if (realIndex >= MAX_ENDPOINT_COUNT)
{
return CHIP_ERROR_NO_MEMORY;
}
if (id == kInvalidEndpointId)
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
auto serverClusterCount = emberAfClusterCountForEndpointType(ep, /* server = */ true);
if (dataVersionStorage.size() < serverClusterCount)
{
return CHIP_ERROR_NO_MEMORY;
}
index = static_cast<uint16_t>(realIndex);
for (uint16_t i = FIXED_ENDPOINT_COUNT; i < MAX_ENDPOINT_COUNT; i++)
{
if (emAfEndpoints[i].endpoint == id)
{
return CHIP_ERROR_ENDPOINT_EXISTS;
}
}
emAfEndpoints[index].endpoint = id;
emAfEndpoints[index].deviceTypeList = deviceTypeList;
emAfEndpoints[index].endpointType = ep;
emAfEndpoints[index].dataVersions = dataVersionStorage.data();
// Start the endpoint off as disabled.
emAfEndpoints[index].bitmask.Clear(EmberAfEndpointOptions::isEnabled);
emAfEndpoints[index].parentEndpointId = parentEndpointId;
emberAfSetDynamicEndpointCount(MAX_ENDPOINT_COUNT - FIXED_ENDPOINT_COUNT);
// Initialize the data versions.
size_t dataSize = sizeof(DataVersion) * serverClusterCount;
if (dataSize != 0)
{
if (Crypto::DRBG_get_bytes(reinterpret_cast<uint8_t *>(dataVersionStorage.data()), dataSize) != CHIP_NO_ERROR)
{
// Now what? At least 0-init it.
memset(dataVersionStorage.data(), 0, dataSize);
}
}
// Now enable the endpoint.
emberAfEndpointEnableDisable(id, true);
return CHIP_NO_ERROR;
}
EndpointId emberAfClearDynamicEndpoint(uint16_t index)
{
EndpointId ep = 0;
index = static_cast<uint8_t>(index + FIXED_ENDPOINT_COUNT);
if ((index < MAX_ENDPOINT_COUNT) && (emAfEndpoints[index].endpoint != kInvalidEndpointId) &&
(emberAfEndpointIndexIsEnabled(index)))
{
ep = emAfEndpoints[index].endpoint;
emberAfEndpointEnableDisable(ep, false);
emAfEndpoints[index].endpoint = kInvalidEndpointId;
}
return ep;
}
uint16_t emberAfFixedEndpointCount()
{
return FIXED_ENDPOINT_COUNT;
}
uint16_t emberAfEndpointCount()
{
return emberEndpointCount;
}
bool emberAfEndpointIndexIsEnabled(uint16_t index)
{
return (emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled));
}
// This function is used to call the per-cluster attribute changed callback
void emAfClusterAttributeChangedCallback(const app::ConcreteAttributePath & attributePath)
{
const EmberAfCluster * cluster = emberAfFindServerCluster(attributePath.mEndpointId, attributePath.mClusterId);
if (cluster != nullptr)
{
EmberAfGenericClusterFunction f = emberAfFindClusterFunction(cluster, CLUSTER_MASK_ATTRIBUTE_CHANGED_FUNCTION);
if (f != nullptr)
{
((EmberAfClusterAttributeChangedCallback) f)(attributePath);
}
}
}
// This function is used to call the per-cluster pre-attribute changed callback
Status emAfClusterPreAttributeChangedCallback(const app::ConcreteAttributePath & attributePath, EmberAfAttributeType attributeType,
uint16_t size, uint8_t * value)
{
const EmberAfCluster * cluster = emberAfFindServerCluster(attributePath.mEndpointId, attributePath.mClusterId);
if (cluster == nullptr)
{
if (!emberAfEndpointIsEnabled(attributePath.mEndpointId))
{
return Status::UnsupportedEndpoint;
}
return Status::UnsupportedCluster;
}
Status status = Status::Success;
// Casting and calling a function pointer on the same line results in ignoring the return
// of the call on gcc-arm-none-eabi-9-2019-q4-major
EmberAfClusterPreAttributeChangedCallback f = (EmberAfClusterPreAttributeChangedCallback) (emberAfFindClusterFunction(
cluster, CLUSTER_MASK_PRE_ATTRIBUTE_CHANGED_FUNCTION));
if (f != nullptr)
{
status = f(attributePath, attributeType, size, value);
}
return status;
}
static void initializeEndpoint(EmberAfDefinedEndpoint * definedEndpoint)
{
uint8_t clusterIndex;
const EmberAfEndpointType * epType = definedEndpoint->endpointType;
for (clusterIndex = 0; clusterIndex < epType->clusterCount; clusterIndex++)
{
const EmberAfCluster * cluster = &(epType->cluster[clusterIndex]);
EmberAfGenericClusterFunction f;
emberAfClusterInitCallback(definedEndpoint->endpoint, cluster->clusterId);
f = emberAfFindClusterFunction(cluster, CLUSTER_MASK_INIT_FUNCTION);
if (f != nullptr)
{
((EmberAfInitFunction) f)(definedEndpoint->endpoint);
}
}
}
static void shutdownEndpoint(EmberAfDefinedEndpoint * definedEndpoint)
{
// Call shutdown callbacks from clusters, mainly for canceling pending timers
uint8_t clusterIndex;
const EmberAfEndpointType * epType = definedEndpoint->endpointType;
for (clusterIndex = 0; clusterIndex < epType->clusterCount; clusterIndex++)
{
const EmberAfCluster * cluster = &(epType->cluster[clusterIndex]);
EmberAfGenericClusterFunction f = emberAfFindClusterFunction(cluster, CLUSTER_MASK_SHUTDOWN_FUNCTION);
if (f != nullptr)
{
((EmberAfShutdownFunction) f)(definedEndpoint->endpoint);
}
}
// Clear out any command handler overrides registered for this
// endpoint.
chip::app::InteractionModelEngine::GetInstance()->UnregisterCommandHandlers(definedEndpoint->endpoint);
unregisterAllAttributeAccessOverridesForEndpoint(definedEndpoint);
}
// Calls the init functions.
void emAfCallInits()
{
uint16_t index;
for (index = 0; index < emberAfEndpointCount(); index++)
{
if (emberAfEndpointIndexIsEnabled(index))
{
initializeEndpoint(&(emAfEndpoints[index]));
}
}
}
// Returns the pointer to metadata, or null if it is not found
const EmberAfAttributeMetadata * emberAfLocateAttributeMetadata(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId)
{
const EmberAfAttributeMetadata * metadata = nullptr;
EmberAfAttributeSearchRecord record;
record.endpoint = endpoint;
record.clusterId = clusterId;
record.attributeId = attributeId;
emAfReadOrWriteAttribute(&record, &metadata,
nullptr, // buffer
0, // buffer size
false); // write?
return metadata;
}
static uint8_t * singletonAttributeLocation(const EmberAfAttributeMetadata * am)
{
const EmberAfAttributeMetadata * m = &(generatedAttributes[0]);
uint16_t index = 0;
while (m < am)
{
if (m->IsSingleton() && !m->IsExternal())
{
index = static_cast<uint16_t>(index + m->size);
}
m++;
}
return (uint8_t *) (singletonAttributeData + index);
}
// This function does mem copy, but smartly, which means that if the type is a
// string, it will copy as much as it can.
// If src == NULL, then this method will set memory to zeroes
// See documentation for emAfReadOrWriteAttribute for the semantics of
// readLength when reading and writing.
static Status typeSensitiveMemCopy(ClusterId clusterId, uint8_t * dest, uint8_t * src, const EmberAfAttributeMetadata * am,
bool write, uint16_t readLength)
{
EmberAfAttributeType attributeType = am->attributeType;
// readLength == 0 for a read indicates that we should just trust that the
// caller has enough space for an attribute...
bool ignoreReadLength = write || (readLength == 0);
uint16_t bufferSize = ignoreReadLength ? am->size : readLength;
if (emberAfIsStringAttributeType(attributeType))
{
if (bufferSize < 1)
{
return Status::ResourceExhausted;
}
emberAfCopyString(dest, src, bufferSize - 1);
}
else if (emberAfIsLongStringAttributeType(attributeType))
{
if (bufferSize < 2)
{
return Status::ResourceExhausted;
}
emberAfCopyLongString(dest, src, bufferSize - 2);
}
else if (emberAfIsThisDataTypeAListType(attributeType))
{
if (bufferSize < 2)
{
return Status::ResourceExhausted;
}
// Just copy the length.
memmove(dest, src, 2);
}
else
{
if (!ignoreReadLength && readLength < am->size)
{
return Status::ResourceExhausted;
}
if (src == nullptr)
{
memset(dest, 0, am->size);
}
else
{
memmove(dest, src, am->size);
}
}
return Status::Success;
}
/**
* @brief Matches a cluster based on cluster id and direction.
*
* This function assumes that the passed cluster's endpoint already
* matches the endpoint of the EmberAfAttributeSearchRecord.
*
* Clusters match if:
* 1. Cluster ids match AND
* 2. Cluster is a server cluster (because there are no client attributes).
*/
bool emAfMatchCluster(const EmberAfCluster * cluster, const EmberAfAttributeSearchRecord * attRecord)
{
return (cluster->clusterId == attRecord->clusterId && (cluster->mask & CLUSTER_MASK_SERVER));
}
/**
* @brief Matches an attribute based on attribute id.
* This function assumes that the passed cluster already matches the
* clusterId and direction of the passed EmberAfAttributeSearchRecord.
*
* Attributes match if attr ids match.
*/
bool emAfMatchAttribute(const EmberAfCluster * cluster, const EmberAfAttributeMetadata * am,
const EmberAfAttributeSearchRecord * attRecord)
{
return (am->attributeId == attRecord->attributeId);
}
// When reading non-string attributes, this function returns an error when destination
// buffer isn't large enough to accommodate the attribute type. For strings, the
// function will copy at most readLength bytes. This means the resulting string
// may be truncated. The length byte(s) in the resulting string will reflect
// any truncation. If readLength is zero, we are working with backwards-
// compatibility wrapper functions and we just cross our fingers and hope for
// the best.
//
// When writing attributes, readLength is ignored. For non-string attributes,
// this function assumes the source buffer is the same size as the attribute
// type. For strings, the function will copy as many bytes as will fit in the
// attribute. This means the resulting string may be truncated. The length
// byte(s) in the resulting string will reflect any truncated.
Status emAfReadOrWriteAttribute(const EmberAfAttributeSearchRecord * attRecord, const EmberAfAttributeMetadata ** metadata,
uint8_t * buffer, uint16_t readLength, bool write)
{
assertChipStackLockedByCurrentThread();
uint16_t attributeOffsetIndex = 0;
for (uint16_t ep = 0; ep < emberAfEndpointCount(); ep++)
{
// Is this a dynamic endpoint?
bool isDynamicEndpoint = (ep >= emberAfFixedEndpointCount());
if (emAfEndpoints[ep].endpoint == attRecord->endpoint)
{
const EmberAfEndpointType * endpointType = emAfEndpoints[ep].endpointType;
uint8_t clusterIndex;
if (!emberAfEndpointIndexIsEnabled(ep))
{
continue;
}
for (clusterIndex = 0; clusterIndex < endpointType->clusterCount; clusterIndex++)
{
const EmberAfCluster * cluster = &(endpointType->cluster[clusterIndex]);
if (emAfMatchCluster(cluster, attRecord))
{ // Got the cluster
uint16_t attrIndex;
for (attrIndex = 0; attrIndex < cluster->attributeCount; attrIndex++)
{
const EmberAfAttributeMetadata * am = &(cluster->attributes[attrIndex]);
if (emAfMatchAttribute(cluster, am, attRecord))
{ // Got the attribute
// If passed metadata location is not null, populate
if (metadata != nullptr)
{
*metadata = am;
}
{
uint8_t * attributeLocation =
(am->mask & ATTRIBUTE_MASK_SINGLETON ? singletonAttributeLocation(am)
: attributeData + attributeOffsetIndex);
uint8_t *src, *dst;
if (write)
{
src = buffer;
dst = attributeLocation;
if (!emberAfAttributeWriteAccessCallback(attRecord->endpoint, attRecord->clusterId,
am->attributeId))
{
return Status::UnsupportedAccess;
}
}
else
{
if (buffer == nullptr)
{
return Status::Success;
}
src = attributeLocation;
dst = buffer;
if (!emberAfAttributeReadAccessCallback(attRecord->endpoint, attRecord->clusterId,
am->attributeId))
{
return Status::UnsupportedAccess;
}
}
// Is the attribute externally stored?
if (am->mask & ATTRIBUTE_MASK_EXTERNAL_STORAGE)
{
return (write ? emberAfExternalAttributeWriteCallback(attRecord->endpoint, attRecord->clusterId,
am, buffer)
: emberAfExternalAttributeReadCallback(attRecord->endpoint, attRecord->clusterId,
am, buffer, emberAfAttributeSize(am)));
}
// Internal storage is only supported for fixed endpoints
if (!isDynamicEndpoint)
{
return typeSensitiveMemCopy(attRecord->clusterId, dst, src, am, write, readLength);
}
return Status::Failure;
}
}
else
{ // Not the attribute we are looking for
// Increase the index if attribute is not externally stored
if (!(am->mask & ATTRIBUTE_MASK_EXTERNAL_STORAGE) && !(am->mask & ATTRIBUTE_MASK_SINGLETON))
{
attributeOffsetIndex = static_cast<uint16_t>(attributeOffsetIndex + emberAfAttributeSize(am));
}
}
}
// Attribute is not in the cluster.
return Status::UnsupportedAttribute;
}
// Not the cluster we are looking for
attributeOffsetIndex = static_cast<uint16_t>(attributeOffsetIndex + cluster->clusterSize);
}
// Cluster is not in the endpoint.
return Status::UnsupportedCluster;
}
// Not the endpoint we are looking for
// Dynamic endpoints are external and don't factor into storage size
if (!isDynamicEndpoint)
{
attributeOffsetIndex = static_cast<uint16_t>(attributeOffsetIndex + emAfEndpoints[ep].endpointType->endpointSize);
}
}
return Status::UnsupportedEndpoint; // Sorry, endpoint was not found.
}
const EmberAfEndpointType * emberAfFindEndpointType(chip::EndpointId endpointId)
{
uint16_t ep = emberAfIndexFromEndpoint(endpointId);
if (ep == kEmberInvalidEndpointIndex)
{
return nullptr;
}
return emAfEndpoints[ep].endpointType;
}
const EmberAfCluster * emberAfFindClusterInType(const EmberAfEndpointType * endpointType, ClusterId clusterId,
EmberAfClusterMask mask, uint8_t * index)
{
uint8_t i;
uint8_t scopedIndex = 0;
for (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 emberAfClusterIndex(EndpointId endpoint, ClusterId clusterId, EmberAfClusterMask mask)
{
for (uint16_t ep = 0; ep < emberAfEndpointCount(); ep++)
{
// Check the endpoint id first, because that way we avoid examining the
// endpoint type for endpoints that are not actually defined.
if (emAfEndpoints[ep].endpoint == endpoint)
{
const EmberAfEndpointType * endpointType = emAfEndpoints[ep].endpointType;
uint8_t index = 0xFF;
if (emberAfFindClusterInType(endpointType, clusterId, mask, &index) != nullptr)
{
return index;
}
}
}
return 0xFF;
}
// Returns whether the given endpoint has the server of the given cluster on it.
bool emberAfContainsServer(EndpointId endpoint, ClusterId clusterId)
{
return (emberAfFindServerCluster(endpoint, clusterId) != nullptr);
}
// Returns whether the given endpoint has the client of the given cluster on it.
bool emberAfContainsClient(EndpointId endpoint, ClusterId clusterId)
{
uint16_t ep = emberAfIndexFromEndpoint(endpoint);
if (ep == kEmberInvalidEndpointIndex)
{
return false;
}
return (emberAfFindClusterInType(emAfEndpoints[ep].endpointType, clusterId, CLUSTER_MASK_CLIENT) != nullptr);
}
// This will find the first server that has the clusterId given from the index of endpoint.
bool emberAfContainsServerFromIndex(uint16_t index, ClusterId clusterId)
{
if (index == kEmberInvalidEndpointIndex)
{
return false;
}
return emberAfFindClusterInType(emAfEndpoints[index].endpointType, clusterId, CLUSTER_MASK_SERVER);
}
namespace chip {
namespace app {
EnabledEndpointsWithServerCluster::EnabledEndpointsWithServerCluster(ClusterId clusterId) :
mEndpointCount(emberAfEndpointCount()), mClusterId(clusterId)
{
EnsureMatchingEndpoint();
}
EndpointId EnabledEndpointsWithServerCluster::operator*() const
{
return emberAfEndpointFromIndex(mEndpointIndex);
}
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 chip
// Finds the cluster that matches endpoint, clusterId, direction.
const EmberAfCluster * emberAfFindServerCluster(EndpointId endpoint, ClusterId clusterId)
{
uint16_t ep = emberAfIndexFromEndpoint(endpoint);
if (ep == kEmberInvalidEndpointIndex)
{
return nullptr;
}
return emberAfFindClusterInType(emAfEndpoints[ep].endpointType, clusterId, CLUSTER_MASK_SERVER);
}
// Returns cluster within the endpoint; Does not ignore disabled endpoints
const EmberAfCluster * emberAfFindClusterIncludingDisabledEndpoints(EndpointId endpoint, ClusterId clusterId,
EmberAfClusterMask mask)
{
uint16_t ep = emberAfIndexFromEndpointIncludingDisabledEndpoints(endpoint);
if (ep < MAX_ENDPOINT_COUNT)
{
return emberAfFindClusterInType(emAfEndpoints[ep].endpointType, clusterId, mask);
}
return nullptr;
}
uint16_t emberAfGetClusterServerEndpointIndex(EndpointId endpoint, ClusterId cluster, uint16_t fixedClusterServerEndpointCount)
{
VerifyOrDie(fixedClusterServerEndpointCount <= FIXED_ENDPOINT_COUNT);
uint16_t epIndex = findIndexFromEndpoint(endpoint, true /*ignoreDisabledEndpoints*/);
// Endpoint must be configured and enabled
if (epIndex == kEmberInvalidEndpointIndex)
{
return kEmberInvalidEndpointIndex;
}
if (emberAfFindClusterInType(emAfEndpoints[epIndex].endpointType, cluster, CLUSTER_MASK_SERVER) == nullptr)
{
// The provided endpoint does not contain the given cluster server.
return kEmberInvalidEndpointIndex;
}
if (epIndex < FIXED_ENDPOINT_COUNT)
{
// This endpoint is a fixed one.
// Return the index of this endpoint in the list of fixed endpoints that support the given cluster.
uint16_t adjustedEndpointIndex = 0;
for (uint16_t i = 0; i < epIndex; i++)
{
// Increase adjustedEndpointIndex for every endpoint containing the cluster server
// before our endpoint of interest
if (emAfEndpoints[i].endpoint != kInvalidEndpointId &&
(emberAfFindClusterInType(emAfEndpoints[i].endpointType, cluster, CLUSTER_MASK_SERVER) != nullptr))
{
adjustedEndpointIndex++;
}
}
// If this asserts, the provided fixedClusterServerEndpointCount doesn't match the app data model.
VerifyOrDie(adjustedEndpointIndex < fixedClusterServerEndpointCount);
epIndex = adjustedEndpointIndex;
}
else
{
// This is a dynamic endpoint.
// Its index is just its index in the dynamic endpoint list, offset by fixedClusterServerEndpointCount.
epIndex = static_cast<uint16_t>(fixedClusterServerEndpointCount + (epIndex - FIXED_ENDPOINT_COUNT));
}
return epIndex;
}
bool emberAfEndpointIsEnabled(EndpointId endpoint)
{
uint16_t index = findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */);
if (kEmberInvalidEndpointIndex == index)
{
return false;
}
return emberAfEndpointIndexIsEnabled(index);
}
bool emberAfEndpointEnableDisable(EndpointId endpoint, bool enable)
{
uint16_t index = findIndexFromEndpoint(endpoint, false /* ignoreDisabledEndpoints */);
bool currentlyEnabled;
if (kEmberInvalidEndpointIndex == index)
{
return false;
}
currentlyEnabled = emAfEndpoints[index].bitmask.Has(EmberAfEndpointOptions::isEnabled);
if (enable)
{
emAfEndpoints[index].bitmask.Set(EmberAfEndpointOptions::isEnabled);
}
if (currentlyEnabled != enable)
{
if (enable)
{
initializeEndpoint(&(emAfEndpoints[index]));
MatterReportingAttributeChangeCallback(endpoint);
}
else
{
shutdownEndpoint(&(emAfEndpoints[index]));
emAfEndpoints[index].bitmask.Clear(EmberAfEndpointOptions::isEnabled);
}
EndpointId parentEndpointId = emberAfParentEndpointFromIndex(index);
while (parentEndpointId != kInvalidEndpointId)
{
MatterReportingAttributeChangeCallback(parentEndpointId, app::Clusters::Descriptor::Id,
app::Clusters::Descriptor::Attributes::PartsList::Id);
uint16_t parentIndex = emberAfIndexFromEndpoint(parentEndpointId);
if (parentIndex == kEmberInvalidEndpointIndex)
{
// Something has gone wrong.
break;
}
parentEndpointId = emberAfParentEndpointFromIndex(parentIndex);
}
MatterReportingAttributeChangeCallback(/* endpoint = */ 0, app::Clusters::Descriptor::Id,
app::Clusters::Descriptor::Attributes::PartsList::Id);
}
return true;
}
// Returns the index of a given endpoint. Does not consider disabled endpoints.
uint16_t emberAfIndexFromEndpoint(EndpointId endpoint)
{
return findIndexFromEndpoint(endpoint, true /* ignoreDisabledEndpoints */);
}
EndpointId emberAfEndpointFromIndex(uint16_t index)
{
return emAfEndpoints[index].endpoint;
}
EndpointId emberAfParentEndpointFromIndex(uint16_t index)
{
return emAfEndpoints[index].parentEndpointId;
}
// If server == true, returns the number of server clusters,
// otherwise number of client clusters on this endpoint
uint8_t emberAfClusterCount(EndpointId endpoint, bool server)
{
uint16_t index = emberAfIndexFromEndpoint(endpoint);
if (index == kEmberInvalidEndpointIndex)
{
return 0;
}
return emberAfClusterCountByIndex(index, server);
}
uint8_t emberAfClusterCountByIndex(uint16_t endpointIndex, bool server)
{
const EmberAfDefinedEndpoint * de = &(emAfEndpoints[endpointIndex]);
if (de->endpointType == nullptr)
{
return 0;
}
return emberAfClusterCountForEndpointType(de->endpointType, server);
}
uint8_t emberAfClusterCountForEndpointType(const EmberAfEndpointType * type, bool server)
{
const EmberAfClusterMask cluster_mask = server ? CLUSTER_MASK_SERVER : CLUSTER_MASK_CLIENT;
return static_cast<uint8_t>(std::count_if(type->cluster, type->cluster + type->clusterCount,
[=](const EmberAfCluster & cluster) { return (cluster.mask & cluster_mask) != 0; }));
}
uint8_t emberAfGetClusterCountForEndpoint(EndpointId endpoint)