Example #1
0
 def _set_link_from_dut_and_open_channel(self,
                                         signal_id=1,
                                         scid=0x0101,
                                         psm=0x33,
                                         mtu=1000,
                                         mps=100,
                                         initial_credit=6):
     # Cert Advertises
     gap_name = hci_packets.GapData()
     gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
     gap_name.data = list(bytes(b'Im_The_DUT'))
     gap_data = le_advertising_facade.GapDataMsg(data=bytes(gap_name.Serialize()))
     config = le_advertising_facade.AdvertisingConfig(
         advertisement=[gap_data],
         interval_min=512,
         interval_max=768,
         event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
         address_type=common.RANDOM_DEVICE_ADDRESS,
         channel_map=7,
         filter_policy=le_advertising_facade.AdvertisingFilterPolicy.ALL_DEVICES)
     request = le_advertising_facade.CreateAdvertiserRequest(config=config)
     create_response = self.cert.hci_le_advertising_manager.CreateAdvertiser(request)
     response_future = self.dut_l2cap.connect_coc_to_cert(self.cert_address, psm)
     self.cert_l2cap.wait_for_connection()
     # TODO: Currently we can only connect by using Dynamic channel API. Use fixed channel instead.
     cert_channel = self.cert_l2cap.verify_and_respond_open_channel_from_remote(psm)
     dut_channel = response_future.get_channel()
     return (dut_channel, cert_channel)
Example #2
0
    def test_le_ad_scan_dut_scans(self):
        privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=common.BluetoothAddressWithType(
                address=common.BluetoothAddress(
                    address=bytes(b'D0:05:04:03:02:01')),
                type=common.RANDOM_DEVICE_ADDRESS),
            rotation_irk=
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.dut.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
            privacy_policy)
        cert_privacy_policy = le_initiator_address_facade.PrivacyPolicy(
            address_policy=le_initiator_address_facade.AddressPolicy.
            USE_STATIC_ADDRESS,
            address_with_type=common.BluetoothAddressWithType(
                address=common.BluetoothAddress(
                    address=bytes(b'C0:05:04:03:02:01')),
                type=common.RANDOM_DEVICE_ADDRESS),
            rotation_irk=
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
            minimum_rotation_time=0,
            maximum_rotation_time=0)
        self.cert.hci_le_initiator_address.SetPrivacyPolicyForInitiatorAddress(
            cert_privacy_policy)
        with EventStream(
                # DUT Scans
                self.dut.hci_le_scanning_manager.StartScan(
                    empty_proto.Empty())) as advertising_event_stream:

            # CERT Advertises
            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_The_CERT!'))
            gap_data = le_advertising_facade.GapDataMsg(
                data=bytes(gap_name.Serialize()))
            config = le_advertising_facade.AdvertisingConfig(
                advertisement=[gap_data],
                interval_min=512,
                interval_max=768,
                event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
                address_type=common.RANDOM_DEVICE_ADDRESS,
                channel_map=7,
                filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
                ALL_DEVICES)
            request = le_advertising_facade.CreateAdvertiserRequest(
                config=config)

            create_response = self.cert.hci_le_advertising_manager.CreateAdvertiser(
                request)

            advertising_event_stream.assert_event_occurs(
                lambda packet: b'Im_The_CERT' in packet.event)

            remove_request = le_advertising_facade.RemoveAdvertiserRequest(
                advertiser_id=create_response.advertiser_id)
            self.cert.hci_le_advertising_manager.RemoveAdvertiser(
                remove_request)
Example #3
0
    def test_le_connect_list_connection_cert_advertises(self):
        self.dut_hci.register_for_le_events(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)
        # DUT Connects
        self.dut_hci.send_command_with_complete(
            hci_packets.LeSetRandomAddressBuilder('0D:05:04:03:02:01'))
        self.dut_hci.send_command_with_complete(
            hci_packets.LeAddDeviceToConnectListBuilder(
                hci_packets.ConnectListAddressType.RANDOM,
                '0C:05:04:03:02:01'))
        phy_scan_params = DirectHciTest._create_phy_scan_params()
        self.dut_hci.send_command_with_status(
            hci_packets.LeExtendedCreateConnectionBuilder(
                hci_packets.InitiatorFilterPolicy.USE_CONNECT_LIST,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                'BA:D5:A4:A3:A2:A1', 1, [phy_scan_params]))

        # CERT Advertises
        advertising_handle = 1
        self.send_hal_hci_command(
            hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                advertising_handle,
                hci_packets.LegacyAdvertisingProperties.ADV_IND,
                512,
                768,
                7,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.PeerAddressType.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                'A6:A5:A4:A3:A2:A1',
                hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                0x7F,
                0,  # SID
                hci_packets.Enable.DISABLED  # Scan request notification
            ))

        self.send_hal_hci_command(
            hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                advertising_handle, '0C:05:04:03:02:01'))

        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(b'Im_A_Cert'))

        self.send_hal_hci_command(
            hci_packets.LeSetExtendedAdvertisingDataBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_name]))
        enabled_set = hci_packets.EnabledSet()
        enabled_set.advertising_handle = 1
        enabled_set.duration = 0
        enabled_set.max_extended_advertising_events = 0
        self.send_hal_hci_command(
            hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                hci_packets.Enable.ENABLED, [enabled_set]))

        # LeConnectionComplete
        self._verify_le_connection_complete()
