@@ -475,8 +475,21 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
475
475
}];
476
476
}
477
477
478
+ static NSDictionary <NSString *, id > * _MakeDataValueDictionary (NSString * type, id _Nullable value, NSNumber * _Nullable dataVersion)
479
+ {
480
+ if (value && dataVersion) {
481
+ return @ { MTRTypeKey : type, MTRValueKey : value, MTRDataVersionKey : dataVersion };
482
+ } else if (value) {
483
+ return @ { MTRTypeKey : type, MTRValueKey : value };
484
+ } else if (dataVersion) {
485
+ return @ { MTRTypeKey : type, MTRDataVersionKey : dataVersion };
486
+ } else {
487
+ return @ { MTRTypeKey : type };
488
+ }
489
+ }
490
+
478
491
// Convert TLV data into data-value dictionary as described in MTRDeviceResponseHandler
479
- NSDictionary <NSString *, id > * _Nullable MTRDecodeDataValueDictionaryFromCHIPTLV (chip::TLV::TLVReader * data)
492
+ NSDictionary <NSString *, id > * _Nullable MTRDecodeDataValueDictionaryFromCHIPTLV (chip::TLV::TLVReader * data, NSNumber * dataVersion )
480
493
{
481
494
chip::TLV::TLVType dataTLVType = data->GetType ();
482
495
switch (dataTLVType) {
@@ -487,8 +500,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
487
500
MTR_LOG_ERROR (" Error(%s): TLV signed integer decoding failed" , chip::ErrorStr (err));
488
501
return nil ;
489
502
}
490
- return [NSDictionary dictionaryWithObjectsAndKeys: MTRSignedIntegerValueType, MTRTypeKey, [NSNumber numberWithLongLong: val],
491
- MTRValueKey, nil ];
503
+ return _MakeDataValueDictionary (MTRSignedIntegerValueType, @(val), dataVersion);
492
504
}
493
505
case chip::TLV::kTLVType_UnsignedInteger : {
494
506
uint64_t val;
@@ -497,8 +509,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
497
509
MTR_LOG_ERROR (" Error(%s): TLV unsigned integer decoding failed" , chip::ErrorStr (err));
498
510
return nil ;
499
511
}
500
- return [NSDictionary dictionaryWithObjectsAndKeys: MTRUnsignedIntegerValueType, MTRTypeKey,
501
- [NSNumber numberWithUnsignedLongLong: val], MTRValueKey, nil ];
512
+ return _MakeDataValueDictionary (MTRUnsignedIntegerValueType, @(val), dataVersion);
502
513
}
503
514
case chip::TLV::kTLVType_Boolean : {
504
515
bool val;
@@ -507,24 +518,22 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
507
518
MTR_LOG_ERROR (" Error(%s): TLV boolean decoding failed" , chip::ErrorStr (err));
508
519
return nil ;
509
520
}
510
- return [NSDictionary
511
- dictionaryWithObjectsAndKeys: MTRBooleanValueType, MTRTypeKey, [NSNumber numberWithBool: val], MTRValueKey, nil ];
521
+ return _MakeDataValueDictionary (MTRBooleanValueType, @(val), dataVersion);
512
522
}
513
523
case chip::TLV::kTLVType_FloatingPointNumber : {
514
524
// Try float first
515
525
float floatValue;
516
526
CHIP_ERROR err = data->Get (floatValue);
517
527
if (err == CHIP_NO_ERROR) {
518
- return @ { MTRTypeKey : MTRFloatValueType, MTRValueKey : [ NSNumber numberWithFloat: floatValue] } ;
528
+ return _MakeDataValueDictionary ( MTRFloatValueType, @( floatValue), dataVersion) ;
519
529
}
520
530
double val;
521
531
err = data->Get (val);
522
532
if (err != CHIP_NO_ERROR) {
523
533
MTR_LOG_ERROR (" Error(%s): TLV floating point decoding failed" , chip::ErrorStr (err));
524
534
return nil ;
525
535
}
526
- return [NSDictionary
527
- dictionaryWithObjectsAndKeys: MTRDoubleValueType, MTRTypeKey, [NSNumber numberWithDouble: val], MTRValueKey, nil ];
536
+ return _MakeDataValueDictionary (MTRDoubleValueType, @(val), dataVersion);
528
537
}
529
538
case chip::TLV::kTLVType_UTF8String : {
530
539
CharSpan stringValue;
@@ -538,7 +547,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
538
547
MTR_LOG_ERROR (" Error(%s): TLV UTF8String value is not actually UTF-8" , err.AsString ());
539
548
return nil ;
540
549
}
541
- return @ { MTRTypeKey : MTRUTF8StringValueType, MTRValueKey : stringObj } ;
550
+ return _MakeDataValueDictionary ( MTRUTF8StringValueType, stringObj, dataVersion) ;
542
551
}
543
552
case chip::TLV::kTLVType_ByteString : {
544
553
ByteSpan bytesValue;
@@ -547,10 +556,10 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
547
556
MTR_LOG_ERROR (" Error(%s): TLV ByteString decoding failed" , chip::ErrorStr (err));
548
557
return nil ;
549
558
}
550
- return @ { MTRTypeKey : MTROctetStringValueType, MTRValueKey : AsData (bytesValue) } ;
559
+ return _MakeDataValueDictionary ( MTROctetStringValueType, AsData (bytesValue), dataVersion) ;
551
560
}
552
561
case chip::TLV::kTLVType_Null : {
553
- return [ NSDictionary dictionaryWithObjectsAndKeys: MTRNullValueType, MTRTypeKey, nil ] ;
562
+ return _MakeDataValueDictionary ( MTRNullValueType, nil , dataVersion) ;
554
563
}
555
564
case chip::TLV::kTLVType_Structure :
556
565
case chip::TLV::kTLVType_Array : {
@@ -606,7 +615,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
606
615
MTR_LOG_ERROR (" Error(%s): TLV container exiting failed" , chip::ErrorStr (err));
607
616
return nil ;
608
617
}
609
- return [ NSDictionary dictionaryWithObjectsAndKeys: typeName, MTRTypeKey, array, MTRValueKey, nil ] ;
618
+ return _MakeDataValueDictionary ( typeName, array, dataVersion) ;
610
619
}
611
620
default :
612
621
MTR_LOG_ERROR (" Error: Unsupported TLV type for conversion: %u" , (unsigned ) data->GetType ());
@@ -815,7 +824,7 @@ CHIP_ERROR Encode(chip::TLV::TLVWriter & writer, chip::TLV::Tag tag) const
815
824
class BufferedReadClientCallback final : public app::ReadClient::Callback {
816
825
public:
817
826
using OnSuccessAttributeCallbackType
818
- = std::function<void (const ConcreteAttributePath & aPath, const DecodableValueType & aData)>;
827
+ = std::function<void (const ConcreteDataAttributePath & aPath, const DecodableValueType & aData)>;
819
828
using OnSuccessEventCallbackType = std::function<void (const EventHeader & aEventHeader, const DecodableValueType & aData)>;
820
829
using OnErrorCallbackType = std::function<void (
821
830
const app::ConcreteAttributePath * attributePath, const app::ConcreteEventPath * eventPath, CHIP_ERROR aError)>;
@@ -1027,6 +1036,16 @@ - (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attri
1027
1036
params : (MTRReadParams * _Nullable)params
1028
1037
queue : (dispatch_queue_t )queue
1029
1038
completion : (MTRDeviceResponseHandler)completion
1039
+ {
1040
+ [self readAttributePaths: attributePaths eventPaths: eventPaths params: params includeDataVersion: NO queue: queue completion: completion];
1041
+ }
1042
+
1043
+ - (void )readAttributePaths : (NSArray <MTRAttributeRequestPath *> * _Nullable)attributePaths
1044
+ eventPaths : (NSArray <MTREventRequestPath *> * _Nullable)eventPaths
1045
+ params : (MTRReadParams * _Nullable)params
1046
+ includeDataVersion : (BOOL )includeDataVersion
1047
+ queue : (dispatch_queue_t )queue
1048
+ completion : (MTRDeviceResponseHandler)completion
1030
1049
{
1031
1050
if ((attributePaths == nil || [attributePaths count ] == 0 ) && (eventPaths == nil || [eventPaths count ] == 0 )) {
1032
1051
// No paths, just return an empty array.
@@ -1056,11 +1075,20 @@ - (void)readAttributePaths:(NSArray<MTRAttributeRequestPath *> * _Nullable)attri
1056
1075
1057
1076
auto resultArray = [[NSMutableArray alloc ] init ];
1058
1077
auto onAttributeSuccessCb
1059
- = [resultArray](const ConcreteAttributePath & aAttributePath, const MTRDataValueDictionaryDecodableType & aData) {
1060
- [resultArray addObject: @ {
1061
- MTRAttributePathKey : [[MTRAttributePath alloc ] initWithPath: aAttributePath],
1062
- MTRDataKey : aData.GetDecodedObject ()
1063
- }];
1078
+ = [resultArray, includeDataVersion](const ConcreteDataAttributePath & aAttributePath, const MTRDataValueDictionaryDecodableType & aData) {
1079
+ // TODO: move this logic into MTRDataValueDictionaryDecodableType
1080
+ if (includeDataVersion && aAttributePath.mDataVersion .HasValue ()) {
1081
+ NSDictionary * dataValue = aData.GetDecodedObject ();
1082
+ [resultArray addObject: @{
1083
+ MTRAttributePathKey : [[MTRAttributePath alloc ] initWithPath: aAttributePath],
1084
+ MTRDataKey : _MakeDataValueDictionary (dataValue[MTRTypeKey], dataValue[MTRValueKey], @(aAttributePath.mDataVersion .Value ()))
1085
+ }];
1086
+ } else {
1087
+ [resultArray addObject: @ {
1088
+ MTRAttributePathKey : [[MTRAttributePath alloc ] initWithPath: aAttributePath],
1089
+ MTRDataKey : aData.GetDecodedObject ()
1090
+ }];
1091
+ }
1064
1092
};
1065
1093
1066
1094
auto onEventSuccessCb
0 commit comments