Commit 8aea8e63 authored by Fabio Utzig's avatar Fabio Utzig
Browse files

Fix issues with usb packet fragmentation

On some machines, some usb responses would be broken
into more that two bulk_requests. Now the reception
of the whole response in handled in a single call to
wait_response.
parent 9a9ebb1e
...@@ -156,15 +156,33 @@ static int send_command(libusb_device_handle *handle, int size) ...@@ -156,15 +156,33 @@ static int send_command(libusb_device_handle *handle, int size)
return retval; return retval;
} }
static int wait_response(libusb_device_handle *handle, int *size) static int wait_response(libusb_device_handle *handle, int *has_ack, int *size)
{ {
int retval; int retval;
int transferred = 0;
*has_ack = 0;
*size = 0;
retval = libusb_bulk_transfer(handle, ENDPOINT_IN, buf.u8, BUF_SIZE, size, 0); do {
retval = libusb_bulk_transfer(handle,
ENDPOINT_IN,
&buf.u8[*size],
BUF_SIZE - *size,
&transferred,
0);
if (retval != 0) { if (retval != 0) {
printf("Error receiving data %d\n", retval); printf("Error receiving data %d\n", retval);
return retval;
} }
if (transferred >= 1 && buf.c[0] == '+')
*has_ack = 1;
*size += transferred;
} while ((*size < 3) || (buf.c[*size - 3] != '#'));
#ifdef DEBUG #ifdef DEBUG
printf("<<< received %d bytes\n", *size); printf("<<< received %d bytes\n", *size);
pretty_print_buf(buf.u8, *size); pretty_print_buf(buf.u8, *size);
...@@ -178,6 +196,7 @@ static int checksum_and_send(libusb_device_handle *handle, size_t idx, int *xfer ...@@ -178,6 +196,7 @@ static int checksum_and_send(libusb_device_handle *handle, size_t idx, int *xfer
size_t i; size_t i;
uint8_t sum = 0; uint8_t sum = 0;
int retval, transfered; int retval, transfered;
int has_ack;
if (idx + SNPRINTF_OFFSET + END_LEN > BUF_SIZE) if (idx + SNPRINTF_OFFSET + END_LEN > BUF_SIZE)
return LIBUSB_ERROR_NO_MEM; return LIBUSB_ERROR_NO_MEM;
...@@ -191,16 +210,13 @@ static int checksum_and_send(libusb_device_handle *handle, size_t idx, int *xfer ...@@ -191,16 +210,13 @@ static int checksum_and_send(libusb_device_handle *handle, size_t idx, int *xfer
if (retval) if (retval)
return retval; return retval;
/* wait for ack (+/-) */ retval = wait_response(handle, &has_ack, &transfered);
retval = wait_response(handle, &transfered);
if (retval) if (retval)
return retval; return retval;
if (transfered != 1 || buf.c[0] != '+') if (!has_ack)
return LIBUSB_ERROR_OTHER; return LIBUSB_ERROR_OTHER;
/* wait for command response */
retval = wait_response(handle, &transfered);
if (xfer) if (xfer)
*xfer = transfered; *xfer = transfered;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment