Exemplo n.º 1
0
 def get_remote_name(self, remote_address):
     """
     Get the remote name and return a session which can be used for event queue assertion
     """
     self._register_remote_host_supported_features_notification()
     self.device.neighbor.ReadRemoteName(
         neighbor_facade.RemoteNameRequestMsg(
             address=remote_address.encode('utf8'),
             page_scan_repetition_mode=1,
             clock_offset=0x6855))
     return GetRemoteNameSession(self.device)
Exemplo n.º 2
0
    def test_remote_name(self):
        self.register_for_dut_event(
            hci_packets.EventCode.REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION)

        with EventCallbackStream(self.cert_device.hci.FetchEvents(empty_proto.Empty())) as hci_event_stream, \
            EventCallbackStream(self.device_under_test.neighbor.GetRemoteNameEvents(empty_proto.Empty())) as name_event_stream:
            name_event_asserts = EventAsserts(name_event_stream)
            hci_event_asserts = EventAsserts(hci_event_stream)

            cert_name = b'Im_A_Cert'
            padded_name = cert_name
            while len(padded_name) < 248:
                padded_name = padded_name + b'\0'
            self.enqueue_hci_command(
                hci_packets.WriteLocalNameBuilder(padded_name), True)

            hci_event_asserts.assert_event_occurs(
                lambda msg: b'\x0e\x04\x01\x13\x0c' in msg.event)

            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_asserts.assert_event_occurs(get_address_from_complete)

            cert_address = address.encode('utf8')

            self.device_under_test.neighbor.ReadRemoteName(
                neighbor_facade.RemoteNameRequestMsg(
                    address=cert_address,
                    page_scan_repetition_mode=1,
                    clock_offset=0x6855))
            name_event_asserts.assert_event_occurs(
                lambda msg: cert_name in msg.name)
    def pair_justworks(self, cert_iocap_reply, expected_ui_event):
        # Cert event registration
        self.register_for_event(hci_packets.EventCode.LINK_KEY_REQUEST)
        self.register_for_event(hci_packets.EventCode.IO_CAPABILITY_REQUEST)
        self.register_for_event(hci_packets.EventCode.IO_CAPABILITY_RESPONSE)
        self.register_for_event(
            hci_packets.EventCode.USER_PASSKEY_NOTIFICATION)
        self.register_for_event(
            hci_packets.EventCode.USER_CONFIRMATION_REQUEST)
        self.register_for_event(
            hci_packets.EventCode.REMOTE_HOST_SUPPORTED_FEATURES_NOTIFICATION)
        self.register_for_event(hci_packets.EventCode.LINK_KEY_NOTIFICATION)
        self.register_for_event(hci_packets.EventCode.SIMPLE_PAIRING_COMPLETE)
        with EventStream(self.dut.security.FetchUiEvents(empty_proto.Empty())) as dut_ui_stream, \
            EventStream(self.dut.security.FetchBondEvents(empty_proto.Empty())) as dut_bond_stream, \
            EventStream(self.dut.neighbor.GetRemoteNameEvents(empty_proto.Empty())) as dut_name_stream, \
            EventStream(self.cert.hci.FetchEvents(empty_proto.Empty())) as cert_hci_event_stream:

            dut_address = self.dut.hci_controller.GetMacAddress(
                empty_proto.Empty()).address
            cert_address = self.cert.hci_controller.GetMacAddress(
                empty_proto.Empty()).address

            # Enable Simple Secure Pairing
            self.enqueue_hci_command(
                hci_packets.WriteSimplePairingModeBuilder(
                    hci_packets.Enable.ENABLED), True)

            cert_hci_event_stream.assert_event_occurs(
                lambda msg: b'\x0e\x04\x01\x56\x0c' in msg.event)

            # Get the name
            self.dut.neighbor.ReadRemoteName(
                neighbor_facade.RemoteNameRequestMsg(
                    address=cert_address,
                    page_scan_repetition_mode=1,
                    clock_offset=0x6855))

            dut_name_stream.assert_event_occurs(
                lambda msg: self.cert_name in msg.name)

            self.dut.security.CreateBond(
                common.BluetoothAddressWithType(
                    address=common.BluetoothAddress(address=cert_address),
                    type=common.BluetoothAddressTypeEnum.PUBLIC_DEVICE_ADDRESS)
            )

            cert_hci_event_stream.assert_event_occurs(
                lambda event: logging.debug(event.event) or hci_packets.
                EventCode.IO_CAPABILITY_REQUEST in event.event)

            self.enqueue_hci_command(cert_iocap_reply, True)

            cert_hci_event_stream.assert_event_occurs(
                lambda event: logging.debug(event.event) or hci_packets.
                EventCode.USER_CONFIRMATION_REQUEST in event.event)
            self.enqueue_hci_command(
                hci_packets.UserConfirmationRequestReplyBuilder(
                    dut_address.decode('utf8')), True)

            logging.info("Waiting for UI event")
            ui_id = -1

            def get_unique_id(event):
                if (event.message_type == expected_ui_event):
                    nonlocal ui_id
                    ui_id = event.unique_id
                    return True
                return False

            dut_ui_stream.assert_event_occurs(get_unique_id)

            logging.info("Sending UI response")
            self.dut.security.SendUiCallback(
                security_facade.UiCallbackMsg(
                    message_type=security_facade.UiCallbackType.YES_NO,
                    boolean=True,
                    unique_id=ui_id,
                    address=common.BluetoothAddressWithType(
                        address=common.BluetoothAddress(address=cert_address),
                        type=common.BluetoothAddressTypeEnum.
                        PUBLIC_DEVICE_ADDRESS)))