Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 115 additions & 40 deletions Runner/suites/Connectivity/Bluetooth/BT_SCAN_PAIR/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,68 +28,139 @@ fi

# shellcheck disable=SC1090,SC1091
. "$TOOLS/functestlib.sh"
# shellcheck disable=SC1090,SC1091
. "$TOOLS/lib_bluetooth.sh"

TESTNAME="BT_SCAN_PAIR"
test_path=$(find_test_case_by_name "$TESTNAME") || {
log_fail "$TESTNAME : Test directory not found."
echo "$TESTNAME FAIL" > "./$TESTNAME.res"
exit 1
exit 0
}
cd "$test_path" || exit 1

if ! cd "$test_path"; then
log_fail "$TESTNAME : Failed to cd into test directory: $test_path"
echo "$TESTNAME FAIL" > "./$TESTNAME.res"
exit 0
fi

RES_FILE="./$TESTNAME.res"
rm -f "$RES_FILE"

log_info "------------------------------------------------------------"
log_info "Starting $TESTNAME Testcase"

# Capture BT_MAC from environment (LAVA secret) *before* we touch BT_MAC locally
BT_ENV_MAC=""
if [ -n "${BT_MAC:-}" ]; then
BT_ENV_MAC="$BT_MAC"
fi

# Defaults
PAIR_RETRIES="${PAIR_RETRIES:-3}"
SCAN_ATTEMPTS="${SCAN_ATTEMPTS:-2}"

BT_NAME=""
BT_MAC=""
WHITELIST=""

# Parse CLI args
# -------------------------
# CLI parsing
# -------------------------
if [ -n "$1" ]; then
if echo "$1" | grep -Eq '^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$'; then
# CLI MAC has highest priority
BT_MAC="$1"
else
BT_NAME="$1"
fi
fi

if [ -n "$2" ]; then
WHITELIST="$2"
if [ -z "$BT_MAC" ] && echo "$2" | grep -Eq '^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$'; then
BT_MAC="$2"
fi
fi

# Skip if no CLI input and no list file
# If BT_MAC not set by CLI, fall back to BT_ENV_MAC (LAVA export)
if [ -z "$BT_MAC" ] && [ -n "$BT_ENV_MAC" ] && \
echo "$BT_ENV_MAC" | grep -Eq '^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$'; then
BT_MAC="$BT_ENV_MAC"
fi

# Optionally: if BT_MAC still empty and whitelist itself is a MAC, treat it as BT_MAC
if [ -z "$BT_MAC" ] && [ -n "$WHITELIST" ] && \
echo "$WHITELIST" | grep -Eq '^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$'; then
BT_MAC="$WHITELIST"
fi

if [ -n "$BT_MAC" ]; then
log_info "Effective BT_MAC resolved to: $BT_MAC"
fi

# Skip if no MAC/name and no list file
if [ -z "$BT_MAC" ] && [ -z "$BT_NAME" ] && [ ! -f "./bt_device_list.txt" ]; then
log_warn "No MAC/name or bt_device_list.txt found. Skipping test."
log_warn "No BT_MAC/BT_NAME and no bt_device_list.txt found. Skipping test."
echo "$TESTNAME SKIP" > "$RES_FILE"
exit 0
fi

check_dependencies bluetoothctl rfkill expect hciconfig || {
log_warn "Missing required tools; skipping $TESTNAME."
echo "$TESTNAME SKIP" > "$RES_FILE"
exit 0
}

cleanup_bt_test() {
# Cleanup only the primary MAC we worked with (if any)
[ -n "$BT_MAC" ] && bt_cleanup_paired_device "$BT_MAC"
killall -q bluetoothctl 2>/dev/null
killall -q bluetoothctl 2>/dev/null || true
}
trap cleanup_bt_test EXIT

rfkill unblock bluetooth
retry_command_bt "hciconfig hci0 up" "Bring up hci0" || {
log_fail "Failed to bring up hci0"
# RF-kill unblock (best effort)
rfkill unblock bluetooth 2>/dev/null || true

# Detect adapter (no hardcoded hci0)
BT_ADAPTER="${BT_ADAPTER:-}"
if [ -z "$BT_ADAPTER" ]; then
BT_ADAPTER="$(listhcis | head -n1)"
fi

if [ -z "$BT_ADAPTER" ]; then
log_fail "No Bluetooth HCI adapter found under /sys/class/bluetooth; cannot run $TESTNAME."
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 1
}
exit 0
fi

log_info "Detected Bluetooth adapter: $BT_ADAPTER"

# Ensure controller is visible to bluetoothctl (public-addr bootstrap)
if bt_ensure_controller_visible "$BT_ADAPTER"; then
log_info "Bluetooth controller is visible to bluetoothctl after bt_ensure_controller_visible."
else
log_fail "Bluetooth controller is not visible to bluetoothctl after bt_ensure_controller_visible."
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 0
fi

# Power on adapter via existing helper; do this regardless of HCICONFIG state.
if ! btpower "$BT_ADAPTER" on; then
log_fail "Failed to power on adapter $BT_ADAPTER via btpower (RF-kill/firmware issue?)."
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 0
fi

# Optional debug: show BD address and confirm firmware/driver presence
bdaddr="$(btgetbdaddr "$BT_ADAPTER" 2>/dev/null || true)"
[ -n "$bdaddr" ] && log_info "Adapter $BT_ADAPTER BD_ADDR=$bdaddr"