Example #4
0
    def test_le_ad_scan_dut_advertises(self):
        self.register_for_le_event(hci_packets.SubeventCode.ADVERTISING_REPORT)
        self.register_for_le_event(
            hci_packets.SubeventCode.EXTENDED_ADVERTISING_REPORT)
        with EventStream(self.cert.hci.FetchLeSubevents(
                empty_proto.Empty())) as hci_le_event_stream:

            # CERT Scans
            self.enqueue_hci_command(
                hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'),
                True)
            scan_parameters = hci_packets.PhyScanParameters()
            scan_parameters.le_scan_type = hci_packets.LeScanType.ACTIVE
            scan_parameters.le_scan_interval = 40
            scan_parameters.le_scan_window = 20
            self.enqueue_hci_command(
                hci_packets.LeSetExtendedScanParametersBuilder(
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.LeScanningFilterPolicy.ACCEPT_ALL, 1,
                    [scan_parameters]), True)
            self.enqueue_hci_command(
                hci_packets.LeSetExtendedScanEnableBuilder(
                    hci_packets.Enable.ENABLED,
                    hci_packets.FilterDuplicates.DISABLED, 0, 0), True)

            # DUT Advertises
            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_The_DUT'))
            gap_data = le_advertising_facade.GapDataMsg(
                data=bytes(gap_name.Serialize()))
            config = le_advertising_facade.AdvertisingConfig(
                advertisement=[gap_data],
                interval_min=512,
                interval_max=768,
                event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
                address_type=common.RANDOM_DEVICE_ADDRESS,
                channel_map=7,
                filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
                ALL_DEVICES)
            request = le_advertising_facade.CreateAdvertiserRequest(
                config=config)

            create_response = self.dut.hci_le_advertising_manager.CreateAdvertiser(
                request)

            hci_le_event_stream.assert_event_occurs(
                lambda packet: b'Im_The_DUT' in packet.event)

            remove_request = le_advertising_facade.RemoveAdvertiserRequest(
                advertiser_id=create_response.advertiser_id)
            self.dut.hci_le_advertising_manager.RemoveAdvertiser(
                remove_request)
            self.enqueue_hci_command(
                hci_packets.LeSetScanEnableBuilder(
                    hci_packets.Enable.DISABLED, hci_packets.Enable.DISABLED),
                True)
Example #5
0
 def _setup_link_from_cert(self):
     # DUT Advertises
     gap_name = hci_packets.GapData()
     gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
     gap_name.data = list(bytes(b'Im_The_DUT'))
     gap_data = le_advertising_facade.GapDataMsg(data=bytes(gap_name.Serialize()))
     config = le_advertising_facade.AdvertisingConfig(
         advertisement=[gap_data],
         interval_min=512,
         interval_max=768,
         event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
         address_type=common.RANDOM_DEVICE_ADDRESS,
         channel_map=7,
         filter_policy=le_advertising_facade.AdvertisingFilterPolicy.ALL_DEVICES)
     request = le_advertising_facade.CreateAdvertiserRequest(config=config)
     create_response = self.dut.hci_le_advertising_manager.CreateAdvertiser(request)
     self.cert_l2cap.connect_le_acl(self.dut_address)
Example #6
0
    def test_inquiry_extended_from_dut(self):
        self._set_name()
        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(self.cert_name))
        gap_data = list([gap_name])

        self.cert_hci.send_command(
            hci_packets.WriteExtendedInquiryResponseBuilder(hci_packets.FecRequired.NOT_REQUIRED, gap_data))
        inquiry_msg = neighbor_facade.InquiryMsg(
            inquiry_mode=neighbor_facade.DiscoverabilityMode.GENERAL,
            result_mode=neighbor_facade.ResultMode.EXTENDED,
            length_1_28s=8,
            max_results=0)
        session = self.dut_neighbor.set_inquiry_mode(inquiry_msg)
        self.cert_hci.send_command(hci_packets.WriteScanEnableBuilder(hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN))
        assertThat(session).emits(
            NeighborMatchers.ExtendedInquiryResult(self.cert_address), timeout=timedelta(seconds=10))
Example #7
0
    def test_le_ad_scan_dut_scans(self):
        with EventCallbackStream(
                # DUT Scans
                self.device_under_test.hci_le_scanning_manager.StartScan(
                    empty_proto.Empty())) as advertising_event_stream:

            hci_event_asserts = EventAsserts(advertising_event_stream)

            # CERT Advertises
            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_The_CERT!'))
            gap_data = le_advertising_facade.GapDataMsg(
                data=bytes(gap_name.Serialize()))
            config = le_advertising_facade.AdvertisingConfig(
                advertisement=[gap_data],
                random_address=common.BluetoothAddress(
                    address=bytes(b'A6:A5:A4:A3:A2:A1')),
                interval_min=512,
                interval_max=768,
                event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
                address_type=common.RANDOM_DEVICE_ADDRESS,
                peer_address_type=common.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                peer_address=common.BluetoothAddress(
                    address=bytes(b'0C:05:04:03:02:01')),
                channel_map=7,
                filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
                ALL_DEVICES)
            request = le_advertising_facade.CreateAdvertiserRequest(
                config=config)

            create_response = self.cert_device.hci_le_advertising_manager.CreateAdvertiser(
                request)

            hci_event_asserts.assert_event_occurs(
                lambda packet: b'Im_The_CERT' in packet.event)

            remove_request = le_advertising_facade.RemoveAdvertiserRequest(
                advertiser_id=create_response.advertiser_id)
            self.cert_device.hci_le_advertising_manager.RemoveAdvertiser(
                remove_request)
    def test_inquiry_extended_from_dut(self):
        name_string = b'Im_A_Cert'
        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(name_string))
        gap_data = list([gap_name])

        self.enqueue_hci_command(
            hci_packets.WriteExtendedInquiryResponseBuilder(
                hci_packets.FecRequired.NOT_REQUIRED, gap_data), True)
        inquiry_msg = neighbor_facade.InquiryMsg(
            inquiry_mode=neighbor_facade.DiscoverabilityMode.GENERAL,
            result_mode=neighbor_facade.ResultMode.EXTENDED,
            length_1_28s=3,
            max_results=0)
        with EventStream(self.dut.neighbor.SetInquiryMode(
                inquiry_msg)) as inquiry_event_stream:
            self.enqueue_hci_command(
                hci_packets.WriteScanEnableBuilder(
                    hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN), True)
            inquiry_event_stream.assert_event_occurs(
                lambda msg: name_string in msg.packet)
