From 58d0e0a62a61b23c3a29a807d5ddbcda76cd2821 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 11 Aug 2012 23:08:24 +0200 Subject: [PATCH] Fix the libusb timeout handling. A bulk transfer of more than 8K of data takes about one second with the Cobalt. Because we use a one second timeout combined with a 10K buffer, such a transfer can easily exceed the timeout. Normally this shouldn't be a problem because the leftover data is supposed to be received with the next transfer. However to break out of the loop we check the actual number of bytes received, and ignore the libusb LIBUSB_ERROR_TIMEOUT return code. To fix this problem, the internal buffer is reduced to 8K, and the timeout is increased to 2 seconds. This should avoid hitting the timeout and allows to consider LIBUSB_ERROR_TIMEOUT a fatal error. --- src/atomics_cobalt.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/atomics_cobalt.c b/src/atomics_cobalt.c index e7f1cde..ee949e6 100644 --- a/src/atomics_cobalt.c +++ b/src/atomics_cobalt.c @@ -36,9 +36,11 @@ #include "utils.h" #include "array.h" +#define EXITCODE(rc) (rc == LIBUSB_ERROR_TIMEOUT ? DEVICE_STATUS_TIMEOUT : DEVICE_STATUS_IO) + #define VID 0x0471 #define PID 0x0888 -#define TIMEOUT 1000 +#define TIMEOUT 2000 #define FP_OFFSET 20 @@ -218,7 +220,7 @@ atomics_cobalt_device_version (device_t *abstract, unsigned char data[], unsigne bRequest, 0, 0, NULL, 0, TIMEOUT); if (rc != LIBUSB_SUCCESS) { WARNING ("Failed to send the command."); - return DEVICE_STATUS_IO; + return EXITCODE(rc); } // Receive the answer from the dive computer. @@ -228,7 +230,7 @@ atomics_cobalt_device_version (device_t *abstract, unsigned char data[], unsigne packet, sizeof (packet), &length, TIMEOUT); if (rc != LIBUSB_SUCCESS || length != sizeof (packet)) { WARNING ("Failed to receive the answer."); - return DEVICE_STATUS_IO; + return EXITCODE(rc); } // Verify the checksum of the packet. @@ -274,19 +276,19 @@ atomics_cobalt_read_dive (device_t *abstract, dc_buffer_t *buffer, int init, dev bRequest, 0, 0, NULL, 0, TIMEOUT); if (rc != LIBUSB_SUCCESS) { WARNING ("Failed to send the command."); - return DEVICE_STATUS_IO; + return EXITCODE(rc); } unsigned int nbytes = 0; while (1) { // Receive the answer from the dive computer. int length = 0; - unsigned char packet[10 * 1024] = {0}; + unsigned char packet[8 * 1024] = {0}; rc = libusb_bulk_transfer (device->handle, 0x82, packet, sizeof (packet), &length, TIMEOUT); - if (rc != LIBUSB_SUCCESS && rc != LIBUSB_ERROR_TIMEOUT) { + if (rc != LIBUSB_SUCCESS) { WARNING ("Failed to receive the answer."); - return DEVICE_STATUS_IO; + return EXITCODE(rc); } // Update and emit a progress event.