| 1 | # ex:ts=8 sw=4: | ||||||||||
| 2 | # $OpenBSD$ | ||||||||||
| 3 | # | ||||||||||
| 4 | # Copyright (c) 2026 Dick Olsson <hi@senzilla.io> | ||||||||||
| 5 | # | ||||||||||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||||||||||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||||||||||
| 8 | # copyright notice and this permission notice appear in all copies. | ||||||||||
| 9 | # | ||||||||||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||||||||||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||||||||||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||||||||||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||||||||||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||||||||||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||||||||||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||||||||||
| 17 | |||||||||||
| 18 | 7 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | package Protocol::HAP; | ||||||||||
| 21 | 7 | our $VERSION = '0.1.0'; | |||||||||
| 22 | |||||||||||
| 23 | # Protocol::HAP - the HomeKit Accessory Protocol as a library. | ||||||||||
| 24 | # | ||||||||||
| 25 | # The namespace holds the complete protocol: the TLV8 and HTTP codecs, | ||||||||||
| 26 | # the crypto primitives, SRP-6a, the pairing and session state | ||||||||||
| 27 | # machines, the accessory data model, the sans-IO accessory-server | ||||||||||
| 28 | # engine, and a controller. It is self-contained: core Perl plus the | ||||||||||
| 29 | # declared Crypt::* modules, and nothing else. The host owns sockets, | ||||||||||
| 30 | # timers, logging, and persistence, injected through narrow contracts. | ||||||||||
| 31 | # | ||||||||||
| 32 | # This module is the umbrella. It holds the null logger, the default | ||||||||||
| 33 | # for every class that takes a logger argument, and the short-UUID | ||||||||||
| 34 | # rule that the data model shares. See Protocol/HAP.pod for the host | ||||||||||
| 35 | # contracts. | ||||||||||
| 36 | |||||||||||
| 37 | # The HAP base UUID suffix of every Apple-defined type | ||||||||||
| 38 | 7 | ▶ | use constant HAP_BASE_UUID => '-0000-1000-8000-0026BB765291'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | # uuid_to_short($uuid): | ||||||||||
| 41 | # Convert a full UUID to the short form for JSON. The function | ||||||||||
| 42 | # returns a short hex string for Apple UUIDs and the full UUID | ||||||||||
| 43 | # for custom ones. | ||||||||||
| 44 | 35 | ▶ | sub uuid_to_short ($uuid) | ||||||||
|
Subroutine
uuid_to_short: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 45 | { | ||||||||||
| 46 | 35 | my $base = HAP_BASE_UUID; | |||||||||
| 47 | 35 | ▶ | if ( $uuid =~ /^0*([0-9A-Fa-f]+)\Q$base\E$/i ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 33 | return uc($1); | |||||||||
| 49 | } | ||||||||||
| 50 | 2 | return $uuid; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | # device_id($ltpk): | ||||||||||
| 54 | # The MAC-format device id of an accessory: the first six bytes | ||||||||||
| 55 | # of the long-term public key, as uppercase colon-separated hex. | ||||||||||
| 56 | # The server advertises it and pair-setup signs over it, so both | ||||||||||
| 57 | # must format it the same way. | ||||||||||
| 58 | 0 | ▶ | sub device_id ($ltpk) | ||||||||
|
Subroutine
device_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 59 | { | ||||||||||
| 60 | 0 | my $id = uc( unpack( 'H*', substr( $ltpk, 0, 6 ) ) ); | |||||||||
| 61 | 0 | return join( ':', $id =~ /../g ); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | # $class->null_logger: | ||||||||||
| 65 | # Return the shared null logger instance. Every message | ||||||||||
| 66 | # disappears. A class that takes no logger argument uses it. | ||||||||||
| 67 | 87 | ▶ | sub null_logger ($) | ||||||||
|
Subroutine
null_logger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 68 | { | ||||||||||
| 69 | 87 | state $null = bless {}, 'Protocol::HAP::Log::Null'; | |||||||||
| 70 | 87 | return $null; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | package Protocol::HAP::Log::Null; | ||||||||||
| 74 | 7 | our $VERSION = '0.1.0'; | |||||||||
| 75 | |||||||||||
| 76 | # The null logger. It answers the four methods of the logger contract | ||||||||||
| 77 | # and drops every message. The host injects a real logger to see them. | ||||||||||
| 78 | |||||||||||
| 79 | 61 | ▶ | sub debug ( $, @ ) { return } | ||||||||
|
Subroutine
debug: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 0 | ▶ | sub info ( $, @ ) { return } | ||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
| |||||||||||
| 81 | 1 | ▶ | sub warning ( $, @ ) { return } | ||||||||
|
Subroutine
warning: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 0 | ▶ | sub error ( $, @ ) { return } | ||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
| |||||||||||
| 83 | |||||||||||
| 84 | 7 | 1; | |||||||||