Example #9
0
 def _prepare_cert_for_connection(self):
     # DUT Advertises
     gap_name = hci_packets.GapData()
     gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
     gap_name.data = list(bytes(b'Im_The_CERT'))
     gap_data = le_advertising_facade.GapDataMsg(
         data=bytes(gap_name.Serialize()))
     config = le_advertising_facade.AdvertisingConfig(
         advertisement=[gap_data],
         random_address=self.cert_address.address,
         interval_min=512,
         interval_max=768,
         event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
         address_type=common.RANDOM_DEVICE_ADDRESS,
         peer_address_type=common.PUBLIC_DEVICE_ADDRESS,
         peer_address=common.BluetoothAddress(
             address=bytes(b'00:00:00:00:00:00')),
         channel_map=7,
         filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
         ALL_DEVICES)
     request = le_advertising_facade.CreateAdvertiserRequest(config=config)
     create_response = self.cert.hci_le_advertising_manager.CreateAdvertiser(
         request)
Example #10
0
    def test_le_ad_scan_cert_advertises(self):
        with EventCallbackStream(
                self.device_under_test.hal.FetchHciEvent(
                    empty_pb2.Empty())) as hci_event_stream:
            hci_event_asserts = EventAsserts(hci_event_stream)

            # DUT scans
            self.send_dut_hci_command(
                hci_packets.LeSetRandomAddressBuilder('0D:05:04:03:02:01'))
            phy_scan_params = hci_packets.PhyScanParameters()
            phy_scan_params.le_scan_interval = 6553
            phy_scan_params.le_scan_window = 6553
            phy_scan_params.le_scan_type = hci_packets.LeScanType.ACTIVE

            self.send_dut_hci_command(
                hci_packets.LeSetExtendedScanParametersBuilder(
                    hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.LeSetScanningFilterPolicy.ACCEPT_ALL, 1,
                    [phy_scan_params]))
            self.send_dut_hci_command(
                hci_packets.LeSetExtendedScanEnableBuilder(
                    hci_packets.Enable.ENABLED,
                    hci_packets.FilterDuplicates.DISABLED, 0, 0))

            # CERT Advertises
            advertising_handle = 0
            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                    advertising_handle,
                    hci_packets.LegacyAdvertisingProperties.ADV_IND,
                    512,
                    768,
                    7,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.PeerAddressType.
                    PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                    'A6:A5:A4:A3:A2:A1',
                    hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                    0x7F,
                    0,  # SID
                    hci_packets.Enable.DISABLED  # Scan request notification
                ))

            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                    advertising_handle, '0C:05:04:03:02:01'))

            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(
                bytes(b'Im_A_Cert!'))  # TODO: Fix and remove !

            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingDataBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_name]))
            enabled_set = hci_packets.EnabledSet()
            enabled_set.advertising_handle = advertising_handle
            enabled_set.duration = 0
            enabled_set.max_extended_advertising_events = 0
            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.ENABLED, [enabled_set]))

            hci_event_asserts.assert_event_occurs(
                lambda packet: b'Im_A_Cert' in packet.payload)

            # Disable Advertising
            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.DISABLED, [enabled_set]))

            # Disable Scanning
            self.send_dut_hci_command(
                hci_packets.LeSetExtendedScanEnableBuilder(
                    hci_packets.Enable.ENABLED,
                    hci_packets.FilterDuplicates.DISABLED, 0, 0))
    def test_scan_filter_device_name_legacy_pdu(self):
        # Use public address on cert side
        logging.info("Setting public address")
        DEVICE_NAME = 'Im_The_CERT!'
        public_address = self.set_cert_privacy_policy_with_public_address()
        logging.info("Set public address")

        # Setup cert side to advertise
        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(DEVICE_NAME, encoding='utf8'))
        gap_data = le_advertising_facade.GapDataMsg(
            data=bytes(gap_name.Serialize()))
        config = le_advertising_facade.AdvertisingConfig(
            advertisement=[gap_data],
            interval_min=512,
            interval_max=768,
            advertising_type=le_advertising_facade.AdvertisingEventType.
            ADV_IND,
            own_address_type=common.USE_PUBLIC_DEVICE_ADDRESS,
            channel_map=7,
            filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
            ALL_DEVICES,
            tx_power=20)
        request = le_advertising_facade.CreateAdvertiserRequest(config=config)
        logging.info("Creating advertiser")
        create_response = self.cert.hci_le_advertising_manager.CreateAdvertiser(
            request)
        logging.info("Created advertiser")

        # Setup SL4A DUT side to scan
        logging.info("Start scanning with public address %s" % public_address)
        self.dut.droid.bleSetScanSettingsScanMode(
            ble_scan_settings_modes['low_latency'])
        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
            self.dut.droid)
        expected_event_name = scan_result.format(scan_callback)

        # Setup SL4A DUT filter
        self.dut.droid.bleSetScanFilterDeviceName(DEVICE_NAME)
        self.dut.droid.bleBuildScanFilter(filter_list)

        # Start scanning on SL4A DUT side
        self.dut.droid.bleStartBleScan(filter_list, scan_settings,
                                       scan_callback)
        logging.info("Started scanning")
        try:
            # Verify if there is scan result
            event_info = self.dut.ed.pop_event(expected_event_name,
                                               self.default_timeout)
        except queue.Empty as error:
            self.log.error("Could not find initial advertisement.")
            return False
        # Print out scan result
        mac_address = event_info['data']['Result']['deviceInfo']['address']
        self.log.info(
            "Filter advertisement with address {}".format(mac_address))

        # Stop scanning
        logging.info("Stop scanning")
        self.dut.droid.bleStopBleScan(scan_callback)
        logging.info("Stopped scanning")

        # Stop advertising
        logging.info("Stop advertising")
        remove_request = le_advertising_facade.RemoveAdvertiserRequest(
            advertiser_id=create_response.advertiser_id)
        self.cert.hci_le_advertising_manager.RemoveAdvertiser(remove_request)
        logging.info("Stopped advertising")

        return True
