|
| 1 | +#!/bin/sh |
| 2 | +# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause-Clear |
| 4 | +# BT_ON_OFF - Basic Bluetooth power toggle validation (non-expect version) |
| 5 | + |
| 6 | +# Robustly find and source init_env |
| 7 | +SCRIPT_DIR="$( |
| 8 | + cd "$(dirname "$0")" || exit 1 |
| 9 | + pwd |
| 10 | +)" |
| 11 | +INIT_ENV="" |
| 12 | +SEARCH="$SCRIPT_DIR" |
| 13 | + |
| 14 | +while [ "$SEARCH" != "/" ]; do |
| 15 | + if [ -f "$SEARCH/init_env" ]; then |
| 16 | + INIT_ENV="$SEARCH/init_env" |
| 17 | + break |
| 18 | + fi |
| 19 | + SEARCH=$(dirname "$SEARCH") |
| 20 | +done |
| 21 | + |
| 22 | +if [ -z "$INIT_ENV" ]; then |
| 23 | + echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Only source once (idempotent) |
| 28 | +if [ -z "${__INIT_ENV_LOADED:-}" ]; then |
| 29 | + # shellcheck disable=SC1090 |
| 30 | + . "$INIT_ENV" |
| 31 | + __INIT_ENV_LOADED=1 |
| 32 | +fi |
| 33 | + |
| 34 | +# shellcheck disable=SC1091 |
| 35 | +. "$TOOLS/functestlib.sh" |
| 36 | +# shellcheck disable=SC1091 |
| 37 | +. "$TOOLS/lib_bluetooth.sh" |
| 38 | + |
| 39 | +# ---------- CLI / env parameters ---------- |
| 40 | +# BT_ADAPTER can be set from CLI via --adapter or from environment. |
| 41 | +BT_ADAPTER="${BT_ADAPTER-}" |
| 42 | + |
| 43 | +while [ "$#" -gt 0 ]; do |
| 44 | + case "$1" in |
| 45 | + --adapter) |
| 46 | + BT_ADAPTER="$2" |
| 47 | + shift 2 |
| 48 | + ;; |
| 49 | + *) |
| 50 | + log_warn "Unknown argument ignored: $1" |
| 51 | + shift 1 |
| 52 | + ;; |
| 53 | + esac |
| 54 | +done |
| 55 | + |
| 56 | +TESTNAME="BT_ON_OFF" |
| 57 | +testpath="$(find_test_case_by_name "$TESTNAME")" || { |
| 58 | + log_fail "$TESTNAME : Test directory not found." |
| 59 | + echo "$TESTNAME FAIL" > "./$TESTNAME.res" |
| 60 | + exit 1 |
| 61 | +} |
| 62 | + |
| 63 | +cd "$testpath" || exit 1 |
| 64 | +res_file="./$TESTNAME.res" |
| 65 | +rm -f "$res_file" |
| 66 | + |
| 67 | +log_info "------------------------------------------------------------" |
| 68 | +log_info "Starting $TESTNAME Testcase" |
| 69 | +log_info "Checking dependency: bluetoothctl" |
| 70 | + |
| 71 | +# verify that all necessary dependencies |
| 72 | +check_dependencies bluetoothctl pgrep |
| 73 | + |
| 74 | +log_info "Checking if bluetoothd is running..." |
| 75 | +MAX_RETRIES=3 |
| 76 | +RETRY_DELAY=5 |
| 77 | +retry=0 |
| 78 | + |
| 79 | +while [ "$retry" -lt "$MAX_RETRIES" ]; do |
| 80 | + if pgrep bluetoothd >/dev/null 2>&1; then |
| 81 | + log_info "bluetoothd is running" |
| 82 | + break |
| 83 | + fi |
| 84 | + log_warn "bluetoothd not running, retrying in ${RETRY_DELAY}s..." |
| 85 | + sleep "$RETRY_DELAY" |
| 86 | + retry=$((retry + 1)) |
| 87 | +done |
| 88 | + |
| 89 | +if [ "$retry" -eq "$MAX_RETRIES" ]; then |
| 90 | + log_fail "Bluetooth daemon not detected after ${MAX_RETRIES} attempts." |
| 91 | + echo "$TESTNAME FAIL" > "$res_file" |
| 92 | + exit 0 |
| 93 | +fi |
| 94 | + |
| 95 | +# ----------------------------- |
| 96 | +# Detect adapter with precedence: CLI/ENV > auto-detect |
| 97 | +# ----------------------------- |
| 98 | +if [ -n "$BT_ADAPTER" ]; then |
| 99 | + ADAPTER="$BT_ADAPTER" |
| 100 | + log_info "Using adapter from BT_ADAPTER/CLI: $ADAPTER" |
| 101 | +elif findhcisysfs >/dev/null 2>&1; then |
| 102 | + ADAPTER="$(findhcisysfs 2>/dev/null || true)" |
| 103 | +else |
| 104 | + ADAPTER="" |
| 105 | +fi |
| 106 | + |
| 107 | +if [ -n "$ADAPTER" ]; then |
| 108 | + log_info "Using adapter: $ADAPTER" |
| 109 | +else |
| 110 | + log_warn "No HCI adapter found; skipping test." |
| 111 | + echo "$TESTNAME SKIP" > "./$TESTNAME.res" |
| 112 | + exit 0 |
| 113 | +fi |
| 114 | + |
| 115 | +# Ensure controller is visible to bluetoothctl (try public-addr if needed) |
| 116 | +if ! bt_ensure_controller_visible "$ADAPTER"; then |
| 117 | + log_warn "SKIP — no controller visible to bluetoothctl (HCI RAW/DOWN or attach incomplete)." |
| 118 | + echo "$TESTNAME SKIP" > "$res_file" |
| 119 | + exit 0 |
| 120 | +fi |
| 121 | + |
| 122 | +# Read initial power state |
| 123 | +initial_power="$(btgetpower "$ADAPTER" 2>/dev/null || true)" |
| 124 | +[ -z "$initial_power" ] && initial_power="unknown" |
| 125 | +log_info "Initial Powered = $initial_power" |
| 126 | + |
| 127 | +# ---- Power OFF test ---- |
| 128 | +log_info "Powering OFF..." |
| 129 | +if ! btpower "$ADAPTER" off; then |
| 130 | + log_fail "btpower($ADAPTER, off) failed (command-level error)." |
| 131 | + echo "$TESTNAME FAIL" > "$res_file" |
| 132 | + exit 0 |
| 133 | +fi |
| 134 | + |
| 135 | +after_off="$(btgetpower "$ADAPTER" 2>/dev/null || true)" |
| 136 | +[ -z "$after_off" ] && after_off="unknown" |
| 137 | + |
| 138 | +if [ "$after_off" = "no" ]; then |
| 139 | + log_pass "Post-OFF verification: Powered=no (as expected)." |
| 140 | +else |
| 141 | + log_fail "Post-OFF verification failed (Powered=$after_off)." |
| 142 | + echo "$TESTNAME FAIL" > "$res_file" |
| 143 | + exit 0 |
| 144 | +fi |
| 145 | + |
| 146 | +# ---- Power ON test ---- |
| 147 | +log_info "Powering ON..." |
| 148 | +if ! btpower "$ADAPTER" on; then |
| 149 | + log_fail "btpower($ADAPTER, on) failed (command-level error)." |
| 150 | + echo "$TESTNAME FAIL" > "$res_file" |
| 151 | + exit 0 |
| 152 | +fi |
| 153 | + |
| 154 | +after_on="$(btgetpower "$ADAPTER" 2>/dev/null || true)" |
| 155 | +[ -z "$after_on" ] && after_on="unknown" |
| 156 | + |
| 157 | +if [ "$after_on" = "yes" ]; then |
| 158 | + log_pass "Post-ON verification: Powered=yes (as expected)." |
| 159 | + echo "$TESTNAME PASS" > "$res_file" |
| 160 | + exit 0 |
| 161 | +fi |
| 162 | + |
| 163 | +log_fail "Post-ON verification failed (Powered=$after_on)." |
| 164 | +echo "$TESTNAME FAIL" > "$res_file" |
| 165 | +exit 0 |
0 commit comments