Skip to content

Commit 9937be7

Browse files
CanWang001danieldegrasse
authored andcommitted
tests: Bluetooth: BR: Add general test suite for SMP
IUT works as an SM Initiator/Responder. The peer device, SM Responder/Initiator, is a PC running bumble on it. Add shell commands to create and manage L2CAP server and channel with different security level, reboot the board, and transfer data on the SDP channel. In the test suite, there are 28 test cases to verify SMP functions. The test case list is: - BR_SM_INIT_001: Basic Pairing with No Input No Output Capability - BR_SM_INIT_002: Pairing with Display Only Capability - BR_SM_INIT_003: Pairing with Keyboard Only Capability - BR_SM_INIT_005: L2CAP Connection Failure After Successful Pairing - BR_SM_INIT_006: L2CAP Server with Incoming Connection - BR_SM_INIT_008: ACL Disconnection During Pairing - BR_SM_INIT_011: Repeated Pairing Between Same Devices - BR_SM_INIT_012: Security Level Escalation - BR_SM_INIT_013: Multiple Channel Establishment - BR_SM_INIT_017: Pairing Rejection by Remote Device - BR_SM_INIT_018: Pairing Timeout - BR_SM_INIT_021: Authentication Failure during Pairing - BR_SM_INIT_022: Secure Connection vs. Legacy Pairing Fallback - BR_SM_INIT_025: Rejection of Weak Pairing Methods - BR_SM_INIT_028: Reconnection After Key Deletion - BR_SM_INIT_029: Pairing with Delayed Response - BR_SM_INIT_030: Stress Testing with Multiple Connect/Disconnect Cycles - BR_SM_INIT_032: Power Loss Recovery - BR_SM_INIT_034: Pairing During Active Data Transfer - BR_SM_INIT_035: Pairing with Very Long PIN/Passkey - BR_SM_INIT_037: Pairing with Peripheral Requesting Maximum Key Size - BR_SM_INIT_039: Security Level Downgrade Attempt - BR_SM_RSP_001: Basic Non-bondable Pairing with No Input No Output - BR_SM_RSP_002: Display Only with Secure Connection - BR_SM_RSP_003: Keyboard Only with Dedicated Bonding - BR_SM_RSP_006: Security Level Enforcement - BR_SM_RSP_008: L2CAP Connection with Security Level 1 - BR_SM_RSP_009: L2CAP Server with Security Level 2 Signed-off-by: Can Wang <can.wang@nxp.com>
1 parent f4257e1 commit 9937be7

File tree

9 files changed

+3426
-0
lines changed

9 files changed

