1#!/usr/bin/env python3.4
2#
3#   Copyright 2018 - The Android Open Source Project
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 copy
18import time
19from acts import utils
20from acts.controllers.ap_lib import hostapd_constants as hc
21from acts.test_decorators import test_tracker_info
22from acts.test_utils.power import PowerWiFiBaseTest as PWBT
23from acts.test_utils.wifi import wifi_constants as wc
24from acts.test_utils.wifi import wifi_test_utils as wutils
25from acts.test_utils.power import plot_utils
26
27PHONE_BATTERY_VOLTAGE = 4.2
28
29
30class PowerWiFiroamingTest(PWBT.PowerWiFiBaseTest):
31    def setup_class(self):
32        super().setup_class()
33        self.unpack_userparams(toggle_interval=20, toggle_times=10)
34
35    def teardown_test(self):
36        # Delete the brconfigs attributes as this is duplicated with one of the
37        # ap's bridge interface config
38        delattr(self, 'brconfigs')
39        super().teardown_test()
40
41    # Test cases
42    @test_tracker_info(uuid='392622d3-0c5c-4767-afa2-abfb2058b0b8')
43    def test_screenoff_roaming(self):
44        """Test roaming power consumption with screen off.
45        Change the attenuation level to trigger roaming between two APs
46
47        """
48        # Setup both APs
49        network_main = copy.deepcopy(self.main_network)[hc.BAND_2G]
50        network_aux = copy.deepcopy(self.aux_network)[hc.BAND_2G]
51        self.log.info('Set attenuation to connect device to the aux AP')
52        self.set_attenuation(self.atten_level[wc.AP_AUX])
53        self.brconfigs_aux = self.setup_ap_connection(network_aux,
54                                                      ap=self.access_point_aux)
55        self.log.info('Set attenuation to connect device to the main AP')
56        self.set_attenuation(self.atten_level[wc.AP_MAIN])
57        self.brconfigs_main = self.setup_ap_connection(
58            network_main, ap=self.access_point_main)
59        self.dut.droid.goToSleepNow()
60        time.sleep(5)
61        # Set attenuator to trigger roaming
62        self.dut.log.info('Trigger roaming now')
63        self.set_attenuation(self.atten_level[self.current_test_name])
64        self.measure_power_and_validate()
65
66    @test_tracker_info(uuid='a0459b7c-74ce-4adb-8e55-c5365bc625eb')
67    def test_screenoff_toggle_between_AP(self):
68
69        # Set attenuator to connect phone to both networks
70        network_main = copy.deepcopy(self.main_network)[hc.BAND_2G]
71        network_aux = copy.deepcopy(self.aux_network)[hc.BAND_2G]
72        # Connect to both APs
73        network_main = self.main_network[hc.BAND_2G]
74        network_aux = self.aux_network[hc.BAND_2G]
75        self.log.info('Set attenuation to connect device to the main AP')
76        self.set_attenuation(self.atten_level[wc.AP_MAIN])
77        self.brconfigs_main = self.setup_ap_connection(
78            network_main, ap=self.access_point_main)
79        self.log.info('Set attenuation to connect device to the aux AP')
80        self.set_attenuation(self.atten_level[wc.AP_AUX])
81        self.brconfigs_aux = self.setup_ap_connection(network_aux,
82                                                      ap=self.access_point_aux)
83        self.mon_info.duration = self.toggle_interval
84        self.dut.droid.goToSleepNow()
85        time.sleep(5)
86        # Toggle between two networks
87        begin_time = utils.get_current_epoch_time()
88        results = []
89        for i in range(self.toggle_times):
90            self.dut.log.info('Connecting to %s' % network_main[wc.SSID])
91            self.dut.droid.wifiConnect(network_main)
92            results.append(self.monsoon_data_collect_save())
93            self.dut.log.info('Connecting to %s' % network_aux[wc.SSID])
94            self.dut.droid.wifiConnect(network_aux)
95            results.append(self.monsoon_data_collect_save())
96        plot_utils.monsoon_data_plot(self.mon_info, results)
97
98        total_current = 0
99        total_samples = 0
100        for result in results:
101            total_current += result.average_current * result.num_samples
102            total_samples += result.num_samples
103        average_current = total_current / total_samples
104
105        self.power_result.metric_value = [
106            result.total_power for result in results
107        ]
108        # Take Bugreport
109        if self.bug_report:
110            self.dut.take_bug_report(self.test_name, begin_time)
111        # Path fail check
112        self.pass_fail_check(average_current)
113
114    @test_tracker_info(uuid='e5ff95c0-b17e-425c-a903-821ba555a9b9')
115    def test_screenon_toggle_between_AP(self):
116
117        # Set attenuator to connect phone to both networks
118        network_main = copy.deepcopy(self.main_network)[hc.BAND_2G]
119        network_aux = copy.deepcopy(self.aux_network)[hc.BAND_2G]
120        # Connect to both APs
121        network_main = self.main_network[hc.BAND_2G]
122        network_aux = self.aux_network[hc.BAND_2G]
123        self.log.info('Set attenuation to connect device to the main AP')
124        self.set_attenuation(self.atten_level[wc.AP_MAIN])
125        self.brconfigs_main = self.setup_ap_connection(
126            network_main, ap=self.access_point_main)
127        self.log.info('Set attenuation to connect device to the aux AP')
128        self.set_attenuation(self.atten_level[wc.AP_AUX])
129        self.brconfigs_aux = self.setup_ap_connection(network_aux,
130                                                      ap=self.access_point_aux)
131        self.mon_info.duration = self.toggle_interval
132        time.sleep(5)
133        # Toggle between two networks
134        begin_time = utils.get_current_epoch_time()
135        results = []
136        for i in range(self.toggle_times):
137            self.dut.log.info('Connecting to %s' % network_main[wc.SSID])
138            self.dut.droid.wifiConnect(network_main)
139            results.append(self.monsoon_data_collect_save())
140            self.dut.log.info('Connecting to %s' % network_aux[wc.SSID])
141            self.dut.droid.wifiConnect(network_aux)
142            results.append(self.monsoon_data_collect_save())
143        plot_utils.monsoon_data_plot(self.mon_info, results)
144
145        total_current = 0
146        total_samples = 0
147        for result in results:
148            total_current += result.average_current * result.num_samples
149            total_samples += result.num_samples
150        average_current = total_current / total_samples
151
152        self.power_result.metric_value = [
153            result.total_power for result in results
154        ]
155        # Take Bugreport
156        if self.bug_report:
157            self.dut.take_bug_report(self.test_name, begin_time)
158        # Path fail check
159        self.pass_fail_check(average_current)
160
161    @test_tracker_info(uuid='a16ae337-326f-4d09-990f-42232c3c0dc4')
162    def test_screenoff_wifi_wedge(self):
163
164        # Set attenuator to connect phone to both networks
165        network_main = copy.deepcopy(self.main_network)[hc.BAND_2G]
166        network_aux = copy.deepcopy(self.aux_network)[hc.BAND_2G]
167        # Connect to both APs
168        network_main = self.main_network[hc.BAND_2G]
169        network_aux = self.aux_network[hc.BAND_2G]
170        self.log.info('Set attenuation to connect device to the main AP')
171        self.set_attenuation(self.atten_level[wc.AP_MAIN])
172        self.brconfigs_main = self.setup_ap_connection(
173            network_main, ap=self.access_point_main)
174        self.log.info('Set attenuation to connect device to the aux AP')
175        self.set_attenuation(self.atten_level[wc.AP_AUX])
176        self.brconfigs_aux = self.setup_ap_connection(network_aux,
177                                                      ap=self.access_point_aux)
178        self.log.info('Forget network {}'.format(network_aux[wc.SSID]))
179        wutils.wifi_forget_network(self.dut, network_aux[wc.SSID])
180        self.log.info('Set attenuation to trigger wedge condition')
181        self.set_attenuation(self.atten_level[self.current_test_name])
182        self.dut.droid.goToSleepNow()
183        self.measure_power_and_validate()
184