Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: build/android/pylib/device/device_utils.py

Issue 993733002: Add new method to disable android device charging. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | build/android/pylib/device/device_utils_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Provides a variety of device interactions based on adb. 5 """Provides a variety of device interactions based on adb.
6 6
7 Eventually, this will be based on adb_wrapper. 7 Eventually, this will be based on adb_wrapper.
8 """ 8 """
9 # pylint: disable=unused-argument 9 # pylint: disable=unused-argument
10 10
11 import collections 11 import collections
12 import contextlib
12 import itertools 13 import itertools
13 import logging 14 import logging
14 import multiprocessing 15 import multiprocessing
15 import os 16 import os
16 import re 17 import re
17 import sys 18 import sys
18 import tempfile 19 import tempfile
19 import time 20 import time
20 import zipfile 21 import zipfile
21 22
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 'enable_command': ( 63 'enable_command': (
63 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' 64 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && '
64 'echo 1 > /sys/class/power_supply/usb/online'), 65 'echo 1 > /sys/class/power_supply/usb/online'),
65 'disable_command': ( 66 'disable_command': (
66 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' 67 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && '
67 'chmod 644 /sys/class/power_supply/usb/online && ' 68 'chmod 644 /sys/class/power_supply/usb/online && '
68 'echo 0 > /sys/class/power_supply/usb/online'), 69 'echo 0 > /sys/class/power_supply/usb/online'),
69 }, 70 },
70 ] 71 ]
71 72
72
73 @decorators.WithExplicitTimeoutAndRetries( 73 @decorators.WithExplicitTimeoutAndRetries(
74 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) 74 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES)
75 def GetAVDs(): 75 def GetAVDs():
76 """Returns a list of Android Virtual Devices. 76 """Returns a list of Android Virtual Devices.
77 77
78 Returns: 78 Returns:
79 A list containing the configured AVDs. 79 A list containing the configured AVDs.
80 """ 80 """
81 lines = cmd_helper.GetCmdOutput([ 81 lines = cmd_helper.GetCmdOutput([
82 os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android'), 82 os.path.join(constants.ANDROID_SDK_ROOT, 'tools', 'android'),
(...skipping 1324 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 timeout: timeout in seconds 1407 timeout: timeout in seconds
1408 retries: number of retries 1408 retries: number of retries
1409 Returns: 1409 Returns:
1410 A dict containing various battery information as reported by dumpsys 1410 A dict containing various battery information as reported by dumpsys
1411 battery. 1411 battery.
1412 """ 1412 """
1413 result = {} 1413 result = {}
1414 # Skip the first line, which is just a header. 1414 # Skip the first line, which is just a header.
1415 for line in self.RunShellCommand( 1415 for line in self.RunShellCommand(
1416 ['dumpsys', 'battery'], check_return=True)[1:]: 1416 ['dumpsys', 'battery'], check_return=True)[1:]:
1417 k, v = line.split(': ', 1) 1417 # If usb charging has been disabled, an extra line of header exists.
1418 result[k.strip()] = v.strip() 1418 if 'UPDATES STOPPED' in line:
1419 logging.warning('Dumpsys battery not receiving updates. '
1420 'Run dumpsys battery reset if this is in error.')
1421 elif ':' not in line:
1422 logging.warning('Unknown line found in dumpsys battery.')
1423 logging.warning(line)
1424 else:
1425 k, v = line.split(': ', 1)
1426 result[k.strip()] = v.strip()
1419 return result 1427 return result
1420 1428
1421 @decorators.WithTimeoutAndRetriesFromInstance() 1429 @decorators.WithTimeoutAndRetriesFromInstance()
1422 def GetCharging(self, timeout=None, retries=None): 1430 def GetCharging(self, timeout=None, retries=None):
1423 """Gets the charging state of the device. 1431 """Gets the charging state of the device.
1424 1432
1425 Args: 1433 Args:
1426 timeout: timeout in seconds 1434 timeout: timeout in seconds
1427 retries: number of retries 1435 retries: number of retries
1428 Returns: 1436 Returns:
(...skipping 29 matching lines...) Expand all
1458 command = self._cache['charging_config']['enable_command'] 1466 command = self._cache['charging_config']['enable_command']
1459 else: 1467 else:
1460 command = self._cache['charging_config']['disable_command'] 1468 command = self._cache['charging_config']['disable_command']
1461 1469
1462 def set_and_verify_charging(): 1470 def set_and_verify_charging():
1463 self.RunShellCommand(command, check_return=True) 1471 self.RunShellCommand(command, check_return=True)
1464 return self.GetCharging() == enabled 1472 return self.GetCharging() == enabled
1465 1473
1466 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) 1474 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1)
1467 1475
1476 # TODO(rnephew): Make private when all use cases can use the context manager.
1477 @decorators.WithTimeoutAndRetriesFromInstance()
1478 def DisableBatteryUpdates(self, timeout=None, retries=None):
1479 """ Resets battery data and makes device appear like it is not
1480 charging so that it will collect power data since last charge.
1481
1482 Args:
1483 timeout: timeout in seconds
1484 retries: number of retries
1485 """
1486 def battery_updates_disabled():
1487 return self.GetCharging() is False
1488
1489 self.RunShellCommand(
1490 ['dumpsys', 'batterystats', '--reset'], check_return=True)
1491 battery_data = self.RunShellCommand(
1492 ['dumpsys', 'batterystats', '--charged',
1493 '--checkin'], check_return=True)
jbudorick 2015/03/17 20:03:51 nit: formatting. Can --checkin fit on the line abo
rnephew (Wrong account) 2015/03/17 20:37:58 Done.
1494 for line in battery_data:
1495 l = line.split(',')
1496 if l[3] == 'pwi' and l[1] != 0:
jbudorick 2015/03/17 20:03:51 This should check the length of the list returned
rnephew (Wrong account) 2015/03/17 20:37:58 Done.
1497 raise device_errors.CommandFailedError(
1498 'Non-zero pmi value found after reset.')
1499 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'],
1500 check_return=True)
1501 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1)
1502
1503 # TODO(rnephew): Make private when all use cases can use the context manager.
1504 @decorators.WithTimeoutAndRetriesFromInstance()
1505 def EnableBatteryUpdates(self, timeout=None, retries=None):
1506 """ Restarts device charging so that dumpsys no longer collects power data.
1507
1508 Args:
1509 timeout: timeout in seconds
1510 retries: number of retries
1511 """
1512 def battery_updates_enabled():
1513 return self.GetCharging() is True
1514
1515 self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True)
1516 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1)
1517
1518 @contextlib.contextmanager
1519 def BatteryMeasurement(self, timeout=None, retries=None):
1520 """Context manager that enables battery data collection. It makes
1521 the device appear to stop charging so that dumpsys will start collecting
1522 power data since last charge. Once the with block is exited, charging is
1523 resumed and power data since last charge is no longer collected.
1524
1525 Only for devices L and higher.
1526
1527 Example usage:
1528 with BatteryMeasurement():
1529 browser_actions()
1530 get_power_data() # report usage within this block
1531 after_measurements() # Anything that runs after power
1532 # measurements are collected
1533
1534 Args:
1535 timeout: timeout in seconds
1536 retries: number of retries
1537 """
1538 if self.build_version_sdk < constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP:
1539 raise device_errors.CommandFailedError('Device must be L or higher.')
1540 try:
1541 self.DisableBatteryUpdates(timeout=timeout, retries=retries)
1542 yield
1543 finally:
1544 self.EnableBatteryUpdates(timeout=timeout, retries=retries)
1545
1468 @decorators.WithTimeoutAndRetriesFromInstance() 1546 @decorators.WithTimeoutAndRetriesFromInstance()
1469 def GetDevicePieWrapper(self, timeout=None, retries=None): 1547 def GetDevicePieWrapper(self, timeout=None, retries=None):
1470 """Gets the absolute path to the run_pie wrapper on the device. 1548 """Gets the absolute path to the run_pie wrapper on the device.
1471 1549
1472 We have to build our device executables to be PIE, but they need to be able 1550 We have to build our device executables to be PIE, but they need to be able
1473 to run on versions of android that don't support PIE (i.e. ICS and below). 1551 to run on versions of android that don't support PIE (i.e. ICS and below).
1474 To do so, we push a wrapper to the device that lets older android versions 1552 To do so, we push a wrapper to the device that lets older android versions
1475 run PIE executables. This method pushes that wrapper to the device if 1553 run PIE executables. This method pushes that wrapper to the device if
1476 necessary and returns the path to it. 1554 necessary and returns the path to it.
1477 1555
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1519 """ 1597 """
1520 if not devices: 1598 if not devices:
1521 devices = adb_wrapper.AdbWrapper.GetDevices() 1599 devices = adb_wrapper.AdbWrapper.GetDevices()
1522 if not devices: 1600 if not devices:
1523 raise device_errors.NoDevicesError() 1601 raise device_errors.NoDevicesError()
1524 devices = [d if isinstance(d, cls) else cls(d) for d in devices] 1602 devices = [d if isinstance(d, cls) else cls(d) for d in devices]
1525 if async: 1603 if async:
1526 return parallelizer.Parallelizer(devices) 1604 return parallelizer.Parallelizer(devices)
1527 else: 1605 else:
1528 return parallelizer.SyncParallelizer(devices) 1606 return parallelizer.SyncParallelizer(devices)
OLDNEW
« no previous file with comments | « no previous file | build/android/pylib/device/device_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698