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.
This commit is contained in:
@@ -54,54 +54,27 @@ void print_xfer(struct libusb_transfer *xfer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract data from a non-isochronous transfer.
|
// compact the data in an isochronous transfer. The contents of individual
|
||||||
int extract_data(struct libusb_transfer *xfer, void *raw, int max, unsigned char *status) {
|
// 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 i;
|
||||||
|
int sum = 0;
|
||||||
unsigned char *in = xfer->buffer;
|
unsigned char *in = xfer->buffer;
|
||||||
unsigned char *out = raw;
|
unsigned char *out = xfer->buffer;
|
||||||
|
|
||||||
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;
|
|
||||||
for (i = 0; i < xfer->num_iso_packets; i++) {
|
for (i = 0; i < xfer->num_iso_packets; i++) {
|
||||||
struct libusb_iso_packet_descriptor pkt = xfer->iso_packet_desc[i];
|
struct libusb_iso_packet_descriptor pkt = xfer->iso_packet_desc[i];
|
||||||
|
if (pkt.status != 0) {
|
||||||
|
*status = pkt.status;
|
||||||
|
break;
|
||||||
|
}
|
||||||
// Copy the data
|
// Copy the data
|
||||||
int len = pkt.actual_length;
|
int len = pkt.actual_length;
|
||||||
if (copied + len > max) {
|
memmove(out, in, len);
|
||||||
len = max - copied;
|
|
||||||
}
|
|
||||||
memcpy(out, in, len);
|
|
||||||
copied += len;
|
|
||||||
|
|
||||||
// Increment offsets
|
// Increment offsets
|
||||||
|
sum += len;
|
||||||
in += pkt.length;
|
in += pkt.length;
|
||||||
out += len;
|
out += len;
|
||||||
|
|
||||||
if (copied == max) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract first error
|
|
||||||
if (pkt.status == 0 || *status != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
*status = pkt.status;
|
|
||||||
}
|
}
|
||||||
return copied;
|
return sum;
|
||||||
}
|
}
|
||||||
|
@@ -17,8 +17,7 @@ package usb
|
|||||||
/*
|
/*
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
int extract_data(struct libusb_transfer *xfer, void *data, int max, unsigned char *status);
|
int compact_iso_data(struct libusb_transfer *xfer, unsigned char *status);
|
||||||
int extract_iso_data(struct libusb_transfer *xfer, void *data, int max, unsigned char *status);
|
|
||||||
int submit(struct libusb_transfer *xfer);
|
int submit(struct libusb_transfer *xfer);
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
@@ -56,15 +55,16 @@ func (t *usbTransfer) wait() (n int, err error) {
|
|||||||
return 0, fmt.Errorf("wait timed out after 10s")
|
return 0, fmt.Errorf("wait timed out after 10s")
|
||||||
case <-t.done:
|
case <-t.done:
|
||||||
}
|
}
|
||||||
var status uint8
|
var status TransferStatus
|
||||||
switch TransferType(t.xfer._type) {
|
switch TransferType(t.xfer._type) {
|
||||||
case TRANSFER_TYPE_ISOCHRONOUS:
|
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:
|
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 {
|
if status != LIBUSB_TRANSFER_COMPLETED {
|
||||||
err = TransferStatus(status)
|
return 0, status
|
||||||
}
|
}
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user