From 9625607a2dc74871d67248dd0fa256a5711cc86f Mon Sep 17 00:00:00 2001 From: Srikanth Muppandam Date: Mon, 27 Apr 2026 22:43:53 +0530 Subject: [PATCH 1/2] fix: add usable Bluetooth adapter selection helpers Add Bluetooth helper functions to identify usable HCI adapters based on UP/RUNNING state and a valid non-zero BD address. The new helpers log all available HCI candidates and prefer controllers that are already usable, avoiding DOWN or uninitialized adapters such as UART hci0 with 00:00:00:00:00:00 when another valid controller is available. Signed-off-by: Srikanth Muppandam --- Runner/utils/lib_bluetooth.sh | 134 ++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/Runner/utils/lib_bluetooth.sh b/Runner/utils/lib_bluetooth.sh index 8fce0f0c..4bee53ef 100755 --- a/Runner/utils/lib_bluetooth.sh +++ b/Runner/utils/lib_bluetooth.sh @@ -2235,3 +2235,137 @@ bt_scan_poll_off() { log_warn "Discovering did not settle to 'no' within scan OFF polling window." return 1 } + +# Return BD address for a given HCI adapter using hciconfig. +# Prints empty output if the adapter is missing or address cannot be parsed. +bt_hci_bdaddr() { + adapter="$1" + + [ -n "$adapter" ] || return 1 + + if ! command -v hciconfig >/dev/null 2>&1; then + return 1 + fi + + hciconfig "$adapter" 2>/dev/null | awk ' + /BD Address:/ { + for (i = 1; i <= NF; i++) { + if ($i == "Address:") { + print $(i + 1) + exit + } + } + } + ' +} + +# Check whether an HCI adapter is usable for BT_ON_OFF. +# Requires UP RUNNING state and a non-zero BD address. +bt_adapter_is_usable() { + adapter="$1" + out="" + addr="" + + [ -n "$adapter" ] || return 1 + + if ! command -v hciconfig >/dev/null 2>&1; then + return 1 + fi + + out="$(hciconfig "$adapter" 2>/dev/null || true)" + [ -n "$out" ] || return 1 + + addr="$(bt_hci_bdaddr "$adapter" 2>/dev/null || true)" + case "$addr" in + ""|"00:00:00:00:00:00") + return 1 + ;; + esac + + if printf '%s\n' "$out" | grep -q 'UP RUNNING'; then + return 0 + fi + + return 1 +} + +# Select the best available Bluetooth adapter. +# Prefer UP/RUNNING + non-zero BD address, then fallback to non-zero BD only. +bt_select_usable_adapter() { + adapters="" + adapter="" + addr="" + + if command -v hciconfig >/dev/null 2>&1; then + adapters="$(hciconfig 2>/dev/null | awk -F: '/^hci[0-9]+:/ { print $1 }')" + + for adapter in $adapters; do + if bt_adapter_is_usable "$adapter"; then + printf '%s\n' "$adapter" + return 0 + fi + done + + for adapter in $adapters; do + addr="$(bt_hci_bdaddr "$adapter" 2>/dev/null || true)" + case "$addr" in + ""|"00:00:00:00:00:00") + continue + ;; + *) + printf '%s\n' "$adapter" + return 0 + ;; + esac + done + fi + + if command -v findhcisysfs >/dev/null 2>&1; then + findhcisysfs 2>/dev/null || true + return 0 + fi + + return 1 +} + +# Log all HCI candidates with address/state details for CI/LAVA debug. +# This helps explain why one adapter was selected or ignored. +bt_log_hci_candidates() { + adapters="" + adapter="" + out="" + addr="" + state="" + usable="no" + + if ! command -v hciconfig >/dev/null 2>&1; then + log_warn "hciconfig not available; cannot log HCI adapter candidates." + return 0 + fi + + adapters="$(hciconfig 2>/dev/null | awk -F: '/^hci[0-9]+:/ { print $1 }')" + if [ -z "$adapters" ]; then + log_warn "No HCI adapters reported by hciconfig." + return 0 + fi + + for adapter in $adapters; do + out="$(hciconfig "$adapter" 2>/dev/null || true)" + addr="$(bt_hci_bdaddr "$adapter" 2>/dev/null || true)" + state="$(printf '%s\n' "$out" | awk '/UP|DOWN|RUNNING/ { + gsub(/^[ \t]+|[ \t]+$/, "") + print + exit + }')" + + [ -n "$addr" ] || addr="unknown" + [ -n "$state" ] || state="unknown" + + usable="no" + if bt_adapter_is_usable "$adapter"; then + usable="yes" + fi + + log_info "[bt-adapter] $adapter addr=$addr state='$state' usable=$usable" + done +} From 601d7a95e214258258f8d6dcb3e0e95ecd755c03 Mon Sep 17 00:00:00 2001 From: Srikanth Muppandam Date: Mon, 27 Apr 2026 22:44:06 +0530 Subject: [PATCH 2/2] fix: prefer usable HCI adapter in BT_ON_OFF Update BT_ON_OFF adapter selection to prefer an UP/RUNNING controller with a valid BD address when BT_ADAPTER is not explicitly provided. This fixes platforms with multiple HCI devices where hci0 may be DOWN or uninitialized while another adapter, such as USB hci1, is valid and usable. The test now logs adapter candidates for CI debug and avoids incorrectly skipping when a usable controller exists. Signed-off-by: Srikanth Muppandam --- .../Connectivity/Bluetooth/BT_ON_OFF/run.sh | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Runner/suites/Connectivity/Bluetooth/BT_ON_OFF/run.sh b/Runner/suites/Connectivity/Bluetooth/BT_ON_OFF/run.sh index daac39aa..0e23bc64 100755 --- a/Runner/suites/Connectivity/Bluetooth/BT_ON_OFF/run.sh +++ b/Runner/suites/Connectivity/Bluetooth/BT_ON_OFF/run.sh @@ -97,20 +97,24 @@ fi if [ -n "$BT_ADAPTER" ]; then ADAPTER="$BT_ADAPTER" log_info "Using adapter from BT_ADAPTER/CLI: $ADAPTER" -elif findhcisysfs >/dev/null 2>&1; then - ADAPTER="$(findhcisysfs 2>/dev/null || true)" -else - ADAPTER="" -fi - -if [ -n "$ADAPTER" ]; then - log_info "Using adapter: $ADAPTER" + + if command -v bt_adapter_is_usable >/dev/null 2>&1; then + if ! bt_adapter_is_usable "$ADAPTER"; then + log_warn "Requested adapter '$ADAPTER' is not currently UP/RUNNING with a valid BD address." + bt_log_hci_candidates || true + fi + fi else - log_warn "No HCI adapter found; skipping test." - echo "$TESTNAME SKIP" > "./$TESTNAME.res" - exit 0 + bt_log_hci_candidates || true + + if command -v bt_select_usable_adapter >/dev/null 2>&1; then + ADAPTER="$(bt_select_usable_adapter 2>/dev/null || true)" + elif findhcisysfs >/dev/null 2>&1; then + ADAPTER="$(findhcisysfs 2>/dev/null || true)" + else + ADAPTER="" + fi fi - # --- NEW: warn/diag if non-interactive "bluetoothctl list" is empty (non-fatal) --- btwarniflistempty "$ADAPTER" || true