#!/usr/bin/env python3 """Host regression tests for Bluetooth 5.x/6.x headset support (A2DP + HSP/HFP). Covers: - WirePlumber 50-bluetooth.conf consistency across all overlay/user templates (cvsd-only HFP without mSBC/lc3-a127/lc3_swb/bap/hfp_ag, dummy-avrcp-player, LDAC auto ABR and AAC CBR defaults). - Dual-mode BR/EDR vs BLE bearer selection in bananarchy-bt-obex without regressing LE HOGP keyboards/mice/gamepads, plus AVRCP media key dispatch. - SCO keepalive race handling, zero pkill dependency, hardware volume routing, headset ATA call pickup synchronization, and n-sparkler SCO haptic guard. - Off-by-default discoverability with bounded 60s timeout across toggle, daemon, and xnetworks UI. - All four target DTBs (argon, beatles, coconut, sparkler) exposing WCNSS-BT and QDSP6 INTERNAL_BT_SCO DAIs. """ import ast import pathlib import shutil import subprocess import sys BASE = pathlib.Path(__file__).resolve().parent.parent WP_CONFS = ( BASE / "overlay/rootfs/etc/wireplumber/wireplumber.conf.d/50-bluetooth.conf", BASE / "overlay/rootfs/usr/share/wireplumber/wireplumber.conf.d/50-bluetooth.conf", BASE / "overlay/rootfs/home/user/.config/wireplumber/wireplumber.conf.d/50-bluetooth.conf", BASE / "overlay/rootfs/etc/skel/.config/wireplumber/wireplumber.conf.d/50-bluetooth.conf", ) BT_OBEX = BASE / "overlay/rootfs/usr/bin/bananarchy-bt-obex" BT_TOGGLE = BASE / "overlay/rootfs/usr/bin/sxmo_bluetoothtoggle.sh" AUDIO_SH = BASE / "overlay/rootfs/usr/bin/sxmo_audio.sh" MODEMAUDIO_SH = BASE / "overlay/rootfs/usr/bin/sxmo_modemaudio.sh" CALLSCREEN_CPP = BASE / "src/dwm-plugins/callscreen_plugin.cpp" NETWORKS_CPP = BASE / "src/dwm-plugins/networks_plugin.cpp" MUSIC_CPP = BASE / "src/dwm-plugins/music_plugin.cpp" VIBRATE_C = BASE / "src/dwm-plugins/sxmo_vibrate.c" TARGET_DTBS = ( BASE / "prebuilt/kernel-6.12.1-msm8916/dtbs/qcom-msm8905-nokia-argon.dtb", BASE / "prebuilt/kernel-6.12.1-msm8916/dtbs/qcom-msm8905-nokia-beatles.dtb", BASE / "prebuilt/kernel-6.12.1-msm8916/dtbs/qcom-msm8905-nokia-coconut.dtb", BASE / "prebuilt/kernel-6.12.1-msm8916/dtbs/qcom-msm8909-nokia-sparkler.dtb", ) def require(cond, msg): if not cond: raise SystemExit("FAIL: " + msg) def read(path): return path.read_text(encoding="utf-8") def load_obex_pure_helpers(): src = read(BT_OBEX) tree = ast.parse(src, filename=str(BT_OBEX)) wanted_assigns = {"CLASSIC_AUDIO_UUID_PREFIXES"} wanted_defs = { "is_phone_or_computer_props", "is_hog_device_props", "is_classic_audio_props", "avrcp_fifo_command_for_key", } selected = [] for node in tree.body: if isinstance(node, ast.Assign): names = [t.id for t in node.targets if isinstance(t, ast.Name)] if any(n in wanted_assigns for n in names): selected.append(ast.get_source_segment(src, node)) elif isinstance(node, ast.FunctionDef) and node.name in wanted_defs: selected.append(ast.get_source_segment(src, node)) require(len(selected) == len(wanted_assigns) + len(wanted_defs), "missing pure audio/AVRCP helpers in bananarchy-bt-obex") ns = {} exec("\n\n".join(selected), ns) return ns def test_wireplumber_config(): texts = [read(p) for p in WP_CONFS] for idx, t in enumerate(texts[1:], 1): require(t == texts[0], f"50-bluetooth.conf copy {WP_CONFS[idx]} drifted from etc/wireplumber copy") cfg = texts[0] for forbidden in ("bap_sink", "bap_source", "a2dp_source", "hsp_ag", "hfp_ag", "opus_05_dup ", "opus_05_51", "opus_05_71", "opus_05_pro", " lc3 "): require(forbidden not in cfg, f"50-bluetooth.conf still contains incompatible token {forbidden!r}") for required in ( "bluez5.roles = [ a2dp_sink hsp_hs hfp_hf ]", "opus_05_duplex", "bluez5.enable-msbc = false", "bluez5.enable-lc3-a127 = false", "bluez5.enable-hw-volume = true", "bluez5.dummy-avrcp-player = true", 'bluez5.a2dp.ldac.quality = "auto"', "bluez5.a2dp.aac.bitratemode = 0", 'bluez5.hfphsp-backend = "native"', ): require(required in cfg, f"50-bluetooth.conf missing {required!r}") print("[ok] WirePlumber 50-bluetooth.conf profiles, codecs, AVRCP, and CVSD lock") def test_obex_bearer_and_avrcp(): ns = load_obex_pure_helpers() is_phone = ns["is_phone_or_computer_props"] is_hog = ns["is_hog_device_props"] is_audio = ns["is_classic_audio_props"] map_avrcp = ns["avrcp_fifo_command_for_key"] hog_kbd = {"UUIDs": ["00001812-0000-1000-8000-00805f9b34fb"]} bt5_dual_buds = { "AddressType": "public", "UUIDs": [ "0000110b-0000-1000-8000-00805f9b34fb", "0000111e-0000-1000-8000-00805f9b34fb", "0000184e-0000-1000-8000-00805f9b34fb", ], "Icon": "audio-headset", "Class": 0x240404, } android_phone = { "AddressType": "public", "Icon": "phone", "Class": 0x5a020c, "UUIDs": [ "00001105-0000-1000-8000-00805f9b34fb", "0000110a-0000-1000-8000-00805f9b34fb", "0000111f-0000-1000-8000-00805f9b34fb", ], } ble_beacon = {"AddressType": "random", "UUIDs": ["00001800-0000-1000-8000-00805f9b34fb"]} cod_only_speaker = {"Class": 0x200414} require(is_hog(hog_kbd) and not is_audio(hog_kbd), "HOGP keyboard misclassified as Classic Audio") require(is_phone(android_phone) and not is_audio(android_phone), "Android phone with Audio Source/HFP-AG UUIDs misclassified as Classic Audio") require(is_audio(bt5_dual_buds) and not is_hog(bt5_dual_buds), "Dual-mode BT 5.x/6.x headset not recognized for BR/EDR bearer selection") require(is_audio({"Icon": "audio-headphones"}), "audio-headphones Icon not recognized as Classic Audio") require(is_audio(cod_only_speaker), "Audio CoD speaker not recognized as Classic Audio") require(not is_audio(ble_beacon), "Generic BLE beacon falsely matched Classic Audio") require(map_avrcp(164) == "PLAYPAUSE", "KEY_PLAYPAUSE mapping wrong") require(map_avrcp(200) == "PLAY", "KEY_PLAYCD mapping wrong") require(map_avrcp(201) == "PAUSE", "KEY_PAUSECD mapping wrong") require(map_avrcp(166) == "STOP", "KEY_STOPCD mapping wrong") require(map_avrcp(163) == "NEXT", "KEY_NEXTSONG mapping wrong") require(map_avrcp(165) == "PREV", "KEY_PREVIOUSSONG mapping wrong") require(map_avrcp(30) is None, "unrelated key mapped as AVRCP") obex_src = read(BT_OBEX) for tok in ( "set_hog_preferred_bearer(", "set_audio_preferred_bearer(", '("s", "bredr")', "ConnectProfile", "has_media_transport(", "is_tentative_ble_non_hog(", "avrcp_devs", "dispatch_avrcp_key(", ): require(tok in obex_src, f"bananarchy-bt-obex missing {tok}") print("[ok] Dual-bearer BR/EDR headset selection, HOGP preservation, and AVRCP bridge") def test_discoverability_and_power(): obex_src = read(BT_OBEX) toggle_src = read(BT_TOGGLE) net_src = read(NETWORKS_CPP) main_src = read(BASE / "overlay/rootfs/etc/bluetooth/main.conf") require('DiscoverableTimeout = 60' in main_src and 'PairableTimeout = 0' in main_src, "main.conf must enforce DiscoverableTimeout = 60 and PairableTimeout = 0") require('discoverable-timeout 0' not in toggle_src, "sxmo_bluetoothtoggle.sh still sets infinite discoverable-timeout 0") require('discoverable-timeout 60' in toggle_src and 'bt_disc="0"' in toggle_src, "sxmo_bluetoothtoggle.sh must default bt_disc=0 and timeout=60") require('"discoverable-timeout", "0"' not in obex_src and '("org.bluez.Adapter1", "PairableTimeout", ("u", 0))' in obex_src, "bananarchy-bt-obex must bound discoverable-timeout to 60 and keep PairableTimeout=0") require('"discoverable-timeout", "60"' in obex_src and 'bt_disc = "0"' in obex_src, "bananarchy-bt-obex must default bt_disc='0' and timeout=60") require("discoverable-timeout 0" not in net_src and "discoverable-timeout 60" in net_src, "networks_plugin.cpp must use 60s discoverable timeout") confd_bt = BASE / "overlay/rootfs/etc/conf.d/bluetooth" require(confd_bt.is_file(), "missing overlay/rootfs/etc/conf.d/bluetooth") confd_src = read(confd_bt) for tok in ( "supervisor=supervise-daemon", "--noplugin=hfp,bap,bass,mcp,vcp,micp,csip,ccp,tmap,gmap,asha,midi,neard", 'retry="TERM/3/KILL/1"', ): require(tok in confd_src, f"etc/conf.d/bluetooth missing {tok}") rec_lines = [ln for ln in main_src.splitlines() if ln.startswith("ReconnectUUIDs=")] require(len(rec_lines) == 1, "main.conf must define ReconnectUUIDs") rec_line = rec_lines[0] for forbidden in ("0000111f-", "00001112-", "0000110a-"): require(forbidden not in rec_line, f"main.conf ReconnectUUIDs must exclude gateway UUID {forbidden}") for keep in ("0000110b-", "00001108-", "0000111e-", "00001124-", "00001812-"): require(keep in rec_line, f"main.conf ReconnectUUIDs missing {keep}") for tok in ( "bluetoothctl disconnect", "btmgmt -i hci0 power off", "kill -KILL", "/run/bananarchy/bt-obex/ctrl_ready", "/run/bananarchy/bt-obex/bt_state.tsv", ): require(tok in toggle_src, f"sxmo_bluetoothtoggle.sh missing clean stop step {tok}") print("[ok] Bounded 60s discoverability, BlueZ --noplugin guard, and clean power-off") def test_sco_audio_call_and_haptics(): audio_src = read(AUDIO_SH) maudio_src = read(MODEMAUDIO_SH) cs_src = read(CALLSCREEN_CPP) net_src = read(NETWORKS_CPP) music_src = read(MUSIC_CPP) vib_src = read(VIBRATE_C) require("pkill" not in audio_src, "sxmo_audio.sh must not invoke missing pkill binary") require("launch_bt_sco_pacat" in audio_src and "/tmp/sxmo_bt_sco_wait.pid" in audio_src and "apply_bt_sink_volume" in audio_src, "sxmo_audio.sh missing bounded SCO keepalive retry or $bt_sink volume control") require("elif [ ! -r /tmp/sxmo_in_call ]; then\n stop_bt_sco_keepalive" not in audio_src, "sxmo_audio.sh leaks SCO pacat keepalive when switching off BT mid-call") require("bluez_output\\." in maudio_src and "switch_bt_card_hfp" in maudio_src, "sxmo_modemaudio.sh missing synchronous BT SCO sink readiness wait") require('.pickedupcall' in cs_src and 'writeIntFile("/tmp/sxmo_in_call", 1)' in cs_src, "callscreen_plugin.cpp missing headset ATA .pickedupcall / sxmo_in_call sync") require("refreshTargetBtFromList" in net_src and "infoCount >= 24" in net_src, "networks_plugin.cpp missing stale targetBt refresh or info cap bump") for tok in ("XF86XK_AudioPlay", "XF86XK_AudioNext", "XF86XK_AudioPrev", 'cmd == "PLAYPAUSE"', 'cmd == "NEXT"', 'cmd == "PREV"'): require(tok in music_src, f"music_plugin.cpp missing AVRCP handler {tok}") require("/tmp/sxmo_bt_sco_mm_active" in vib_src, "sxmo_vibrate.c missing /tmp/sxmo_bt_sco_mm_active haptic suppression") print("[ok] SCO keepalive, ATA call sync, AVRCP music control, and Sparkler SCO haptic guard") def test_target_dtbs(): dtc = shutil.which("dtc") if not dtc: print("[skip] dtc not installed; skipping DTB inspection") return for dtb in TARGET_DTBS: require(dtb.is_file(), f"missing shipped DTB: {dtb}") dts = subprocess.check_output( [dtc, "-q", "-I", "dtb", "-O", "dts", str(dtb)], text=True, stderr=subprocess.DEVNULL, ) for node in ('compatible = "qcom,wcnss-bt"', "dai@136", "dai@137", 'link-name = "Internal BT-SCO RX"', 'link-name = "Internal BT-SCO TX"'): require(node in dts, f"{dtb.name} missing BT/QDSP6 node {node!r}") print("[ok] All 4 target DTBs include WCNSS-BT and INTERNAL_BT_SCO DAIs") def main(): test_wireplumber_config() test_obex_bearer_and_avrcp() test_discoverability_and_power() test_sco_audio_call_and_haptics() test_target_dtbs() print("bluetooth headset verification: all checks passed") if __name__ == "__main__": main()