if ! btkmdpresent; then
log_warn "Bluetooth kernel modules / driver not clearly present (btkmdpresent failed)."
fi

if ! btfwpresent >/dev/null 2>&1; then
log_warn "No obvious Bluetooth firmware files found (btfwpresent); continuing anyway."
fi

# Remove any previously paired devices to start clean
bt_remove_all_paired_devices

# Helper: l2ping link verification
Expand All @@ -104,37 +175,40 @@ verify_link() {
fi
}

# Direct pairing if CLI MAC provided
# -------------------------
# Direct pairing path (BT_MAC known)
# -------------------------
if [ -n "$BT_MAC" ]; then
log_info "Direct pairing requested for $BT_MAC"
log_info "Direct pairing requested for BT_MAC=$BT_MAC (BT_NAME='$BT_NAME')"
sleep 2
for attempt in $(seq 1 "$PAIR_RETRIES"); do
log_info "Pair attempt $attempt/$PAIR_RETRIES for $BT_MAC"
bt_cleanup_paired_device "$BT_MAC"

if bt_pair_with_mac "$BT_MAC"; then
log_info "Pair succeeded; connecting to $BT_MAC"
log_info "Pair succeeded; attempting post-pair connect to $BT_MAC"
if bt_post_pair_connect "$BT_MAC"; then
log_pass "Post-pair connect succeeded for $BT_MAC"
verify_link "$BT_MAC"
else
log_warn "Connect failed; trying l2ping fallback for $BT_MAC"
log_warn "Post-pair connect failed; trying l2ping fallback for $BT_MAC"
verify_link "$BT_MAC"
bt_cleanup_paired_device "$BT_MAC"
fi
else
log_warn "Pair failed for $BT_MAC (attempt $attempt)"
bt_cleanup_paired_device "$BT_MAC"
fi
done

log_warn "Exhausted direct pairing attempts for $BT_MAC"
# If CLI arg was provided, do not fallback on empty .txt
if [ -n "$BT_NAME" ] || [ -n "$BT_MAC" ]; then
log_fail "Direct pairing failed for ${BT_MAC:-$BT_NAME}"
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 1
fi
log_fail "Direct pairing failed for ${BT_MAC:-$BT_NAME}"
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 0
fi

# Fallback: only if no CLI input and list exists
# -------------------------
# Fallback list-based flow
# -------------------------
if [ -z "$BT_MAC" ] && [ -z "$BT_NAME" ] && [ -f "./bt_device_list.txt" ]; then
# Skip if list is empty or only comments
if ! grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' bt_device_list.txt | grep -q .; then
Expand All @@ -143,36 +217,37 @@ if [ -z "$BT_MAC" ] && [ -z "$BT_NAME" ] && [ -f "./bt_device_list.txt" ]; then
exit 0
fi

log_info "Using fallback list in bt_device_list.txt"
log_info "Using fallback device list in bt_device_list.txt"
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in ''|\#*) continue ;; esac
# split into MAC and NAME

# split into MAC and NAME (simple space-separated)
IFS=' ' read -r MAC NAME <<EOF
$line
EOF
[ -z "$MAC" ] && continue

# Whitelist filter
# Whitelist filter (name-based simple match)
if [ -n "$WHITELIST" ] && ! printf '%s' "$NAME" | grep -iq "$WHITELIST"; then
log_info "Skipping $MAC ($NAME): not in whitelist '$WHITELIST'"
continue
fi

BT_MAC=$MAC
BT_NAME=$NAME
BT_MAC="$MAC"
BT_NAME="$NAME"

log_info "===== Attempting $BT_MAC ($BT_NAME) ====="
log_info "===== Attempting $BT_MAC ($BT_NAME) from device list ====="
bt_cleanup_paired_device "$BT_MAC"

for attempt in $(seq 1 "$PAIR_RETRIES"); do
log_info "Pair attempt $attempt/$PAIR_RETRIES for $BT_MAC"
if bt_pair_with_mac "$BT_MAC"; then
log_info "Pair succeeded; connecting to $BT_MAC"
log_info "Pair succeeded; attempting post-pair connect to $BT_MAC"
if bt_post_pair_connect "$BT_MAC"; then
log_pass "Post-pair connect succeeded for $BT_MAC"
verify_link "$BT_MAC"
else
log_warn "Connect failed; trying l2ping fallback for $BT_MAC"
log_warn "Post-pair connect failed; trying l2ping fallback for $BT_MAC"
verify_link "$BT_MAC"
fi
else
Expand All @@ -181,15 +256,15 @@ EOF
bt_cleanup_paired_device "$BT_MAC"
done

log_warn "Exhausted $PAIR_RETRIES attempts for $BT_MAC; moving to next"
log_warn "Exhausted $PAIR_RETRIES attempts for $BT_MAC; moving to next entry"
done < "./bt_device_list.txt"

log_fail "All fallback devices failed"
log_fail "All fallback devices from bt_device_list.txt failed"
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 1
exit 0
fi

# Should never reach here
log_fail "No execution path matched; exiting"
log_fail "No execution path matched; exiting with FAIL."
echo "$TESTNAME FAIL" > "$RES_FILE"
exit 1
exit 0
Loading
Loading