OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 with power. | 5 """Provides a variety of device interactions with power. |
6 """ | 6 """ |
7 # pylint: disable=unused-argument | 7 # pylint: disable=unused-argument |
8 | 8 |
9 import collections | 9 import collections |
10 import contextlib | 10 import contextlib |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 Args: | 480 Args: |
481 enabled: A boolean indicating whether charging should be enabled or | 481 enabled: A boolean indicating whether charging should be enabled or |
482 disabled. | 482 disabled. |
483 timeout: timeout in seconds | 483 timeout: timeout in seconds |
484 retries: number of retries | 484 retries: number of retries |
485 """ | 485 """ |
486 if self.GetCharging() == enabled: | 486 if self.GetCharging() == enabled: |
487 logging.warning('Device charging already in expected state: %s', enabled) | 487 logging.warning('Device charging already in expected state: %s', enabled) |
488 return | 488 return |
489 | 489 |
| 490 self._DiscoverDeviceProfile() |
490 if enabled: | 491 if enabled: |
491 try: | 492 if self._cache['profile']['enable_command']: |
492 self.SetCharging(enabled) | 493 self.SetCharging(enabled) |
493 except device_errors.CommandFailedError: | 494 else: |
494 logging.info('Unable to enable charging via hardware.' | 495 logging.info('Unable to enable charging via hardware. ' |
495 ' Falling back to software enabling.') | 496 'Falling back to software enabling.') |
496 self.EnableBatteryUpdates() | 497 self.EnableBatteryUpdates() |
497 else: | 498 else: |
498 try: | 499 if self._cache['profile']['enable_command']: |
499 self._ClearPowerData() | 500 self._ClearPowerData() |
500 self.SetCharging(enabled) | 501 self.SetCharging(enabled) |
501 except device_errors.CommandFailedError: | 502 else: |
502 logging.info('Unable to disable charging via hardware.' | 503 logging.info('Unable to disable charging via hardware. ' |
503 ' Falling back to software disabling.') | 504 'Falling back to software disabling.') |
504 self.DisableBatteryUpdates() | 505 self.DisableBatteryUpdates() |
505 | 506 |
506 @contextlib.contextmanager | 507 @contextlib.contextmanager |
507 def PowerMeasurement(self, timeout=None, retries=None): | 508 def PowerMeasurement(self, timeout=None, retries=None): |
508 """Context manager that enables battery power collection. | 509 """Context manager that enables battery power collection. |
509 | 510 |
510 Once the with block is exited, charging is resumed. Will attempt to disable | 511 Once the with block is exited, charging is resumed. Will attempt to disable |
511 charging at the hardware level, and if that fails will fall back to software | 512 charging at the hardware level, and if that fails will fall back to software |
512 disabling of battery updates. | 513 disabling of battery updates. |
513 | 514 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 self._cache['profile'] = { | 586 self._cache['profile'] = { |
586 'name': None, | 587 'name': None, |
587 'witness_file': None, | 588 'witness_file': None, |
588 'enable_command': None, | 589 'enable_command': None, |
589 'disable_command': None, | 590 'disable_command': None, |
590 'charge_counter': None, | 591 'charge_counter': None, |
591 'voltage': None, | 592 'voltage': None, |
592 'current': None, | 593 'current': None, |
593 } | 594 } |
594 return False | 595 return False |
OLD | NEW |