Files
bananarchy/scripts/test-bluetooth-live-scan.py
T
Luxferre 1e7077e314 Fix bluetoothd CPU busy-loops, cache BT state, and refresh a1120 images
- Add /etc/conf.d/bluetooth (supervise-daemon, retry=TERM/3/KILL/1, --noplugin=hfp,bap,bass,mcp,vcp,micp,csip,ccp,tmap,gmap,asha,midi,neard) and drop gateway UUIDs (111f/1112/110a) from main.conf ReconnectUUIDs so Experimental = true keeps PreferredBearer without BlueZ hfp-hf/ATT EOF GMainLoop spins.

- Set duplicate-data off in bananarchy-bt-scan.sh and update sxmo_bluetoothtoggle.sh to disconnect, btmgmt power off, and clean state files on disable so adapter_stop() never busy-loops on dual-bearer links.

- Add single-shot BlueZ ObjectManager state caching (bt_state.tsv, dump-state, CTRL_READY_FILE, EPHEMERAL_DEV_PROPS filter) in bananarchy-bt-obex and consume it in networks.so instead of per-device bluetoothctl refresh storms.

- Extend Bluetooth host tests and check-rebuild.sh, update docs, and rebuild/pin a1120 (cocojamboo) images and release archives for nokia-argon, nokia-beatles, nokia-coconut, and nokia-sparkler.
2026-11-18 06:34:13 +02:00

124 lines
4.9 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 off", "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)
if "duplicate-data on" in helper:
raise SystemExit("live scan helper must keep duplicate-data off to avoid bluetoothd flood")
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()