_hardware_android.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from __future__ import print_function
  6. from _hardware import Hardware
  7. import sys
  8. import time
  9. class HardwareAndroid(Hardware):
  10. def __init__(self, adb):
  11. Hardware.__init__(self)
  12. self.warmup_time = 5
  13. self._adb = adb
  14. if self._adb.root():
  15. self._adb.remount()
  16. def __enter__(self):
  17. Hardware.__enter__(self)
  18. if not self._adb.is_root() and self._adb.root():
  19. self._adb.remount()
  20. self._adb.shell('\n'.join([
  21. # turn on airplane mode.
  22. '''
  23. settings put global airplane_mode_on 1''',
  24. # disable GPS.
  25. '''
  26. settings put secure location_providers_allowed -gps
  27. settings put secure location_providers_allowed -wifi
  28. settings put secure location_providers_allowed -network''']))
  29. if self._adb.is_root():
  30. self._adb.shell('\n'.join([
  31. # disable bluetooth, wifi, and mobile data.
  32. '''
  33. service call bluetooth_manager 8
  34. svc wifi disable
  35. svc data disable''',
  36. # kill the gui.
  37. '''
  38. setprop ctl.stop media
  39. setprop ctl.stop zygote
  40. setprop ctl.stop surfaceflinger
  41. setprop ctl.stop drm''',
  42. # disable ASLR
  43. '''
  44. echo 0 > /proc/sys/kernel/randomize_va_space''']))
  45. else:
  46. print("WARNING: no adb root access; results may be unreliable.",
  47. file=sys.stderr)
  48. return self
  49. def __exit__(self, exception_type, exception_value, traceback):
  50. Hardware.__exit__(self, exception_type, exception_value, traceback)
  51. self._adb.reboot() # some devices struggle waking up; just hard reboot.
  52. def sanity_check(self):
  53. Hardware.sanity_check(self)
  54. def print_debug_diagnostics(self):
  55. # search for and print thermal trip points that may have been exceeded.
  56. self._adb.shell('''\
  57. THERMALDIR=/sys/class/thermal
  58. if [ ! -d $THERMALDIR ]; then
  59. exit
  60. fi
  61. for ZONE in $(cd $THERMALDIR; echo thermal_zone*); do
  62. cd $THERMALDIR/$ZONE
  63. if [ ! -e mode ] || grep -Fxqv enabled mode || [ ! -e trip_point_0_temp ]; then
  64. continue
  65. fi
  66. TEMP=$(cat temp)
  67. TRIPPOINT=trip_point_0_temp
  68. if [ $TEMP -le $(cat $TRIPPOINT) ]; then
  69. echo "$ZONE ($(cat type)): temp=$TEMP <= $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
  70. else
  71. let i=1
  72. while [ -e trip_point_${i}_temp ] &&
  73. [ $TEMP -gt $(cat trip_point_${i}_temp) ]; do
  74. TRIPPOINT=trip_point_${i}_temp
  75. let i=i+1
  76. done
  77. echo "$ZONE ($(cat type)): temp=$TEMP > $TRIPPOINT=$(cat $TRIPPOINT)" 1>&2
  78. fi
  79. done''')
  80. Hardware.print_debug_diagnostics(self)