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 acts.test_utils.power.PowerBaseTest as PBT
18from acts.test_utils.wifi import wifi_test_utils as wutils
19from acts.test_utils.wifi import wifi_power_test_utils as wputils
20
21IPERF_DURATION = 'iperf_duration'
22INITIAL_ATTEN = [0, 0, 90, 90]
23
24
25class PowerWiFiBaseTest(PBT.PowerBaseTest):
26    """Base class for WiFi power related tests.
27
28    Inherited from the PowerBaseTest class
29    """
30
31    def setup_class(self):
32
33        super().setup_class()
34        if hasattr(self, 'access_points'):
35            self.access_point = self.access_points[0]
36            self.access_point_main = self.access_points[0]
37            if len(self.access_points) > 1:
38                self.access_point_aux = self.access_points[1]
39        if hasattr(self, 'attenuators'):
40            self.set_attenuation(INITIAL_ATTEN)
41        if hasattr(self, 'network_file'):
42            self.networks = self.unpack_custom_file(self.network_file, False)
43            self.main_network = self.networks['main_network']
44            self.aux_network = self.networks['aux_network']
45        if hasattr(self, 'packet_senders'):
46            self.pkt_sender = self.packet_senders[0]
47        if hasattr(self, 'iperf_servers'):
48            self.iperf_server = self.iperf_servers[0]
49        if self.iperf_duration:
50            self.mon_duration = self.iperf_duration - 10
51            self.create_monsoon_info()
52
53    def teardown_test(self):
54        """Tear down necessary objects after test case is finished.
55
56        Bring down the AP interface, delete the bridge interface, stop the
57        packet sender, and reset the ethernet interface for the packet sender
58        """
59        super().teardown_test()
60        if hasattr(self, 'pkt_sender'):
61            self.pkt_sender.stop_sending(ignore_status=True)
62        if hasattr(self, 'brconfigs'):
63            self.access_point.bridge.teardown(self.brconfigs)
64            delattr(self, 'brconfigs')
65        if hasattr(self, 'brconfigs_main'):
66            self.access_point_main.bridge.teardown(self.brconfigs_main)
67            delattr(self, 'brconfigs_main')
68        if hasattr(self, 'brconfigs_aux'):
69            self.access_point_aux.bridge.teardown(self.brconfigs_aux)
70            delattr(self, 'brconfigs_aux')
71        if hasattr(self, 'access_points'):
72            for ap in self.access_points:
73                ap.close()
74        if hasattr(self, 'pkt_sender'):
75            wputils.reset_host_interface(self.pkt_sender.interface)
76        if hasattr(self, 'iperf_server'):
77            self.iperf_server.stop()
78
79    def teardown_class(self):
80        """Clean up the test class after tests finish running
81
82        """
83        super().teardown_class()
84        if hasattr(self, 'access_points'):
85            for ap in self.access_points:
86                ap.close()
87
88    def setup_ap_connection(self, network, bandwidth=80, connect=True,
89                            ap=None):
90        """Setup AP and connect DUT to it.
91
92        Args:
93            network: the network config for the AP to be setup
94            bandwidth: bandwidth of the WiFi network to be setup
95            connect: indicator of if connect dut to the network after setup
96            ap: access point object, default is None to find the main AP
97        Returns:
98            self.brconfigs: dict for bridge interface configs
99        """
100        wutils.wifi_toggle_state(self.dut, True)
101        if not ap:
102            if hasattr(self, 'access_points'):
103                self.brconfigs = wputils.ap_setup(
104                    self.access_point, network, bandwidth=bandwidth)
105        else:
106            self.brconfigs = wputils.ap_setup(ap, network, bandwidth=bandwidth)
107        if connect:
108            wutils.wifi_connect(self.dut, network, num_of_tries=3)
109
110        if ap or (not ap and hasattr(self, 'access_points')):
111            return self.brconfigs
112
113    def collect_power_data(self):
114        """Measure power, plot and check pass/fail.
115
116        If IPERF is run, need to pull iperf results and attach it to the plot.
117        """
118        result = super().collect_power_data()
119        tag = ''
120        if self.iperf_duration:
121            throughput = self.process_iperf_results()
122            tag = '_RSSI_{0:d}dBm_Throughput_{1:.2f}Mbps'.format(
123                self.RSSI, throughput)
124            wputils.monsoon_data_plot(self.mon_info, result, tag=tag)
125        return result
126