1#!/usr/bin/env python3
2#
3#   Copyright 2020 - 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 time
18
19from blueberry.tests.gd.cert import gd_base_test
20from blueberry.tests.gd.cert.truth import assertThat
21from google.protobuf import empty_pb2 as empty_proto
22from blueberry.facade.hci import controller_facade_pb2 as controller_facade
23from mobly import test_runner
24import hci_packets as hci
25
26
27class ControllerTest(gd_base_test.GdBaseTestClass):
28
29    def setup_class(self):
30        gd_base_test.GdBaseTestClass.setup_class(self, dut_module='HCI_INTERFACES', cert_module='HCI_INTERFACES')
31
32    def test_get_addresses(self):
33        cert_address = self.cert.hci_controller.GetMacAddressSimple()
34        dut_address = self.dut.hci_controller.GetMacAddressSimple()
35
36        assertThat(cert_address).isNotEqualTo(dut_address)
37        time.sleep(1)  # This shouldn't be needed b/149120542
38
39    def test_write_local_name(self):
40        self.dut.hci_controller.WriteLocalName(controller_facade.NameMsg(name=b'ImTheDUT'))
41        self.cert.hci_controller.WriteLocalName(controller_facade.NameMsg(name=b'ImTheCert'))
42        cert_name = self.cert.hci_controller.GetLocalNameSimple()
43        dut_name = self.dut.hci_controller.GetLocalNameSimple()
44
45        assertThat(dut_name).isEqualTo(b'ImTheDUT')
46        assertThat(cert_name).isEqualTo(b'ImTheCert')
47
48    def test_extended_advertising_support(self):
49        extended_advertising_supported = self.dut.hci_controller.SupportsBleExtendedAdvertising(empty_proto.Empty())
50        if extended_advertising_supported.supported:
51            number_of_sets = self.dut.hci_controller.GetLeNumberOfSupportedAdvertisingSets(empty_proto.Empty())
52            assertThat(number_of_sets.value).isGreaterThan(5)  # Android threshold for CTS
53            supported = self.dut.hci_controller.IsSupportedCommand(
54                controller_facade.OpCodeMsg(op_code=int(hci.OpCode.LE_SET_EXTENDED_ADVERTISING_PARAMETERS)))
55            assertThat(supported.supported).isEqualTo(True)
56
57
58if __name__ == '__main__':
59    test_runner.main()
60