| OLD | NEW |
| 1 # Copyright 2015 Google Inc. All rights reserved. | 1 # Copyright 2015 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 # anymore. It is sometimes thrown as USBErrorIO. | 84 # anymore. It is sometimes thrown as USBErrorIO. |
| 85 _ERRORS = ( | 85 _ERRORS = ( |
| 86 usb_exceptions.CommonUsbError, | 86 usb_exceptions.CommonUsbError, |
| 87 common.usb1.USBError, | 87 common.usb1.USBError, |
| 88 common.usb1.USBErrorIO) | 88 common.usb1.USBErrorIO) |
| 89 | 89 |
| 90 _SHELL_SUFFIX = ' ;echo -e "\n$?"' | 90 _SHELL_SUFFIX = ' ;echo -e "\n$?"' |
| 91 | 91 |
| 92 def __init__( | 92 def __init__( |
| 93 self, handle, banner, rsa_keys, on_error, port_path=None, | 93 self, handle, banner, rsa_keys, on_error, port_path=None, |
| 94 default_timeout_ms=10000, auth_timeout_ms=10000, lost_timeout_ms=10000): | 94 default_timeout_ms=10000, auth_timeout_ms=10000, lost_timeout_ms=10000, |
| 95 enable_resets=False): |
| 95 """Constructs an AdbCommandsSafe. | 96 """Constructs an AdbCommandsSafe. |
| 96 | 97 |
| 97 Arguments: | 98 Arguments: |
| 98 - port_path: str addressing the device on the USB bus, e.g. '1/2'. | 99 - port_path: str addressing the device on the USB bus, e.g. '1/2'. |
| 99 - handle: common.UsbHandle or None. | 100 - handle: common.UsbHandle or None. |
| 100 - banner: How the app present itself to the device. This affects | 101 - banner: How the app present itself to the device. This affects |
| 101 authentication so it is better to use an hardcoded constant. | 102 authentication so it is better to use an hardcoded constant. |
| 102 - rsa_keys: list of AuthSigner. | 103 - rsa_keys: list of AuthSigner. |
| 103 - on_error: callback to call in case of error. | 104 - on_error: callback to call in case of error. |
| 104 - default_timeout_ms: Timeout for adbd to reply to a command. | 105 - default_timeout_ms: Timeout for adbd to reply to a command. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 122 auth_timeout_ms, lost_timeout_ms) | 123 auth_timeout_ms, lost_timeout_ms) |
| 123 # Immutable. | 124 # Immutable. |
| 124 self._auth_timeout_ms = auth_timeout_ms | 125 self._auth_timeout_ms = auth_timeout_ms |
| 125 self._default_timeout_ms = default_timeout_ms | 126 self._default_timeout_ms = default_timeout_ms |
| 126 self._banner = banner or socket.gethostname() | 127 self._banner = banner or socket.gethostname() |
| 127 self._lost_timeout_ms = lost_timeout_ms | 128 self._lost_timeout_ms = lost_timeout_ms |
| 128 self._on_error = on_error | 129 self._on_error = on_error |
| 129 self._rsa_keys = rsa_keys | 130 self._rsa_keys = rsa_keys |
| 130 self._sleep = 0.1 | 131 self._sleep = 0.1 |
| 131 self._tries = int(round((self._lost_timeout_ms / 1000. + 5) / self._sleep)) | 132 self._tries = int(round((self._lost_timeout_ms / 1000. + 5) / self._sleep)) |
| 133 self._should_reset = enable_resets |
| 132 | 134 |
| 133 # State. | 135 # State. |
| 134 self._adb_cmd = None | 136 self._adb_cmd = None |
| 135 self._serial = None | 137 self._serial = None |
| 136 self._failure = None | 138 self._failure = None |
| 137 self._handle = handle | 139 self._handle = handle |
| 138 self._port_path = '/'.join(str(p) for p in port_path) if port_path else None | 140 self._port_path = '/'.join(str(p) for p in port_path) if port_path else None |
| 139 | 141 |
| 140 @classmethod | 142 @classmethod |
| 141 def ConnectDevice(cls, port_path, **kwargs): | 143 def ConnectDevice(cls, port_path, **kwargs): |
| (...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 self._handle, banner=self._banner, rsa_keys=self._rsa_keys, | 823 self._handle, banner=self._banner, rsa_keys=self._rsa_keys, |
| 822 auth_timeout_ms=self._auth_timeout_ms) | 824 auth_timeout_ms=self._auth_timeout_ms) |
| 823 self._failure = None if self._adb_cmd else 'unknown' | 825 self._failure = None if self._adb_cmd else 'unknown' |
| 824 break | 826 break |
| 825 except usb_exceptions.DeviceAuthError as e: | 827 except usb_exceptions.DeviceAuthError as e: |
| 826 self._failure = 'unauthorized' | 828 self._failure = 'unauthorized' |
| 827 _LOG.warning('AUTH FAILURE: %s: %s', self.port_path, e) | 829 _LOG.warning('AUTH FAILURE: %s: %s', self.port_path, e) |
| 828 except usb_exceptions.LibusbWrappingError as e: | 830 except usb_exceptions.LibusbWrappingError as e: |
| 829 self._failure = 'usb_failure' | 831 self._failure = 'usb_failure' |
| 830 _LOG.warning('I/O FAILURE: %s: %s', self.port_path, e) | 832 _LOG.warning('I/O FAILURE: %s: %s', self.port_path, e) |
| 831 # TODO(crbug.com/642440): Reset the handle here if fleet health | 833 if self._should_reset: |
| 832 # regresses and host kernel panics don't subside. | 834 self._handle.Reset() |
| 833 except adb_protocol.InvalidResponseError as e: | 835 except adb_protocol.InvalidResponseError as e: |
| 834 self._failure = 'protocol_fault' | 836 self._failure = 'protocol_fault' |
| 835 _LOG.warning('SYNC FAILURE: %s: %s', self.port_path, e) | 837 _LOG.warning('SYNC FAILURE: %s: %s', self.port_path, e) |
| 836 finally: | 838 finally: |
| 837 # Do not leak the USB handle when we can't talk to the device. | 839 # Do not leak the USB handle when we can't talk to the device. |
| 838 if not self._adb_cmd: | 840 if not self._adb_cmd: |
| 839 self.Close() | 841 self.Close() |
| 840 | 842 |
| 841 if not self._adb_cmd and self._handle: | 843 if not self._adb_cmd and self._handle: |
| 842 _LOG.error('Unexpected close') | 844 _LOG.error('Unexpected close') |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 883 self.Close() | 885 self.Close() |
| 884 if not self._WaitUntilFound(use_serial=use_serial, timeout=timeout): | 886 if not self._WaitUntilFound(use_serial=use_serial, timeout=timeout): |
| 885 return False | 887 return False |
| 886 if not self._OpenHandle(): | 888 if not self._OpenHandle(): |
| 887 return False | 889 return False |
| 888 return self._Connect(use_serial=use_serial) | 890 return self._Connect(use_serial=use_serial) |
| 889 | 891 |
| 890 def __repr__(self): | 892 def __repr__(self): |
| 891 return '<Device %s %s>' % ( | 893 return '<Device %s %s>' % ( |
| 892 self.port_path, self.serial if self.is_valid else '(broken)') | 894 self.port_path, self.serial if self.is_valid else '(broken)') |
| OLD | NEW |