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