Example #12
0
    def test_le_connection_dut_advertises(self):
        self.dut_hci.register_for_le_events(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)
        # Cert Connects
        self.send_hal_hci_command(
            hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'))
        phy_scan_params = DirectHciTest._create_phy_scan_params()
        self.send_hal_hci_command(
            hci_packets.LeExtendedCreateConnectionBuilder(
                hci_packets.InitiatorFilterPolicy.USE_PEER_ADDRESS,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                '0D:05:04:03:02:01', 1, [phy_scan_params]))

        # DUT Advertises
        advertising_handle = 0
        self.dut_hci.send_command_with_complete(
            hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                advertising_handle,
                hci_packets.LegacyAdvertisingProperties.ADV_IND,
                400,
                450,
                7,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.PeerAddressType.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                '00:00:00:00:00:00',
                hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                0xF8,
                1,  #SID
                hci_packets.Enable.DISABLED  # Scan request notification
            ))

        self.dut_hci.send_command_with_complete(
            hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                advertising_handle, '0D:05:04:03:02:01'))

        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(b'Im_The_DUT!'))  # TODO: Fix and remove !

        self.dut_hci.send_command_with_complete(
            hci_packets.LeSetExtendedAdvertisingDataBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_name]))

        gap_short_name = hci_packets.GapData()
        gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME
        gap_short_name.data = list(bytes(b'Im_The_D'))

        self.dut_hci.send_command_with_complete(
            hci_packets.LeSetExtendedAdvertisingScanResponseBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_short_name]))

        enabled_set = hci_packets.EnabledSet()
        enabled_set.advertising_handle = advertising_handle
        enabled_set.duration = 0
        enabled_set.max_extended_advertising_events = 0
        self.dut_hci.send_command_with_complete(
            hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                hci_packets.Enable.ENABLED, [enabled_set]))

        # Check for success of Enable
        assertThat(self.dut_hci.get_event_stream()).emits(
            HciMatchers.CommandComplete(
                hci_packets.OpCode.LE_SET_EXTENDED_ADVERTISING_ENABLE))

        (dut_handle, cert_handle) = self._verify_le_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: logging.debug(
                packet.payload) or b'SomeAclData' in packet.payload)
        assertThat(self.dut_hci.get_raw_acl_stream()).emits(
            lambda packet: logging.debug(
                packet.data) or b'SomeMoreAclData' in packet.data)
    def test_le_ad_scan_cert_advertises(self):
        self.register_for_le_event(
            hci_packets.SubeventCode.EXTENDED_ADVERTISING_REPORT)
        self.register_for_le_event(hci_packets.SubeventCode.ADVERTISING_REPORT)
        with EventStream(self.dut.hci.FetchLeSubevents(
                empty_proto.Empty())) as hci_le_event_stream:

            # DUT Scans
            self.enqueue_hci_command(
                hci_packets.LeSetRandomAddressBuilder('0D:05:04:03:02:01'),
                True)
            phy_scan_params = hci_packets.PhyScanParameters()
            phy_scan_params.le_scan_interval = 6553
            phy_scan_params.le_scan_window = 6553
            phy_scan_params.le_scan_type = hci_packets.LeScanType.ACTIVE

            self.enqueue_hci_command(
                hci_packets.LeSetExtendedScanParametersBuilder(
                    hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.LeSetScanningFilterPolicy.ACCEPT_ALL, 1,
                    [phy_scan_params]), True)
            self.enqueue_hci_command(
                hci_packets.LeSetExtendedScanEnableBuilder(
                    hci_packets.Enable.ENABLED,
                    hci_packets.FilterDuplicates.DISABLED, 0, 0), True)

            # CERT Advertises
            advertising_handle = 0
            self.send_hal_hci_command(
                hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                    advertising_handle,
                    hci_packets.LegacyAdvertisingProperties.ADV_IND,
                    512,
                    768,
                    7,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.PeerAddressType.
                    PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                    'A6:A5:A4:A3:A2:A1',
                    hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                    0xF7,
                    1,  # SID
                    hci_packets.Enable.DISABLED  # Scan request notification
                ))

            self.send_hal_hci_command(
                hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                    advertising_handle, '0C:05:04:03:02:01'))
            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_A_Cert!'))  # TODO: Fix and remove !

            self.send_hal_hci_command(
                hci_packets.LeSetExtendedAdvertisingDataBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_name]))

            gap_short_name = hci_packets.GapData()
            gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME
            gap_short_name.data = list(bytes(b'Im_A_C'))

            self.send_hal_hci_command(
                hci_packets.LeSetExtendedAdvertisingScanResponseBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_short_name]))

            enabled_set = hci_packets.EnabledSet()
            enabled_set.advertising_handle = 0
            enabled_set.duration = 0
            enabled_set.max_extended_advertising_events = 0
            self.send_hal_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.ENABLED, [enabled_set]))

            hci_le_event_stream.assert_event_occurs(
                lambda packet: b'Im_A_Cert' in packet.event)

            self.send_hal_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.DISABLED, [enabled_set]))
            self.enqueue_hci_command(
                hci_packets.LeSetExtendedScanEnableBuilder(
                    hci_packets.Enable.DISABLED,
                    hci_packets.FilterDuplicates.DISABLED, 0, 0), True)
    def dut_connects(self, check_address):
        self.register_for_le_event(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)
        self.register_for_le_event(
            hci_packets.SubeventCode.ENHANCED_CONNECTION_COMPLETE)

        # Cert Advertises
        advertising_handle = 0
        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                advertising_handle,
                hci_packets.LegacyAdvertisingProperties.ADV_IND,
                400,
                450,
                7,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.PeerAddressType.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                '00:00:00:00:00:00',
                hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                0xF8,
                1,  #SID
                hci_packets.Enable.DISABLED  # Scan request notification
            ))

        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                advertising_handle, '0C:05:04:03:02:01'))

        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(b'Im_A_Cert'))

        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingDataBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_name]))

        gap_short_name = hci_packets.GapData()
        gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME
        gap_short_name.data = list(bytes(b'Im_A_C'))

        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingScanResponseBuilder(
                advertising_handle,
                hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                [gap_short_name]))

        enabled_set = hci_packets.EnabledSet()
        enabled_set.advertising_handle = advertising_handle
        enabled_set.duration = 0
        enabled_set.max_extended_advertising_events = 0
        self.enqueue_hci_command(
            hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                hci_packets.Enable.ENABLED, [enabled_set]))

        self.dut_le_acl = self.dut_le_acl_manager.connect_to_remote(
            remote_addr=common.BluetoothAddressWithType(
                address=common.BluetoothAddress(
                    address=bytes('0C:05:04:03:02:01', 'utf8')),
                type=int(hci_packets.AddressType.RANDOM_DEVICE_ADDRESS)))

        # Cert gets ConnectionComplete with a handle and sends ACL data
        handle = 0xfff
        address = hci_packets.Address()

        def get_handle(packet):
            packet_bytes = packet.payload
            nonlocal handle
            nonlocal address
            if b'\x3e\x13\x01\x00' in packet_bytes:
                cc_view = hci_packets.LeConnectionCompleteView(
                    hci_packets.LeMetaEventView(
                        hci_packets.EventView(
                            bt_packets.PacketViewLittleEndian(
                                list(packet_bytes)))))
                handle = cc_view.GetConnectionHandle()
                address = cc_view.GetPeerAddress()
                return True
            if b'\x3e\x13\x0A\x00' in packet_bytes:
                cc_view = hci_packets.LeEnhancedConnectionCompleteView(
                    hci_packets.LeMetaEventView(
                        hci_packets.EventView(
                            bt_packets.PacketViewLittleEndian(
                                list(packet_bytes)))))
                handle = cc_view.GetConnectionHandle()
                address = cc_view.GetPeerResolvablePrivateAddress()
                return True
            return False

        self.cert_hci_le_event_stream.assert_event_occurs(get_handle)
        self.cert_handle = handle
        dut_address_from_complete = address
        if check_address:
            assertThat(dut_address_from_complete).isEqualTo(
                self.dut_address.decode())
    def test_cert_connects(self):
        self.set_privacy_policy_static()
        self.register_for_le_event(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)

        self.dut_le_acl_manager.listen_for_incoming_connections()

        # DUT Advertises
        gap_name = hci_packets.GapData()
        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
        gap_name.data = list(bytes(b'Im_The_DUT'))
        gap_data = le_advertising_facade.GapDataMsg(
            data=bytes(gap_name.Serialize()))
        config = le_advertising_facade.AdvertisingConfig(
            advertisement=[gap_data],
            interval_min=512,
            interval_max=768,
            advertising_type=le_advertising_facade.AdvertisingEventType.
            ADV_IND,
            own_address_type=common.USE_RANDOM_DEVICE_ADDRESS,
            peer_address_type=common.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
            peer_address=common.BluetoothAddress(
                address=bytes(b'A6:A5:A4:A3:A2:A1')),
            channel_map=7,
            filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
            ALL_DEVICES)
        request = le_advertising_facade.CreateAdvertiserRequest(config=config)

        self.dut.hci_le_advertising_manager.CreateAdvertiser(request)

        # Cert Connects
        self.enqueue_hci_command(
            hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'))
        phy_scan_params = hci_packets.LeCreateConnPhyScanParameters()
        phy_scan_params.scan_interval = 0x60
        phy_scan_params.scan_window = 0x30
        phy_scan_params.conn_interval_min = 0x18
        phy_scan_params.conn_interval_max = 0x28
        phy_scan_params.conn_latency = 0
        phy_scan_params.supervision_timeout = 0x1f4
        phy_scan_params.min_ce_length = 0
        phy_scan_params.max_ce_length = 0
        self.enqueue_hci_command(
            hci_packets.LeExtendedCreateConnectionBuilder(
                hci_packets.InitiatorFilterPolicy.USE_PEER_ADDRESS,
                hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                self.dut_address.decode(), 1, [phy_scan_params]))

        # Cert gets ConnectionComplete with a handle and sends ACL data
        handle = 0xfff

        def get_handle(packet):
            packet_bytes = packet.payload
            nonlocal handle
            if b'\x3e\x13\x01\x00' in packet_bytes:
                cc_view = hci_packets.LeConnectionCompleteView(
                    hci_packets.LeMetaEventView(
                        hci_packets.EventView(
                            bt_packets.PacketViewLittleEndian(
                                list(packet_bytes)))))
                handle = cc_view.GetConnectionHandle()
                return True
            if b'\x3e\x13\x0A\x00' in packet_bytes:
                cc_view = hci_packets.LeEnhancedConnectionCompleteView(
                    hci_packets.LeMetaEventView(
                        hci_packets.EventView(
                            bt_packets.PacketViewLittleEndian(
                                list(packet_bytes)))))
                handle = cc_view.GetConnectionHandle()
                return True
            return False

        self.cert_hci_le_event_stream.assert_event_occurs(get_handle)
        self.cert_handle = handle

        self.enqueue_acl_data(
            self.cert_handle,
            hci_packets.PacketBoundaryFlag.FIRST_NON_AUTOMATICALLY_FLUSHABLE,
            hci_packets.BroadcastFlag.POINT_TO_POINT,
            bytes(b'\x19\x00\x07\x00SomeAclData from the Cert'))

        # DUT gets a connection complete event and sends and receives
        handle = 0xfff
        self.dut_le_acl = self.dut_le_acl_manager.complete_incoming_connection(
        )

        self.send_receive_and_check()
Example #16
0
    def test_dut_connects(self):
        self.register_for_le_event(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)
        with EventCallbackStream(self.cert_device.hci.FetchLeSubevents(empty_proto.Empty())) as cert_hci_le_event_stream, \
            EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \
            EventCallbackStream(self.device_under_test.hci_le_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream:

            cert_hci_le_event_asserts = EventAsserts(cert_hci_le_event_stream)
            acl_data_asserts = EventAsserts(acl_data_stream)
            cert_acl_data_asserts = EventAsserts(cert_acl_data_stream)

            # Cert Advertises
            advertising_handle = 0
            self.enqueue_hci_command(
                hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                    advertising_handle,
                    hci_packets.LegacyAdvertisingProperties.ADV_IND,
                    400,
                    450,
                    7,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.PeerAddressType.
                    PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                    '00:00:00:00:00:00',
                    hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                    0xF8,
                    1,  #SID
                    hci_packets.Enable.DISABLED  # Scan request notification
                ),
                True)

            self.enqueue_hci_command(
                hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                    advertising_handle, '0C:05:04:03:02:01'), True)

            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_A_Cert'))

            self.enqueue_hci_command(
                hci_packets.LeSetExtendedAdvertisingDataBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_name]), True)

            gap_short_name = hci_packets.GapData()
            gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME
            gap_short_name.data = list(bytes(b'Im_A_C'))

            self.enqueue_hci_command(
                hci_packets.LeSetExtendedAdvertisingScanResponseBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_short_name]), True)

            enabled_set = hci_packets.EnabledSet()
            enabled_set.advertising_handle = advertising_handle
            enabled_set.duration = 0
            enabled_set.max_extended_advertising_events = 0
            self.enqueue_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.ENABLED, [enabled_set]), True)

            with EventCallbackStream(
                    self.device_under_test.hci_le_acl_manager.CreateConnection(
                        le_acl_manager_facade.LeConnectionMsg(
                            address_type=int(
                                hci_packets.AddressType.RANDOM_DEVICE_ADDRESS),
                            address=bytes(
                                '0C:05:04:03:02:01',
                                'utf8')))) as connection_event_stream:

                connection_event_asserts = EventAsserts(
                    connection_event_stream)

                # Cert gets ConnectionComplete with a handle and sends ACL data
                handle = 0xfff

                def get_handle(packet):
                    packet_bytes = packet.event
                    nonlocal handle
                    if b'\x3e\x13\x01\x00' in packet_bytes:
                        cc_view = hci_packets.LeConnectionCompleteView(
                            hci_packets.LeMetaEventView(
                                hci_packets.EventPacketView(
                                    bt_packets.PacketViewLittleEndian(
                                        list(packet_bytes)))))
                        handle = cc_view.GetConnectionHandle()
                        return True
                    if b'\x3e\x13\x0A\x00' in packet_bytes:
                        cc_view = hci_packets.LeEnhancedConnectionCompleteView(
                            hci_packets.LeMetaEventView(
                                hci_packets.EventPacketView(
                                    bt_packets.PacketViewLittleEndian(
                                        list(packet_bytes)))))
                        handle = cc_view.GetConnectionHandle()
                        return True
                    return False

                cert_hci_le_event_asserts.assert_event_occurs(get_handle)
                cert_handle = handle

                self.enqueue_acl_data(
                    cert_handle, hci_packets.PacketBoundaryFlag.
                    FIRST_AUTOMATICALLY_FLUSHABLE,
                    hci_packets.BroadcastFlag.POINT_TO_POINT,
                    bytes(b'\x19\x00\x07\x00SomeAclData from the Cert'))

                # DUT gets a connection complete event and sends and receives
                handle = 0xfff
                connection_event_asserts.assert_event_occurs(get_handle)

                self.device_under_test.hci_le_acl_manager.SendAclData(
                    le_acl_manager_facade.LeAclData(
                        handle=handle,
                        payload=bytes(
                            b'\x1C\x00\x07\x00SomeMoreAclData from the DUT')))

                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)
