| 1 | 2 | ▶ | use v5.36; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | |||||||||||||
| 3 | package Protocol::HAP::TLV; | ||||||||||||
| 4 | 2 | our $VERSION = '0.1.0'; | |||||||||||
| 5 | |||||||||||||
| 6 | # TLV8 encoding and decoding for the HomeKit Accessory Protocol | ||||||||||||
| 7 | # The format is Type-Length-Value with 8-bit type and length | ||||||||||||
| 8 | # fields. The codec splits values of more than 255 bytes into | ||||||||||||
| 9 | # multiple chunks with the same type. The pairing type values, | ||||||||||||
| 10 | # including the 0xFF separator, live in Protocol::HAP::Pairing. | ||||||||||||
| 11 | |||||||||||||
| 12 | # encode(@items) - Encode type-value pairs in order | ||||||||||||
| 13 | # The function takes a list of type, value pairs. It keeps the | ||||||||||||
| 14 | # insertion order. | ||||||||||||
| 15 | # Example: encode(0x06, $state, 0x03, $pubkey) | ||||||||||||
| 16 | 6 | ▶ | sub encode (@items) | ||||||||||
|
Subroutine
encode: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 17 | { | ||||||||||||
| 18 | 6 | my $out = ''; | |||||||||||
| 19 | |||||||||||||
| 20 | 6 | ▶ | while ( @items >= 2 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 16 | my $type = shift @items; | |||||||||||
| 22 | 16 | my $value = shift @items; | |||||||||||
| 23 | |||||||||||||
| 24 | # Use the empty string for an undefined value | ||||||||||||
| 25 | 16 | ▶ | $value //= ''; | ||||||||||
|
Condition50%1/2
$value //= ""
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||
| 26 | |||||||||||||
| 27 | # Encode empty values, for example the separator 0xFF | ||||||||||||
| 28 | 16 | ▶ | if ( length($value) == 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 3 | $out .= pack( 'CC', $type, 0 ); | |||||||||||
| 30 | 3 | next; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | # Split values of more than 255 bytes into chunks | ||||||||||||
| 34 | 13 | ▶ | while ( length($value) > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 14 | my $chunk = substr( $value, 0, 255, '' ); | |||||||||||
| 36 | 14 | $out .= pack( 'CC', $type, length($chunk) ) . $chunk; | |||||||||||
| 37 | } | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | 6 | return $out; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | # decode($data) - Decode a TLV8 buffer into a type => value hash | ||||||||||||
| 44 | # The function concatenates consecutive records with the same | ||||||||||||
| 45 | # type (fragmentation). It returns the empty list if the buffer | ||||||||||||
| 46 | # is malformed. A malformed buffer has a truncated record header | ||||||||||||
| 47 | # or a length field that points past the end of the buffer. | ||||||||||||
| 48 | 2 | ▶ | sub decode ($data) | ||||||||||
|
Subroutine
decode: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||
| 49 | { | ||||||||||||
| 50 | 2 | my %items; | |||||||||||
| 51 | 2 | my $pos = 0; | |||||||||||
| 52 | |||||||||||||
| 53 | 2 | ▶ | while ( $pos < length($data) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | |||||||||||||
| 55 | # Reject a truncated record header | ||||||||||||
| 56 | 4 | ▶ | return if $pos + 2 > length($data); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 4 | my ( $type, $len ) = unpack( 'CC', substr( $data, $pos, 2 ) ); | |||||||||||
| 58 | 4 | $pos += 2; | |||||||||||
| 59 | |||||||||||||
| 60 | # Reject a length that runs past the end of the buffer | ||||||||||||
| 61 | 4 | ▶ | return if $pos + $len > length($data); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 4 | my $value = substr( $data, $pos, $len ); | |||||||||||
| 63 | 4 | $pos += $len; | |||||||||||
| 64 | |||||||||||||
| 65 | # Concatenate chunks with the same type | ||||||||||||
| 66 | 4 | ▶ | $items{$type} = ( $items{$type} // '' ) . $value; | ||||||||||
|
Condition100%2/2
$items{$type} // ""
MC/DC100%1/1
$items{$type} // ""
$items{$type}
Truth table100%2/2
A
A= $items{$type}
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 2 | return %items; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 2 | 1; | |||||||||||