| 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 | 2 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Protocol::HAP::Store::Memory; | ||||||||||||||||||||||||||||
| 21 | 2 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Protocol::HAP::Store::Memory - the store contract over plain hashes. | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # This is the reference implementation of the twelve store methods | ||||||||||||||||||||||||||||
| 26 | # that Protocol/HAP/Store.pod documents. Nothing persists: the state | ||||||||||||||||||||||||||||
| 27 | # lives in the object and dies with it. Tests and embedders start | ||||||||||||||||||||||||||||
| 28 | # here; a production host implements the same contract over durable | ||||||||||||||||||||||||||||
| 29 | # storage. | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | # The mutating pairing methods increment the configuration number | ||||||||||||||||||||||||||||
| 32 | # themselves. A store that skips the increment breaks c# in one | ||||||||||||||||||||||||||||
| 33 | # direction; an engine that adds one on top breaks it in the other. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | sub new ($class) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | 5 | return bless { | |||||||||||||||||||||||||||
| 38 | ltsk => undef, | ||||||||||||||||||||||||||||
| 39 | ltpk => undef, | ||||||||||||||||||||||||||||
| 40 | pairings => {}, | ||||||||||||||||||||||||||||
| 41 | config_number => 1, | ||||||||||||||||||||||||||||
| 42 | config_digest => undef, | ||||||||||||||||||||||||||||
| 43 | auth_attempts => 0, | ||||||||||||||||||||||||||||
| 44 | }, $class; | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # $self->load_accessory_keys: | ||||||||||||||||||||||||||||
| 48 | # Return ($ltsk, $ltpk), or the empty list when no identity is | ||||||||||||||||||||||||||||
| 49 | # stored yet. | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub load_accessory_keys ($self) | ||||||||||||||||||||||||||
|
Subroutine
load_accessory_keys: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 2 | ▶ | return () unless defined $self->{ltsk} && defined $self->{ltpk}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"ltsk"} and defined $self->{"ltpk"}
MC/DC50%1/2
defined $self->{"ltsk"} and defined $self->{"ltpk"}
defined $self->{"ltsk"}
defined $self->{"ltpk"}
Truth table66%2/3
A and B
A= defined $self->{"ltsk"}B= defined $self->{"ltpk"}
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | return ( $self->{ltsk}, $self->{ltpk} ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub save_accessory_keys ( $self, $ltsk, $ltpk ) | ||||||||||||||||||||||||||
|
Subroutine
save_accessory_keys: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 1 | $self->{ltsk} = $ltsk; | |||||||||||||||||||||||||||
| 60 | 1 | $self->{ltpk} = $ltpk; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | return; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # $self->load_pairings: | ||||||||||||||||||||||||||||
| 66 | # Return the pairings as a hash reference keyed by controller | ||||||||||||||||||||||||||||
| 67 | # id. Each value holds ltpk and permissions. | ||||||||||||||||||||||||||||
| 68 | 6 | ▶ | sub load_pairings ($self) | ||||||||||||||||||||||||||
|
Subroutine
load_pairings: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | # A copy, so a caller that edits the result cannot bypass the | ||||||||||||||||||||||||||||
| 71 | # increment rule of the mutating methods | ||||||||||||||||||||||||||||
| 72 | return { | ||||||||||||||||||||||||||||
| 73 | 10 | map { $_ => { %{ $self->{pairings}{$_} } } } | |||||||||||||||||||||||||||
| 74 | 6 | keys %{ $self->{pairings} } }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | sub save_pairing ( $self, $controller_id, $ltpk, $permissions = 1 ) | ||||||||||||||||||||||||||
|
Subroutine
save_pairing: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 6 | $self->{pairings}{$controller_id} = { | |||||||||||||||||||||||||||
| 80 | ltpk => $ltpk, | ||||||||||||||||||||||||||||
| 81 | permissions => $permissions, | ||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | 6 | $self->increment_config_number; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | sub remove_pairing ( $self, $controller_id ) | ||||||||||||||||||||||||||
|
Subroutine
remove_pairing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 4 | delete $self->{pairings}{$controller_id}; | |||||||||||||||||||||||||||
| 91 | 4 | $self->increment_config_number; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 4 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | ▶ | sub remove_all_pairings ($self) | ||||||||||||||||||||||||||
|
Subroutine
remove_all_pairings: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 2 | $self->{pairings} = {}; | |||||||||||||||||||||||||||
| 99 | 2 | $self->increment_config_number; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | return; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # The configuration number starts at 1 and only ever goes up. A | ||||||||||||||||||||||||||||
| 105 | # controller that sees it go backwards drops the accessory. | ||||||||||||||||||||||||||||
| 106 | 12 | ▶ | sub get_config_number ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_config_number: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 12 | return $self->{config_number}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 13 | ▶ | sub increment_config_number ($self) | ||||||||||||||||||||||||||
|
Subroutine
increment_config_number: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||
| 113 | 13 | return ++$self->{config_number}; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub get_config_digest ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_config_digest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 2 | return $self->{config_digest}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | sub save_config_digest ( $self, $digest ) | ||||||||||||||||||||||||||
|
Subroutine
save_config_digest: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 1 | $self->{config_digest} = $digest; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | return; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | sub get_auth_attempts ($self) | ||||||||||||||||||||||||||
|
Subroutine
get_auth_attempts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 3 | return $self->{auth_attempts}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | sub set_auth_attempts ( $self, $count ) | ||||||||||||||||||||||||||
|
Subroutine
set_auth_attempts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 2 | $self->{auth_attempts} = $count; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | 1; | |||||||||||||||||||||||||||