Example #17
0
    def test_le_connection_dut_advertises(self):
        with EventCallbackStream(self.device_under_test.hal.FetchHciEvent(empty_pb2.Empty())) as hci_event_stream, \
                EventCallbackStream(self.cert_device.hal.FetchHciEvent(empty_pb2.Empty())) as cert_hci_event_stream, \
                EventCallbackStream(self.device_under_test.hal.FetchHciAcl(empty_pb2.Empty())) as acl_data_stream, \
                EventCallbackStream(self.cert_device.hal.FetchHciAcl(empty_pb2.Empty())) as cert_acl_data_stream:

            hci_event_asserts = EventAsserts(hci_event_stream)
            cert_hci_event_asserts = EventAsserts(cert_hci_event_stream)
            acl_data_asserts = EventAsserts(acl_data_stream)
            cert_acl_data_asserts = EventAsserts(cert_acl_data_stream)

            # Cert Connects
            self.send_cert_hci_command(
                hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'))
            phy_scan_params = hci_packets.LeCreateConnPhyScanParameters()
            phy_scan_params.scan_interval = 0x60
            phy_scan_params.scan_window = 0x30
            phy_scan_params.conn_interval_min = 0x18
            phy_scan_params.conn_interval_max = 0x28
            phy_scan_params.conn_latency = 0
            phy_scan_params.supervision_timeout = 0x1f4
            phy_scan_params.min_ce_length = 0
            phy_scan_params.max_ce_length = 0
            self.send_cert_hci_command(
                hci_packets.LeExtendedCreateConnectionBuilder(
                    hci_packets.InitiatorFilterPolicy.USE_PEER_ADDRESS,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                    '0D:05:04:03:02:01', 1, [phy_scan_params]))

            # DUT Advertises
            advertising_handle = 0
            self.send_dut_hci_command(
                hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                    advertising_handle,
                    hci_packets.LegacyAdvertisingProperties.ADV_IND,
                    400,
                    450,
                    7,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.PeerAddressType.
                    PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                    '00:00:00:00:00:00',
                    hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                    0xF8,
                    1,  #SID
                    hci_packets.Enable.DISABLED  # Scan request notification
                ))

            self.send_dut_hci_command(
                hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                    advertising_handle, '0D:05:04:03:02:01'))

            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(
                bytes(b'Im_The_DUT!'))  # TODO: Fix and remove !

            self.send_dut_hci_command(
                hci_packets.LeSetExtendedAdvertisingDataBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_name]))

            gap_short_name = hci_packets.GapData()
            gap_short_name.data_type = hci_packets.GapDataType.SHORTENED_LOCAL_NAME
            gap_short_name.data = list(bytes(b'Im_The_D'))

            self.send_dut_hci_command(
                hci_packets.LeSetExtendedAdvertisingScanResponseBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_short_name]))

            enabled_set = hci_packets.EnabledSet()
            enabled_set.advertising_handle = advertising_handle
            enabled_set.duration = 0
            enabled_set.max_extended_advertising_events = 0
            self.send_dut_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.ENABLED, [enabled_set]))

            conn_handle = 0xfff

            def payload_handle(packet):
                packet_bytes = packet.payload
                if b'\x3e\x13\x01\x00' in packet_bytes:
                    nonlocal conn_handle
                    cc_view = hci_packets.LeConnectionCompleteView(
                        hci_packets.LeMetaEventView(
                            hci_packets.EventPacketView(
                                bt_packets.PacketViewLittleEndian(
                                    list(packet_bytes)))))
                    conn_handle = cc_view.GetConnectionHandle()
                    return True
                return False

            cert_hci_event_asserts.assert_event_occurs(payload_handle)
            cert_handle = conn_handle
            conn_handle = 0xfff
            hci_event_asserts.assert_event_occurs(payload_handle)
            dut_handle = conn_handle

            # Send ACL Data
            self.send_dut_acl_data(
                dut_handle, hci_packets.PacketBoundaryFlag.
                FIRST_NON_AUTOMATICALLY_FLUSHABLE,
                hci_packets.BroadcastFlag.POINT_TO_POINT,
                bytes(b'Just SomeAclData'))
            self.send_cert_acl_data(
                cert_handle, hci_packets.PacketBoundaryFlag.
                FIRST_NON_AUTOMATICALLY_FLUSHABLE,
                hci_packets.BroadcastFlag.POINT_TO_POINT,
                bytes(b'Just SomeMoreAclData'))

            cert_acl_data_asserts.assert_event_occurs(
                lambda packet: b'SomeAclData' in packet.payload)
            acl_data_asserts.assert_event_occurs(
                lambda packet: b'SomeMoreAclData' in packet.payload)
