Skip to content

Commit 46432ad

Browse files
Improve logging in MTREncodeTLVFromDataValueDictionary.
"Object to encode is corrupt" was not very clear, and now log the value we fail to encode when we fail to encode a value.
1 parent 3136753 commit 46432ad

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/darwin/Framework/CHIP/MTRBaseDevice.mm

+15-6
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue
576576
}
577577
}
578578

579-
static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVWriter & writer, chip::TLV::Tag tag)
579+
static CHIP_ERROR MTREncodeTLVFromDataValueDictionaryInternal(id object, chip::TLV::TLVWriter & writer, chip::TLV::Tag tag)
580580
{
581581
if (![object isKindOfClass:[NSDictionary class]]) {
582582
MTR_LOG_ERROR("Error: Unsupported object to encode: %@", [object class]);
@@ -585,7 +585,7 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW
585585
NSString * typeName = ((NSDictionary *) object)[MTRTypeKey];
586586
id value = ((NSDictionary *) object)[MTRValueKey];
587587
if (![typeName isKindOfClass:[NSString class]]) {
588-
MTR_LOG_ERROR("Error: Object to encode is corrupt");
588+
MTR_LOG_ERROR("Error: Object to encode has no MTRTypeKey: %@", object);
589589
return CHIP_ERROR_INVALID_ARGUMENT;
590590
}
591591

@@ -632,14 +632,14 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW
632632
MTR_LOG_ERROR("Error: Object to encode has corrupt UTF8 string type: %@", [value class]);
633633
return CHIP_ERROR_INVALID_ARGUMENT;
634634
}
635-
return writer.PutString(tag, [value UTF8String]);
635+
return writer.PutString(tag, AsCharSpan(value));
636636
}
637637
if ([typeName isEqualToString:MTROctetStringValueType]) {
638638
if (![value isKindOfClass:[NSData class]]) {
639639
MTR_LOG_ERROR("Error: Object to encode has corrupt octet string type: %@", [value class]);
640640
return CHIP_ERROR_INVALID_ARGUMENT;
641641
}
642-
return writer.Put(tag, chip::ByteSpan(static_cast<const uint8_t *>([value bytes]), [value length]));
642+
return writer.Put(tag, AsByteSpan(value));
643643
}
644644
if ([typeName isEqualToString:MTRStructureValueType]) {
645645
if (![value isKindOfClass:[NSArray class]]) {
@@ -674,7 +674,7 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW
674674
tag = TLV::ContextTag(static_cast<uint8_t>(tagValue));
675675
}
676676
ReturnErrorOnFailure(
677-
MTREncodeTLVFromDataValueDictionary(elementValue, writer, tag));
677+
MTREncodeTLVFromDataValueDictionaryInternal(elementValue, writer, tag));
678678
}
679679
ReturnErrorOnFailure(writer.EndContainer(outer));
680680
return CHIP_NO_ERROR;
@@ -696,7 +696,7 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW
696696
MTR_LOG_ERROR("Error: Array element to encode has corrupt value: %@", element);
697697
return CHIP_ERROR_INVALID_ARGUMENT;
698698
}
699-
ReturnErrorOnFailure(MTREncodeTLVFromDataValueDictionary(elementValue, writer, chip::TLV::AnonymousTag()));
699+
ReturnErrorOnFailure(MTREncodeTLVFromDataValueDictionaryInternal(elementValue, writer, chip::TLV::AnonymousTag()));
700700
}
701701
ReturnErrorOnFailure(writer.EndContainer(outer));
702702
return CHIP_NO_ERROR;
@@ -705,6 +705,15 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW
705705
return CHIP_ERROR_INVALID_ARGUMENT;
706706
}
707707

708+
static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVWriter & writer, chip::TLV::Tag tag)
709+
{
710+
CHIP_ERROR err = MTREncodeTLVFromDataValueDictionaryInternal(object, writer, tag);
711+
if (err != CHIP_NO_ERROR) {
712+
MTR_LOG_ERROR("Failed to encode to TLV: %@", object);
713+
}
714+
return err;
715+
}
716+
708717
NSData * _Nullable MTREncodeTLVFromDataValueDictionary(NSDictionary<NSString *, id> * value, NSError * __autoreleasing * error)
709718
{
710719
// A single data item cannot be bigger than a packet, so just use 1200 bytes

0 commit comments

Comments
 (0)