def initiate_connection(self, remote_addr): self.send_command_with_status( hci_packets.CreateConnectionBuilder( remote_addr.decode('utf-8'), 0xcc18, # Packet Type hci_packets.PageScanRepetitionMode.R1, 0x0, hci_packets.ClockOffsetValid.INVALID, hci_packets.CreateConnectionRoleSwitch.ALLOW_ROLE_SWITCH))
def test_connection_dut_connects(self): self.dut_hci.send_command_with_complete( hci_packets.WritePageTimeoutBuilder(0x4000)) # CERT Enables scans and gets its address self.send_hal_hci_command(hci_packets.ReadBdAddrBuilder()) cert_read_bd_addr_capture = HalCaptures.ReadBdAddrCompleteCapture() assertThat(self.cert_hal.get_hci_event_stream()).emits( cert_read_bd_addr_capture) address = cert_read_bd_addr_capture.get().GetBdAddr() self.send_hal_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN)) # DUT Connects self.dut_hci.send_command_with_status( hci_packets.CreateConnectionBuilder( address, 0xcc18, # Packet Type hci_packets.PageScanRepetitionMode.R0, 0, hci_packets.ClockOffsetValid.INVALID, hci_packets.CreateConnectionRoleSwitch.ALLOW_ROLE_SWITCH)) # Cert Accepts connect_request_capture = HalCaptures.ConnectionRequestCapture() assertThat(self.cert_hal.get_hci_event_stream()).emits( connect_request_capture, timeout=timedelta(seconds=20)) connection_request = connect_request_capture.get() self.send_hal_hci_command( hci_packets.AcceptConnectionRequestBuilder( connection_request.GetBdAddr(), hci_packets.AcceptConnectionRequestRole.REMAIN_SLAVE)) (dut_handle, cert_handle) = self._verify_connection_complete() # Send ACL Data self.enqueue_acl_data( dut_handle, hci_packets.PacketBoundaryFlag.FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'Just SomeAclData')) self.send_hal_acl_data( cert_handle, hci_packets.PacketBoundaryFlag.FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'Just SomeMoreAclData')) assertThat(self.cert_hal.get_acl_stream()).emits( lambda packet: b'SomeAclData' in packet.payload) assertThat(self.dut_hci.get_raw_acl_stream()).emits( lambda packet: b'SomeMoreAclData' in packet.data)
def test_connection_cert_connects(self): self.register_for_event(hci_packets.EventCode.CONNECTION_COMPLETE) self.register_for_event( hci_packets.EventCode.CONNECTION_PACKET_TYPE_CHANGED) self.register_for_event(hci_packets.EventCode.CONNECTION_REQUEST) self.send_hal_hci_command(hci_packets.WritePageTimeoutBuilder(0x4000)) with EventStream(self.dut.hci.FetchEvents(empty_proto.Empty())) as hci_event_stream, \ EventStream(self.dut.hci.FetchAclPackets(empty_proto.Empty())) as acl_data_stream, \ EventStream(self.cert.hal.FetchHciEvent(empty_proto.Empty())) as cert_hci_event_stream, \ EventStream(self.cert.hal.FetchHciAcl(empty_proto.Empty())) as cert_acl_data_stream: address = hci_packets.Address() def get_address_from_complete(packet): packet_bytes = packet.event if b'\x0e\x0a\x01\x09\x10' in packet_bytes: nonlocal address addr_view = hci_packets.ReadBdAddrCompleteView( hci_packets.CommandCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes))))) address = addr_view.GetBdAddr() return True return False # DUT Enables scans and gets its address self.enqueue_hci_command( hci_packets.WriteScanEnableBuilder( hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True) self.enqueue_hci_command(hci_packets.ReadBdAddrBuilder(), True) hci_event_stream.assert_event_occurs(get_address_from_complete) # Cert Connects self.send_hal_hci_command( hci_packets.CreateConnectionBuilder( address, 0xcc18, # Packet Type hci_packets.PageScanRepetitionMode.R0, 0, hci_packets.ClockOffsetValid.INVALID, hci_packets.CreateConnectionRoleSwitch.ALLOW_ROLE_SWITCH)) # DUT Accepts connection_request = None def get_connect_request(packet): if b'\x04\x0a' in packet.event: nonlocal connection_request connection_request = hci_packets.ConnectionRequestView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet.event)))) return True return False hci_event_stream.assert_event_occurs( get_connect_request, timeout=timedelta(seconds=20)) self.enqueue_hci_command( hci_packets.AcceptConnectionRequestBuilder( connection_request.GetBdAddr(), hci_packets.AcceptConnectionRequestRole.REMAIN_SLAVE), False) conn_handle = 0xfff def get_handle(packet_bytes): if b'\x03\x0b\x00' in packet_bytes: nonlocal conn_handle cc_view = hci_packets.ConnectionCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes)))) conn_handle = cc_view.GetConnectionHandle() return True return False def event_handle(packet): packet_bytes = packet.event return get_handle(packet_bytes) def payload_handle(packet): packet_bytes = packet.payload return get_handle(packet_bytes) cert_hci_event_stream.assert_event_occurs(payload_handle) cert_handle = conn_handle conn_handle = 0xfff hci_event_stream.assert_event_occurs(event_handle) dut_handle = conn_handle if dut_handle == 0xfff: logging.warning("Failed to get the DUT handle") return False if cert_handle == 0xfff: logging.warning("Failed to get the CERT handle") return False # Send ACL Data self.enqueue_acl_data( dut_handle, hci_packets.PacketBoundaryFlag. FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'This is just SomeAclData')) self.send_hal_acl_data( cert_handle, hci_packets.PacketBoundaryFlag. FIRST_NON_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes(b'This is just SomeMoreAclData')) cert_acl_data_stream.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload) acl_data_stream.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data)
def test_cert_connects(self): self.register_for_event(hci_packets.EventCode.CONNECTION_COMPLETE) self.register_for_event(hci_packets.EventCode.ROLE_CHANGE) self.register_for_event( hci_packets.EventCode.CONNECTION_PACKET_TYPE_CHANGED) with EventCallbackStream(self.cert_device.hci.FetchEvents(empty_proto.Empty())) as cert_hci_event_stream, \ EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \ EventCallbackStream(self.device_under_test.hci_acl_manager.FetchIncomingConnection(empty_proto.Empty())) as incoming_connection_stream, \ EventCallbackStream(self.device_under_test.hci_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream: cert_hci_event_asserts = EventAsserts(cert_hci_event_stream) incoming_connection_asserts = EventAsserts( incoming_connection_stream) cert_acl_data_asserts = EventAsserts(cert_acl_data_stream) acl_data_asserts = EventAsserts(acl_data_stream) # DUT Enables scans and gets its address dut_address = self.device_under_test.hci_controller.GetMacAddress( empty_proto.Empty()).address self.device_under_test.neighbor.EnablePageScan( neighbor_facade.EnableMsg(enabled=True)) # Cert connects self.enqueue_hci_command( hci_packets.CreateConnectionBuilder( dut_address.decode('utf-8'), 0xcc18, # Packet Type hci_packets.PageScanRepetitionMode.R1, 0x0, hci_packets.ClockOffsetValid.INVALID, hci_packets.CreateConnectionRoleSwitch.ALLOW_ROLE_SWITCH), False) conn_handle = 0xfff def get_handle(packet): packet_bytes = packet.event if b'\x03\x0b\x00' in packet_bytes: nonlocal conn_handle cc_view = hci_packets.ConnectionCompleteView( hci_packets.EventPacketView( bt_packets.PacketViewLittleEndian( list(packet_bytes)))) conn_handle = cc_view.GetConnectionHandle() return True return False # DUT gets a connection request incoming_connection_asserts.assert_event_occurs(get_handle) self.device_under_test.hci_acl_manager.SendAclData( acl_manager_facade.AclData( handle=conn_handle, payload=bytes( b'\x29\x00\x07\x00This is just SomeMoreAclData from the DUT' ))) conn_handle = 0xfff cert_hci_event_asserts.assert_event_occurs(get_handle) cert_handle = conn_handle self.enqueue_acl_data( cert_handle, hci_packets.PacketBoundaryFlag.FIRST_AUTOMATICALLY_FLUSHABLE, hci_packets.BroadcastFlag.POINT_TO_POINT, bytes( b'\x26\x00\x07\x00This is just SomeAclData from the Cert')) cert_acl_data_asserts.assert_event_occurs( lambda packet: b'SomeMoreAclData' in packet.data) acl_data_asserts.assert_event_occurs( lambda packet: b'SomeAclData' in packet.payload)