aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/skpbench/_hardware_android.py
blob: d7990dcd819dbfa86defee347c5a4dc51c2b5159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright 2016 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import print_function
from _hardware import Hardware
import sys
import time

class HardwareAndroid(Hardware):
  def __init__(self, adb):
    Hardware.__init__(self)
    self.kick_in_time = 5
    self._adb = adb
    self._is_root = self._adb.attempt_root()
    if self._is_root:
      self._adb.remount()
    self._initial_airplane_mode = None
    self._initial_location_providers = None
    self._initial_ASLR = None

  def __enter__(self):
    # turn on airplane mode.
    self._initial_airplane_mode = \
      self._adb.check('settings get global airplane_mode_on')
    self._adb.shell('settings put global airplane_mode_on 1')

    # disable GPS.
    self._initial_location_providers = \
      self._adb.check('settings get secure location_providers_allowed')
    self._initial_location_providers = \
      self._initial_location_providers.replace(',', ' ')
    self._adb.shell('''\
      for PROVIDER in %s; do
        settings put secure location_providers_allowed -$PROVIDER
      done''' % self._initial_location_providers)

    if self._is_root:
      # disable bluetooth, wifi, and mobile data.
      # TODO: can we query these initial values?
      self._adb.shell('''\
        service call bluetooth_manager 8 &&
        svc wifi disable &&
        svc data disable''')

      # kill the gui.
      self._adb.shell('''\
        setprop ctl.stop media &&
        setprop ctl.stop zygote &&
        setprop ctl.stop surfaceflinger &&
        setprop ctl.stop drm''')

      # disable ASLR.
      self._initial_ASLR = \
        self._adb.check('cat /proc/sys/kernel/randomize_va_space')
      self._adb.shell('echo 0 > /proc/sys/kernel/randomize_va_space')
    else:
      print("WARNING: no adb root access; results may be unreliable.",
            file=sys.stderr)

    return Hardware.__enter__(self)

  def __exit__(self, exception_type, exception_value, traceback):
    Hardware.__exit__(self, exception_type, exception_value, traceback)

    if self._is_root:
      # restore ASLR.
      self._adb.shell('echo %s > /proc/sys/kernel/randomize_va_space' %
                      self._initial_ASLR)

      # revive the gui.
      self._adb.shell('''\
        setprop ctl.start drm &&
        setprop ctl.start surfaceflinger &&
        setprop ctl.start zygote &&
        setprop ctl.start media''')

    # restore GPS (doesn't seem to work if we killed the gui).
    self._adb.shell('''\
      for PROVIDER in %s; do
        settings put secure location_providers_allowed +$PROVIDER
      done''' % self._initial_location_providers)

    # restore airplane mode (doesn't seem to work if we killed the gui).
    self._adb.shell('settings put global airplane_mode_on %s' %
                    self._initial_airplane_mode)

  def sanity_check(self):
    Hardware.sanity_check(self)

  def print_debug_diagnostics(self):
    # search for and print thermal trip points that may have been exceeded.
    self._adb.shell('''\
      THERMALDIR=/sys/class/thermal
      if [ -e $THERMALDIR ]; then
        for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do
          cd $THERMALDIR/$ZONE
          if [ -e mode ] && grep -Fxq enabled mode; then
            TEMP=$(cat temp)
            TRIPPOINT=
            let i=0
            while [ -e trip_point_${i}_temp ] &&
                  [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do
              TRIPPOINT=trip_point_${i}_temp
              let i=i+1
            done
            if [ $TRIPPOINT ]; then
              echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)"
            fi
          fi
        done
      fi''')

    Hardware.print_debug_diagnostics(self)

  def sleep(self, sleeptime):
    Hardware.sleep(self, sleeptime)