• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2020 - 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
18import random
19import re
20
21from queue import Empty
22from acts.utils import rand_ascii_str
23from acts_contrib.test_utils.tel.tel_defines import GEN_5G
24from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_NR_LTE_GSM_WCDMA
25from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_NR_ONLY
26from acts_contrib.test_utils.tel.tel_defines import WFC_MODE_CELLULAR_PREFERRED
27from acts_contrib.test_utils.tel.tel_defines import WFC_MODE_WIFI_PREFERRED
28from acts_contrib.test_utils.tel.tel_test_utils import set_preferred_network_mode_pref
29from acts_contrib.test_utils.tel.tel_test_utils import multithread_func
30from acts_contrib.test_utils.tel.tel_test_utils import ensure_wifi_connected
31from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode
32from acts_contrib.test_utils.tel.tel_voice_utils import phone_setup_volte
33from acts_contrib.test_utils.tel.tel_voice_utils import phone_setup_iwlan
34from acts_contrib.test_utils.tel.tel_voice_utils import phone_setup_csfb
35from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g_nsa
36from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g_sa
37
38
39def provision_device_for_5g(log, ads, sa_5g=False, nsa_mmwave=False):
40    """Provision Devices for 5G
41
42    Args:
43        log: Log object.
44        ads: android device object(s).
45        sa_5g: Check for provision on sa_5G or not
46        nsa_mmwave: If true, check the band of NSA network is mmWave. Default is to check sub-6.
47
48    Returns:
49        True: Device(s) are provisioned on 5G
50        False: Device(s) are not provisioned on 5G
51    """
52    if sa_5g:
53        if not provision_device_for_5g_sa(log, ads):
54            return False
55    else:
56        if not provision_device_for_5g_nsa(log, ads, nsa_mmwave=nsa_mmwave):
57            return False
58    return True
59
60
61def provision_device_for_5g_nsa(log, ads, nsa_mmwave=False):
62    """Provision Devices for 5G NSA
63
64    Args:
65        log: Log object.
66        ads: android device object(s).
67        nsa_mmwave: If true, check the band of NSA network is mmWave. Default is to check sub-6.
68
69    Returns:
70        True: Device(s) are provisioned on 5G NSA
71        False: Device(s) are not provisioned on 5G NSA
72    """
73    if isinstance(ads, list):
74        # Mode Pref
75        tasks = [(set_preferred_mode_for_5g, [ad]) for ad in ads]
76        if not multithread_func(log, tasks):
77            log.error("failed to set preferred network mode on 5g")
78            return False
79        # Attach
80        tasks = [(is_current_network_5g_nsa, [ad, nsa_mmwave]) for ad in ads]
81        if not multithread_func(log, tasks):
82            log.error("phone not on 5g nsa")
83            return False
84        return True
85    else:
86        # Mode Pref
87        set_preferred_mode_for_5g(ads)
88
89        # Attach nsa5g
90        if not is_current_network_5g_nsa(ads, nsa_mmwave=nsa_mmwave):
91            ads.log.error("Phone not attached on nsa 5g")
92            return False
93        return True
94
95
96def provision_both_devices_for_volte(log, ads):
97    # LTE attach and enable VoLTE on both phones
98    tasks = [(phone_setup_volte, (log, ads[0])),
99             (phone_setup_volte, (log, ads[1]))]
100    if not multithread_func(log, tasks):
101        log.error("phone failed to set up in volte")
102        return False
103    return True
104
105
106def provision_both_devices_for_csfb(log, ads):
107    tasks = [(phone_setup_csfb, (log, ads[0])),
108             (phone_setup_csfb, (log, ads[1]))]
109    if not multithread_func(log, tasks):
110        log.error("Phone Failed to Set Up in csfb.")
111        return False
112    return True
113
114
115def provision_both_devices_for_wfc_cell_pref(log,
116                                             ads,
117                                             wifi_ssid,
118                                             wifi_pass,
119                                             apm_mode=False):
120    tasks = [(phone_setup_iwlan,
121              (log, ads[0], apm_mode, WFC_MODE_CELLULAR_PREFERRED,
122               wifi_ssid, wifi_pass)),
123             (phone_setup_iwlan,
124              (log, ads[1], apm_mode, WFC_MODE_CELLULAR_PREFERRED,
125               wifi_ssid, wifi_pass))]
126    if not multithread_func(log, tasks):
127        log.error("failed to setup in wfc_cell_pref mode")
128        return False
129    return True
130
131
132def provision_both_devices_for_wfc_wifi_pref(log,
133                                             ads,
134                                             wifi_ssid,
135                                             wifi_pass,
136                                             apm_mode=False):
137    tasks = [(phone_setup_iwlan,
138              (log, ads[0], apm_mode, WFC_MODE_WIFI_PREFERRED,
139               wifi_ssid, wifi_pass)),
140             (phone_setup_iwlan,
141              (log, ads[1], apm_mode, WFC_MODE_WIFI_PREFERRED,
142               wifi_ssid, wifi_pass))]
143    if not multithread_func(log, tasks):
144        log.error("failed to setup in wfc_wifi_pref mode")
145        return False
146    return True
147
148
149def disable_apm_mode_both_devices(log, ads):
150    # Turn off airplane mode
151    log.info("Turn off apm mode on both devices")
152    tasks = [(toggle_airplane_mode, (log, ads[0], False)),
153             (toggle_airplane_mode, (log, ads[1], False))]
154    if not multithread_func(log, tasks):
155        log.error("Failed to turn off airplane mode")
156        return False
157    return True
158
159
160def connect_both_devices_to_wifi(log,
161                                 ads,
162                                 wifi_ssid,
163                                 wifi_pass):
164    tasks = [(ensure_wifi_connected, (log, ad, wifi_ssid, wifi_pass))
165             for ad in ads]
166    if not multithread_func(log, tasks):
167        log.error("phone failed to connect to wifi.")
168        return False
169    return True
170
171
172def verify_5g_attach_for_both_devices(log, ads, sa_5g=False, nsa_mmwave=False):
173    """Verify the network is attached
174
175    Args:
176        log: Log object.
177        ads: android device object(s).
178        sa_5g: Check for verify data network type is on 5G SA or not
179        nsa_mmwave: If true, check the band of NSA network is mmWave. Default is to check sub-6.
180
181    Returns:
182        True: Device(s) are attached on 5G
183        False: Device(s) are not attached on 5G NSA
184    """
185    if sa_5g:
186        # Attach
187        tasks = [(is_current_network_5g_sa, [ad]) for ad in ads]
188        if not multithread_func(log, tasks):
189            log.error("phone not on 5g sa")
190            return False
191        return True
192    else:
193        # Attach
194        tasks = [(is_current_network_5g_nsa, [ad, nsa_mmwave]) for ad in ads]
195        if not multithread_func(log, tasks):
196            log.error("phone not on 5g nsa")
197            return False
198        return True
199
200
201def set_preferred_mode_for_5g(ad, sub_id=None, mode=None):
202    """Set Preferred Network Mode for 5G NSA
203    Args:
204        ad: Android device object.
205        sub_id: Subscription ID.
206        mode: 5G Network Mode Type
207    """
208    if sub_id is None:
209        sub_id = ad.droid.subscriptionGetDefaultSubId()
210    if mode is None:
211        mode = NETWORK_MODE_NR_LTE_GSM_WCDMA
212    return set_preferred_network_mode_pref(ad.log, ad, sub_id, mode)
213
214
215def provision_device_for_5g_sa(log, ads):
216    """Provision Devices for 5G SA
217
218    Args:
219        log: Log object.
220        ads: android device object(s).
221
222    Returns:
223        True: Device(s) are provisioned on 5G SA
224        False: Device(s) are not provisioned on 5G SA
225    """
226    if isinstance(ads, list):
227        # Mode Pref
228        tasks = [(set_preferred_mode_for_5g, [ad, None, NETWORK_MODE_NR_ONLY]) for ad in ads]
229        if not multithread_func(log, tasks):
230            log.error("failed to set preferred network mode on 5g SA")
231            return False
232
233        tasks = [(is_current_network_5g_sa, [ad]) for ad in ads]
234        if not multithread_func(log, tasks):
235            log.error("phone not on 5g SA")
236            return False
237        return True
238    else:
239        # Mode Pref
240        set_preferred_mode_for_5g(ads, None, NETWORK_MODE_NR_ONLY)
241
242        if not is_current_network_5g_sa(ads):
243            ads.log.error("Phone not attached on SA 5g")
244            return False
245        return True
246
247
248def check_current_network_5g(ad, timeout=30, sa_5g=False, nsa_mmwave=False):
249    """Verifies data network type is on 5G
250
251    Args:
252        ad: android device object.
253        timeout: max time to wait for event
254        sa_5g: Check for verify data network type is on 5G SA or not
255        nsa_mmwave: If true, check the band of NSA network is mmWave. Default is to check sub-6.
256
257    Returns:
258        True: if data is on 5g
259        False: if data is not on 5g
260    """
261    if sa_5g:
262        if not is_current_network_5g_sa(ad):
263            return False
264    else:
265        if not is_current_network_5g_nsa(ad, nsa_mmwave=nsa_mmwave, timeout=timeout):
266            return False
267    return True
268
269