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 26 matching lines...) Expand all Loading... |
37 from pylib.utils import zip_utils | 37 from pylib.utils import zip_utils |
38 | 38 |
39 _DEFAULT_TIMEOUT = 30 | 39 _DEFAULT_TIMEOUT = 30 |
40 _DEFAULT_RETRIES = 3 | 40 _DEFAULT_RETRIES = 3 |
41 | 41 |
42 # A sentinel object for default values | 42 # A sentinel object for default values |
43 # TODO(jbudorick,perezju): revisit how default values are handled by | 43 # TODO(jbudorick,perezju): revisit how default values are handled by |
44 # the timeout_retry decorators. | 44 # the timeout_retry decorators. |
45 DEFAULT = object() | 45 DEFAULT = object() |
46 | 46 |
| 47 _CONTROL_USB_CHARGING_COMMANDS = [ |
| 48 { |
| 49 # Nexus 4 |
| 50 'witness_file': '/sys/module/pm8921_charger/parameters/disabled', |
| 51 'enable_command': 'echo 0 > /sys/module/pm8921_charger/parameters/disabled', |
| 52 'disable_command': |
| 53 'echo 1 > /sys/module/pm8921_charger/parameters/disabled', |
| 54 }, |
| 55 { |
| 56 # Nexus 5 |
| 57 # Setting the HIZ bit of the bq24192 causes the charger to actually ignore |
| 58 # energy coming from USB. Setting the power_supply offline just updates the |
| 59 # Android system to reflect that. |
| 60 'witness_file': '/sys/kernel/debug/bq24192/INPUT_SRC_CONT', |
| 61 'enable_command': ( |
| 62 'echo 0x4A > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' |
| 63 'echo 1 > /sys/class/power_supply/usb/online'), |
| 64 'disable_command': ( |
| 65 'echo 0xCA > /sys/kernel/debug/bq24192/INPUT_SRC_CONT && ' |
| 66 'chmod 644 /sys/class/power_supply/usb/online && ' |
| 67 'echo 0 > /sys/class/power_supply/usb/online'), |
| 68 }, |
| 69 ] |
| 70 |
47 | 71 |
48 @decorators.WithExplicitTimeoutAndRetries( | 72 @decorators.WithExplicitTimeoutAndRetries( |
49 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) | 73 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
50 def GetAVDs(): | 74 def GetAVDs(): |
51 """Returns a list of Android Virtual Devices. | 75 """Returns a list of Android Virtual Devices. |
52 | 76 |
53 Returns: | 77 Returns: |
54 A list containing the configured AVDs. | 78 A list containing the configured AVDs. |
55 """ | 79 """ |
56 lines = cmd_helper.GetCmdOutput([ | 80 lines = cmd_helper.GetCmdOutput([ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 self.old_interface = device | 160 self.old_interface = device |
137 else: | 161 else: |
138 raise ValueError('Unsupported device value: %r' % device) | 162 raise ValueError('Unsupported device value: %r' % device) |
139 self._commands_installed = None | 163 self._commands_installed = None |
140 self._default_timeout = default_timeout | 164 self._default_timeout = default_timeout |
141 self._default_retries = default_retries | 165 self._default_retries = default_retries |
142 self._cache = {} | 166 self._cache = {} |
143 assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR) | 167 assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR) |
144 assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR) | 168 assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR) |
145 | 169 |
| 170 def __str__(self): |
| 171 """Returns the device serial.""" |
| 172 return self.adb.GetDeviceSerial() |
| 173 |
146 @decorators.WithTimeoutAndRetriesFromInstance() | 174 @decorators.WithTimeoutAndRetriesFromInstance() |
147 def IsOnline(self, timeout=None, retries=None): | 175 def IsOnline(self, timeout=None, retries=None): |
148 """Checks whether the device is online. | 176 """Checks whether the device is online. |
149 | 177 |
150 Args: | 178 Args: |
151 timeout: timeout in seconds | 179 timeout: timeout in seconds |
152 retries: number of retries | 180 retries: number of retries |
153 | 181 |
154 Returns: | 182 Returns: |
155 True if the device is online, False otherwise. | 183 True if the device is online, False otherwise. |
(...skipping 1207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1363 | 1391 |
1364 Parameters passed to this function are passed directly to | 1392 Parameters passed to this function are passed directly to |
1365 |logcat_monitor.LogcatMonitor| and are documented there. | 1393 |logcat_monitor.LogcatMonitor| and are documented there. |
1366 | 1394 |
1367 Args: | 1395 Args: |
1368 timeout: timeout in seconds | 1396 timeout: timeout in seconds |
1369 retries: number of retries | 1397 retries: number of retries |
1370 """ | 1398 """ |
1371 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) | 1399 return logcat_monitor.LogcatMonitor(self.adb, *args, **kwargs) |
1372 | 1400 |
1373 def __str__(self): | 1401 @decorators.WithTimeoutAndRetriesFromInstance() |
1374 """Returns the device serial.""" | 1402 def GetBatteryInfo(self, timeout=None, retries=None): |
1375 return self.adb.GetDeviceSerial() | 1403 """Gets battery info for the device. |
| 1404 |
| 1405 Args: |
| 1406 timeout: timeout in seconds |
| 1407 retries: number of retries |
| 1408 Returns: |
| 1409 A dict containing various battery information as reported by dumpsys |
| 1410 battery. |
| 1411 """ |
| 1412 result = {} |
| 1413 # Skip the first line, which is just a header. |
| 1414 for line in self.RunShellCommand( |
| 1415 ['dumpsys', 'battery'], check_return=True)[1:]: |
| 1416 k, v = line.split(': ', 1) |
| 1417 result[k.strip()] = v.strip() |
| 1418 return result |
| 1419 |
| 1420 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1421 def GetUsbCharging(self, timeout=None, retries=None): |
| 1422 """Gets the USB charging state of the device. |
| 1423 |
| 1424 Args: |
| 1425 timeout: timeout in seconds |
| 1426 retries: number of retries |
| 1427 Returns: |
| 1428 True if the device is charging via USB, false otherwise. |
| 1429 """ |
| 1430 return (self.GetBatteryInfo().get('USB powered', '').lower() |
| 1431 in ('true', '1', 'yes')) |
| 1432 |
| 1433 @decorators.WithTimeoutAndRetriesFromInstance() |
| 1434 def SetUsbCharging(self, enabled, timeout=None, retries=None): |
| 1435 """Enables or disables USB charging on the device. |
| 1436 |
| 1437 Args: |
| 1438 enabled: A boolean indicating whether USB charging should be enabled or |
| 1439 disabled. |
| 1440 timeout: timeout in seconds |
| 1441 retries: number of retries |
| 1442 """ |
| 1443 if 'usb_charging_config' not in self._cache: |
| 1444 for c in _CONTROL_USB_CHARGING_COMMANDS: |
| 1445 if self.FileExists(c['witness_file']): |
| 1446 self._cache['usb_charging_config'] = c |
| 1447 break |
| 1448 else: |
| 1449 raise device_errors.CommandFailedError( |
| 1450 'Unable to find charging commands.') |
| 1451 |
| 1452 if enabled: |
| 1453 command = self._cache['usb_charging_config']['enable_command'] |
| 1454 else: |
| 1455 command = self._cache['usb_charging_config']['disable_command'] |
| 1456 |
| 1457 def set_and_verify_usb_charging(): |
| 1458 self.RunShellCommand(command) |
| 1459 return self.GetUsbCharging() == enabled |
| 1460 |
| 1461 timeout_retry.WaitFor(set_and_verify_usb_charging, wait_period=1) |
1376 | 1462 |
1377 @decorators.WithTimeoutAndRetriesFromInstance() | 1463 @decorators.WithTimeoutAndRetriesFromInstance() |
1378 def GetDevicePieWrapper(self, timeout=None, retries=None): | 1464 def GetDevicePieWrapper(self, timeout=None, retries=None): |
1379 """Gets the absolute path to the run_pie wrapper on the device. | 1465 """Gets the absolute path to the run_pie wrapper on the device. |
1380 | 1466 |
1381 We have to build our device executables to be PIE, but they need to be able | 1467 We have to build our device executables to be PIE, but they need to be able |
1382 to run on versions of android that don't support PIE (i.e. ICS and below). | 1468 to run on versions of android that don't support PIE (i.e. ICS and below). |
1383 To do so, we push a wrapper to the device that lets older android versions | 1469 To do so, we push a wrapper to the device that lets older android versions |
1384 run PIE executables. This method pushes that wrapper to the device if | 1470 run PIE executables. This method pushes that wrapper to the device if |
1385 necessary and returns the path to it. | 1471 necessary and returns the path to it. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1428 """ | 1514 """ |
1429 if not devices: | 1515 if not devices: |
1430 devices = adb_wrapper.AdbWrapper.GetDevices() | 1516 devices = adb_wrapper.AdbWrapper.GetDevices() |
1431 if not devices: | 1517 if not devices: |
1432 raise device_errors.NoDevicesError() | 1518 raise device_errors.NoDevicesError() |
1433 devices = [d if isinstance(d, cls) else cls(d) for d in devices] | 1519 devices = [d if isinstance(d, cls) else cls(d) for d in devices] |
1434 if async: | 1520 if async: |
1435 return parallelizer.Parallelizer(devices) | 1521 return parallelizer.Parallelizer(devices) |
1436 else: | 1522 else: |
1437 return parallelizer.SyncParallelizer(devices) | 1523 return parallelizer.SyncParallelizer(devices) |
OLD | NEW |