Files
gousb/usb/iso.c
Sebastian Zagrodzki a35725f4fb When extracting transfer data, pay attention to the size of the buffer
provided by the user, which may be smaller than the length of data
received in the transfer.
2017-02-08 22:37:12 +01:00

99 lines
2.7 KiB
C

// Copyright 2013 Google Inc. All rights reserved.
// Copyright 2016 the gousb Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <libusb-1.0/libusb.h>
#include <stdio.h>
#include <string.h>
void print_xfer(struct libusb_transfer *xfer);
void iso_callback(void *);
void callback(struct libusb_transfer *xfer) {
//printf("Callback!\n");
//print_xfer(xfer);
iso_callback(xfer->user_data);
}
int submit(struct libusb_transfer *xfer) {
xfer->callback = &callback;
xfer->status = -1;
//print_xfer(xfer);
//printf("Transfer submitted\n");
/* fake
strcpy(xfer->buffer, "hello");
xfer->actual_length = 5;
callback(xfer);
return 0; */
return libusb_submit_transfer(xfer);
}
void print_xfer(struct libusb_transfer *xfer) {
int i;
printf("Transfer:\n");
printf(" dev_handle: %p\n", xfer->dev_handle);
printf(" flags: %08x\n", xfer->flags);
printf(" endpoint: %x\n", xfer->endpoint);
printf(" type: %x\n", xfer->type);
printf(" timeout: %dms\n", xfer->timeout);
printf(" status: %x\n", xfer->status);
printf(" length: %d (act: %d)\n", xfer->length, xfer->actual_length);
printf(" callback: %p\n", xfer->callback);
printf(" user_data: %p\n", xfer->user_data);
printf(" buffer: %p\n", xfer->buffer);
printf(" num_iso_pkts: %d\n", xfer->num_iso_packets);
printf(" packets:\n");
for (i = 0; i < xfer->num_iso_packets; i++) {
printf(" [%04d] %d (act: %d) %x\n", i,
xfer->iso_packet_desc[i].length,
xfer->iso_packet_desc[i].actual_length,
xfer->iso_packet_desc[i].status);
}
}
int extract_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++) {
struct libusb_iso_packet_descriptor pkt = xfer->iso_packet_desc[i];
// Copy the data
int len = pkt.actual_length;
if (copied + len > max) {
len = max - copied;
}
memcpy(out, in, len);
copied += len;
// Increment offsets
in += pkt.length;
out += len;
if (copied == max) {
break;
}
// Extract first error
if (pkt.status == 0 || *status != 0) {
continue;
}
*status = pkt.status;
}
return copied;
}