1#!/usr/bin/env python3
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
20
21from acts.test_decorators import test_tracker_info
22from acts.controllers.anritsu_lib._anritsu_utils import AnritsuError
23from acts.controllers.anritsu_lib.md8475a import MD8475A
24from acts.controllers.anritsu_lib.md8475a import BtsNumber
25from acts.test_utils.tel.anritsu_utils import WAIT_TIME_ANRITSU_REG_AND_CALL
26from acts.test_utils.tel.anritsu_utils import handover_tc
27from acts.test_utils.tel.anritsu_utils import make_ims_call
28from acts.test_utils.tel.anritsu_utils import tear_down_call
29from acts.test_utils.tel.anritsu_utils import set_system_model_lte_lte
30from acts.test_utils.tel.anritsu_utils import set_system_model_lte_wcdma
31from acts.test_utils.tel.anritsu_utils import set_system_model_lte_gsm
32from acts.test_utils.tel.anritsu_utils import set_system_model_lte_1x
33from acts.test_utils.tel.anritsu_utils import set_system_model_lte_evdo
34from acts.test_utils.tel.anritsu_utils import set_usim_parameters
35from acts.test_utils.tel.anritsu_utils import set_post_sim_params
36from acts.test_utils.tel.tel_defines import CALL_TEARDOWN_PHONE
37from acts.test_utils.tel.tel_defines import RAT_FAMILY_CDMA2000
38from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM
39from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
40from acts.test_utils.tel.tel_defines import RAT_FAMILY_UMTS
41from acts.test_utils.tel.tel_defines import RAT_1XRTT
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_CDMA_EVDO
46from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA
47from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_GSM_WCDMA
48from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL
49from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL_FOR_IMS
50from acts.test_utils.tel.tel_test_utils import ensure_network_rat
51from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
52from acts.test_utils.tel.tel_test_utils import get_host_ip_address
53from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode_by_adb
54from acts.test_utils.tel.tel_test_utils import toggle_volte
55from acts.test_utils.tel.tel_test_utils import run_multithread_func
56from acts.test_utils.tel.tel_test_utils import iperf_test_by_adb
57from acts.test_utils.tel.tel_test_utils import set_preferred_apn_by_adb
58from acts.test_utils.tel.tel_test_utils import start_qxdm_loggers
59from acts.test_utils.tel.tel_voice_utils import phone_idle_volte
60from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
61from acts.utils import adb_shell_ping
62from acts.utils import rand_ascii_str
63from acts.controllers import iperf_server
64from acts.utils import exe_cmd
65
66DEFAULT_CALL_NUMBER = "+11234567891"
67DEFAULT_PING_DURATION = 5
68WAITTIME_BEFORE_HANDOVER = 20
69WAITTIME_AFTER_HANDOVER = 20
70
71
72class TelLabMobilityTest(TelephonyBaseTest):
73    def setup_class(self):
74        super().setup_class()
75        self.ad = self.android_devices[0]
76        self.ad.sim_card = getattr(self.ad, "sim_card", None)
77        self.md8475a_ip_address = self.user_params[
78            "anritsu_md8475a_ip_address"]
79        self.wlan_option = self.user_params.get("anritsu_wlan_option", False)
80        self.md8475_version = self.user_params.get("md8475", "A")
81        self.voice_call_number = self.user_params.get('voice_call_number',
82                                                      DEFAULT_CALL_NUMBER)
83        self.ip_server = self.iperf_servers[0]
84        self.port_num = self.ip_server.port
85        self.log.info("Iperf Port is %s", self.port_num)
86        if self.ad.sim_card == "VzW12349":
87            set_preferred_apn_by_adb(self.ad, "VZWINTERNET")
88
89        try:
90            self.anritsu = MD8475A(self.md8475a_ip_address, self.wlan_option,
91                                   self.md8475_version)
92        except AnritsuError:
93            self.log.error("Error in connecting to Anritsu Simulator")
94            return False
95        return True
96
97    def setup_test(self):
98        try:
99            if getattr(self, "qxdm_log", True):
100                start_qxdm_loggers(self.log, self.android_devices)
101            self.ad.droid.telephonyFactoryReset()
102        except Exception as e:
103            self.ad.log.error(e)
104        toggle_airplane_mode_by_adb(self.log, self.ad, True)
105        self.ad.adb.shell(
106            "setprop net.lte.ims.volte.provisioned 1", ignore_status=True)
107        # get a handle to virtual phone
108        self.virtualPhoneHandle = self.anritsu.get_VirtualPhone()
109        return True
110
111    def teardown_test(self):
112        self.log.info("Stopping Simulation")
113        self.anritsu.stop_simulation()
114        toggle_airplane_mode_by_adb(self.log, self.ad, True)
115        return True
116
117    def teardown_class(self):
118        self.anritsu.disconnect()
119        return True
120
121    def active_handover(self,
122                        set_simulation_func,
123                        phone_setup_func,
124                        phone_idle_func_after_registration=None,
125                        volte=True,
126                        iperf=True,
127                        all_bands=False,
128                        is_wait_for_registration=True,
129                        voice_number=DEFAULT_CALL_NUMBER,
130                        teardown_side=CALL_TEARDOWN_PHONE,
131                        wait_time_in_call=WAIT_TIME_IN_CALL):
132        try:
133            bts = set_simulation_func(self.anritsu, self.user_params,
134                                      self.ad.sim_card)
135            set_usim_parameters(self.anritsu, self.ad.sim_card)
136            set_post_sim_params(self.anritsu, self.user_params,
137                                self.ad.sim_card)
138            self.anritsu.start_simulation()
139            self.anritsu.send_command("IMSSTARTVN 1")
140            self.anritsu.send_command("IMSSTARTVN 2")
141            self.anritsu.send_command("IMSSTARTVN 3")
142            # turn off all other BTS to ensure UE registers on BTS1
143            simmodel = self.anritsu.get_simulation_model().split(',')
144            no_of_bts = len(simmodel)
145            for i in range(2, no_of_bts + 1):
146                self.anritsu.send_command("OUTOFSERVICE OUT,BTS{}".format(i))
147            if phone_setup_func is not None:
148                if not phone_setup_func(self.ad):
149                    self.log.warning("phone_setup_func failed. Rebooting UE")
150                    self.ad.reboot()
151                    time.sleep(30)
152                    if self.ad.sim_card == "VzW12349":
153                        set_preferred_apn_by_adb(self.ad, "VZWINTERNET")
154                    if not phone_setup_func(self.ad):
155                        self.log.error("phone_setup_func failed.")
156            if is_wait_for_registration:
157                self.anritsu.wait_for_registration_state()
158            if phone_idle_func_after_registration:
159                if not phone_idle_func_after_registration(self.log, self.ad):
160                    self.log.error("phone_idle_func failed.")
161            for i in range(2, no_of_bts + 1):
162                self.anritsu.send_command("OUTOFSERVICE IN,BTS{}".format(i))
163            time.sleep(WAIT_TIME_ANRITSU_REG_AND_CALL)
164            if iperf:  # setup iPerf server
165                server_ip = self.iperf_setup()
166                if not server_ip:
167                    self.log.error("iperf server can not be reached by ping")
168                    return False
169            if volte:  # make a VoLTE MO call
170                if not make_ims_call(self.log, self.ad, self.anritsu,
171                                     voice_number):
172                    self.log.error("Phone {} Failed to make volte call to {}"
173                                   .format(self.ad.serial, voice_number))
174                    return False
175            if all_bands and (simmodel[1] == "WCDMA"):
176                band = []
177                for rat in simmodel[:2]:
178                    band.append(self.anritsu.get_supported_bands(rat))
179                self.log.info("UE reported LTE bands are {}".format(band[0]))
180                self.log.info("UE reported WCDMA bands are {}".format(band[1]))
181                current_lte_band = bts[0].band
182                # move current LTE band to the last in the list
183                band[0].remove(current_lte_band)
184                band[0].append(current_lte_band)
185                n = max(len(band[0]), len(band[1]))
186            else:
187                n = 1  # n is the number of LTE->WCDMA->LTE handovers
188
189            for i in range(n):
190                if all_bands:
191                    bts[1].band = band[1][i % len(band[1])]
192                if not iperf:  # VoLTE only
193                    result = handover_tc(self.log, self.anritsu,
194                                         WAITTIME_BEFORE_HANDOVER,
195                                         BtsNumber.BTS1, BtsNumber.BTS2)
196                    time.sleep(WAITTIME_AFTER_HANDOVER)
197                else:  # with iPerf
198                    iperf_task = (self._iperf_task,
199                                  (server_ip, WAITTIME_BEFORE_HANDOVER +
200                                   WAITTIME_AFTER_HANDOVER - 10))
201                    ho_task = (handover_tc, (self.log, self.anritsu,
202                                             WAITTIME_BEFORE_HANDOVER,
203                                             BtsNumber.BTS1, BtsNumber.BTS2))
204                    result = run_multithread_func(self.log,
205                                                  [ho_task, iperf_task])
206                    if not result[1]:
207                        self.log.error("iPerf failed.")
208                        return False
209
210                self.log.info(
211                    "handover test case result code {}.".format(result[0]))
212                if volte:
213                    # check if the phone stay in call
214                    if not self.ad.droid.telecomIsInCall():
215                        self.log.error("Call is already ended in the phone.")
216                        return False
217
218                    if not tear_down_call(self.log, self.ad, self.anritsu):
219                        self.log.error("Phone {} Failed to tear down"
220                                       .format(self.ad.serial, voice_number))
221                        return False
222                if simmodel[1] == "WCDMA" and iperf:
223                    if all_bands:
224                        bts[0].band = band[0][i % len(band[0])]
225                    iperf_task = (self._iperf_task,
226                                  (server_ip, WAITTIME_BEFORE_HANDOVER +
227                                   WAITTIME_AFTER_HANDOVER - 10))
228                    ho_task = (handover_tc, (self.log, self.anritsu,
229                                             WAITTIME_BEFORE_HANDOVER,
230                                             BtsNumber.BTS2, BtsNumber.BTS1))
231                    result = run_multithread_func(self.log,
232                                                  [ho_task, iperf_task])
233                    if not result[1]:
234                        self.log.error("iPerf failed.")
235                        return False
236                    self.log.info(
237                        "handover test case result code {}.".format(result[0]))
238
239        except AnritsuError as e:
240            self.log.error("Error in connection with Anritsu Simulator: " +
241                           str(e))
242            return False
243        except Exception as e:
244            self.log.error("Exception during voice call procedure: " + str(e))
245            return False
246        return True
247
248    def iperf_setup(self):
249        # Fetch IP address of the host machine
250        destination_ip = get_host_ip_address(self)
251
252        if not adb_shell_ping(self.ad, DEFAULT_PING_DURATION, destination_ip):
253            self.log.error("Pings failed to Destination.")
254            return False
255
256        return destination_ip
257
258    def _iperf_task(self, destination_ip, duration):
259        self.log.info("Starting iPerf task")
260        self.ip_server.start()
261        tput_dict = {"Uplink": 0, "Downlink": 0}
262        if iperf_test_by_adb(
263                self.log,
264                self.ad,
265                destination_ip,
266                self.port_num,
267                True,  # reverse = true
268                duration,
269                rate_dict=tput_dict):
270            uplink = tput_dict["Uplink"]
271            downlink = tput_dict["Downlink"]
272            self.ip_server.stop()
273            return True
274        else:
275            self.log.error("iperf failed to Destination.")
276            self.ip_server.stop()
277            return False
278
279    def _phone_setup_lte_wcdma(self, ad):
280        return ensure_network_rat(
281            self.log,
282            ad,
283            NETWORK_MODE_LTE_GSM_WCDMA,
284            RAT_FAMILY_LTE,
285            toggle_apm_after_setting=True)
286
287    def _phone_setup_lte_1x(self, ad):
288        return ensure_network_rat(
289            self.log,
290            ad,
291            NETWORK_MODE_LTE_CDMA_EVDO,
292            RAT_FAMILY_LTE,
293            toggle_apm_after_setting=True)
294
295    def _phone_setup_wcdma(self, ad):
296        return ensure_network_rat(
297            self.log,
298            ad,
299            NETWORK_MODE_GSM_UMTS,
300            RAT_FAMILY_UMTS,
301            toggle_apm_after_setting=True)
302
303    def _phone_setup_gsm(self, ad):
304        return ensure_network_rat(
305            self.log,
306            ad,
307            NETWORK_MODE_GSM_ONLY,
308            RAT_FAMILY_GSM,
309            toggle_apm_after_setting=True)
310
311    def _phone_setup_1x(self, ad):
312        return ensure_network_rat(
313            self.log,
314            ad,
315            NETWORK_MODE_CDMA,
316            RAT_FAMILY_CDMA2000,
317            toggle_apm_after_setting=True)
318
319    def _phone_setup_airplane_mode(self, ad):
320        return toggle_airplane_mode_by_adb(self.log, ad, True)
321
322    def _phone_setup_volte_airplane_mode(self, ad):
323        toggle_volte(self.log, ad, True)
324        return toggle_airplane_mode_by_adb(self.log, ad, True)
325
326    def _phone_setup_volte(self, ad):
327        ad.droid.telephonyToggleDataConnection(True)
328        toggle_volte(self.log, ad, True)
329        return ensure_network_rat(
330            self.log,
331            ad,
332            NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA,
333            RAT_FAMILY_LTE,
334            toggle_apm_after_setting=True)
335
336    """ Tests Begin """
337
338    @test_tracker_info(uuid="bd014822-2c09-4503-9e01-594513ea6808")
339    @TelephonyBaseTest.tel_test_wrap
340    def test_volte_iperf_handover(self):
341        """ Test VoLTE to VoLTE Inter-Freq handover with iPerf data
342        Steps:
343        1. Setup CallBox for 2 LTE cells with 2 different bands.
344        2. Turn on DUT and enable VoLTE. Make an voice call to DEFAULT_CALL_NUMBER.
345        3. Check if VoLTE voice call connected successfully.
346        4. Start iPerf data transfer
347        5. Handover the call to BTS2 and check if the call is still up.
348        6. Check iPerf data throughput
349        7. Tear down the call.
350
351        Expected Results:
352        1. VoLTE Voice call is made successfully.
353        2. After handover, the call is not dropped.
354        3. Tear down call succeed.
355
356        Returns:
357            True if pass; False if fail
358        """
359        return self.active_handover(
360            set_system_model_lte_lte,
361            self._phone_setup_volte,
362            phone_idle_volte,
363            volte=True,
364            iperf=True)
365
366    @test_tracker_info(uuid="a5a15947-40eb-4a70-b652-0b52a548c3c1")
367    @TelephonyBaseTest.tel_test_wrap
368    def test_volte_handover(self):
369        """ Test VoLTE to VoLTE Inter-Freq handover without iPerf data
370        Steps:
371        1. Setup CallBox for 2 LTE cells with 2 different bands.
372        2. Turn on DUT and enable VoLTE. Make an voice call to DEFAULT_CALL_NUMBER.
373        3. Check if VoLTE voice call connected successfully.
374        4. Handover the call to BTS2 and check if the call is still up.
375        5. Tear down the call.
376
377        Expected Results:
378        1. VoLTE Voice call is made successfully.
379        2. After handover, the call is not dropped.
380        3. Tear down call succeed.
381
382        Returns:
383            True if pass; False if fail
384        """
385        return self.active_handover(
386            set_system_model_lte_lte,
387            self._phone_setup_volte,
388            phone_idle_volte,
389            volte=True,
390            iperf=False)
391
392    @test_tracker_info(uuid="382521d9-d991-49bc-8347-2e766ec0db74")
393    @TelephonyBaseTest.tel_test_wrap
394    def test_iperf_handover(self):
395        """ Test Inter-Freq handover with iPerf data
396        Steps:
397        1. Setup CallBox for 2 LTE cells with 2 different bands.
398        2. Turn on DUT and enable VoLTE.
399        3. Start iPerf data transfer
400        4. Handover the call to BTS2
401        5. Check iPerf data throughput
402
403        Expected Results:
404        1. Data call is made successfully.
405        2. After handover, the data is not dropped.
406
407        Returns:
408            True if pass; False if fail
409        """
410        return self.active_handover(
411            set_system_model_lte_lte,
412            self._phone_setup_volte,
413            phone_idle_volte,
414            volte=False,
415            iperf=True)
416
417    @test_tracker_info(uuid="d255a58b-8697-4d0a-9bc0-1e7ffa4cccaf")
418    @TelephonyBaseTest.tel_test_wrap
419    def test_volte_iperf_handover_wcdma(self):
420        """ Test VoLTE to WCDMA to LTE handover with iPerf data
421        Steps:
422        1. Setup CallBox for LTE and WCDMA simulation.
423        2. Turn on DUT and enable VoLTE. Make an voice call to DEFAULT_CALL_NUMBER.
424        3. Check if VoLTE voice call connected successfully.
425        4. Start iPerf data transfer
426        5. SRVCC to WCDMA and check if the call is still up.
427        6. Check iPerf data throughput
428        7. Tear down the call.
429        8. Handover back to LTE with iPerf
430
431        Expected Results:
432        1. VoLTE Voice call is made successfully.
433        2. After handover, the call is not dropped.
434        3. iPerf continue after handover
435
436        Returns:
437            True if pass; False if fail
438        """
439        return self.active_handover(
440            set_system_model_lte_wcdma,
441            self._phone_setup_volte,
442            phone_idle_volte,
443            volte=True,
444            iperf=True)
445
446    @test_tracker_info(uuid="28bc2e85-602e-4143-afe7-6dd442bef5c8")
447    @TelephonyBaseTest.tel_test_wrap
448    def test_volte_handover_wcdma(self):
449        """ Test VoLTE to WCDMA handover (SRVCC)
450        Steps:
451        1. Setup CallBox for LTE and WCDMA simulation.
452        2. Turn on DUT and enable VoLTE. Make an voice call to DEFAULT_CALL_NUMBER.
453        3. Check if VoLTE voice call connected successfully.
454        4. Start iPerf data transfer
455        5. Handover the call to BTS2 and check if the call is still up.
456        6. Check iPerf data throughput
457        7. Tear down the call.
458
459        Expected Results:
460        1. VoLTE Voice call is made successfully.
461        2. After handover, the call is not dropped.
462        3. Tear down call succeed.
463
464        Returns:
465            True if pass; False if fail
466        """
467        return self.active_handover(
468            set_system_model_lte_wcdma,
469            self._phone_setup_volte,
470            phone_idle_volte,
471            volte=True,
472            iperf=False)
473
474    @test_tracker_info(uuid="3ef15650-8e44-4b75-b809-8d7dec5a41e3")
475    @TelephonyBaseTest.tel_test_wrap
476    def test_iperf_handover_wcdma(self):
477        """ Test LTE to WCDMA to LTE handovers with iPerf data
478        Steps:
479        1. Setup CallBox for LTE and WCDMA simulation.
480        2. Turn on DUT and register on LTE BTS.
481        3. Start iPerf data transfer
482        4. Handover to WCDMA.
483        5. Stop and check iPerf data throughput
484        6. Start iPerf data transfer
485        7. Handover to WCDMA.
486        8. Stop and check iPerf data throughput
487
488
489        Expected Results:
490        1. Each handover is successful
491        2. After each handover, the iPerf continues successfully.
492
493        Returns:
494            True if pass; False if fail
495        """
496        return self.active_handover(
497            set_system_model_lte_wcdma,
498            self._phone_setup_volte,
499            phone_idle_volte,
500            volte=False,
501            iperf=True)
502
503    @test_tracker_info(uuid="2bfad82d-1797-474b-9bf7-c14602b061cd")
504    @TelephonyBaseTest.tel_test_wrap
505    def test_iperf_handover_wcdma_all_bands(self):
506        """ Test LTE->WCDMA->LTE handovers through all bands UE supports with iPerf data
507        Steps:
508        1. Setup CallBox for LTE and WCDMA simulation.
509        2. Turn on DUT and register on LTE BTS.
510        3. Query MD8475A for UE supported bands contained in UE Capability Information
511        4. Set target WCDMA band with first band in WCDMA supported band list
512        5. Start iPerf data transfer
513        6. Handover to WCDMA.
514        7. Stop and check iPerf data throughput
515        8. Set target LTE band with first band in LTE supported band list
516        9. Start iPerf data transfer
517        10. Handover to LTE.
518        11. Stop and check iPerf data throughput
519        12. Repeat step 4-11 with second WCDMA/LTE bands in supported band lists
520        13. Repeat step 12 until all bands are tested. Reuse the begining of the shorter list to match the longer list.
521
522        Expected Results:
523        1. Each handover is successful
524        2. After each handover, the iPerf continues successfully.
525
526        Returns:
527            True if pass; False if fail
528        """
529        return self.active_handover(
530            set_system_model_lte_wcdma,
531            self._phone_setup_volte,
532            phone_idle_volte,
533            volte=False,
534            iperf=True,
535            all_bands=True)
536
537    """ Tests End """
538