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 connectivity tests in telephony
18"""
19
20import time
21import json
22import logging
23import os
24
25from acts.test_decorators import test_tracker_info
26from acts.controllers.anritsu_lib._anritsu_utils import AnritsuError
27from acts.controllers.anritsu_lib.md8475a import MD8475A
28from acts.controllers.anritsu_lib.md8475a import BtsBandwidth
29from acts.controllers.anritsu_lib.md8475a import VirtualPhoneStatus
30from acts.test_utils.tel.anritsu_utils import cb_serial_number
31from acts.test_utils.tel.anritsu_utils import set_system_model_1x
32from acts.test_utils.tel.anritsu_utils import set_system_model_gsm
33from acts.test_utils.tel.anritsu_utils import set_system_model_lte
34from acts.test_utils.tel.anritsu_utils import set_system_model_lte_wcdma
35from acts.test_utils.tel.anritsu_utils import set_system_model_wcdma
36from acts.test_utils.tel.anritsu_utils import sms_mo_send
37from acts.test_utils.tel.anritsu_utils import sms_mt_receive_verify
38from acts.test_utils.tel.anritsu_utils import set_usim_parameters
39from acts.test_utils.tel.anritsu_utils import set_post_sim_params
40from acts.test_utils.tel.tel_defines import DIRECTION_MOBILE_ORIGINATED
41from acts.test_utils.tel.tel_defines import DIRECTION_MOBILE_TERMINATED
42from acts.test_utils.tel.tel_defines import NETWORK_MODE_CDMA
43from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_ONLY
44from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_UMTS
45from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_GSM_WCDMA
46from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_CDMA_EVDO
47from acts.test_utils.tel.tel_defines import RAT_1XRTT
48from acts.test_utils.tel.tel_defines import RAT_GSM
49from acts.test_utils.tel.tel_defines import RAT_LTE
50from acts.test_utils.tel.tel_defines import RAT_WCDMA
51from acts.test_utils.tel.tel_defines import RAT_FAMILY_CDMA2000
52from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM
53from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
54from acts.test_utils.tel.tel_defines import RAT_FAMILY_UMTS
55from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
56from acts.test_utils.tel.tel_defines import GEN_4G
57from acts.test_utils.tel.tel_test_utils import ensure_network_rat
58from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
59from acts.test_utils.tel.tel_test_utils import ensure_network_generation
60from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
61from acts.test_utils.tel.tel_test_utils import iperf_test_by_adb
62from acts.test_utils.tel.tel_test_utils import start_qxdm_loggers
63from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
64from acts.utils import adb_shell_ping
65from acts.utils import rand_ascii_str
66from acts.controllers import iperf_server
67from acts.utils import exe_cmd
68
69DEFAULT_PING_DURATION = 30
70
71
72class TelLabDataTest(TelephonyBaseTest):
73    SETTLING_TIME = 30
74    SERIAL_NO = cb_serial_number()
75
76    def __init__(self, controllers):
77        TelephonyBaseTest.__init__(self, controllers)
78        self.ad = self.android_devices[0]
79        self.ip_server = self.iperf_servers[0]
80        self.port_num = self.ip_server.port
81        self.log.info("Iperf Port is %s", self.port_num)
82        self.ad.sim_card = getattr(self.ad, "sim_card", None)
83        self.log.info("SIM Card is %s", self.ad.sim_card)
84        self.md8475a_ip_address = self.user_params[
85            "anritsu_md8475a_ip_address"]
86        self.wlan_option = self.user_params.get("anritsu_wlan_option", False)
87        self.step_size = self.user_params.get("power_step_size", 5)
88        self.start_power_level = self.user_params.get("start_power_level", -40)
89        self.stop_power_level = self.user_params.get("stop_power_level", -100)
90        self.lte_bandwidth = self.user_params.get("lte_bandwidth", 20)
91        self.MAX_ITERATIONS = abs(int((self.stop_power_level - \
92                                 self.start_power_level) / self.step_size))
93        self.log.info("Max iterations is %d", self.MAX_ITERATIONS)
94
95    def setup_class(self):
96        try:
97            self.anritsu = MD8475A(self.md8475a_ip_address, self.log,
98                                   self.wlan_option)
99        except AnritsuError:
100            self.log.error("Error in connecting to Anritsu Simulator")
101            return False
102        return True
103
104    def setup_test(self):
105        if getattr(self, "qxdm_log", True):
106            start_qxdm_loggers(self.log, self.android_devices)
107        ensure_phones_idle(self.log, self.android_devices)
108        toggle_airplane_mode(self.log, self.ad, True)
109        return True
110
111    def teardown_test(self):
112        self.log.info("Stopping Simulation")
113        self.anritsu.stop_simulation()
114        toggle_airplane_mode(self.log, self.ad, True)
115        return True
116
117    def teardown_class(self):
118        self.anritsu.disconnect()
119        return True
120
121    def _setup_data(self, set_simulation_func, rat):
122        try:
123            [self.bts1] = set_simulation_func(self.anritsu, self.user_params,
124                                              self.ad.sim_card)
125            set_usim_parameters(self.anritsu, self.ad.sim_card)
126            set_post_sim_params(self.anritsu, self.user_params,
127                                self.ad.sim_card)
128            if self.lte_bandwidth == 20:
129                self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_20MHz
130            elif self.lte_bandwidth == 15:
131                self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_15MHz
132            elif self.lte_bandwidth == 10:
133                self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_10MHz
134            else:
135                self.bts1.bandwidth = BtsBandwidth.LTE_BANDWIDTH_5MHz
136
137            self.anritsu.start_simulation()
138
139            if rat == RAT_LTE:
140                preferred_network_setting = NETWORK_MODE_LTE_CDMA_EVDO
141                rat_family = RAT_FAMILY_LTE
142            elif rat == RAT_WCDMA:
143                preferred_network_setting = NETWORK_MODE_GSM_UMTS
144                rat_family = RAT_FAMILY_UMTS
145            elif rat == RAT_GSM:
146                preferred_network_setting = NETWORK_MODE_GSM_ONLY
147                rat_family = RAT_FAMILY_GSM
148            elif rat == RAT_1XRTT:
149                preferred_network_setting = NETWORK_MODE_CDMA
150                rat_family = RAT_FAMILY_CDMA2000
151            else:
152                self.log.error("No valid RAT provided for SMS test.")
153                return False
154
155            if not ensure_network_rat(
156                    self.log,
157                    self.ad,
158                    preferred_network_setting,
159                    rat_family,
160                    toggle_apm_after_setting=True):
161                self.log.error(
162                    "Failed to set rat family {}, preferred network:{}".format(
163                        rat_family, preferred_network_setting))
164                return False
165
166            self.anritsu.wait_for_registration_state()
167            time.sleep(self.SETTLING_TIME)
168
169            # Fetch IP address of the host machine
170            cmd = "|".join(("ifconfig", "grep eth0 -A1", "grep inet",
171                            "cut -d ':' -f2", "cut -d ' ' -f 1"))
172            destination_ip = exe_cmd(cmd)
173            destination_ip = (destination_ip.decode("utf-8")).split("\n")[0]
174            self.log.info("Dest IP is %s", destination_ip)
175
176            if not adb_shell_ping(self.ad, DEFAULT_PING_DURATION,
177                                  destination_ip):
178                self.log.error("Pings failed to Destination.")
179                return False
180            self.bts1.output_level = self.start_power_level
181
182            # Power, iperf, file output, power change
183            for iteration in range(1, self.MAX_ITERATIONS + 1):
184                self.log.info("------- Current Iteration: %d / %d -------",
185                              iteration, self.MAX_ITERATIONS)
186                current_power = self.bts1.output_level
187                self.log.info("Current Power Level is %s", current_power)
188
189                self.ip_server.start()
190                tput_dict = {"Uplink": 0, "Downlink": 0}
191                if iperf_test_by_adb(
192                        self.log,
193                        self.ad,
194                        destination_ip,
195                        self.port_num,
196                        True,
197                        10,
198                        rate_dict=tput_dict):
199                    uplink = tput_dict["Uplink"]
200                    downlink = tput_dict["Downlink"]
201                else:
202                    self.log.error("iperf failed to Destination.")
203                    self.log.info("Iteration %d Failed", iteration)
204                    if float(current_power) < -55.0:
205                        return True
206                    else:
207                        return False
208                self.ip_server.stop()
209
210                self.log.info("Iteration %d Passed", iteration)
211                self.logpath = os.path.join(logging.log_path, "power_tput.txt")
212                line = "Power " + current_power + " DL TPUT " + str(downlink)
213                with open(self.logpath, "a") as tput_file:
214                    tput_file.write(line)
215                    tput_file.write("\n")
216                current_power = float(current_power)
217                new_power = current_power - self.step_size
218                self.log.info("Setting Power Level to %f", new_power)
219                self.bts1.output_level = new_power
220
221        except AnritsuError as e:
222            self.log.error("Error in connection with Anritsu Simulator: " +
223                           str(e))
224            return False
225        except Exception as e:
226            self.log.error("Exception during Data procedure: " + str(e))
227            return False
228        return True
229
230    """ Tests Begin """
231
232    @test_tracker_info(uuid="df40279a-46dc-40ee-9205-bce2d0fba7e8")
233    @TelephonyBaseTest.tel_test_wrap
234    def test_lte_pings_iperf(self):
235        """ Test Pings functionality on LTE
236
237        Make Sure Phone is in LTE mode
238        Ping to destination server IP
239        iperf server on host machine
240        iperf client in on adb
241        iperf DL
242
243        Returns:
244            True if pass; False if fail
245        """
246        return self._setup_data(set_system_model_lte, RAT_LTE)
247
248    """ Tests End """
249