Example #18
0
    def test_le_white_list_connection_cert_advertises(self):
        with EventCallbackStream(self.device_under_test.hal.FetchHciEvent(empty_pb2.Empty())) as hci_event_stream, \
            EventCallbackStream(self.cert_device.hal.FetchHciEvent(empty_pb2.Empty())) as cert_hci_event_stream:
            hci_event_asserts = EventAsserts(hci_event_stream)
            cert_hci_event_asserts = EventAsserts(cert_hci_event_stream)

            # DUT Connects
            self.send_dut_hci_command(
                hci_packets.LeSetRandomAddressBuilder('0D:05:04:03:02:01'))
            self.send_dut_hci_command(
                hci_packets.LeAddDeviceToWhiteListBuilder(
                    hci_packets.WhiteListAddressType.RANDOM,
                    '0C:05:04:03:02:01'))
            phy_scan_params = hci_packets.LeCreateConnPhyScanParameters()
            phy_scan_params.scan_interval = 0x60
            phy_scan_params.scan_window = 0x30
            phy_scan_params.conn_interval_min = 0x18
            phy_scan_params.conn_interval_max = 0x28
            phy_scan_params.conn_latency = 0
            phy_scan_params.supervision_timeout = 0x1f4
            phy_scan_params.min_ce_length = 0
            phy_scan_params.max_ce_length = 0
            self.send_dut_hci_command(
                hci_packets.LeExtendedCreateConnectionBuilder(
                    hci_packets.InitiatorFilterPolicy.USE_WHITE_LIST,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                    'BA:D5:A4:A3:A2:A1', 1, [phy_scan_params]))

            # CERT Advertises
            advertising_handle = 1
            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingLegacyParametersBuilder(
                    advertising_handle,
                    hci_packets.LegacyAdvertisingProperties.ADV_IND,
                    512,
                    768,
                    7,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.PeerAddressType.
                    PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                    'A6:A5:A4:A3:A2:A1',
                    hci_packets.AdvertisingFilterPolicy.ALL_DEVICES,
                    0x7F,
                    0,  # SID
                    hci_packets.Enable.DISABLED  # Scan request notification
                ))

            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingRandomAddressBuilder(
                    advertising_handle, '0C:05:04:03:02:01'))

            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(
                bytes(b'Im_A_Cert!'))  # TODO: Fix and remove !

            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingDataBuilder(
                    advertising_handle,
                    hci_packets.Operation.COMPLETE_ADVERTISEMENT,
                    hci_packets.FragmentPreference.CONTROLLER_SHOULD_NOT,
                    [gap_name]))
            enabled_set = hci_packets.EnabledSet()
            enabled_set.advertising_handle = 1
            enabled_set.duration = 0
            enabled_set.max_extended_advertising_events = 0
            self.send_cert_hci_command(
                hci_packets.LeSetExtendedAdvertisingEnableBuilder(
                    hci_packets.Enable.ENABLED, [enabled_set]))

            # LeConnectionComplete
            cert_hci_event_asserts.assert_event_occurs(
                lambda packet: b'\x3e\x13\x01\x00' in packet.payload,
                timeout=timedelta(seconds=20))
            hci_event_asserts.assert_event_occurs(
                lambda packet: b'\x3e\x13\x01\x00' in packet.payload,
                timeout=timedelta(seconds=20))
