Keep live BlueZ discovery client alive for real scans

The previous Bluetooth fix only proved parser behavior with a synthetic stream. Real BlueZ discovery needs one interactive client to stay alive while unpaired Device1 objects exist. Add a bluetoothctl wrapper and bananarchy-bt-scan.sh PTY session that powers on, sets auto/duplicate-data filters, queries devices during discovery, and stops discovery cleanly. Add util-linux-misc, host test, docs and rebuild guards. Deployed to the live phone; a physical peripheral in pairing mode is still needed for final confirmation.
This commit is contained in:
Luxferre committed 2026-11-10 16:04:14 +02:00
1 parent f892531df6
commit 77127c7844
10 files changed
+232 -4

No files matched your search

+4
View File
@@ -166,6 +166,8 @@ chown 0:0 "$TARGET_ROOTFS/etc/chrony/chrony.conf" \
"$TARGET_ROOTFS/usr/bin/bananarchy-time-sync.sh" \
"$TARGET_ROOTFS/usr/bin/bananarchy-sync-net-time.sh" \
"$TARGET_ROOTFS/usr/bin/bananarchy-network-event.sh" \
"$TARGET_ROOTFS/usr/local/bin/bluetoothctl" \
"$TARGET_ROOTFS/usr/bin/bananarchy-bt-scan.sh" \
"$TARGET_ROOTFS/etc/init.d/bananarchy-time-sync" \
"$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh" \
"$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/30-bananarchy-network-status.sh" \
@@ -187,6 +189,8 @@ chmod 0755 "$TARGET_ROOTFS/usr/bin/bananarchy-idle-blank.sh" \
"$TARGET_ROOTFS/usr/bin/bananarchy-time-sync.sh" \
"$TARGET_ROOTFS/usr/bin/bananarchy-sync-net-time.sh" \
"$TARGET_ROOTFS/usr/bin/bananarchy-network-event.sh" \
"$TARGET_ROOTFS/usr/local/bin/bluetoothctl" \
"$TARGET_ROOTFS/usr/bin/bananarchy-bt-scan.sh" \
"$TARGET_ROOTFS/etc/init.d/bananarchy-time-sync" \
"$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/20-bananarchy-time-sync.sh" \
"$TARGET_ROOTFS/etc/NetworkManager/dispatcher.d/30-bananarchy-network-status.sh" \
+39
View File
@@ -1209,6 +1209,45 @@ if command -v g++ >/dev/null 2>&1 && \
fail "host Bluetooth scan parser test failed"
fi
echo "[check] Live Bluetooth discovery wrapper..."
BT_WRAPPER="$BASE_DIR/overlay/rootfs/usr/local/bin/bluetoothctl"
BT_HELPER="$BASE_DIR/overlay/rootfs/usr/bin/bananarchy-bt-scan.sh"
for f in "$BT_WRAPPER" "$BT_HELPER"; do
if [ ! -x "$f" ]; then
fail "${f#$BASE_DIR/} is missing or not executable"
fi
if ! sh -n "$f" 2>/dev/null; then
fail "shell syntax: ${f#$BASE_DIR/}"
fi
done
for token in 'BANANARCHY_REAL_BLUETOOTHCTL' 'BANANARCHY_BT_SCAN_HELPER' \
'" scan on "' 'exec "$REAL" "$@"'; do
if ! grep -qF "$token" "$BT_WRAPPER"; then
fail "bluetoothctl wrapper is missing $token"
fi
done
for token in 'script -q -c' 'transport auto' 'duplicate-data on' \
'scan off' 'BANANARCHY_BT_SCAN_SECONDS'; do
if ! grep -qF "$token" "$BT_HELPER"; then
fail "live Bluetooth scan helper is missing $token"
fi
done
if ! grep -q '^util-linux-misc$' "$PKGS_FILE"; then
fail "rootfs package set is missing util-linux-misc (script)"
fi
if ! grep -qF 'usr/local/bin/bluetoothctl' \
"$BASE_DIR/scripts/apply-bananarchy-overlay.sh" || \
! grep -qF 'usr/bin/bananarchy-bt-scan.sh' \
"$BASE_DIR/scripts/apply-bananarchy-overlay.sh"; then
fail "apply-bananarchy-overlay.sh no longer installs the live Bluetooth scan wrapper"
fi
if [ ! -x "$BASE_DIR/scripts/test-bluetooth-live-scan.py" ]; then
fail "missing host unit test scripts/test-bluetooth-live-scan.py"
fi
if ! "$BASE_DIR/scripts/test-bluetooth-live-scan.py" >/dev/null 2>&1; then
fail "host live Bluetooth discovery test failed"
fi
echo "[check] Charger plug/unplug charge icon..."
BATTERY_RULES="$BASE_DIR/overlay/rootfs/etc/udev/rules.d/85-bananarchy-battery.rules"
CHARGE_EVENT="$BASE_DIR/overlay/rootfs/usr/bin/bananarchy-battery-charge-event.sh"
+1
View File
@@ -83,6 +83,7 @@ soc-qcom-msm8916-ucm
sudo
tcl
tor
util-linux-misc
v4l-utils
w3m
webkit2gtk-4.1
+108
View File
@@ -0,0 +1,108 @@
#!/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"
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")
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)")
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"):
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"):
if token not in overlay:
raise SystemExit("overlay application is not installing %s" % token)
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()