1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - 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"""
17    Test Script for Telephony Pre Check In Sanity
18"""
19
20import re
21import time
22from acts import signals
23from acts.test_decorators import test_tracker_info
24from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
25from acts.test_utils.tel.tel_defines import CAPABILITY_WFC
26from acts.test_utils.tel.tel_defines import DEFAULT_DEVICE_PASSWORD
27from acts.test_utils.tel.tel_defines import PHONE_TYPE_CDMA
28from acts.test_utils.tel.tel_defines import WFC_MODE_WIFI_PREFERRED
29from acts.test_utils.tel.tel_lookup_tables import operator_capabilities
30from acts.test_utils.tel.tel_test_utils import abort_all_tests
31from acts.test_utils.tel.tel_test_utils import call_setup_teardown
32from acts.test_utils.tel.tel_test_utils import dumpsys_last_call_info
33from acts.test_utils.tel.tel_test_utils import dumpsys_last_call_number
34from acts.test_utils.tel.tel_test_utils import dumpsys_new_call_info
35from acts.test_utils.tel.tel_test_utils import ensure_phone_default_state
36from acts.test_utils.tel.tel_test_utils import get_operator_name
37from acts.test_utils.tel.tel_test_utils import get_service_state_by_adb
38from acts.test_utils.tel.tel_test_utils import fastboot_wipe
39from acts.test_utils.tel.tel_test_utils import hangup_call_by_adb
40from acts.test_utils.tel.tel_test_utils import initiate_call
41from acts.test_utils.tel.tel_test_utils import is_sim_lock_enabled
42from acts.test_utils.tel.tel_test_utils import initiate_emergency_dialer_call_by_adb
43from acts.test_utils.tel.tel_test_utils import last_call_drop_reason
44from acts.test_utils.tel.tel_test_utils import reset_device_password
45from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode_by_adb
46from acts.test_utils.tel.tel_test_utils import unlock_sim
47from acts.test_utils.tel.tel_test_utils import verify_internet_connection
48from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection
49from acts.test_utils.tel.tel_test_utils import wait_for_sim_ready_by_adb
50from acts.test_utils.tel.tel_voice_utils import phone_setup_csfb
51from acts.test_utils.tel.tel_voice_utils import phone_setup_iwlan
52from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_3g
53from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_2g
54from acts.utils import get_current_epoch_time
55
56CARRIER_OVERRIDE_CMD = (
57    "am broadcast -a com.google.android.carrier.action.LOCAL_OVERRIDE -n "
58    "com.google.android.carrier/.ConfigOverridingReceiver --ez")
59IMS_FIRST = "carrier_use_ims_first_for_emergency_bool"
60ALLOW_NON_EMERGENCY_CALL = "allow_non_emergency_calls_in_ecm_bool"
61BLOCK_DURATION_CMD = "duration_blocking_disabled_after_emergency_int"
62BLOCK_DURATION = 300
63
64
65class TelLiveEmergencyBase(TelephonyBaseTest):
66    def setup_class(self):
67        TelephonyBaseTest.setup_class(self)
68        self.number_of_devices = 1
69        fake_number = self.user_params.get("fake_emergency_number", "411")
70        self.fake_emergency_number = fake_number.strip("+").replace("-", "")
71        self.my_devices = self.android_devices[:]
72
73        for ad in self.android_devices:
74            if not is_sim_lock_enabled(ad):
75                self.setup_dut(ad)
76                return True
77        self.log.error("No device meets SIM READY or LOADED requirement")
78        raise signals.TestAbortClass("No device meets SIM requirement")
79
80    def setup_dut(self, ad):
81        self.dut = ad
82        output = self.dut.adb.shell("dumpsys carrier_config")
83        self.default_settings = {}
84        for setting in (IMS_FIRST, ALLOW_NON_EMERGENCY_CALL,
85                        BLOCK_DURATION_CMD):
86            values = re.findall(r"%s = (\S+)" % setting, output)
87            if values:
88                self.default_settings[setting] = values[-1]
89            else:
90                self.default_settings[setting] = ""
91        self.dut.adb.shell(" ".join(
92            [CARRIER_OVERRIDE_CMD, BLOCK_DURATION_CMD,
93             "%s" % BLOCK_DURATION]))
94        self.dut_operator = get_operator_name(self.log, ad)
95        if self.dut_operator == "tmo":
96            self.fake_emergency_number = "611"
97        elif self.dut_operator == "vzw":
98            self.fake_emergency_number = "922"
99        elif self.dut_operator == "spt":
100            self.fake_emergency_number = "526"
101        if len(self.my_devices) > 1:
102            self.android_devices.remove(ad)
103            self.android_devices.insert(0, ad)
104
105    def teardown_class(self):
106        self.android_devices = self.my_devices
107        TelephonyBaseTest.teardown_class(self)
108
109    def setup_test(self):
110        if not unlock_sim(self.dut):
111            abort_all_tests(self.dut.log, "unable to unlock SIM")
112        self.expected_call_result = True
113
114    def teardown_test(self):
115        self.dut.ensure_screen_on()
116        self.dut.exit_setup_wizard()
117        reset_device_password(self.dut, None)
118        output = self.dut.adb.shell("dumpsys carrier_config")
119        for setting, state in self.default_settings.items():
120            values = re.findall(r"%s = (\S+)" % setting, output)
121            if values and values[-1] != state:
122                self.dut.adb.shell(" ".join(
123                    [CARRIER_OVERRIDE_CMD, setting, state]))
124        ensure_phone_default_state(self.log, self.dut)
125
126    def change_emergency_number_list(self):
127        test_number = "%s:%s" % (self.fake_emergency_number,
128                                 self.fake_emergency_number)
129        output = self.dut.adb.getprop("ril.test.emergencynumber")
130        if output != test_number:
131            cmd = "setprop ril.test.emergencynumber %s" % test_number
132            self.dut.log.info(cmd)
133            self.dut.adb.shell(cmd)
134        for _ in range(5):
135            existing = self.dut.adb.getprop("ril.ecclist")
136            self.dut.log.info("Existing ril.ecclist is: %s", existing)
137            if self.fake_emergency_number in existing:
138                return True
139            emergency_numbers = "%s,%s" % (existing,
140                                           self.fake_emergency_number)
141            cmd = "setprop ril.ecclist %s" % emergency_numbers
142            self.dut.log.info(cmd)
143            self.dut.adb.shell(cmd)
144            # After some system events, ril.ecclist might change
145            # wait sometime for it to settle
146            time.sleep(10)
147            if self.fake_emergency_number in existing:
148                return True
149        return False
150
151    def change_qcril_emergency_source_mcc_table(self):
152        # This will add the fake number into emergency number list for a mcc
153        # in qcril. Please note, the fake number will be send as an emergency
154        # number by modem and reach the real 911 by this
155        qcril_database_path = self.dut.adb.shell("find /data -iname  qcril.db")
156        if not qcril_database_path: return
157        mcc = self.dut.droid.telephonyGetNetworkOperator()
158        mcc = mcc[:3]
159        self.dut.log.info("Add %s mcc %s in qcril_emergency_source_mcc_table")
160        self.dut.adb.shell(
161            "sqlite3 %s \"INSERT INTO qcril_emergency_source_mcc_table VALUES('%s','%s','','')\""
162            % (qcril_database_path, mcc, self.fake_emergency_number))
163
164    def fake_emergency_call_test(self, by_emergency_dialer=True, attemps=3):
165        self.dut.log.info("ServiceState is in %s",
166                          get_service_state_by_adb(self.log, self.dut))
167        if by_emergency_dialer:
168            dialing_func = initiate_emergency_dialer_call_by_adb
169            callee = self.fake_emergency_number
170        else:
171            dialing_func = initiate_call
172            # Initiate_call method has to have "+" in front
173            # otherwise the number will be in dialer without dial out
174            # with sl4a fascade. Need further investigation
175            callee = "+%s" % self.fake_emergency_number
176        for i in range(attemps):
177            begin_time = get_current_epoch_time()
178            result = True
179            if not self.change_emergency_number_list():
180                self.dut.log.error("Unable to add number to ril.ecclist")
181                return False
182            time.sleep(1)
183            last_call_number = dumpsys_last_call_number(self.dut)
184            call_result = dialing_func(self.log, self.dut, callee)
185            time.sleep(3)
186            hangup_call_by_adb(self.dut)
187            if not call_result:
188                last_call_drop_reason(self.dut, begin_time)
189            self.dut.send_keycode("BACK")
190            self.dut.send_keycode("BACK")
191            if not dumpsys_new_call_info(self.dut, last_call_number):
192                result = False
193            if call_result == self.expected_call_result:
194                self.dut.log.info("Call to %s returns %s as expected", callee,
195                                  self.expected_call_result)
196            else:
197                self.dut.log.info("Call to %s returns %s", callee,
198                                  not self.expected_call_result)
199                result = False
200            if result:
201                return True
202            ecclist = self.dut.adb.getprop("ril.ecclist")
203            self.dut.log.info("ril.ecclist = %s", ecclist)
204            if self.fake_emergency_number in ecclist:
205                if i == attemps - 1:
206                    self.dut.log.error("%s is in ril-ecclist, but call failed",
207                                       self.fake_emergency_number)
208                else:
209                    self.dut.log.warning(
210                        "%s is in ril-ecclist, but call failed, try again",
211                        self.fake_emergency_number)
212            else:
213                if i == attemps - 1:
214                    self.dut.log.error("Fail to write %s to ril-ecclist",
215                                       self.fake_emergency_number)
216                else:
217                    self.dut.log.info("%s is not in ril-ecclist",
218                                      self.fake_emergency_number)
219        self.dut.log.info("fake_emergency_call_test result is %s", result)
220        return result
221
222    def set_ims_first(self, state):
223        output = self.dut.adb.shell(
224            "dumpsys carrier_config | grep %s | tail -1" % IMS_FIRST)
225        self.dut.log.info(output)
226        if state in output: return
227        cmd = " ".join([CARRIER_OVERRIDE_CMD, IMS_FIRST, state])
228        self.dut.log.info(cmd)
229        self.dut.adb.shell(cmd)
230        self.dut.log.info(
231            self.dut.adb.shell(
232                "dumpsys carrier_config | grep %s | tail -1" % IMS_FIRST))
233
234    def set_allow_non_emergency_call(self, state):
235        output = self.dut.adb.shell(
236            "dumpsys carrier_config | grep %s | tail -1" %
237            ALLOW_NON_EMERGENCY_CALL)
238        self.dut.log.info(output)
239        if state in output: return
240        cmd = " ".join([CARRIER_OVERRIDE_CMD, ALLOW_NON_EMERGENCY_CALL, state])
241        self.dut.log.info(cmd)
242        self.dut.adb.shell(cmd)
243        self.dut.log.info(
244            self.dut.adb.shell("dumpsys carrier_config | grep %s | tail -1" %
245                               ALLOW_NON_EMERGENCY_CALL))
246
247    def check_emergency_call_back_mode(self,
248                                       by_emergency_dialer=True,
249                                       non_emergency_call_allowed=True,
250                                       attemps=3):
251        state = "true" if non_emergency_call_allowed else "false"
252        self.set_allow_non_emergency_call(state)
253        result = True
254        for _ in range(attemps):
255            if not self.change_emergency_number_list():
256                self.dut.log.error("Unable to add number to ril.ecclist")
257                return False
258            last_call_number = dumpsys_last_call_number(self.dut)
259            self.dut.log.info("Hung up fake emergency call in ringing")
260            if by_emergency_dialer:
261                initiate_emergency_dialer_call_by_adb(
262                    self.log, self.dut, self.fake_emergency_number, timeout=0)
263            else:
264                callee = "+%s" % self.fake_emergency_number
265                self.dut.droid.telecomCallNumber(callee)
266            time.sleep(3)
267            hangup_call_by_adb(self.dut)
268            ecclist = self.dut.adb.getprop("ril.ecclist")
269            self.dut.log.info("ril.ecclist = %s", ecclist)
270            if self.fake_emergency_number in ecclist:
271                break
272        call_info = dumpsys_new_call_info(self.dut, last_call_number)
273        if not call_info:
274            return False
275        call_tech = call_info.get("callTechnologies", "")
276        if "CDMA" in call_tech:
277            expected_ecbm = True
278            expected_data = False
279            expected_call = non_emergency_call_allowed
280        elif "GSM" in call_tech:
281            expected_ecbm = True
282            expected_data = True
283            expected_call = True
284        else:
285            expected_ecbm = False
286            expected_data = True
287            expected_call = True
288        last_call_number = dumpsys_last_call_number(self.dut)
289        begin_time = get_current_epoch_time()
290        self.dut.ensure_screen_on()
291        self.dut.exit_setup_wizard()
292        reset_device_password(self.dut, None)
293        call_check = call_setup_teardown(
294            self.log, self.dut, self.android_devices[1], ad_hangup=self.dut)
295        if call_check != expected_call:
296            self.dut.log.error("Regular phone call is %s, expecting %s",
297                               call_check, expected_call)
298            result = False
299        call_info = dumpsys_new_call_info(self.dut, last_call_number)
300        if not call_info:
301            self.dut.log.error("New call is not in dumpsys telecom")
302            return False
303        if expected_ecbm:
304            if "ecbm" in call_info["callProperties"]:
305                self.dut.log.info("New call is in ecbm.")
306            else:
307                self.dut.log.error(
308                    "New call is not in emergency call back mode.")
309                result = False
310        else:
311            if "ecbm" in call_info["callProperties"]:
312                self.dut.log.error("New call is in emergency call back mode")
313                result = False
314        if not expected_data:
315            if self.dut.droid.telephonyGetDataConnectionState():
316                self.dut.log.info(
317                    "Data connection is off as expected in ECB mode")
318                self.dut.log.info("Wait for getting out of ecbm")
319                if not wait_for_cell_data_connection(self.log, self.dut, True,
320                                                     400):
321                    self.dut.log.error(
322                        "Data connection didn't come back after 5 minutes")
323                    result = False
324                #if not self.dut.droid.telephonyGetDataConnectionState():
325                #    self.dut.log.error(
326                #        "Data connection is not coming back")
327                #    result = False
328                elif not verify_internet_connection(self.log, self.dut):
329                    self.dut.log.error(
330                        "Internet connection check failed after getting out of ECB"
331                    )
332                    result = False
333
334            else:
335                self.dut.log.error("Data connection is not off in ECB mode")
336                if not verify_internet_connection(self.log, self.dut, False):
337                    self.dut.log.error("Internet connection is not off")
338                    result = False
339        else:
340            if self.dut.droid.telephonyGetDataConnectionState():
341                self.dut.log.info("Data connection is on as expected")
342                if not verify_internet_connection(self.log, self.dut):
343                    self.dut.log.error("Internet connection check failed")
344                    result = False
345            else:
346                self.dut.log.error("Data connection is off, expecting on")
347                result = False
348        if expected_call:
349            return result
350        elapsed_time = (get_current_epoch_time() - begin_time) / 1000
351        if elapsed_time < BLOCK_DURATION:
352            time.sleep(BLOCK_DURATION - elapsed_time + 10)
353        if not call_setup_teardown(
354                self.log, self.dut, self.android_devices[1],
355                ad_hangup=self.dut):
356            self.dut.log.error("Regular phone call failed after out of ecbm")
357            result = False
358        return result
359
360    def check_normal_call(self):
361        result = True
362        if "wfc" not in self.test_name:
363            toggle_airplane_mode_by_adb(self.log, self.dut, False)
364        self.dut.ensure_screen_on()
365        self.dut.exit_setup_wizard()
366        reset_device_password(self.dut, None)
367        begin_time = get_current_epoch_time()
368        if not call_setup_teardown(
369                self.log,
370                self.android_devices[1],
371                self.dut,
372                ad_hangup=self.android_devices[1]):
373            self.dut.log.error("Regular MT phone call fails")
374            self.dut.log.info("call_info = %s", dumpsys_last_call_info(
375                self.dut))
376            result = False
377        if not call_setup_teardown(
378                self.log, self.dut, self.android_devices[1],
379                ad_hangup=self.dut):
380            self.dut.log.error("Regular MO phone call fails")
381            self.dut.log.info("call_info = %s", dumpsys_last_call_info(
382                self.dut))
383            result = False
384        return result
385