Esempio n. 1
0
 def accept_pairing(self, dut_address, reply_boolean):
     """
         Here we handle the pairing events at the HCI level
     """
     logging.info("Cert: Waiting for LINK_KEY_REQUEST")
     assertThat(self._hci_event_stream).emits(HciMatchers.LinkKeyRequest())
     logging.info("Cert: Sending LINK_KEY_REQUEST_NEGATIVE_REPLY")
     self._enqueue_hci_command(
         hci_packets.LinkKeyRequestNegativeReplyBuilder(
             dut_address.decode('utf8')), True)
     logging.info("Cert: Waiting for IO_CAPABILITY_REQUEST")
     assertThat(self._hci_event_stream).emits(
         HciMatchers.IoCapabilityRequest())
     logging.info("Cert: Sending IO_CAPABILITY_REQUEST_REPLY")
     self._enqueue_hci_command(
         hci_packets.IoCapabilityRequestReplyBuilder(
             dut_address.decode('utf8'), self._io_caps, self._oob_data,
             self._auth_reqs), True)
     logging.info("Cert: Waiting for USER_CONFIRMATION_REQUEST")
     assertThat(self._hci_event_stream).emits(
         HciMatchers.UserConfirmationRequest())
     logging.info("Cert: Sending Simulated User Response '%s'" %
                  reply_boolean)
     if reply_boolean:
         logging.info("Cert: Sending USER_CONFIRMATION_REQUEST_REPLY")
         self._enqueue_hci_command(
             hci_packets.UserConfirmationRequestReplyBuilder(
                 dut_address.decode('utf8')), True)
         logging.info("Cert: Waiting for SIMPLE_PAIRING_COMPLETE")
         assertThat(self._hci_event_stream).emits(
             HciMatchers.SimplePairingComplete())
         logging.info("Cert: Waiting for LINK_KEY_NOTIFICATION")
         assertThat(self._hci_event_stream).emits(
             HciMatchers.LinkKeyNotification())
     else:
         logging.info(
             "Cert: Sending USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY")
         self._enqueue_hci_command(
             hci_packets.UserConfirmationRequestNegativeReplyBuilder(
                 dut_address.decode('utf8')), True)
         logging.info("Cert: Waiting for SIMPLE_PAIRING_COMPLETE")
         assertThat(self._hci_event_stream).emits(
             HciMatchers.SimplePairingComplete())
    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)))