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