|
| 1 | +# Copyright 2025 NXP |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import logging |
| 7 | + |
| 8 | +from bumble.core import BT_BR_EDR_TRANSPORT |
| 9 | +from bumble.hci import ( |
| 10 | + HCI_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES_ERROR, |
| 11 | +) |
| 12 | +from bumble.pairing import PairingDelegate |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | +L2CAP_SERVER_PSM = 0x1001 |
| 17 | +MODE = "basic" |
| 18 | +L2CAP_CHAN_IUT_ID = 0 |
| 19 | +MAX_MTU = 0xFFFF |
| 20 | +MIN_MTU = 0x30 |
| 21 | +STRESS_TEST_MAX_COUNT = 50 |
| 22 | + |
| 23 | + |
| 24 | +class Delegate(PairingDelegate): |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + dut, |
| 28 | + io_capability, |
| 29 | + ): |
| 30 | + super().__init__( |
| 31 | + io_capability, |
| 32 | + ) |
| 33 | + self.dut = dut |
| 34 | + |
| 35 | + |
| 36 | +async def device_power_on(device) -> None: |
| 37 | + while True: |
| 38 | + try: |
| 39 | + await device.power_on() |
| 40 | + break |
| 41 | + except Exception: |
| 42 | + continue |
| 43 | + |
| 44 | + |
| 45 | +async def wait_for_shell_response(dut, message): |
| 46 | + found = False |
| 47 | + lines = [] |
| 48 | + try: |
| 49 | + while found is False: |
| 50 | + read_lines = dut.readlines() |
| 51 | + for line in read_lines: |
| 52 | + if message in line: |
| 53 | + found = True |
| 54 | + break |
| 55 | + lines = lines + read_lines |
| 56 | + await asyncio.sleep(0.1) |
| 57 | + except Exception as e: |
| 58 | + logger.error(f'{e}!', exc_info=True) |
| 59 | + raise e |
| 60 | + return found, lines |
| 61 | + |
| 62 | + |
| 63 | +async def send_cmd_to_iut(shell, dut, cmd, parse=None, wait=True): |
| 64 | + found = False |
| 65 | + lines = shell.exec_command(cmd) |
| 66 | + if wait: |
| 67 | + if parse is not None: |
| 68 | + found, lines = await wait_for_shell_response(dut, parse) |
| 69 | + else: |
| 70 | + found = True |
| 71 | + else: |
| 72 | + found = False |
| 73 | + if parse is not None: |
| 74 | + for line in lines: |
| 75 | + if parse in line: |
| 76 | + found = True |
| 77 | + break |
| 78 | + else: |
| 79 | + found = True |
| 80 | + logger.info(f'{lines}') |
| 81 | + assert found is True |
| 82 | + return lines |
| 83 | + |
| 84 | + |
| 85 | +async def bumble_acl_connect(shell, dut, device, target_address): |
| 86 | + connection = None |
| 87 | + try: |
| 88 | + connection = await device.connect(target_address, transport=BT_BR_EDR_TRANSPORT) |
| 89 | + logger.info(f'=== Connected to {connection.peer_address}!') |
| 90 | + except Exception as e: |
| 91 | + logger.error(f'Fail to connect to {target_address}!') |
| 92 | + raise e |
| 93 | + return connection |
| 94 | + |
| 95 | + |
| 96 | +async def bumble_acl_disconnect(shell, dut, device, connection): |
| 97 | + await device.disconnect( |
| 98 | + connection, reason=HCI_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES_ERROR |
| 99 | + ) |
| 100 | + found, lines = await wait_for_shell_response(dut, "Disconnected:") |
| 101 | + logger.info(f'lines : {lines}') |
| 102 | + assert found is True |
| 103 | + return found, lines |
| 104 | + |
| 105 | + |
| 106 | +async def bumble_l2cap_disconnect(shell, dut, incoming_channel, iut_id=L2CAP_CHAN_IUT_ID): |
| 107 | + try: |
| 108 | + await incoming_channel.disconnect() |
| 109 | + except Exception as e: |
| 110 | + logger.error('Fail to send l2cap disconnect command!') |
| 111 | + raise e |
| 112 | + assert incoming_channel.disconnection_result is None |
| 113 | + |
| 114 | + found, _ = await wait_for_shell_response(dut, f"Channel {iut_id} disconnected") |
| 115 | + assert found is True |
0 commit comments