This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
/
TraitData.cpp
1718 lines (1445 loc) · 58.3 KB
/
TraitData.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) 2016-2017 Nest Labs, Inc.
* 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 forms the crux of the TDM layer (trait data management), providing
* various classes that manage and process data as it applies to traits and their
* associated schemas.
*
*/
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#ifndef ULLONG_MAX
#define ULLONG_MAX (unsigned long long)(-1)
#endif
#include <Weave/Profiles/data-management/Current/WdmManagedNamespace.h>
#include <Weave/Profiles/data-management/DataManagement.h>
#include <Weave/Support/WeaveFaultInjection.h>
#include <Weave/Support/RandUtils.h>
using namespace ::nl::Weave;
using namespace ::nl::Weave::TLV;
using namespace ::nl::Weave::Profiles;
using namespace ::nl::Weave::Profiles::Common;
using namespace ::nl::Weave::Profiles::DataManagement;
using namespace ::nl::Weave::Profiles::DataManagement_Current;
UpdateDirtyPathFilter::UpdateDirtyPathFilter(SubscriptionClient *apSubClient,
TraitDataHandle traitDataHandle,
const TraitSchemaEngine * aEngine)
{
mpSubClient = apSubClient;
mTraitDataHandle = traitDataHandle;
mSchemaEngine = aEngine;
}
bool UpdateDirtyPathFilter::FilterPath (PropertyPathHandle pathhandle)
{
bool retval = false;
#if WEAVE_CONFIG_ENABLE_WDM_UPDATE
if (mpSubClient)
{
// TODO: clean this up:
// mpSubClient is set to something only by UpdatableDataSink instances
retval = mpSubClient->FilterNotifiedPath(mTraitDataHandle, pathhandle, mSchemaEngine);
}
#endif // WEAVE_CONFIG_ENABLE_WDM_UPDATE
return retval;
}
UpdateDictionaryDirtyPathCut::UpdateDictionaryDirtyPathCut(TraitDataHandle aTraitDataHandle, UpdateEncoder * apEncoder)
{
mpUpdateEncoder = apEncoder;
mTraitDataHandle = aTraitDataHandle;
}
WEAVE_ERROR UpdateDictionaryDirtyPathCut::CutPath (PropertyPathHandle aPathhandle, const TraitSchemaEngine * apEngine)
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
// TODO: rename this struct, and pass apEngine to the constructor.
// Probably just replace the struct with a function; I don't see the point of it really.
#if WEAVE_CONFIG_ENABLE_WDM_UPDATE
err = mpUpdateEncoder->InsertInProgressUpdateItem(TraitPath(mTraitDataHandle, aPathhandle));
WeaveLogDetail(DataManagement, "Cut dictionary %u, %u; err %d", mTraitDataHandle, aPathhandle, err);
#endif // WEAVE_CONFIG_ENABLE_WDM_UPDATE
return err;
}
WEAVE_ERROR TraitSchemaEngine::ParseTagString(const char *apTagString, char **apEndptr, uint64_t& aParseRes, bool & aAnyParsed) const
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
const char *data;
uint64_t sum = 0;
int c;
bool anyParsed = false;
uint64_t threshold = ULLONG_MAX / (uint64_t)10;
int remainder = ULLONG_MAX % (uint64_t)10;
VerifyOrExit(apTagString != NULL, err = WEAVE_ERROR_INCORRECT_STATE);
data = apTagString;
c = (unsigned char) *data++;
VerifyOrExit(c == '/', err = WEAVE_ERROR_INVALID_TLV_TAG);
c = (unsigned char) *data ++;
VerifyOrExit(c != '/', err = WEAVE_ERROR_INVALID_TLV_TAG);
if (c == '\0')
{
goto exit;
}
for (sum = 0;; c = (unsigned char) *data++)
{
if (c >= '0' && c <= '9')
{
c -= '0';
}
else if (c == '/' || c == '\0')
{
break;
}
else
{
err = WEAVE_ERROR_INVALID_TLV_TAG;
SuccessOrExit(err);
}
if (sum > threshold || (sum == threshold && c > remainder))
{
anyParsed = false;
sum = ULLONG_MAX;
err = WEAVE_ERROR_INVALID_TLV_TAG;
SuccessOrExit(err);
}
else
{
anyParsed = true;
sum *= (uint64_t)10;
sum += c;
}
}
exit:
if (apEndptr != 0)
{
*apEndptr = (char *) (anyParsed ? data - 1 : apTagString);
}
aParseRes = sum;
aAnyParsed = anyParsed;
return err;
}
WEAVE_ERROR TraitSchemaEngine::MapPathToHandle(const char * aPathString, PropertyPathHandle & aHandle) const
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
PropertyPathHandle childProperty, curProperty;
char *parseBeg, *parseEnd;
uint64_t parseRes = 0;
uint64_t tag = 0;
bool anyParsed = false;
VerifyOrExit(aPathString != NULL, err = WEAVE_ERROR_INCORRECT_STATE);
err = ParseTagString(aPathString, &parseEnd, parseRes, anyParsed);
SuccessOrExit(err);
// initialize the out argument to NULL
aHandle = kNullPropertyPathHandle;
// Set our starting point for traversal to the root node.
curProperty = kRootPropertyPathHandle;
VerifyOrExit(anyParsed, );
parseBeg = parseEnd;
// Descend into our schema tree using the tags encountered to help navigate through the various branches.
do
{
// Todo: add dictionary support, not yet supported
VerifyOrExit(parseRes < kContextTagMaxNum, err = WEAVE_ERROR_INVALID_TLV_TAG);
tag = ContextTag(parseRes);
if (IsProfileTag(tag))
{
err = WEAVE_ERROR_NOT_IMPLEMENTED;
SuccessOrExit(err);
}
else
{
childProperty = GetChildHandle(curProperty, TagNumFromTag(tag));
}
if (IsNullPropertyPathHandle(childProperty))
{
err = WEAVE_ERROR_TLV_TAG_NOT_FOUND;
SuccessOrExit(err);
}
// Set the current node.
curProperty = childProperty;
if (*parseEnd == 0)
{
break;
}
parseBeg = parseEnd;
err = ParseTagString(parseBeg, &parseEnd, parseRes, anyParsed);
SuccessOrExit(err);
if (!anyParsed)
{
err = WEAVE_ERROR_INVALID_TLV_TAG;
SuccessOrExit(err);
}
}
while (1);
exit:
if (err == WEAVE_NO_ERROR)
{
aHandle = curProperty;
}
return err;
}
WEAVE_ERROR TraitSchemaEngine::MapPathToHandle(TLVReader & aPathReader, PropertyPathHandle & aHandle) const
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
PropertyPathHandle childProperty, curProperty;
nl::Weave::TLV::TLVType dummyContainerType = kTLVType_Path;
// initialize the out argument to NULL
aHandle = kNullPropertyPathHandle;
// Set our starting point for traversal to the root node.
curProperty = kRootPropertyPathHandle;
// Descend into our schema tree using the tags encountered to help navigate through the various branches.
while ((err = aPathReader.Next()) == WEAVE_NO_ERROR)
{
const uint64_t tag = aPathReader.GetTag();
// If it's a profile tag, we know we're dealing with a dictionary item - get the appropriate dictionary item. Otherwise,
// treat it like a regular child node.
if (IsProfileTag(tag))
{
VerifyOrExit(ProfileIdFromTag(tag) == kWeaveProfile_DictionaryKey, err = WEAVE_ERROR_INVALID_TLV_TAG);
childProperty = GetDictionaryItemHandle(curProperty, TagNumFromTag(tag));
}
else
{
childProperty = GetChildHandle(curProperty, TagNumFromTag(tag));
}
if (IsNullPropertyPathHandle(childProperty))
{
err = WEAVE_ERROR_TLV_TAG_NOT_FOUND;
break;
}
// Set the current node.
curProperty = childProperty;
}
// End of TLV is the only expected error here and if so, correctly update the handle passed in by the caller.
if (err == WEAVE_END_OF_TLV)
{
err = aPathReader.ExitContainer(dummyContainerType);
SuccessOrExit(err);
aHandle = curProperty;
err = WEAVE_NO_ERROR;
}
exit:
return err;
}
WEAVE_ERROR TraitSchemaEngine::MapHandleToPath(PropertyPathHandle aHandle, TLVWriter & aPathWriter) const
{
// Use the tree depth specified by the schema to correctly size our path walk store
PropertyPathHandle pathWalkStore[mSchema.mTreeDepth];
uint32_t pathWalkDepth = 0;
WEAVE_ERROR err = WEAVE_NO_ERROR;
PropertyPathHandle curProperty = aHandle;
// Walk up the path till root and keep track of the handles encountered along the way.
while (curProperty != kRootPropertyPathHandle)
{
pathWalkStore[pathWalkDepth++] = curProperty;
curProperty = GetParent(curProperty);
}
// Write it into TLV by reverse walking over the encountered handles starting from root.
while (pathWalkDepth)
{
PropertyPathHandle curHandle = pathWalkStore[pathWalkDepth - 1];
err = aPathWriter.PutNull(GetTag(curHandle));
SuccessOrExit(err);
pathWalkDepth--;
}
exit:
return WEAVE_NO_ERROR;
}
uint64_t TraitSchemaEngine::GetTag(PropertyPathHandle aHandle) const
{
if (IsDictionary(GetParent(aHandle)))
{
return ProfileTag(kWeaveProfile_DictionaryKey, GetPropertyDictionaryKey(aHandle));
}
else
{
return ContextTag(GetMap(aHandle)->mContextTag);
}
}
WEAVE_ERROR TraitSchemaEngine::RetrieveData(PropertyPathHandle aHandle, uint64_t aTagToWrite, nl::Weave::TLV::TLVWriter & aWriter,
IGetDataDelegate * aDelegate, IDirtyPathCut * updateDirtyPathCut) const
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
if (IsLeaf(aHandle) || IsNullable(aHandle) || IsOptional(aHandle))
{
bool isPresent = true, isNull = false;
err = aDelegate->GetData(aHandle, aTagToWrite, aWriter, isNull, isPresent);
SuccessOrExit(err);
if (!isPresent && !(IsOptional(aHandle) || IsEphemeral(aHandle)))
{
err = WEAVE_ERROR_WDM_SCHEMA_MISMATCH;
}
VerifyOrExit(isPresent, /* no-op, either no error or set above */);
if (isNull)
{
if (!IsNullable(aHandle))
{
err = WEAVE_ERROR_WDM_SCHEMA_MISMATCH;
}
else
{
err = aWriter.PutNull(aTagToWrite);
SuccessOrExit(err);
}
}
VerifyOrExit(!isNull, /* no-op, either no error or set above */);
}
if (!IsLeaf(aHandle))
{
TLVType type;
err = aWriter.StartContainer(aTagToWrite, kTLVType_Structure, type);
SuccessOrExit(err);
#if TDM_ENABLE_PUBLISHER_DICTIONARY_SUPPORT
if (IsDictionary(aHandle))
{
PropertyDictionaryKey dictionaryItemKey;
uintptr_t context = 0;
if (NULL == updateDirtyPathCut)
{
// TODO: this looks like RetrieveUpdatableDictionaryData; must avoid the duplication
// if it's a dictionary, we need to iterate through the items in the container by asking our delegate.
while ((err = aDelegate->GetNextDictionaryItemKey(aHandle, context, dictionaryItemKey)) == WEAVE_NO_ERROR)
{
uint64_t tag = ProfileTag(kWeaveProfile_DictionaryKey, dictionaryItemKey);
PropertySchemaHandle itemHandle = GetFirstChild(aHandle);
VerifyOrExit(itemHandle != kNullPropertyPathHandle, err = WEAVE_ERROR_WDM_SCHEMA_MISMATCH);
err = RetrieveData(CreatePropertyPathHandle(itemHandle, dictionaryItemKey), tag, aWriter, aDelegate);
SuccessOrExit(err);
}
VerifyOrExit(err == WEAVE_END_OF_INPUT, );
err = WEAVE_NO_ERROR;
}
else
{
// Looks like we come down to a dictionary during a recursion started by TraitUpdatableDataSink::ReadData,
// because updateDirtyPathCut is not NULL.
// The dictionary is supposed to be replaced completely.
// The way this is implemented is:
// - an empty dictionary is encoded here;
// - the handle of the dictionary is put back in the queue.
// The empty dictionary is encoded here because if the dictionary is not a child of the
// current DataElement's path, it cannot be omitted.
// The dictionary will be encoded in a new DataElement as a "merge".
// The reason for that is that if the dictionary is too large to fit in the payload,
// it's easier to split it in more than one data element outside of a recursion.
if (aDelegate->GetNextDictionaryItemKey(aHandle, context, dictionaryItemKey) == WEAVE_NO_ERROR)
{
err = updateDirtyPathCut->CutPath(aHandle, this);
SuccessOrExit(err);
}
}
}
else
#endif // TDM_ENABLE_PUBLISHER_DICTIONARY_SUPPORT
{
PropertyPathHandle childProperty;
// Recursively iterate over all child nodes and call RetrieveData on them.
for (childProperty = GetFirstChild(aHandle); !IsNullPropertyPathHandle(childProperty);
childProperty = GetNextChild(aHandle, childProperty))
{
const PropertyInfo * childInfo = GetMap(childProperty);
err = RetrieveData(childProperty, ContextTag(childInfo->mContextTag), aWriter, aDelegate, updateDirtyPathCut);
SuccessOrExit(err);
}
}
err = aWriter.EndContainer(type);
SuccessOrExit(err);
} // if (!IsLeaf(aHandle))
exit:
return err;
}
#if WEAVE_CONFIG_ENABLE_WDM_UPDATE
WEAVE_ERROR TraitSchemaEngine::RetrieveUpdatableDictionaryData(PropertyPathHandle aHandle,
uint64_t aTagToWrite,
TLVWriter & aWriter,
IGetDataDelegate * aDelegate,
PropertyPathHandle & aPropertyPathHandleOfDictItemToStartFrom) const
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
#if TDM_ENABLE_PUBLISHER_DICTIONARY_SUPPORT
nl::Weave::TLV::TLVType dataContainerType;
PropertyDictionaryKey dictionaryItemKey;
uintptr_t context = 0;
uint32_t numKeysEncoded = 0;
PropertySchemaHandle dictionaryItemSchemaHandle = GetPropertySchemaHandle(GetFirstChild(aHandle));
PropertyPathHandle dictionaryItemPathHandle;
PropertyPathHandle itemToSkipTo = aPropertyPathHandleOfDictItemToStartFrom;
VerifyOrExit(IsDictionary(aHandle), err = WEAVE_ERROR_WDM_SCHEMA_MISMATCH);
// Clear the key to start from; it will be set again below if we can't encode all remaing
// items.
aPropertyPathHandleOfDictItemToStartFrom = kNullPropertyPathHandle;
err = aWriter.StartContainer(aTagToWrite,
nl::Weave::TLV::kTLVType_Structure, dataContainerType);
SuccessOrExit(err);
while ((err = aDelegate->GetNextDictionaryItemKey(aHandle, context, dictionaryItemKey)) == WEAVE_NO_ERROR)
{
uint64_t tag = ProfileTag(kWeaveProfile_DictionaryKey, dictionaryItemKey);
dictionaryItemPathHandle = CreatePropertyPathHandle(dictionaryItemSchemaHandle, dictionaryItemKey);
if (dictionaryItemPathHandle < itemToSkipTo)
{
continue;
}
TLVWriter backupWriter = aWriter;
aPropertyPathHandleOfDictItemToStartFrom = kNullPropertyPathHandle;
err = RetrieveData(dictionaryItemPathHandle, tag, aWriter, aDelegate);
if (err != WEAVE_NO_ERROR)
{
WeaveLogDetail(DataManagement, "Dictionary item whith path 0x%" PRIx32 ", tag 0x% " PRIx64 " failed with error % " PRIu32 "",
dictionaryItemPathHandle, tag, err);
}
if (numKeysEncoded > 0 &&
((err == WEAVE_ERROR_BUFFER_TOO_SMALL) || (err == WEAVE_ERROR_NO_MEMORY)))
{
// BUFFER_TOO_SMALL means there is no more space in the current buffer.
// NO_MEMORY means the application is trying to build a chain of pBufs, but
// there are no more buffers.
aWriter = backupWriter;
aPropertyPathHandleOfDictItemToStartFrom = dictionaryItemPathHandle;
err = WEAVE_NO_ERROR;
break;
}
SuccessOrExit(err);
numKeysEncoded++;
}
if (err == WEAVE_END_OF_INPUT)
{
err = WEAVE_NO_ERROR;
}
SuccessOrExit(err);
err = aWriter.EndContainer(dataContainerType);
SuccessOrExit(err);
exit:
#endif // TDM_ENABLE_PUBLISHER_DICTIONARY_SUPPORT
return err;
}
WEAVE_ERROR TraitSchemaEngine::GetRelativePathTags(const PropertyPathHandle aCandidateHandle,
uint64_t *aTags,
const uint32_t aTagsSize,
uint32_t &aNumTags) const
{
PropertyPathHandle pathWalkStore[mSchema.mTreeDepth];
uint32_t pathWalkDepth = 0;
PropertyPathHandle curProperty;
WEAVE_ERROR err = WEAVE_NO_ERROR;
aNumTags = 0;
if (aCandidateHandle != kRootPropertyPathHandle)
{
curProperty = aCandidateHandle;
while (curProperty != kRootPropertyPathHandle)
{
pathWalkStore[pathWalkDepth++] = curProperty;
curProperty = GetParent(curProperty);
}
VerifyOrExit(aTagsSize >= pathWalkDepth, err = WEAVE_ERROR_NO_MEMORY);
// Write it into TLV by reverse walking over the encountered handles starting from root.
while (pathWalkDepth)
{
PropertyPathHandle curHandle = pathWalkStore[pathWalkDepth - 1];
aTags[aNumTags] = GetTag(curHandle);
pathWalkDepth--;
aNumTags++;
}
}
exit:
return err;
}
#endif // WEAVE_CONFIG_ENABLE_WDM_UPDATE
WEAVE_ERROR TraitSchemaEngine::StoreData(PropertyPathHandle aHandle, TLVReader & aReader, ISetDataDelegate * aDelegate, IPathFilter * apPathFilter) const
{
WEAVE_ERROR err = WEAVE_NO_ERROR;
TLVType type = kTLVType_NotSpecified;
PropertyPathHandle curHandle = aHandle;
PropertyPathHandle parentHandle = kNullPropertyPathHandle;
bool dictionaryEventSignalled = false;
PropertyPathHandle dictionaryItemHandle;
bool descending = true;
VerifyOrExit(!(apPathFilter != NULL && apPathFilter->FilterPath(curHandle)), );
// While the logic to actually parse out dictionaries is relatively easy, the logic to appropriately emit the
// OnReplace and OnItemModified events is not similarly so.
//
// This logic here deals with the case where this function was called with a path made to a dictionary *element* or deeper.
// The logic further below deals with the cases where this function was called on a path handle at the dictionary or higher.
if (IsInDictionary(curHandle, dictionaryItemHandle))
{
aDelegate->OnSetDataEvent(ISetDataDelegate::kSetDataEvent_DictionaryItemModifyBegin, dictionaryItemHandle);
dictionaryEventSignalled = true;
}
if (IsLeaf(curHandle))
{
err = aDelegate->SetData(curHandle, aReader, aReader.GetType() == kTLVType_Null);
SuccessOrExit(err);
}
else
{
// The crux of this loop is to iteratively parse out TLV and descend into the schema as necessary. The loop is bounded
// by the return of the iterator handle (curHandle) back to the original start point (aHandle).
//
// The loop also has a notion of ascension and descension. Descension occurs when you go deeper into the schema tree while
// ascension is returning back to a higher point in the tree.
do
{
if (!(apPathFilter != NULL && apPathFilter->FilterPath(curHandle))) {
#if TDM_DISABLE_STRICT_SCHEMA_COMPLIANCE
if (!IsNullPropertyPathHandle(curHandle))
#endif
{
if (!IsLeaf(curHandle)) {
if (descending) {
bool enterContainer = (aReader.GetType() != kTLVType_Null);
if (enterContainer) {
err = aReader.EnterContainer(type);
SuccessOrExit(err);
parentHandle = curHandle;
} else {
if (IsNullable(curHandle)) {
err = aDelegate->SetData(curHandle, aReader, !enterContainer);
} else {
err = WEAVE_ERROR_WDM_SCHEMA_MISMATCH;
}
SuccessOrExit(err);
}
}
} else {
err = aDelegate->SetData(curHandle, aReader, aReader.GetType() == kTLVType_Null);
SuccessOrExit(err);
// Setting a leaf data can be interpreted as ascension since you are evaluating another node
// at the same level there-after by going back up to your parent and checking for more children.
descending = false;
}
}
if (!descending) {
if (IsDictionary(curHandle)) {
// We can surmise this is a replace if we're ascending to a node that is a dictionary, and that node
// is lower than the target node this function was directed at (we can't get to this point in code if the
// two handles (target and current) are equivalent to each other).
aDelegate->OnSetDataEvent(ISetDataDelegate::kSetDataEvent_DictionaryReplaceEnd, curHandle);
} else if (IsDictionary(parentHandle)) {
// We can surmise this is a modify/add if we're ascending to a node whose parent is a dictionary, and that node
// is lower than the target node this function was directed at (we can't get to this point in code if the
// two handles (target and current) are equivalent to each other). Those cases are handled by the two 'if'
// statements at the top and bottom of this function.
aDelegate->OnSetDataEvent(ISetDataDelegate::kSetDataEvent_DictionaryItemModifyEnd, curHandle);
}
}
}
// Get the next element in this container.
err = aReader.Next();
VerifyOrExit((err == WEAVE_NO_ERROR) || (err == WEAVE_END_OF_TLV), );
if (err == WEAVE_END_OF_TLV) {
// We've hit the end of the container - exit out and point our current handle to its parent.
// In the process, restore the parentHandle as well.
err = aReader.ExitContainer(type);
SuccessOrExit(err);
curHandle = parentHandle;
parentHandle = GetParent(curHandle);
descending = false;
} else {
const uint64_t tag = aReader.GetTag();
descending = true;
if (IsProfileTag(tag)) {
VerifyOrExit(ProfileIdFromTag(tag) == kWeaveProfile_DictionaryKey,
err = WEAVE_ERROR_INVALID_TLV_TAG);
curHandle = GetDictionaryItemHandle(parentHandle, TagNumFromTag(tag));
} else {
curHandle = GetChildHandle(parentHandle, TagNumFromTag(tag));
}
if (!(apPathFilter != NULL && apPathFilter->FilterPath(curHandle))) {
if (IsDictionary(curHandle)) {
// If we're descending onto a node that is a dictionary, we know for certain that it is a replace operation
// since the target path handle for this function was higher in the tree than the node representing the
// dictionary itself.
aDelegate->OnSetDataEvent(ISetDataDelegate::kSetDataEvent_DictionaryReplaceBegin, curHandle);
} else if (IsDictionary(parentHandle)) {
// Alternatively, if we're descending onto a node whose parent is a dictionary, we know that this node
// represents an element in the dictionary and as such, is an appropriate point in the traversal to notify the
// application of an upcoming dictionary item modification/insertion.
aDelegate->OnSetDataEvent(ISetDataDelegate::kSetDataEvent_DictionaryItemModifyBegin,
curHandle);
}
}
#if !TDM_DISABLE_STRICT_SCHEMA_COMPLIANCE
if (IsNullPropertyPathHandle(curHandle)) {
err = WEAVE_ERROR_TLV_TAG_NOT_FOUND;
break;
}
#endif
}
} while (curHandle != aHandle);
}
if (dictionaryEventSignalled)
{
aDelegate->OnSetDataEvent(ISetDataDelegate::kSetDataEvent_DictionaryItemModifyEnd, dictionaryItemHandle);
}
exit:
return err;
}
PropertyPathHandle TraitSchemaEngine::GetFirstChild(PropertyPathHandle aParentHandle) const
{
return GetNextChild(aParentHandle, kRootPropertyPathHandle);
}
bool TraitSchemaEngine::IsParent(PropertyPathHandle aChildHandle, PropertyPathHandle aParentHandle) const
{
bool retval = false;
VerifyOrExit(aChildHandle != kNullPropertyPathHandle &&
aParentHandle != kNullPropertyPathHandle, );
do
{
aChildHandle = GetParent(aChildHandle);
if (aChildHandle == aParentHandle)
{
ExitNow(retval = true);
}
} while (aChildHandle != kNullPropertyPathHandle);
exit:
return retval;
}
PropertyPathHandle TraitSchemaEngine::GetParent(PropertyPathHandle aHandle) const
{
PropertySchemaHandle schemaHandle = GetPropertySchemaHandle(aHandle);
PropertyDictionaryKey dictionaryKey = GetPropertyDictionaryKey(aHandle);
const PropertyInfo * handleMap = GetMap(schemaHandle);
if (!handleMap)
{
return kNullPropertyPathHandle;
}
// update the schema handle to point to the parent handle.
schemaHandle = handleMap->mParentHandle;
// if the parent is a dictionary, just return the schema handle with the key cleared out since the key doesn't make sense
// anymore at this level or higher.
if (IsDictionary(schemaHandle))
{
return schemaHandle;
}
else
{
// Otherwise, preserve the dictionaroy key in the new path handle being created.
return CreatePropertyPathHandle(schemaHandle, dictionaryKey);
}
return WEAVE_NO_ERROR;
}
PropertyPathHandle TraitSchemaEngine::GetNextChild(PropertyPathHandle aParentHandle, PropertyPathHandle aChildHandle) const
{
unsigned int i;
PropertySchemaHandle parentSchemaHandle = GetPropertySchemaHandle(aParentHandle);
PropertySchemaHandle childSchemaHandle = GetPropertySchemaHandle(aChildHandle);
PropertyDictionaryKey parentDictionaryKey = GetPropertyDictionaryKey(aParentHandle);
// Starting from 1 node after the child node that's been passed in, iterate till we find the next child belonging to aParentId.
for (i = (childSchemaHandle - 1); i < mSchema.mNumSchemaHandleEntries; i++)
{
if (mSchema.mSchemaHandleTbl[i].mParentHandle == parentSchemaHandle)
{
break;
}
}
if (i == mSchema.mNumSchemaHandleEntries)
{
return kNullPropertyPathHandle;
}
else
{
return CreatePropertyPathHandle(i + kHandleTableOffset, parentDictionaryKey);
}
}
PropertyPathHandle TraitSchemaEngine::GetChildHandle(PropertyPathHandle aParentHandle, uint8_t aContextTag) const
{
if (IsDictionary(aParentHandle))
{
return kNullPropertyPathHandle;
}
return _GetChildHandle(aParentHandle, aContextTag);
}
PropertyPathHandle TraitSchemaEngine::_GetChildHandle(PropertyPathHandle aParentHandle, uint8_t aContextTag) const
{
for (PropertyPathHandle childProperty = GetFirstChild(aParentHandle); !IsNullPropertyPathHandle(childProperty);
childProperty = GetNextChild(aParentHandle, childProperty))
{
if (mSchema.mSchemaHandleTbl[GetPropertySchemaHandle(childProperty) - kHandleTableOffset].mContextTag == aContextTag)
{
return childProperty;
}
}
return kNullPropertyPathHandle;
}
PropertyPathHandle TraitSchemaEngine::GetDictionaryItemHandle(PropertyPathHandle aParentHandle, uint16_t aDictionaryKey) const
{
if (!IsDictionary(aParentHandle))
{
return kNullPropertyPathHandle;
}
return CreatePropertyPathHandle(_GetChildHandle(aParentHandle, 0), aDictionaryKey);
}
bool TraitSchemaEngine::IsLeaf(PropertyPathHandle aHandle) const
{
PropertySchemaHandle schemaHandle = GetPropertySchemaHandle(aHandle);
// Root is by definition not a leaf. This also conveniently handles the cases where we have traits that
// don't have any properties in them.
if (aHandle == kRootPropertyPathHandle)
{
return false;
}
else
{
for (unsigned int i = 0; i < mSchema.mNumSchemaHandleEntries; i++)
{
if (mSchema.mSchemaHandleTbl[i].mParentHandle == schemaHandle)
{
return false;
}
}
return true;
}
}
int32_t TraitSchemaEngine::GetDepth(PropertyPathHandle aHandle) const
{
int depth = 0;
PropertySchemaHandle schemaHandle = GetPropertySchemaHandle(aHandle);
if (schemaHandle > (mSchema.mNumSchemaHandleEntries + 1))
{
return -1;
}
while (schemaHandle != kRootPropertyPathHandle)
{
depth++;
schemaHandle = mSchema.mSchemaHandleTbl[schemaHandle - kHandleTableOffset].mParentHandle;
}
return depth;
}
PropertyPathHandle TraitSchemaEngine::FindLowestCommonAncestor(PropertyPathHandle aHandle1, PropertyPathHandle aHandle2,
PropertyPathHandle * aHandle1BranchChild,
PropertyPathHandle * aHandle2BranchChild) const
{
int32_t depth1 = GetDepth(aHandle1);
int32_t depth2 = GetDepth(aHandle2);
PropertyPathHandle laggingHandle1, laggingHandle2;
if (depth1 < 0 || depth2 < 0)
{
return kNullPropertyPathHandle;
}
laggingHandle1 = kNullPropertyPathHandle;
laggingHandle2 = kNullPropertyPathHandle;
while (depth1 != depth2)
{
if (depth1 > depth2)
{
laggingHandle1 = aHandle1;
aHandle1 = GetParent(aHandle1);
depth1--;
}
else
{
laggingHandle2 = aHandle2;
aHandle2 = GetParent(aHandle2);
depth2--;
}
}
while (aHandle1 != aHandle2)
{
laggingHandle1 = aHandle1;
laggingHandle2 = aHandle2;
aHandle1 = GetParent(aHandle1);
aHandle2 = GetParent(aHandle2);
}
if (aHandle1BranchChild)
{
*aHandle1BranchChild = laggingHandle1;
}
if (aHandle2BranchChild)
{
*aHandle2BranchChild = laggingHandle2;
}
return aHandle1;
}
const TraitSchemaEngine::PropertyInfo * TraitSchemaEngine::GetMap(PropertyPathHandle aHandle) const
{
PropertySchemaHandle schemaHandle = GetPropertySchemaHandle(aHandle);
if (schemaHandle < 2 || (schemaHandle >= (mSchema.mNumSchemaHandleEntries + kHandleTableOffset)))
{
return NULL;
}
return &mSchema.mSchemaHandleTbl[schemaHandle - kHandleTableOffset];
}
bool TraitSchemaEngine::IsDictionary(PropertyPathHandle aHandle) const
{
// The 'mIsDictionaryBitfield' is only populated by code-gen on traits that do have dictionaries. Otherwise, it defaults
// to NULL.
return GetBitFromPathHandleBitfield(mSchema.mIsDictionaryBitfield, aHandle);
}
bool TraitSchemaEngine::IsInDictionary(PropertyPathHandle aHandle, PropertyPathHandle & aDictionaryItemHandle) const
{
while (aHandle != kRootPropertyPathHandle)
{
PropertyPathHandle parentHandle = GetParent(aHandle);
if (IsDictionary(parentHandle))
{
aDictionaryItemHandle = aHandle;
return true;
}
aHandle = parentHandle;
}
return false;
}
bool TraitSchemaEngine::IsOptional(PropertyPathHandle aHandle) const
{
return GetBitFromPathHandleBitfield(mSchema.mIsOptionalBitfield, aHandle);
}
bool TraitSchemaEngine::IsNullable(PropertyPathHandle aHandle) const
{
return GetBitFromPathHandleBitfield(mSchema.mIsNullableBitfield, aHandle);
}
bool TraitSchemaEngine::IsEphemeral(PropertyPathHandle aHandle) const
{
return GetBitFromPathHandleBitfield(mSchema.mIsEphemeralBitfield, aHandle);
}
bool TraitSchemaEngine::GetBitFromPathHandleBitfield(uint8_t * aBitfield, PropertyPathHandle aPathHandle) const
{
bool retval = false;
if (aBitfield != NULL && !IsRootPropertyPathHandle(aPathHandle) && !IsNullPropertyPathHandle(aPathHandle))
{
PropertySchemaHandle adjustedSchemaHandle = GetPropertySchemaHandle(aPathHandle) - kHandleTableOffset;
retval = aBitfield[(adjustedSchemaHandle) / 8] & (1 << (adjustedSchemaHandle % 8));
}
return retval;
}
bool TraitSchemaEngine::MatchesProfileId(uint32_t aProfileId) const
{
return (aProfileId == mSchema.mProfileId);
}
uint32_t TraitSchemaEngine::GetProfileId(void) const
{
return mSchema.mProfileId;
}
bool TraitSchemaEngine::GetVersionIntersection(SchemaVersionRange & aVersion, SchemaVersionRange & aIntersection) const
{
SchemaVersion minCurrentVersion = 1;
SchemaVersion maxCurrentVersion = 1;
#if (TDM_VERSIONING_SUPPORT)
if (mSchema.mVersionRange != NULL)
{
minCurrentVersion = mSchema.mVersionRange->mMinVersion;
maxCurrentVersion = mSchema.mVersionRange->mMaxVersion;
}
#endif // TDM_VERSIONING_SUPPORT
aIntersection.mMinVersion = max(aVersion.mMinVersion, minCurrentVersion);
aIntersection.mMaxVersion = min(aVersion.mMaxVersion, maxCurrentVersion);
if (aIntersection.mMinVersion <= aIntersection.mMaxVersion)