• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2022 - Google
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 acts_contrib.test_utils.bt.bt_test_utils import bluetooth_enabled_check
20from acts_contrib.test_utils.bt.bt_test_utils import disable_bluetooth
21from acts_contrib.test_utils.bt.bt_test_utils import pair_pri_to_sec
22from acts_contrib.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
23from acts_contrib.test_utils.tel.tel_data_utils import test_internet_connection
24from acts_contrib.test_utils.tel.tel_data_utils import wait_for_cell_data_connection
25from acts_contrib.test_utils.tel.tel_phone_setup_utils import ensure_network_generation
26from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection
27from acts_contrib.test_utils.tel.tel_test_utils import wait_for_state
28from acts_contrib.test_utils.tel.tel_voice_utils import call_setup_teardown
29from acts_contrib.test_utils.tel.tel_voice_utils import hangup_call
30
31
32def enable_bluetooth_tethering_connection(log, provider, clients):
33    for ad in [provider] + clients:
34        if not bluetooth_enabled_check(ad):
35            ad.log.info("Bluetooth is not enabled")
36            return False
37        else:
38            ad.log.info("Bluetooth is enabled")
39    time.sleep(5)
40    provider.log.info("Provider enabling bluetooth tethering")
41    try:
42        provider.droid.bluetoothPanSetBluetoothTethering(True)
43    except Exception as e:
44        provider.log.warning(
45            "Failed to enable provider Bluetooth tethering with %s", e)
46        provider.droid.bluetoothPanSetBluetoothTethering(True)
47
48    if wait_for_state(provider.droid.bluetoothPanIsTetheringOn, True):
49        provider.log.info("Provider Bluetooth tethering is enabled.")
50    else:
51        provider.log.error(
52            "Failed to enable provider Bluetooth tethering.")
53        provider.log.error("bluetoothPanIsTetheringOn = %s",
54                           provider.droid.bluetoothPanIsTetheringOn())
55        return False
56    for client in clients:
57        if not (pair_pri_to_sec(provider, client)):
58            client.log.error("Client failed to pair with provider")
59            return False
60        else:
61            client.log.info("Client paired with provider")
62
63    time.sleep(5)
64    for client in clients:
65        client.droid.bluetoothConnectBonded(provider.droid.bluetoothGetLocalAddress())
66
67    time.sleep(20)
68    return True
69
70
71def verify_bluetooth_tethering_connection(log, provider, clients,
72                                           change_rat=None,
73                                           toggle_data=False,
74                                           toggle_tethering=False,
75                                           voice_call=False,
76                                           toggle_bluetooth=True):
77    """Setups up a bluetooth tethering connection between two android devices.
78
79    Returns:
80        True if PAN connection and verification is successful,
81        false if unsuccessful.
82    """
83
84
85    if not enable_bluetooth_tethering_connection(log, provider, clients):
86        return False
87
88    if not test_internet_connection(log, provider, clients):
89        log.error("Internet connection check failed")
90        return False
91    if voice_call:
92        log.info("====== Voice call test =====")
93        for caller, callee in [(provider, clients[0]),
94                               (clients[0], provider)]:
95            if not call_setup_teardown(
96                    log, caller, callee, ad_hangup=None):
97                log.error("Setup Call Failed.")
98                hangup_call(log, caller)
99                return False
100            log.info("Verify data.")
101            if not verify_internet_connection(
102                    log, clients[0], retries=1):
103                clients[0].log.warning(
104                    "client internet connection state is not on")
105            else:
106                clients[0].log.info(
107                    "client internet connection state is on")
108            hangup_call(log, caller)
109            if not verify_internet_connection(
110                    log, clients[0], retries=1):
111                clients[0].log.warning(
112                    "client internet connection state is not on")
113                return False
114            else:
115                clients[0].log.info(
116                    "client internet connection state is on")
117    if toggle_tethering:
118        log.info("====== Toggling provider bluetooth tethering =====")
119        provider.log.info("Disable bluetooth tethering")
120        provider.droid.bluetoothPanSetBluetoothTethering(False)
121        if not test_internet_connection(log, provider, clients, False, True):
122            log.error(
123                "Internet connection check failed after disable tethering")
124            return False
125        provider.log.info("Enable bluetooth tethering")
126        if not enable_bluetooth_tethering_connection(log,
127                provider, clients):
128            provider.log.error(
129                "Fail to re-enable bluetooth tethering")
130            return False
131        if not test_internet_connection(log, provider, clients, True, True):
132            log.error(
133                "Internet connection check failed after enable tethering")
134            return False
135    if toggle_bluetooth:
136        log.info("====== Toggling provider bluetooth =====")
137        provider.log.info("Disable provider bluetooth")
138        disable_bluetooth(provider.droid)
139        time.sleep(10)
140        if not test_internet_connection(log, provider, clients, False, True):
141            log.error(
142                "Internet connection check failed after disable bluetooth")
143            return False
144        if not enable_bluetooth_tethering_connection(log,
145                provider, clients):
146            provider.log.error(
147                "Fail to re-enable bluetooth tethering")
148            return False
149        if not test_internet_connection(log, provider, clients, True, True):
150            log.error(
151                "Internet connection check failed after enable bluetooth")
152            return False
153    if toggle_data:
154        log.info("===== Toggling provider data connection =====")
155        provider.log.info("Disable provider data connection")
156        provider.droid.telephonyToggleDataConnection(False)
157        time.sleep(10)
158        if not test_internet_connection(log, provider, clients, False, False):
159            return False
160        provider.log.info("Enable provider data connection")
161        provider.droid.telephonyToggleDataConnection(True)
162        if not wait_for_cell_data_connection(log, provider,
163                                             True):
164            provider.log.error(
165                "Provider failed to enable data connection.")
166            return False
167        if not test_internet_connection(log, provider, clients, True, True):
168            log.error(
169                "Internet connection check failed after enable data")
170            return False
171    if change_rat:
172        log.info("===== Change provider RAT to %s =====", change_rat)
173        if not ensure_network_generation(
174                log,
175                provider,
176                change_rat,
177                voice_or_data=NETWORK_SERVICE_DATA,
178                toggle_apm_after_setting=False):
179            provider.log.error("Provider failed to reselect to %s.",
180                                    change_rat)
181            return False
182        if not test_internet_connection(log, provider, clients, True, True):
183            log.error(
184                "Internet connection check failed after RAT change to %s",
185                change_rat)
186            return False
187    return True