Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| (...skipping 1420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1431 | 1431 |
| 1432 Parameters passed to this function are passed directly to | 1432 Parameters passed to this function are passed directly to |
| 1433 |logcat_monitor.LogcatMonitor| and are documented there. | 1433 |logcat_monitor.LogcatMonitor| and are documented there. |
| 1434 | 1434 |
| 1435 Args: | 1435 Args: |
| 1436 timeout: timeout in seconds | 1436 timeout: timeout in seconds |
| 1437 retries: number of retries | 1437 retries: number of retries |
| 1438 """ | 1438 """ |
| 1439 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) | 1439 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) |
| 1440 | 1440 |
| 1441 # TODO(rnephew): Remove when battery_utils is switched to. | |
| 1441 @decorators.WithTimeoutAndRetriesFromInstance() | 1442 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1442 def GetBatteryInfo(self, timeout=None, retries=None): | 1443 def GetBatteryInfo(self, timeout=None, retries=None): |
| 1443 """Gets battery info for the device. | 1444 """Gets battery info for the device. |
| 1444 | 1445 |
| 1445 Args: | 1446 Args: |
| 1446 timeout: timeout in seconds | 1447 timeout: timeout in seconds |
| 1447 retries: number of retries | 1448 retries: number of retries |
| 1448 Returns: | 1449 Returns: |
| 1449 A dict containing various battery information as reported by dumpsys | 1450 A dict containing various battery information as reported by dumpsys |
| 1450 battery. | 1451 battery. |
| 1451 """ | 1452 """ |
| 1452 result = {} | 1453 result = {} |
| 1453 # Skip the first line, which is just a header. | 1454 # Skip the first line, which is just a header. |
| 1454 for line in self.RunShellCommand( | 1455 for line in self.RunShellCommand( |
| 1455 ['dumpsys', 'battery'], check_return=True)[1:]: | 1456 ['dumpsys', 'battery'], check_return=True)[1:]: |
| 1456 # If usb charging has been disabled, an extra line of header exists. | 1457 # If usb charging has been disabled, an extra line of header exists. |
| 1457 if 'UPDATES STOPPED' in line: | 1458 if 'UPDATES STOPPED' in line: |
| 1458 logging.warning('Dumpsys battery not receiving updates. ' | 1459 logging.warning('Dumpsys battery not receiving updates. ' |
| 1459 'Run dumpsys battery reset if this is in error.') | 1460 'Run dumpsys battery reset if this is in error.') |
| 1460 elif ':' not in line: | 1461 elif ':' not in line: |
| 1461 logging.warning('Unknown line found in dumpsys battery.') | 1462 logging.warning('Unknown line found in dumpsys battery.') |
| 1462 logging.warning(line) | 1463 logging.warning(line) |
| 1463 else: | 1464 else: |
| 1464 k, v = line.split(': ', 1) | 1465 k, v = line.split(': ', 1) |
| 1465 result[k.strip()] = v.strip() | 1466 result[k.strip()] = v.strip() |
| 1466 return result | 1467 return result |
| 1467 | 1468 |
| 1469 # TODO(rnephew): Remove when battery_utils is switched to. | |
| 1468 @decorators.WithTimeoutAndRetriesFromInstance() | 1470 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1469 def GetCharging(self, timeout=None, retries=None): | 1471 def GetCharging(self, timeout=None, retries=None): |
| 1470 """Gets the charging state of the device. | 1472 """Gets the charging state of the device. |
| 1471 | 1473 |
| 1472 Args: | 1474 Args: |
| 1473 timeout: timeout in seconds | 1475 timeout: timeout in seconds |
| 1474 retries: number of retries | 1476 retries: number of retries |
| 1475 Returns: | 1477 Returns: |
| 1476 True if the device is charging, false otherwise. | 1478 True if the device is charging, false otherwise. |
| 1477 """ | 1479 """ |
| 1478 battery_info = self.GetBatteryInfo() | 1480 battery_info = self.GetBatteryInfo() |
| 1479 for k in ('AC powered', 'USB powered', 'Wireless powered'): | 1481 for k in ('AC powered', 'USB powered', 'Wireless powered'): |
| 1480 if (k in battery_info and | 1482 if (k in battery_info and |
| 1481 battery_info[k].lower() in ('true', '1', 'yes')): | 1483 battery_info[k].lower() in ('true', '1', 'yes')): |
| 1482 return True | 1484 return True |
| 1483 return False | 1485 return False |
| 1484 | 1486 |
| 1487 # TODO(rnephew): Remove when battery_utils is switched to. | |
| 1485 @decorators.WithTimeoutAndRetriesFromInstance() | 1488 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1486 def SetCharging(self, enabled, timeout=None, retries=None): | 1489 def SetCharging(self, enabled, timeout=None, retries=None): |
| 1487 """Enables or disables charging on the device. | 1490 """Enables or disables charging on the device. |
| 1488 | 1491 |
| 1489 Args: | 1492 Args: |
| 1490 enabled: A boolean indicating whether charging should be enabled or | 1493 enabled: A boolean indicating whether charging should be enabled or |
| 1491 disabled. | 1494 disabled. |
| 1492 timeout: timeout in seconds | 1495 timeout: timeout in seconds |
| 1493 retries: number of retries | 1496 retries: number of retries |
| 1494 """ | 1497 """ |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1505 command = self._cache['charging_config']['enable_command'] | 1508 command = self._cache['charging_config']['enable_command'] |
| 1506 else: | 1509 else: |
| 1507 command = self._cache['charging_config']['disable_command'] | 1510 command = self._cache['charging_config']['disable_command'] |
| 1508 | 1511 |
| 1509 def set_and_verify_charging(): | 1512 def set_and_verify_charging(): |
| 1510 self.RunShellCommand(command, check_return=True) | 1513 self.RunShellCommand(command, check_return=True) |
| 1511 return self.GetCharging() == enabled | 1514 return self.GetCharging() == enabled |
| 1512 | 1515 |
| 1513 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) | 1516 timeout_retry.WaitFor(set_and_verify_charging, wait_period=1) |
| 1514 | 1517 |
| 1515 # TODO(rnephew): Make private when all use cases can use the context manager. | 1518 # TODO(rnephew): Remove when battery_utils is switched to. |
| 1516 @decorators.WithTimeoutAndRetriesFromInstance() | 1519 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1517 def DisableBatteryUpdates(self, timeout=None, retries=None): | 1520 def DisableBatteryUpdates(self, timeout=None, retries=None): |
| 1518 """ Resets battery data and makes device appear like it is not | 1521 """ Resets battery data and makes device appear like it is not |
| 1519 charging so that it will collect power data since last charge. | 1522 charging so that it will collect power data since last charge. |
| 1520 | 1523 |
| 1521 Args: | 1524 Args: |
| 1522 timeout: timeout in seconds | 1525 timeout: timeout in seconds |
| 1523 retries: number of retries | 1526 retries: number of retries |
| 1524 """ | 1527 """ |
| 1525 def battery_updates_disabled(): | 1528 def battery_updates_disabled(): |
| 1526 return self.GetCharging() is False | 1529 return self.GetCharging() is False |
| 1527 | 1530 |
| 1528 self.RunShellCommand( | 1531 self.RunShellCommand( |
| 1529 ['dumpsys', 'batterystats', '--reset'], check_return=True) | 1532 ['dumpsys', 'batterystats', '--reset'], check_return=True) |
| 1530 battery_data = self.RunShellCommand( | 1533 battery_data = self.RunShellCommand( |
| 1531 ['dumpsys', 'batterystats', '--charged', '--checkin'], | 1534 ['dumpsys', 'batterystats', '--charged', '--checkin'], |
| 1532 check_return=True) | 1535 check_return=True) |
| 1533 ROW_TYPE_INDEX = 3 | 1536 ROW_TYPE_INDEX = 3 |
| 1534 PWI_POWER_INDEX = 5 | 1537 PWI_POWER_INDEX = 5 |
| 1535 for line in battery_data: | 1538 for line in battery_data: |
| 1536 l = line.split(',') | 1539 l = line.split(',') |
| 1537 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi' | 1540 if (len(l) > PWI_POWER_INDEX and l[ROW_TYPE_INDEX] == 'pwi' |
| 1538 and l[PWI_POWER_INDEX] != 0): | 1541 and l[PWI_POWER_INDEX] != 0): |
| 1539 raise device_errors.CommandFailedError( | 1542 raise device_errors.CommandFailedError( |
| 1540 'Non-zero pmi value found after reset.') | 1543 'Non-zero pmi value found after reset.') |
| 1541 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], | 1544 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '0'], |
| 1542 check_return=True) | 1545 check_return=True) |
| 1543 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) | 1546 timeout_retry.WaitFor(battery_updates_disabled, wait_period=1) |
| 1544 | 1547 |
| 1545 # TODO(rnephew): Make private when all use cases can use the context manager. | 1548 # TODO(rnephew): Remove when battery_utils is switched to. |
| 1546 @decorators.WithTimeoutAndRetriesFromInstance() | 1549 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1547 def EnableBatteryUpdates(self, timeout=None, retries=None): | 1550 def EnableBatteryUpdates(self, timeout=None, retries=None): |
| 1548 """ Restarts device charging so that dumpsys no longer collects power data. | 1551 """ Restarts device charging so that dumpsys no longer collects power data. |
| 1549 | 1552 |
| 1550 Args: | 1553 Args: |
| 1551 timeout: timeout in seconds | 1554 timeout: timeout in seconds |
| 1552 retries: number of retries | 1555 retries: number of retries |
| 1553 """ | 1556 """ |
| 1554 def battery_updates_enabled(): | 1557 def battery_updates_enabled(): |
| 1555 return self.GetCharging() is True | 1558 return self.GetCharging() is True |
| 1556 | 1559 |
| 1557 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '1'], | 1560 self.RunShellCommand(['dumpsys', 'battery', 'set', 'usb', '1'], |
| 1558 check_return=True) | 1561 check_return=True) |
| 1559 self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True) | 1562 self.RunShellCommand(['dumpsys', 'battery', 'reset'], check_return=True) |
| 1560 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) | 1563 timeout_retry.WaitFor(battery_updates_enabled, wait_period=1) |
| 1561 | 1564 |
| 1565 # TODO(rnephew): Remove when battery_utils is switched to. | |
| 1562 @contextlib.contextmanager | 1566 @contextlib.contextmanager |
| 1563 def BatteryMeasurement(self, timeout=None, retries=None): | 1567 def BatteryMeasurement(self, timeout=None, retries=None): |
| 1564 """Context manager that enables battery data collection. It makes | 1568 """Context manager that enables battery data collection. It makes |
| 1565 the device appear to stop charging so that dumpsys will start collecting | 1569 the device appear to stop charging so that dumpsys will start collecting |
| 1566 power data since last charge. Once the with block is exited, charging is | 1570 power data since last charge. Once the with block is exited, charging is |
| 1567 resumed and power data since last charge is no longer collected. | 1571 resumed and power data since last charge is no longer collected. |
| 1568 | 1572 |
| 1569 Only for devices L and higher. | 1573 Only for devices L and higher. |
| 1570 | 1574 |
| 1571 Example usage: | 1575 Example usage: |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1641 """ | 1645 """ |
| 1642 if not devices: | 1646 if not devices: |
| 1643 devices = adb_wrapper.AdbWrapper.GetDevices() | 1647 devices = adb_wrapper.AdbWrapper.GetDevices() |
| 1644 if not devices: | 1648 if not devices: |
| 1645 raise device_errors.NoDevicesError() | 1649 raise device_errors.NoDevicesError() |
| 1646 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1650 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
| 1647 if async: | 1651 if async: |
| 1648 return parallelizer.Parallelizer(devices) | 1652 return parallelizer.Parallelizer(devices) |
| 1649 else: | 1653 else: |
| 1650 return parallelizer.SyncParallelizer(devices) | 1654 return parallelizer.SyncParallelizer(devices) |
| 1655 | |
| 1656 # TODO(rnephew): Implement cache class. | |
| 1657 def GetCacheEntry(self, key): | |
|
perezju
2015/03/30 11:05:23
I'm not sure I like this idea. Why would a class e
jbudorick
2015/03/30 13:35:03
BatteryUtils doesn't know how to control the lifet
rnephew (Wrong account)
2015/03/30 17:50:29
It will use this when it stops passing the calls t
| |
| 1658 """Cache getter.""" | |
| 1659 return self._cache.get(key) | |
| 1660 | |
| 1661 def SetCacheEntry(self, key, value, override=False): | |
| 1662 """Cache setter.""" | |
| 1663 if key not in self._cache: | |
| 1664 self._cache[key] = value | |
| 1665 elif override and value != self._cache[key]: | |
| 1666 logging.warning('%s already in cache. Overriding %s with %s.', key, | |
| 1667 self._cache[key], value) | |
| 1668 self._cache[key] = value | |
| 1669 else: | |
| 1670 logging.warning('%s already in cache. Not overriding %s with %s.', key, | |
| 1671 self._cache[key], value) | |
| OLD | NEW |