1# /usr/bin/env python3.4
2#
3# Copyright (C) 2018 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
17from acts.test_utils.bt import BtEnum
18from acts.test_utils.bt.bt_test_utils import clear_bonded_devices
19from acts.test_utils.coex.CoexBaseTest import CoexBaseTest
20from acts.test_utils.coex.coex_test_utils import connect_ble
21from acts.test_utils.coex.coex_test_utils import initiate_disconnect_from_hf
22from acts.test_utils.coex.coex_test_utils import multithread_func
23from acts.test_utils.coex.coex_test_utils import music_play_and_check_via_app
24from acts.test_utils.coex.coex_test_utils import pair_and_connect_headset
25from acts.test_utils.coex.coex_test_utils import setup_tel_config
26
27
28class CoexBtMultiProfileFunctionalityTest(CoexBaseTest):
29
30    def __init__(self, controllers):
31        CoexBaseTest.__init__(self, controllers)
32
33    def setup_class(self):
34        CoexBaseTest.setup_class(self)
35        req_params = ["sim_conf_file", "music_play_time"]
36        self.unpack_userparams(req_params)
37        self.ag_phone_number, self.re_phone_number = setup_tel_config(
38            self.pri_ad, self.sec_ad, self.sim_conf_file)
39
40    def setup_test(self):
41        CoexBaseTest.setup_test(self)
42        self.audio_receiver.pairing_mode()
43        if not pair_and_connect_headset(
44                self.pri_ad, self.audio_receiver.mac_address,
45                set([BtEnum.BluetoothProfile.HEADSET.value]) and
46                set([BtEnum.BluetoothProfile.A2DP.value])):
47            self.log.error("Failed to pair and connect to headset")
48            return False
49
50    def teardown_test(self):
51        clear_bonded_devices(self.pri_ad)
52        CoexBaseTest.teardown_test(self)
53        self.audio_receiver.clean_up()
54
55    def start_media_streaming_initiate_hfp_call_with_iperf(self):
56        """Start media streaming and initiate call from hf to check
57        SCO connection along with iperf.
58
59        Returns:
60            True if successful, False otherwise.
61        """
62        self.run_iperf_and_get_result()
63        if not music_play_and_check_via_app(
64                self.pri_ad, self.audio_receiver.mac_address):
65            self.log.error("Failed to stream music file")
66            return False
67        if not initiate_disconnect_from_hf(
68                self.audio_receiver, self.pri_ad, self.sec_ad,
69                self.iperf["duration"]):
70            self.log.error("Failed to initiate/hung up call")
71            return False
72        return self.teardown_result()
73
74    def ble_with_multiprofile_connection(self):
75        """Wrapper function to check ble connection alongwith a2dp streaming
76        and hfp call connection with iperf.
77        """
78        if not connect_ble(self.pri_ad, self.sec_ad):
79            self.log.error("Failed to connect BLE device")
80            return False
81        if not music_play_and_check_via_app(
82                self.pri_ad, self.audio_receiver.mac_address):
83            self.log.error("Failed to stream music file")
84            return False
85        self.run_iperf_and_get_result()
86        tasks = [(initiate_disconnect_from_hf,
87                  (self.audio_receiver, self.pri_ad, self.sec_ad,
88                   self.iperf["duration"]))]
89        if not multithread_func(self.log, tasks):
90            return False
91        return self.teardown_result()
92
93    def test_a2dp_streaming_hfp_call_with_tcp_ul(self):
94        """Starts TCP-uplink traffic with media streaming and HFP call.
95
96        This test is to start TCP-uplink traffic between host machine and
97        android device and test the functional behaviour of media streaming
98        via A2DP and initiating a call when media streaming is ongoing to
99        check HFP.
100
101        Steps:
102        1. Start TCP-uplink traffic.
103        1. Enable bluetooth.
104        2. Start media streaming to A2DP headset.
105        4. Initiate a call from headset.
106
107        Returns:
108            True if successful, False otherwise.
109
110        Test Id: Bt_CoEx_066
111        """
112        if not self.start_media_streaming_initiate_hfp_call_with_iperf():
113            return False
114        return True
115
116    def test_a2dp_streaming_hfp_call_with_tcp_dl(self):
117        """Starts TCP-downlink traffic with media streaming and HFP call.
118
119        This test is to start TCP-downlink traffic between host machine and
120        android device and test the functional behaviour of media streaming
121        via A2DP and initiating a call when media streaming is ongoing to
122        check HFP.
123
124        Steps:
125        1. Start TCP-downlink traffic.
126        1. Enable bluetooth.
127        2. Start media streaming to A2DP headset.
128        4. Initiate a call from headset.
129
130        Returns:
131            True if successful, False otherwise.
132
133        Test Id: Bt_CoEx_067
134        """
135        if not self.start_media_streaming_initiate_hfp_call_with_iperf():
136            return False
137        return True
138
139    def test_a2dp_streaming_hfp_call_with_udp_ul(self):
140        """Starts UDP-uplink traffic with media streaming and HFP call.
141
142        This test is to start UDP-uplink traffic between host machine and
143        android device and test the functional behaviour of media streaming
144        via A2DP and initiating a call when media streaming is ongoing to
145        check HFP.
146
147        Steps:
148        1. Start UDP-uplink traffic.
149        1. Enable bluetooth.
150        2. Start media streaming to A2DP headset.
151        4. Initiate a call from headset.
152
153        Returns:
154            True if successful, False otherwise.
155
156        Test Id: Bt_CoEx_068
157        """
158        if not self.start_media_streaming_initiate_hfp_call_with_iperf():
159            return False
160        return True
161
162    def test_a2dp_streaming_hfp_call_with_udp_dl(self):
163        """Starts UDP-downlink traffic with media streaming and HFP call.
164
165        This test is to start UDP-uplink traffic between host machine and
166        android device and test the functional behaviour of media streaming
167        via A2DP and initiating a call when media streaming is ongoing to
168        check HFP.
169
170        Steps:
171        1. Start UDP-downlink traffic.
172        1. Enable bluetooth.
173        2. Start media streaming to A2DP headset.
174        4. Initiate a call from headset.
175
176        Returns:
177            True if successful, False otherwise.
178
179        Test Id: Bt_CoEx_069
180        """
181        if not self.start_media_streaming_initiate_hfp_call_with_iperf():
182            return False
183        return True
184
185    def test_ble_connection_a2dp_streaming_hfp_call_with_tcp_ul(self):
186        """Starts TCP-uplink traffic while connecting to BLE device,
187        A2DP streaming and HFP call.
188
189        This test is to start TCP-uplink traffic between host machine and
190        android device and test the functional behaviour of BLE connection,
191        media streaming via A2DP and HFP call connection.
192
193        Steps:
194        1. Enable Bluetooth.
195        2. Connect to BLE device.
196        3. Start media streaming to A2DP headset.
197        4. Start TCP-uplink traffic.
198        5. Initiate HFP call.
199
200        Returns:
201            True if successful, False otherwise.
202
203        Test Id: Bt_CoEx_082
204        """
205        if not self.ble_with_multiprofile_connection():
206            return True
207        return False
208
209    def test_ble_connection_a2dp_streaming_hfp_call_with_tcp_dl(self):
210        """Starts TCP-downlink traffic while connecting to BLE device,
211        A2DP streaming and HFP call.
212
213        This test is to start TCP-downlink traffic between host machine and
214        android device and test the functional behaviour of BLE connection,
215        media streaming via A2DP and HFP call connection.
216
217        Steps:
218        1. Enable Bluetooth.
219        2. Connect to BLE device.
220        3. Start media streaming to A2DP headset.
221        4. Start TCP-uplink traffic.
222        5. Initiate HFP call.
223
224        Returns:
225            True if successful, False otherwise.
226
227        Test Id: Bt_CoEx_083.
228        """
229        if not self.ble_with_multiprofile_connection():
230            return True
231        return False
232