csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 1 | # 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 | |
| 6 | from __future__ import print_function |
| 7 | from _hardware import Hardware |
| 8 | import sys |
| 9 | import time |
| 10 | |
| 11 | class HardwareAndroid(Hardware): |
| 12 | def __init__(self, adb): |
| 13 | Hardware.__init__(self) |
csmartdalton | 5772eaa | 2016-10-11 18:28:54 -0700 | [diff] [blame] | 14 | self.warmup_time = 5 |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 15 | self._adb = adb |
csmartdalton | e4fd078 | 2016-11-09 08:41:23 -0800 | [diff] [blame] | 16 | |
| 17 | if self._adb.root(): |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 18 | self._adb.remount() |
csmartdalton | e4fd078 | 2016-11-09 08:41:23 -0800 | [diff] [blame] | 19 | |
| 20 | def __enter__(self): |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 21 | Hardware.__enter__(self) |
| 22 | if not self._adb.is_root() and self._adb.root(): |
| 23 | self._adb.remount() |
| 24 | |
csmartdalton | e4fd078 | 2016-11-09 08:41:23 -0800 | [diff] [blame] | 25 | self._adb.shell('\n'.join([ |
| 26 | # turn on airplane mode. |
| 27 | ''' |
| 28 | settings put global airplane_mode_on 1''', |
| 29 | |
| 30 | # disable GPS. |
| 31 | ''' |
Chris Dalton | 117d972 | 2018-04-27 17:10:39 -0600 | [diff] [blame] | 32 | settings put secure location_providers_allowed -gps |
| 33 | settings put secure location_providers_allowed -wifi |
| 34 | settings put secure location_providers_allowed -network'''])) |
csmartdalton | e4fd078 | 2016-11-09 08:41:23 -0800 | [diff] [blame] | 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'''])) |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 54 | else: |
| 55 | print("WARNING: no adb root access; results may be unreliable.", |
| 56 | file=sys.stderr) |
| 57 | |
Chris Dalton | 34d9055 | 2017-10-20 09:58:32 -0600 | [diff] [blame] | 58 | return self |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 59 | |
| 60 | def __exit__(self, exception_type, exception_value, traceback): |
| 61 | Hardware.__exit__(self, exception_type, exception_value, traceback) |
Chris Dalton | 49b7ed3 | 2017-10-23 17:19:37 -0600 | [diff] [blame] | 62 | self._adb.reboot() # some devices struggle waking up; just hard reboot. |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 63 | |
| 64 | def sanity_check(self): |
| 65 | Hardware.sanity_check(self) |
| 66 | |
csmartdalton | 2a96101 | 2016-10-11 12:15:13 -0700 | [diff] [blame] | 67 | 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 |
csmartdalton | 310d72c | 2016-10-18 09:19:50 -0700 | [diff] [blame] | 71 | 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''') |
csmartdalton | 2a96101 | 2016-10-11 12:15:13 -0700 | [diff] [blame] | 93 | |
| 94 | Hardware.print_debug_diagnostics(self) |