Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return result
class USBTransfer(object):
"""
USB asynchronous transfer control & data.
All modification methods will raise if called on a submitted transfer.
Methods noted as "should not be called on a submitted transfer" will not
prevent you from reading, but returned value is unspecified.
"""
# Prevent garbage collector from freeing the free function before our
# instances, as we need it to property destruct them.
__libusb_free_transfer = libusb1.libusb_free_transfer
__libusb_cancel_transfer = libusb1.libusb_cancel_transfer
__USBError = libusb1.USBError
__LIBUSB_ERROR_NOT_FOUND = libusb1.LIBUSB_ERROR_NOT_FOUND
__transfer = None
__initialized = False
__submitted = False
__callback = None
__ctypesCallbackWrapper = None
def __init__(self, handle, iso_packets=0):
"""
You should not instanciate this class directly.
Call "getTransfer" method on an USBDeviceHandle instance to get
instances of this class.
"""
if iso_packets < 0:
raise ValueError('Cannot request a negative number of iso '
'packets.')
self.__handle = handle
if address & libusb1.USB_ENDPOINT_DIR_MASK:
self._read_endpoint = address
self._max_read_packet_len = endpoint.getMaxPacketSize()
else:
self._write_endpoint = address
assert self._read_endpoint is not None
assert self._write_endpoint is not None
handle = self._device.open()
iface_number = self._setting.getNumber()
try:
if handle.kernelDriverActive(iface_number):
handle.detachKernelDriver(iface_number)
except libusb1.USBError as e:
if e.value == libusb1.LIBUSB_ERROR_NOT_FOUND:
_LOG.warning('Kernel driver not found for interface: %s.', iface_number)
else:
raise
handle.claimInterface(iface_number)
self._handle = handle
self._interface_number = iface_number
with self._HANDLE_CACHE_LOCK:
self._HANDLE_CACHE[port_path] = self
# When this object is deleted, make sure it's closed.
weakref.ref(self, self.Close)
def getASCIIStringDescriptor(self, descriptor):
"""
Fetch description string for given descriptor in first available
language.
Return value is an ASCII string.
Return None if there is no such descriptor on device.
"""
descriptor_string = create_binary_buffer(STRING_LENGTH)
result = libusb1.libusb_get_string_descriptor_ascii(self.__handle,
descriptor, descriptor_string, sizeof(descriptor_string))
if result == libusb1.LIBUSB_ERROR_NOT_FOUND:
return None
if result < 0:
raise libusb1.USBError(result)
return descriptor_string.value
self.device_p = device_p
# Fetch device descriptor
device_descriptor = libusb1.libusb_device_descriptor()
result = libusb1.libusb_get_device_descriptor(device_p,
byref(device_descriptor))
if result:
raise libusb1.USBError(result)
self.device_descriptor = device_descriptor
# Fetch all configuration descriptors
self.__configuration_descriptor_list = []
append = self.__configuration_descriptor_list.append
for configuration_id in xrange(device_descriptor.bNumConfigurations):
config = libusb1.libusb_config_descriptor_p()
result = libusb1.libusb_get_config_descriptor(device_p,
configuration_id, byref(config))
if result == libusb1.LIBUSB_ERROR_NOT_FOUND:
# Some devices (ex windows' root hubs) tell they have one
# configuration, but they have no configuration descriptor.
continue
if result:
raise libusb1.USBError(result)
append(config.contents)
raise usb_exceptions.InvalidEndpointsError(
'Multiple out endpoints found')
self._write_endpoint = address
if self._read_endpoint is None:
raise usb_exceptions.InvalidEndpointsError('Missing in endpoint')
if self._write_endpoint is None:
raise usb_exceptions.InvalidEndpointsError('Missing out endpoint')
handle = self._device.open()
iface_number = self._setting.getNumber()
try:
if handle.kernelDriverActive(iface_number):
handle.detachKernelDriver(iface_number)
except libusb1.USBError as exception:
if exception.value == libusb1.LIBUSB_ERROR_NOT_FOUND:
_LOG.warning('Kernel driver not found for interface: %s.', iface_number)
else:
raise usb_exceptions.LibusbWrappingError(
exception, '%s failed to detach kernel driver', self)
# Try to claim the interface, if the device is gone, raise an IOError
try:
handle.claimInterface(iface_number)
except libusb1.USBError as exception:
raise usb_exceptions.LibusbWrappingError(
exception, '%s failed to claim interface: %d', self, iface_number)
self._handle = handle
self._interface_number = iface_number
All modification methods will raise if called on a submitted transfer.
Methods noted as "should not be called on a submitted transfer" will not
prevent you from reading, but returned value is unspecified.
Note on user_data: because of pypy's current ctype restrictions, user_data
is not provided to C level, but is managed purely in python. It should
change nothing for you, unless you are looking at underlying C transfer
structure - which you should never have to.
"""
# Prevent garbage collector from freeing the free function before our
# instances, as we need it to property destruct them.
__libusb_free_transfer = libusb1.libusb_free_transfer
__libusb_cancel_transfer = libusb1.libusb_cancel_transfer
__USBError = libusb1.USBError
__LIBUSB_ERROR_NOT_FOUND = libusb1.LIBUSB_ERROR_NOT_FOUND
__transfer = None
__initialized = False
__submitted = False
__callback = None
__ctypesCallbackWrapper = None
__doomed = False
__user_data = None
__transfer_buffer = None
def __init__(self, handle, iso_packets=0):
"""
You should not instanciate this class directly.
Call "getTransfer" method on an USBDeviceHandle instance to get
instances of this class.
"""
if iso_packets < 0:
raise usb_exceptions.InvalidEndpointsError(
'Multiple out endpoints found')
self._write_endpoint = address
if self._read_endpoint is None:
raise usb_exceptions.InvalidEndpointsError('Missing in endpoint')
if self._write_endpoint is None:
raise usb_exceptions.InvalidEndpointsError('Missing out endpoint')
handle = self._device.open()
iface_number = self._setting.getNumber()
try:
if handle.kernelDriverActive(iface_number):
handle.detachKernelDriver(iface_number)
except libusb1.USBError as exception:
if exception.value == libusb1.LIBUSB_ERROR_NOT_FOUND:
_LOG.warning('Kernel driver not found for interface: %s.', iface_number)
else:
raise usb_exceptions.LibusbWrappingError(
exception, '%s failed to detach kernel driver', self)
# Try to claim the interface, if the device is gone, raise an IOError
try:
handle.claimInterface(iface_number)
except libusb1.USBError as exception:
raise usb_exceptions.LibusbWrappingError(
exception, '%s failed to claim interface: %d', self, iface_number)
self._handle = handle
self._interface_number = iface_number
self._read_endpoint = address
self._max_read_packet_len = endpoint.getMaxPacketSize()
else:
self._write_endpoint = address
assert self._read_endpoint is not None
assert self._write_endpoint is not None
handle = self._device.open()
iface_number = self._setting.getNumber()
try:
if (platform.system() != 'Windows'
and handle.kernelDriverActive(iface_number)):
handle.detachKernelDriver(iface_number)
except libusb1.USBError as e:
if e.value == libusb1.LIBUSB_ERROR_NOT_FOUND:
_LOG.warning('Kernel driver not found for interface: %s.', iface_number)
else:
raise
handle.claimInterface(iface_number)
self._handle = handle
self._interface_number = iface_number
with self._HANDLE_CACHE_LOCK:
self._HANDLE_CACHE[port_path] = self
# When this object is deleted, make sure it's closed.
weakref.ref(self, self.Close)