1#!/usr/bin/env python3 2# 3# Copyright 2021 - 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.tel.tel_defines import TYPE_WIFI 20from acts_contrib.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_STATE_CHECK 21from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode 22from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection 23from acts_contrib.test_utils.wifi import wifi_test_utils 24 25WIFI_SSID_KEY = wifi_test_utils.WifiEnums.SSID_KEY 26WIFI_PWD_KEY = wifi_test_utils.WifiEnums.PWD_KEY 27WIFI_CONFIG_APBAND_2G = 1 28WIFI_CONFIG_APBAND_5G = 2 29WIFI_CONFIG_APBAND_AUTO = wifi_test_utils.WifiEnums.WIFI_CONFIG_APBAND_AUTO 30 31 32def get_wifi_signal_strength(ad): 33 signal_strength = ad.droid.wifiGetConnectionInfo()['rssi'] 34 ad.log.info("WiFi Signal Strength is %s" % signal_strength) 35 return signal_strength 36 37 38def get_wifi_usage(ad, sid=None, apk=None): 39 if not sid: 40 sid = ad.droid.subscriptionGetDefaultDataSubId() 41 current_time = int(time.time() * 1000) 42 begin_time = current_time - 10 * 24 * 60 * 60 * 1000 43 end_time = current_time + 10 * 24 * 60 * 60 * 1000 44 45 if apk: 46 uid = ad.get_apk_uid(apk) 47 ad.log.debug("apk %s uid = %s", apk, uid) 48 try: 49 return ad.droid.connectivityQueryDetailsForUid( 50 TYPE_WIFI, 51 ad.droid.telephonyGetSubscriberIdForSubscription(sid), 52 begin_time, end_time, uid) 53 except: 54 return ad.droid.connectivityQueryDetailsForUid( 55 ad.droid.telephonyGetSubscriberIdForSubscription(sid), 56 begin_time, end_time, uid) 57 else: 58 try: 59 return ad.droid.connectivityQuerySummaryForDevice( 60 TYPE_WIFI, 61 ad.droid.telephonyGetSubscriberIdForSubscription(sid), 62 begin_time, end_time) 63 except: 64 return ad.droid.connectivityQuerySummaryForDevice( 65 ad.droid.telephonyGetSubscriberIdForSubscription(sid), 66 begin_time, end_time) 67 68 69def check_is_wifi_connected(log, ad, wifi_ssid): 70 """Check if ad is connected to wifi wifi_ssid. 71 72 Args: 73 log: Log object. 74 ad: Android device object. 75 wifi_ssid: WiFi network SSID. 76 77 Returns: 78 True if wifi is connected to wifi_ssid 79 False if wifi is not connected to wifi_ssid 80 """ 81 wifi_info = ad.droid.wifiGetConnectionInfo() 82 if wifi_info["supplicant_state"] == "completed" and wifi_info["SSID"] == wifi_ssid: 83 ad.log.info("Wifi is connected to %s", wifi_ssid) 84 ad.on_mobile_data = False 85 return True 86 else: 87 ad.log.info("Wifi is not connected to %s", wifi_ssid) 88 ad.log.debug("Wifi connection_info=%s", wifi_info) 89 ad.on_mobile_data = True 90 return False 91 92 93def ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd=None, retries=3, apm=False): 94 """Ensure ad connected to wifi on network wifi_ssid. 95 96 Args: 97 log: Log object. 98 ad: Android device object. 99 wifi_ssid: WiFi network SSID. 100 wifi_pwd: optional secure network password. 101 retries: the number of retries. 102 103 Returns: 104 True if wifi is connected to wifi_ssid 105 False if wifi is not connected to wifi_ssid 106 """ 107 if not toggle_airplane_mode(log, ad, apm, strict_checking=False): 108 return False 109 110 network = {WIFI_SSID_KEY: wifi_ssid} 111 if wifi_pwd: 112 network[WIFI_PWD_KEY] = wifi_pwd 113 for i in range(retries): 114 if not ad.droid.wifiCheckState(): 115 ad.log.info("Wifi state is down. Turn on Wifi") 116 ad.droid.wifiToggleState(True) 117 if check_is_wifi_connected(log, ad, wifi_ssid): 118 ad.log.info("Wifi is connected to %s", wifi_ssid) 119 return verify_internet_connection(log, ad, retries=3) 120 else: 121 ad.log.info("Connecting to wifi %s", wifi_ssid) 122 try: 123 ad.droid.wifiConnectByConfig(network) 124 except Exception: 125 ad.log.info("Connecting to wifi by wifiConnect instead") 126 ad.droid.wifiConnect(network) 127 time.sleep(20) 128 if check_is_wifi_connected(log, ad, wifi_ssid): 129 ad.log.info("Connected to Wifi %s", wifi_ssid) 130 return verify_internet_connection(log, ad, retries=3) 131 ad.log.info("Fail to connected to wifi %s", wifi_ssid) 132 return False 133 134 135def forget_all_wifi_networks(log, ad): 136 """Forget all stored wifi network information 137 138 Args: 139 log: log object 140 ad: AndroidDevice object 141 142 Returns: 143 boolean success (True) or failure (False) 144 """ 145 if not ad.droid.wifiGetConfiguredNetworks(): 146 ad.on_mobile_data = True 147 return True 148 try: 149 old_state = ad.droid.wifiCheckState() 150 wifi_test_utils.reset_wifi(ad) 151 wifi_toggle_state(log, ad, old_state) 152 except Exception as e: 153 log.error("forget_all_wifi_networks with exception: %s", e) 154 return False 155 ad.on_mobile_data = True 156 return True 157 158 159def wifi_reset(log, ad, disable_wifi=True): 160 """Forget all stored wifi networks and (optionally) disable WiFi 161 162 Args: 163 log: log object 164 ad: AndroidDevice object 165 disable_wifi: boolean to disable wifi, defaults to True 166 Returns: 167 boolean success (True) or failure (False) 168 """ 169 if not forget_all_wifi_networks(log, ad): 170 ad.log.error("Unable to forget all networks") 171 return False 172 if not wifi_toggle_state(log, ad, not disable_wifi): 173 ad.log.error("Failed to toggle WiFi state to %s!", not disable_wifi) 174 return False 175 return True 176 177 178def set_wifi_to_default(log, ad): 179 """Set wifi to default state (Wifi disabled and no configured network) 180 181 Args: 182 log: log object 183 ad: AndroidDevice object 184 185 Returns: 186 boolean success (True) or failure (False) 187 """ 188 ad.droid.wifiFactoryReset() 189 ad.droid.wifiToggleState(False) 190 ad.on_mobile_data = True 191 192 193def wifi_toggle_state(log, ad, state, retries=3): 194 """Toggle the WiFi State 195 196 Args: 197 log: log object 198 ad: AndroidDevice object 199 state: True, False, or None 200 201 Returns: 202 boolean success (True) or failure (False) 203 """ 204 for i in range(retries): 205 if wifi_test_utils.wifi_toggle_state(ad, state, assert_on_fail=False): 206 ad.on_mobile_data = not state 207 return True 208 time.sleep(WAIT_TIME_BETWEEN_STATE_CHECK) 209 return False 210 211 212def start_wifi_tethering(log, ad, ssid, password, ap_band=None): 213 """Start a Tethering Session 214 215 Args: 216 log: log object 217 ad: AndroidDevice object 218 ssid: the name of the WiFi network 219 password: optional password, used for secure networks. 220 ap_band=DEPRECATED specification of 2G or 5G tethering 221 Returns: 222 boolean success (True) or failure (False) 223 """ 224 return wifi_test_utils._assert_on_fail_handler( 225 wifi_test_utils.start_wifi_tethering, 226 False, 227 ad, 228 ssid, 229 password, 230 band=ap_band) 231 232 233def stop_wifi_tethering(log, ad): 234 """Stop a Tethering Session 235 236 Args: 237 log: log object 238 ad: AndroidDevice object 239 Returns: 240 boolean success (True) or failure (False) 241 """ 242 return wifi_test_utils._assert_on_fail_handler( 243 wifi_test_utils.stop_wifi_tethering, False, ad)