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_contrib.test_utils.power.PowerBaseTest as PBT 18from acts_contrib.test_utils.wifi import wifi_test_utils as wutils 19from acts_contrib.test_utils.wifi import wifi_power_test_utils as wputils 20from acts_contrib.test_utils.power import plot_utils 21 22IPERF_DURATION = 'iperf_duration' 23INITIAL_ATTEN = [0, 0, 90, 90] 24IPERF_TAIL = 5 25 26 27class PowerWiFiBaseTest(PBT.PowerBaseTest): 28 """Base class for WiFi power related tests. 29 30 Inherited from the PowerBaseTest class 31 """ 32 def setup_class(self): 33 34 super().setup_class() 35 if hasattr(self, 'access_points'): 36 self.access_point = self.access_points[0] 37 self.access_point_main = self.access_points[0] 38 if len(self.access_points) > 1: 39 self.access_point_aux = self.access_points[1] 40 if hasattr(self, 'attenuators'): 41 self.set_attenuation(INITIAL_ATTEN) 42 if hasattr(self, 'network_file'): 43 self.networks = self.unpack_custom_file(self.network_file, False) 44 self.main_network = self.networks['main_network'] 45 self.aux_network = self.networks['aux_network'] 46 if hasattr(self, 'packet_senders'): 47 self.pkt_sender = self.packet_senders[0] 48 if hasattr(self, 'iperf_servers'): 49 self.iperf_server = self.iperf_servers[0] 50 if self.iperf_duration: 51 self.mon_duration = self.iperf_duration - self.mon_offset - IPERF_TAIL 52 self.mon_info = self.create_monsoon_info() 53 54 wutils.set_wifi_country_code(self.dut, 'US') 55 56 def teardown_test(self): 57 """Tear down necessary objects after test case is finished. 58 59 Bring down the AP interface, delete the bridge interface, stop the 60 packet sender, and reset the ethernet interface for the packet sender 61 """ 62 super().teardown_test() 63 if hasattr(self, 'pkt_sender'): 64 self._safe_teardown('pkt_sender stop sending', 65 self.pkt_sender.stop_sending, 66 ignore_status=True) 67 if hasattr(self, 'brconfigs'): 68 self._safe_teardown('brconfigs', self.access_point.bridge.teardown, 69 self.brconfigs) 70 delattr(self, 'brconfigs') 71 if hasattr(self, 'brconfigs_main'): 72 self._safe_teardown('brconfigs_main', 73 self.access_point_main.bridge.teardown, 74 self.brconfigs_main) 75 delattr(self, 'brconfigs_main') 76 if hasattr(self, 'brconfigs_aux'): 77 self._safe_teardown('brconfigs_aux', 78 self.access_point_aux.bridge.teardown, 79 self.brconfigs_aux) 80 delattr(self, 'brconfigs_aux') 81 if hasattr(self, 'access_points'): 82 for ap in self.access_points: 83 self._safe_teardown('access point {}'.format(ap.identifier), 84 ap.close) 85 if hasattr(self, 'pkt_sender'): 86 self._safe_teardown('pkt_sender reset host interface', 87 wputils.reset_host_interface, 88 self.pkt_sender.interface) 89 if hasattr(self, 'iperf_server'): 90 self._safe_teardown('iperf_server', self.iperf_server.stop); 91 92 def _safe_teardown(self, attr, teardown_method, *arg, **kwargs): 93 """Teardown the object with try block. 94 95 Adds a try block for each teardown step to make sure that each 96 teardown step is executed. 97 98 Args: 99 attr: the teardown attribute description for logging 100 teardown_method: the method for teardown 101 *arg: positional arguments for teardown_method 102 **kwargs: keyword arguments for teardown_method 103 """ 104 try: 105 self.log.info('teardown %s with %s', attr, teardown_method.__name__) 106 teardown_method(*arg, **kwargs) 107 except Exception as e: 108 self.log.warning('teardown of %s fails with %s', attr, e) 109 110 def teardown_class(self): 111 """Clean up the test class after tests finish running 112 113 """ 114 super().teardown_class() 115 if hasattr(self, 'access_points'): 116 for ap in self.access_points: 117 ap.close() 118 119 def setup_ap_connection(self, 120 network, 121 bandwidth=80, 122 connect=True, 123 ap=None, 124 dtim_period=None): 125 """Setup AP and connect DUT to it. 126 127 Args: 128 network: the network config for the AP to be setup 129 bandwidth: bandwidth of the WiFi network to be setup 130 connect: indicator of if connect dut to the network after setup 131 ap: access point object, default is None to find the main AP 132 dtim_period: the dtim period of access point 133 Returns: 134 self.brconfigs: dict for bridge interface configs 135 """ 136 wutils.wifi_toggle_state(self.dut, True) 137 if not dtim_period: 138 dtim_period = self.ap_dtim_period 139 if not ap: 140 if hasattr(self, 'access_points'): 141 self.brconfigs = wputils.ap_setup( 142 self.access_point, 143 network, 144 bandwidth=bandwidth, 145 dtim_period=dtim_period) 146 else: 147 self.brconfigs = wputils.ap_setup( 148 ap, network, bandwidth=bandwidth, dtim_period=dtim_period) 149 if connect: 150 wutils.wifi_connect(self.dut, network, num_of_tries=3) 151 152 if ap or (not ap and hasattr(self, 'access_points')): 153 return self.brconfigs 154 155 def collect_power_data(self): 156 """Measure power, plot and check pass/fail. 157 158 If IPERF is run, need to pull iperf results and attach it to the plot. 159 """ 160 samples = super().collect_power_data() 161 tag = '' 162 if self.iperf_duration: 163 throughput = self.process_iperf_results() 164 plot_title = ('{0}_{1}_{2}_RSSI_{3:d}dBm_Throughput_{4:.2f}' 165 'Mbps'.format(self.test_name, 166 self.dut.model, 167 self.dut.build_info['build_id'], 168 self.RSSI, 169 throughput)) 170 plot_utils.current_waveform_plot(samples, self.mon_voltage, 171 self.mon_info.data_path, 172 plot_title) 173 return samples 174 175 def setup_test(self): 176 """Set up test specific parameters or configs. 177 178 """ 179 super().setup_test() 180 181 wutils.reset_wifi(self.dut) 182 wutils.wifi_toggle_state(self.dut, False) 183 184