|
| 1 | + |
| 2 | +# Bluetooth BT_SCAN_PAIR Test |
| 3 | + |
| 4 | +This test automates Bluetooth scanning and pairing for 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. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- Scans for Bluetooth devices |
| 9 | +- Optionally pairs with a device by name or MAC address |
| 10 | +- Retries pairing on failure, including handling for busy/temporarily unavailable devices |
| 11 | +- Cleans up previous pairings for repeatable CI runs |
| 12 | +- Accepts device name/MAC as argument, environment variable, or in `bt_device_list.txt` |
| 13 | +- Generates summary and detailed logs (`scan.log`, `pair.log`, `found_devices.log`) |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```sh |
| 18 | +./run.sh [DEVICE_NAME_OR_MAC] [WHITELIST] |
| 19 | +``` |
| 20 | +- `DEVICE_NAME_OR_MAC` – (optional) Device name or MAC address to pair. |
| 21 | + - Can also be set as `BT_NAME_ENV` or in `bt_device_list.txt` |
| 22 | +- `WHITELIST` – (optional) Comma-separated MACs/names allowed for pairing. |
| 23 | + - Can also be set as `BT_WHITELIST_ENV` |
| 24 | + |
| 25 | +If no device name is given, only scanning is performed and the test passes if devices are found. |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +```sh |
| 30 | +./run.sh [BT_NAME] [WHITELIST] |
| 31 | +``` |
| 32 | + |
| 33 | +- `BT_NAME` - Optional. Bluetooth name or MAC to search for. |
| 34 | +- `WHITELIST` - Optional. Comma-separated names/MACs allowed for pairing. |
| 35 | + |
| 36 | +- Scan for any device (no pairing): |
| 37 | + |
| 38 | + ``` |
| 39 | + ./run.sh |
| 40 | + ``` |
| 41 | + |
| 42 | +- Scan and pair with a device named "MySpeaker": |
| 43 | + |
| 44 | + ``` |
| 45 | + ./run.sh MySpeaker |
| 46 | + ``` |
| 47 | + |
| 48 | +- Scan and pair only if device MAC is in whitelist: |
| 49 | + |
| 50 | + ``` |
| 51 | + ./run.sh MySpeaker 00:11:22:33:44:55,AnotherSpeaker |
| 52 | + ``` |
| 53 | + |
| 54 | +- Use environment variables: |
| 55 | + |
| 56 | + ``` |
| 57 | + export BT_NAME_ENV="MySpeaker" |
| 58 | + export BT_WHITELIST_ENV="00:11:22:33:44:55" |
| 59 | + ./run.sh |
| 60 | + ``` |
| 61 | + |
| 62 | +- Device list file (first line is used): |
| 63 | + |
| 64 | + ``` |
| 65 | + echo "MySpeaker" > bt_device_list.txt |
| 66 | + ./run.sh |
| 67 | + ``` |
| 68 | + |
| 69 | +## Whitelist Usage |
| 70 | + |
| 71 | +To ensure only known devices are considered during scan: |
| 72 | + |
| 73 | +```sh |
| 74 | +./run.sh JBL_Speaker "JBL_Speaker,12:34:56:78:9A:BC" |
| 75 | +``` |
| 76 | + |
| 77 | +## Arguments & Variables |
| 78 | + |
| 79 | +- Argument 1: Device name or MAC address (takes precedence) |
| 80 | +- `BT_NAME_ENV`: Device name or MAC from the environment |
| 81 | +- `bt_device_list.txt`: Fallback if argument or env is not set |
| 82 | + |
| 83 | +## Example Summary Output |
| 84 | + |
| 85 | +``` |
| 86 | +[INFO] 2025-06-23 10:00:00 - Starting BT_SCAN_PAIR Testcase |
| 87 | +[INFO] 2025-06-23 10:00:02 - Unblocking and powering on Bluetooth |
| 88 | +[INFO] 2025-06-23 10:00:05 - Devices found during scan: |
| 89 | +Device 12:34:56:78:9A:BC SomeBTHeadset |
| 90 | +[INFO] 2025-06-23 10:00:06 - Expected device 'SomeBTHeadset' found in scan |
| 91 | +[PASS] 2025-06-23 10:00:08 - Pairing successful with 12:34:56:78:9A:BC |
| 92 | +``` |
| 93 | + |
| 94 | +## Result |
| 95 | + |
| 96 | +- PASS: Device paired (or scan-only with no target) |
| 97 | +- FAIL: Device not found, not in whitelist, or pairing failed |
| 98 | +- All paired devices are removed after the test |
| 99 | + |
| 100 | +## Files Generated |
| 101 | + |
| 102 | +- `BT_SCAN_PAIR.res`: Test PASS/FAIL/SKIP result |
| 103 | +- `scan.log`: Output of Bluetooth device scan |
| 104 | +- `found_devices.log`: List of discovered device names/MACs |
| 105 | +- `pair.log`: Detailed pairing output and errors |
| 106 | + |
| 107 | +## Troubleshooting |
| 108 | + |
| 109 | +- Ensure `bluetoothctl`, `rfkill`, `expect`, and `hciconfig` are available. |
| 110 | +- For headless automation, the remote device must be in pairing/discoverable mode. |
| 111 | +- The script retries pairing if "busy" or "temporarily unavailable" errors are seen. |
| 112 | +- Check `pair.log` and `scan.log` for detailed debug info if a failure occurs. |
| 113 | + |
| 114 | +## Helper Functions (in functestlib.sh) |
| 115 | + |
| 116 | +- `bt_scan_devices` – Scans and logs found BT devices |
| 117 | +- `bt_pair_with_mac` – Attempts pairing via expect with retries |
| 118 | +- `bt_in_whitelist` – Checks if MAC/name is in whitelist |
| 119 | +- `bt_cleanup_paired_device` – Removes paired device by MAC |
| 120 | + |
| 121 | +## Customization |
| 122 | + |
| 123 | +- **Whitelist**: You can restrict scan to a whitelist of MAC addresses or names using environment variables or script customization. |
| 124 | +- **Retries/Timeouts**: Retry and timeout values can be set in the script for more robust pairing. |
| 125 | + |
| 126 | +## Integration with LAVA |
| 127 | + |
| 128 | +In your LAVA job: |
| 129 | + |
| 130 | +```yaml |
| 131 | +deploy: |
| 132 | + to: tftp |
| 133 | + images: |
| 134 | + bt_device_list.txt: |
| 135 | + image: path/to/bt_device_list.txt |
| 136 | + compression: none |
| 137 | +``` |
| 138 | +
|
| 139 | +Injects a per-DUT Bluetooth configuration. |
| 140 | +
|
| 141 | +## Dependencies |
| 142 | +
|
| 143 | +- `bluetoothctl`, `expect`, `rfkill`, `hciconfig` |
| 144 | +- BlueZ stack running on embedded Linux |
| 145 | + |
| 146 | +## License |
| 147 | + |
| 148 | +SPDX-License-Identifier: BSD-3-Clause-Clear |
| 149 | +Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
0 commit comments