Skip to content

Commit

Permalink
retry usb read operations after timeout with 0 bytes transmitted
Browse files Browse the repository at this point in the history
  • Loading branch information
jboekesteijn committed Jun 29, 2017
1 parent 666a72c commit 1beb0f4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ccid_usb.c
Expand Up @@ -814,13 +814,17 @@ status_t WriteUSB(unsigned int reader_index, unsigned int length,
status_t ReadUSB(unsigned int reader_index, unsigned int * length,
unsigned char *buffer)
{
#define READUSB_MAX_ATTEMPTS 3
int attempt = 0;
int rv;
int actual_length;
char debug_header[] = "<- 121234 ";
_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
int duplicate_frame = 0;

read_again:
actual_length = -1;

(void)snprintf(debug_header, sizeof(debug_header), "<- %06X ",
(int)reader_index);

Expand All @@ -830,6 +834,19 @@ status_t ReadUSB(unsigned int reader_index, unsigned int * length,

if (rv < 0)
{
if (LIBUSB_ERROR_TIMEOUT == rv && actual_length == 0)
{
/* zero-length packet, drop it and try again
* not sure why this is reported as timeout and not as success (http://libusb.org/static/api-1.0/group__syncio.html#gab8ae853ab492c22d707241dc26c8a805)
*/
attempt++;
DEBUG_INFO5("read zero-length (%d/%d): attempt %d of %d",
usbDevice[reader_index].bus_number,
usbDevice[reader_index].device_address, attempt, READUSB_MAX_ATTEMPTS);
if (READUSB_MAX_ATTEMPTS != attempt)
goto read_again;
}

*length = 0;
DEBUG_CRITICAL5("read failed (%d/%d): %d %s",
usbDevice[reader_index].bus_number,
Expand All @@ -856,6 +873,7 @@ status_t ReadUSB(unsigned int reader_index, unsigned int * length,
return STATUS_UNSUCCESSFUL;
}
DEBUG_INFO1("Duplicate frame detected");
attempt = 0;
goto read_again;
}

Expand Down

0 comments on commit 1beb0f4

Please sign in to comment.