1#!/usr/bin/env python3
2#
3#   Copyright 2021 - 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 re
18import time
19
20from acts.controllers.android_device import SL4A_APK_NAME
21from acts.controllers.android_device import list_adb_devices
22from acts.controllers.android_device import list_fastboot_devices
23from acts_contrib.test_utils.tel.tel_ims_utils import activate_wfc_on_device
24from acts_contrib.test_utils.tel.tel_logging_utils import set_qxdm_logger_command
25from acts_contrib.test_utils.tel.tel_logging_utils import start_qxdm_logger
26from acts_contrib.test_utils.tel.tel_test_utils import abort_all_tests
27from acts_contrib.test_utils.tel.tel_test_utils import bring_up_sl4a
28from acts_contrib.test_utils.tel.tel_test_utils import refresh_sl4a_session
29from acts_contrib.test_utils.tel.tel_test_utils import set_phone_silent_mode
30from acts_contrib.test_utils.tel.tel_test_utils import synchronize_device_time
31from acts_contrib.test_utils.tel.tel_test_utils import unlock_sim
32
33
34def fastboot_wipe(ad, skip_setup_wizard=True):
35    """Wipe the device in fastboot mode.
36
37    Pull sl4a apk from device. Terminate all sl4a sessions,
38    Reboot the device to bootloader, wipe the device by fastboot.
39    Reboot the device. wait for device to complete booting
40    Re-intall and start an sl4a session.
41    """
42    status = True
43    # Pull sl4a apk from device
44    out = ad.adb.shell("pm path %s" % SL4A_APK_NAME)
45    result = re.search(r"package:(.*)", out)
46    if not result:
47        ad.log.error("Couldn't find sl4a apk")
48    else:
49        sl4a_apk = result.group(1)
50        ad.log.info("Get sl4a apk from %s", sl4a_apk)
51        ad.pull_files([sl4a_apk], "/tmp/")
52    ad.stop_services()
53    attempts = 3
54    for i in range(1, attempts + 1):
55        try:
56            if ad.serial in list_adb_devices():
57                ad.log.info("Reboot to bootloader")
58                ad.adb.reboot("bootloader", ignore_status=True)
59                time.sleep(10)
60            if ad.serial in list_fastboot_devices():
61                ad.log.info("Wipe in fastboot")
62                ad.fastboot._w(timeout=300, ignore_status=True)
63                time.sleep(30)
64                ad.log.info("Reboot in fastboot")
65                ad.fastboot.reboot()
66            ad.wait_for_boot_completion()
67            ad.root_adb()
68            if ad.skip_sl4a:
69                break
70            if ad.is_sl4a_installed():
71                break
72            ad.log.info("Re-install sl4a")
73            ad.adb.shell("settings put global verifier_verify_adb_installs 0")
74            ad.adb.install("-r /tmp/base.apk")
75            time.sleep(10)
76            break
77        except Exception as e:
78            ad.log.warning(e)
79            if i == attempts:
80                abort_all_tests(ad.log, str(e))
81            time.sleep(5)
82    try:
83        ad.start_adb_logcat()
84    except:
85        ad.log.error("Failed to start adb logcat!")
86    if skip_setup_wizard:
87        ad.exit_setup_wizard()
88    if getattr(ad, "qxdm_log", True):
89        set_qxdm_logger_command(ad, mask=getattr(ad, "qxdm_log_mask", None))
90        start_qxdm_logger(ad)
91    if ad.skip_sl4a: return status
92    bring_up_sl4a(ad)
93    synchronize_device_time(ad)
94    set_phone_silent_mode(ad.log, ad)
95    # Activate WFC on Verizon, AT&T and Canada operators as per # b/33187374 &
96    # b/122327716
97    activate_wfc_on_device(ad.log, ad)
98    return status
99
100
101def flash_radio(ad, file_path, skip_setup_wizard=True, sideload_img=True):
102    """Flash radio image or modem binary.
103
104    Args:
105        file_path: The file path of test radio(radio.img)/binary(modem.bin).
106        skip_setup_wizard: Skip Setup Wizard if True.
107        sideload_img: True to flash radio, False to flash modem.
108    """
109    ad.stop_services()
110    ad.log.info("Reboot to bootloader")
111    ad.adb.reboot_bootloader(ignore_status=True)
112    ad.log.info("Sideload radio in fastboot")
113    try:
114        if sideload_img:
115            ad.fastboot.flash("radio %s" % file_path, timeout=300)
116        else:
117            ad.fastboot.flash("modem %s" % file_path, timeout=300)
118    except Exception as e:
119        ad.log.error(e)
120    ad.fastboot.reboot("bootloader")
121    time.sleep(5)
122    output = ad.fastboot.getvar("version-baseband")
123    result = re.search(r"version-baseband: (\S+)", output)
124    if not result:
125        ad.log.error("fastboot getvar version-baseband output = %s", output)
126        abort_all_tests(ad.log, "Radio version-baseband is not provided")
127    fastboot_radio_version_output = result.group(1)
128    for _ in range(2):
129        try:
130            ad.log.info("Reboot in fastboot")
131            ad.fastboot.reboot()
132            ad.wait_for_boot_completion()
133            break
134        except Exception as e:
135            ad.log.error("Exception error %s", e)
136    ad.root_adb()
137    adb_radio_version_output = ad.adb.getprop("gsm.version.baseband")
138    ad.log.info("adb getprop gsm.version.baseband = %s",
139                adb_radio_version_output)
140    if fastboot_radio_version_output not in adb_radio_version_output:
141        msg = ("fastboot radio version output %s does not match with adb"
142               " radio version output %s" % (fastboot_radio_version_output,
143                                             adb_radio_version_output))
144        abort_all_tests(ad.log, msg)
145    if not ad.ensure_screen_on():
146        ad.log.error("User window cannot come up")
147    ad.start_services(skip_setup_wizard=skip_setup_wizard)
148    unlock_sim(ad)
149
150
151def reset_device_password(ad, device_password=None):
152    # Enable or Disable Device Password per test bed config
153    unlock_sim(ad)
154    screen_lock = ad.is_screen_lock_enabled()
155    if device_password:
156        try:
157            refresh_sl4a_session(ad)
158            ad.droid.setDevicePassword(device_password)
159        except Exception as e:
160            ad.log.warning("setDevicePassword failed with %s", e)
161            try:
162                ad.droid.setDevicePassword(device_password, "1111")
163            except Exception as e:
164                ad.log.warning(
165                    "setDevicePassword providing previous password error: %s",
166                    e)
167        time.sleep(2)
168        if screen_lock:
169            # existing password changed
170            return
171        else:
172            # enable device password and log in for the first time
173            ad.log.info("Enable device password")
174            ad.adb.wait_for_device(timeout=180)
175    else:
176        if not screen_lock:
177            # no existing password, do not set password
178            return
179        else:
180            # password is enabled on the device
181            # need to disable the password and log in on the first time
182            # with unlocking with a swipe
183            ad.log.info("Disable device password")
184            ad.unlock_screen(password="1111")
185            refresh_sl4a_session(ad)
186            ad.ensure_screen_on()
187            try:
188                ad.droid.disableDevicePassword()
189            except Exception as e:
190                ad.log.warning("disableDevicePassword failed with %s", e)
191                fastboot_wipe(ad)
192            time.sleep(2)
193            ad.adb.wait_for_device(timeout=180)
194    refresh_sl4a_session(ad)
195    if not ad.is_adb_logcat_on:
196        ad.start_adb_logcat()