|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause-Clear |
| 5 | + |
| 6 | +# Robustly find and source init_env |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 8 | +INIT_ENV="" |
| 9 | +SEARCH="$SCRIPT_DIR" |
| 10 | +while [ "$SEARCH" != "/" ]; do |
| 11 | + if [ -f "$SEARCH/init_env" ]; then |
| 12 | + INIT_ENV="$SEARCH/init_env" |
| 13 | + break |
| 14 | + fi |
| 15 | + SEARCH=$(dirname "$SEARCH") |
| 16 | +done |
| 17 | + |
| 18 | +if [ -z "$INIT_ENV" ]; then |
| 19 | + echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +if [ -z "$__INIT_ENV_LOADED" ]; then |
| 24 | + # shellcheck disable=SC1090 |
| 25 | + . "$INIT_ENV" |
| 26 | +fi |
| 27 | +# shellcheck disable=SC1090,SC1091 |
| 28 | +. "$TOOLS/functestlib.sh" |
| 29 | + |
| 30 | +TESTNAME="BT_SCAN_PAIR" |
| 31 | +test_path=$(find_test_case_by_name "$TESTNAME") || { |
| 32 | + log_fail "$TESTNAME : Test directory not found." |
| 33 | + echo "$TESTNAME FAIL" > "./$TESTNAME.res" |
| 34 | + exit 1 |
| 35 | +} |
| 36 | + |
| 37 | +cd "$test_path" || exit 1 |
| 38 | +res_file="./$TESTNAME.res" |
| 39 | +rm -f "$res_file" |
| 40 | + |
| 41 | +log_info "------------------------------------------------------------" |
| 42 | +log_info "Starting $TESTNAME Testcase" |
| 43 | + |
| 44 | +BT_NAME="" |
| 45 | +BT_MAC="" |
| 46 | + |
| 47 | +# Get expected BT name |
| 48 | +if [ -n "$1" ]; then |
| 49 | + BT_NAME="$1" |
| 50 | +elif [ -n "$BT_NAME_ENV" ]; then |
| 51 | + BT_NAME="$BT_NAME_ENV" |
| 52 | +elif [ -f "./bt_device_list.txt" ]; then |
| 53 | + BT_NAME=$(awk 'NR==1 {print $1}' ./bt_device_list.txt) |
| 54 | +fi |
| 55 | + |
| 56 | +check_dependencies bluetoothctl rfkill expect hciconfig || { |
| 57 | + echo "$TESTNAME FAIL" > "$res_file" |
| 58 | + exit 1 |
| 59 | +} |
| 60 | + |
| 61 | +cleanup_bt() { |
| 62 | + log_info "Cleaning up previous Bluetooth state..." |
| 63 | + bluetoothctl power on >/dev/null 2>&1 |
| 64 | + if [ -n "$BT_MAC" ]; then |
| 65 | + bluetoothctl remove "$BT_MAC" >/dev/null 2>&1 |
| 66 | + log_info "Unpaired device: $BT_MAC" |
| 67 | + fi |
| 68 | + killall -q bluetoothctl 2>/dev/null |
| 69 | +} |
| 70 | + |
| 71 | +retry() { |
| 72 | + cmd="$1" |
| 73 | + desc="$2" |
| 74 | + max=3 |
| 75 | + count=1 |
| 76 | + while [ "$count" -le "$max" ]; do |
| 77 | + if eval "$cmd"; then |
| 78 | + return 0 |
| 79 | + fi |
| 80 | + log_warn "Retry $count/$max failed: $desc" |
| 81 | + count=$((count + 1)) |
| 82 | + sleep 2 |
| 83 | + done |
| 84 | + return 1 |
| 85 | +} |
| 86 | + |
| 87 | +log_info "Unblocking and powering on Bluetooth" |
| 88 | +rfkill unblock bluetooth |
| 89 | +retry "hciconfig hci0 up" "Bring up hci0" || { |
| 90 | + log_fail "Failed to bring up hci0" |
| 91 | + echo "$TESTNAME FAIL" > "$res_file" |
| 92 | + exit 1 |
| 93 | +} |
| 94 | + |
| 95 | +cleanup_bt |
| 96 | + |
| 97 | +log_info "Scanning for Bluetooth devices..." |
| 98 | +retry "bluetoothctl --timeout 10 scan on > scan.log 2>&1 & sleep 12; killall -q bluetoothctl" "Bluetooth scanning" || { |
| 99 | + log_fail "Device scan failed" |
| 100 | + echo "$TESTNAME FAIL" > "$res_file" |
| 101 | + exit 1 |
| 102 | +} |
| 103 | + |
| 104 | +DEVICES=$(grep -i 'Device' scan.log | sort | uniq) |
| 105 | +echo "$DEVICES" > found_devices.log |
| 106 | +log_info "Devices found during scan: |
| 107 | +$DEVICES" |
| 108 | + |
| 109 | +# If no expected device, consider scan pass |
| 110 | +if [ -z "$BT_NAME" ]; then |
| 111 | + log_pass "No expected device specified. Scan only." |
| 112 | + echo "$TESTNAME PASS" > "$res_file" |
| 113 | + exit 0 |
| 114 | +fi |
| 115 | + |
| 116 | +if echo "$DEVICES" | grep -i "$BT_NAME" >/dev/null; then |
| 117 | + log_info "Expected device '$BT_NAME' found in scan" |
| 118 | + BT_MAC=$(grep -i "$BT_NAME" scan.log | awk '{print $3}' | head -n1) |
| 119 | +else |
| 120 | + log_fail "Expected device '$BT_NAME' not found" |
| 121 | + echo "$TESTNAME FAIL" > "$res_file" |
| 122 | + exit 1 |
| 123 | +fi |
| 124 | + |
| 125 | +if [ -z "$BT_MAC" ]; then |
| 126 | + log_fail "MAC address not found for device '$BT_NAME'" |
| 127 | + echo "$TESTNAME FAIL" > "$res_file" |
| 128 | + exit 1 |
| 129 | +fi |
| 130 | + |
| 131 | +log_info "Attempting to pair with $BT_NAME ($BT_MAC)" |
| 132 | + |
| 133 | +expect <<EOF > pair.log 2>&1 |
| 134 | +spawn bluetoothctl |
| 135 | +expect "#" |
| 136 | +send "agent on\r" |
| 137 | +expect "#" |
| 138 | +send "default-agent\r" |
| 139 | +expect "#" |
| 140 | +send "pair $BT_MAC\r" |
| 141 | +expect { |
| 142 | + "Pairing successful" { |
| 143 | + exit 0 |
| 144 | + } |
| 145 | + "Failed to pair: org.bluez.Error.AlreadyExists" { |
| 146 | + send "remove $BT_MAC\r" |
| 147 | + expect "#" |
| 148 | + send "pair $BT_MAC\r" |
| 149 | + expect { |
| 150 | + "Pairing successful" { exit 0 } |
| 151 | + timeout { exit 1 } |
| 152 | + } |
| 153 | + } |
| 154 | + timeout { |
| 155 | + exit 1 |
| 156 | + } |
| 157 | +} |
| 158 | +EOF |
| 159 | + |
| 160 | +if grep -q "Pairing successful" pair.log; then |
| 161 | + log_pass "Pairing successful with $BT_MAC" |
| 162 | + echo "$TESTNAME PASS" > "$res_file" |
| 163 | +else |
| 164 | + log_fail "Pairing failed with $BT_MAC" |
| 165 | + echo "$TESTNAME FAIL" > "$res_file" |
| 166 | + exit 1 |
| 167 | +fi |
| 168 | + |
| 169 | +cleanup_bt |
| 170 | +log_info "Completed $TESTNAME Testcase" |
0 commit comments