Skip to content

Commit

Permalink
Add a Darwin framework utility to convert data-value dictionaries to …
Browse files Browse the repository at this point in the history
…TLV in NSData. (#31666)

Not public API, just an internal utility.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Apr 23, 2024
1 parent 998567a commit e482ca4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,27 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW
return CHIP_ERROR_INVALID_ARGUMENT;
}

NSData * _Nullable MTREncodeTLVFromDataValueDictionary(NSDictionary<NSString *, id> * 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);

Expand Down
6 changes: 6 additions & 0 deletions src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,10 @@ static inline MTRTransportType MTRMakeTransportType(chip::Transport::Type type)
// Convert TLV data into data-value dictionary as described in MTRDeviceResponseHandler
NSDictionary<NSString *, id> * _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<NSString *, id> * value, NSError * __autoreleasing * error);

NS_ASSUME_NONNULL_END

0 comments on commit e482ca4

Please sign in to comment.