2828
2929# shellcheck disable=SC1090,SC1091
3030. " $TOOLS /functestlib.sh"
31+ # shellcheck disable=SC1090,SC1091
32+ . " $TOOLS /lib_bluetooth.sh"
3133
3234TESTNAME=" BT_SCAN_PAIR"
3335test_path=$( find_test_case_by_name " $TESTNAME " ) || {
3436 log_fail " $TESTNAME : Test directory not found."
3537 echo " $TESTNAME FAIL" > " ./$TESTNAME .res"
36- exit 1
38+ exit 0
3739}
38- cd " $test_path " || exit 1
40+
41+ if ! cd " $test_path " ; then
42+ log_fail " $TESTNAME : Failed to cd into test directory: $test_path "
43+ echo " $TESTNAME FAIL" > " ./$TESTNAME .res"
44+ exit 0
45+ fi
46+
3947RES_FILE=" ./$TESTNAME .res"
4048rm -f " $RES_FILE "
4149
4250log_info " ------------------------------------------------------------"
4351log_info " Starting $TESTNAME Testcase"
4452
53+ # Capture BT_MAC from environment (LAVA secret) *before* we touch BT_MAC locally
54+ BT_ENV_MAC=" "
55+ if [ -n " ${BT_MAC:- } " ]; then
56+ BT_ENV_MAC=" $BT_MAC "
57+ fi
58+
4559# Defaults
4660PAIR_RETRIES=" ${PAIR_RETRIES:- 3} "
47- SCAN_ATTEMPTS=" ${SCAN_ATTEMPTS:- 2} "
4861
4962BT_NAME=" "
5063BT_MAC=" "
5164WHITELIST=" "
5265
53- # Parse CLI args
66+ # -------------------------
67+ # CLI parsing
68+ # -------------------------
5469if [ -n " $1 " ]; then
5570 if echo " $1 " | grep -Eq ' ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$' ; then
71+ # CLI MAC has highest priority
5672 BT_MAC=" $1 "
5773 else
5874 BT_NAME=" $1 "
5975 fi
6076fi
77+
6178if [ -n " $2 " ]; then
6279 WHITELIST=" $2 "
63- if [ -z " $BT_MAC " ] && echo " $2 " | grep -Eq ' ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$' ; then
64- BT_MAC=" $2 "
65- fi
6680fi
6781
68- # Skip if no CLI input and no list file
82+ # If BT_MAC not set by CLI, fall back to BT_ENV_MAC (LAVA export)
83+ if [ -z " $BT_MAC " ] && [ -n " $BT_ENV_MAC " ] && \
84+ echo " $BT_ENV_MAC " | grep -Eq ' ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$' ; then
85+ BT_MAC=" $BT_ENV_MAC "
86+ fi
87+
88+ # Optionally: if BT_MAC still empty and whitelist itself is a MAC, treat it as BT_MAC
89+ if [ -z " $BT_MAC " ] && [ -n " $WHITELIST " ] && \
90+ echo " $WHITELIST " | grep -Eq ' ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$' ; then
91+ BT_MAC=" $WHITELIST "
92+ fi
93+
94+ if [ -n " $BT_MAC " ]; then
95+ log_info " Effective BT_MAC resolved to: $BT_MAC "
96+ fi
97+
98+ # Skip if no MAC/name and no list file
6999if [ -z " $BT_MAC " ] && [ -z " $BT_NAME " ] && [ ! -f " ./bt_device_list.txt" ]; then
70- log_warn " No MAC/name or bt_device_list.txt found. Skipping test."
100+ log_warn " No BT_MAC/BT_NAME and no bt_device_list.txt found. Skipping test."
71101 echo " $TESTNAME SKIP" > " $RES_FILE "
72102 exit 0
73103fi
74104
75105check_dependencies bluetoothctl rfkill expect hciconfig || {
106+ log_warn " Missing required tools; skipping $TESTNAME ."
76107 echo " $TESTNAME SKIP" > " $RES_FILE "
77108 exit 0
78109}
79110
80111cleanup_bt_test () {
112+ # Cleanup only the primary MAC we worked with (if any)
81113 [ -n " $BT_MAC " ] && bt_cleanup_paired_device " $BT_MAC "
82- killall -q bluetoothctl 2> /dev/null
114+ killall -q bluetoothctl 2> /dev/null || true
83115}
84116trap cleanup_bt_test EXIT
85117
86- rfkill unblock bluetooth
87- retry_command_bt " hciconfig hci0 up" " Bring up hci0" || {
88- log_fail " Failed to bring up hci0"
118+ # RF-kill unblock (best effort)
119+ rfkill unblock bluetooth 2> /dev/null || true
120+
121+ # Detect adapter (no hardcoded hci0)
122+ BT_ADAPTER=" ${BT_ADAPTER:- } "
123+ if [ -z " $BT_ADAPTER " ]; then
124+ BT_ADAPTER=" $( listhcis | head -n1) "
125+ fi
126+
127+ if [ -z " $BT_ADAPTER " ]; then
128+ log_fail " No Bluetooth HCI adapter found under /sys/class/bluetooth; cannot run $TESTNAME ."
89129 echo " $TESTNAME FAIL" > " $RES_FILE "
90- exit 1
91- }
130+ exit 0
131+ fi
132+
133+ log_info " Detected Bluetooth adapter: $BT_ADAPTER "
134+
135+ # Ensure controller is visible to bluetoothctl (public-addr bootstrap)
136+ if bt_ensure_controller_visible " $BT_ADAPTER " ; then
137+ log_info " Bluetooth controller is visible to bluetoothctl after bt_ensure_controller_visible."
138+ else
139+ log_fail " Bluetooth controller is not visible to bluetoothctl after bt_ensure_controller_visible."
140+ echo " $TESTNAME FAIL" > " $RES_FILE "
141+ exit 0
142+ fi
143+
144+ # Power on adapter via existing helper; do this regardless of HCICONFIG state.
145+ if ! btpower " $BT_ADAPTER " on; then
146+ log_fail " Failed to power on adapter $BT_ADAPTER via btpower (RF-kill/firmware issue?)."
147+ echo " $TESTNAME FAIL" > " $RES_FILE "
148+ exit 0
149+ fi
150+
151+ # Optional debug: show BD address and confirm firmware/driver presence
152+ bdaddr=" $( btgetbdaddr " $BT_ADAPTER " 2> /dev/null || true) "
153+ [ -n " $bdaddr " ] && log_info " Adapter $BT_ADAPTER BD_ADDR=$bdaddr "
154+
155+ if ! btkmdpresent; then
156+ log_warn " Bluetooth kernel modules / driver not clearly present (btkmdpresent failed)."
157+ fi
92158
159+ if ! btfwpresent > /dev/null 2>&1 ; then
160+ log_warn " No obvious Bluetooth firmware files found (btfwpresent); continuing anyway."
161+ fi
162+
163+ # Remove any previously paired devices to start clean
93164bt_remove_all_paired_devices
94165
95166# Helper: l2ping link verification
@@ -104,37 +175,40 @@ verify_link() {
104175 fi
105176}
106177
107- # Direct pairing if CLI MAC provided
178+ # -------------------------
179+ # Direct pairing path (BT_MAC known)
180+ # -------------------------
108181if [ -n " $BT_MAC " ]; then
109- log_info " Direct pairing requested for $BT_MAC "
182+ log_info " Direct pairing requested for BT_MAC= $BT_MAC (BT_NAME=' $BT_NAME ') "
110183 sleep 2
111184 for attempt in $( seq 1 " $PAIR_RETRIES " ) ; do
112185 log_info " Pair attempt $attempt /$PAIR_RETRIES for $BT_MAC "
186+ bt_cleanup_paired_device " $BT_MAC "
187+
113188 if bt_pair_with_mac " $BT_MAC " ; then
114- log_info " Pair succeeded; connecting to $BT_MAC "
189+ log_info " Pair succeeded; attempting post-pair connect to $BT_MAC "
115190 if bt_post_pair_connect " $BT_MAC " ; then
116191 log_pass " Post-pair connect succeeded for $BT_MAC "
117192 verify_link " $BT_MAC "
118193 else
119- log_warn " Connect failed; trying l2ping fallback for $BT_MAC "
194+ log_warn " Post-pair connect failed; trying l2ping fallback for $BT_MAC "
120195 verify_link " $BT_MAC "
121196 bt_cleanup_paired_device " $BT_MAC "
122197 fi
123198 else
124199 log_warn " Pair failed for $BT_MAC (attempt $attempt )"
125- bt_cleanup_paired_device " $BT_MAC "
126200 fi
127201 done
202+
128203 log_warn " Exhausted direct pairing attempts for $BT_MAC "
129- # If CLI arg was provided, do not fallback on empty .txt
130- if [ -n " $BT_NAME " ] || [ -n " $BT_MAC " ]; then
131- log_fail " Direct pairing failed for ${BT_MAC:- $BT_NAME } "
132- echo " $TESTNAME FAIL" > " $RES_FILE "
133- exit 1
134- fi
204+ log_fail " Direct pairing failed for ${BT_MAC:- $BT_NAME } "
205+ echo " $TESTNAME FAIL" > " $RES_FILE "
206+ exit 0
135207fi
136208
137- # Fallback: only if no CLI input and list exists
209+ # -------------------------
210+ # Fallback list-based flow
211+ # -------------------------
138212if [ -z " $BT_MAC " ] && [ -z " $BT_NAME " ] && [ -f " ./bt_device_list.txt" ]; then
139213 # Skip if list is empty or only comments
140214 if ! grep -v -e ' ^[[:space:]]*#' -e ' ^[[:space:]]*$' bt_device_list.txt | grep -q . ; then
@@ -143,36 +217,37 @@ if [ -z "$BT_MAC" ] && [ -z "$BT_NAME" ] && [ -f "./bt_device_list.txt" ]; then
143217 exit 0
144218 fi
145219
146- log_info " Using fallback list in bt_device_list.txt"
220+ log_info " Using fallback device list in bt_device_list.txt"
147221 while IFS= read -r line || [ -n " $line " ]; do
148222 case " $line " in ' ' |\# * ) continue ;; esac
149- # split into MAC and NAME
223+
224+ # split into MAC and NAME (simple space-separated)
150225 IFS=' ' read -r MAC NAME << EOF
151226$line
152227EOF
153228 [ -z " $MAC " ] && continue
154229
155- # Whitelist filter
230+ # Whitelist filter (name-based simple match)
156231 if [ -n " $WHITELIST " ] && ! printf ' %s' " $NAME " | grep -iq " $WHITELIST " ; then
157232 log_info " Skipping $MAC ($NAME ): not in whitelist '$WHITELIST '"
158233 continue
159234 fi
160235
161- BT_MAC=$MAC
162- BT_NAME=$NAME
236+ BT_MAC=" $MAC "
237+ BT_NAME=" $NAME "
163238
164- log_info " ===== Attempting $BT_MAC ($BT_NAME ) ====="
239+ log_info " ===== Attempting $BT_MAC ($BT_NAME ) from device list ====="
165240 bt_cleanup_paired_device " $BT_MAC "
166241
167242 for attempt in $( seq 1 " $PAIR_RETRIES " ) ; do
168243 log_info " Pair attempt $attempt /$PAIR_RETRIES for $BT_MAC "
169244 if bt_pair_with_mac " $BT_MAC " ; then
170- log_info " Pair succeeded; connecting to $BT_MAC "
245+ log_info " Pair succeeded; attempting post-pair connect to $BT_MAC "
171246 if bt_post_pair_connect " $BT_MAC " ; then
172247 log_pass " Post-pair connect succeeded for $BT_MAC "
173248 verify_link " $BT_MAC "
174249 else
175- log_warn " Connect failed; trying l2ping fallback for $BT_MAC "
250+ log_warn " Post-pair connect failed; trying l2ping fallback for $BT_MAC "
176251 verify_link " $BT_MAC "
177252 fi
178253 else
@@ -181,15 +256,15 @@ EOF
181256 bt_cleanup_paired_device " $BT_MAC "
182257 done
183258
184- log_warn " Exhausted $PAIR_RETRIES attempts for $BT_MAC ; moving to next"
259+ log_warn " Exhausted $PAIR_RETRIES attempts for $BT_MAC ; moving to next entry "
185260 done < " ./bt_device_list.txt"
186261
187- log_fail " All fallback devices failed"
262+ log_fail " All fallback devices from bt_device_list.txt failed"
188263 echo " $TESTNAME FAIL" > " $RES_FILE "
189- exit 1
264+ exit 0
190265fi
191266
192267# Should never reach here
193- log_fail " No execution path matched; exiting"
268+ log_fail " No execution path matched; exiting with FAIL. "
194269echo " $TESTNAME FAIL" > " $RES_FILE "
195- exit 1
270+ exit 0
0 commit comments