1#/usr/bin/env python3.4
2#
3#   Copyright 2016 - 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"""
17Sanity tests for voice tests in telephony
18"""
19import time, os
20
21from acts.test_utils.tel.anritsu_utils import make_ims_call
22from acts.test_utils.tel.anritsu_utils import tear_down_call
23from acts.test_utils.tel.tel_test_utils import iperf_test_by_adb
24from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
25from acts.test_utils.tel.TelephonyLabPowerTest import TelephonyLabPowerTest
26from acts.utils import adb_shell_ping
27from acts.controllers import iperf_server
28from acts.utils import exe_cmd
29import json
30
31DEFAULT_PING_DURATION = 10
32IPERF_DURATION = 30
33IPERF_LOG_FILE_PATH = "/sdcard/iperf.txt"
34
35DEFAULT_CALL_NUMBER = "+11234567891"
36WAIT_TIME_VOLTE = 5
37
38
39class TelLabPowerDataTest(TelephonyLabPowerTest):
40    # TODO Keep if we want to add more in here for this class.
41    def __init__(self, controllers):
42        TelephonyLabPowerTest.__init__(self, controllers)
43        self.ip_server = self.iperf_servers[0]
44        self.port_num = self.ip_server.port
45        self.log.info("Iperf Port is %s", self.port_num)
46        self.log.info("End of __init__ class of TelLabPowerDataTest")
47
48    # May not need
49    def teardown_class(self):
50        # Always take down the simulation
51        TelephonyLabPowerTest.teardown_class(self)
52
53    def iperf_setup(self):
54        # Fetch IP address of the host machine
55        cmd = "|".join(("ifconfig", "grep eth0 -A1", "grep inet",
56                        "cut -d ':' -f2", "cut -d ' ' -f 1"))
57        destination_ip = exe_cmd(cmd)
58        destination_ip = (destination_ip.decode("utf-8")).split("\n")[0]
59        self.log.info("Dest IP is %s", destination_ip)
60        time.sleep(1)
61        if not adb_shell_ping(
62                self.ad, DEFAULT_PING_DURATION, destination_ip,
63                loss_tolerance=95):
64            self.log.error("Pings failed to Destination.")
65            return False
66
67        return destination_ip
68
69    def _iperf_task(self, destination_ip, duration):
70        self.log.info("Starting iPerf task")
71        self.ip_server.start()
72        tput_dict = {"Uplink": 0, "Downlink": 0}
73        if iperf_test_by_adb(
74                self.log,
75                self.ad,
76                destination_ip,
77                self.port_num,
78                True,  # reverse
79                duration,
80                rate_dict=tput_dict,
81                blocking=False,
82                log_file_path=IPERF_LOG_FILE_PATH):
83            return True
84        else:
85            self.log.error("iperf failed to Destination.")
86            self.ip_server.stop()
87            return False
88
89    def power_iperf_test(self, olvl, rflvl, sch_mode="DYNAMIC", volte=False):
90        if volte:
91            # make a VoLTE MO call
92            self.log.info("DEFAULT_CALL_NUMBER = " + DEFAULT_CALL_NUMBER)
93            if not make_ims_call(self.log, self.ad, self.anritsu,
94                                 DEFAULT_CALL_NUMBER):
95                self.log.error("Phone {} Failed to make volte call to {}"
96                               .format(self.ad.serial, DEFAULT_CALL_NUMBER))
97                return False
98            self.log.info("wait for %d seconds" % WAIT_TIME_VOLTE)
99            time.sleep(WAIT_TIME_VOLTE)
100
101        server_ip = self.iperf_setup()
102        if not server_ip:
103            self.log.error("iperf server can not be reached by ping")
104            return False
105
106        self._iperf_task(server_ip, IPERF_DURATION)
107        self.log.info("Wait for 10 secconds before power measurement")
108        time.sleep(10)
109        self.power_test(olvl, rflvl, sch_mode)
110
111        result = self.ad.adb.shell("cat {}".format(IPERF_LOG_FILE_PATH))
112        if result is not None:
113            data_json = json.loads(''.join(result))
114            rx_rate = data_json['end']['sum_received']['bits_per_second']
115            xfer_time = data_json['end']['sum_received']['seconds']
116            self.ad.log.info('iPerf3 transfer time was %ssecs', xfer_time)
117            self.ad.log.info('iPerf3 download speed is %sbps', rx_rate)
118
119        if volte:
120            # check if the phone is still in call, then tear it down
121            if not self.ad.droid.telecomIsInCall():
122                self.log.error("Call is already ended in the phone.")
123                return False
124            if not tear_down_call(self.log, self.ad, self.anritsu):
125                self.log.error("Phone {} Failed to tear down VoLTE call"
126                               .format(self.ad.serial))
127                return False
128
129        return True
130
131    """ Tests Begin """
132
133    @TelephonyBaseTest.tel_test_wrap
134    def test_data_power_n30_n30(self):
135        """ Test power consumption for iPerf data @ DL/UL -30/-30dBm
136        Steps:
137        1. Assume UE already in Communication mode.
138        2. Initiate iPerf data transfer.
139        3. Set DL/UL power and Dynamic scheduling.
140        4. Measure power consumption.
141
142        Expected Results:
143        1. power consumption measurement is successful
144        2. measurement results is saved accordingly
145
146        Returns:
147            True if pass; False if fail
148        """
149        return self.power_iperf_test(-30, -30)
150
151    @TelephonyBaseTest.tel_test_wrap
152    def test_data_power_n50_n10(self):
153        """ Test power consumption for iPerf data @ DL/UL -50/-10dBm
154        Steps:
155        1. Assume UE already in Communication mode.
156        2. Initiate iPerf data transfer.
157        3. Set DL/UL power and Dynamic scheduling.
158        4. Measure power consumption.
159
160        Expected Results:
161        1. power consumption measurement is successful
162        2. measurement results is saved accordingly
163
164        Returns:
165            True if pass; False if fail
166        """
167        return self.power_iperf_test(-50, -10)
168
169    @TelephonyBaseTest.tel_test_wrap
170    def test_data_power_n70_10(self):
171        """ Test power consumption for iPerf data @ DL/UL -70/+10dBm
172        Steps:
173        1. Assume UE already in Communication mode.
174        2. Initiate iPerf data transfer.
175        3. Set DL/UL power and Dynamic scheduling.
176        4. Measure power consumption.
177
178        Expected Results:
179        1. power consumption measurement is successful
180        2. measurement results is saved accordingly
181
182        Returns:
183            True if pass; False if fail
184        """
185        return self.power_iperf_test(-70, 10)
186
187    @TelephonyBaseTest.tel_test_wrap
188    def test_data_volte_power_n30_n30(self):
189        """ Test power consumption for iPerf data and volte @ DL/UL -30/-30dBm
190        Steps:
191        1. Assume UE already in Communication mode.
192        2. Make MO VoLTE call.
193        3. Initiate iPerf data transfer.
194        4. Set DL/UL power and Dynamic scheduling.
195        5. Measure power consumption.
196
197        Expected Results:
198        1. power consumption measurement is successful
199        2. measurement results is saved accordingly
200
201        Returns:
202            True if pass; False if fail
203        """
204        return self.power_iperf_test(-30, -30, volte=True)
205
206    @TelephonyBaseTest.tel_test_wrap
207    def test_data_volte_power_n50_n10(self):
208        """ Test power consumption for iPerf data and volte @ DL/UL -50/-10dBm
209        Steps:
210        1. Assume UE already in Communication mode.
211        2. Make MO VoLTE call.
212        3. Initiate iPerf data transfer.
213        4. Set DL/UL power and Dynamic scheduling.
214        5. Measure power consumption.
215
216        Expected Results:
217        1. power consumption measurement is successful
218        2. measurement results is saved accordingly
219
220        Returns:
221            True if pass; False if fail
222        """
223        return self.power_iperf_test(-50, -10, volte=True)
224
225    @TelephonyBaseTest.tel_test_wrap
226    def test_data_volte_power_n70_10(self):
227        """ Test power consumption for iPerf data and volte @ DL/UL -70/+10dBm
228        Steps:
229        1. Assume UE already in Communication mode.
230        2. Make MO VoLTE call.
231        3. Initiate iPerf data transfer.
232        4. Set DL/UL power and Dynamic scheduling.
233        5. Measure power consumption.
234
235        Expected Results:
236        1. power consumption measurement is successful
237        2. measurement results is saved accordingly
238
239        Returns:
240            True if pass; False if fail
241        """
242        return self.power_iperf_test(-70, 10, volte=True)
243
244    """ Tests End """
245