1# 2# Copyright 2014 - The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import time 17 18from acts import asserts 19from acts import base_test 20from acts.test_decorators import test_tracker_info 21import acts.test_utils.wifi.wifi_test_utils as wutils 22from acts.test_utils.wifi.WifiBaseTest import WifiBaseTest 23 24WifiEnums = wutils.WifiEnums 25MAX_ATTN = 95 26 27class WifiPnoTest(WifiBaseTest): 28 29 def __init__(self, controllers): 30 WifiBaseTest.__init__(self, controllers) 31 32 def setup_class(self): 33 self.dut = self.android_devices[0] 34 wutils.wifi_test_device_init(self.dut) 35 req_params = ["attn_vals", "pno_interval"] 36 opt_param = ["reference_networks"] 37 self.unpack_userparams( 38 req_param_names=req_params, opt_param_names=opt_param) 39 40 if "AccessPoint" in self.user_params: 41 self.legacy_configure_ap_and_start() 42 43 self.pno_network_a = self.reference_networks[0]['2g'] 44 self.pno_network_b = self.reference_networks[0]['5g'] 45 self.attn_a = self.attenuators[0] 46 self.attn_b = self.attenuators[1] 47 # Disable second AP's networks, so that it does not interfere during PNO 48 self.attenuators[2].set_atten(MAX_ATTN) 49 self.attenuators[3].set_atten(MAX_ATTN) 50 self.set_attns("default") 51 52 def setup_test(self): 53 self.dut.droid.wifiStartTrackingStateChange() 54 self.dut.droid.wakeLockRelease() 55 self.dut.droid.goToSleepNow() 56 wutils.reset_wifi(self.dut) 57 self.dut.ed.clear_all_events() 58 59 def teardown_test(self): 60 self.dut.droid.wifiStopTrackingStateChange() 61 wutils.reset_wifi(self.dut) 62 self.dut.ed.clear_all_events() 63 self.set_attns("default") 64 65 def on_fail(self, test_name, begin_time): 66 self.dut.take_bug_report(test_name, begin_time) 67 self.dut.cat_adb_log(test_name, begin_time) 68 69 def teardown_class(self): 70 if "AccessPoint" in self.user_params: 71 del self.user_params["reference_networks"] 72 del self.user_params["open_network"] 73 74 """Helper Functions""" 75 76 def set_attns(self, attn_val_name): 77 """Sets attenuation values on attenuators used in this test. 78 79 Args: 80 attn_val_name: Name of the attenuation value pair to use. 81 """ 82 self.log.info("Set attenuation values to %s", 83 self.attn_vals[attn_val_name]) 84 try: 85 self.attn_a.set_atten(self.attn_vals[attn_val_name][0]) 86 self.attn_b.set_atten(self.attn_vals[attn_val_name][1]) 87 except: 88 self.log.error("Failed to set attenuation values %s.", 89 attn_val_name) 90 raise 91 92 def trigger_pno_and_assert_connect(self, attn_val_name, expected_con): 93 """Sets attenuators to disconnect current connection to trigger PNO. 94 Validate that the DUT connected to the new SSID as expected after PNO. 95 96 Args: 97 attn_val_name: Name of the attenuation value pair to use. 98 expected_con: The expected info of the network to we expect the DUT 99 to roam to. 100 """ 101 connection_info = self.dut.droid.wifiGetConnectionInfo() 102 self.log.info("Triggering PNO connect from %s to %s", 103 connection_info[WifiEnums.SSID_KEY], 104 expected_con[WifiEnums.SSID_KEY]) 105 self.set_attns(attn_val_name) 106 self.log.info("Wait %ss for PNO to trigger.", self.pno_interval) 107 time.sleep(self.pno_interval) 108 try: 109 self.log.info("Connected to %s network after PNO interval" 110 % self.dut.droid.wifiGetConnectionInfo()) 111 expected_ssid = expected_con[WifiEnums.SSID_KEY] 112 verify_con = {WifiEnums.SSID_KEY: expected_ssid} 113 wutils.verify_wifi_connection_info(self.dut, verify_con) 114 self.log.info("Connected to %s successfully after PNO", 115 expected_ssid) 116 finally: 117 pass 118 119 def add_and_enable_dummy_networks(self, num_networks): 120 """Add some dummy networks to the device and enable them. 121 122 Args: 123 num_networks: Number of networks to add. 124 """ 125 ssid_name_base = "pno_dummy_network_" 126 for i in range(0, num_networks): 127 network = {} 128 network[WifiEnums.SSID_KEY] = ssid_name_base + str(i) 129 network[WifiEnums.PWD_KEY] = "pno_dummy" 130 self.add_network_and_enable(network) 131 132 def add_network_and_enable(self, network): 133 """Add a network and enable it. 134 135 Args: 136 network : Network details for the network to be added. 137 138 """ 139 ret = self.dut.droid.wifiAddNetwork(network) 140 asserts.assert_true(ret != -1, "Add network %r failed" % network) 141 self.dut.droid.wifiEnableNetwork(ret, 0) 142 143 144 """ Tests Begin """ 145 146 @test_tracker_info(uuid="33d3cae4-5fa7-4e90-b9e2-5d3747bba64c") 147 def test_simple_pno_connection_to_2g(self): 148 """Test PNO triggered autoconnect to a network. 149 150 Steps: 151 1. Switch off the screen on the device. 152 2. Save 2 valid network configurations (a & b) in the device. 153 3. Attenuate 5Ghz network and wait for a few seconds to trigger PNO. 154 4. Check the device connected to 2Ghz network automatically. 155 """ 156 self.add_network_and_enable(self.pno_network_a) 157 self.add_network_and_enable(self.pno_network_b) 158 self.trigger_pno_and_assert_connect("a_on_b_off", self.pno_network_a) 159 160 @test_tracker_info(uuid="39b945a1-830f-4f11-9e6a-9e9641066a96") 161 def test_simple_pno_connection_to_5g(self): 162 """Test PNO triggered autoconnect to a network. 163 164 Steps: 165 1. Switch off the screen on the device. 166 2. Save 2 valid network configurations (a & b) in the device. 167 3. Attenuate 2Ghz network and wait for a few seconds to trigger PNO. 168 4. Check the device connected to 5Ghz network automatically. 169 170 """ 171 self.add_network_and_enable(self.pno_network_a) 172 self.add_network_and_enable(self.pno_network_b) 173 self.trigger_pno_and_assert_connect("b_on_a_off", self.pno_network_b) 174 175 @test_tracker_info(uuid="844b15be-ff45-4b09-a11b-0b2b4bb13b22") 176 def test_pno_connection_with_multiple_saved_networks(self): 177 """Test PNO triggered autoconnect to a network when there are more 178 than 16 networks saved in the device. 179 180 16 is the max list size of PNO watch list for most devices. The device 181 should automatically pick the 16 latest added networks in the list. 182 So add 16 dummy networks and then add 2 valid networks. 183 184 Steps: 185 1. Save 16 dummy network configurations in the device. 186 2. Run the simple pno test. 187 """ 188 self.add_and_enable_dummy_networks(16) 189 self.add_network_and_enable(self.pno_network_a) 190 self.add_network_and_enable(self.pno_network_b) 191 # Force single scan so that both networks become preferred before PNO. 192 wutils.start_wifi_connection_scan_and_return_status(self.dut) 193 time.sleep(10) 194 self.trigger_pno_and_assert_connect("a_on_b_off", self.pno_network_a) 195 196 """ Tests End """ 197