1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17This is base class for tests that exercises different GATT procedures between two connected devices.
18Setup/Teardown methods take care of establishing connection, and doing GATT DB initialization/discovery.
19"""
20
21import os
22import time
23from queue import Empty
24
25from acts.keys import Config
26from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
27from acts.test_utils.bt.bt_test_utils import pair_pri_to_sec
28from acts.test_utils.tel.tel_test_utils import ensure_phones_default_state
29from acts.test_utils.tel.tel_test_utils import get_phone_number
30from acts.test_utils.tel.tel_test_utils import setup_droid_properties
31
32
33class BluetoothCarHfpBaseTest(BluetoothBaseTest):
34    DEFAULT_TIMEOUT = 15
35    ag_phone_number = ""
36    re_phone_number = ""
37
38    def __init__(self, controllers):
39        BluetoothBaseTest.__init__(self, controllers)
40        # HF : HandsFree (CarKit role)
41        self.hf = self.android_devices[0]
42        self.hf.log.info("Role set to HF (HandsFree Carkit role).")
43        # AG : Audio Gateway (Phone role)
44        self.ag = self.android_devices[1]
45        self.ag.log.info("Role set to AG (Audio Gateway Phone role).")
46        # RE : Remote Device (Phone being talked to role)
47        if len(self.android_devices) > 2:
48            self.re = self.android_devices[2]
49            self.re.log.info("Role set to RE (Remote device).")
50        else:
51            self.re = None
52        if len(self.android_devices) > 3:
53            self.re2 = self.android_devices[3]
54            self.re2.log.info("Role set to RE2 (Remote device 2).")
55        else:
56            self.re2 = None
57
58    def setup_class(self):
59        super(BluetoothCarHfpBaseTest, self).setup_class()
60        if not "sim_conf_file" in self.user_params.keys():
61            self.log.error("Missing mandatory user config \"sim_conf_file\"!")
62            return False
63        sim_conf_file = self.user_params["sim_conf_file"][0]
64        if not os.path.isfile(sim_conf_file):
65            sim_conf_file = os.path.join(
66                self.user_params[Config.key_config_path.value], sim_conf_file)
67            if not os.path.isfile(sim_conf_file):
68                self.log.error("Unable to load user config " + sim_conf_file +
69                               " from test config file.")
70                return False
71        setup_droid_properties(self.log, self.ag, sim_conf_file)
72        self.ag_phone_number = get_phone_number(self.log, self.ag)
73        self.ag.log.info("ag tel: {}".format(self.ag_phone_number))
74        if self.re:
75            setup_droid_properties(self.log, self.re, sim_conf_file)
76            self.re_phone_number = get_phone_number(self.log, self.re)
77            self.re.log.info("re tel: {}".format(self.re_phone_number))
78        if self.re2:
79            setup_droid_properties(self.log, self.re2, sim_conf_file)
80            self.re2_phone_number = get_phone_number(self.log, self.re2)
81            self.re2.log.info("re2 tel: {}".format(self.re2_phone_number))
82        # Pair and connect the devices.
83        # Grace time inbetween stack state changes
84        time.sleep(5)
85        if not pair_pri_to_sec(
86                self.hf, self.ag, attempts=4, auto_confirm=False):
87            self.log.error("Failed to pair")
88            return False
89        return True
90
91    def setup_test(self):
92        if not super(BluetoothCarHfpBaseTest, self).setup_test():
93            return False
94        return ensure_phones_default_state(self.log, self.android_devices[1:])
95
96    def teardown_test(self):
97        if not super(BluetoothCarHfpBaseTest, self).teardown_test():
98            return False
99        return ensure_phones_default_state(self.log, self.android_devices[1:])
100
101    def on_fail(self, test_name, begin_time):
102        result = True
103        if not super(BluetoothCarHfpBaseTest, self).on_fail(test_name,
104                                                            begin_time):
105            result = False
106        ensure_phones_default_state(self.log, self.android_devices[1:])
107        return result
108