Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preparing data in TLV8

I'm writing a HomeKit (so perhaps Bluetooth) characteristic in TLV8 format. Apple doc says

The value is an NSData object containing a set of one or more TLV8's, which are packed type-length-value items with an 8-bit type, 8-bit length, and N-byte value.

According to Wikipeida a type-length value is

Type

A binary code, often simply alphanumeric, which indicates the kind of field that this part of the message represents;

Length

The size of the value field (typically in bytes);

Value

Variable-sized series of bytes which contains data for this part of the message.

I have no idea how to pack one. I suppose I can write raw bytes to NSData, but what do I write for pad, if I need any padding, etc. So is there an example of how to do that?

like image 928
huggie Avatar asked May 19 '17 07:05

huggie


1 Answers

Oh I figured it out. TLV8 consist of three sections: "Tag", "Length", and "Value". I don't know what 8 means.

Both tag and length are UInt8. I believe what the tag may be depend on where the TLV8 is used. Length is the length of the value. Value is the content it self.

So when I want to send a simple 1 as a value, I use:

let tag = 0x02 // For example
let length = 0x01
let value = 0x01
let data = Data(bytes: [tag, length, value]) // NSData
like image 60
huggie Avatar answered Nov 03 '22 13:11

huggie