Skip to content

Commit 6e7ab2b

Browse files
committed
Bluetooth: Rename bt_on_off test and add robust BT_SCAN_PAIR test
- Renamed existing Bluetooth validation script to bt_on_off for clarity - Added new BT_SCAN_PAIR test: - Supports dynamic BT device name input - Uses bluetoothctl and expect for pairing flow - Includes retry logic for hci0 up, scan, and pairing steps - Performs full scan-pair-cleanup lifecycle - Logs all intermediate steps for better CI debugging - Compatible with POSIX and ShellCheck guidelines Resolves issues in earlier pairing logic and supports robust CI execution with dynamic device handling. Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent d75bcd4 commit 6e7ab2b

File tree

5 files changed

+216
-13
lines changed

5 files changed

+216
-13
lines changed
File renamed without changes.

Runner/suites/Connectivity/Bluetooth/run.sh renamed to Runner/suites/Connectivity/Bluetooth/BT_ON_FF/run.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
5-
6-
# Source init_env and functestlib.sh
5+
6+
# Robustly find and source init_env
77
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
88
INIT_ENV=""
99
SEARCH="$SCRIPT_DIR"
@@ -14,31 +14,32 @@ while [ "$SEARCH" != "/" ]; do
1414
fi
1515
SEARCH=$(dirname "$SEARCH")
1616
done
17-
17+
1818
if [ -z "$INIT_ENV" ]; then
1919
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
2020
exit 1
2121
fi
22-
23-
# shellcheck disable=SC1090
24-
. "$INIT_ENV"
25-
22+
23+
if [ -z "$__INIT_ENV_LOADED" ]; then
24+
# shellcheck disable=SC1090
25+
. "$INIT_ENV"
26+
fi
2627
# shellcheck disable=SC1090,SC1091
2728
. "$TOOLS/functestlib.sh"
28-
29-
TESTNAME="Bluetooth"
29+
30+
TESTNAME="BT_ON_FF"
3031
test_path=$(find_test_case_by_name "$TESTNAME") || {
3132
log_fail "$TESTNAME : Test directory not found."
3233
echo "$TESTNAME FAIL" > "./$TESTNAME.res"
3334
exit 1
3435
}
35-
36+
3637
cd "$test_path" || exit 1
3738
res_file="./$TESTNAME.res"
3839
rm -f "$res_file"
39-
40-
log_info "-----------------------------------------------------------------------------------------"
41-
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
40+
41+
log_info "------------------------------------------------------------"
42+
log_info "Starting $TESTNAME Testcase"
4243
log_info "Checking dependency: bluetoothctl"
4344
check_dependencies bluetoothctl
4445

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Bluetooth Scan and Pair Test
2+
3+
## Test Name
4+
`BT_SCAN_PAIR`
5+
6+
## Description
7+
This script validates Bluetooth functionality on a target device by:
8+
- Ensuring the Bluetooth controller (`hci0`) is available and powered on
9+
- Scanning for available Bluetooth devices
10+
- Matching and pairing with a specified expected device
11+
12+
## Prerequisites
13+
- `bluetoothctl`
14+
- `rfkill`
15+
- `hciconfig`
16+
- `expect`
17+
18+
## Input
19+
You can provide the expected device via:
20+
- First command-line argument
21+
- `BT_NAME_ENV` environment variable
22+
- `bt_device_list.txt` (1st line)
23+
24+
Example `bt_device_list.txt`:
25+
```
26+
QCOM-BTD
27+
```
28+
29+
## Cleanup
30+
The script will:
31+
- Unpair the expected device after test (to keep CI clean)
32+
- Kill background `bluetoothctl`

Runner/suites/Connectivity/Bluetooth/BT_SCAN_PAIR/bt_device_list.txt

Whitespace-only changes.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)