1from dataclasses import dataclass
2import hci_packets as hci
3import link_layer_packets as ll
4import unittest
5from hci_packets import ErrorCode
6from py.bluetooth import Address
7from py.controller import ControllerTest
8
9
10class Test(ControllerTest):
11
12    # Verify that the controller can establish a connection if the remote device
13    # is initiating a connection at the same time. The local device responds to he
14    # page events and accepts the connection. The local connection attempt is
15    # abandoned and a Connection Complete event with status Connection Already
16    # Exists is sent to the Host.
17    async def test(self):
18        # Test parameters.
19        controller = self.controller
20        acl_connection_handle = 0xefe
21        peer_address = Address('11:22:33:44:55:66')
22
23        controller.send_cmd(hci.WriteScanEnable(scan_enable=hci.ScanEnable.PAGE_SCAN_ONLY))
24
25        await self.expect_evt(hci.WriteScanEnableComplete(status=ErrorCode.SUCCESS, num_hci_command_packets=1))
26
27        controller.send_cmd(
28            hci.CreateConnection(
29                bd_addr=peer_address,
30                packet_type=0,
31                page_scan_repetition_mode=hci.PageScanRepetitionMode.R1,
32                allow_role_switch=hci.CreateConnectionRoleSwitch.REMAIN_CENTRAL,
33            ))
34
35        await self.expect_evt(hci.CreateConnectionStatus(status=ErrorCode.SUCCESS, num_hci_command_packets=1))
36
37        await self.expect_ll(
38            ll.Page(source_address=controller.address, destination_address=peer_address, allow_role_switch=False))
39
40        controller.send_ll(
41            ll.Page(source_address=peer_address, destination_address=controller.address, allow_role_switch=False))
42
43        await self.expect_evt(hci.ConnectionRequest(bd_addr=peer_address, link_type=hci.ConnectionRequestLinkType.ACL))
44
45        controller.send_cmd(
46            hci.AcceptConnectionRequest(bd_addr=peer_address, role=hci.AcceptConnectionRequestRole.REMAIN_PERIPHERAL))
47
48        await self.expect_evt(hci.AcceptConnectionRequestStatus(status=ErrorCode.SUCCESS, num_hci_command_packets=1))
49
50        await self.expect_ll(
51            ll.PageResponse(source_address=controller.address, destination_address=peer_address, try_role_switch=False))
52
53        await self.expect_evt(
54            hci.ConnectionComplete(status=ErrorCode.SUCCESS,
55                                   connection_handle=acl_connection_handle,
56                                   bd_addr=peer_address,
57                                   link_type=hci.LinkType.ACL,
58                                   encryption_enabled=hci.Enable.DISABLED))
59