-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·228 lines (201 loc) · 6.34 KB
/
run.sh
File metadata and controls
executable file
·228 lines (201 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/sh
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause#
# BT_SCAN – Bluetooth scanning validation (non-expect version)
# ---------- Repo env + helpers ----------
SCRIPT_DIR="$(
cd "$(dirname "$0")" || exit 1
pwd
)"
INIT_ENV=""
SEARCH="$SCRIPT_DIR"
while [ "$SEARCH" != "/" ]; do
if [ -f "$SEARCH/init_env" ]; then
INIT_ENV="$SEARCH/init_env"
break
fi
SEARCH=$(dirname "$SEARCH")
done
if [ -z "$INIT_ENV" ]; then
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
exit 1
fi
if [ -z "${__INIT_ENV_LOADED:-}" ]; then
# shellcheck disable=SC1090
. "$INIT_ENV"
__INIT_ENV_LOADED=1
fi
# shellcheck disable=SC1091
. "$TOOLS/functestlib.sh"
# shellcheck disable=SC1091
. "$TOOLS/lib_bluetooth.sh"
# ---------- CLI / env parameters ----------
BT_ADAPTER="${BT_ADAPTER-}"
BT_SCAN_TARGET_MAC="${BT_SCAN_TARGET_MAC-}"
BT_TARGET_MAC="${BT_TARGET_MAC-}"
while [ "$#" -gt 0 ]; do
case "$1" in
--adapter)
BT_ADAPTER="$2"
shift 2
;;
--target-mac)
BT_TARGET_MAC="$2"
shift 2
;;
*)
log_warn "Unknown argument ignored: $1"
shift 1
;;
esac
done
TESTNAME="BT_SCAN"
testpath="$(find_test_case_by_name "$TESTNAME")" || {
log_fail "$TESTNAME : Test directory not found."
echo "$TESTNAME FAIL" > "./$TESTNAME.res"
exit 0
}
cd "$testpath" || exit 1
res_file="./$TESTNAME.res"
rm -f "$res_file"
log_info "------------------------------------------------------------"
log_info "Starting $TESTNAME Testcase"
log_info "Checking dependencies: bluetoothctl pgrep"
if ! check_dependencies bluetoothctl pgrep; then
echo "$TESTNAME SKIP" > "$res_file"
exit 0
fi
# -----------------------------
# 1. Ensure bluetoothd is running
# -----------------------------
log_info "Checking if bluetoothd is running..."
retry=0
MAX_RETRIES=3
RETRY_DELAY=5
while [ "$retry" -lt "$MAX_RETRIES" ]; do
if pgrep bluetoothd >/dev/null 2>&1; then
log_info "bluetoothd is running"
break
fi
log_warn "bluetoothd not running, retrying in ${RETRY_DELAY}s..."
sleep "$RETRY_DELAY"
retry=$((retry + 1))
done
if [ "$retry" -eq "$MAX_RETRIES" ]; then
log_fail "bluetoothd not detected after $MAX_RETRIES attempts."
echo "$TESTNAME FAIL" > "$res_file"
exit 0
fi
# -----------------------------
# 2. Detect adapter (CLI/ENV > auto-detect)
# -----------------------------
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"
else
log_warn "No HCI adapter found; skipping test."
echo "$TESTNAME SKIP" > "$res_file"
exit 0
fi
# -----------------------------
# 3. Ensure controller is visible
# -----------------------------
if ! bt_ensure_controller_visible "$ADAPTER"; then
log_warn "SKIP — controller not visible to bluetoothctl."
echo "$TESTNAME SKIP" > "$res_file"
exit 0
fi
# -----------------------------
# 4. Ensure power is ON
# -----------------------------
pw="$(btgetpower "$ADAPTER" 2>/dev/null || true)"
if [ "$pw" = "yes" ]; then
log_pass "Power ON verified before scan."
else
log_info "Controller Power=$pw — enabling now..."
if ! btpower "$ADAPTER" on; then
log_fail "Failed to power ON controller."
echo "$TESTNAME FAIL" > "$res_file"
exit 0
fi
log_pass "Power ON successful."
fi
# -----------------------------
# 5. Determine scan target MAC
# -----------------------------
TARGET_MAC="${BT_SCAN_TARGET_MAC:-$BT_TARGET_MAC}"
if [ -n "$TARGET_MAC" ]; then
log_info "Target MAC provided: $TARGET_MAC — will validate its presence after scan."
else
log_info "No target MAC provided, BT_SCAN will check for generic device visibility."
fi
# -----------------------------
# 6. Scan ON via helper
# -----------------------------
log_info "Testing scan ON..."
if ! bt_set_scan on "$ADAPTER"; then
log_warn "bt_set_scan(on) returned non-zero will still inspect devices list."
fi
# Optional: single Discovering snapshot after scan-on window
dstate_on="$(bt_get_discovering 2>/dev/null || true)"
[ -z "$dstate_on" ] && dstate_on="unknown"
log_info "Discovering state after scan ON window: $dstate_on"
# -----------------------------
# 7. Get devices list after scan ON
# - Try non-interactive bluetoothctl first
# - If empty/flaky, fallback to btctl_script "devices" "quit"
# -----------------------------
devices_out="$(
bt_list_devices_raw 2>/dev/null \
| grep '^Device ' || true
)"
if [ -n "$TARGET_MAC" ]; then
mac_up=$(printf '%s\n' "$TARGET_MAC" | tr '[:lower:]' '[:upper:]')
if printf '%s\n' "$devices_out" \
| awk '/^Device /{print toupper($2)}' \
| grep -q "$mac_up"
then
log_pass "Target MAC $TARGET_MAC detected."
else
log_fail "Target MAC $TARGET_MAC missing after scan ON window."
echo "$TESTNAME FAIL" > "$res_file"
exit 0
fi
else
if [ -n "$devices_out" ]; then
log_info "Devices seen by bluetoothctl after scan ON:"
printf '%s\n' "$devices_out" | while IFS= read -r line; do
[ -n "$line" ] && log_info " $line"
done
log_pass "At least one device discovered."
else
log_fail "No devices discovered in bluetoothctl devices after scan ON."
echo "$TESTNAME FAIL" > "$res_file"
exit 0
fi
fi
# -----------------------------
# 8. Scan OFF via helper + Discovering check
# -----------------------------
log_info "Testing scan OFF..."
if ! bt_set_scan off "$ADAPTER"; then
# bt_set_scan(off) can be flaky on minimal images; rely on poll helper
log_warn "bt_set_scan(off) returned non-zero; continuing with scan-off polling."
fi
# Use lib helper to avoid repetitive log spam and handle 'unknown' cleanly.
if bt_scan_poll_off 10 1; then
# On minimal/ramdisk images bt_scan_poll_off may treat persistent 'unknown' as non-fatal.
log_pass "Scan OFF cleanup completed."
else
# If you keep bt_scan_poll_off strict, this may still warn; not a test failure.
log_warn "Scan OFF cleanup did not confirm Discovering=no (non-fatal)."
fi
echo "$TESTNAME PASS" > "$res_file"
exit 0