1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - 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 os
18import threading
19import time
20
21from acts import base_test
22from acts import asserts
23from acts.controllers import adb
24from acts.controllers import monsoon
25from acts.test_decorators import test_tracker_info
26from acts.test_utils.wifi import wifi_test_utils as wutils
27from acts.test_utils.tel import tel_data_utils as tel_utils
28from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_2G
29from acts.test_utils.tel.tel_test_utils import WIFI_CONFIG_APBAND_5G
30from acts.test_utils.tel.tel_test_utils import http_file_download_by_chrome
31from acts.utils import force_airplane_mode
32from acts.utils import set_adaptive_brightness
33from acts.utils import set_ambient_display
34from acts.utils import set_auto_rotate
35from acts.utils import set_location_service
36
37
38class WifiTetheringPowerTest(base_test.BaseTestClass):
39
40    def setup_class(self):
41        self.hotspot_device = self.android_devices[0]
42        self.tethered_devices = self.android_devices[1:]
43        req_params = ("ssid", "password", "url")
44        self.unpack_userparams(req_params)
45        self.network = { "SSID": self.ssid, "password": self.password }
46
47        self.offset = 1 * 60
48        self.hz = 5000
49        self.duration = 9 * 60 + self.offset
50        self.mon_data_path = os.path.join(self.log_path, "Monsoon")
51        self.mon = self.monsoons[0]
52        self.mon.set_voltage(4.2)
53        self.mon.set_max_current(7.8)
54        self.mon.attach_device(self.hotspot_device)
55
56        asserts.assert_true(self.mon.usb("auto"),
57            "Failed to turn USB mode to auto on monsoon.")
58        set_location_service(self.hotspot_device, False)
59        set_adaptive_brightness(self.hotspot_device, False)
60        set_ambient_display(self.hotspot_device, False)
61        self.hotspot_device.adb.shell("settings put system screen_brightness 0")
62        set_auto_rotate(self.hotspot_device, False)
63        wutils.wifi_toggle_state(self.hotspot_device, False)
64        self.hotspot_device.droid.telephonyToggleDataConnection(True)
65        tel_utils.wait_for_cell_data_connection(self.log, self.hotspot_device, True)
66        asserts.assert_true(
67            tel_utils.verify_http_connection(self.log, self.hotspot_device),
68            "HTTP verification failed on cell data connection")
69        for ad in self.tethered_devices:
70            wutils.reset_wifi(ad)
71
72    def teardown_class(self):
73        self.mon.usb("on")
74        wutils.wifi_toggle_state(self.hotspot_device, True)
75
76    def on_fail(self, test_name, begin_time):
77        self.hotspot_device.take_bug_report(test_name, begin_time)
78
79    def on_pass(self, test_name, begin_time):
80        self.hotspot_device.take_bug_report(test_name, begin_time)
81
82    """ Helper functions """
83    def _measure_and_process_result(self):
84        """ Measure the current drawn by the device for the period of
85            self.duration, at the frequency of self.hz.
86        """
87        tag = self.current_test_name
88        result = self.mon.measure_power(self.hz,
89                                        self.duration,
90                                        tag=tag,
91                                        offset=self.offset)
92        asserts.assert_true(result,
93                            "Got empty measurement data set in %s." % tag)
94        self.log.info(repr(result))
95        data_path = os.path.join(self.mon_data_path, "%s.txt" % tag)
96        monsoon.MonsoonData.save_to_text_file([result], data_path)
97        actual_current = result.average_current
98        actual_current_str = "%.2fmA" % actual_current
99        result_extra = {"Average Current": actual_current_str}
100
101    def _start_wifi_tethering(self, wifi_band):
102        """ Start wifi tethering on hotspot device
103
104            Args:
105              1. wifi_band: specifies the wifi band to start the hotspot
106              on. The current options are 2G and 5G
107        """
108        wutils.start_wifi_tethering(self.hotspot_device,
109                                    self.ssid,
110                                    self.password,
111                                    wifi_band)
112
113    def _start_traffic_on_device(self, ad):
114        """ Start traffic on the device by downloading data
115            Run the traffic continuosly for self.duration
116
117            Args:
118              1. ad device object
119        """
120        timeout = time.time() + self.duration
121        while True:
122            if time.time() > timeout:
123                break
124            http_file_download_by_chrome(ad, self.url)
125
126    def _start_traffic_measure_power(self, ad_list):
127        """ Start traffic on the tethered devices and measure power
128
129            Args:
130              1. ad_list: list of tethered devices to run traffic on
131        """
132        threads = []
133        for ad in ad_list:
134            t = threading.Thread(target = self._start_traffic_on_device,
135                                 args = (ad,))
136            t.start()
137            threads.append(t)
138        try:
139            self._measure_and_process_result()
140        finally:
141            for t in threads:
142                t.join()
143
144
145    """ Tests begin """
146    @test_tracker_info(uuid="ebb74144-e22a-46e1-b8c1-9ada22b13133")
147    def test_power_wifi_tethering_2ghz_no_devices_connected(self):
148        """ Steps:
149              1. Start wifi hotspot with 2.4Ghz band
150              2. No devices connected to hotspot
151              3. Measure power
152        """
153        self._start_wifi_tethering(WIFI_CONFIG_APBAND_2G)
154        self._measure_and_process_result()
155        wutils.stop_wifi_tethering(self.hotspot_device)
156
157    @test_tracker_info(uuid="2560c088-4010-4354-ade3-6aaac83b1cfd")
158    def test_power_wifi_tethering_5ghz_no_devices_connected(self):
159        """ Steps:
160              1. Start wifi hotspot with 5Ghz band
161              2. No devices connected to hotspot
162              3. Measure power
163        """
164        self._start_wifi_tethering(WIFI_CONFIG_APBAND_5G)
165        self._measure_and_process_result()
166        wutils.stop_wifi_tethering(self.hotspot_device)
167
168    @test_tracker_info(uuid="644795b0-cd30-4a8f-82ee-cc0618c41c6b")
169    def test_power_wifi_tethering_2ghz_connect_1device(self):
170        """ Steps:
171              1. Start wifi hotspot with 2.4GHz band
172              2. Connect 1 device to hotspot
173              3. Measure power
174        """
175        self._start_wifi_tethering(WIFI_CONFIG_APBAND_2G)
176        wutils.wifi_connect(self.tethered_devices[0], self.network)
177        self._measure_and_process_result()
178        wutils.stop_wifi_tethering(self.hotspot_device)
179
180    @test_tracker_info(uuid="8fca9898-f493-44c3-810f-d2262ac72187")
181    def test_power_wifi_tethering_5ghz_connect_1device(self):
182        """ Steps:
183              1. Start wifi hotspot with 5GHz band
184              2. Connect 1 device to hotspot
185              3. Measure power
186        """
187        self._start_wifi_tethering(WIFI_CONFIG_APBAND_5G)
188        wutils.wifi_connect(self.tethered_devices[0], self.network)
189        self._measure_and_process_result()
190        wutils.stop_wifi_tethering(self.hotspot_device)
191
192    @test_tracker_info(uuid="16ef5f63-1a7a-44ae-bf8d-c3a181c89b63")
193    def test_power_wifi_tethering_2ghz_connect_5devices(self):
194        """ Steps:
195              1. Start wifi hotspot with 2GHz band
196              2. Connect 5 devices to hotspot
197              3. Measure power
198        """
199        self._start_wifi_tethering(WIFI_CONFIG_APBAND_2G)
200        for ad in self.tethered_devices:
201            wutils.wifi_connect(ad, self.network)
202        self._measure_and_process_result()
203        wutils.stop_wifi_tethering(self.hotspot_device)
204
205    @test_tracker_info(uuid="769aedfc-d309-40e0-95dd-51ff40f4e097")
206    def test_power_wifi_tethering_5ghz_connect_5devices(self):
207        """ Steps:
208              1. Start wifi hotspot with 5GHz band
209              2. Connect 5 devices to hotspot
210              3. Measure power
211        """
212        self._start_wifi_tethering(WIFI_CONFIG_APBAND_5G)
213        for ad in self.tethered_devices:
214            wutils.wifi_connect(ad, self.network)
215        self._measure_and_process_result()
216        wutils.stop_wifi_tethering(self.hotspot_device)
217
218    @test_tracker_info(uuid="e5b71f34-1dc0-4045-a45e-48c1e9426ec3")
219    def test_power_wifi_tethering_2ghz_connect_1device_with_traffic(self):
220        """ Steps:
221              1. Start wifi hotspot with 2GHz band
222              2. Connect 1 device to hotspot device
223              3. Start traffic and measure power
224        """
225        self._start_wifi_tethering(WIFI_CONFIG_APBAND_2G)
226        wutils.wifi_connect(self.tethered_devices[0], self.network)
227        self._start_traffic_measure_power(self.tethered_devices[0:1])
228        wutils.stop_wifi_tethering(self.hotspot_device)
229
230    @test_tracker_info(uuid="29c5cd6e-8df1-46e5-a735-526dc9154f6e")
231    def test_power_wifi_tethering_5ghz_connect_1device_with_traffic(self):
232        """ Steps:
233              1. Start wifi hotspot with 5GHz band
234              2. Connect 1 device to hotspot device
235              3. Start traffic and measure power
236        """
237        self._start_wifi_tethering(WIFI_CONFIG_APBAND_5G)
238        wutils.wifi_connect(self.tethered_devices[0], self.network)
239        self._start_traffic_measure_power(self.tethered_devices[0:1])
240        wutils.stop_wifi_tethering(self.hotspot_device)
241
242    @test_tracker_info(uuid="da71b06f-7b98-4c14-a2e2-361f395b39a8")
243    def test_power_wifi_tethering_2ghz_connect_5devices_with_traffic(self):
244        """ Steps:
245              1. Start wifi hotspot with 2GHz band
246              2. Connect 5 devices to hotspot device
247              3. Start traffic and measure power
248        """
249        self._start_wifi_tethering(WIFI_CONFIG_APBAND_2G)
250        for ad in self.tethered_devices:
251            wutils.wifi_connect(ad, self.network)
252        self._start_traffic_measure_power(self.tethered_devices)
253        wutils.stop_wifi_tethering(self.hotspot_device)
254
255    @test_tracker_info(uuid="7f3173ab-fd8f-4579-8c45-f9a8c5cd17f7")
256    def test_power_wifi_tethering_5ghz_connect_5devices_with_traffic(self):
257        """ Steps:
258              1. Start wifi hotspot with 2GHz band
259              2. Connect 5 devices to hotspot device
260              3. Start traffic and measure power
261        """
262        self._start_wifi_tethering(WIFI_CONFIG_APBAND_5G)
263        for ad in self.tethered_devices:
264            wutils.wifi_connect(ad, self.network)
265        self._start_traffic_measure_power(self.tethered_devices)
266        wutils.stop_wifi_tethering(self.hotspot_device)
267