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 18 19from acts.libs.utils.multithread import multithread_func 20from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_NR_LTE_GSM_WCDMA 21from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_NR_ONLY 22from acts_contrib.test_utils.tel.tel_defines import WFC_MODE_CELLULAR_PREFERRED 23from acts_contrib.test_utils.tel.tel_defines import WFC_MODE_WIFI_PREFERRED 24from acts_contrib.test_utils.tel.tel_defines import GEN_4G 25from acts_contrib.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA 26from acts_contrib.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING 27from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_WCDMA_ONLY 28from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g_nsa 29from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g_sa 30from acts_contrib.test_utils.tel.tel_phone_setup_utils import phone_setup_volte 31from acts_contrib.test_utils.tel.tel_phone_setup_utils import phone_setup_iwlan 32from acts_contrib.test_utils.tel.tel_phone_setup_utils import phone_setup_csfb 33from acts_contrib.test_utils.tel.tel_phone_setup_utils import wait_for_network_generation 34from acts_contrib.test_utils.tel.tel_test_utils import set_preferred_network_mode_pref 35from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode 36from acts_contrib.test_utils.tel.tel_test_utils import get_current_override_network_type 37from acts_contrib.test_utils.tel.tel_wifi_utils import ensure_wifi_connected 38from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_toggle_state 39 40 41def provision_device_for_5g(log, ads, nr_type = None, mmwave = None): 42 """Provision Devices for 5G 43 44 Args: 45 log: Log object. 46 ads: android device object(s). 47 nr_type: NR network type. 48 mmwave: True to detect 5G millimeter wave, False to detect sub-6, 49 None to detect both. 50 51 Returns: 52 True: Device(s) are provisioned on 5G 53 False: Device(s) are not provisioned on 5G 54 """ 55 if nr_type == 'sa': 56 if not provision_device_for_5g_sa( 57 log, ads, mmwave=mmwave): 58 return False 59 elif nr_type == 'nsa': 60 if not provision_device_for_5g_nsa( 61 log, ads, mmwave=mmwave): 62 return False 63 elif nr_type == 'mmwave': 64 if not provision_device_for_5g_nsa( 65 log, ads, mmwave=mmwave): 66 return False 67 else: 68 if not provision_device_for_5g_nsa( 69 log, ads, mmwave=mmwave): 70 return False 71 return True 72 73 74def provision_device_for_5g_nsa(log, ads, mmwave = None): 75 """Provision Devices for 5G NSA 76 77 Args: 78 log: Log object. 79 ads: android device object(s). 80 mmwave: True to detect 5G millimeter wave, False to detect sub-6, 81 None to detect both. 82 83 Returns: 84 True: Device(s) are provisioned on 5G NSA 85 False: Device(s) are not provisioned on 5G NSA 86 """ 87 88 if isinstance(ads, list): 89 # Mode Pref 90 tasks = [(set_preferred_mode_for_5g, [ad]) for ad in ads] 91 if not multithread_func(log, tasks): 92 log.error("failed to set preferred network mode on 5g") 93 return False 94 # Attach 95 tasks = [(is_current_network_5g_nsa, [ad, None, mmwave]) for ad in ads] 96 if not multithread_func(log, tasks): 97 log.error("phone not on 5g") 98 return False 99 return True 100 else: 101 # Mode Pref 102 set_preferred_mode_for_5g(ads) 103 104 # Attach nsa5g 105 if not is_current_network_5g_nsa(ads, mmwave=mmwave): 106 ads.log.error("Phone not attached on 5g") 107 return False 108 return True 109 110 111def provision_both_devices_for_volte(log, ads, nw_gen, nr_type=None): 112 # LTE or NR attach and enable VoLTE on both phones 113 tasks = [(phone_setup_volte, (log, ads[0], nw_gen, nr_type)), 114 (phone_setup_volte, (log, ads[1], nw_gen, nr_type))] 115 if not multithread_func(log, tasks): 116 log.error("phone failed to set up in volte") 117 return False 118 return True 119 120 121def provision_both_devices_for_csfb(log, ads): 122 tasks = [(phone_setup_csfb, (log, ads[0])), 123 (phone_setup_csfb, (log, ads[1]))] 124 if not multithread_func(log, tasks): 125 log.error("Phone Failed to Set Up in csfb.") 126 return False 127 return True 128 129 130def provision_both_devices_for_wfc_cell_pref(log, 131 ads, 132 wifi_ssid, 133 wifi_pass, 134 apm_mode=False): 135 tasks = [(phone_setup_iwlan, 136 (log, ads[0], apm_mode, WFC_MODE_CELLULAR_PREFERRED, 137 wifi_ssid, wifi_pass)), 138 (phone_setup_iwlan, 139 (log, ads[1], apm_mode, WFC_MODE_CELLULAR_PREFERRED, 140 wifi_ssid, wifi_pass))] 141 if not multithread_func(log, tasks): 142 log.error("failed to setup in wfc_cell_pref mode") 143 return False 144 return True 145 146 147def provision_both_devices_for_wfc_wifi_pref(log, 148 ads, 149 wifi_ssid, 150 wifi_pass, 151 apm_mode=False): 152 tasks = [(phone_setup_iwlan, 153 (log, ads[0], apm_mode, WFC_MODE_WIFI_PREFERRED, 154 wifi_ssid, wifi_pass)), 155 (phone_setup_iwlan, 156 (log, ads[1], apm_mode, WFC_MODE_WIFI_PREFERRED, 157 wifi_ssid, wifi_pass))] 158 if not multithread_func(log, tasks): 159 log.error("failed to setup in wfc_wifi_pref mode") 160 return False 161 return True 162 163 164def disable_apm_mode_both_devices(log, ads): 165 # Turn off airplane mode 166 log.info("Turn off apm mode on both devices") 167 tasks = [(toggle_airplane_mode, (log, ads[0], False)), 168 (toggle_airplane_mode, (log, ads[1], False))] 169 if not multithread_func(log, tasks): 170 log.error("Failed to turn off airplane mode") 171 return False 172 return True 173 174 175def connect_both_devices_to_wifi(log, 176 ads, 177 wifi_ssid, 178 wifi_pass): 179 tasks = [(ensure_wifi_connected, (log, ad, wifi_ssid, wifi_pass)) 180 for ad in ads] 181 if not multithread_func(log, tasks): 182 log.error("phone failed to connect to wifi.") 183 return False 184 return True 185 186 187def verify_5g_attach_for_both_devices(log, ads, nr_type = None, mmwave = None): 188 """Verify the network is attached 189 190 Args: 191 log: Log object. 192 ads: android device object(s). 193 nr_type: 'sa' for 5G standalone, 'nsa' for 5G non-standalone, 194 'mmwave' for 5G millimeter wave. 195 mmwave: True to detect 5G millimeter wave, False to detect sub-6, 196 None to detect both. 197 198 Returns: 199 True: Device(s) are attached on 5G 200 False: Device(s) are not attached on 5G NSA 201 """ 202 203 if nr_type=='sa': 204 # Attach 205 tasks = [(is_current_network_5g_sa, [ad, None, mmwave]) for ad in ads] 206 if not multithread_func(log, tasks): 207 log.error("phone not on 5g sa") 208 return False 209 return True 210 else: 211 # Attach 212 tasks = [(is_current_network_5g_nsa, [ad, None, mmwave]) for ad in ads] 213 if not multithread_func(log, tasks): 214 log.error("phone not on 5g nsa") 215 return False 216 return True 217 218 219def set_preferred_mode_for_5g(ad, sub_id=None, mode=None): 220 """Set Preferred Network Mode for 5G NSA 221 Args: 222 ad: Android device object. 223 sub_id: Subscription ID. 224 mode: 5G Network Mode Type 225 """ 226 if sub_id is None: 227 sub_id = ad.droid.subscriptionGetDefaultSubId() 228 if mode is None: 229 mode = NETWORK_MODE_NR_LTE_GSM_WCDMA 230 return set_preferred_network_mode_pref(ad.log, ad, sub_id, mode) 231 232 233def provision_device_for_5g_sa(log, ads, mmwave = None): 234 """Provision Devices for 5G SA 235 236 Args: 237 log: Log object. 238 ads: android device object(s). 239 mmwave: True to detect 5G millimeter wave, False to detect sub-6, 240 None to detect both. 241 242 Returns: 243 True: Device(s) are provisioned on 5G SA 244 False: Device(s) are not provisioned on 5G SA 245 """ 246 247 if isinstance(ads, list): 248 # Mode Pref 249 tasks = [(set_preferred_mode_for_5g, [ad, None, NETWORK_MODE_NR_ONLY]) for ad in ads] 250 if not multithread_func(log, tasks): 251 log.error("failed to set preferred network mode on 5g SA") 252 return False 253 254 tasks = [(is_current_network_5g_sa, [ad, None, mmwave]) for ad in ads] 255 if not multithread_func(log, tasks): 256 log.error("phone not on 5g SA") 257 return False 258 return True 259 else: 260 # Mode Pref 261 set_preferred_mode_for_5g(ads, None, NETWORK_MODE_NR_ONLY) 262 263 if not is_current_network_5g_sa(ads, None, mmwave): 264 ads.log.error("Phone not attached on SA 5g") 265 return False 266 return True 267 268 269def check_current_network_5g( 270 ad, sub_id = None, nr_type = None, mmwave = None, timeout = 30): 271 """Verifies data network type is on 5G 272 273 Args: 274 ad: android device object. 275 sub_id: The target SIM for querying. 276 nr_type: 'sa' for 5G standalone, 'nsa' for 5G non-standalone, 'mmwave' for 5G millimeter 277 wave. 278 mmwave: True to detect 5G millimeter wave, False to detect sub-6, 279 None to detect both. 280 timeout: max time to wait for event. 281 282 Returns: 283 True: if data is on 5g 284 False: if data is not on 5g 285 """ 286 sub_id = sub_id if sub_id else ad.droid.subscriptionGetDefaultDataSubId() 287 288 if nr_type == 'sa': 289 if not is_current_network_5g_sa(ad, sub_id, mmwave=mmwave): 290 return False 291 else: 292 if not is_current_network_5g_nsa(ad, sub_id, mmwave=mmwave, 293 timeout=timeout): 294 return False 295 return True 296 297 298def test_activation_by_condition(ad, sub_id=None, from_3g=False, nr_type=None, 299 precond_func=None, mmwave=None): 300 """Test 5G activation based on various pre-conditions. 301 302 Args: 303 ad: android device object. 304 sub_id: The target SIM for querying. 305 from_3g: If true, test 5G activation from 3G attaching. Otherwise, starting from 5G attaching. 306 nr_type: check the band of NR network. Default is to check sub-6. 307 precond_func: A function to execute pre conditions before testing 5G activation. 308 mmwave: True to detect 5G millimeter wave, False to detect sub-6, 309 None to detect both. 310 311 Returns: 312 If success, return true. Otherwise, return false. 313 """ 314 sub_id = sub_id if sub_id else ad.droid.subscriptionGetDefaultDataSubId() 315 316 wifi_toggle_state(ad.log, ad, False) 317 toggle_airplane_mode(ad.log, ad, False) 318 if not from_3g: 319 set_preferred_mode_for_5g(ad) 320 for iteration in range(3): 321 ad.log.info("Attempt %d", iteration + 1) 322 sub_id=ad.droid.subscriptionGetDefaultSubId() 323 if from_3g: 324 # Set mode pref to 3G 325 set_preferred_network_mode_pref(ad.log, 326 ad, 327 sub_id, 328 NETWORK_MODE_WCDMA_ONLY) 329 time.sleep(15) 330 # Set mode pref to 5G 331 set_preferred_mode_for_5g(ad) 332 333 elif precond_func: 334 if not precond_func(): 335 return False 336 # LTE attach 337 if not wait_for_network_generation( 338 ad.log, ad, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA): 339 ad.log.error("Fail to ensure initial data in 4G") 340 # 5G attach 341 ad.log.info("Waiting for 5g NSA attach for 60 secs") 342 if is_current_network_5g_nsa(ad, sub_id, mmwave=mmwave, timeout=60): 343 ad.log.info("Success! attached on 5g NSA") 344 return True 345 else: 346 ad.log.error("Failure - expected NR_NSA, current %s", 347 get_current_override_network_type(ad)) 348 time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING) 349 ad.log.info("nsa5g attach test FAIL for all 3 iterations") 350 return False 351