| OLD | NEW |
| 1 # Copyright 2014 Google Inc. All rights reserved. | 1 # Copyright 2014 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 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 device_matcher: See cls.FindDevices. | 333 device_matcher: See cls.FindDevices. |
| 334 **kwargs: See cls.FindDevices. | 334 **kwargs: See cls.FindDevices. |
| 335 | 335 |
| 336 Returns: | 336 Returns: |
| 337 An instance of UsbHandle. | 337 An instance of UsbHandle. |
| 338 | 338 |
| 339 Raises: | 339 Raises: |
| 340 DeviceNotFoundError: Raised if the device is not available. | 340 DeviceNotFoundError: Raised if the device is not available. |
| 341 """ | 341 """ |
| 342 try: | 342 try: |
| 343 return next(cls.FindDevices( | 343 return next(cls.FindDevicesSafe( |
| 344 setting_matcher, device_matcher=device_matcher, **kwargs)) | 344 setting_matcher, device_matcher=device_matcher, **kwargs)) |
| 345 except StopIteration: | 345 except StopIteration: |
| 346 raise usb_exceptions.DeviceNotFoundError( | 346 raise usb_exceptions.DeviceNotFoundError( |
| 347 'No device available, or it is in the wrong configuration.') | 347 'No device available, or it is in the wrong configuration.') |
| 348 | 348 |
| 349 @classmethod | 349 @classmethod |
| 350 def FindDevices(cls, setting_matcher, device_matcher=None, | 350 def FindDevices(cls, setting_matcher, device_matcher=None, |
| 351 usb_info='', timeout_ms=None): | 351 usb_info='', timeout_ms=None): |
| 352 """Find and yield the devices that match. | 352 """Find and yield the devices that match. |
| 353 | 353 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 366 for device in ctx.getDeviceList(skip_on_error=True): | 366 for device in ctx.getDeviceList(skip_on_error=True): |
| 367 setting = setting_matcher(device) | 367 setting = setting_matcher(device) |
| 368 if setting is None: | 368 if setting is None: |
| 369 continue | 369 continue |
| 370 | 370 |
| 371 handle = cls(device, setting, usb_info=usb_info, timeout_ms=timeout_ms) | 371 handle = cls(device, setting, usb_info=usb_info, timeout_ms=timeout_ms) |
| 372 if device_matcher is None or device_matcher(handle): | 372 if device_matcher is None or device_matcher(handle): |
| 373 yield handle | 373 yield handle |
| 374 | 374 |
| 375 @classmethod | 375 @classmethod |
| 376 def FindDevicesSafe(cls, *args, **kwargs): | 376 def FindDevicesSafe(cls, setting_matcher, device_matcher=None, |
| 377 """Safe version of FindDevicesSafe. | 377 usb_info='', timeout_ms=None): |
| 378 """Safe version of FindDevices. |
| 378 | 379 |
| 379 Use manual iterator handling instead of "for handle in generator" to catch | 380 Like FindDevices, but catch USB exceptions as devices are iterated through. |
| 380 USB exception explicitly. | 381 |
| 382 Yields: |
| 383 Unopened UsbHandle instances. |
| 381 """ | 384 """ |
| 382 generator = cls.FindDevices(*args, **kwargs) | 385 ctx = usb1.USBContext() |
| 383 while True: | 386 try: |
| 384 try: | 387 for device in ctx.getDeviceList(skip_on_error=True): |
| 385 handle = generator.next() | 388 setting = setting_matcher(device) |
| 386 except usb1.USBErrorOther as e: | 389 if setting is None: |
| 387 logging.error( | 390 continue |
| 388 'Failed to open USB device, is user in group plugdev? %s', e) | 391 |
| 389 continue | 392 try: |
| 390 yield handle | 393 handle = cls(device, setting, usb_info=usb_info, |
| 394 timeout_ms=timeout_ms) |
| 395 if device_matcher is None or device_matcher(handle): |
| 396 yield handle |
| 397 except (usb1.USBErrorOther, usb1.USBErrorNoDevice) as e: |
| 398 logging.error( |
| 399 'Failed to open USB device, is user in group plugdev? %s', e) |
| 400 continue |
| 401 except usb1.USBError as e: |
| 402 logging.error('Failed to get device list: %s', e) |
| 391 | 403 |
| 392 | 404 |
| 393 class TcpHandle(Handle): | 405 class TcpHandle(Handle): |
| 394 """TCP connection object. | 406 """TCP connection object. |
| 395 | 407 |
| 396 Provides same interface as UsbHandle.""" | 408 Provides same interface as UsbHandle.""" |
| 397 | 409 |
| 398 def __init__(self, serial, timeout_ms=None): | 410 def __init__(self, serial, timeout_ms=None): |
| 399 """Initialize the TCP Handle. | 411 """Initialize the TCP Handle. |
| 400 Arguments: | 412 Arguments: |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 'Could not send data (timeout %sms)' % (self.Timeout(timeout_ms)), e) | 474 'Could not send data (timeout %sms)' % (self.Timeout(timeout_ms)), e) |
| 463 | 475 |
| 464 def BulkRead(self, length, timeout_ms=None): | 476 def BulkRead(self, length, timeout_ms=None): |
| 465 try: | 477 try: |
| 466 self._connection.settimeout(self.Timeout(timeout_ms) / 1000.0) | 478 self._connection.settimeout(self.Timeout(timeout_ms) / 1000.0) |
| 467 return self._connection.recv(length) | 479 return self._connection.recv(length) |
| 468 except socket.timeout as e: | 480 except socket.timeout as e: |
| 469 raise usb_exceptions.ReadFailedError( | 481 raise usb_exceptions.ReadFailedError( |
| 470 'Could not receive data (timeout %sms)' % ( | 482 'Could not receive data (timeout %sms)' % ( |
| 471 self.Timeout(timeout_ms)), e) | 483 self.Timeout(timeout_ms)), e) |
| OLD | NEW |