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 time
18from acts.test_decorators import test_tracker_info
19from acts.test_utils.power import PowerWiFiBaseTest as PWBT
20from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_2G
21from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G
22from acts.test_utils.wifi import wifi_test_utils as wutils
23from acts.test_utils.wifi import wifi_power_test_utils as wputils
24from acts.test_utils.power.IperfHelper import IperfHelper
25
26
27class PowerWiFiHotspotTest(PWBT.PowerWiFiBaseTest):
28    """ WiFi Tethering HotSpot Power test.
29
30     Treated as a different case of data traffic. Traffic flows between DUT
31     (android_device[0]) and client device (android_device[1]).
32     The iperf server is always the client device and the iperf client is
33     always the DUT (hotspot)
34
35     """
36
37    # Class config parameters
38    CONFIG_KEY_WIFI = 'hotspot_network'
39
40    # Test name configuration keywords
41    PARAM_WIFI_BAND = 'wifiband'
42    PARAM_2G_BAND = '2g'
43    PARAM_5G_BAND = '5g'
44
45    # Iperf waiting time (margin)
46    IPERF_MARGIN = 10
47
48    def __init__(self, controllers):
49
50        super().__init__(controllers)
51
52        # Initialize values
53        self.client_iperf_helper = None
54        self.iperf_server_address = None
55        self.network = None
56
57    def setup_class(self):
58        """ Executed before the test class is started.
59
60        Configures the Hotspot SSID
61        """
62        super().setup_class()
63
64        # If an SSID and password are indicated in the configuration parameters,
65        # use those. If not, use default parameters and warn the user.
66        if hasattr(self, self.CONFIG_KEY_WIFI):
67
68            self.network = getattr(self, self.CONFIG_KEY_WIFI)
69
70            if not (wutils.WifiEnums.SSID_KEY in self.network
71                    and wutils.WifiEnums.PWD_KEY in self.network):
72                raise RuntimeError(
73                    "The '{}' key in the configuration file needs"
74                    " to contain the '{}' and '{}' fields.".format(
75                        self.CONFIG_KEY_WIFI, wutils.WifiEnums.SSID_KEY,
76                        wutils.WifiEnums.PWD_KEY))
77        else:
78
79            self.log.warning("The configuration file doesn't indicate an SSID "
80                             "password for the hotspot. Using default values. "
81                             "To configured the SSID and pwd include a the key"
82                             " {} containing the '{}' and '{}' fields.".format(
83                                 self.CONFIG_KEY_WIFI,
84                                 wutils.WifiEnums.SSID_KEY,
85                                 wutils.WifiEnums.PWD_KEY))
86
87            self.network = {
88                wutils.WifiEnums.SSID_KEY: 'Pixel_1030',
89                wutils.WifiEnums.PWD_KEY: '1234567890'
90            }
91
92        # Both devices need to have a country code in order
93        # to use the 5 GHz band.
94        wutils.set_wifi_country_code(self.android_devices[0], 'US')
95        wutils.set_wifi_country_code(self.android_devices[1], 'US')
96
97    def setup_test(self):
98        """Set up test specific parameters or configs.
99
100        """
101        super().setup_test()
102        wutils.reset_wifi(self.android_devices[1])
103
104    def setup_hotspot(self, connect_client=False):
105        """Configure Hotspot and connects client device.
106
107        Args:
108            connect_client: Connects (or not) the client device to the Hotspot
109        """
110        try:
111            if self.test_configs.band == self.PARAM_2G_BAND:
112                wifi_band_id = WIFI_CONFIG_APBAND_2G
113            elif self.test_configs.band == self.PARAM_5G_BAND:
114                wifi_band_id = WIFI_CONFIG_APBAND_5G
115            else:
116                raise ValueError()
117        except ValueError:
118            self.log.error(
119                "The test name has to include parameter {} followed by "
120                "either {} or {}.".format(self.PARAM_WIFI_BAND,
121                                          self.PARAM_2G_BAND,
122                                          self.PARAM_5G_BAND))
123            return False
124
125        # Turn WiFi ON for DUT (hotspot) and connect to AP (WiFiSharing)
126        # Hotspot needs airplane mode OFF
127        self.dut.droid.connectivityToggleAirplaneMode(False)
128        time.sleep(2)
129        if self.test_configs.wifi_sharing == 'OFF':
130            wutils.wifi_toggle_state(self.dut, True)
131            time.sleep(2)
132        else:
133            self.setup_ap_connection(
134                self.main_network[self.test_configs.wifi_sharing])
135
136        # Setup tethering on dut
137        wutils.start_wifi_tethering(
138            self.dut, self.network[wutils.WifiEnums.SSID_KEY],
139            self.network[wutils.WifiEnums.PWD_KEY], wifi_band_id)
140
141        # Connect client device to Hotspot
142        if connect_client:
143            wutils.wifi_connect(
144                self.android_devices[1],
145                self.network,
146                check_connectivity=False)
147
148    def measure_power_and_validate(self, traffic=False):
149        """ Measure power and validate.
150
151        Args:
152            traffic: Flag indicating whether or not iperf traffic is running
153        """
154        # Set Screen Status
155        if self.test_configs.screen_status == 'OFF':
156            self.dut.droid.goToSleepNow()
157            self.dut.log.info('Screen is OFF')
158        time.sleep(2)
159
160        # Measure power
161        result = self.collect_power_data()
162
163        if traffic:
164            # Wait for iperf to finish
165            time.sleep(self.IPERF_MARGIN + 2)
166
167            # Process iperf results
168            self.client_iperf_helper.process_iperf_results(
169                self.dut, self.log, self.iperf_servers, self.test_name)
170
171        self.pass_fail_check(result.average_current)
172
173    def power_idle_tethering_test(self):
174        """ Start power test when Hotspot is idle
175
176        Starts WiFi tethering. Hotspot is idle and has (or not) a client
177        connected. Hotspot device can also share WiFi internet
178        """
179        attrs = ['screen_status', 'band', 'client_connect', 'wifi_sharing']
180        indices = [2, 4, 7, 9]
181        self.decode_test_configs(attrs, indices)
182
183        client_connect = self.test_configs.client_connect == 'true'
184
185        # Setup Hotspot with desired config and connect (or not) client
186        self.setup_hotspot(client_connect)
187
188        # Measure power and validate
189        self.measure_power_and_validate(False)
190
191    def power_traffic_tethering_test(self):
192        """ Measure power and throughput during data transmission.
193
194        Starts WiFi tethering and connects the client device.
195        The iperf server is always the client device and the iperf client is
196        always the DUT (hotspot)
197
198        """
199        attrs = [
200            'screen_status', 'band', 'traffic_direction', 'traffic_type',
201            'wifi_sharing'
202        ]
203        indices = [2, 4, 5, 6, 8]
204        self.decode_test_configs(attrs, indices)
205
206        # Setup Hotspot and connect client device
207        self.setup_hotspot(True)
208
209        # Create the iperf config
210        iperf_config = {
211            'traffic_type': self.test_configs.traffic_type,
212            'duration':
213            self.mon_duration + self.mon_offset + self.IPERF_MARGIN,
214            'server_idx': 0,
215            'traffic_direction': self.test_configs.traffic_direction,
216            'port': self.iperf_servers[0].port,
217            'start_meas_time': 4,
218        }
219
220        # Start iperf traffic (dut is the client)
221        self.client_iperf_helper = IperfHelper(iperf_config)
222        self.iperf_servers[0].start()
223        time.sleep(1)
224        self.iperf_server_address = wputils.get_phone_ip(
225            self.android_devices[1])
226        wputils.run_iperf_client_nonblocking(
227            self.dut, self.iperf_server_address,
228            self.client_iperf_helper.iperf_args)
229
230        # Measure power
231        self.measure_power_and_validate(True)
232
233    # Test cases - Idle
234    @test_tracker_info(uuid='95438fcb-4a0d-4528-ad70-d723db8e841c')
235    def test_screen_OFF_wifiband_2g_standby_client_false_wifiSharing_OFF(self):
236        self.power_idle_tethering_test()
237
238    @test_tracker_info(uuid='a2b5ce92-f22f-4442-99be-ff6fa89b1f55')
239    def test_screen_OFF_wifiband_2g_standby_client_true_wifiSharing_OFF(self):
240        self.power_idle_tethering_test()
241
242    @test_tracker_info(uuid='7c1180c0-6ab9-4890-8dbc-eec8a1de2137')
243    def test_screen_OFF_wifiband_2g_standby_client_false_wifiSharing_5g(self):
244        self.power_idle_tethering_test()
245
246    @test_tracker_info(uuid='f9befd44-9096-44d2-a2ca-6bc09d274fc9')
247    def test_screen_OFF_wifiband_2g_standby_client_true_wifiSharing_5g(self):
248        self.power_idle_tethering_test()
249
250    @test_tracker_info(uuid='0a6c1d8d-eb70-4b38-b9ad-511a5c9107ba')
251    def test_screen_OFF_wifiband_2g_standby_client_true_wifiSharing_2g(self):
252        self.power_idle_tethering_test()
253
254    @test_tracker_info(uuid='e9881c0c-2464-4f0b-8602-152eae3c8206')
255    def test_screen_OFF_wifiband_5g_standby_client_false_wifiSharing_OFF(self):
256        self.power_idle_tethering_test()
257
258    @test_tracker_info(uuid='4abb2760-ca85-4489-abb4-54d078016f59')
259    def test_screen_OFF_wifiband_5g_standby_client_true_wifiSharing_OFF(self):
260        self.power_idle_tethering_test()
261
262    @test_tracker_info(uuid='47f99ea5-06aa-4f7a-b4c3-8f397c93be7a')
263    def test_screen_OFF_wifiband_5g_standby_client_false_wifiSharing_2g(self):
264        self.power_idle_tethering_test()
265
266    @test_tracker_info(uuid='5caba4a7-fe7c-4071-bb5e-df7d2585ed64')
267    def test_screen_OFF_wifiband_5g_standby_client_true_wifiSharing_2g(self):
268        self.power_idle_tethering_test()
269
270    @test_tracker_info(uuid='27976823-b1be-4928-9e6c-4631ecfff65c')
271    def test_screen_OFF_wifiband_5g_standby_client_true_wifiSharing_5g(self):
272        self.power_idle_tethering_test()
273
274    # Test cases - Traffic
275    @test_tracker_info(uuid='1b0cd04c-afa5-423a-8f8e-1169d403e93a')
276    def test_screen_OFF_wifiband_5g_DL_TCP_wifiSharing_OFF(self):
277        self.power_traffic_tethering_test()
278
279    @test_tracker_info(uuid='9d1a3be2-22f7-4002-a440-9d50fba6a8c0')
280    def test_screen_OFF_wifiband_5g_UL_TCP_wifiSharing_OFF(self):
281        self.power_traffic_tethering_test()
282
283    @test_tracker_info(uuid='3af59262-7f32-4759-82d0-30b89d448b37')
284    def test_screen_OFF_wifiband_5g_DL_TCP_wifiSharing_5g(self):
285        self.power_traffic_tethering_test()
286
287    @test_tracker_info(uuid='069ebf3c-dc8a-4a71-ad62-e10da3be53cc')
288    def test_screen_OFF_wifiband_5g_UL_TCP_wifiSharing_5g(self):
289        self.power_traffic_tethering_test()
290
291    @test_tracker_info(uuid='f9155204-6441-4a50-a77a-24b063db88f7')
292    def test_screen_OFF_wifiband_5g_DL_TCP_wifiSharing_2g(self):
293        self.power_traffic_tethering_test()
294
295    @test_tracker_info(uuid='145f838a-c07c-4102-b767-96be942f8080')
296    def test_screen_OFF_wifiband_5g_UL_TCP_wifiSharing_2g(self):
297        self.power_traffic_tethering_test()
298
299    @test_tracker_info(uuid='a166e4d1-aaec-4d91-abbe-f8ac4288acd7')
300    def test_screen_OFF_wifiband_2g_DL_TCP_wifiSharing_OFF(self):
301        self.power_traffic_tethering_test()
302
303    @test_tracker_info(uuid='2f3e0cf9-25c6-40e2-8eee-ce3af3b39c92')
304    def test_screen_OFF_wifiband_2g_UL_TCP_wifiSharing_OFF(self):
305        self.power_traffic_tethering_test()
306
307    @test_tracker_info(uuid='08470f44-d25c-432c-9133-f1fd5a30c3b0')
308    def test_screen_OFF_wifiband_2g_DL_TCP_wifiSharing_2g(self):
309        self.power_traffic_tethering_test()
310
311    @test_tracker_info(uuid='0d233720-c5a4-4cef-a47a-9fd9b524031b')
312    def test_screen_OFF_wifiband_2g_UL_TCP_wifiSharing_2g(self):
313        self.power_traffic_tethering_test()
314
315    @test_tracker_info(uuid='ec3a0982-4dcb-4175-9c27-fe1205ad4519')
316    def test_screen_OFF_wifiband_2g_DL_TCP_wifiSharing_5g(self):
317        self.power_traffic_tethering_test()
318
319    @test_tracker_info(uuid='cbd83309-b4dc-44a3-8049-9e0f9275c91e')
320    def test_screen_OFF_wifiband_2g_UL_TCP_wifiSharing_5g(self):
321        self.power_traffic_tethering_test()
322