Skip to content

Commit a782207

Browse files
MarkWangChinesedanieldegrasse
authored andcommitted
tests: bluetooth: add test case that sdp client l2cap connecting fail
test the sdp request callback is called when sdp l2cap connecting fail. Signed-off-by: Mark Wang <yichang.wang@nxp.com>
1 parent d56f1e0 commit a782207

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

tests/bluetooth/classic/sdp_c/pytest/test_sdp.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424
from bumble.device import Device
2525
from bumble.hci import Address, HCI_Write_Page_Timeout_Command
26+
from bumble.l2cap import ClassicChannel, L2CAP_Connection_Response
2627
from bumble.sdp import (
2728
SDP_BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRIBUTE_ID,
2829
SDP_BROWSE_GROUP_LIST_ATTRIBUTE_ID,
@@ -888,6 +889,59 @@ async def sdp_sa_discover_multiple_records(hci_port, shell, dut, address) -> Non
888889
assert found is True
889890

890891

892+
async def sdp_ssa_discover_fail(hci_port, shell, dut, address) -> None:
893+
def on_app_connection_request(self, request) -> None:
894+
logger.info('Force L2CAP connection failure')
895+
self.destination_cid = request.source_cid
896+
self.send_control_frame(
897+
L2CAP_Connection_Response(
898+
identifier=request.identifier,
899+
destination_cid=self.source_cid,
900+
source_cid=self.destination_cid,
901+
result=L2CAP_Connection_Response.CONNECTION_REFUSED_NO_RESOURCES_AVAILABLE,
902+
status=0x0000,
903+
)
904+
)
905+
906+
# Save the origin method
907+
on_connection_request = ClassicChannel.on_connection_request
908+
# Replace the origin method with a new one to force L2CAP connection failure
909+
ClassicChannel.on_connection_request = on_app_connection_request
910+
911+
logger.info('<<< SDP Discovery ...')
912+
async with await open_transport_or_link(hci_port) as hci_transport:
913+
device = Device.with_hci(
914+
'Bumble',
915+
Address('F0:F1:F2:F3:F4:F5'),
916+
hci_transport.source,
917+
hci_transport.sink,
918+
)
919+
device.classic_enabled = True
920+
device.le_enabled = False
921+
with open(f"bumble_hci_{sys._getframe().f_code.co_name}.log", "wb") as snoop_file:
922+
device.host.snooper = BtSnooper(snoop_file)
923+
await device_power_on(device)
924+
await device.send_command(HCI_Write_Page_Timeout_Command(page_timeout=0xFFFF))
925+
926+
target_address = address.split(" ")[0]
927+
logger.info(f'=== Connecting to {target_address}...')
928+
try:
929+
connection = await device.connect(target_address, transport=BT_BR_EDR_TRANSPORT)
930+
logger.info(f'=== Connected to {connection.peer_address}!')
931+
except Exception as e:
932+
logger.error(f'Fail to connect to {target_address}!')
933+
raise e
934+
935+
# Discover SDP Record
936+
shell.exec_command("sdp_client ssa_discovery_fail")
937+
found, lines = await wait_for_shell_response(dut, "test pass")
938+
logger.info(f'{lines}')
939+
assert found is True
940+
941+
# Restore the origin method
942+
ClassicChannel.on_connection_request = on_connection_request
943+
944+
891945
class TestSdpServer:
892946
def test_sdp_ssa_discover_no_record(self, shell: Shell, dut: DeviceAdapter, sdp_client_dut):
893947
"""Test case to request SDP records. No SDP record registered."""
@@ -966,3 +1020,9 @@ def test_sdp_sa_discover_multiple_records(
9661020
logger.info(f'test_sdp_sa_discover_multiple_records {sdp_client_dut}')
9671021
hci, iut_address = sdp_client_dut
9681022
asyncio.run(sdp_sa_discover_multiple_records(hci, shell, dut, iut_address))
1023+
1024+
def test_sdp_ssa_discover_fail(self, shell: Shell, dut: DeviceAdapter, sdp_client_dut):
1025+
"""Test case to request SDP records. but the L2CAP connecting fail."""
1026+
logger.info(f'test_sdp_ssa_discover_fail {sdp_client_dut}')
1027+
hci, iut_address = sdp_client_dut
1028+
asyncio.run(sdp_ssa_discover_fail(hci, shell, dut, iut_address))

tests/bluetooth/classic/sdp_c/src/sdp_client.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,40 @@ static int cmd_sa_discovery(const struct shell *sh, size_t argc, char *argv[])
280280
return 0;
281281
}
282282

283+
static uint8_t sdp_discover_fail_func(struct bt_conn *conn, struct bt_sdp_client_result *result,
284+
const struct bt_sdp_discover_params *params)
285+
{
286+
if ((result == NULL) || (result->resp_buf == NULL) || (result->resp_buf->len == 0)) {
287+
printk("test pass\n");
288+
} else {
289+
printk("test fail\n");
290+
}
291+
292+
return BT_SDP_DISCOVER_UUID_STOP;
293+
}
294+
295+
static int cmd_ssa_discovery_fail(const struct shell *sh, size_t argc, char *argv[])
296+
{
297+
int err;
298+
299+
sdp_discover.uuid = BT_UUID_DECLARE_16(BT_SDP_HANDSFREE_SVCLASS),
300+
sdp_discover.func = sdp_discover_fail_func;
301+
sdp_discover.pool = &sdp_client_pool;
302+
sdp_discover.type = BT_SDP_DISCOVER_SERVICE_SEARCH_ATTR;
303+
304+
err = bt_sdp_discover(default_conn, &sdp_discover);
305+
if (err) {
306+
shell_error(sh, "Fail to start SDP Discovery (err %d)", err);
307+
return err;
308+
}
309+
return 0;
310+
}
311+
283312
SHELL_STATIC_SUBCMD_SET_CREATE(sdp_client_cmds,
284313
SHELL_CMD_ARG(ss_discovery, NULL, "<UUID>", cmd_ss_discovery, 2, 0),
285314
SHELL_CMD_ARG(sa_discovery, NULL, "<Service Record Handle>", cmd_sa_discovery, 2, 0),
286315
SHELL_CMD_ARG(ssa_discovery, NULL, "<UUID>", cmd_ssa_discovery, 2, 0),
316+
SHELL_CMD_ARG(ssa_discovery_fail, NULL, "", cmd_ssa_discovery_fail, 1, 0),
287317
SHELL_SUBCMD_SET_END
288318
);
289319

0 commit comments

Comments
 (0)