Example #19
0
    def test_cert_connects(self):
        self.register_for_le_event(
            hci_packets.SubeventCode.CONNECTION_COMPLETE)
        with EventCallbackStream(self.cert_device.hci.FetchLeSubevents(empty_proto.Empty())) as cert_hci_le_event_stream, \
                EventCallbackStream(self.cert_device.hci.FetchAclPackets(empty_proto.Empty())) as cert_acl_data_stream, \
                EventCallbackStream(self.device_under_test.hci_le_acl_manager.FetchIncomingConnection(empty_proto.Empty())) as incoming_connection_stream, \
                EventCallbackStream(self.device_under_test.hci_le_acl_manager.FetchAclData(empty_proto.Empty())) as acl_data_stream:

            cert_hci_le_event_asserts = EventAsserts(cert_hci_le_event_stream)
            incoming_connection_asserts = EventAsserts(
                incoming_connection_stream)
            acl_data_asserts = EventAsserts(acl_data_stream)
            cert_acl_data_asserts = EventAsserts(cert_acl_data_stream)

            # DUT Advertises
            gap_name = hci_packets.GapData()
            gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
            gap_name.data = list(bytes(b'Im_The_DUT'))
            gap_data = le_advertising_facade.GapDataMsg(
                data=bytes(gap_name.Serialize()))
            config = le_advertising_facade.AdvertisingConfig(
                advertisement=[gap_data],
                random_address=common.BluetoothAddress(
                    address=bytes(b'0D:05:04:03:02:01')),
                interval_min=512,
                interval_max=768,
                event_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
                address_type=common.RANDOM_DEVICE_ADDRESS,
                peer_address_type=common.PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
                peer_address=common.BluetoothAddress(
                    address=bytes(b'A6:A5:A4:A3:A2:A1')),
                channel_map=7,
                filter_policy=le_advertising_facade.AdvertisingFilterPolicy.
                ALL_DEVICES)
            request = le_advertising_facade.CreateAdvertiserRequest(
                config=config)

            create_response = self.device_under_test.hci_le_advertising_manager.CreateAdvertiser(
                request)

            # Cert Connects
            self.enqueue_hci_command(
                hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'),
                True)
            phy_scan_params = hci_packets.LeCreateConnPhyScanParameters()
            phy_scan_params.scan_interval = 0x60
            phy_scan_params.scan_window = 0x30
            phy_scan_params.conn_interval_min = 0x18
            phy_scan_params.conn_interval_max = 0x28
            phy_scan_params.conn_latency = 0
            phy_scan_params.supervision_timeout = 0x1f4
            phy_scan_params.min_ce_length = 0
            phy_scan_params.max_ce_length = 0
            self.enqueue_hci_command(
                hci_packets.LeExtendedCreateConnectionBuilder(
                    hci_packets.InitiatorFilterPolicy.USE_PEER_ADDRESS,
                    hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
                    hci_packets.AddressType.RANDOM_DEVICE_ADDRESS,
                    '0D:05:04:03:02:01', 1, [phy_scan_params]), False)

            # Cert gets ConnectionComplete with a handle and sends ACL data
            handle = 0xfff

            def get_handle(packet):
                packet_bytes = packet.event
                nonlocal handle
                if b'\x3e\x13\x01\x00' in packet_bytes:
                    cc_view = hci_packets.LeConnectionCompleteView(
                        hci_packets.LeMetaEventView(
                            hci_packets.EventPacketView(
                                bt_packets.PacketViewLittleEndian(
                                    list(packet_bytes)))))
                    handle = cc_view.GetConnectionHandle()
                    return True
                if b'\x3e\x13\x0A\x00' in packet_bytes:
                    cc_view = hci_packets.LeEnhancedConnectionCompleteView(
                        hci_packets.LeMetaEventView(
                            hci_packets.EventPacketView(
                                bt_packets.PacketViewLittleEndian(
                                    list(packet_bytes)))))
                    handle = cc_view.GetConnectionHandle()
                    return True
                return False

            cert_hci_le_event_asserts.assert_event_occurs(get_handle)
            cert_handle = handle

            self.enqueue_acl_data(
                cert_handle,
                hci_packets.PacketBoundaryFlag.FIRST_AUTOMATICALLY_FLUSHABLE,
                hci_packets.BroadcastFlag.POINT_TO_POINT,
                bytes(b'\x19\x00\x07\x00SomeAclData from the Cert'))

            # DUT gets a connection complete event and sends and receives
            handle = 0xfff
            incoming_connection_asserts.assert_event_occurs(get_handle)

            self.device_under_test.hci_le_acl_manager.SendAclData(
                le_acl_manager_facade.LeAclData(
                    handle=handle,
                    payload=bytes(
                        b'\x1C\x00\x07\x00SomeMoreAclData from the DUT')))

            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)