+3426
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
set(NO_QEMU_SERIAL_BT_SERVER 1)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(bluetooth)
8+
9+
FILE(GLOB app_sources src/*.c)
10+
target_sources(app PRIVATE ${app_sources})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.. _bluetooth_classic_smp_general_tests:
2+
3+
Bluetooth Classic SMP General Tests
4+
##################################
5+
6+
Overview
7+
********
8+
9+
This test suite uses ``bumble`` for testing Bluetooth Classic communication between a host
10+
PC (running :ref:`Twister <twister_script>`) and a device under test (DUT) running Zephyr.
11+
12+
Prerequisites
13+
*************
14+
15+
The test suite has the following prerequisites:
16+
17+
* The ``bumble`` library installed on the host PC.
18+
The Bluetooth Classic controller on PC side is required. Refer to getting started of `bumble`_
19+
for details.
20+
21+
The HCI transport for ``bumble`` can be configured as follows:
22+
23+
* A specific configuration context can be provided along with the ``usb_hci`` fixture separated by
24+
a ``:`` (i.e. specify fixture ``usb_hci:usb:0`` to use the ``usb:0`` as hci transport for
25+
``bumble``).
26+
* The configuration context can be overridden using the `hci transport`_ can be provided using the
27+
``--hci-transport`` test suite argument (i.e. run ``twister`` with the
28+
``--pytest-args=--hci-transport=usb:0`` argument to use the ``usb:0`` as hci transport for
29+
``bumble``).
30+
31+
Building and Running
32+
********************
33+
34+
Running on mimxrt1170_evk@B/mimxrt1176/cm7
35+
==========================================
36+
37+
Running the test suite on :ref:`mimxrt1170_evk` relies on configuration of ``bumble``.
38+
39+
On the host PC, a HCI transport needs to be required. Refer to `bumble platforms`_ page of
40+
``bumble`` for details.
41+
42+
For example, on windows, a PTS dongle is used. After `WinUSB driver`_ has been installed,
43+
the HCI transport would be USB transport interface ``usb:<index>``.
44+
45+
If the HCI transport is ``usb:0`` and debug console port is ``COM4``, the test suite can be
46+
launched using Twister:
47+
48+
.. code-block:: shell
49+
50+
west twister -v -p mimxrt1170_evk@B/mimxrt1176/cm7 --device-testing --device-serial COM4 -T tests/bluetooth/classic/smp_general -O smp_general --force-platform --west-flash --west-runner=jlink -X usb_hci:usb:0
51+
52+
Running on Hardware
53+
===================
54+
55+
Running the test suite on hardware requires a HCI transport connected to the host PC.
56+
57+
The test suite can be launched using Twister. Below is an example for running on the
58+
:zephyr:board:`mimxrt1170_evk@B/mimxrt1176/cm7`:
59+
60+
.. code-block:: shell
61+
62+
west twister -v -p mimxrt1170_evk@B/mimxrt1176/cm7 --device-testing --device-serial COM4 -T tests/bluetooth/classic/smp_general -O smp_general --force-platform --west-flash --west-runner=jlink -X usb_hci:usb:0
63+
64+
.. _bumble:
65+
https://google.github.io/bumble/getting_started.html
66+
67+
.. _hci transport:
68+
https://google.github.io/bumble/transports/index.html
69+
70+
.. _bumble platforms:
71+
https://google.github.io/bumble/platforms/index.html
72+
73+
.. _WinUSB driver:
74+
https://google.github.io/bumble/platforms/windows.html
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#select NXP NW612 Chipset
2+
CONFIG_BT_NXP_NW612=y
3+
4+
CONFIG_ENTROPY_GENERATOR=y
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright 2024 NXP
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
chosen {
9+
zephyr,sram = &dtcm;
10+
};
11+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CONFIG_BT=y
2+
CONFIG_BT_CLASSIC=y
3+
CONFIG_BT_SMP=y
4+
CONFIG_BT_SHELL=y
5+
CONFIG_LOG=y
6+
CONFIG_ZTEST=y
7+
CONFIG_BT_SETTINGS=y
8+
CONFIG_FLASH=y
9+
CONFIG_FLASH_MAP=y
10+
CONFIG_NVS=y
11+
CONFIG_SETTINGS=y
12+
CONFIG_BT_BR_MIN_ENC_KEY_SIZE=16
13+
CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA=10
14+
CONFIG_REBOOT=y
15+
16+
CONFIG_BT_DEVICE_NAME="smp_general"
17+
18+
CONFIG_BT_CREATE_CONN_TIMEOUT=30
19+
CONFIG_BT_PAGE_TIMEOUT=0xFFFF
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2025 NXP
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import contextlib
6+
import logging
7+
import re
8+
import time
9+
10+
import pytest
11+
from twister_harness import DeviceAdapter, Shell
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
def pytest_addoption(parser) -> None:
17+
"""Add local parser options to pytest."""
18+
parser.addoption('--hci-transport', default=None, help='Configuration HCI transport for bumble')
19+
20+
21+
@pytest.fixture(name='initialize', scope='session')
22+
def fixture_initialize(request, shell: Shell, dut: DeviceAdapter):
23+
"""Session initializtion"""
24+
# Get HCI transport for bumble
25+
hci = request.config.getoption('--hci-transport')
26+
27+
if hci is None:
28+
for fixture in dut.device_config.fixtures:
29+
if fixture.startswith('usb_hci:'):
30+
hci = fixture.split(sep=':', maxsplit=1)[1]
31+
break
32+
33+
assert hci is not None
34+
35+
shell.exec_command("bt init")
36+
lines = dut.readlines_until("Settings Loaded")
37+
regex = r'Identity: *(?P<bd_addr>([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}) *\((.*?)\))'
38+
bd_addr = None
39+
for line in lines:
40+
logger.info(f"Shell log {line}")
41+
m = re.search(regex, line)
42+
if m:
43+
bd_addr = m.group('bd_addr')
44+
45+
if bd_addr is None:
46+
logger.error('Fail to get IUT BD address')
47+
raise AssertionError
48+
49+
shell.exec_command("br pscan on")
50+
shell.exec_command("br iscan on")
51+
logger.info('initialized')
52+
return hci, bd_addr
53+
54+
55+
@pytest.fixture
56+
def smp_initiator_dut(initialize):
57+
logger.info('Start running testcase')
58+
yield initialize
59+
logger.info('Done')
60+
61+
62+
def app_handle_device_output(self) -> None:
63+
"""
64+
This method is dedicated to run it in separate thread to read output
65+
from device and put them into internal queue and save to log file.
66+
"""
67+
with open(self.handler_log_path, 'a+') as log_file:
68+
while self.is_device_running():
69+
if self.is_device_connected():
70+
output = self._read_device_output().decode(errors='replace').rstrip("\r\n")
71+
if output:
72+
self._device_read_queue.put(output)
73+
logger.debug(f'{output}\n')
74+
try:
75+
log_file.write(f'{output}\n')
76+
except Exception:
77+
contextlib.suppress(Exception)
78+
log_file.flush()
79+
else:
80+
# ignore output from device
81+
self._flush_device_output()
82+
time.sleep(0.1)
83+
84+
85+
# After reboot, there may be gbk character in the console, so replace _handle_device_output to
86+
# handle the exception.
87+
DeviceAdapter._handle_device_output = app_handle_device_output

0 commit comments

Comments
 (0)