blob: 443cbfd210a8357412a8b618a26325d56b6ba107 [file] [log] [blame]
jasonkmluff0d5b92019-03-21 11:24:45 +08001#!/usr/bin/env python3.5
2#
3# Copyright 2019 - 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
18import re
19import os
jasonkmlu903ece82019-05-16 14:56:19 +080020import math
21import collections
22import shutil
jasonkmlu4fcdcad2019-07-10 14:35:12 +080023import fnmatch
24import posixpath
jasonkmluff0d5b92019-03-21 11:24:45 +080025
26from acts import utils
27from acts import signals
28from acts.controllers.android_device import list_adb_devices
29from acts.controllers.android_device import list_fastboot_devices
30from acts.controllers.android_device import DEFAULT_QXDM_LOG_PATH
31from acts.controllers.android_device import SL4A_APK_NAME
32from acts.test_utils.wifi import wifi_test_utils as wutils
33from acts.test_utils.tel import tel_test_utils as tutils
34from acts.utils import get_current_epoch_time
35
36WifiEnums = wutils.WifiEnums
37PULL_TIMEOUT = 300
jasonkmlu66fda532019-07-16 11:12:45 +080038GNSSSTATUS_LOG_PATH = (
39 "/storage/emulated/0/Android/data/com.android.gpstool/files/")
jasonkmlu214f0d62019-04-08 19:53:59 +080040QXDM_MASKS = ["GPS.cfg", "GPS-general.cfg", "default.cfg"]
jasonkmlu4fcdcad2019-07-10 14:35:12 +080041TTFF_REPORT = collections.namedtuple(
42 "TTFF_REPORT", "ttff_loop ttff_sec ttff_pe ttff_cn")
43TRACK_REPORT = collections.namedtuple(
44 "TRACK_REPORT", "track_l5flag track_pe track_top4cn track_cn")
jasonkmlu38bd4d12019-10-04 16:38:48 +080045LOCAL_PROP_FILE_CONTENTS = """\
46log.tag.LocationManagerService=VERBOSE
47log.tag.GnssLocationProvider=VERBOSE
48log.tag.GnssMeasurementsProvider=VERBOSE
49log.tag.GpsNetInitiatedHandler=VERBOSE
50log.tag.GnssNetworkConnectivityHandler=VERBOSE
51log.tag.ConnectivityService=VERBOSE
52log.tag.ConnectivityManager=VERBOSE
53log.tag.GnssVisibilityControl=VERBOSE"""
jasonkmluff0d5b92019-03-21 11:24:45 +080054
55
56class GnssTestUtilsError(Exception):
57 pass
58
jasonkmlu0f546de2019-12-13 20:58:41 +080059
jasonkmluff0d5b92019-03-21 11:24:45 +080060def remount_device(ad):
61 """Remount device file system to read and write.
62
63 Args:
64 ad: An AndroidDevice object.
65 """
jasonkmlu81b8b032019-06-21 15:18:27 +080066 for retries in range(5):
jasonkmluff0d5b92019-03-21 11:24:45 +080067 ad.root_adb()
68 remount_result = ad.adb.remount()
69 ad.log.info("Attempt %d - %s" % (retries + 1, remount_result))
jasonkmlu81b8b032019-06-21 15:18:27 +080070 if "remount succeeded" in remount_result:
jasonkmluff0d5b92019-03-21 11:24:45 +080071 break
72 if ad.adb.getprop("ro.boot.veritymode") == "enforcing":
jasonkmluff0d5b92019-03-21 11:24:45 +080073 disable_verity_result = ad.adb.disable_verity()
jasonkmlu214f0d62019-04-08 19:53:59 +080074 reboot(ad)
75
jasonkmlu0f546de2019-12-13 20:58:41 +080076
jasonkmlu214f0d62019-04-08 19:53:59 +080077def reboot(ad):
78 """Reboot device and check if mobile data is available.
79
80 Args:
81 ad: An AndroidDevice object.
82 """
83 ad.log.info("Reboot device to make changes take effect.")
84 ad.reboot()
85 ad.unlock_screen(password=None)
86 if not int(ad.adb.shell("settings get global mobile_data")) == 1:
87 set_mobile_data(ad, True)
jasonkmlu49c32812019-04-12 18:11:10 +080088 utils.sync_device_time(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +080089
jasonkmlu0f546de2019-12-13 20:58:41 +080090
jasonkmluff0d5b92019-03-21 11:24:45 +080091def enable_gnss_verbose_logging(ad):
jasonkmlu81b8b032019-06-21 15:18:27 +080092 """Enable GNSS VERBOSE Logging and persistent logcat.
jasonkmluff0d5b92019-03-21 11:24:45 +080093
94 Args:
95 ad: An AndroidDevice object.
96 """
97 remount_device(ad)
jasonkmlu97ac56e2019-05-29 15:04:51 +080098 ad.log.info("Enable GNSS VERBOSE Logging and persistent logcat.")
jasonkmluff0d5b92019-03-21 11:24:45 +080099 ad.adb.shell("echo DEBUG_LEVEL = 5 >> /vendor/etc/gps.conf")
jasonkmlu38bd4d12019-10-04 16:38:48 +0800100 ad.adb.shell("echo %r >> /data/local.prop" % LOCAL_PROP_FILE_CONTENTS)
jasonkmluff0d5b92019-03-21 11:24:45 +0800101 ad.adb.shell("chmod 644 /data/local.prop")
jasonkmluc1ec3052019-10-25 14:57:53 +0800102 ad.adb.shell("setprop persist.logd.logpersistd.size 20000")
jasonkmlu180a08c2019-04-23 17:24:55 +0800103 ad.adb.shell("setprop persist.logd.size 16777216")
jasonkmluff0d5b92019-03-21 11:24:45 +0800104 ad.adb.shell("setprop persist.vendor.radio.adb_log_on 1")
jasonkmlu81b8b032019-06-21 15:18:27 +0800105 ad.adb.shell("setprop persist.logd.logpersistd logcatd")
jasonkmluff0d5b92019-03-21 11:24:45 +0800106 ad.adb.shell("setprop log.tag.copresGcore VERBOSE")
107 ad.adb.shell("sync")
108
jasonkmlu0f546de2019-12-13 20:58:41 +0800109
jasonkmlu81b8b032019-06-21 15:18:27 +0800110def enable_compact_and_particle_fusion_log(ad):
111 """Enable CompactLog and FLP particle fusion log.
112
113 Args:
114 ad: An AndroidDevice object.
115 """
116 ad.root_adb()
117 ad.log.info("Enable CompactLog and FLP particle fusion log.")
118 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
119 "GSERVICES_OVERRIDE -e location:compact_log_enabled true")
120 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
jasonkmlueb9183d2019-08-06 10:52:58 +0800121 "GSERVICES_OVERRIDE -e location:proks_config 28")
122 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
jasonkmlud8b2acf2019-08-23 16:48:27 +0800123 "GSERVICES_OVERRIDE -e location:flp_use_particle_fusion true")
jasonkmlu81b8b032019-06-21 15:18:27 +0800124 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
jasonkmlud8b2acf2019-08-23 16:48:27 +0800125 "GSERVICES_OVERRIDE -e "
126 "location:flp_particle_fusion_extended_bug_report true")
jasonkmlueb9183d2019-08-06 10:52:58 +0800127 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
jasonkmlud8b2acf2019-08-23 16:48:27 +0800128 "GSERVICES_OVERRIDE -e location:flp_event_log_size 86400")
129 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
130 "GSERVICES_OVERRIDE -e "
131 "location:flp_particle_fusion_bug_report_window_sec 86400")
132 ad.adb.shell("am broadcast -a com.google.gservices.intent.action."
133 "GSERVICES_OVERRIDE -e location:"
134 "flp_particle_fusion_bug_report_max_buffer_size 86400")
jasonkmlu81b8b032019-06-21 15:18:27 +0800135 ad.adb.shell("am force-stop com.google.android.gms")
136 ad.adb.shell("am broadcast -a com.google.android.gms.INITIALIZE")
jasonkmlud8b2acf2019-08-23 16:48:27 +0800137 ad.adb.shell("dumpsys activity service com.google.android.location."
138 "internal.GoogleLocationManagerService")
jasonkmlu81b8b032019-06-21 15:18:27 +0800139
jasonkmlu0f546de2019-12-13 20:58:41 +0800140
jasonkmluff0d5b92019-03-21 11:24:45 +0800141def disable_xtra_throttle(ad):
142 """Disable XTRA throttle will have no limit to download XTRA data.
143
144 Args:
145 ad: An AndroidDevice object.
146 """
147 remount_device(ad)
148 ad.log.info("Disable XTRA Throttle.")
149 ad.adb.shell("echo XTRA_TEST_ENABLED=1 >> /vendor/etc/gps.conf")
150 ad.adb.shell("echo XTRA_THROTTLE_ENABLED=0 >> /vendor/etc/gps.conf")
151
jasonkmlu0f546de2019-12-13 20:58:41 +0800152
jasonkmluff0d5b92019-03-21 11:24:45 +0800153def enable_supl_mode(ad):
154 """Enable SUPL back on for next test item.
155
156 Args:
157 ad: An AndroidDevice object.
158 """
159 remount_device(ad)
160 ad.log.info("Enable SUPL mode.")
161 ad.adb.shell("echo SUPL_MODE=1 >> /etc/gps_debug.conf")
162
jasonkmlu0f546de2019-12-13 20:58:41 +0800163
jasonkmluff0d5b92019-03-21 11:24:45 +0800164def disable_supl_mode(ad):
165 """Kill SUPL to test XTRA only test item.
166
167 Args:
168 ad: An AndroidDevice object.
169 """
170 remount_device(ad)
171 ad.log.info("Disable SUPL mode.")
172 ad.adb.shell("echo SUPL_MODE=0 >> /etc/gps_debug.conf")
jasonkmlu214f0d62019-04-08 19:53:59 +0800173 reboot(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800174
jasonkmlu0f546de2019-12-13 20:58:41 +0800175
jasonkmluff0d5b92019-03-21 11:24:45 +0800176def kill_xtra_daemon(ad):
177 """Kill XTRA daemon to test SUPL only test item.
178
179 Args:
180 ad: An AndroidDevice object.
181 """
182 ad.root_adb()
183 ad.log.info("Disable XTRA-daemon until next reboot.")
184 ad.adb.shell("killall xtra-daemon")
185
jasonkmlu0f546de2019-12-13 20:58:41 +0800186
jasonkmluff0d5b92019-03-21 11:24:45 +0800187def disable_private_dns_mode(ad):
188 """Due to b/118365122, it's better to disable private DNS mode while
189 testing. 8.8.8.8 private dns sever is unstable now, sometimes server
190 will not response dns query suddenly.
191
192 Args:
193 ad: An AndroidDevice object.
194 """
195 tutils.get_operator_name(ad.log, ad, subId=None)
196 if ad.adb.shell("settings get global private_dns_mode") != "off":
197 ad.log.info("Disable Private DNS mode.")
198 ad.adb.shell("settings put global private_dns_mode off")
199
jasonkmlu0f546de2019-12-13 20:58:41 +0800200
jasonkmluff0d5b92019-03-21 11:24:45 +0800201def _init_device(ad):
202 """Init GNSS test devices.
203
204 Args:
205 ad: An AndroidDevice object.
206 """
207 enable_gnss_verbose_logging(ad)
jasonkmlu81b8b032019-06-21 15:18:27 +0800208 enable_compact_and_particle_fusion_log(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800209 disable_xtra_throttle(ad)
210 enable_supl_mode(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800211 ad.adb.shell("settings put system screen_off_timeout 1800000")
212 wutils.wifi_toggle_state(ad, False)
213 ad.log.info("Setting Bluetooth state to False")
214 ad.droid.bluetoothToggleState(False)
jasonkmluff0d5b92019-03-21 11:24:45 +0800215 set_gnss_qxdm_mask(ad, QXDM_MASKS)
jasonkmluff0d5b92019-03-21 11:24:45 +0800216 check_location_service(ad)
217 set_wifi_and_bt_scanning(ad, True)
jasonkmlu214f0d62019-04-08 19:53:59 +0800218 disable_private_dns_mode(ad)
jasonkmlu81b8b032019-06-21 15:18:27 +0800219 init_gtw_gpstool(ad)
jasonkmlu180a08c2019-04-23 17:24:55 +0800220 reboot(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800221
jasonkmlu0f546de2019-12-13 20:58:41 +0800222
jasonkmluff0d5b92019-03-21 11:24:45 +0800223def connect_to_wifi_network(ad, network):
224 """Connection logic for open and psk wifi networks.
225
226 Args:
227 ad: An AndroidDevice object.
228 network: Dictionary with network info.
229 """
230 SSID = network[WifiEnums.SSID_KEY]
jasonkmlub663d3e2019-12-30 15:11:07 +0800231 ad.ed.clear_all_events()
232 wutils.reset_wifi(ad)
233 wutils.start_wifi_connection_scan_and_ensure_network_found(ad, SSID)
jasonkmluff0d5b92019-03-21 11:24:45 +0800234 wutils.wifi_connect(ad, network, num_of_tries=5)
235
jasonkmlu0f546de2019-12-13 20:58:41 +0800236
jasonkmluff0d5b92019-03-21 11:24:45 +0800237def set_wifi_and_bt_scanning(ad, state=True):
238 """Set Wi-Fi and Bluetooth scanning on/off in Settings -> Location
239
240 Args:
241 ad: An AndroidDevice object.
jasonkmlu81b8b032019-06-21 15:18:27 +0800242 state: True to turn on "Wi-Fi and Bluetooth scanning".
243 False to turn off "Wi-Fi and Bluetooth scanning".
jasonkmluff0d5b92019-03-21 11:24:45 +0800244 """
245 ad.root_adb()
246 if state:
247 ad.adb.shell("settings put global wifi_scan_always_enabled 1")
248 ad.adb.shell("settings put global ble_scan_always_enabled 1")
249 ad.log.info("Wi-Fi and Bluetooth scanning are enabled")
250 else:
251 ad.adb.shell("settings put global wifi_scan_always_enabled 0")
252 ad.adb.shell("settings put global ble_scan_always_enabled 0")
253 ad.log.info("Wi-Fi and Bluetooth scanning are disabled")
254
jasonkmlu0f546de2019-12-13 20:58:41 +0800255
jasonkmluff0d5b92019-03-21 11:24:45 +0800256def check_location_service(ad):
257 """Set location service on.
258 Verify if location service is available.
259
260 Args:
261 ad: An AndroidDevice object.
jasonkmluff0d5b92019-03-21 11:24:45 +0800262 """
jasonkmlu81b8b032019-06-21 15:18:27 +0800263 remount_device(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800264 utils.set_location_service(ad, True)
jasonkmlu81b8b032019-06-21 15:18:27 +0800265 location_mode = int(ad.adb.shell("settings get secure location_mode"))
266 ad.log.info("Current Location Mode >> %d" % location_mode)
267 if location_mode != 3:
jasonkmlu78540632019-11-15 18:04:20 +0800268 raise signals.TestError("Failed to turn Location on")
jasonkmluff0d5b92019-03-21 11:24:45 +0800269
jasonkmlu0f546de2019-12-13 20:58:41 +0800270
jasonkmluff0d5b92019-03-21 11:24:45 +0800271def clear_logd_gnss_qxdm_log(ad):
272 """Clear /data/misc/logd,
273 /storage/emulated/0/Android/data/com.android.gpstool/files and
274 /data/vendor/radio/diag_logs/logs from previous test item then reboot.
275
276 Args:
277 ad: An AndroidDevice object.
278 """
279 remount_device(ad)
280 ad.log.info("Clear Logd, GNSS and QXDM Log from previous test item.")
281 ad.adb.shell("rm -rf /data/misc/logd", ignore_status=True)
jasonkmlu670cfbc2019-11-27 18:58:21 +0800282 ad.adb.shell('find %s -name "*.txt" -type f -delete' % GNSSSTATUS_LOG_PATH)
283 output_path = posixpath.join(DEFAULT_QXDM_LOG_PATH, "logs")
jasonkmluff0d5b92019-03-21 11:24:45 +0800284 ad.adb.shell("rm -rf %s" % output_path, ignore_status=True)
jasonkmlu214f0d62019-04-08 19:53:59 +0800285 reboot(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800286
jasonkmlu0f546de2019-12-13 20:58:41 +0800287
jasonkmlu81b8b032019-06-21 15:18:27 +0800288def get_gnss_qxdm_log(ad, qdb_path):
jasonkmluff0d5b92019-03-21 11:24:45 +0800289 """Get /storage/emulated/0/Android/data/com.android.gpstool/files and
jasonkmlu81b8b032019-06-21 15:18:27 +0800290 /data/vendor/radio/diag_logs/logs for test item.
jasonkmluff0d5b92019-03-21 11:24:45 +0800291
292 Args:
293 ad: An AndroidDevice object.
jasonkmlu81b8b032019-06-21 15:18:27 +0800294 qdb_path: The path of qdsp6m.qdb on different projects.
jasonkmluff0d5b92019-03-21 11:24:45 +0800295 """
jasonkmlu903ece82019-05-16 14:56:19 +0800296 log_path = ad.device_log_path
jasonkmluff0d5b92019-03-21 11:24:45 +0800297 utils.create_dir(log_path)
jasonkmlu903ece82019-05-16 14:56:19 +0800298 gnss_log_name = "gnssstatus_log_%s_%s" % (ad.model, ad.serial)
jasonkmlu670cfbc2019-11-27 18:58:21 +0800299 gnss_log_path = posixpath.join(log_path, gnss_log_name)
jasonkmluff0d5b92019-03-21 11:24:45 +0800300 utils.create_dir(gnss_log_path)
301 ad.log.info("Pull GnssStatus Log to %s" % gnss_log_path)
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800302 ad.adb.pull("%s %s" % (GNSSSTATUS_LOG_PATH+".", gnss_log_path),
jasonkmluff0d5b92019-03-21 11:24:45 +0800303 timeout=PULL_TIMEOUT, ignore_status=True)
jasonkmlu903ece82019-05-16 14:56:19 +0800304 shutil.make_archive(gnss_log_path, "zip", gnss_log_path)
305 shutil.rmtree(gnss_log_path)
jasonkmlu670cfbc2019-11-27 18:58:21 +0800306 output_path = posixpath.join(DEFAULT_QXDM_LOG_PATH, "logs/.")
jasonkmlu035eee12019-10-08 17:11:20 +0800307 file_count = ad.adb.shell(
308 "find %s -type f -iname *.qmdl | wc -l" % output_path)
jasonkmluff0d5b92019-03-21 11:24:45 +0800309 if not int(file_count) == 0:
jasonkmlu903ece82019-05-16 14:56:19 +0800310 qxdm_log_name = "QXDM_%s_%s" % (ad.model, ad.serial)
jasonkmlu670cfbc2019-11-27 18:58:21 +0800311 qxdm_log_path = posixpath.join(log_path, qxdm_log_name)
jasonkmluff0d5b92019-03-21 11:24:45 +0800312 utils.create_dir(qxdm_log_path)
313 ad.log.info("Pull QXDM Log %s to %s" % (output_path, qxdm_log_path))
314 ad.adb.pull("%s %s" % (output_path, qxdm_log_path),
315 timeout=PULL_TIMEOUT, ignore_status=True)
jasonkmlu81b8b032019-06-21 15:18:27 +0800316 for path in qdb_path:
317 output = ad.adb.pull("%s %s" % (path, qxdm_log_path),
318 timeout=PULL_TIMEOUT, ignore_status=True)
319 if "No such file or directory" in output:
320 continue
321 break
jasonkmlu903ece82019-05-16 14:56:19 +0800322 shutil.make_archive(qxdm_log_path, "zip", qxdm_log_path)
323 shutil.rmtree(qxdm_log_path)
jasonkmluff0d5b92019-03-21 11:24:45 +0800324 else:
325 ad.log.error("QXDM file count is %d. There is no QXDM log on device."
326 % int(file_count))
327
jasonkmlu0f546de2019-12-13 20:58:41 +0800328
jasonkmluff0d5b92019-03-21 11:24:45 +0800329def set_mobile_data(ad, state):
330 """Set mobile data on or off and check mobile data state.
331
332 Args:
333 ad: An AndroidDevice object.
334 state: True to enable mobile data. False to disable mobile data.
335 """
336 ad.root_adb()
337 if state:
338 ad.log.info("Enable mobile data.")
339 ad.adb.shell("svc data enable")
340 else:
341 ad.log.info("Disable mobile data.")
342 ad.adb.shell("svc data disable")
343 time.sleep(5)
344 out = int(ad.adb.shell("settings get global mobile_data"))
345 if state and out == 1:
346 ad.log.info("Mobile data is enabled and set to %d" % out)
347 elif not state and out == 0:
348 ad.log.info("Mobile data is disabled and set to %d" % out)
349 else:
350 ad.log.error("Mobile data is at unknown state and set to %d" % out)
351
jasonkmlu0f546de2019-12-13 20:58:41 +0800352
jasonkmlu78540632019-11-15 18:04:20 +0800353def gnss_trigger_modem_ssr_by_adb(ad, dwelltime=60):
354 """Trigger modem SSR crash by adb and verify if modem crash and recover
jasonkmlu035eee12019-10-08 17:11:20 +0800355 successfully.
jasonkmluff0d5b92019-03-21 11:24:45 +0800356
357 Args:
358 ad: An AndroidDevice object.
jasonkmlu78540632019-11-15 18:04:20 +0800359 dwelltime: Waiting time for modem reset. Default is 60 seconds.
jasonkmluff0d5b92019-03-21 11:24:45 +0800360
361 Returns:
jasonkmlu81b8b032019-06-21 15:18:27 +0800362 True if success.
363 False if failed.
jasonkmluff0d5b92019-03-21 11:24:45 +0800364 """
jasonkmlu81b8b032019-06-21 15:18:27 +0800365 begin_time = get_current_epoch_time()
366 ad.root_adb()
367 cmds = ("echo restart > /sys/kernel/debug/msm_subsys/modem",
368 r"echo 'at+cfun=1,1\r' > /dev/at_mdm0")
369 for cmd in cmds:
370 ad.log.info("Triggering modem SSR crash by %s" % cmd)
371 output = ad.adb.shell(cmd, ignore_status=True)
372 if "No such file or directory" in output:
373 continue
374 break
375 time.sleep(dwelltime)
jasonkmluff0d5b92019-03-21 11:24:45 +0800376 ad.send_keycode("HOME")
jasonkmlu81b8b032019-06-21 15:18:27 +0800377 logcat_results = ad.search_logcat("SSRObserver", begin_time)
378 if logcat_results:
379 for ssr in logcat_results:
380 if "mSubsystem='modem', mCrashReason" in ssr["log_message"]:
381 ad.log.debug(ssr["log_message"])
382 ad.log.info("Triggering modem SSR crash successfully.")
383 return True
jasonkmlu78540632019-11-15 18:04:20 +0800384 raise signals.TestError("Failed to trigger modem SSR crash")
385 raise signals.TestError("No SSRObserver found in logcat")
386
jasonkmlu0f546de2019-12-13 20:58:41 +0800387
jasonkmlu78540632019-11-15 18:04:20 +0800388def gnss_trigger_modem_ssr_by_mds(ad, dwelltime=60):
389 """Trigger modem SSR crash by mds tool and verify if modem crash and recover
390 successfully.
391
392 Args:
393 ad: An AndroidDevice object.
394 dwelltime: Waiting time for modem reset. Default is 60 seconds.
395 """
396 mds_check = ad.adb.shell("pm path com.google.mdstest")
397 if not mds_check:
398 raise signals.TestError("MDS Tool is not properly installed.")
399 ad.root_adb()
400 cmd = ('am instrument -w -e request "4b 25 03 00" '
401 '"com.google.mdstest/com.google.mdstest.instrument'
402 '.ModemCommandInstrumentation"')
403 ad.log.info("Triggering modem SSR crash by MDS")
404 output = ad.adb.shell(cmd, ignore_status=True)
405 ad.log.debug(output)
406 time.sleep(dwelltime)
407 ad.send_keycode("HOME")
408 if "SUCCESS" in output:
409 ad.log.info("Triggering modem SSR crash by MDS successfully.")
410 else:
411 raise signals.TestError(
412 "Failed to trigger modem SSR crash by MDS. \n%s" % output)
413
jasonkmlu0f546de2019-12-13 20:58:41 +0800414
jasonkmlu78540632019-11-15 18:04:20 +0800415def pull_mdstool(ad):
416 """Pull ModemDiagnosticSystemTest.apk from device.
417
418 Args:
419 ad: An AndroidDevice object.
420 """
421 out = ad.adb.shell("pm path com.google.mdstest")
422 result = re.search(r"package:(.*)", out)
423 if not result:
424 raise signals.TestError("No ModemDiagnosticSystemTest.apk found.")
425 else:
426 mds_tool = result.group(1)
427 ad.log.info("Get ModemDiagnosticSystemTest.apk from %s" % mds_tool)
428 apkdir = "/tmp/MDS/"
429 utils.create_dir(apkdir)
430 ad.pull_files([mds_tool], apkdir)
431
jasonkmlu0f546de2019-12-13 20:58:41 +0800432
jasonkmlu78540632019-11-15 18:04:20 +0800433def reinstall_mdstool(ad):
434 """Reinstall ModemDiagnosticSystemTest.apk.
435
436 Args:
437 ad: An AndroidDevice object.
438 """
439 ad.log.info("Re-install ModemDiagnosticSystemTest.apk")
440 ad.adb.install("-r -g -t /tmp/MDS/base.apk")
441 mds_check = ad.adb.shell("pm path com.google.mdstest")
442 if not mds_check:
443 raise signals.TestError("MDS Tool is not properly re-installed.")
444 ad.log.info("MDS Tool is re-installed successfully.")
jasonkmluff0d5b92019-03-21 11:24:45 +0800445
jasonkmlu0f546de2019-12-13 20:58:41 +0800446
jasonkmluff0d5b92019-03-21 11:24:45 +0800447def check_xtra_download(ad, begin_time):
448 """Verify XTRA download success log message in logcat.
449
450 Args:
451 ad: An AndroidDevice object.
452 begin_time: test begin time
453
454 Returns:
455 True: xtra_download if XTRA downloaded and injected successfully
456 otherwise return False.
457 """
458 ad.send_keycode("HOME")
459 logcat_results = ad.search_logcat("XTRA download success. "
460 "inject data into modem", begin_time)
461 if logcat_results:
jasonkmlu81b8b032019-06-21 15:18:27 +0800462 ad.log.debug("%s" % logcat_results[-1]["log_message"])
jasonkmluff0d5b92019-03-21 11:24:45 +0800463 ad.log.info("XTRA downloaded and injected successfully.")
464 return True
465 ad.log.error("XTRA downloaded FAIL.")
466 return False
467
jasonkmlu0f546de2019-12-13 20:58:41 +0800468
jasonkmluff0d5b92019-03-21 11:24:45 +0800469def pull_gtw_gpstool(ad):
470 """Pull GTW_GPSTool apk from device.
471
472 Args:
473 ad: An AndroidDevice object.
474 """
475 out = ad.adb.shell("pm path com.android.gpstool")
476 result = re.search(r"package:(.*)", out)
477 if not result:
478 tutils.abort_all_tests(ad.log, "Couldn't find GTW GPSTool apk")
479 else:
480 GTW_GPSTool_apk = result.group(1)
481 ad.log.info("Get GTW GPSTool apk from %s" % GTW_GPSTool_apk)
482 apkdir = "/tmp/GNSS/"
483 utils.create_dir(apkdir)
484 ad.pull_files([GTW_GPSTool_apk], apkdir)
485
jasonkmlu0f546de2019-12-13 20:58:41 +0800486
jasonkmluff0d5b92019-03-21 11:24:45 +0800487def reinstall_gtw_gpstool(ad):
488 """Reinstall GTW_GPSTool apk.
489
490 Args:
491 ad: An AndroidDevice object.
492 """
493 ad.log.info("Re-install GTW GPSTool")
jasonkmlu903ece82019-05-16 14:56:19 +0800494 ad.adb.install("-r -g -t /tmp/GNSS/base.apk")
jasonkmlu78540632019-11-15 18:04:20 +0800495 gpstool_check = ad.adb.shell("pm path com.android.gpstool")
496 if not gpstool_check:
497 raise signals.TestError("GTW GPSTool is not properly re-installed.")
498 ad.log.info("GTW GPSTool is re-installed successfully.")
jasonkmluff0d5b92019-03-21 11:24:45 +0800499
jasonkmlu0f546de2019-12-13 20:58:41 +0800500
jasonkmlu180a08c2019-04-23 17:24:55 +0800501def init_gtw_gpstool(ad):
502 """Init GTW_GPSTool apk.
503
504 Args:
505 ad: An AndroidDevice object.
506 """
507 remount_device(ad)
508 pull_gtw_gpstool(ad)
509 ad.adb.shell("settings put global verifier_verify_adb_installs 0")
jasonkmlu180a08c2019-04-23 17:24:55 +0800510 reinstall_gtw_gpstool(ad)
511
jasonkmlu0f546de2019-12-13 20:58:41 +0800512
jasonkmluff0d5b92019-03-21 11:24:45 +0800513def fastboot_factory_reset(ad):
514 """Factory reset the device in fastboot mode.
515 Pull sl4a apk from device. Terminate all sl4a sessions,
516 Reboot the device to bootloader,
517 factory reset the device by fastboot.
518 Reboot the device. wait for device to complete booting
519 Re-install and start an sl4a session.
520
521 Args:
522 ad: An AndroidDevice object.
523
524 Returns:
525 True if factory reset process complete.
526 """
527 status = True
528 skip_setup_wizard = True
529 out = ad.adb.shell("pm path %s" % SL4A_APK_NAME)
530 result = re.search(r"package:(.*)", out)
531 if not result:
532 tutils.abort_all_tests(ad.log, "Couldn't find sl4a apk")
533 else:
534 sl4a_apk = result.group(1)
535 ad.log.info("Get sl4a apk from %s" % sl4a_apk)
536 ad.pull_files([sl4a_apk], "/tmp/")
537 pull_gtw_gpstool(ad)
jasonkmlu78540632019-11-15 18:04:20 +0800538 pull_mdstool(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800539 tutils.stop_qxdm_logger(ad)
540 ad.stop_services()
541 attempts = 3
542 for i in range(1, attempts + 1):
543 try:
544 if ad.serial in list_adb_devices():
545 ad.log.info("Reboot to bootloader")
546 ad.adb.reboot("bootloader", ignore_status=True)
547 time.sleep(10)
548 if ad.serial in list_fastboot_devices():
549 ad.log.info("Factory reset in fastboot")
550 ad.fastboot._w(timeout=300, ignore_status=True)
551 time.sleep(30)
552 ad.log.info("Reboot in fastboot")
553 ad.fastboot.reboot()
554 ad.wait_for_boot_completion()
555 ad.root_adb()
556 if ad.skip_sl4a:
557 break
558 if ad.is_sl4a_installed():
559 break
560 ad.log.info("Re-install sl4a")
561 ad.adb.shell("settings put global verifier_verify_adb_installs 0")
jasonkmlu903ece82019-05-16 14:56:19 +0800562 ad.adb.install("-r -g -t /tmp/base.apk")
jasonkmluff0d5b92019-03-21 11:24:45 +0800563 reinstall_gtw_gpstool(ad)
jasonkmlu78540632019-11-15 18:04:20 +0800564 reinstall_mdstool(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800565 time.sleep(10)
566 break
567 except Exception as e:
568 ad.log.error(e)
569 if i == attempts:
570 tutils.abort_all_tests(ad.log, str(e))
571 time.sleep(5)
572 try:
573 ad.start_adb_logcat()
574 except Exception as e:
575 ad.log.error(e)
576 if skip_setup_wizard:
577 ad.exit_setup_wizard()
578 if ad.skip_sl4a:
579 return status
580 tutils.bring_up_sl4a(ad)
jasonkmluff0d5b92019-03-21 11:24:45 +0800581 return status
582
jasonkmlu0f546de2019-12-13 20:58:41 +0800583
jasonkmluff0d5b92019-03-21 11:24:45 +0800584def clear_aiding_data_by_gtw_gpstool(ad):
585 """Launch GTW GPSTool and Clear all GNSS aiding data.
586 Wait 5 seconds for GTW GPStool to clear all GNSS aiding
587 data properly.
588
589 Args:
590 ad: An AndroidDevice object.
591 """
592 ad.log.info("Launch GTW GPSTool and Clear all GNSS aiding data")
593 ad.adb.shell("am start -S -n com.android.gpstool/.GPSTool --es mode clear")
594 time.sleep(10)
595
Jayakumar Munuswamybc6038c2019-10-30 14:46:38 -0700596
597def start_gnss_by_gtw_gpstool(ad, state, type="gnss", bgdisplay=False):
jasonkmluff0d5b92019-03-21 11:24:45 +0800598 """Start or stop GNSS on GTW_GPSTool.
599
600 Args:
601 ad: An AndroidDevice object.
602 state: True to start GNSS. False to Stop GNSS.
jasonkmlu81b8b032019-06-21 15:18:27 +0800603 type: Different API for location fix. Use gnss/flp/nmea
Jayakumar Munuswamybc6038c2019-10-30 14:46:38 -0700604 bgdisplay: true to run GTW when Display off.
605 false to not run GTW when Display off.
jasonkmluff0d5b92019-03-21 11:24:45 +0800606 """
Jayakumar Munuswamybc6038c2019-10-30 14:46:38 -0700607 if state and not bgdisplay:
jasonkmlu81b8b032019-06-21 15:18:27 +0800608 ad.adb.shell("am start -S -n com.android.gpstool/.GPSTool "
609 "--es mode gps --es type %s" % type)
Jayakumar Munuswamybc6038c2019-10-30 14:46:38 -0700610 elif state and bgdisplay:
jasonkmlu0f546de2019-12-13 20:58:41 +0800611 ad.adb.shell("am start -S -n com.android.gpstool/.GPSTool --es mode "
612 "gps --es type {} --ez BG {}".format(type, bgdisplay))
jasonkmluff0d5b92019-03-21 11:24:45 +0800613 if not state:
jasonkmlu81b8b032019-06-21 15:18:27 +0800614 ad.log.info("Stop %s on GTW_GPSTool." % type)
jasonkmluff0d5b92019-03-21 11:24:45 +0800615 ad.adb.shell("am broadcast -a com.android.gpstool.stop_gps_action")
616 time.sleep(3)
617
Jayakumar Munuswamybc6038c2019-10-30 14:46:38 -0700618
jasonkmlu81b8b032019-06-21 15:18:27 +0800619def process_gnss_by_gtw_gpstool(ad, criteria, type="gnss"):
jasonkmluff0d5b92019-03-21 11:24:45 +0800620 """Launch GTW GPSTool and Clear all GNSS aiding data
621 Start GNSS tracking on GTW_GPSTool.
622
623 Args:
624 ad: An AndroidDevice object.
625 criteria: Criteria for current test item.
jasonkmlu81b8b032019-06-21 15:18:27 +0800626 type: Different API for location fix. Use gnss/flp/nmea
jasonkmluff0d5b92019-03-21 11:24:45 +0800627
628 Returns:
629 True: First fix TTFF are within criteria.
630 False: First fix TTFF exceed criteria.
631 """
632 retries = 3
633 for i in range(retries):
634 begin_time = get_current_epoch_time()
635 clear_aiding_data_by_gtw_gpstool(ad)
jasonkmlu035eee12019-10-08 17:11:20 +0800636 ad.log.info("Start %s on GTW_GPSTool - attempt %d" % (type.upper(),
637 i+1))
jasonkmlu81b8b032019-06-21 15:18:27 +0800638 start_gnss_by_gtw_gpstool(ad, True, type)
jasonkmluff0d5b92019-03-21 11:24:45 +0800639 for _ in range(10 + criteria):
640 logcat_results = ad.search_logcat("First fixed", begin_time)
641 if logcat_results:
jasonkmlu81b8b032019-06-21 15:18:27 +0800642 ad.log.debug(logcat_results[-1]["log_message"])
jasonkmluff0d5b92019-03-21 11:24:45 +0800643 first_fixed = int(logcat_results[-1]["log_message"].split()[-1])
jasonkmlu81b8b032019-06-21 15:18:27 +0800644 ad.log.info("%s First fixed = %.3f seconds" %
645 (type.upper(), first_fixed/1000))
646 if (first_fixed/1000) <= criteria:
jasonkmluff0d5b92019-03-21 11:24:45 +0800647 return True
jasonkmlu81b8b032019-06-21 15:18:27 +0800648 start_gnss_by_gtw_gpstool(ad, False, type)
jasonkmlu035eee12019-10-08 17:11:20 +0800649 raise signals.TestFailure("Fail to get %s location fixed "
650 "within %d seconds criteria."
651 % (type.upper(), criteria))
jasonkmluff0d5b92019-03-21 11:24:45 +0800652 time.sleep(1)
jasonkmlua4a569f2019-04-10 14:25:31 +0800653 if not ad.is_adb_logcat_on:
654 ad.start_adb_logcat()
jasonkmlu97ac56e2019-05-29 15:04:51 +0800655 check_currrent_focus_app(ad)
jasonkmlu81b8b032019-06-21 15:18:27 +0800656 start_gnss_by_gtw_gpstool(ad, False, type)
jasonkmlu035eee12019-10-08 17:11:20 +0800657 raise signals.TestFailure("Fail to get %s location fixed within %d "
658 "attempts." % (type.upper(), retries))
jasonkmluff0d5b92019-03-21 11:24:45 +0800659
Scott Hong72269692019-12-16 16:59:56 +0800660def start_ttff_by_gtw_gpstool(ad, ttff_mode, iteration, aid_data=False):
jasonkmluff0d5b92019-03-21 11:24:45 +0800661 """Identify which TTFF mode for different test items.
662
663 Args:
664 ad: An AndroidDevice object.
665 ttff_mode: TTFF Test mode for current test item.
666 iteration: Iteration of TTFF cycles.
Scott Hong72269692019-12-16 16:59:56 +0800667 aid_data: Boolean for identify aid_data existed or not
jasonkmluff0d5b92019-03-21 11:24:45 +0800668 """
jasonkmlu97ac56e2019-05-29 15:04:51 +0800669 begin_time = get_current_epoch_time()
Scott Hong72269692019-12-16 16:59:56 +0800670 if (ttff_mode == "hs" or ttff_mode == "ws") and not aid_data:
jasonkmlu81b8b032019-06-21 15:18:27 +0800671 ad.log.info("Wait 5 minutes to start TTFF %s..." % ttff_mode.upper())
jasonkmluff0d5b92019-03-21 11:24:45 +0800672 time.sleep(300)
673 if ttff_mode == "cs":
674 ad.log.info("Start TTFF Cold Start...")
675 time.sleep(3)
jasonkmlu97ac56e2019-05-29 15:04:51 +0800676 for i in range(1, 4):
677 ad.adb.shell("am broadcast -a com.android.gpstool.ttff_action "
678 "--es ttff %s --es cycle %d" % (ttff_mode, iteration))
jasonkmlu81b8b032019-06-21 15:18:27 +0800679 time.sleep(1)
jasonkmlu97ac56e2019-05-29 15:04:51 +0800680 if ad.search_logcat("act=com.android.gpstool.start_test_action",
681 begin_time):
682 ad.log.info("Send TTFF start_test_action successfully.")
683 break
jasonkmlu035eee12019-10-08 17:11:20 +0800684 else:
685 check_currrent_focus_app(ad)
jasonkmlu78540632019-11-15 18:04:20 +0800686 raise signals.TestError("Fail to send TTFF start_test_action.")
jasonkmluff0d5b92019-03-21 11:24:45 +0800687
jasonkmlu0f546de2019-12-13 20:58:41 +0800688
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800689def gnss_tracking_via_gtw_gpstool(ad, criteria, type="gnss", testtime=60):
jasonkmlu66fda532019-07-16 11:12:45 +0800690 """Start GNSS/FLP tracking tests for input testtime on GTW_GPSTool.
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800691
692 Args:
693 ad: An AndroidDevice object.
694 criteria: Criteria for current TTFF.
695 type: Different API for location fix. Use gnss/flp/nmea
696 testtime: Tracking test time for minutes. Default set to 60 minutes.
697 """
698 process_gnss_by_gtw_gpstool(ad, criteria, type)
699 ad.log.info("Start %s tracking test for %d minutes" % (type.upper(),
700 testtime))
701 begin_time = get_current_epoch_time()
702 while get_current_epoch_time() - begin_time < testtime * 60 * 1000 :
703 if not ad.is_adb_logcat_on:
704 ad.start_adb_logcat()
705 crash_result = ad.search_logcat("Force finishing activity "
706 "com.android.gpstool/.GPSTool",
707 begin_time)
708 if crash_result:
jasonkmlu78540632019-11-15 18:04:20 +0800709 raise signals.TestError("GPSTool crashed. Abort test.")
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800710 ad.log.info("Successfully tested for %d minutes" % testtime)
711 start_gnss_by_gtw_gpstool(ad, False, type)
712
jasonkmlu0f546de2019-12-13 20:58:41 +0800713
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800714def parse_gtw_gpstool_log(ad, true_position, type="gnss"):
715 """Process GNSS/FLP API logs from GTW GPSTool and output track_data to
716 test_run_info for ACTS plugin to parse and display on MobileHarness as
717 Property.
718
719 Args:
720 ad: An AndroidDevice object.
721 true_position: Coordinate as [latitude, longitude] to calculate
722 position error.
723 type: Different API for location fix. Use gnss/flp/nmea
724 """
725 test_logfile = {}
726 track_data = {}
727 history_top4_cn = 0
728 history_cn = 0
jasonkmlu035eee12019-10-08 17:11:20 +0800729 l5flag = "false"
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800730 file_count = int(ad.adb.shell("find %s -type f -iname *.txt | wc -l"
731 % GNSSSTATUS_LOG_PATH))
732 if file_count != 1:
733 ad.log.error("%d API logs exist." % file_count)
734 dir = ad.adb.shell("ls %s" % GNSSSTATUS_LOG_PATH).split()
735 for path_key in dir:
736 if fnmatch.fnmatch(path_key, "*.txt"):
737 logpath = posixpath.join(GNSSSTATUS_LOG_PATH, path_key)
738 out = ad.adb.shell("wc -c %s" % logpath)
739 file_size = int(out.split(" ")[0])
740 if file_size < 2000:
741 ad.log.info("Skip log %s due to log size %d bytes" %
742 (path_key, file_size))
743 continue
744 test_logfile = logpath
745 if not test_logfile:
jasonkmlu78540632019-11-15 18:04:20 +0800746 raise signals.TestError("Failed to get test log file in device.")
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800747 lines = ad.adb.shell("cat %s" % test_logfile).split("\n")
748 for line in lines:
749 if "History Avg Top4" in line:
750 history_top4_cn = float(line.split(":")[-1].strip())
751 if "History Avg" in line:
752 history_cn = float(line.split(":")[-1].strip())
753 if "L5 used in fix" in line:
754 l5flag = line.split(":")[-1].strip()
755 if "Latitude" in line:
756 track_lat = float(line.split(":")[-1].strip())
757 if "Longitude" in line:
758 track_long = float(line.split(":")[-1].strip())
759 if "Time" in line:
760 track_utc = line.split("Time:")[-1].strip()
761 if track_utc in track_data.keys():
762 continue
763 track_pe = calculate_position_error(ad, track_lat, track_long,
764 true_position)
765 track_data[track_utc] = TRACK_REPORT(track_l5flag=l5flag,
766 track_pe=track_pe,
767 track_top4cn=history_top4_cn,
768 track_cn=history_cn)
769 ad.log.debug(track_data)
770 prop_basename = "TestResult %s_tracking_" % type.upper()
771 time_list = sorted(track_data.keys())
772 l5flag_list = [track_data[key].track_l5flag for key in time_list]
773 pe_list = [float(track_data[key].track_pe) for key in time_list]
774 top4cn_list = [float(track_data[key].track_top4cn) for key in time_list]
775 cn_list = [float(track_data[key].track_cn) for key in time_list]
776 ad.log.info(prop_basename+"StartTime %s" % time_list[0].replace(" ", "-"))
777 ad.log.info(prop_basename+"EndTime %s" % time_list[-1].replace(" ", "-"))
778 ad.log.info(prop_basename+"TotalFixPoints %d" % len(time_list))
779 ad.log.info(prop_basename+"L5FixRate "+'{percent:.2%}'.format(
780 percent=l5flag_list.count("true")/len(l5flag_list)))
781 ad.log.info(prop_basename+"AvgDis %.1f" % (sum(pe_list)/len(pe_list)))
782 ad.log.info(prop_basename+"MaxDis %.1f" % max(pe_list))
783 ad.log.info(prop_basename+"AvgTop4Signal %.1f" % top4cn_list[-1])
784 ad.log.info(prop_basename+"AvgSignal %.1f" % cn_list[-1])
785
jasonkmlu0f546de2019-12-13 20:58:41 +0800786
jasonkmlu81b8b032019-06-21 15:18:27 +0800787def process_ttff_by_gtw_gpstool(ad, begin_time, true_position, type="gnss"):
788 """Process TTFF and record results in ttff_data.
jasonkmluff0d5b92019-03-21 11:24:45 +0800789
790 Args:
791 ad: An AndroidDevice object.
jasonkmlu903ece82019-05-16 14:56:19 +0800792 begin_time: test begin time.
jasonkmlu733738f2019-09-02 18:59:42 +0800793 true_position: Coordinate as [latitude, longitude] to calculate
794 position error.
jasonkmlu81b8b032019-06-21 15:18:27 +0800795 type: Different API for location fix. Use gnss/flp/nmea
jasonkmluff0d5b92019-03-21 11:24:45 +0800796
797 Returns:
jasonkmlu81b8b032019-06-21 15:18:27 +0800798 ttff_data: A dict of all TTFF data.
jasonkmluff0d5b92019-03-21 11:24:45 +0800799 """
jasonkmlu903ece82019-05-16 14:56:19 +0800800 ttff_data = {}
jasonkmlu733738f2019-09-02 18:59:42 +0800801 ttff_loop_time = get_current_epoch_time()
jasonkmlu97ac56e2019-05-29 15:04:51 +0800802 while True:
jasonkmlu733738f2019-09-02 18:59:42 +0800803 if get_current_epoch_time() - ttff_loop_time >= 120000:
jasonkmlu78540632019-11-15 18:04:20 +0800804 raise signals.TestError("Fail to search specific GPSService "
805 "message in logcat. Abort test.")
jasonkmlu97ac56e2019-05-29 15:04:51 +0800806 if not ad.is_adb_logcat_on:
807 ad.start_adb_logcat()
808 stop_gps_results = ad.search_logcat("stop gps test", begin_time)
809 if stop_gps_results:
810 ad.send_keycode("HOME")
811 break
812 crash_result = ad.search_logcat("Force finishing activity "
813 "com.android.gpstool/.GPSTool",
814 begin_time)
815 if crash_result:
jasonkmlu78540632019-11-15 18:04:20 +0800816 raise signals.TestError("GPSTool crashed. Abort test.")
jasonkmlu97ac56e2019-05-29 15:04:51 +0800817 logcat_results = ad.search_logcat("write TTFF log", begin_time)
818 if logcat_results:
819 ttff_log = logcat_results[-1]["log_message"].split()
820 ttff_loop = int(ttff_log[8].split(":")[-1])
jasonkmlu97ac56e2019-05-29 15:04:51 +0800821 if ttff_loop in ttff_data.keys():
822 continue
jasonkmlu733738f2019-09-02 18:59:42 +0800823 ttff_loop_time = get_current_epoch_time()
jasonkmlu81b8b032019-06-21 15:18:27 +0800824 ttff_sec = float(ttff_log[11])
825 if ttff_sec != 0.0:
826 ttff_cn = float(ttff_log[18].strip("]"))
827 if type == "gnss":
jasonkmlu733738f2019-09-02 18:59:42 +0800828 gnss_results = ad.search_logcat("GPSService: Check item",
829 begin_time)
jasonkmlu81b8b032019-06-21 15:18:27 +0800830 if gnss_results:
831 ad.log.debug(gnss_results[-1]["log_message"])
jasonkmlu733738f2019-09-02 18:59:42 +0800832 gnss_location_log = \
833 gnss_results[-1]["log_message"].split()
834 ttff_lat = float(
835 gnss_location_log[8].split("=")[-1].strip(","))
836 ttff_lon = float(
837 gnss_location_log[9].split("=")[-1].strip(","))
jasonkmlu81b8b032019-06-21 15:18:27 +0800838 elif type == "flp":
jasonkmlu733738f2019-09-02 18:59:42 +0800839 flp_results = ad.search_logcat("GPSService: FLP Location",
840 begin_time)
jasonkmlu81b8b032019-06-21 15:18:27 +0800841 if flp_results:
842 ad.log.debug(flp_results[-1]["log_message"])
jasonkmlu733738f2019-09-02 18:59:42 +0800843 flp_location_log = \
844 flp_results[-1]["log_message"].split()
jasonkmlu81b8b032019-06-21 15:18:27 +0800845 ttff_lat = float(flp_location_log[8].split(",")[0])
846 ttff_lon = float(flp_location_log[8].split(",")[1])
847 else:
848 ttff_cn = float(ttff_log[19].strip("]"))
jasonkmlu97ac56e2019-05-29 15:04:51 +0800849 ttff_lat = 0.0
850 ttff_lon = 0.0
jasonkmlu2565d892019-11-12 15:31:26 +0800851 ad.log.debug("TTFF Loop %d - (Lat, Lon) = (%s, %s)" % (ttff_loop,
852 ttff_lat,
853 ttff_lon))
jasonkmlu97ac56e2019-05-29 15:04:51 +0800854 ttff_pe = calculate_position_error(ad, ttff_lat, ttff_lon,
855 true_position)
856 ttff_data[ttff_loop] = TTFF_REPORT(ttff_loop=ttff_loop,
857 ttff_sec=ttff_sec,
858 ttff_pe=ttff_pe,
859 ttff_cn=ttff_cn)
860 ad.log.info("Loop %d = %.1f seconds, "
861 "Position Error = %.1f meters, "
862 "Average Signal = %.1f dbHz"
863 % (ttff_loop, ttff_sec, ttff_pe, ttff_cn))
864 return ttff_data
jasonkmluff0d5b92019-03-21 11:24:45 +0800865
jasonkmlu0f546de2019-12-13 20:58:41 +0800866
jasonkmlu903ece82019-05-16 14:56:19 +0800867def check_ttff_data(ad, ttff_data, ttff_mode, criteria):
jasonkmlu81b8b032019-06-21 15:18:27 +0800868 """Verify all TTFF results from ttff_data.
jasonkmluff0d5b92019-03-21 11:24:45 +0800869
870 Args:
871 ad: An AndroidDevice object.
jasonkmlu903ece82019-05-16 14:56:19 +0800872 ttff_data: TTFF data of secs, position error and signal strength.
jasonkmluff0d5b92019-03-21 11:24:45 +0800873 ttff_mode: TTFF Test mode for current test item.
874 criteria: Criteria for current test item.
875
876 Returns:
877 True: All TTFF results are within criteria.
878 False: One or more TTFF results exceed criteria or Timeout.
879 """
880 ad.log.info("%d iterations of TTFF %s tests finished."
jasonkmlu903ece82019-05-16 14:56:19 +0800881 % (len(ttff_data.keys()), ttff_mode))
jasonkmluff0d5b92019-03-21 11:24:45 +0800882 ad.log.info("%s PASS criteria is %d seconds" % (ttff_mode, criteria))
jasonkmlu81b8b032019-06-21 15:18:27 +0800883 ad.log.debug("%s TTFF data: %s" % (ttff_mode, ttff_data))
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800884 ttff_property_key_and_value(ad, ttff_data, ttff_mode)
jasonkmlu903ece82019-05-16 14:56:19 +0800885 if len(ttff_data.keys()) == 0:
jasonkmluff0d5b92019-03-21 11:24:45 +0800886 ad.log.error("GTW_GPSTool didn't process TTFF properly.")
887 return False
jasonkmlu903ece82019-05-16 14:56:19 +0800888 elif any(float(ttff_data[key].ttff_sec) == 0.0 for key in ttff_data.keys()):
jasonkmluff0d5b92019-03-21 11:24:45 +0800889 ad.log.error("One or more TTFF %s Timeout" % ttff_mode)
890 return False
jasonkmlu035eee12019-10-08 17:11:20 +0800891 elif any(float(ttff_data[key].ttff_sec) >= criteria for key in
892 ttff_data.keys()):
jasonkmluff0d5b92019-03-21 11:24:45 +0800893 ad.log.error("One or more TTFF %s are over test criteria %d seconds"
894 % (ttff_mode, criteria))
895 return False
896 ad.log.info("All TTFF %s are within test criteria %d seconds."
897 % (ttff_mode, criteria))
898 return True
899
jasonkmlu0f546de2019-12-13 20:58:41 +0800900
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800901def ttff_property_key_and_value(ad, ttff_data, ttff_mode):
jasonkmlu81b8b032019-06-21 15:18:27 +0800902 """Output ttff_data to test_run_info for ACTS plugin to parse and display
903 on MobileHarness as Property.
904
905 Args:
906 ad: An AndroidDevice object.
907 ttff_data: TTFF data of secs, position error and signal strength.
908 ttff_mode: TTFF Test mode for current test item.
909 """
910 prop_basename = "TestResult "+ttff_mode.replace(" ", "_")+"_TTFF_"
911 sec_list = [float(ttff_data[key].ttff_sec) for key in ttff_data.keys()]
912 pe_list = [float(ttff_data[key].ttff_pe) for key in ttff_data.keys()]
913 cn_list = [float(ttff_data[key].ttff_cn) for key in ttff_data.keys()]
914 timeoutcount = sec_list.count(0.0)
jasonkmluc1ec3052019-10-25 14:57:53 +0800915 if len(sec_list) == timeoutcount:
916 avgttff = 9527
917 else:
918 avgttff = sum(sec_list)/(len(sec_list) - timeoutcount)
jasonkmlu81b8b032019-06-21 15:18:27 +0800919 if timeoutcount != 0:
jasonkmlu4fcdcad2019-07-10 14:35:12 +0800920 maxttff = 9527
jasonkmlu81b8b032019-06-21 15:18:27 +0800921 else:
922 maxttff = max(sec_list)
923 avgdis = sum(pe_list)/len(pe_list)
924 maxdis = max(pe_list)
925 avgcn = sum(cn_list)/len(cn_list)
926 ad.log.info(prop_basename+"AvgTime %.1f" % avgttff)
927 ad.log.info(prop_basename+"MaxTime %.1f" % maxttff)
928 ad.log.info(prop_basename+"TimeoutCount %d" % timeoutcount)
929 ad.log.info(prop_basename+"AvgDis %.1f" % avgdis)
930 ad.log.info(prop_basename+"MaxDis %.1f" % maxdis)
931 ad.log.info(prop_basename+"AvgSignal %.1f" % avgcn)
932
jasonkmlu0f546de2019-12-13 20:58:41 +0800933
jasonkmlu903ece82019-05-16 14:56:19 +0800934def calculate_position_error(ad, latitude, longitude, true_position):
935 """Use haversine formula to calculate position error base on true location
936 coordinate.
937
938 Args:
939 ad: An AndroidDevice object.
940 latitude: latitude of location fixed in the present.
941 longitude: longitude of location fixed in the present.
942 true_position: [latitude, longitude] of true location coordinate.
943
944 Returns:
945 position_error of location fixed in the present.
946 """
947 radius = 6371009
948 dlat = math.radians(latitude - true_position[0])
949 dlon = math.radians(longitude - true_position[1])
950 a = math.sin(dlat/2) * math.sin(dlat/2) + \
951 math.cos(math.radians(true_position[0])) * \
952 math.cos(math.radians(latitude)) * math.sin(dlon/2) * math.sin(dlon/2)
953 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
954 return radius * c
955
jasonkmlu0f546de2019-12-13 20:58:41 +0800956
jasonkmluff0d5b92019-03-21 11:24:45 +0800957def launch_google_map(ad):
958 """Launch Google Map via intent.
959
960 Args:
961 ad: An AndroidDevice object.
962 """
963 ad.log.info("Launch Google Map.")
964 try:
965 ad.adb.shell("am start -S -n com.google.android.apps.maps/"
966 "com.google.android.maps.MapsActivity")
967 ad.send_keycode("BACK")
968 ad.force_stop_apk("com.google.android.apps.maps")
969 ad.adb.shell("am start -S -n com.google.android.apps.maps/"
970 "com.google.android.maps.MapsActivity")
971 except Exception as e:
972 ad.log.error(e)
jasonkmlu78540632019-11-15 18:04:20 +0800973 raise signals.TestError("Failed to launch google map.")
jasonkmlu903ece82019-05-16 14:56:19 +0800974 check_currrent_focus_app(ad)
jasonkmlu49c32812019-04-12 18:11:10 +0800975
jasonkmlu0f546de2019-12-13 20:58:41 +0800976
jasonkmlu903ece82019-05-16 14:56:19 +0800977def check_currrent_focus_app(ad):
978 """Check to see current focused window and app.
jasonkmlu49c32812019-04-12 18:11:10 +0800979
980 Args:
981 ad: An AndroidDevice object.
jasonkmlu49c32812019-04-12 18:11:10 +0800982 """
jasonkmlu903ece82019-05-16 14:56:19 +0800983 time.sleep(1)
jasonkmlu035eee12019-10-08 17:11:20 +0800984 current = ad.adb.shell(
985 "dumpsys window | grep -E 'mCurrentFocus|mFocusedApp'")
jasonkmlu81b8b032019-06-21 15:18:27 +0800986 ad.log.debug("\n"+current)
jasonkmluff0d5b92019-03-21 11:24:45 +0800987
jasonkmlu0f546de2019-12-13 20:58:41 +0800988
jasonkmluff0d5b92019-03-21 11:24:45 +0800989def check_location_api(ad, retries):
990 """Verify if GnssLocationProvider API reports location.
991
992 Args:
993 ad: An AndroidDevice object.
994 retries: Retry time.
995
996 Returns:
997 True: GnssLocationProvider API reports location.
998 otherwise return False.
999 """
1000 for i in range(retries):
1001 begin_time = get_current_epoch_time()
1002 ad.log.info("Try to get location report from GnssLocationProvider API "
1003 "- attempt %d" % (i+1))
1004 while get_current_epoch_time() - begin_time <= 30000:
1005 logcat_results = ad.search_logcat("REPORT_LOCATION", begin_time)
1006 if logcat_results:
1007 ad.log.info("%s" % logcat_results[-1]["log_message"])
jasonkmlu035eee12019-10-08 17:11:20 +08001008 ad.log.info("GnssLocationProvider reports location "
1009 "successfully.")
jasonkmluff0d5b92019-03-21 11:24:45 +08001010 return True
1011 if not ad.is_adb_logcat_on:
1012 ad.start_adb_logcat()
1013 ad.log.error("GnssLocationProvider is unable to report location.")
1014 return False
1015
Scott Hong72269692019-12-16 16:59:56 +08001016def check_network_location(ad, retries, location_type, criteria=30):
jasonkmluff0d5b92019-03-21 11:24:45 +08001017 """Verify if NLP reports location after requesting via GPSTool.
1018
1019 Args:
1020 ad: An AndroidDevice object.
1021 retries: Retry time.
1022 location_type: neworkLocationType of cell or wifi.
Scott Hong72269692019-12-16 16:59:56 +08001023 criteria: expected nlp return time, default 30 seconds
jasonkmluff0d5b92019-03-21 11:24:45 +08001024
1025 Returns:
1026 True: NLP reports location.
1027 otherwise return False.
1028 """
Scott Hong72269692019-12-16 16:59:56 +08001029 criteria = criteria * 1000
jasonkmluff0d5b92019-03-21 11:24:45 +08001030 for i in range(retries):
1031 time.sleep(1)
1032 begin_time = get_current_epoch_time()
1033 ad.log.info("Try to get NLP status - attempt %d" % (i+1))
jasonkmlu035eee12019-10-08 17:11:20 +08001034 ad.adb.shell(
1035 "am start -S -n com.android.gpstool/.GPSTool --es mode nlp")
Scott Hong72269692019-12-16 16:59:56 +08001036 while get_current_epoch_time() - begin_time <= criteria:
jasonkmlu035eee12019-10-08 17:11:20 +08001037 logcat_results = ad.search_logcat("LocationManagerService: "
1038 "incoming location: Location",
1039 begin_time)
jasonkmluff0d5b92019-03-21 11:24:45 +08001040 if logcat_results:
1041 for logcat_result in logcat_results:
1042 if location_type in logcat_result["log_message"]:
1043 ad.log.info(logcat_result["log_message"])
1044 ad.send_keycode("BACK")
1045 return True
1046 if not ad.is_adb_logcat_on:
1047 ad.start_adb_logcat()
1048 ad.send_keycode("BACK")
1049 ad.log.error("Unable to report network location \"%s\"." % location_type)
1050 return False
1051
jasonkmlu0f546de2019-12-13 20:58:41 +08001052
jasonkmlu8d6bbfc2019-03-29 15:57:43 +08001053def set_attenuator_gnss_signal(ad, attenuator, atten_value):
jasonkmluff0d5b92019-03-21 11:24:45 +08001054 """Set attenuation value for different GNSS signal.
1055
1056 Args:
1057 ad: An AndroidDevice object.
jasonkmlu8d6bbfc2019-03-29 15:57:43 +08001058 attenuator: The attenuator object.
jasonkmluff0d5b92019-03-21 11:24:45 +08001059 atten_value: attenuation value
1060 """
jasonkmluff0d5b92019-03-21 11:24:45 +08001061 try:
jasonkmlu035eee12019-10-08 17:11:20 +08001062 ad.log.info(
1063 "Set attenuation value to \"%d\" for GNSS signal." % atten_value)
jasonkmlu8d6bbfc2019-03-29 15:57:43 +08001064 attenuator[0].set_atten(atten_value)
jasonkmlu81b8b032019-06-21 15:18:27 +08001065 except Exception as e:
jasonkmlu4fcdcad2019-07-10 14:35:12 +08001066 ad.log.error(e)
jasonkmluff0d5b92019-03-21 11:24:45 +08001067
jasonkmlu0f546de2019-12-13 20:58:41 +08001068
jasonkmluff0d5b92019-03-21 11:24:45 +08001069def set_battery_saver_mode(ad, state):
1070 """Enable or diable battery saver mode via adb.
1071
1072 Args:
1073 ad: An AndroidDevice object.
1074 state: True is enable Battery Saver mode. False is disable.
1075 """
1076 ad.root_adb()
1077 if state:
1078 ad.log.info("Enable Battery Saver mode.")
1079 ad.adb.shell("cmd battery unplug")
1080 ad.adb.shell("settings put global low_power 1")
1081 else:
1082 ad.log.info("Disable Battery Saver mode.")
1083 ad.adb.shell("settings put global low_power 0")
1084 ad.adb.shell("cmd battery reset")
1085
jasonkmlu0f546de2019-12-13 20:58:41 +08001086
jasonkmluff0d5b92019-03-21 11:24:45 +08001087def set_gnss_qxdm_mask(ad, masks):
1088 """Find defined gnss qxdm mask and set as default logging mask.
1089
1090 Args:
1091 ad: An AndroidDevice object.
1092 masks: Defined gnss qxdm mask.
1093 """
1094 try:
1095 for mask in masks:
1096 if not tutils.find_qxdm_log_mask(ad, mask):
1097 continue
1098 tutils.set_qxdm_logger_command(ad, mask)
1099 break
1100 except Exception as e:
1101 ad.log.error(e)
jasonkmlu78540632019-11-15 18:04:20 +08001102 raise signals.TestError("Failed to set any QXDM masks.")
jasonkmlu903ece82019-05-16 14:56:19 +08001103
jasonkmlu0f546de2019-12-13 20:58:41 +08001104
jasonkmlu903ece82019-05-16 14:56:19 +08001105def start_youtube_video(ad, url=None, retries=0):
1106 """Start youtube video and verify if audio is in music state.
jasonkmlu81b8b032019-06-21 15:18:27 +08001107
jasonkmlu903ece82019-05-16 14:56:19 +08001108 Args:
1109 ad: An AndroidDevice object.
1110 url: Youtube video url.
1111 retries: Retry times if audio is not in music state.
jasonkmlu81b8b032019-06-21 15:18:27 +08001112
jasonkmlu903ece82019-05-16 14:56:19 +08001113 Returns:
1114 True if youtube video is playing normally.
1115 False if youtube video is not playing properly.
1116 """
1117 for i in range(retries):
1118 ad.log.info("Open an youtube video - attempt %d" % (i+1))
1119 ad.adb.shell("am start -a android.intent.action.VIEW -d \"%s\"" % url)
1120 time.sleep(2)
jasonkmlu035eee12019-10-08 17:11:20 +08001121 out = ad.adb.shell(
1122 "dumpsys activity | grep NewVersionAvailableActivity")
jasonkmlu903ece82019-05-16 14:56:19 +08001123 if out:
1124 ad.log.info("Skip Youtube New Version Update.")
1125 ad.send_keycode("BACK")
1126 if tutils.wait_for_state(ad.droid.audioIsMusicActive, True, 15, 1):
1127 ad.log.info("Started a video in youtube, audio is in MUSIC state")
1128 return True
1129 ad.log.info("Force-Stop youtube and reopen youtube again.")
1130 ad.force_stop_apk("com.google.android.youtube")
jasonkmlu903ece82019-05-16 14:56:19 +08001131 check_currrent_focus_app(ad)
jasonkmlu78540632019-11-15 18:04:20 +08001132 raise signals.TestError("Started a video in youtube, "
1133 "but audio is not in MUSIC state")
jasonkmlu81b8b032019-06-21 15:18:27 +08001134
jasonkmlu0f546de2019-12-13 20:58:41 +08001135
jasonkmlu81b8b032019-06-21 15:18:27 +08001136def get_baseband_and_gms_version(ad, extra_msg=""):
1137 """Get current radio baseband and GMSCore version of AndroidDevice object.
1138
1139 Args:
1140 ad: An AndroidDevice object.
1141 """
1142 try:
jasonkmlud93060f2019-11-05 15:12:55 +08001143 build_version = ad.adb.getprop("ro.build.id")
jasonkmlu81b8b032019-06-21 15:18:27 +08001144 baseband_version = ad.adb.getprop("gsm.version.baseband")
jasonkmlu035eee12019-10-08 17:11:20 +08001145 gms_version = ad.adb.shell(
1146 "dumpsys package com.google.android.gms | grep versionName"
1147 ).split("\n")[0].split("=")[1]
1148 mpss_version = ad.adb.shell("cat /sys/devices/soc0/images | grep MPSS "
1149 "| cut -d ':' -f 3")
jasonkmlu81b8b032019-06-21 15:18:27 +08001150 if not extra_msg:
jasonkmlud93060f2019-11-05 15:12:55 +08001151 ad.log.info("TestResult Build_Version %s" % build_version)
jasonkmlu81b8b032019-06-21 15:18:27 +08001152 ad.log.info("TestResult Baseband_Version %s" % baseband_version)
jasonkmlu035eee12019-10-08 17:11:20 +08001153 ad.log.info(
1154 "TestResult GMS_Version %s" % gms_version.replace(" ", ""))
1155 ad.log.info("TestResult MPSS_Version %s" % mpss_version)
jasonkmlu81b8b032019-06-21 15:18:27 +08001156 else:
jasonkmlu035eee12019-10-08 17:11:20 +08001157 ad.log.info(
1158 "%s, Baseband_Version = %s" % (extra_msg, baseband_version))
jasonkmlu81b8b032019-06-21 15:18:27 +08001159 except Exception as e:
jasonkmlu035eee12019-10-08 17:11:20 +08001160 ad.log.error(e)
1161
jasonkmlu0f546de2019-12-13 20:58:41 +08001162
jasonkmlu035eee12019-10-08 17:11:20 +08001163def start_toggle_gnss_by_gtw_gpstool(ad, iteration):
1164 """Send toggle gnss off/on start_test_action
1165
1166 Args:
1167 ad: An AndroidDevice object.
1168 iteration: Iteration of toggle gnss off/on cycles.
1169 """
1170 msg_list = []
1171 begin_time = get_current_epoch_time()
1172 try:
1173 for i in range(1, 4):
1174 ad.adb.shell("am start -S -n com.android.gpstool/.GPSTool "
1175 "--es mode toggle --es cycle %d" % iteration)
1176 time.sleep(1)
1177 if ad.search_logcat("cmp=com.android.gpstool/.ToggleGPS",
1178 begin_time):
1179 ad.log.info("Send ToggleGPS start_test_action successfully.")
1180 break
1181 else:
1182 check_currrent_focus_app(ad)
jasonkmlu78540632019-11-15 18:04:20 +08001183 raise signals.TestError("Fail to send ToggleGPS "
1184 "start_test_action within 3 attempts.")
jasonkmlu035eee12019-10-08 17:11:20 +08001185 time.sleep(2)
1186 test_start = ad.search_logcat("GPSTool_ToggleGPS: startService",
1187 begin_time)
1188 if test_start:
1189 ad.log.info(test_start[-1]["log_message"].split(":")[-1].strip())
1190 else:
jasonkmlu78540632019-11-15 18:04:20 +08001191 raise signals.TestError("Fail to start toggle GPS off/on test.")
jasonkmlu035eee12019-10-08 17:11:20 +08001192 # Every iteration is expected to finish within 4 minutes.
1193 while get_current_epoch_time() - begin_time <= iteration * 240000:
1194 crash_end = ad.search_logcat("Force finishing activity "
1195 "com.android.gpstool/.GPSTool",
1196 begin_time)
1197 if crash_end:
jasonkmlu78540632019-11-15 18:04:20 +08001198 raise signals.TestError("GPSTool crashed. Abort test.")
jasonkmlu035eee12019-10-08 17:11:20 +08001199 toggle_results = ad.search_logcat("GPSTool : msg", begin_time)
1200 if toggle_results:
1201 for toggle_result in toggle_results:
1202 msg = toggle_result["log_message"]
1203 if not msg in msg_list:
1204 ad.log.info(msg.split(":")[-1].strip())
1205 msg_list.append(msg)
1206 if "timeout" in msg:
1207 raise signals.TestFailure("Fail to get location fixed "
1208 "within 60 seconds.")
1209 if "Test end" in msg:
1210 raise signals.TestPass("Completed quick toggle GNSS "
1211 "off/on test.")
1212 raise signals.TestFailure("Fail to finish toggle GPS off/on test "
1213 "within %d minutes" % (iteration * 4))
1214 finally:
1215 ad.send_keycode("HOME")