| 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 | use v5.36; | ||
| 19 | |||
| 20 | package App::OpenHAP::Test::Integration; | ||
| 21 | our $VERSION = '0.1.0'; | ||
| 22 | |||
| 23 | use IO::Socket::INET; | ||
| 24 | use Time::HiRes qw(sleep); | ||
| 25 | |||
| 26 | use App::OpenHAP::Devices; | ||
| 27 | use Fugu::Config; | ||
| 28 | use Protocol::HAP::HTTP; | ||
| 29 | use Fugu::Log; | ||
| 30 | use Fugu::Process; | ||
| 31 | |||
| 32 | use constant { | ||
| 33 | DEFAULT_CONFIG => '/etc/openhapd.conf', | ||
| 34 | DEFAULT_HAP_PORT => 51827, | ||
| 35 | DEFAULT_HAP_PIN => '1995-1018', | ||
| 36 | DEFAULT_MQTT_HOST => '127.0.0.1', | ||
| 37 | DEFAULT_MQTT_PORT => 1883, | ||
| 38 | SYSLOG_FILE => '/var/log/daemon', | ||
| 39 | PIDFILE => '/var/run/openhapd.pid', | ||
| 40 | DB_PATH => '/var/db/openhapd', | ||
| 41 | |||
| 42 | # The bound on one response the helper accumulates. The | ||
| 43 | # accessory database of a bridge is the largest message HAP | ||
| 44 | # sends. | ||
| 45 | MAX_RESPONSE => 1048576, | ||
| 46 | }; | ||
| 47 | |||
| 48 | use constant PAIRINGS_FILE => DB_PATH . '/pairings.db'; | ||
| 49 | |||
| 50 | # The daemon keeps its counters here: the configuration number, the | ||
| 51 | # configuration digest, and the failed-attempt count. A wipe of the | ||
| 52 | # pairing state removes this file too, or the attempt counter of the | ||
| 53 | # last test survives into the next one. | ||
| 54 | use constant STATE_FILE => DB_PATH . '/state.json'; | ||
| 55 | |||
| 56 | sub new ( $class, %options ) | ||
| 57 | { | ||
| 58 | my $self = bless { | ||
| 59 | config_file => $options{config_file} // DEFAULT_CONFIG, | ||
| 60 | hap_port => $options{hap_port} // DEFAULT_HAP_PORT, | ||
| 61 | mqtt_host => $options{mqtt_host} // DEFAULT_MQTT_HOST, | ||
| 62 | mqtt_port => $options{mqtt_port} // DEFAULT_MQTT_PORT, | ||
| 63 | sockets => [], | ||
| 64 | controllers => [], | ||
| 65 | mqtt => undef, | ||
| 66 | }, $class; | ||
| 67 | |||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub setup ($self) | ||
| 72 | { | ||
| 73 | # Make sure the tests run in integration test mode | ||
| 74 | die "OPENHAP_INTEGRATION_TEST not set\n" | ||
| 75 | unless $ENV{OPENHAP_INTEGRATION_TEST}; | ||
| 76 | |||
| 77 | # The controller drives the accessory's own crypto and pairing | ||
| 78 | # code in this process, and that code reports through the | ||
| 79 | # process default logger. The default writes to standard error, | ||
| 80 | # which is the TAP stream. Install a quiet one. | ||
| 81 | Fugu::Log->set_default( | ||
| 82 | Fugu::Log->new( | ||
| 83 | mode => 'quiet', | ||
| 84 | ident => 'openhap-integration' | ||
| 85 | ) ); | ||
| 86 | |||
| 87 | # Check the system prerequisites | ||
| 88 | $self->_verify_system or die "System prerequisites not met\n"; | ||
| 89 | |||
| 90 | # Parse the configuration | ||
| 91 | $self->_parse_config; | ||
| 92 | |||
| 93 | # Make sure the daemon is running | ||
| 94 | $self->ensure_daemon_running or die "Cannot start openhapd daemon\n"; | ||
| 95 | |||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub teardown ($self) | ||
| 100 | { | ||
| 101 | # Close any controller connections | ||
| 102 | for my $controller ( @{ $self->{controllers} } ) { | ||
| 103 | $controller->close if defined $controller; | ||
| 104 | } | ||
| 105 | $self->{controllers} = []; | ||
| 106 | |||
| 107 | # Close any open sockets | ||
| 108 | $self->close_sockets; | ||
| 109 | |||
| 110 | # Disconnect MQTT if it is connected | ||
| 111 | if ( defined $self->{mqtt} ) { | ||
| 112 | eval { undef $self->{mqtt}; }; | ||
| 113 | } | ||
| 114 | |||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | # $self->get_controller(%args): | ||
| 119 | # Construct an Protocol::HAP::Controller for the configured | ||
| 120 | # host/port/PIN. The harness tracks the connection and closes | ||
| 121 | # it in teardown. | ||
| 122 | sub get_controller ( $self, %args ) | ||
| 123 | { | ||
| 124 | require Protocol::HAP::Controller; | ||
| 125 | |||
| 126 | # The timeout default is generous: under TCG emulation, one | ||
| 127 | # SRP modexp can take many seconds. OPENHAP_TEST_TIMEOUT | ||
| 128 | # raises it further. | ||
| 129 | my $controller = Protocol::HAP::Controller->new( | ||
| 130 | host => '127.0.0.1', | ||
| 131 | port => $self->{hap_port}, | ||
| 132 | pin => $self->get_config_value('hap_pin') // DEFAULT_HAP_PIN, | ||
| 133 | controller_id => 'openhap-test-ctrl', | ||
| 134 | timeout => $ENV{OPENHAP_TEST_TIMEOUT} // 30, | ||
| 135 | %args, | ||
| 136 | ); | ||
| 137 | |||
| 138 | push @{ $self->{controllers} }, $controller; | ||
| 139 | |||
| 140 | return $controller; | ||
| 141 | } | ||
| 142 | |||
| 143 | # $self->ensure_unpaired(): | ||
| 144 | # Make sure the daemon is unpaired. When stored | ||
| 145 | # pairings exist, stop the daemon, wipe the pairing state, and | ||
| 146 | # start it again. The wipe keeps the accessory identity. A | ||
| 147 | # POST /identify probes the post-condition. It succeeds only | ||
| 148 | # while the daemon is unpaired (HAP-HTTP.md §3). | ||
| 149 | sub ensure_unpaired ($self) | ||
| 150 | { | ||
| 151 | if ( $self->_has_pairings ) { | ||
| 152 | $self->ensure_daemon_stopped or return; | ||
| 153 | |||
| 154 | for my $file ( PAIRINGS_FILE, STATE_FILE, | ||
| 155 | DB_PATH . '/auth_attempts' ) | ||
| 156 | { | ||
| 157 | next unless -e $file; | ||
| 158 | unlink $file or do { | ||
| 159 | warn "Cannot remove $file: $!\n"; | ||
| 160 | return; | ||
| 161 | }; | ||
| 162 | } | ||
| 163 | |||
| 164 | $self->ensure_daemon_running or return; | ||
| 165 | } | ||
| 166 | |||
| 167 | return $self->_verify_unpaired; | ||
| 168 | } | ||
| 169 | |||
| 170 | # $self->_has_pairings(): | ||
| 171 | # Return true when the pairings database holds at least one | ||
| 172 | # entry. The daemon leaves comment headers in the file after | ||
| 173 | # its first save. Thus a size check cannot tell paired from | ||
| 174 | # unpaired. Parse for non-comment lines instead. | ||
| 175 | sub _has_pairings ($self) | ||
| 176 | { | ||
| 177 | open my $fh, '<', PAIRINGS_FILE or return 0; | ||
| 178 | while (<$fh>) { | ||
| 179 | next if /^#/ || /^\s*$/; | ||
| 180 | close $fh; | ||
| 181 | return 1; | ||
| 182 | } | ||
| 183 | close $fh; | ||
| 184 | |||
| 185 | return 0; | ||
| 186 | } | ||
| 187 | |||
| 188 | # $self->_verify_unpaired(): | ||
| 189 | # Probe the pairing state with POST /identify. The daemon | ||
| 190 | # returns 204 only when unpaired. It returns 400 with | ||
| 191 | # {"status":-70401} when still paired (HAP-HTTP.md §3). This | ||
| 192 | # sub fails loudly on the paired answer. It closes the probe | ||
| 193 | # socket immediately. The socket does not stay registered with | ||
| 194 | # the daemon until teardown. | ||
| 195 | sub _verify_unpaired ($self) | ||
| 196 | { | ||
| 197 | my $before = scalar @{ $self->{sockets} }; | ||
| 198 | my $response = $self->http_request( 'POST', '/identify' ); | ||
| 199 | for my $socket ( splice @{ $self->{sockets} }, $before ) { | ||
| 200 | $socket->close if defined $socket; | ||
| 201 | } | ||
| 202 | |||
| 203 | unless ( defined $response ) { | ||
| 204 | warn "No response to identify probe\n"; | ||
| 205 | return; | ||
| 206 | } | ||
| 207 | |||
| 208 | my ($status) = parse_http_response($response); | ||
| 209 | return 1 if defined $status && $status == 204; | ||
| 210 | |||
| 211 | warn sprintf "Daemon not unpaired: identify returned %s\n", | ||
| 212 | $status // 'no status'; | ||
| 213 | |||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | # $self->close_sockets(): | ||
| 218 | # Close and forget every raw socket that http_request opened. | ||
| 219 | # Then a probe connection does not stay registered with the | ||
| 220 | # daemon until teardown. | ||
| 221 | sub close_sockets ($self) | ||
| 222 | { | ||
| 223 | for my $socket ( @{ $self->{sockets} } ) { | ||
| 224 | $socket->close if defined $socket; | ||
| 225 | } | ||
| 226 | $self->{sockets} = []; | ||
| 227 | |||
| 228 | return 1; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub http_request ( $self, $method, $path, $body = undef, $headers = {} ) | ||
| 232 | { | ||
| 233 | my $socket = IO::Socket::INET->new( | ||
| 234 | PeerAddr => '127.0.0.1', | ||
| 235 | PeerPort => $self->{hap_port}, | ||
| 236 | Proto => 'tcp', | ||
| 237 | Timeout => 2, | ||
| 238 | ); | ||
| 239 | return unless defined $socket; | ||
| 240 | |||
| 241 | push @{ $self->{sockets} }, $socket; | ||
| 242 | |||
| 243 | print {$socket} Protocol::HAP::HTTP::build_request( | ||
| 244 | method => $method, | ||
| 245 | path => $path, | ||
| 246 | body => $body, | ||
| 247 | headers => { Host => "127.0.0.1:$self->{hap_port}", %$headers }, | ||
| 248 | ); | ||
| 249 | $socket->flush; | ||
| 250 | |||
| 251 | # Read until the buffer holds one whole message. A stream | ||
| 252 | # socket gives whatever arrived, which is not a message. | ||
| 253 | my $response = ''; | ||
| 254 | while (1) { | ||
| 255 | last | ||
| 256 | if Protocol::HAP::HTTP::message_complete( $response, | ||
| 257 | max_size => MAX_RESPONSE ); | ||
| 258 | |||
| 259 | my $bytes = $socket->sysread( my $chunk, 65536 ); | ||
| 260 | last unless $bytes; | ||
| 261 | $response .= $chunk; | ||
| 262 | } | ||
| 263 | |||
| 264 | return $response; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub parse_http_response ($response) | ||
| 268 | { | ||
| 269 | return unless defined $response; | ||
| 270 | |||
| 271 | my $parsed = Protocol::HAP::HTTP::parse_response($response) or return; | ||
| 272 | |||
| 273 | return ( $parsed->{status}, $parsed->{headers}, $parsed->{body} ); | ||
| 274 | } | ||
| 275 | |||
| 276 | # status($response): | ||
| 277 | # Return the status code of an HTTP response, for the tests | ||
| 278 | # that need nothing else from it. | ||
| 279 | sub status ($response) | ||
| 280 | { | ||
| 281 | my ($status) = parse_http_response($response); | ||
| 282 | |||
| 283 | return $status; | ||
| 284 | } | ||
| 285 | |||
| 286 | # $self->find_char($database, $type, %opt): | ||
| 287 | # Find a characteristic by its short type string in a decoded | ||
| 288 | # /accessories structure. The walk skips the bridge (aid 1). | ||
| 289 | # The option name limits the walk to the accessory whose Name | ||
| 290 | # characteristic holds that value. The option ev demands the ev | ||
| 291 | # permission. Return (aid, iid, characteristic) on a match. | ||
| 292 | # Return the empty list when there is no match. | ||
| 293 | sub find_char ( $self, $database, $type, %opt ) | ||
| 294 | { | ||
| 295 | for my $accessory ( @{ $database->{accessories} // [] } ) { | ||
| 296 | next if $accessory->{aid} == 1; | ||
| 297 | next | ||
| 298 | if defined $opt{name} | ||
| 299 | && !_accessory_named( $accessory, $opt{name} ); | ||
| 300 | for my $service ( @{ $accessory->{services} } ) { | ||
| 301 | for my $char ( @{ $service->{characteristics} } ) { | ||
| 302 | next unless $char->{type} eq $type; | ||
| 303 | next | ||
| 304 | if $opt{ev} | ||
| 305 | && !grep { $_ eq 'ev' } | ||
| 306 | @{ $char->{perms} // [] }; | ||
| 307 | return ( $accessory->{aid}, $char->{iid}, | ||
| 308 | $char ); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | return; | ||
| 314 | } | ||
| 315 | |||
| 316 | # _accessory_named($accessory, $name): | ||
| 317 | # Return true when the accessory's Name characteristic (type | ||
| 318 | # 23) holds $name. | ||
| 319 | sub _accessory_named ( $accessory, $name ) | ||
| 320 | { | ||
| 321 | for my $service ( @{ $accessory->{services} } ) { | ||
| 322 | for my $char ( @{ $service->{characteristics} } ) { | ||
| 323 | return 1 | ||
| 324 | if $char->{type} eq '23' | ||
| 325 | && ( $char->{value} // '' ) eq $name; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | return 0; | ||
| 330 | } | ||
| 331 | |||
| 332 | # $self->wait_value($code, $want, $timeout): | ||
| 333 | # Poll $code->() every quarter second, up to $timeout seconds | ||
| 334 | # (default 10). A code reference in $want is the predicate over | ||
| 335 | # the polled value. Any other $want stops the poll when the | ||
| 336 | # value equals it as a string. Return 1 on success. Return | ||
| 337 | # undef on the deadline. | ||
| 338 | sub wait_value ( $self, $code, $want, $timeout = 10 ) | ||
| 339 | { | ||
| 340 | my $deadline = time + $timeout; | ||
| 341 | |||
| 342 | while (1) { | ||
| 343 | my $value = $code->(); | ||
| 344 | if ( ref $want eq 'CODE' ) { | ||
| 345 | return 1 if $want->($value); | ||
| 346 | } | ||
| 347 | elsif ( defined $value && "$value" eq "$want" ) { | ||
| 348 | return 1; | ||
| 349 | } | ||
| 350 | return if time >= $deadline; | ||
| 351 | sleep 0.25; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | sub get_config_value ( $self, $key ) | ||
| 356 | { | ||
| 357 | return $self->{config}{$key}; | ||
| 358 | } | ||
| 359 | |||
| 360 | # $self->get_device_topics(): | ||
| 361 | # Return the MQTT topic of each configured device that has one. | ||
| 362 | sub get_device_topics ($self) | ||
| 363 | { | ||
| 364 | return | ||
| 365 | map { $_->{topic} } grep { defined $_->{topic} } $self->get_devices; | ||
| 366 | } | ||
| 367 | |||
| 368 | # $self->get_devices(): | ||
| 369 | # Return the configured device records as a list of hashes with | ||
| 370 | # type, subtype, id, name, and topic. | ||
| 371 | sub get_devices ($self) | ||
| 372 | { | ||
| 373 | return @{ $self->{devices} // [] }; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub ensure_daemon_running ($self) | ||
| 377 | { | ||
| 378 | if ( !_rcctl( 'check', 'openhapd' ) ) { | ||
| 379 | _rcctl( 'start', 'openhapd' ); | ||
| 380 | sleep 1; | ||
| 381 | return if !_rcctl( 'check', 'openhapd' ); | ||
| 382 | } | ||
| 383 | |||
| 384 | # A daemon that runs per rcctl does not serve yet. At | ||
| 385 | # startup, the daemon publishes the mDNS advertisement and | ||
| 386 | # waits for the mdnsd replies. The HAP listener opens after | ||
| 387 | # that. Thus wait for the port, not for a fixed sleep. | ||
| 388 | return $self->wait_for_hap_port; | ||
| 389 | } | ||
| 390 | |||
| 391 | # $self->wait_for_hap_port($timeout): | ||
| 392 | # Wait until the HAP port accepts connections. Poll every | ||
| 393 | # quarter second, up to $timeout seconds. The default is 30 | ||
| 394 | # seconds, which is generous for TCG emulation. Return 1 when | ||
| 395 | # the daemon serves. Return undef on the deadline. | ||
| 396 | sub wait_for_hap_port ( $self, $timeout = 30 ) | ||
| 397 | { | ||
| 398 | my $port = $self->get_config_value('hap_port') // DEFAULT_HAP_PORT; | ||
| 399 | my $deadline = time + $timeout; | ||
| 400 | |||
| 401 | while ( time < $deadline ) { | ||
| 402 | my $socket = IO::Socket::INET->new( | ||
| 403 | PeerAddr => '127.0.0.1', | ||
| 404 | PeerPort => $port, | ||
| 405 | Proto => 'tcp', | ||
| 406 | Timeout => 2, | ||
| 407 | ); | ||
| 408 | if ( defined $socket ) { | ||
| 409 | $socket->close; | ||
| 410 | return 1; | ||
| 411 | } | ||
| 412 | sleep 0.25; | ||
| 413 | } | ||
| 414 | |||
| 415 | return; | ||
| 416 | } | ||
| 417 | |||
| 418 | sub ensure_daemon_stopped ($self) | ||
| 419 | { | ||
| 420 | return 1 if !_rcctl( 'check', 'openhapd' ); | ||
| 421 | |||
| 422 | _rcctl( 'stop', 'openhapd' ); | ||
| 423 | sleep 1; | ||
| 424 | |||
| 425 | return !_rcctl( 'check', 'openhapd' ); | ||
| 426 | } | ||
| 427 | |||
| 428 | # $self->restart_daemon(): | ||
| 429 | # Restart openhapd through rcctl. Then wait until the HAP port | ||
| 430 | # serves again. Return 1 when the daemon serves. Return undef | ||
| 431 | # on failure. | ||
| 432 | sub restart_daemon ($self) | ||
| 433 | { | ||
| 434 | _rcctl( 'restart', 'openhapd' ); | ||
| 435 | $self->wait_for_hap_port or return; | ||
| 436 | |||
| 437 | return 1; | ||
| 438 | } | ||
| 439 | |||
| 440 | # $self->ensure_mdnsd_running(): | ||
| 441 | # Make sure that mdnsd runs and continues to run. Start it if | ||
| 442 | # necessary. Then check again across a settle window. A | ||
| 443 | # point-in-time probe races green when mdnsd starts and then | ||
| 444 | # exits shortly after. On failure, emit the captured | ||
| 445 | # diagnostics. Then a dead mdnsd is diagnosable from the test | ||
| 446 | # output, and the sub does not fail bare. | ||
| 447 | sub ensure_mdnsd_running ($self) | ||
| 448 | { | ||
| 449 | my $check = sub { _rcctl( 'check', 'mdnsd' ) }; | ||
| 450 | |||
| 451 | unless ( $check->() ) { | ||
| 452 | _rcctl( 'enable', 'mdnsd' ); | ||
| 453 | _rcctl( 'start', 'mdnsd' ); | ||
| 454 | } | ||
| 455 | |||
| 456 | for my $probe ( 1 .. 3 ) { | ||
| 457 | sleep 1; | ||
| 458 | next if $check->(); | ||
| 459 | $self->_warn_mdnsd_diagnostics( | ||
| 460 | "mdnsd not running at settle probe $probe/3"); | ||
| 461 | return; | ||
| 462 | } | ||
| 463 | |||
| 464 | return 1; | ||
| 465 | } | ||
| 466 | |||
| 467 | # $self->browse(): | ||
| 468 | # Return one bounded mdnsctl observation of the advertised HAP | ||
| 469 | # services. | ||
| 470 | sub browse ($self) | ||
| 471 | { | ||
| 472 | return `timeout 5 mdnsctl browse hap tcp 2>&1 || true`; | ||
| 473 | } | ||
| 474 | |||
| 475 | # $self->browse_txt(): | ||
| 476 | # Return one bounded mdnsctl observation with the TXT strings | ||
| 477 | # resolved. | ||
| 478 | sub browse_txt ($self) | ||
| 479 | { | ||
| 480 | return `timeout 5 mdnsctl browse -r hap tcp 2>&1 || true`; | ||
| 481 | } | ||
| 482 | |||
| 483 | # $self->_warn_mdnsd_diagnostics($reason): | ||
| 484 | # Emit the captured mdnsd state as warnings for the failure | ||
| 485 | # diagnostics. The state holds the rcctl views, the process | ||
| 486 | # list, and recent syslog lines. | ||
| 487 | sub _warn_mdnsd_diagnostics ( $self, $reason ) | ||
| 488 | { | ||
| 489 | warn "$reason\n"; | ||
| 490 | warn 'rcctl get mdnsd: ' . _capture( 'rcctl', 'get', 'mdnsd' ); | ||
| 491 | |||
| 492 | my @processes = grep { /\bmdnsd\b/ && !/grep/ } | ||
| 493 | split /\n/, _capture( 'ps', '-axo', 'pid,command' ); | ||
| 494 | warn 'mdnsd processes: ' | ||
| 495 | . ( @processes ? join( "\n", @processes ) . "\n" : "none\n" ); | ||
| 496 | |||
| 497 | my @syslog = grep { /mdnsd/ } _read_syslog_tail(200); | ||
| 498 | @syslog = splice( @syslog, -20 ) if @syslog > 20; | ||
| 499 | warn "recent mdnsd syslog lines:\n" | ||
| 500 | . ( @syslog ? join( "\n", @syslog ) . "\n" : "none\n" ); | ||
| 501 | |||
| 502 | return; | ||
| 503 | } | ||
| 504 | |||
| 505 | # _rcctl(@args): | ||
| 506 | # Run rcctl and report whether it succeeded. The run captures its | ||
| 507 | # output, so a check does not print to the TAP stream. | ||
| 508 | sub _rcctl (@args) | ||
| 509 | { | ||
| 510 | return Fugu::Process->run( cmd => [ 'rcctl', @args ] )->{success}; | ||
| 511 | } | ||
| 512 | |||
| 513 | # _capture(@cmd): | ||
| 514 | # Run a command and return what it wrote, for a diagnostic. A | ||
| 515 | # command that will not start returns the empty string. | ||
| 516 | sub _capture (@cmd) | ||
| 517 | { | ||
| 518 | my $result = Fugu::Process->run( cmd => \@cmd ); | ||
| 519 | |||
| 520 | return ( $result->{stdout} // '' ) . ( $result->{stderr} // '' ); | ||
| 521 | } | ||
| 522 | |||
| 523 | # _read_syslog_tail($lines): | ||
| 524 | # Return the last lines of the system log. The read is a plain | ||
| 525 | # file read: a shell pipeline of tail and grep would need a | ||
| 526 | # shell, and every argument in it would need quoting. | ||
| 527 | sub _read_syslog_tail ($lines) | ||
| 528 | { | ||
| 529 | open my $fh, '<', SYSLOG_FILE or return (); | ||
| 530 | my @all = <$fh>; | ||
| 531 | close $fh; | ||
| 532 | |||
| 533 | @all = splice( @all, -$lines ) if @all > $lines; | ||
| 534 | chomp @all; | ||
| 535 | |||
| 536 | return @all; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub ensure_mqtt_running ($self) | ||
| 540 | { | ||
| 541 | # Check if the broker is already running | ||
| 542 | return 1 if _rcctl( 'check', 'mosquitto' ); | ||
| 543 | |||
| 544 | # Try to start the broker | ||
| 545 | _rcctl( 'start', 'mosquitto' ); | ||
| 546 | sleep 1; | ||
| 547 | |||
| 548 | # Make sure the broker started | ||
| 549 | return _rcctl( 'check', 'mosquitto' ); | ||
| 550 | } | ||
| 551 | |||
| 552 | sub get_mqtt ($self) | ||
| 553 | { | ||
| 554 | return $self->{mqtt} if defined $self->{mqtt}; | ||
| 555 | |||
| 556 | # Require Net::MQTT::Simple | ||
| 557 | eval { require Net::MQTT::Simple; }; | ||
| 558 | return if $@; | ||
| 559 | |||
| 560 | # Make sure the broker is running | ||
| 561 | return unless $self->ensure_mqtt_running; | ||
| 562 | |||
| 563 | # Create the connection | ||
| 564 | eval { | ||
| 565 | $self->{mqtt} = Net::MQTT::Simple->new( | ||
| 566 | "$self->{mqtt_host}:$self->{mqtt_port}"); | ||
| 567 | }; | ||
| 568 | |||
| 569 | return $self->{mqtt}; | ||
| 570 | } | ||
| 571 | |||
| 572 | sub _verify_system ($self) | ||
| 573 | { | ||
| 574 | # Check the required binaries | ||
| 575 | return unless -x '/usr/sbin/rcctl'; | ||
| 576 | return unless -x '/usr/local/bin/openhapd'; | ||
| 577 | return unless -x '/usr/local/bin/hapctl'; | ||
| 578 | |||
| 579 | # Check that the configuration exists | ||
| 580 | return unless -f $self->{config_file}; | ||
| 581 | return unless -r $self->{config_file}; | ||
| 582 | |||
| 583 | # Check that the system user exists | ||
| 584 | return | ||
| 585 | unless Fugu::Process->run( cmd => [ 'id', '_openhap' ] )->{success}; | ||
| 586 | |||
| 587 | # Check that the data directory exists | ||
| 588 | return unless -d '/var/db/openhapd'; | ||
| 589 | |||
| 590 | return 1; | ||
| 591 | } | ||
| 592 | |||
| 593 | # $self->_parse_config: | ||
| 594 | # Read the daemon's own configuration through the parser the | ||
| 595 | # daemon uses, and the device blocks through the reader the | ||
| 596 | # daemon uses. A second parser here would let the test agree | ||
| 597 | # with itself while it disagreed with openhapd. | ||
| 598 | sub _parse_config ($self) | ||
| 599 | { | ||
| 600 | my $config = Fugu::Config->new( file => $self->{config_file} ); | ||
| 601 | $config->load or return; | ||
| 602 | |||
| 603 | my %settings = map { $_ => $config->get($_) } $config->setting_names; | ||
| 604 | |||
| 605 | $self->{hap_port} = $settings{hap_port} if defined $settings{hap_port}; | ||
| 606 | $self->{config} = \%settings; | ||
| 607 | $self->{devices} = [ App::OpenHAP::Devices->devices($config) ]; | ||
| 608 | |||
| 609 | return 1; | ||
| 610 | } | ||
| 611 | |||
| 612 | 1; |