From 040679f76aaedc8e5272455e4b94dad5ab1b2a54 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 22 Nov 2013 09:30:46 +0100 Subject: [PATCH] Improve the progress events. Reading the data packet in multiple smaller chunks greatly improves the progress events. Instead of just two events, before and after the download, there are now many intermediate events. This change also allows to significantly reduce the timeout, from 30 seconds to just one second, which avoids blocking for too long in case the device doesn't respond at all. --- src/suunto_eon.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 5cb2d51..de7d2aa 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -104,8 +104,8 @@ suunto_eon_device_open (dc_device_t **out, dc_context_t *context, const char *na return DC_STATUS_IO; } - // Set the timeout for receiving data (30s). - if (serial_set_timeout (device->port, 30000) == -1) { + // Set the timeout for receiving data (1000ms). + if (serial_set_timeout (device->port, 1000) == -1) { ERROR (context, "Failed to set the timeout."); serial_close (device->port); free (device); @@ -170,16 +170,34 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) } // Receive the answer. + unsigned int nbytes = 0; unsigned char answer[SZ_MEMORY + 1] = {0}; - rc = serial_read (device->port, answer, sizeof (answer)); - if (rc != sizeof (answer)) { - ERROR (abstract->context, "Failed to receive the answer."); - return EXITCODE (rc); - } + while (nbytes < sizeof(answer)) { + // Set the minimum packet size. + unsigned int len = 64; - // Update and emit a progress event. - progress.current += sizeof (answer); - device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + // Increase the packet size if more data is immediately available. + int available = serial_get_received (device->port); + if (available > len) + len = available; + + // Limit the packet size to the total size. + if (nbytes + len > sizeof(answer)) + len = sizeof(answer) - nbytes; + + // Read the packet. + int n = serial_read (device->port, answer + nbytes, len); + if (n != len) { + ERROR (abstract->context, "Failed to receive the answer."); + return EXITCODE (n); + } + + // Update and emit a progress event. + progress.current += len; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); + + nbytes += len; + } // Verify the checksum of the package. unsigned char crc = answer[sizeof (answer) - 1];