|
| 1 | +# Bluetooth BT_SCAN_PAIR Test |
| 2 | + |
| 3 | +This test automates Bluetooth scanning, pairing, and post-pair verification (via l2ping) on embedded Linux devices using BlueZ and bluetoothctl. It is designed for use in the [qcom-linux-testkit](https://github.com/qualcomm-linux/qcom-linux-testkit) test suite. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Scans for Bluetooth devices (up to `$SCAN_ATTEMPTS` retries, default 2) |
| 8 | +- Pairs with a device by MAC or name (up to `$PAIR_RETRIES` attempts, default 3) |
| 9 | +- Verifies connectivity via `bt_post_pair_connect` and mandatory `l2ping` link check |
| 10 | +- Cleans up prior pairings for repeatable CI runs |
| 11 | +- Accepts input via: |
| 12 | + - Command-line argument 1: device MAC or name |
| 13 | + - Command-line argument 2: whitelist filter (MACs/names) |
| 14 | + - Environment variables: |
| 15 | + - `BT_MAC_ENV` or `BT_NAME_ENV` |
| 16 | + - `BT_WHITELIST_ENV` |
| 17 | + - Fallback: `bt_device_list.txt` |
| 18 | +- Generates summary and detailed logs (`scan.log`, `found_devices.log`, `pair.log`) |
| 19 | + |
| 20 | +## Configuration & Environment Variables |
| 21 | + |
| 22 | +| Variable | Default | Description | |
| 23 | +|---------------------|---------|---------------------------------------------------------------| |
| 24 | +| `PAIR_RETRIES` | `3` | Max pairing attempts per device | |
| 25 | +| `SCAN_ATTEMPTS` | `2` | Max scan retries per device before pairing | |
| 26 | +| `BT_MAC_ENV` | ― | Overrides direct MAC (same format as CLI argument) | |
| 27 | +| `BT_NAME_ENV` | ― | Overrides device name | |
| 28 | +| `BT_WHITELIST_ENV` | ― | Comma-separated MACs or names allowed for pairing | |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +```sh |
| 33 | +# Override defaults (optional): |
| 34 | +export PAIR_RETRIES=5 |
| 35 | +export SCAN_ATTEMPTS=3 |
| 36 | +export BT_NAME_ENV="MySpeaker" |
| 37 | +export BT_WHITELIST_ENV="00:11:22:33:44:55,Speaker2" |
| 38 | + |
| 39 | +# Run test (direct pairing by MAC or name): |
| 40 | +./run.sh [DEVICE_NAME_OR_MAC] [WHITELIST] |
| 41 | +``` |
| 42 | + |
| 43 | +### Examples |
| 44 | + |
| 45 | +```sh |
| 46 | +# 1. Scan-only (no target): |
| 47 | +./run.sh |
| 48 | + |
| 49 | +# 2. Pair by name: |
| 50 | +./run.sh MySpeaker |
| 51 | + |
| 52 | +# 3. Pair by MAC with whitelist: |
| 53 | +./run.sh 00:11:22:33:44:55 12:34:56:78:9A:BC,OtherName |
| 54 | + |
| 55 | +# 4. Use command-line and environment variables: |
| 56 | +export BT_NAME_ENV="MySpeaker" |
| 57 | +export BT_WHITELIST_ENV="00:11:22:33:44:55,Speaker2" |
| 58 | +./run.sh |
| 59 | + |
| 60 | +# 5. Override MAC via environment only: |
| 61 | +export BT_MAC_ENV="00:11:22:33:44:55" |
| 62 | +./run.sh |
| 63 | +``` |
| 64 | + |
| 65 | +## Whitelist Behavior |
| 66 | + |
| 67 | +When a whitelist is specified (CLI or `BT_WHITELIST_ENV`), only devices whose MAC or name matches entries in the comma-separated list will be paired. |
| 68 | + |
| 69 | +```sh |
| 70 | +# Only devices matching 'JBL_Speaker' or '12:34:56:78:9A:BC' |
| 71 | +./run.sh "" "JBL_Speaker,12:34:56:78:9A:BC" |
| 72 | +``` |
| 73 | + |
| 74 | +## Script Flow |
| 75 | + |
| 76 | +1. **Initialization**: locate and source `init_env` and `functestlib.sh`. |
| 77 | +2. **Setup**: |
| 78 | + - Unblock and power on `hci0`. |
| 79 | + - Remove all existing pairings. |
| 80 | +3. **Candidates**: build a list of one or more `(MAC, NAME)` from: |
| 81 | + - CLI arg |
| 82 | + - Env vars |
| 83 | + - `bt_device_list.txt` |
| 84 | +4. **Per-Device Loop**: for each candidate: |
| 85 | + a. Apply whitelist filter (if any). |
| 86 | + b. Up to `$SCAN_ATTEMPTS` scans to detect device. |
| 87 | + c. Up to `$PAIR_RETRIES` pairing attempts via `expect`. |
| 88 | + d. If paired, call `bt_post_pair_connect` then `bt_l2ping_check`. |
| 89 | + e. On success: log PASS, write `BT_SCAN_PAIR PASS`, exit. |
| 90 | + f. On failure: cleanup pairing, move to next candidate. |
| 91 | +5. **Result**: if all candidates fail, log FAIL and write `BT_SCAN_PAIR FAIL`. |
| 92 | + |
| 93 | +## Generated Files |
| 94 | + |
| 95 | +- `BT_SCAN_PAIR.res`: PASS / FAIL / SKIP result code |
| 96 | +- `scan.log`: raw scan output |
| 97 | +- `found_devices.log`: parsed found devices |
| 98 | +- `pair.log`: detailed pairing output and errors |
| 99 | + |
| 100 | +## Troubleshooting |
| 101 | + |
| 102 | +- Ensure `bluetoothctl`, `rfkill`, `expect`, and `hciconfig` are installed and in PATH. |
| 103 | +- Confirm the DUT’s Bluetooth adapter is present and powered on. |
| 104 | +- For headless devices, ensure target is in discoverable/pairing mode. |
| 105 | +- Inspect `scan.log` and `pair.log` for detailed errors. |
| 106 | +- Increase `PAIR_RETRIES` or `SCAN_ATTEMPTS` for flaky environments. |
| 107 | + |
| 108 | +## Helper Functions (in `functestlib.sh`) |
| 109 | + |
| 110 | +- `bt_scan_devices` – performs scan and writes `found_devices_<timestamp>.log` |
| 111 | +- `bt_in_whitelist MAC NAME` – returns 0 if `MAC` or `NAME` matches whitelist |
| 112 | +- `bt_pair_with_mac MAC` – interactive pairing via `expect` with retries |
| 113 | +- `bt_post_pair_connect MAC` – attempts `bluetoothctl connect` with retries |
| 114 | +- `bt_l2ping_check MAC RES_FILE` – verifies link via `l2ping` |
| 115 | +- `bt_cleanup_paired_device MAC` – removes existing pairing |
| 116 | + |
| 117 | +## Integration & CI |
| 118 | + |
| 119 | +- Place `bt_device_list.txt` alongside `run.sh` to drive test data per DUT. |
| 120 | +- Use LAVA YAML to deploy `bt_device_list.txt` and invoke `run.sh`. |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +SPDX-License-Identifier: BSD-3-Clause-Clear |
| 125 | +© Qualcomm Technologies, Inc. and/or its subsidiaries. |
0 commit comments