Files
bananarchy/scripts/test-bluetooth-live-scan.py
T
Luxferre e4358665f7 Enable WCNSS LE before Bluetooth discovery
Live btmgmt/btmon showed hci0 powered with le absent from current settings; BlueZ issued only BR/EDR Inquiry, so BLE peripherals were invisible. Add a narrow doas helper to run btmgmt -i hci0 le on before each interactive scan, make bluez-btmgmt explicit, add the doas rule, host guards and docs. With real peripherals in pairing mode the plugin UI reported 5 devices and retained BT KeyBoard / LE-QC Ultra entries.
2026-11-10 16:15:50 +02:00

122 lines
4.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""Host test for the live BlueZ discovery wrapper.
The parser test only proves that captured device lines can be merged. This
test drives the interactive bluetoothctl wrapper with a stub real binary and
checks that discovery-client commands, periodic device queries and clean
shutdown all happen through one PTY session.
"""
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
BASE = pathlib.Path(__file__).resolve().parent.parent
WRAPPER = BASE / "overlay/rootfs/usr/local/bin/bluetoothctl"
HELPER = BASE / "overlay/rootfs/usr/bin/bananarchy-bt-scan.sh"
OVERLAY = BASE / "scripts/apply-bananarchy-overlay.sh"
PACKAGES = BASE / "scripts/rootfs-packages.txt"
LE_HELPER = BASE / "overlay/rootfs/usr/bin/bananarchy-bt-le-enable.sh"
DOAS_RULE = BASE / "overlay/rootfs/etc/doas.d/60-bananarchy-bluetooth.conf"
STUB = r'''#!/usr/bin/env python3
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "show":
print("\tPowered: yes")
sys.exit(0)
for line in sys.stdin:
command = line.strip()
if command == "scan on":
print("[NEW] Device AA:BB:CC:DD:EE:FF STUB_STREAM", flush=True)
elif command == "devices":
print("Device AA:BB:CC:DD:EE:FF STUB_LIST", flush=True)
elif command == "scan off":
print("Discovery stopped", flush=True)
elif command == "quit":
break
'''
def read(path):
return path.read_text(encoding="utf-8")
def main():
for tool in ("script", "timeout"):
if not shutil.which(tool):
raise SystemExit("missing required host tool: %s" % tool)
if not WRAPPER.is_file() or not os.access(WRAPPER, os.X_OK):
raise SystemExit("missing executable overlay/usr/local/bin/bluetoothctl")
if not HELPER.is_file() or not os.access(HELPER, os.X_OK):
raise SystemExit("missing executable overlay/usr/bin/bananarchy-bt-scan.sh")
if not LE_HELPER.is_file() or not os.access(LE_HELPER, os.X_OK):
raise SystemExit("missing executable overlay/usr/bin/bananarchy-bt-le-enable.sh")
wrapper = read(WRAPPER)
helper = read(HELPER)
overlay = read(OVERLAY)
packages = {
line.strip()
for line in read(PACKAGES).splitlines()
if line.strip() and not line.lstrip().startswith("#")
}
if "util-linux-misc" not in packages:
raise SystemExit("rootfs package set is missing util-linux-misc (script)")
if "bluez-btmgmt" not in packages:
raise SystemExit("rootfs package set is missing bluez-btmgmt (LE enable)")
for token in ("BANANARCHY_REAL_BLUETOOTHCTL", "BANANARCHY_BT_SCAN_HELPER",
'" scan on "', 'exec "$REAL" "$@"'):
if token not in wrapper:
raise SystemExit("bluetoothctl wrapper is missing %s" % token)
for token in ("script -q -c", "menu scan", "transport auto",
"duplicate-data on", "scan on", "devices", "scan off",
"quit", "BANANARCHY_BT_SCAN_SECONDS",
"bananarchy-bt-le-enable.sh"):
if token not in helper:
raise SystemExit("live scan helper is missing %s" % token)
for token in ("usr/local/bin/bluetoothctl", "usr/bin/bananarchy-bt-scan.sh",
"usr/bin/bananarchy-bt-le-enable.sh",
"etc/doas.d/60-bananarchy-bluetooth.conf"):
if token not in overlay:
raise SystemExit("overlay application is not installing %s" % token)
if not DOAS_RULE.is_file() or "bananarchy-bt-le-enable.sh" not in read(DOAS_RULE):
raise SystemExit("doas rule is missing the LE enable helper")
if "btmgmt -i hci0 le on" not in read(LE_HELPER):
raise SystemExit("LE enable helper lost its btmgmt command")
print("[ok] live Bluetooth discovery wiring")
with tempfile.TemporaryDirectory(prefix="bananarchy-bt-scan-") as tmp:
root = pathlib.Path(tmp)
stub = root / "bluetoothctl-stub"
stub.write_text(STUB, encoding="utf-8")
stub.chmod(0o755)
env = os.environ.copy()
env["BANANARCHY_REAL_BLUETOOTHCTL"] = str(stub)
env["BANANARCHY_BT_SCAN_HELPER"] = str(HELPER)
env["BANANARCHY_BT_SCAN_SECONDS"] = "4"
scan = subprocess.run(
[str(WRAPPER), "--timeout", "15", "scan", "on"],
env=env, capture_output=True, text=True, timeout=15)
if scan.returncode != 0:
raise SystemExit("wrapper scan failed:\n%s" % scan.stderr.strip())
for token in ("STUB_STREAM", "STUB_LIST", "Discovery stopped"):
if token not in scan.stdout:
raise SystemExit("wrapper output is missing %s" % token)
forward = subprocess.run(
[str(WRAPPER), "show"], env=env,
capture_output=True, text=True, timeout=5)
if forward.returncode != 0 or "Powered: yes" not in forward.stdout:
raise SystemExit("wrapper did not forward show to the real binary")
if "STUB_" in forward.stdout:
raise SystemExit("wrapper incorrectly intercepted a non-scan command")
print("[ok] interactive Bluetooth scan session")
if __name__ == "__main__":
main()