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