def usb_callback(transfer): if self.usb_poller.done: return # shutting down if transfer.isSubmitted(): return # transfer not completed status = transfer.getStatus() if status == usb1.TRANSFER_CANCELLED: usb_transfer_type = transfer.getType() if usb_transfer_type == usb1.TRANSFER_TYPE_CONTROL: transfer_type = "CONTROL" if usb_transfer_type == usb1.TRANSFER_TYPE_BULK: transfer_type = "BULK" endpoint = transfer.getEndpoint() if endpoint & usb1.ENDPOINT_DIR_MASK == usb1.ENDPOINT_IN: endpoint_dir = "IN" if endpoint & usb1.ENDPOINT_DIR_MASK == usb1.ENDPOINT_OUT: endpoint_dir = "OUT" logger.trace("USB: %s EP%d %s (cancelled)", transfer_type, endpoint & 0x7f, endpoint_dir) cancel_future.set_result(None) elif result_future.cancelled(): pass elif status == usb1.TRANSFER_COMPLETED: if is_read: result_future.set_result(transfer.getBuffer()[:transfer.getActualLength()]) else: result_future.set_result(None) elif status == usb1.TRANSFER_STALL: result_future.set_exception(usb1.USBErrorPipe()) elif status == usb1.TRANSFER_NO_DEVICE: result_future.set_exception(GlasgowDeviceError("device lost")) else: result_future.set_exception(GlasgowDeviceError( "transfer error: {}".format(usb1.libusb1.libusb_transfer_status(status))))
def usb_callback(transfer): if transfer.isSubmitted(): return # transfer not completed if future.cancelled(): return # future cancelled status = transfer.getStatus() if status == usb1.TRANSFER_COMPLETED: if is_read: future.set_result(transfer.getBuffer()[:transfer.getActualLength()]) else: future.set_result(None) elif status == usb1.TRANSFER_CANCELLED: future.cancel() elif status == usb1.TRANSFER_STALL: future.set_exception(usb1.USBErrorPipe()) elif status == usb1.TRANSFER_NO_DEVICE: future.set_exception(GlasgowDeviceError("device lost")) else: future.set_exception(GlasgowDeviceError("transfer error: {}".format(status)))