diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index 77b3e0160f68a3..a14986d2e24f5a 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -743,6 +743,27 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW return CHIP_ERROR_INVALID_ARGUMENT; } +NSData * _Nullable MTREncodeTLVFromDataValueDictionary(NSDictionary * value, NSError * __autoreleasing * error) +{ + // A single data item cannot be bigger than a packet, so just use 1200 bytes + // as the max size of our buffer. This assumes that lists will not be + // passed as-is to this method but will get chunked, with each list item + // passed to this method separately. + uint8_t buffer[1200]; + TLV::TLVWriter writer; + writer.Init(buffer); + + CHIP_ERROR err = MTREncodeTLVFromDataValueDictionary(value, writer, TLV::AnonymousTag()); + if (err != CHIP_NO_ERROR) { + if (error) { + *error = [MTRError errorForCHIPErrorCode:err]; + } + return nil; + } + + return AsData(ByteSpan(buffer, writer.GetLengthWritten())); +} + // Callback type to pass data value as an NSObject typedef void (*MTRDataValueDictionaryCallback)(void * context, id value); diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h index 606ebdfac3a2e8..71cdee11d3687d 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h @@ -230,4 +230,10 @@ static inline MTRTransportType MTRMakeTransportType(chip::Transport::Type type) // Convert TLV data into data-value dictionary as described in MTRDeviceResponseHandler NSDictionary * _Nullable MTRDecodeDataValueDictionaryFromCHIPTLV(chip::TLV::TLVReader * data); +// Convert a data-value dictionary as described in MTRDeviceResponseHandler into +// TLV Data with an anonymous tag. This method assumes the encoding of the +// value fits in a single UDP MTU; for lists this method might need to be used +// on each list item separately. +NSData * _Nullable MTREncodeTLVFromDataValueDictionary(NSDictionary * value, NSError * __autoreleasing * error); + NS_ASSUME_NONNULL_END