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
23
24from acts.keys import Config
25from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
26from acts_contrib.test_utils.bt.bt_test_utils import pair_pri_to_sec
27from acts_contrib.test_utils.tel.tel_phone_setup_utils import ensure_phones_default_state
28from acts_contrib.test_utils.tel.tel_test_utils import get_phone_number
29from acts_contrib.test_utils.tel.tel_test_utils import setup_droid_properties
30
31
32class BluetoothCarHfpBaseTest(BluetoothBaseTest):
33    DEFAULT_TIMEOUT = 15
34    ag_phone_number = ""
35    re_phone_number = ""
36
37    def __init__(self, controllers):
38        BluetoothBaseTest.__init__(self, controllers)
39        # HF : HandsFree (CarKit role)
40        self.hf = self.android_devices[0]
41        self.hf.log.info("Role set to HF (HandsFree Carkit role).")
42        # AG : Audio Gateway (Phone role)
43        self.ag = self.android_devices[1]
44        self.ag.log.info("Role set to AG (Audio Gateway Phone role).")
45        # RE : Remote Device (Phone being talked to role)
46        if len(self.android_devices) > 2:
47            self.re = self.android_devices[2]
48            self.re.log.info("Role set to RE (Remote device).")
49        else:
50            self.re = None
51        if len(self.android_devices) > 3:
52            self.re2 = self.android_devices[3]
53            self.re2.log.info("Role set to RE2 (Remote device 2).")
54        else:
55            self.re2 = None
56
57    def setup_class(self):
58        super(BluetoothCarHfpBaseTest, self).setup_class()
59        if not "sim_conf_file" in self.user_params.keys():
60            self.log.error("Missing mandatory user config \"sim_conf_file\"!")
61            return False
62        sim_conf_file = self.user_params["sim_conf_file"][0]
63        if not os.path.isfile(sim_conf_file):
64            sim_conf_file = os.path.join(
65                self.user_params[Config.key_config_path.value], sim_conf_file)
66            if not os.path.isfile(sim_conf_file):
67                self.log.error("Unable to load user config " + sim_conf_file +
68                               " from test config file.")
69                return False
70        setup_droid_properties(self.log, self.ag, sim_conf_file)
71        self.ag_phone_number = get_phone_number(self.log, self.ag)
72        self.ag.log.info("ag tel: {}".format(self.ag_phone_number))
73        if self.re:
74            setup_droid_properties(self.log, self.re, sim_conf_file)
75            self.re_phone_number = get_phone_number(self.log, self.re)
76            self.re.log.info("re tel: {}".format(self.re_phone_number))
77        if self.re2:
78            setup_droid_properties(self.log, self.re2, sim_conf_file)
79            self.re2_phone_number = get_phone_number(self.log, self.re2)
80            self.re2.log.info("re2 tel: {}".format(self.re2_phone_number))
81        # Pair and connect the devices.
82        # Grace time inbetween stack state changes
83        time.sleep(5)
84        if not pair_pri_to_sec(
85                self.hf, self.ag, attempts=4, auto_confirm=False):
86            self.log.error("Failed to pair")
87            return False
88        return True
89
90    def setup_test(self):
91        if not super(BluetoothCarHfpBaseTest, self).setup_test():
92            return False
93        return ensure_phones_default_state(self.log, self.android_devices[1:])
94
95    def teardown_test(self):
96        if not super(BluetoothCarHfpBaseTest, self).teardown_test():
97            return False
98        return ensure_phones_default_state(self.log, self.android_devices[1:])
99
100    def on_fail(self, test_name, begin_time):
101        result = True
102        if not super(BluetoothCarHfpBaseTest, self).on_fail(
103                test_name, begin_time):
104            result = False
105        ensure_phones_default_state(self.log, self.android_devices[1:])
106        return result
107