def ctrl_transfer(self, dev_handle, bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout): if usb.util.ctrl_direction(bmRequestType) == usb.util.CTRL_OUT: buff = data_or_wLength else: buff = _interop.as_array((0,) * data_or_wLength) addr, length = buff.buffer_info() length *= buff.itemsize ret = _check(_lib.libusb_control_transfer(dev_handle, bmRequestType, bRequest, wValue, wIndex, cast(addr, POINTER(c_ubyte)), length, timeout)) if usb.util.ctrl_direction(bmRequestType) == usb.util.CTRL_OUT: return ret.value else: return buff[:ret.value]
def ctrl_transfer(self, dev_handle, bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout): if usb.util.ctrl_direction(bmRequestType) == usb.util.CTRL_OUT: address, length = data_or_wLength.buffer_info() length *= data_or_wLength.itemsize return _check(_lib.usb_control_msg( dev_handle, bmRequestType, bRequest, wValue, wIndex, cast(address, c_char_p), length, timeout )) else: data = _interop.as_array((0,) * data_or_wLength) read = int(_check(_lib.usb_control_msg( dev_handle, bmRequestType, bRequest, wValue, wIndex, cast(data.buffer_info()[0], c_char_p), data_or_wLength, timeout ))) return data[:read]
def write(self, endpoint, data, timeout=None): r"""Write data to the endpoint. This method is used to send data to the device. The endpoint parameter corresponds to the bEndpointAddress member whose endpoint you want to communicate with. The data parameter should be a sequence like type convertible to the array type (see array module). The timeout is specified in miliseconds. The method returns the number of bytes written. """ backend = self._ctx.backend fn_map = { util.ENDPOINT_TYPE_BULK: backend.bulk_write, util.ENDPOINT_TYPE_INTR: backend.intr_write, util.ENDPOINT_TYPE_ISO: backend.iso_write } intf, ep = self._ctx.setup_request(self, endpoint) fn = fn_map[util.endpoint_type(ep.bmAttributes)] return fn(self._ctx.handle, ep.bEndpointAddress, intf.bInterfaceNumber, _interop.as_array(data), self.__get_timeout(timeout))
def ctrl_transfer(self, bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None): r"""Do a control transfer on the endpoint 0. This method is used to issue a control transfer over the endpoint 0(endpoint 0 is required to always be a control endpoint). The parameters bmRequestType, bRequest, wValue and wIndex are the same of the USB Standard Control Request format. Control requests may or may not have a data payload to write/read. In cases which it has, the direction bit of the bmRequestType field is used to infere the desired request direction. For host to device requests (OUT), data_or_wLength parameter is the data payload to send, and it must be a sequence type convertible to an array object. In this case, the return value is the number of data payload written. For device to host requests (IN), data_or_wLength is the wLength parameter of the control request specifying the number of bytes to read in data payload. In this case, the return value is the data payload read, as an array object. """ if util.ctrl_direction(bmRequestType) == util.CTRL_OUT: a = _interop.as_array(data_or_wLength) elif data_or_wLength is None: a = 0 else: a = data_or_wLength self._ctx.managed_open() return self._ctx.backend.ctrl_transfer( self._ctx.handle, bmRequestType, bRequest, wValue, wIndex, a, self.__get_timeout(timeout) )
def write(self, endpoint, data, interface=None, timeout=None): r"""Write data to the endpoint. This method is used to send data to the device. The endpoint parameter corresponds to the bEndpointAddress member whose endpoint you want to communicate with. The interface parameter is the bInterfaceNumber field of the interface descriptor which contains the endpoint. If you do not provide one, the first one found will be used, as explained in the set_interface_altsetting() method. The data parameter should be a sequence like type convertible to array type (see array module). The timeout is specified in miliseconds. The method returns the number of bytes written. """ backend = self._ctx.backend fn_map = { util.ENDPOINT_TYPE_BULK: backend.bulk_write, util.ENDPOINT_TYPE_INTR: backend.intr_write, util.ENDPOINT_TYPE_ISO: backend.iso_write } intf = self._ctx.get_interface(self, interface) fn = fn_map[self._ctx.get_endpoint_type(self, endpoint, intf)] self._ctx.managed_claim_interface(self, intf) return fn(self._ctx.handle, endpoint, intf.bInterfaceNumber, _interop.as_array(data), self.__get_timeout(timeout))
def write(self, endpoint, data, interface=None, timeout=None): r"""Write data to the endpoint. This method is used to send data to the device. The endpoint parameter corresponds to the bEndpointAddress member whose endpoint you want to communicate with. The interface parameter is the bInterfaceNumber field of the interface descriptor which contains the endpoint. If you do not provide one, the first one found will be used, as explained in the set_interface_altsetting() method. The data parameter should be a sequence like type convertible to array type (see array module). The timeout is specified in miliseconds. The method returns the number of bytes written. """ backend = self._ctx.backend fn_map = { util.ENDPOINT_TYPE_BULK: backend.bulk_write, util.ENDPOINT_TYPE_INTR: backend.intr_write, util.ENDPOINT_TYPE_ISO: backend.iso_write, } intf = self._ctx.get_interface(self, interface) fn = fn_map[self._ctx.get_endpoint_type(self, endpoint, intf)] self._ctx.managed_claim_interface(self, intf) return fn( self._ctx.handle, endpoint, intf.bInterfaceNumber, _interop.as_array(data), self.__get_timeout(timeout) )
def ctrl_transfer(self, dev_handle, bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout): request = _openusb_ctrl_request() request.setup.bmRequestType = bmRequestType request.setup.bRequest = bRequest request.setup.wValue request.setup.wIndex request.timeout = timeout direction = usb.util.ctrl_direction(bmRequestType) if direction == usb.util.CTRL_OUT: buffer = data_or_wLength else: buffer = _interop.as_array('\x00' * data_or_wLength) payload, request.length = buffer.buffer_info() request.payload = cast(payload, POINTER(c_uint8)) _check(_lib.openusb_ctrl_xfer(dev_handle, 0, 0, byref(request))) if direction == usb.util.CTRL_OUT: return request.result.transferred_bytes else: return buffer[:request.result.transferred_bytes]
def write(self, endpoint, data, timeout = None): r"""Write data to the endpoint. This method is used to send data to the device. The endpoint parameter corresponds to the bEndpointAddress member whose endpoint you want to communicate with. The data parameter should be a sequence like type convertible to the array type (see array module). The timeout is specified in miliseconds. The method returns the number of bytes written. """ backend = self._ctx.backend fn_map = { util.ENDPOINT_TYPE_BULK:backend.bulk_write, util.ENDPOINT_TYPE_INTR:backend.intr_write, util.ENDPOINT_TYPE_ISO:backend.iso_write } intf, ep = self._ctx.setup_request(self, endpoint) fn = fn_map[util.endpoint_type(ep.bmAttributes)] return fn( self._ctx.handle, ep.bEndpointAddress, intf.bInterfaceNumber, _interop.as_array(data), self.__get_timeout(timeout) )
def __read(self, fn, dev_handle, ep, intf, size, timeout): data = _interop.as_array((0, ) * size) address, length = data.buffer_info() length *= data.itemsize ret = int( _check(fn(dev_handle, ep, cast(address, c_char_p), length, timeout))) return data[:ret]
def intr_read(self, dev_handle, ep, intf, size, timeout): request = _openusb_intr_request() buffer = _interop.as_array('B', '\x00' * size) memset(byref(request), 0, sizeof(request)) payload, request.length = buffer.buffer_info() request.payload = cast(payload, POINTER(c_uint8)) request.timeout = timeout _check(_lib.openusb_intr_xfer(dev_handle, intf, ep, byref(request))) return buffer[:request.result.transferred_bytes]
def intr_read(self, dev_handle, ep, intf, size, timeout): request = _openusb_intr_request() buffer = _interop.as_array('\x00' * size) memset(byref(request), 0, sizeof(request)) payload, request.length = buffer.buffer_info() request.payload = cast(payload, POINTER(c_uint8)) request.timeout = timeout _check(_lib.openusb_intr_xfer(dev_handle, intf, ep, byref(request))) return buffer[:request.result.transferred_bytes]
def __read(self, fn, dev_handle, ep, intf, size, timeout): data = _interop.as_array((0, ) * size) address, length = data.buffer_info() length *= data.itemsize transferred = c_int() _check( fn(dev_handle, ep, cast(address, POINTER(c_ubyte)), length, byref(transferred), timeout)) return data[:transferred.value]
def ctrl_transfer(self, bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength = None, timeout = None): r"""Do a control transfer on the endpoint 0. This method is used to issue a control transfer over the endpoint 0 (endpoint 0 is required to always be a control endpoint). The parameters bmRequestType, bRequest, wValue and wIndex are the same of the USB Standard Control Request format. Control requests may or may not have a data payload to write/read. In cases which it has, the direction bit of the bmRequestType field is used to infer the desired request direction. For host to device requests (OUT), data_or_wLength parameter is the data payload to send, and it must be a sequence type convertible to an array object. In this case, the return value is the number of bytes written in the data payload. For device to host requests (IN), data_or_wLength is either the wLength parameter of the control request specifying the number of bytes to read in data payload, and the return value is an array object with data read, or an array object which the data will be read to, and the return value is the number of bytes read. """ try: buff = util.create_buffer(data_or_wLength) except TypeError: buff = _interop.as_array(data_or_wLength) self._ctx.managed_open() # Thanks to Johannes Stezenbach to point me out that we need to # claim the recipient interface recipient = bmRequestType & 3 rqtype = bmRequestType & (3 << 5) if recipient == util.CTRL_RECIPIENT_INTERFACE \ and rqtype != util.CTRL_TYPE_VENDOR: interface_number = wIndex & 0xff self._ctx.managed_claim_interface(self, interface_number) ret = self._ctx.backend.ctrl_transfer( self._ctx.handle, bmRequestType, bRequest, wValue, wIndex, buff, self.__get_timeout(timeout)) if isinstance(data_or_wLength, array.array) \ or util.ctrl_direction(bmRequestType) == util.CTRL_OUT: return ret elif ret != len(buff) * buff.itemsize: return buff[:ret] else: return buff
def ctrl_transfer(self, bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None): r"""Do a control transfer on the endpoint 0. This method is used to issue a control transfer over the endpoint 0 (endpoint 0 is required to always be a control endpoint). The parameters bmRequestType, bRequest, wValue and wIndex are the same of the USB Standard Control Request format. Control requests may or may not have a data payload to write/read. In cases which it has, the direction bit of the bmRequestType field is used to infer the desired request direction. For host to device requests (OUT), data_or_wLength parameter is the data payload to send, and it must be a sequence type convertible to an array object. In this case, the return value is the number of bytes written in the data payload. For device to host requests (IN), data_or_wLength is either the wLength parameter of the control request specifying the number of bytes to read in data payload, and the return value is an array object with data read, or an array object which the data will be read to, and the return value is the number of bytes read. """ try: buff = util.create_buffer(data_or_wLength) except TypeError: buff = _interop.as_array(data_or_wLength) self._ctx.managed_open() # Thanks to Johannes Stezenbach to point me out that we need to # claim the recipient interface recipient = bmRequestType & 3 rqtype = bmRequestType & (3 << 5) if recipient == util.CTRL_RECIPIENT_INTERFACE \ and rqtype != util.CTRL_TYPE_VENDOR: interface_number = wIndex & 0xff self._ctx.managed_claim_interface(self, interface_number) ret = self._ctx.backend.ctrl_transfer(self._ctx.handle, bmRequestType, bRequest, wValue, wIndex, buff, self.__get_timeout(timeout)) if isinstance(data_or_wLength, array.array) \ or util.ctrl_direction(bmRequestType) == util.CTRL_OUT: return ret elif ret != len(buff) * buff.itemsize: return buff[:ret] else: return buff
def __read(self, fn, dev_handle, ep, intf, size, timeout): data = _interop.as_array('\x00' * size) address, length = data.buffer_info() length *= data.itemsize transferred = c_int() retval = fn(dev_handle.handle, ep, cast(address, POINTER(c_ubyte)), length, byref(transferred), timeout) # do not assume LIBUSB_ERROR_TIMEOUT means no I/O. if not (transferred.value and retval == LIBUSB_ERROR_TIMEOUT): _check(retval) return data[:transferred.value]
def __read(self, fn, dev_handle, ep, intf, size, timeout): data = _interop.as_array((0,) * size) address, length = data.buffer_info() length *= data.itemsize transferred = c_int() _check(fn(dev_handle, ep, cast(address, POINTER(c_ubyte)), length, byref(transferred), timeout)) return data[:transferred.value]
def __read(self, fn, dev_handle, ep, intf, size, timeout): data = _interop.as_array((0,) * size) address, length = data.buffer_info() length *= data.itemsize ret = int(_check(fn( dev_handle, ep, cast(address, c_char_p), length, timeout ))) return data[:ret]
def clear_halt(self, dev_handle, ep): bmRequestType = util.build_request_type( util.CTRL_OUT, util.CTRL_TYPE_STANDARD, util.CTRL_RECIPIENT_ENDPOINT) self.ctrl_transfer( dev_handle, bmRequestType, 0x03, 0, ep, _interop.as_array(), 1000)
def __read(self, fn, dev_handle, ep, intf, size, timeout): data = _interop.as_array((0,) * size) address, length = data.buffer_info() length *= data.itemsize transferred = c_int() retval = fn(dev_handle, ep, cast(address, POINTER(c_ubyte)), length, byref(transferred), timeout) # do not assume LIBUSB_ERROR_TIMEOUT means no I/O. if not (transferred.value and retval == LIBUSB_ERROR_TIMEOUT): _check(retval) return data[:transferred.value]
def __read(self, fn, dev_handle, ep, intf, data, size, timeout): read_into = (data != None) if not read_into: data = _interop.as_array((0,) * size) address, length = data.buffer_info() length *= data.itemsize ret = int(_check(fn( dev_handle, ep, cast(address, c_char_p), length, timeout ))) if read_into: return ret else: return data[:ret]
def ctrl_transfer(self, bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None): r"""Do a control transfer on the endpoint 0. This method is used to issue a control transfer over the endpoint 0(endpoint 0 is required to always be a control endpoint). The parameters bmRequestType, bRequest, wValue and wIndex are the same of the USB Standard Control Request format. Control requests may or may not have a data payload to write/read. In cases which it has, the direction bit of the bmRequestType field is used to infere the desired request direction. For host to device requests (OUT), data_or_wLength parameter is the data payload to send, and it must be a sequence type convertible to an array object. In this case, the return value is the number of data payload written. For device to host requests (IN), data_or_wLength is the wLength parameter of the control request specifying the number of bytes to read in data payload. In this case, the return value is the data payload read, as an array object. """ if util.ctrl_direction(bmRequestType) == util.CTRL_OUT: a = _interop.as_array(data_or_wLength) elif data_or_wLength is None: a = 0 else: a = data_or_wLength self._ctx.managed_open() # Thanks to Johannes Stezenbach to point me out that we need to # claim the recipient interface recipient = bmRequestType & 3 if recipient == util.CTRL_RECIPIENT_INTERFACE: interface_number = wIndex & 0xff self._ctx.managed_claim_interface(self, interface_number) return self._ctx.backend.ctrl_transfer(self._ctx.handle, bmRequestType, bRequest, wValue, wIndex, a, self.__get_timeout(timeout))
def to_array(data): return _interop.as_array(data)
def get_array_data2(length=10): data = list(range(length)) data.reverse() return _interop.as_array(data)
def get_array_data1(length=10): return _interop.as_array(range(length))
def data_len(data): a = _interop.as_array(data) return len(data) * a.itemsize
def iso_read(self, dev_handle, ep, intf, size, timeout): data = _interop.as_array('\x00' * size) handler = _IsoTransferHandler(dev_handle, ep, data, timeout) return data[:handler.submit(self.ctx)]
def _10bits_addr_to_bytes(self, addr): byte_h = 0x78 | (addr >> 8) byte_l = addr & 0xFF return _interop.as_array([byte_h, byte_l])
def test_byte_tuple_as_array(self): self.assertEqual(as_array((10, 20, 30)), array('B', [10, 20, 30]))
def test_none_as_array(self): self.assertEqual(as_array(None), array('B'))
def test_byte_array_as_array(self): data = array('B', [10, 20, 30]) self.assertEqual(as_array(data), data) self.assertIs(as_array(data), data)
def get_array_data1(length=8): return _interop.as_array(range(length))
def test_unicode_string_as_array(self): self.assertEqual(as_array('Πύ'), array('B', b'\xce\xa0\xcf\x8d'))
def test_bytes_as_array(self): self.assertEqual(as_array(b'\x10\x20\x30'), array('B', [16, 32, 48]))
def get_array_data2(length=8): data = list(range(length)) data.reverse() return _interop.as_array(data)
def test_byte_list_as_array(self): self.assertEqual(as_array([10, 20, 30]), array('B', [10, 20, 30]))