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