1#!/usr/bin/env python3
2#
3#   Copyright 2016 - Google
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 time
18from acts import signals
19from acts.test_utils.tel.loggers.protos.telephony_metric_pb2 import TelephonyVoiceTestResult
20from acts.test_utils.tel.tel_defines import CALL_PROPERTY_HIGH_DEF_AUDIO
21from acts.test_utils.tel.tel_defines import CALL_STATE_ACTIVE
22from acts.test_utils.tel.tel_defines import CALL_STATE_HOLDING
23from acts.test_utils.tel.tel_defines import CAPABILITY_VOLTE
24from acts.test_utils.tel.tel_defines import CAPABILITY_WFC
25from acts.test_utils.tel.tel_defines import GEN_2G
26from acts.test_utils.tel.tel_defines import GEN_3G
27from acts.test_utils.tel.tel_defines import GEN_4G
28from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
29from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_VOLTE_ENABLED
30from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WFC_ENABLED
31from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
32from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE
33from acts.test_utils.tel.tel_defines import RAT_FAMILY_CDMA2000
34from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
35from acts.test_utils.tel.tel_defines import RAT_FAMILY_GSM
36from acts.test_utils.tel.tel_defines import RAT_FAMILY_WCDMA
37from acts.test_utils.tel.tel_defines import RAT_FAMILY_WLAN
38from acts.test_utils.tel.tel_defines import RAT_1XRTT
39from acts.test_utils.tel.tel_defines import RAT_IWLAN
40from acts.test_utils.tel.tel_defines import RAT_LTE
41from acts.test_utils.tel.tel_defines import RAT_UMTS
42from acts.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_REG_AND_CALL
43from acts.test_utils.tel.tel_defines import WAIT_TIME_IN_CALL
44from acts.test_utils.tel.tel_defines import WAIT_TIME_LEAVE_VOICE_MAIL
45from acts.test_utils.tel.tel_defines import WFC_MODE_DISABLED
46from acts.test_utils.tel.tel_defines import WFC_MODE_CELLULAR_PREFERRED
47from acts.test_utils.tel.tel_defines import NETWORK_MODE_CDMA
48from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_ONLY
49from acts.test_utils.tel.tel_defines import NETWORK_MODE_GSM_UMTS
50from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_CDMA_EVDO
51from acts.test_utils.tel.tel_defines import NETWORK_MODE_LTE_GSM_WCDMA
52from acts.test_utils.tel.tel_subscription_utils import get_outgoing_voice_sub_id
53from acts.test_utils.tel.tel_subscription_utils import set_subid_for_outgoing_call
54from acts.test_utils.tel.tel_subscription_utils import get_subid_from_slot_index
55from acts.test_utils.tel.tel_subscription_utils import get_default_data_sub_id
56from acts.test_utils.tel.tel_test_utils import call_reject_leave_message
57from acts.test_utils.tel.tel_test_utils import call_setup_teardown
58from acts.test_utils.tel.tel_test_utils import call_setup_teardown_for_call_forwarding
59from acts.test_utils.tel.tel_test_utils import ensure_network_generation
60from acts.test_utils.tel.tel_test_utils import \
61    ensure_network_generation_for_subscription
62from acts.test_utils.tel.tel_test_utils import \
63    ensure_network_rat_for_subscription
64from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
65from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
66from acts.test_utils.tel.tel_test_utils import get_network_gen_for_subscription
67from acts.test_utils.tel.tel_test_utils import get_network_rat
68from acts.test_utils.tel.tel_test_utils import get_network_rat_for_subscription
69from acts.test_utils.tel.tel_test_utils import get_telephony_signal_strength
70from acts.test_utils.tel.tel_test_utils import is_wfc_enabled
71from acts.test_utils.tel.tel_test_utils import \
72    reset_preferred_network_type_to_allowable_range
73from acts.test_utils.tel.tel_test_utils import set_wfc_mode
74from acts.test_utils.tel.tel_test_utils import set_wifi_to_default
75from acts.test_utils.tel.tel_test_utils import TelResultWrapper
76from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
77from acts.test_utils.tel.tel_test_utils import toggle_volte
78from acts.test_utils.tel.tel_test_utils import toggle_volte_for_subscription
79from acts.test_utils.tel.tel_test_utils import verify_incall_state
80from acts.test_utils.tel.tel_test_utils import verify_internet_connection
81from acts.test_utils.tel.tel_test_utils import \
82    wait_for_data_attach_for_subscription
83from acts.test_utils.tel.tel_test_utils import wait_for_enhanced_4g_lte_setting
84from acts.test_utils.tel.tel_test_utils import wait_for_network_generation
85from acts.test_utils.tel.tel_test_utils import \
86    wait_for_network_generation_for_subscription
87from acts.test_utils.tel.tel_test_utils import wait_for_not_network_rat
88from acts.test_utils.tel.tel_test_utils import wait_for_network_rat
89from acts.test_utils.tel.tel_test_utils import \
90    wait_for_network_rat_for_subscription
91from acts.test_utils.tel.tel_test_utils import \
92     wait_for_not_network_rat_for_subscription
93from acts.test_utils.tel.tel_test_utils import wait_for_volte_enabled
94from acts.test_utils.tel.tel_test_utils import \
95    wait_for_voice_attach_for_subscription
96from acts.test_utils.tel.tel_test_utils import wait_for_wfc_enabled
97from acts.test_utils.tel.tel_test_utils import wait_for_wfc_disabled
98from acts.test_utils.tel.tel_test_utils import get_capability_for_subscription
99
100CallResult = TelephonyVoiceTestResult.CallResult.Value
101
102
103def two_phone_call_leave_voice_mail(
104        log,
105        caller,
106        caller_idle_func,
107        caller_in_call_check_func,
108        callee,
109        callee_idle_func,
110        wait_time_in_call=WAIT_TIME_LEAVE_VOICE_MAIL):
111    """Call from caller to callee, reject on callee, caller leave a voice mail.
112
113    1. Caller call Callee.
114    2. Callee reject incoming call.
115    3. Caller leave a voice mail.
116    4. Verify callee received the voice mail notification.
117
118    Args:
119        caller: caller android device object.
120        caller_idle_func: function to check caller's idle state.
121        caller_in_call_check_func: function to check caller's in-call state.
122        callee: callee android device object.
123        callee_idle_func: function to check callee's idle state.
124        wait_time_in_call: time to wait when leaving a voice mail.
125            This is optional, default is WAIT_TIME_LEAVE_VOICE_MAIL
126
127    Returns:
128        True: if voice message is received on callee successfully.
129        False: for errors
130    """
131
132    ads = [caller, callee]
133
134    # Make sure phones are idle.
135    ensure_phones_idle(log, ads)
136    if caller_idle_func and not caller_idle_func(log, caller):
137        caller.log.error("Caller Failed to Reselect")
138        return False
139    if callee_idle_func and not callee_idle_func(log, callee):
140        callee.log.error("Callee Failed to Reselect")
141        return False
142
143    # TODO: b/26337871 Need to use proper API to check phone registered.
144    time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
145
146    # Make call and leave a message.
147    if not call_reject_leave_message(
148            log, caller, callee, caller_in_call_check_func, wait_time_in_call):
149        log.error("make a call and leave a message failed.")
150        return False
151    return True
152
153
154def two_phone_call_short_seq(log,
155                             phone_a,
156                             phone_a_idle_func,
157                             phone_a_in_call_check_func,
158                             phone_b,
159                             phone_b_idle_func,
160                             phone_b_in_call_check_func,
161                             call_sequence_func=None,
162                             wait_time_in_call=WAIT_TIME_IN_CALL):
163    """Call process short sequence.
164    1. Ensure phone idle and in idle_func check return True.
165    2. Call from PhoneA to PhoneB, accept on PhoneB.
166    3. Check phone state, hangup on PhoneA.
167    4. Ensure phone idle and in idle_func check return True.
168    5. Call from PhoneA to PhoneB, accept on PhoneB.
169    6. Check phone state, hangup on PhoneB.
170
171    Args:
172        phone_a: PhoneA's android device object.
173        phone_a_idle_func: function to check PhoneA's idle state.
174        phone_a_in_call_check_func: function to check PhoneA's in-call state.
175        phone_b: PhoneB's android device object.
176        phone_b_idle_func: function to check PhoneB's idle state.
177        phone_b_in_call_check_func: function to check PhoneB's in-call state.
178        call_sequence_func: default parameter, not implemented.
179        wait_time_in_call: time to wait in call.
180            This is optional, default is WAIT_TIME_IN_CALL
181
182    Returns:
183        TelResultWrapper which will evaluate as False if error.
184    """
185    ads = [phone_a, phone_b]
186
187    call_params = [
188        (ads[0], ads[1], ads[0], phone_a_in_call_check_func,
189         phone_b_in_call_check_func),
190        (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
191         phone_b_in_call_check_func),
192    ]
193
194    tel_result = TelResultWrapper(CallResult('SUCCESS'))
195    for param in call_params:
196        # Make sure phones are idle.
197        ensure_phones_idle(log, ads)
198        if phone_a_idle_func and not phone_a_idle_func(log, phone_a):
199            phone_a.log.error("Phone A Failed to Reselect")
200            return TelResultWrapper(CallResult('CALL_SETUP_FAILURE'))
201        if phone_b_idle_func and not phone_b_idle_func(log, phone_b):
202            phone_b.log.error("Phone B Failed to Reselect")
203            return TelResultWrapper(CallResult('CALL_SETUP_FAILURE'))
204
205        # TODO: b/26337871 Need to use proper API to check phone registered.
206        time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
207
208        # Make call.
209        log.info("---> Call test: %s to %s <---", param[0].serial,
210                 param[1].serial)
211        tel_result = call_setup_teardown(
212                log, *param, wait_time_in_call=wait_time_in_call)
213        if not tel_result:
214            log.error("Call Iteration Failed")
215            break
216
217    return tel_result
218
219
220def two_phone_call_msim_short_seq(log,
221                             phone_a,
222                             phone_a_idle_func,
223                             phone_a_in_call_check_func,
224                             phone_b,
225                             phone_b_idle_func,
226                             phone_b_in_call_check_func,
227                             call_sequence_func=None,
228                             wait_time_in_call=WAIT_TIME_IN_CALL):
229    """Call process short sequence.
230    1. Ensure phone idle and in idle_func check return True.
231    2. Call from PhoneA to PhoneB, accept on PhoneB.
232    3. Check phone state, hangup on PhoneA.
233    4. Ensure phone idle and in idle_func check return True.
234    5. Call from PhoneA to PhoneB, accept on PhoneB.
235    6. Check phone state, hangup on PhoneB.
236
237    Args:
238        phone_a: PhoneA's android device object.
239        phone_a_idle_func: function to check PhoneA's idle state.
240        phone_a_in_call_check_func: function to check PhoneA's in-call state.
241        phone_b: PhoneB's android device object.
242        phone_b_idle_func: function to check PhoneB's idle state.
243        phone_b_in_call_check_func: function to check PhoneB's in-call state.
244        call_sequence_func: default parameter, not implemented.
245        wait_time_in_call: time to wait in call.
246            This is optional, default is WAIT_TIME_IN_CALL
247
248    Returns:
249        True: if call sequence succeed.
250        False: for errors
251    """
252    ads = [phone_a, phone_b]
253
254    call_params = [
255        (ads[0], ads[1], ads[0], phone_a_in_call_check_func,
256         phone_b_in_call_check_func),
257        (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
258         phone_b_in_call_check_func),
259    ]
260
261    for param in call_params:
262        # Make sure phones are idle.
263        ensure_phones_idle(log, ads)
264        if phone_a_idle_func and not phone_a_idle_func(log, phone_a):
265            phone_a.log.error("Phone A Failed to Reselect")
266            return False
267        if phone_b_idle_func and not phone_b_idle_func(log, phone_b):
268            phone_b.log.error("Phone B Failed to Reselect")
269            return False
270
271        # TODO: b/26337871 Need to use proper API to check phone registered.
272        time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
273
274        # Make call.
275        log.info("--> Call test: %s to %s <--", phone_a.serial, phone_b.serial)
276        slots = 2
277        for slot in range(slots):
278            set_subid_for_outgoing_call(
279                            ads[0], get_subid_from_slot_index(log,ads[0],slot))
280            set_subid_for_outgoing_call(
281                            ads[1], get_subid_from_slot_index(log,ads[1],slot))
282            time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
283            if not call_setup_teardown(log, *param,slot_id_callee = slot,
284                                       wait_time_in_call=wait_time_in_call):
285                log.error("Call Iteration Failed")
286                return False
287            if not call_setup_teardown(log, *param,slot_id_callee = 1-slot,
288                                       wait_time_in_call=wait_time_in_call):
289                log.error("Call Iteration Failed")
290                return False
291    return True
292
293
294def two_phone_call_long_seq(log,
295                            phone_a,
296                            phone_a_idle_func,
297                            phone_a_in_call_check_func,
298                            phone_b,
299                            phone_b_idle_func,
300                            phone_b_in_call_check_func,
301                            call_sequence_func=None,
302                            wait_time_in_call=WAIT_TIME_IN_CALL):
303    """Call process long sequence.
304    1. Ensure phone idle and in idle_func check return True.
305    2. Call from PhoneA to PhoneB, accept on PhoneB.
306    3. Check phone state, hangup on PhoneA.
307    4. Ensure phone idle and in idle_func check return True.
308    5. Call from PhoneA to PhoneB, accept on PhoneB.
309    6. Check phone state, hangup on PhoneB.
310    7. Ensure phone idle and in idle_func check return True.
311    8. Call from PhoneB to PhoneA, accept on PhoneA.
312    9. Check phone state, hangup on PhoneA.
313    10. Ensure phone idle and in idle_func check return True.
314    11. Call from PhoneB to PhoneA, accept on PhoneA.
315    12. Check phone state, hangup on PhoneB.
316
317    Args:
318        phone_a: PhoneA's android device object.
319        phone_a_idle_func: function to check PhoneA's idle state.
320        phone_a_in_call_check_func: function to check PhoneA's in-call state.
321        phone_b: PhoneB's android device object.
322        phone_b_idle_func: function to check PhoneB's idle state.
323        phone_b_in_call_check_func: function to check PhoneB's in-call state.
324        call_sequence_func: default parameter, not implemented.
325        wait_time_in_call: time to wait in call.
326            This is optional, default is WAIT_TIME_IN_CALL
327
328    Returns:
329        TelResultWrapper which will evaluate as False if error.
330
331    """
332    ads = [phone_a, phone_b]
333
334    call_params = [
335        (ads[0], ads[1], ads[0], phone_a_in_call_check_func,
336         phone_b_in_call_check_func),
337        (ads[0], ads[1], ads[1], phone_a_in_call_check_func,
338         phone_b_in_call_check_func),
339        (ads[1], ads[0], ads[0], phone_b_in_call_check_func,
340         phone_a_in_call_check_func),
341        (ads[1], ads[0], ads[1], phone_b_in_call_check_func,
342         phone_a_in_call_check_func),
343    ]
344
345    tel_result = TelResultWrapper(CallResult('SUCCESS'))
346    for param in call_params:
347        # Make sure phones are idle.
348        ensure_phones_idle(log, ads)
349        if phone_a_idle_func and not phone_a_idle_func(log, phone_a):
350            phone_a.log.error("Phone A Failed to Reselect")
351            return TelResultWrapper(CallResult('CALL_SETUP_FAILURE'))
352        if phone_b_idle_func and not phone_b_idle_func(log, phone_b):
353            phone_b.log.error("Phone B Failed to Reselect")
354            return TelResultWrapper(CallResult('CALL_SETUP_FAILURE'))
355
356        # TODO: b/26337871 Need to use proper API to check phone registered.
357        time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
358
359        # Make call.
360        log.info("---> Call test: %s to %s <---", param[0].serial,
361                 param[1].serial)
362        tel_result = call_setup_teardown(
363                log, *param, wait_time_in_call=wait_time_in_call)
364        if not tel_result:
365            log.error("Call Iteration Failed")
366            break
367
368    return tel_result
369
370def three_phone_call_forwarding_short_seq(log,
371                             phone_a,
372                             phone_a_idle_func,
373                             phone_a_in_call_check_func,
374                             phone_b,
375                             phone_c,
376                             wait_time_in_call=WAIT_TIME_IN_CALL,
377                             call_forwarding_type="unconditional",
378                             retry=2):
379    """Short sequence of call process with call forwarding.
380    Test steps:
381        1. Ensure all phones are initially in idle state.
382        2. Enable call forwarding on Phone A.
383        3. Make a call from Phone B to Phone A, The call should be forwarded to
384           PhoneC. Accept the call on Phone C.
385        4. Ensure the call is connected and in correct phone state.
386        5. Hang up the call on Phone B.
387        6. Ensure all phones are in idle state.
388        7. Disable call forwarding on Phone A.
389        7. Make a call from Phone B to Phone A, The call should NOT be forwarded
390           to PhoneC. Accept the call on Phone A.
391        8. Ensure the call is connected and in correct phone state.
392        9. Hang up the call on Phone B.
393
394    Args:
395        phone_a: android object of Phone A
396        phone_a_idle_func: function to check idle state on Phone A
397        phone_a_in_call_check_func: function to check in-call state on Phone A
398        phone_b: android object of Phone B
399        phone_c: android object of Phone C
400        wait_time_in_call: time to wait in call.
401            This is optional, default is WAIT_TIME_IN_CALL
402        call_forwarding_type:
403            - "unconditional"
404            - "busy"
405            - "not_answered"
406            - "not_reachable"
407        retry: times of retry
408
409    Returns:
410        True: if call sequence succeed.
411        False: for errors
412    """
413    ads = [phone_a, phone_b, phone_c]
414
415    call_params = [
416        (ads[1], ads[0], ads[2], ads[1], phone_a_in_call_check_func, False)
417    ]
418
419    if call_forwarding_type != "unconditional":
420        call_params.append((
421            ads[1],
422            ads[0],
423            ads[2],
424            ads[1],
425            phone_a_in_call_check_func,
426            True))
427
428    for param in call_params:
429        ensure_phones_idle(log, ads)
430        if phone_a_idle_func and not phone_a_idle_func(log, phone_a):
431            phone_a.log.error("Phone A Failed to Reselect")
432            return False
433
434        time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
435
436        log.info(
437            "---> Call forwarding %s (caller: %s, callee: %s, callee forwarded:"
438            " %s) <---",
439            call_forwarding_type,
440            param[0].serial,
441            param[1].serial,
442            param[2].serial)
443        while not call_setup_teardown_for_call_forwarding(
444                log,
445                *param,
446                wait_time_in_call=wait_time_in_call,
447                call_forwarding_type=call_forwarding_type) and retry >= 0:
448
449            if retry <= 0:
450                log.error("Call forwarding %s failed." % call_forwarding_type)
451                return False
452            else:
453                log.info(
454                    "RERUN the test case: 'Call forwarding %s'" %
455                    call_forwarding_type)
456
457            retry = retry - 1
458
459    return True
460
461def phone_setup_iwlan(log,
462                      ad,
463                      is_airplane_mode,
464                      wfc_mode,
465                      wifi_ssid=None,
466                      wifi_pwd=None):
467    """Phone setup function for epdg call test.
468    Set WFC mode according to wfc_mode.
469    Set airplane mode according to is_airplane_mode.
470    Make sure phone connect to WiFi. (If wifi_ssid is not None.)
471    Wait for phone to be in iwlan data network type.
472    Wait for phone to report wfc enabled flag to be true.
473
474    Args:
475        log: Log object.
476        ad: Android device object.
477        is_airplane_mode: True to turn on airplane mode. False to turn off airplane mode.
478        wfc_mode: WFC mode to set to.
479        wifi_ssid: WiFi network SSID. This is optional.
480            If wifi_ssid is None, then phone_setup_iwlan will not attempt to connect to wifi.
481        wifi_pwd: WiFi network password. This is optional.
482
483    Returns:
484        True if success. False if fail.
485    """
486    if not get_capability_for_subscription(ad, CAPABILITY_WFC,
487        get_outgoing_voice_sub_id(ad)):
488        ad.log.error("WFC is not supported, abort test.")
489        raise signals.TestSkip("WFC is not supported, abort test.")
490    return phone_setup_iwlan_for_subscription(log, ad,
491                                              get_outgoing_voice_sub_id(ad),
492                                              is_airplane_mode, wfc_mode,
493                                              wifi_ssid, wifi_pwd)
494
495
496def phone_setup_iwlan_for_subscription(log,
497                                       ad,
498                                       sub_id,
499                                       is_airplane_mode,
500                                       wfc_mode,
501                                       wifi_ssid=None,
502                                       wifi_pwd=None):
503    """Phone setup function for epdg call test for subscription id.
504    Set WFC mode according to wfc_mode.
505    Set airplane mode according to is_airplane_mode.
506    Make sure phone connect to WiFi. (If wifi_ssid is not None.)
507    Wait for phone to be in iwlan data network type.
508    Wait for phone to report wfc enabled flag to be true.
509
510    Args:
511        log: Log object.
512        ad: Android device object.
513        sub_id: subscription id.
514        is_airplane_mode: True to turn on airplane mode. False to turn off airplane mode.
515        wfc_mode: WFC mode to set to.
516        wifi_ssid: WiFi network SSID. This is optional.
517            If wifi_ssid is None, then phone_setup_iwlan will not attempt to connect to wifi.
518        wifi_pwd: WiFi network password. This is optional.
519
520    Returns:
521        True if success. False if fail.
522    """
523    toggle_airplane_mode(log, ad, is_airplane_mode, strict_checking=False)
524
525    # check if WFC supported phones
526    if wfc_mode != WFC_MODE_DISABLED and not ad.droid.imsIsWfcEnabledByPlatform(
527    ):
528        ad.log.error("WFC is not enabled on this device by checking "
529                     "ImsManager.isWfcEnabledByPlatform")
530        return False
531
532    if wifi_ssid is not None:
533        if not ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd):
534            ad.log.error("Fail to bring up WiFi connection on %s.", wifi_ssid)
535            return False
536    else:
537        ad.log.info("WiFi network SSID not specified, available user "
538                    "parameters are: wifi_network_ssid, wifi_network_ssid_2g, "
539                    "wifi_network_ssid_5g")
540
541    if not set_wfc_mode(log, ad, wfc_mode):
542        ad.log.error("Unable to set WFC mode to %s.", wfc_mode)
543        return False
544
545    if not wait_for_wfc_enabled(log, ad, max_time=MAX_WAIT_TIME_WFC_ENABLED):
546        ad.log.error("WFC is not enabled")
547        return False
548    return True
549
550
551def phone_setup_iwlan_cellular_preferred(log,
552                                         ad,
553                                         wifi_ssid=None,
554                                         wifi_pwd=None):
555    """Phone setup function for iwlan Non-APM CELLULAR_PREFERRED test.
556    Set WFC mode according to CELLULAR_PREFERRED.
557    Set airplane mode according to False.
558    Make sure phone connect to WiFi. (If wifi_ssid is not None.)
559    Make sure phone don't report iwlan data network type.
560    Make sure phone don't report wfc enabled flag to be true.
561
562    Args:
563        log: Log object.
564        ad: Android device object.
565        wifi_ssid: WiFi network SSID. This is optional.
566            If wifi_ssid is None, then phone_setup_iwlan will not attempt to connect to wifi.
567        wifi_pwd: WiFi network password. This is optional.
568
569    Returns:
570        True if success. False if fail.
571    """
572    toggle_airplane_mode(log, ad, False, strict_checking=False)
573    try:
574        toggle_volte(log, ad, True)
575        if not wait_for_network_generation(
576                log, ad, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA):
577            if not ensure_network_generation(
578                    log, ad, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA):
579                ad.log.error("Fail to ensure data in 4G")
580                return False
581    except Exception as e:
582        ad.log.error(e)
583        ad.droid.telephonyToggleDataConnection(True)
584    if wifi_ssid is not None:
585        if not ensure_wifi_connected(log, ad, wifi_ssid, wifi_pwd):
586            ad.log.error("Connect to WiFi failed.")
587            return False
588    if not set_wfc_mode(log, ad, WFC_MODE_CELLULAR_PREFERRED):
589        ad.log.error("Set WFC mode failed.")
590        return False
591    if not wait_for_not_network_rat(
592            log, ad, RAT_FAMILY_WLAN, voice_or_data=NETWORK_SERVICE_DATA):
593        ad.log.error("Data rat in iwlan mode.")
594        return False
595    elif not wait_for_wfc_disabled(log, ad, MAX_WAIT_TIME_WFC_ENABLED):
596        ad.log.error("Should report wifi calling disabled within %s.",
597                     MAX_WAIT_TIME_WFC_ENABLED)
598        return False
599    return True
600
601
602def phone_setup_data_for_subscription(log, ad, sub_id, network_generation):
603    """Setup Phone <sub_id> Data to <network_generation>
604
605    Args:
606        log: log object
607        ad: android device object
608        sub_id: subscription id
609        network_generation: network generation, e.g. GEN_2G, GEN_3G, GEN_4G
610
611    Returns:
612        True if success, False if fail.
613    """
614    toggle_airplane_mode(log, ad, False, strict_checking=False)
615    set_wifi_to_default(log, ad)
616    if not set_wfc_mode(log, ad, WFC_MODE_DISABLED):
617        ad.log.error("Disable WFC failed.")
618        return False
619    if not ensure_network_generation_for_subscription(
620            log,
621            ad,
622            sub_id,
623            network_generation,
624            voice_or_data=NETWORK_SERVICE_DATA):
625        get_telephony_signal_strength(ad)
626        return False
627    return True
628
629
630def phone_setup_4g(log, ad):
631    """Setup Phone default data sub_id data to 4G.
632
633    Args:
634        log: log object
635        ad: android device object
636
637    Returns:
638        True if success, False if fail.
639    """
640    return phone_setup_4g_for_subscription(log, ad,
641                                           get_default_data_sub_id(ad))
642
643
644def phone_setup_4g_for_subscription(log, ad, sub_id):
645    """Setup Phone <sub_id> Data to 4G.
646
647    Args:
648        log: log object
649        ad: android device object
650        sub_id: subscription id
651
652    Returns:
653        True if success, False if fail.
654    """
655    return phone_setup_data_for_subscription(log, ad, sub_id, GEN_4G)
656
657
658def phone_setup_3g(log, ad):
659    """Setup Phone default data sub_id data to 3G.
660
661    Args:
662        log: log object
663        ad: android device object
664
665    Returns:
666        True if success, False if fail.
667    """
668    return phone_setup_3g_for_subscription(log, ad,
669                                           get_default_data_sub_id(ad))
670
671
672def phone_setup_3g_for_subscription(log, ad, sub_id):
673    """Setup Phone <sub_id> Data to 3G.
674
675    Args:
676        log: log object
677        ad: android device object
678        sub_id: subscription id
679
680    Returns:
681        True if success, False if fail.
682    """
683    return phone_setup_data_for_subscription(log, ad, sub_id, GEN_3G)
684
685
686def phone_setup_2g(log, ad):
687    """Setup Phone default data sub_id data to 2G.
688
689    Args:
690        log: log object
691        ad: android device object
692
693    Returns:
694        True if success, False if fail.
695    """
696    return phone_setup_2g_for_subscription(log, ad,
697                                           get_default_data_sub_id(ad))
698
699
700def phone_setup_2g_for_subscription(log, ad, sub_id):
701    """Setup Phone <sub_id> Data to 3G.
702
703    Args:
704        log: log object
705        ad: android device object
706        sub_id: subscription id
707
708    Returns:
709        True if success, False if fail.
710    """
711    return phone_setup_data_for_subscription(log, ad, sub_id, GEN_2G)
712
713
714def phone_setup_csfb(log, ad):
715    """Setup phone for CSFB call test.
716
717    Setup Phone to be in 4G mode.
718    Disabled VoLTE.
719
720    Args:
721        log: log object
722        ad: Android device object.
723
724    Returns:
725        True if setup successfully.
726        False for errors.
727    """
728    return phone_setup_csfb_for_subscription(log, ad,
729                                             get_outgoing_voice_sub_id(ad))
730
731
732def phone_setup_csfb_for_subscription(log, ad, sub_id):
733    """Setup phone for CSFB call test for subscription id.
734
735    Setup Phone to be in 4G mode.
736    Disabled VoLTE.
737
738    Args:
739        log: log object
740        ad: Android device object.
741        sub_id: subscription id.
742
743    Returns:
744        True if setup successfully.
745        False for errors.
746    """
747    if not phone_setup_4g_for_subscription(log, ad, sub_id):
748        ad.log.error("Failed to set to 4G data.")
749        return False
750    if ad.droid.imsIsEnhanced4gLteModeSettingEnabledByPlatform():
751        toggle_volte(log, ad, False)
752    if not ensure_network_generation_for_subscription(
753            log, ad, sub_id, GEN_4G, voice_or_data=NETWORK_SERVICE_DATA):
754        return False
755
756    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
757                                                  MAX_WAIT_TIME_NW_SELECTION):
758        return False
759
760    return phone_idle_csfb_for_subscription(log, ad, sub_id)
761
762
763def phone_setup_volte(log, ad):
764    """Setup VoLTE enable.
765
766    Args:
767        log: log object
768        ad: android device object.
769
770    Returns:
771        True: if VoLTE is enabled successfully.
772        False: for errors
773    """
774    if not get_capability_for_subscription(ad, CAPABILITY_VOLTE,
775        get_outgoing_voice_sub_id(ad)):
776        ad.log.error("VoLTE is not supported, abort test.")
777        raise signals.TestSkip("VoLTE is not supported, abort test.")
778    return phone_setup_volte_for_subscription(log, ad,
779                                              get_outgoing_voice_sub_id(ad))
780
781
782def phone_setup_volte_for_subscription(log, ad, sub_id):
783    """Setup VoLTE enable for subscription id.
784
785    Args:
786        log: log object
787        ad: android device object.
788        sub_id: subscription id.
789
790    Returns:
791        True: if VoLTE is enabled successfully.
792        False: for errors
793    """
794    if not phone_setup_4g_for_subscription(log, ad, sub_id):
795        ad.log.error("Failed to set to 4G data.")
796        return False
797    if not wait_for_enhanced_4g_lte_setting(log, ad):
798        ad.log.error("Enhanced 4G LTE setting is not available")
799        return False
800    toggle_volte_for_subscription(log, ad, sub_id, True)
801    return phone_idle_volte_for_subscription(log, ad, sub_id)
802
803
804def phone_setup_voice_3g(log, ad):
805    """Setup phone voice to 3G.
806
807    Args:
808        log: log object
809        ad: Android device object.
810
811    Returns:
812        True if setup successfully.
813        False for errors.
814    """
815    return phone_setup_voice_3g_for_subscription(log, ad,
816                                                 get_outgoing_voice_sub_id(ad))
817
818
819def phone_setup_voice_3g_for_subscription(log, ad, sub_id):
820    """Setup phone voice to 3G for subscription id.
821
822    Args:
823        log: log object
824        ad: Android device object.
825        sub_id: subscription id.
826
827    Returns:
828        True if setup successfully.
829        False for errors.
830    """
831    if not phone_setup_3g_for_subscription(log, ad, sub_id):
832        ad.log.error("Failed to set to 3G data.")
833        return False
834    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
835                                                  MAX_WAIT_TIME_NW_SELECTION):
836        return False
837    return phone_idle_3g_for_subscription(log, ad, sub_id)
838
839
840def phone_setup_voice_2g(log, ad):
841    """Setup phone voice to 2G.
842
843    Args:
844        log: log object
845        ad: Android device object.
846
847    Returns:
848        True if setup successfully.
849        False for errors.
850    """
851    return phone_setup_voice_2g_for_subscription(log, ad,
852                                                 get_outgoing_voice_sub_id(ad))
853
854
855def phone_setup_voice_2g_for_subscription(log, ad, sub_id):
856    """Setup phone voice to 2G for subscription id.
857
858    Args:
859        log: log object
860        ad: Android device object.
861        sub_id: subscription id.
862
863    Returns:
864        True if setup successfully.
865        False for errors.
866    """
867    if not phone_setup_2g_for_subscription(log, ad, sub_id):
868        ad.log.error("Failed to set to 2G data.")
869        return False
870    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
871                                                  MAX_WAIT_TIME_NW_SELECTION):
872        return False
873    return phone_idle_2g_for_subscription(log, ad, sub_id)
874
875
876def phone_setup_voice_general(log, ad):
877    """Setup phone for voice general call test.
878
879    Make sure phone attached to voice.
880    Make necessary delay.
881
882    Args:
883        ad: Android device object.
884
885    Returns:
886        True if setup successfully.
887        False for errors.
888    """
889    return phone_setup_voice_general_for_subscription(
890        log, ad, get_outgoing_voice_sub_id(ad))
891
892
893def phone_setup_voice_general_for_slot(log,ad,slot_id):
894    return phone_setup_voice_general_for_subscription(
895        log, ad, get_subid_from_slot_index(log,ad,slot_id))
896
897
898def phone_setup_voice_general_for_subscription(log, ad, sub_id):
899    """Setup phone for voice general call test for subscription id.
900
901    Make sure phone attached to voice.
902    Make necessary delay.
903
904    Args:
905        ad: Android device object.
906        sub_id: subscription id.
907
908    Returns:
909        True if setup successfully.
910        False for errors.
911    """
912    toggle_airplane_mode(log, ad, False, strict_checking=False)
913    if not wait_for_voice_attach_for_subscription(log, ad, sub_id,
914                                                  MAX_WAIT_TIME_NW_SELECTION):
915        # if phone can not attach voice, try phone_setup_voice_3g
916        return phone_setup_voice_3g_for_subscription(log, ad, sub_id)
917    return True
918
919
920def phone_setup_data_general(log, ad):
921    """Setup phone for data general test.
922
923    Make sure phone attached to data.
924    Make necessary delay.
925
926    Args:
927        ad: Android device object.
928
929    Returns:
930        True if setup successfully.
931        False for errors.
932    """
933    return phone_setup_data_general_for_subscription(
934        log, ad, ad.droid.subscriptionGetDefaultDataSubId())
935
936
937def phone_setup_data_general_for_subscription(log, ad, sub_id):
938    """Setup phone for data general test for subscription id.
939
940    Make sure phone attached to data.
941    Make necessary delay.
942
943    Args:
944        ad: Android device object.
945        sub_id: subscription id.
946
947    Returns:
948        True if setup successfully.
949        False for errors.
950    """
951    toggle_airplane_mode(log, ad, False, strict_checking=False)
952    if not wait_for_data_attach_for_subscription(log, ad, sub_id,
953                                                 MAX_WAIT_TIME_NW_SELECTION):
954        # if phone can not attach data, try reset network preference settings
955        reset_preferred_network_type_to_allowable_range(log, ad)
956
957    return wait_for_data_attach_for_subscription(log, ad, sub_id,
958                                                 MAX_WAIT_TIME_NW_SELECTION)
959
960
961def phone_setup_rat_for_subscription(log, ad, sub_id, network_preference,
962                                     rat_family):
963    toggle_airplane_mode(log, ad, False, strict_checking=False)
964    set_wifi_to_default(log, ad)
965    if not set_wfc_mode(log, ad, WFC_MODE_DISABLED):
966        ad.log.error("Disable WFC failed.")
967        return False
968    return ensure_network_rat_for_subscription(log, ad, sub_id,
969                                               network_preference, rat_family)
970
971
972def phone_setup_lte_gsm_wcdma(log, ad):
973    return phone_setup_lte_gsm_wcdma_for_subscription(
974        log, ad, ad.droid.subscriptionGetDefaultSubId())
975
976
977def phone_setup_lte_gsm_wcdma_for_subscription(log, ad, sub_id):
978    return phone_setup_rat_for_subscription(
979        log, ad, sub_id, NETWORK_MODE_LTE_GSM_WCDMA, RAT_FAMILY_LTE)
980
981
982def phone_setup_gsm_umts(log, ad):
983    return phone_setup_gsm_umts_for_subscription(
984        log, ad, ad.droid.subscriptionGetDefaultSubId())
985
986
987def phone_setup_gsm_umts_for_subscription(log, ad, sub_id):
988    return phone_setup_rat_for_subscription(
989        log, ad, sub_id, NETWORK_MODE_GSM_UMTS, RAT_FAMILY_WCDMA)
990
991
992def phone_setup_gsm_only(log, ad):
993    return phone_setup_gsm_only_for_subscription(
994        log, ad, ad.droid.subscriptionGetDefaultSubId())
995
996
997def phone_setup_gsm_only_for_subscription(log, ad, sub_id):
998    return phone_setup_rat_for_subscription(
999        log, ad, sub_id, NETWORK_MODE_GSM_ONLY, RAT_FAMILY_GSM)
1000
1001
1002def phone_setup_lte_cdma_evdo(log, ad):
1003    return phone_setup_lte_cdma_evdo_for_subscription(
1004        log, ad, ad.droid.subscriptionGetDefaultSubId())
1005
1006
1007def phone_setup_lte_cdma_evdo_for_subscription(log, ad, sub_id):
1008    return phone_setup_rat_for_subscription(
1009        log, ad, sub_id, NETWORK_MODE_LTE_CDMA_EVDO, RAT_FAMILY_LTE)
1010
1011
1012def phone_setup_cdma(log, ad):
1013    return phone_setup_cdma_for_subscription(
1014        log, ad, ad.droid.subscriptionGetDefaultSubId())
1015
1016
1017def phone_setup_cdma_for_subscription(log, ad, sub_id):
1018    return phone_setup_rat_for_subscription(log, ad, sub_id, NETWORK_MODE_CDMA,
1019                                            RAT_FAMILY_CDMA2000)
1020
1021
1022def phone_idle_volte(log, ad):
1023    """Return if phone is idle for VoLTE call test.
1024
1025    Args:
1026        ad: Android device object.
1027    """
1028    return phone_idle_volte_for_subscription(log, ad,
1029                                             get_outgoing_voice_sub_id(ad))
1030
1031
1032def phone_idle_volte_for_subscription(log, ad, sub_id):
1033    """Return if phone is idle for VoLTE call test for subscription id.
1034
1035    Args:
1036        ad: Android device object.
1037        sub_id: subscription id.
1038    """
1039    if not wait_for_network_rat_for_subscription(
1040            log, ad, sub_id, RAT_FAMILY_LTE,
1041            voice_or_data=NETWORK_SERVICE_VOICE):
1042        ad.log.error("Voice rat not in LTE mode.")
1043        return False
1044    if not wait_for_volte_enabled(log, ad, MAX_WAIT_TIME_VOLTE_ENABLED):
1045        ad.log.error(
1046            "Failed to <report volte enabled true> within %s seconds.",
1047            MAX_WAIT_TIME_VOLTE_ENABLED)
1048        return False
1049    return True
1050
1051
1052def phone_idle_iwlan(log, ad):
1053    """Return if phone is idle for WiFi calling call test.
1054
1055    Args:
1056        ad: Android device object.
1057    """
1058    return phone_idle_iwlan_for_subscription(log, ad,
1059                                             get_outgoing_voice_sub_id(ad))
1060
1061
1062def phone_idle_iwlan_for_subscription(log, ad, sub_id):
1063    """Return if phone is idle for WiFi calling call test for subscription id.
1064
1065    Args:
1066        ad: Android device object.
1067        sub_id: subscription id.
1068    """
1069    if not wait_for_wfc_enabled(log, ad, MAX_WAIT_TIME_WFC_ENABLED):
1070        ad.log.error("Failed to <report wfc enabled true> within %s seconds.",
1071                     MAX_WAIT_TIME_WFC_ENABLED)
1072        return False
1073    return True
1074
1075
1076def phone_idle_not_iwlan(log, ad):
1077    """Return if phone is idle for non WiFi calling call test.
1078
1079    Args:
1080        ad: Android device object.
1081    """
1082    return phone_idle_not_iwlan_for_subscription(log, ad,
1083                                                 get_outgoing_voice_sub_id(ad))
1084
1085
1086def phone_idle_not_iwlan_for_subscription(log, ad, sub_id):
1087    """Return if phone is idle for non WiFi calling call test for sub id.
1088
1089    Args:
1090        ad: Android device object.
1091        sub_id: subscription id.
1092    """
1093    if not wait_for_not_network_rat_for_subscription(
1094            log, ad, sub_id, RAT_FAMILY_WLAN,
1095            voice_or_data=NETWORK_SERVICE_DATA):
1096        log.error("{} data rat in iwlan mode.".format(ad.serial))
1097        return False
1098    return True
1099
1100
1101def phone_idle_csfb(log, ad):
1102    """Return if phone is idle for CSFB call test.
1103
1104    Args:
1105        ad: Android device object.
1106    """
1107    return phone_idle_csfb_for_subscription(log, ad,
1108                                            get_outgoing_voice_sub_id(ad))
1109
1110
1111def phone_idle_csfb_for_subscription(log, ad, sub_id):
1112    """Return if phone is idle for CSFB call test for subscription id.
1113
1114    Args:
1115        ad: Android device object.
1116        sub_id: subscription id.
1117    """
1118    if not wait_for_network_rat_for_subscription(
1119            log, ad, sub_id, RAT_FAMILY_LTE,
1120            voice_or_data=NETWORK_SERVICE_DATA):
1121        ad.log.error("Data rat not in lte mode.")
1122        return False
1123    return True
1124
1125
1126def phone_idle_3g(log, ad):
1127    """Return if phone is idle for 3G call test.
1128
1129    Args:
1130        ad: Android device object.
1131    """
1132    return phone_idle_3g_for_subscription(log, ad,
1133                                          get_outgoing_voice_sub_id(ad))
1134
1135
1136def phone_idle_3g_for_subscription(log, ad, sub_id):
1137    """Return if phone is idle for 3G call test for subscription id.
1138
1139    Args:
1140        ad: Android device object.
1141        sub_id: subscription id.
1142    """
1143    return wait_for_network_generation_for_subscription(
1144        log, ad, sub_id, GEN_3G, voice_or_data=NETWORK_SERVICE_VOICE)
1145
1146
1147def phone_idle_2g(log, ad):
1148    """Return if phone is idle for 2G call test.
1149
1150    Args:
1151        ad: Android device object.
1152    """
1153    return phone_idle_2g_for_subscription(log, ad,
1154                                          get_outgoing_voice_sub_id(ad))
1155
1156
1157def phone_idle_2g_for_subscription(log, ad, sub_id):
1158    """Return if phone is idle for 2G call test for subscription id.
1159
1160    Args:
1161        ad: Android device object.
1162        sub_id: subscription id.
1163    """
1164    return wait_for_network_generation_for_subscription(
1165        log, ad, sub_id, GEN_2G, voice_or_data=NETWORK_SERVICE_VOICE)
1166
1167
1168def get_current_voice_rat(log, ad):
1169    """Return current Voice RAT
1170
1171    Args:
1172        ad: Android device object.
1173    """
1174    return get_current_voice_rat_for_subscription(
1175        log, ad, get_outgoing_voice_sub_id(ad))
1176
1177
1178def get_current_voice_rat_for_subscription(log, ad, sub_id):
1179    """Return current Voice RAT for subscription id.
1180
1181    Args:
1182        ad: Android device object.
1183        sub_id: subscription id.
1184    """
1185    return get_network_rat_for_subscription(log, ad, sub_id,
1186                                            NETWORK_SERVICE_VOICE)
1187
1188
1189def is_phone_in_call_volte(log, ad):
1190    """Return if phone is in VoLTE call.
1191
1192    Args:
1193        ad: Android device object.
1194    """
1195    return is_phone_in_call_volte_for_subscription(
1196        log, ad, get_outgoing_voice_sub_id(ad))
1197
1198
1199def is_phone_in_call_volte_for_subscription(log, ad, sub_id):
1200    """Return if phone is in VoLTE call for subscription id.
1201
1202    Args:
1203        ad: Android device object.
1204        sub_id: subscription id.
1205    """
1206    if not ad.droid.telecomIsInCall():
1207        ad.log.error("Not in call.")
1208        return False
1209    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
1210                                               NETWORK_SERVICE_VOICE)
1211    if nw_type != RAT_LTE:
1212        ad.log.error("Voice rat on: %s. Expected: LTE", nw_type)
1213        return False
1214    return True
1215
1216
1217def is_phone_in_call_csfb(log, ad):
1218    """Return if phone is in CSFB call.
1219
1220    Args:
1221        ad: Android device object.
1222    """
1223    return is_phone_in_call_csfb_for_subscription(
1224        log, ad, get_outgoing_voice_sub_id(ad))
1225
1226
1227def is_phone_in_call_csfb_for_subscription(log, ad, sub_id):
1228    """Return if phone is in CSFB call for subscription id.
1229
1230    Args:
1231        ad: Android device object.
1232        sub_id: subscription id.
1233    """
1234    if not ad.droid.telecomIsInCall():
1235        ad.log.error("Not in call.")
1236        return False
1237    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
1238                                               NETWORK_SERVICE_VOICE)
1239    if nw_type == RAT_LTE:
1240        ad.log.error("Voice rat on: %s. Expected: not LTE", nw_type)
1241        return False
1242    return True
1243
1244
1245def is_phone_in_call_3g(log, ad):
1246    """Return if phone is in 3G call.
1247
1248    Args:
1249        ad: Android device object.
1250    """
1251    return is_phone_in_call_3g_for_subscription(log, ad,
1252                                                get_outgoing_voice_sub_id(ad))
1253
1254
1255def is_phone_in_call_3g_for_subscription(log, ad, sub_id):
1256    """Return if phone is in 3G call for subscription id.
1257
1258    Args:
1259        ad: Android device object.
1260        sub_id: subscription id.
1261    """
1262    if not ad.droid.telecomIsInCall():
1263        ad.log.error("Not in call.")
1264        return False
1265    nw_gen = get_network_gen_for_subscription(log, ad, sub_id,
1266                                              NETWORK_SERVICE_VOICE)
1267    if nw_gen != GEN_3G:
1268        ad.log.error("Voice rat on: %s. Expected: 3g", nw_gen)
1269        return False
1270    return True
1271
1272
1273def is_phone_in_call_2g(log, ad):
1274    """Return if phone is in 2G call.
1275
1276    Args:
1277        ad: Android device object.
1278    """
1279    return is_phone_in_call_2g_for_subscription(log, ad,
1280                                                get_outgoing_voice_sub_id(ad))
1281
1282
1283def is_phone_in_call_2g_for_subscription(log, ad, sub_id):
1284    """Return if phone is in 2G call for subscription id.
1285
1286    Args:
1287        ad: Android device object.
1288        sub_id: subscription id.
1289    """
1290    if not ad.droid.telecomIsInCall():
1291        ad.log.error("Not in call.")
1292        return False
1293    nw_gen = get_network_gen_for_subscription(log, ad, sub_id,
1294                                              NETWORK_SERVICE_VOICE)
1295    if nw_gen != GEN_2G:
1296        ad.log.error("Voice rat on: %s. Expected: 2g", nw_gen)
1297        return False
1298    return True
1299
1300
1301def is_phone_in_call_1x(log, ad):
1302    """Return if phone is in 1x call.
1303
1304    Args:
1305        ad: Android device object.
1306    """
1307    return is_phone_in_call_1x_for_subscription(log, ad,
1308                                                get_outgoing_voice_sub_id(ad))
1309
1310
1311def is_phone_in_call_1x_for_subscription(log, ad, sub_id):
1312    """Return if phone is in 1x call for subscription id.
1313
1314    Args:
1315        ad: Android device object.
1316        sub_id: subscription id.
1317    """
1318    if not ad.droid.telecomIsInCall():
1319        ad.log.error("Not in call.")
1320        return False
1321    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
1322                                               NETWORK_SERVICE_VOICE)
1323    if nw_type != RAT_1XRTT:
1324        ad.log.error("Voice rat on: %s. Expected: 1xrtt", nw_type)
1325        return False
1326    return True
1327
1328
1329def is_phone_in_call_wcdma(log, ad):
1330    """Return if phone is in WCDMA call.
1331
1332    Args:
1333        ad: Android device object.
1334    """
1335    return is_phone_in_call_wcdma_for_subscription(
1336        log, ad, get_outgoing_voice_sub_id(ad))
1337
1338
1339def is_phone_in_call_wcdma_for_subscription(log, ad, sub_id):
1340    """Return if phone is in WCDMA call for subscription id.
1341
1342    Args:
1343        ad: Android device object.
1344        sub_id: subscription id.
1345    """
1346    # Currently checking 'umts'.
1347    # Changes may needed in the future.
1348    if not ad.droid.telecomIsInCall():
1349        ad.log.error("Not in call.")
1350        return False
1351    nw_type = get_network_rat_for_subscription(log, ad, sub_id,
1352                                               NETWORK_SERVICE_VOICE)
1353    if nw_type != RAT_UMTS:
1354        ad.log.error("%s voice rat on: %s. Expected: umts", nw_type)
1355        return False
1356    return True
1357
1358
1359def is_phone_in_call_iwlan(log, ad, call_id=None):
1360    """Return if phone is in WiFi call.
1361
1362    Args:
1363        ad: Android device object.
1364    """
1365    if not ad.droid.telecomIsInCall():
1366        ad.log.error("Not in call.")
1367        return False
1368    if not ad.droid.telephonyIsImsRegistered():
1369        ad.log.info("IMS is not registered.")
1370        return False
1371    if not ad.droid.telephonyIsWifiCallingAvailable():
1372        ad.log.info("IsWifiCallingAvailble is False")
1373        return False
1374    if not call_id:
1375        call_ids = ad.droid.telecomCallGetCallIds()
1376        if call_ids:
1377            call_id = call_ids[-1]
1378    if not call_id:
1379        ad.log.error("Failed to get call id")
1380        return False
1381    else:
1382        call_prop = ad.droid.telecomCallGetProperties(call_id)
1383        if "WIFI" not in call_prop:
1384            ad.log.info("callProperties = %s, expecting WIFI", call_prop)
1385            return False
1386    nw_type = get_network_rat(log, ad, NETWORK_SERVICE_DATA)
1387    if nw_type != RAT_IWLAN:
1388        ad.log.warning("Data rat on: %s. Expected: iwlan", nw_type)
1389    return True
1390
1391
1392def is_phone_in_call_not_iwlan(log, ad):
1393    """Return if phone is in WiFi call for subscription id.
1394
1395    Args:
1396        ad: Android device object.
1397        sub_id: subscription id.
1398    """
1399    if not ad.droid.telecomIsInCall():
1400        ad.log.error("Not in call.")
1401        return False
1402    nw_type = get_network_rat(log, ad, NETWORK_SERVICE_DATA)
1403    if nw_type == RAT_IWLAN:
1404        ad.log.error("Data rat on: %s. Expected: not iwlan", nw_type)
1405        return False
1406    if is_wfc_enabled(log, ad):
1407        ad.log.error("WiFi Calling feature bit is True.")
1408        return False
1409    return True
1410
1411
1412def swap_calls(log,
1413               ads,
1414               call_hold_id,
1415               call_active_id,
1416               num_swaps=1,
1417               check_call_status=True):
1418    """PhoneA in call with B and C. Swap active/holding call on PhoneA.
1419
1420    Swap call and check status on PhoneA.
1421        (This step may have multiple times according to 'num_swaps'.)
1422    Check if all 3 phones are 'in-call'.
1423
1424    Args:
1425        ads: list of ad object, at least three need to pass in.
1426            Swap operation will happen on ads[0].
1427            ads[1] and ads[2] are call participants.
1428        call_hold_id: id for the holding call in ads[0].
1429            call_hold_id should be 'STATE_HOLDING' when calling this function.
1430        call_active_id: id for the active call in ads[0].
1431            call_active_id should be 'STATE_ACTIVE' when calling this function.
1432        num_swaps: how many swap/check operations will be done before return.
1433        check_call_status: This is optional. Default value is True.
1434            If this value is True, then call status (active/hold) will be
1435            be checked after each swap operation.
1436
1437    Returns:
1438        If no error happened, return True, otherwise, return False.
1439    """
1440    if check_call_status:
1441        # Check status before swap.
1442        if ads[0].droid.telecomCallGetCallState(
1443                call_active_id) != CALL_STATE_ACTIVE:
1444            ads[0].log.error(
1445                "Call_id:%s, state:%s, expected: STATE_ACTIVE", call_active_id,
1446                ads[0].droid.telecomCallGetCallState(call_active_id))
1447            return False
1448        if ads[0].droid.telecomCallGetCallState(
1449                call_hold_id) != CALL_STATE_HOLDING:
1450            ads[0].log.error(
1451                "Call_id:%s, state:%s, expected: STATE_HOLDING", call_hold_id,
1452                ads[0].droid.telecomCallGetCallState(call_hold_id))
1453            return False
1454
1455    i = 1
1456    while (i <= num_swaps):
1457        ads[0].log.info("swap_test %s: swap and check call status.", i)
1458        ads[0].droid.telecomCallHold(call_active_id)
1459        time.sleep(WAIT_TIME_IN_CALL)
1460        # Swap object reference
1461        call_active_id, call_hold_id = call_hold_id, call_active_id
1462        if check_call_status:
1463            # Check status
1464            if ads[0].droid.telecomCallGetCallState(
1465                    call_active_id) != CALL_STATE_ACTIVE:
1466                ads[0].log.error(
1467                    "Call_id:%s, state:%s, expected: STATE_ACTIVE",
1468                    call_active_id,
1469                    ads[0].droid.telecomCallGetCallState(call_active_id))
1470                return False
1471            if ads[0].droid.telecomCallGetCallState(
1472                    call_hold_id) != CALL_STATE_HOLDING:
1473                ads[0].log.error(
1474                    "Call_id:%s, state:%s, expected: STATE_HOLDING",
1475                    call_hold_id,
1476                    ads[0].droid.telecomCallGetCallState(call_hold_id))
1477                return False
1478        # TODO: b/26296375 add voice check.
1479
1480        i += 1
1481
1482    #In the end, check all three phones are 'in-call'.
1483    if not verify_incall_state(log, [ads[0], ads[1], ads[2]], True):
1484        return False
1485
1486    return True
1487
1488
1489def get_audio_route(log, ad):
1490    """Gets the audio route for the active call
1491
1492    Args:
1493        log: logger object
1494        ad: android_device object
1495
1496    Returns:
1497        Audio route string ["BLUETOOTH", "EARPIECE", "SPEAKER", "WIRED_HEADSET"
1498            "WIRED_OR_EARPIECE"]
1499    """
1500
1501    audio_state = ad.droid.telecomCallGetAudioState()
1502    return audio_state["AudioRoute"]
1503
1504
1505def set_audio_route(log, ad, route):
1506    """Sets the audio route for the active call
1507
1508    Args:
1509        log: logger object
1510        ad: android_device object
1511        route: string ["BLUETOOTH", "EARPIECE", "SPEAKER", "WIRED_HEADSET"
1512            "WIRED_OR_EARPIECE"]
1513
1514    Returns:
1515        If no error happened, return True, otherwise, return False.
1516    """
1517    ad.droid.telecomCallSetAudioRoute(route)
1518    return True
1519
1520
1521def is_property_in_call_properties(log, ad, call_id, expected_property):
1522    """Return if the call_id has the expected property
1523
1524    Args:
1525        log: logger object
1526        ad: android_device object
1527        call_id: call id.
1528        expected_property: expected property.
1529
1530    Returns:
1531        True if call_id has expected_property. False if not.
1532    """
1533    properties = ad.droid.telecomCallGetProperties(call_id)
1534    return (expected_property in properties)
1535
1536
1537def is_call_hd(log, ad, call_id):
1538    """Return if the call_id is HD call.
1539
1540    Args:
1541        log: logger object
1542        ad: android_device object
1543        call_id: call id.
1544
1545    Returns:
1546        True if call_id is HD call. False if not.
1547    """
1548    return is_property_in_call_properties(log, ad, call_id,
1549                                          CALL_PROPERTY_HIGH_DEF_AUDIO)
1550
1551
1552def get_cep_conference_call_id(ad):
1553    """Get CEP conference call id if there is an ongoing CEP conference call.
1554
1555    Args:
1556        ad: android device object.
1557
1558    Returns:
1559        call id for CEP conference call if there is an ongoing CEP conference call.
1560        None otherwise.
1561    """
1562    for call in ad.droid.telecomCallGetCallIds():
1563        if len(ad.droid.telecomCallGetCallChildren(call)) != 0:
1564            return call
1565    return None
1566