1#!/usr/bin/env python3
2#
3#   Copyright 2019 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import os
18import sys
19import logging
20
21from cert.gd_base_test import GdBaseTestClass
22from cert.event_stream import EventStream
23from google.protobuf import empty_pb2 as empty_proto
24from facade import rootservice_pb2 as facade_rootservice
25from hci.facade import hci_facade_pb2 as hci_facade
26from hci.facade import \
27  le_advertising_manager_facade_pb2 as le_advertising_facade
28from bluetooth_packets_python3 import hci_packets
29from facade import common_pb2 as common
30from cert.py_hci import PyHci
31from cert.truth import assertThat
32
33
34class LeAdvertisingManagerTest(GdBaseTestClass):
35
36    def setup_class(self):
37        super().setup_class(dut_module='HCI_INTERFACES', cert_module='HCI')
38
39    def setup_test(self):
40        super().setup_test()
41        self.cert_hci = PyHci(self.cert, acl_streaming=True)
42
43    def teardown_test(self):
44        self.cert_hci.close()
45        super().teardown_test()
46
47    def test_le_ad_scan_dut_advertises(self):
48        self.cert_hci.register_for_le_events(hci_packets.SubeventCode.ADVERTISING_REPORT,
49                                             hci_packets.SubeventCode.EXTENDED_ADVERTISING_REPORT)
50
51        # CERT Scans
52        self.cert_hci.send_command(hci_packets.LeSetRandomAddressBuilder('0C:05:04:03:02:01'))
53        scan_parameters = hci_packets.PhyScanParameters()
54        scan_parameters.le_scan_type = hci_packets.LeScanType.ACTIVE
55        scan_parameters.le_scan_interval = 40
56        scan_parameters.le_scan_window = 20
57        self.cert_hci.send_command(
58            hci_packets.LeSetExtendedScanParametersBuilder(hci_packets.OwnAddressType.RANDOM_DEVICE_ADDRESS,
59                                                           hci_packets.LeScanningFilterPolicy.ACCEPT_ALL, 1,
60                                                           [scan_parameters]))
61        self.cert_hci.send_command(
62            hci_packets.LeSetExtendedScanEnableBuilder(hci_packets.Enable.ENABLED,
63                                                       hci_packets.FilterDuplicates.DISABLED, 0, 0))
64
65        # DUT Advertises
66        gap_name = hci_packets.GapData()
67        gap_name.data_type = hci_packets.GapDataType.COMPLETE_LOCAL_NAME
68        gap_name.data = list(bytes(b'Im_The_DUT'))
69        gap_data = le_advertising_facade.GapDataMsg(data=bytes(gap_name.Serialize()))
70        config = le_advertising_facade.AdvertisingConfig(
71            advertisement=[gap_data],
72            interval_min=512,
73            interval_max=768,
74            advertising_type=le_advertising_facade.AdvertisingEventType.ADV_IND,
75            own_address_type=common.USE_RANDOM_DEVICE_ADDRESS,
76            channel_map=7,
77            filter_policy=le_advertising_facade.AdvertisingFilterPolicy.ALL_DEVICES)
78        request = le_advertising_facade.CreateAdvertiserRequest(config=config)
79
80        create_response = self.dut.hci_le_advertising_manager.CreateAdvertiser(request)
81
82        assertThat(self.cert_hci.get_le_event_stream()).emits(lambda packet: b'Im_The_DUT' in packet.payload)
83
84        remove_request = le_advertising_facade.RemoveAdvertiserRequest(advertiser_id=create_response.advertiser_id)
85        self.dut.hci_le_advertising_manager.RemoveAdvertiser(remove_request)
86        self.cert_hci.send_command(
87            hci_packets.LeSetScanEnableBuilder(hci_packets.Enable.DISABLED, hci_packets.Enable.DISABLED))
88