From a47809fda80200bc054fd30011b41fded2c9994c Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Mon, 13 Feb 2017 02:50:47 +0100 Subject: [PATCH] Simplify the C part even more - the buffer that xfer uses for transferring data is the same as the one that was created in newUSBTransfer. --- usb/transfer.c | 53 ++++++++++++------------------------------------- usb/transfer.go | 14 ++++++------- 2 files changed, 20 insertions(+), 47 deletions(-) diff --git a/usb/transfer.c b/usb/transfer.c index 14585e6..2fa3497 100644 --- a/usb/transfer.c +++ b/usb/transfer.c @@ -54,54 +54,27 @@ void print_xfer(struct libusb_transfer *xfer) { } } -// extract data from a non-isochronous transfer. -int extract_data(struct libusb_transfer *xfer, void *raw, int max, unsigned char *status) { +// compact the data in an isochronous transfer. The contents of individual +// iso packets are shifted left, so that no gaps are left between them. +// Status is set to the first non-zero status of an iso packet. +int compact_iso_data(struct libusb_transfer *xfer, unsigned char *status) { int i; + int sum = 0; unsigned char *in = xfer->buffer; - unsigned char *out = raw; - - int len = xfer->actual_length; - if (len > max) { - len = max; - } - memcpy(out, in, len); - if (xfer->status != 0 && *status == 0) { - *status = xfer->status; - } - return len; -} - -// extract data from an isochronous transfer. Very similar to extract_data, but -// acquires data from xfer->iso_packet_desc array instead of xfer attributes. -int extract_iso_data(struct libusb_transfer *xfer, void *raw, int max, unsigned char *status) { - int i; - int copied = 0; - unsigned char *in = xfer->buffer; - unsigned char *out = raw; + unsigned char *out = xfer->buffer; for (i = 0; i < xfer->num_iso_packets; i++) { struct libusb_iso_packet_descriptor pkt = xfer->iso_packet_desc[i]; - + if (pkt.status != 0) { + *status = pkt.status; + break; + } // Copy the data int len = pkt.actual_length; - if (copied + len > max) { - len = max - copied; - } - memcpy(out, in, len); - copied += len; - + memmove(out, in, len); // Increment offsets + sum += len; in += pkt.length; out += len; - - if (copied == max) { - break; - } - - // Extract first error - if (pkt.status == 0 || *status != 0) { - continue; - } - *status = pkt.status; } - return copied; + return sum; } diff --git a/usb/transfer.go b/usb/transfer.go index 0e22080..f5c9129 100644 --- a/usb/transfer.go +++ b/usb/transfer.go @@ -17,8 +17,7 @@ package usb /* #include -int extract_data(struct libusb_transfer *xfer, void *data, int max, unsigned char *status); -int extract_iso_data(struct libusb_transfer *xfer, void *data, int max, unsigned char *status); +int compact_iso_data(struct libusb_transfer *xfer, unsigned char *status); int submit(struct libusb_transfer *xfer); */ import "C" @@ -56,15 +55,16 @@ func (t *usbTransfer) wait() (n int, err error) { return 0, fmt.Errorf("wait timed out after 10s") case <-t.done: } - var status uint8 + var status TransferStatus switch TransferType(t.xfer._type) { case TRANSFER_TYPE_ISOCHRONOUS: - n = int(C.extract_iso_data(t.xfer, unsafe.Pointer(&t.buf[0]), C.int(len(t.buf)), (*C.uchar)(unsafe.Pointer(&status)))) + n = int(C.compact_iso_data(t.xfer, (*C.uchar)(unsafe.Pointer(&status)))) default: - n = int(C.extract_data(t.xfer, unsafe.Pointer(&t.buf[0]), C.int(len(t.buf)), (*C.uchar)(unsafe.Pointer(&status)))) + n = int(t.xfer.length) + status = TransferStatus(t.xfer.status) } - if status != 0 { - err = TransferStatus(status) + if status != LIBUSB_TRANSFER_COMPLETED { + return 0, status } return n, err }