blob: 62e3ddd4207304f4762c0170a36d6b876e6ec6e2 [file] [log] [blame]
csmartdaltond7a9db62016-09-22 05:10:02 -07001# Copyright 2016 Google Inc.
2#
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from __future__ import print_function
7from _hardware import Hardware
8import sys
9import time
10
11class HardwareAndroid(Hardware):
12 def __init__(self, adb):
13 Hardware.__init__(self)
csmartdalton5772eaa2016-10-11 18:28:54 -070014 self.warmup_time = 5
csmartdaltond7a9db62016-09-22 05:10:02 -070015 self._adb = adb
csmartdaltone4fd0782016-11-09 08:41:23 -080016
17 if self._adb.root():
csmartdaltond7a9db62016-09-22 05:10:02 -070018 self._adb.remount()
csmartdaltone4fd0782016-11-09 08:41:23 -080019
20 def __enter__(self):
Chris Dalton34d90552017-10-20 09:58:32 -060021 Hardware.__enter__(self)
22 if not self._adb.is_root() and self._adb.root():
23 self._adb.remount()
24
csmartdaltone4fd0782016-11-09 08:41:23 -080025 self._adb.shell('\n'.join([
26 # turn on airplane mode.
27 '''
28 settings put global airplane_mode_on 1''',
29
30 # disable GPS.
31 '''
32 for MODE in gps wifi network; do
33 settings put secure location_providers_allowed -$MODE
34 done''']))
35
36 if self._adb.is_root():
37 self._adb.shell('\n'.join([
38 # disable bluetooth, wifi, and mobile data.
39 '''
40 service call bluetooth_manager 8
41 svc wifi disable
42 svc data disable''',
43
44 # kill the gui.
45 '''
46 setprop ctl.stop media
47 setprop ctl.stop zygote
48 setprop ctl.stop surfaceflinger
49 setprop ctl.stop drm''',
50
51 # disable ASLR
52 '''
53 echo 0 > /proc/sys/kernel/randomize_va_space''']))
csmartdaltond7a9db62016-09-22 05:10:02 -070054 else:
55 print("WARNING: no adb root access; results may be unreliable.",
56 file=sys.stderr)
57
Chris Dalton34d90552017-10-20 09:58:32 -060058 return self
csmartdaltond7a9db62016-09-22 05:10:02 -070059
60 def __exit__(self, exception_type, exception_value, traceback):
61 Hardware.__exit__(self, exception_type, exception_value, traceback)
Chris Dalton49b7ed32017-10-23 17:19:37 -060062 self._adb.reboot() # some devices struggle waking up; just hard reboot.
csmartdaltond7a9db62016-09-22 05:10:02 -070063
64 def sanity_check(self):
65 Hardware.sanity_check(self)
66
csmartdalton2a961012016-10-11 12:15:13 -070067 def print_debug_diagnostics(self):
68 # search for and print thermal trip points that may have been exceeded.
69 self._adb.shell('''\
70 THERMALDIR=/sys/class/thermal
csmartdalton310d72c2016-10-18 09:19:50 -070071 if [ ! -d $THERMALDIR ]; then
72 exit
73 fi
74 for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do
75 cd $THERMALDIR/$ZONE
76 if [ ! -e mode ] || grep -Fxqv enabled mode || [ ! -e trip_point_0_temp ]; then
77 continue
78 fi
79 TEMP=$(cat temp)
80 TRIPPOINT=trip_point_0_temp
81 if [ $TEMP -le $(cat $TRIPPOINT) ]; then
82 echo "$ZONE ($(cat type)): temp=$TEMP <= $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
83 else
84 let i=1
85 while [ -e trip_point_${i}_temp ] &&
86 [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do
87 TRIPPOINT=trip_point_${i}_temp
88 let i=i+1
89 done
90 echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
91 fi
92 done''')
csmartdalton2a961012016-10-11 12:15:13 -070093
94 Hardware.print_debug_diagnostics(self)