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) |
| 14 | self.kick_in_time = 5 |
| 15 | self._adb = adb |
| 16 | self._is_root = self._adb.attempt_root() |
| 17 | if self._is_root: |
| 18 | self._adb.remount() |
| 19 | self._initial_airplane_mode = None |
| 20 | self._initial_location_providers = None |
| 21 | self._initial_ASLR = None |
| 22 | |
| 23 | def __enter__(self): |
| 24 | # turn on airplane mode. |
| 25 | self._initial_airplane_mode = \ |
| 26 | self._adb.check('settings get global airplane_mode_on') |
| 27 | self._adb.shell('settings put global airplane_mode_on 1') |
| 28 | |
| 29 | # disable GPS. |
| 30 | self._initial_location_providers = \ |
| 31 | self._adb.check('settings get secure location_providers_allowed') |
| 32 | self._initial_location_providers = \ |
| 33 | self._initial_location_providers.replace(',', ' ') |
| 34 | self._adb.shell('''\ |
| 35 | for PROVIDER in %s; do |
| 36 | settings put secure location_providers_allowed -$PROVIDER |
| 37 | done''' % self._initial_location_providers) |
| 38 | |
| 39 | if self._is_root: |
| 40 | # disable bluetooth, wifi, and mobile data. |
| 41 | # TODO: can we query these initial values? |
| 42 | self._adb.shell('''\ |
| 43 | service call bluetooth_manager 8 && |
| 44 | svc wifi disable && |
| 45 | svc data disable''') |
| 46 | |
| 47 | # kill the gui. |
| 48 | self._adb.shell('''\ |
| 49 | setprop ctl.stop media && |
| 50 | setprop ctl.stop zygote && |
| 51 | setprop ctl.stop surfaceflinger && |
| 52 | setprop ctl.stop drm''') |
| 53 | |
| 54 | # disable ASLR. |
| 55 | self._initial_ASLR = \ |
| 56 | self._adb.check('cat /proc/sys/kernel/randomize_va_space') |
| 57 | self._adb.shell('echo 0 > /proc/sys/kernel/randomize_va_space') |
| 58 | else: |
| 59 | print("WARNING: no adb root access; results may be unreliable.", |
| 60 | file=sys.stderr) |
| 61 | |
| 62 | return Hardware.__enter__(self) |
| 63 | |
| 64 | def __exit__(self, exception_type, exception_value, traceback): |
| 65 | Hardware.__exit__(self, exception_type, exception_value, traceback) |
| 66 | |
| 67 | if self._is_root: |
| 68 | # restore ASLR. |
| 69 | self._adb.shell('echo %s > /proc/sys/kernel/randomize_va_space' % |
| 70 | self._initial_ASLR) |
| 71 | |
| 72 | # revive the gui. |
| 73 | self._adb.shell('''\ |
| 74 | setprop ctl.start drm && |
| 75 | setprop ctl.start surfaceflinger && |
| 76 | setprop ctl.start zygote && |
| 77 | setprop ctl.start media''') |
| 78 | |
| 79 | # restore GPS (doesn't seem to work if we killed the gui). |
| 80 | self._adb.shell('''\ |
| 81 | for PROVIDER in %s; do |
| 82 | settings put secure location_providers_allowed +$PROVIDER |
| 83 | done''' % self._initial_location_providers) |
| 84 | |
| 85 | # restore airplane mode (doesn't seem to work if we killed the gui). |
| 86 | self._adb.shell('settings put global airplane_mode_on %s' % |
| 87 | self._initial_airplane_mode) |
| 88 | |
| 89 | def sanity_check(self): |
| 90 | Hardware.sanity_check(self) |
| 91 | |
csmartdalton | 2a96101 | 2016-10-11 12:15:13 -0700 | [diff] [blame^] | 92 | def print_debug_diagnostics(self): |
| 93 | # search for and print thermal trip points that may have been exceeded. |
| 94 | self._adb.shell('''\ |
| 95 | THERMALDIR=/sys/class/thermal |
| 96 | if [ -e $THERMALDIR ]; then |
| 97 | for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do |
| 98 | cd $THERMALDIR/$ZONE |
| 99 | if [ -e mode ] && grep -Fxq enabled mode; then |
| 100 | TEMP=$(cat temp) |
| 101 | TRIPPOINT= |
| 102 | let i=0 |
| 103 | while [ -e trip_point_${i}_temp ] && |
| 104 | [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do |
| 105 | TRIPPOINT=trip_point_${i}_temp |
| 106 | let i=i+1 |
| 107 | done |
| 108 | if [ $TRIPPOINT ]; then |
| 109 | echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)" |
| 110 | fi |
| 111 | fi |
| 112 | done |
| 113 | fi''') |
| 114 | |
| 115 | Hardware.print_debug_diagnostics(self) |
| 116 | |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 117 | def sleep(self, sleeptime): |
| 118 | Hardware.sleep(self, sleeptime) |