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
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)
14    self.warmup_time = 5
15    self._adb = adb
16
17    if self._adb.root():
18      self._adb.remount()
19      self._initial_ASLR = \
20        self._adb.check('cat /proc/sys/kernel/randomize_va_space')
21
22  def __enter__(self):
23    self._adb.shell('\n'.join([
24      # turn on airplane mode.
25      '''
26      settings put global airplane_mode_on 1''',
27
28      # disable GPS.
29      '''
30      for MODE in gps wifi network; do
31        settings put secure location_providers_allowed -$MODE
32      done''']))
33
34    if self._adb.is_root():
35      self._adb.shell('\n'.join([
36        # disable bluetooth, wifi, and mobile data.
37        '''
38        service call bluetooth_manager 8
39        svc wifi disable
40        svc data disable''',
41
42        # kill the gui.
43        '''
44        setprop ctl.stop media
45        setprop ctl.stop zygote
46        setprop ctl.stop surfaceflinger
47        setprop ctl.stop drm''',
48
49        # disable ASLR
50        '''
51        echo 0 > /proc/sys/kernel/randomize_va_space''']))
52    else:
53      print("WARNING: no adb root access; results may be unreliable.",
54            file=sys.stderr)
55
56    return Hardware.__enter__(self)
57
58  def __exit__(self, exception_type, exception_value, traceback):
59    Hardware.__exit__(self, exception_type, exception_value, traceback)
60
61    if self._adb.is_root():
62      self._adb.shell('\n'.join([
63        # restore ASLR.
64        '''
65        echo %s > /proc/sys/kernel/randomize_va_space''' % self._initial_ASLR,
66
67        # revive the gui.
68        '''
69        setprop ctl.start drm
70        setprop ctl.start surfaceflinger
71        setprop ctl.start zygote
72        setprop ctl.start media''']))
73
74  def sanity_check(self):
75    Hardware.sanity_check(self)
76
77  def print_debug_diagnostics(self):
78    # search for and print thermal trip points that may have been exceeded.
79    self._adb.shell('''\
80      THERMALDIR=/sys/class/thermal
81      if [ ! -d $THERMALDIR ]; then
82        exit
83      fi
84      for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do
85        cd $THERMALDIR/$ZONE
86        if [ ! -e mode ] || grep -Fxqv enabled mode || [ ! -e trip_point_0_temp ]; then
87          continue
88        fi
89        TEMP=$(cat temp)
90        TRIPPOINT=trip_point_0_temp
91        if [ $TEMP -le $(cat $TRIPPOINT) ]; then
92          echo "$ZONE ($(cat type)): temp=$TEMP <= $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
93        else
94          let i=1
95          while [ -e trip_point_${i}_temp ] &&
96                [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do
97            TRIPPOINT=trip_point_${i}_temp
98            let i=i+1
99          done
100          echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
101        fi
102      done''')
103
104    Hardware.print_debug_diagnostics(self)
105
106  def sleep(self, sleeptime):
107    Hardware.sleep(self, sleeptime)
108