Skip to main content

Crate hap_tlv8

Crate hap_tlv8 

Source
Expand description

HomeKit Accessory Protocol (HAP) TLV8 encoding and decoding.

This is Milestone 1 of the hap-rust roadmap. Every later milestone (hap-crypto, hap-transport, …) depends on the types defined here: the pairing exchange messages are TLV8.

§The format

A TLV8 stream is a sequence of items, each (type: u8, length: u8, value). Two rules make it more than a trivial format:

  • Fragmentation. A value longer than 255 bytes is split across consecutive items of the same type; the reader concatenates them. Tlv8Writer::push fragments automatically; Tlv8Reader::parse reassembles.
  • Separators. A zero-length item of type SEPARATOR (0xFF) delimits repeated structures.

Integers are encoded little-endian at a fixed width per field (Tlv8Writer::push_u8 → 1 byte, Tlv8Writer::push_u16 → 2 bytes, and so on); there is no minimal-width trimming, because HAP integer characteristics declare their width.

§Fragmentation

A logical value longer than 255 bytes is encoded as consecutive items of the same type. Because a full 255-byte item means “more follows”, a value whose length is a non-zero exact multiple of 255 is terminated by a zero-length item of the same type. For example, a 256-byte value of type 0x09 encodes as [0x09, 0xFF, <255 bytes>, 0x09, 0x01, <1 byte>], and a 255-byte value as [0x09, 0xFF, <255 bytes>, 0x09, 0x00]. The reader reverses this transparently.

§Usage

use hap_tlv8::{Tlv8Writer, Tlv8Reader};

let mut bytes = Vec::new();
let mut w = Tlv8Writer::new(&mut bytes);
w.push(0x01, &[0xAB, 0xCD]);
assert_eq!(bytes, [0x01, 0x02, 0xAB, 0xCD]);

let items = Tlv8Reader::parse(&bytes).unwrap();
assert_eq!(items, vec![(0x01, vec![0xAB, 0xCD])]);

The doc-test uses unwrap(); real library code must propagate the Result instead.

Structs§

Tlv8Map
An owned, queryable view over reassembled TLV8 items.
Tlv8Reader
Stateless TLV8 decoder entry point.
Tlv8Writer
A TLV8 encoder that appends to a borrowed Vec<u8>.

Enums§

Tlv8Error
All errors hap-tlv8 can produce.

Constants§

SEPARATOR
TLV8 separator type (kTLVType_Separator). A zero-length item of this type delimits repeated structures, such as a list of pairings.

Type Aliases§

Result
Result<T, Tlv8Error> for convenience.