From 91d98fb0939c8fdf651d3ccf771590dca860a8db Mon Sep 17 00:00:00 2001 From: David Boulay Date: Mon, 5 Oct 2026 16:31:28 +0200 Subject: [PATCH] Report Bluetooth device failures and verify pairing --- bin/omarchy-bluetooth-device | 52 ++++++++++++++++++++++------ test/shell.d/bluetooth-test.sh | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 10 deletions(-) diff --git a/bin/omarchy-bluetooth-device b/bin/omarchy-bluetooth-device index d1f0b3705e9c..5d9755267307 100755 --- a/bin/omarchy-bluetooth-device +++ b/bin/omarchy-bluetooth-device @@ -25,28 +25,60 @@ power_on() { omarchy-bluetooth-power on || true } -trust_device() { - bluetoothctl trust "$address" >/dev/null 2>&1 || true +device_error() { + local message="$1" + echo "$message" >&2 + omarchy-notification-send --app-name Bluetooth -u normal "Bluetooth $action failed" "$message" || true + exit 1 +} + +run_device_command() { + local seconds="$1" command="$2" output + if output=$(timeout "$seconds" bluetoothctl "$command" "$address" 2>&1); then + # bluetoothctl can print a failure while returning success. + if [[ $output == *"Failed to "* || $output == *"not available"* ]]; then + device_error "$output" + fi + else + device_error "${output:-Bluetooth $command timed out or failed for $address}" + fi +} + +device_has_state() { + local info + info=$(timeout 3s bluetoothctl info "$address" 2>/dev/null) || return 1 + [[ $info == *"$1: yes"* ]] +} + +connect_device() { + if ! device_has_state Connected; then + run_device_command 20s connect + device_has_state Connected || device_error "Connection did not complete for $address" + fi } case "$action" in pair) power_on - timeout 20s bluetoothctl pair "$address" >/dev/null 2>&1 || true - trust_device - timeout 20s bluetoothctl connect "$address" >/dev/null 2>&1 || true + run_device_command 20s pair + device_has_state Paired || device_error "Pairing did not complete for $address" + run_device_command 10s trust + connect_device ;; connect) power_on - trust_device - timeout 20s bluetoothctl connect "$address" >/dev/null 2>&1 || true + run_device_command 10s trust + connect_device ;; disconnect) - timeout 10s bluetoothctl disconnect "$address" >/dev/null 2>&1 || true + run_device_command 10s disconnect ;; forget) power_on - timeout 10s bluetoothctl disconnect "$address" >/dev/null 2>&1 || true - timeout 10s bluetoothctl remove "$address" >/dev/null 2>&1 || true + # An already disconnected device can still be forgotten. + if device_has_state Connected; then + run_device_command 10s disconnect + fi + run_device_command 10s remove ;; esac diff --git a/test/shell.d/bluetooth-test.sh b/test/shell.d/bluetooth-test.sh index b20727e6d9c4..e1def9783d4f 100644 --- a/test/shell.d/bluetooth-test.sh +++ b/test/shell.d/bluetooth-test.sh @@ -156,6 +156,20 @@ cat >"$mock_bin/bluetoothctl" <<'SH' #!/bin/bash printf '%s\n' "$*" >>"$BLUETOOTHCTL_LOG" +if [[ $1 == "${MOCK_DEVICE_FAIL:-}" ]]; then + echo "Failed to $1: org.bluez.Error.AuthenticationFailed" + exit "${MOCK_DEVICE_EXIT:-0}" +fi +if [[ $1 == "info" ]]; then + echo "Paired: ${MOCK_PAIRED:-yes}" + echo "Connected: ${MOCK_CONNECTED:-no}" +fi +if [[ $1 == "connect" ]]; then + touch "$POWERED_FILE.connected" +fi +if [[ $1 == "info" && -f "$POWERED_FILE.connected" ]]; then + echo "Connected: yes" +fi [[ $1 == "power" && $2 == "on" ]] && echo yes >"$POWERED_FILE" [[ $1 == "list" ]] && for c in ${MOCK_CONTROLLERS:-AA:BB:CC:DD:EE:FF}; do printf 'Controller %s mock\n' "$c"; done @@ -180,6 +194,12 @@ printf 'rfkill %s\n' "$*" >>"$BLUETOOTHCTL_LOG" exit 0 SH +cat >"$mock_bin/omarchy-notification-send" <<'SH' +#!/bin/bash +printf 'notification %s\n' "$*" >>"$BLUETOOTHCTL_LOG" +SH +chmod +x "$mock_bin/omarchy-notification-send" + chmod +x "$mock_bin/bluetoothctl" "$mock_bin/rfkill" # $ROOT/bin so omarchy-bluetooth-device resolves the real omarchy-bluetooth-power. @@ -189,6 +209,7 @@ bluetooth_run() { echo "$powered" >"$POWERED_FILE" : >"$device_tmp/log" + rm -f "$POWERED_FILE.connected" PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" \ OMARCHY_BLUETOOTH_POWER_WAIT_SECONDS=0 "$@" || fail "$* exits cleanly with Powered: $powered" @@ -280,3 +301,45 @@ pass "bluetooth counts a secondary controller as on" grep -q 'AutoEnable=false' "$ROOT/install/hardware/bluetooth.sh" && fail "bluetooth install leaves AutoEnable at its default" pass "bluetooth install leaves AutoEnable at its default" + +# Detached panel commands must expose errors even when bluetoothctl exits zero. +for status in 0 1 124; do + echo yes >"$POWERED_FILE" + : >"$device_tmp/log" + if PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" MOCK_DEVICE_FAIL=pair MOCK_DEVICE_EXIT="$status" \ + "$ROOT/bin/omarchy-bluetooth-device" pair AA:BB:CC:DD:EE:FF >"$device_tmp/error" 2>&1; then + fail "pairing failure exits nonzero (bluetoothctl status $status)" + fi + grep -q 'notification Bluetooth pairing\|notification .*Bluetooth pair failed' "$device_tmp/log" || fail "pair failure notifies" + if grep -Eq '^(trust|connect) ' "$device_tmp/log"; then + fail "pair failure must not trust or connect" + fi + pass "pair failure reports and stops (bluetoothctl status $status)" +done + +paired_log=$(MOCK_CONNECTED=yes bluetooth_run yes "$ROOT/bin/omarchy-bluetooth-device" pair AA:BB:CC:DD:EE:FF) +grep -qx 'trust AA:BB:CC:DD:EE:FF' "$paired_log" || fail "successful pair saves trust" +if grep -q '^connect ' "$paired_log"; then + fail "already connected pair skips redundant connect" +fi +pass "already connected pair saves trust without reconnecting" + +echo yes >"$POWERED_FILE" +: >"$device_tmp/log" +if PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" MOCK_PAIRED=no \ + "$ROOT/bin/omarchy-bluetooth-device" pair AA:BB:CC:DD:EE:FF >"$device_tmp/error" 2>&1; then + fail "pairing must verify Paired state" +fi +if grep -Eq '^(trust|connect) ' "$device_tmp/log"; then + fail "unconfirmed pairing must not trust or connect" +fi +pass "pairing verifies state before trusting" + +: >"$device_tmp/log" +rm -f "$POWERED_FILE.connected" +if PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" MOCK_DEVICE_FAIL=connect \ + "$ROOT/bin/omarchy-bluetooth-device" connect AA:BB:CC:DD:EE:FF >"$device_tmp/error" 2>&1; then + fail "connection failure exits nonzero even when bluetoothctl exits zero" +fi +grep -q 'Bluetooth connect failed' "$device_tmp/log" || fail "connection failure notifies" +pass "connection failure is reported"