From f42df2d846bc6d079e4ecebebe8ae0c9dc287e01 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 9 Feb 2021 20:57:24 +0100 Subject: [PATCH] Remove the infinite timeout When an Uwatec Aladin is connected, but the transfer hasn't been started yet, we receive a continuous stream of zero bytes. Approximately every 7-8ms a new zero byte is received. But when the dive computer is (temporary) disconnected, the stream of zero bytes also ends. The consequence is that due to the use of blocking read call with an infinite timeout, the application becomes unresponsive, without any chance to abort the communication. This can eaily be avoided by using a timeout instead. Receiving the main 2048 byte packet takes about 1050ms. Thus a 3000ms timeout should be long enough to not cause the main data transfer to timeout, but still short enough to cancel reasonable fast. --- src/uwatec_aladin.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 15bcbfd..1cfec54 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -96,8 +96,8 @@ uwatec_aladin_device_open (dc_device_t **out, dc_context_t *context, dc_iostream goto error_free; } - // Set the timeout for receiving data (INFINITE). - status = dc_iostream_set_timeout (device->iostream, -1); + // Set the timeout for receiving data (3000ms). + status = dc_iostream_set_timeout (device->iostream, 3000); if (status != DC_STATUS_SUCCESS) { ERROR (context, "Failed to set the timeout."); goto error_free; @@ -172,11 +172,12 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) status = dc_iostream_read (device->iostream, answer + i, 1, NULL); if (status != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to receive the answer."); - return status; + if (status != DC_STATUS_TIMEOUT) + return status; } const unsigned char expected = i < 3 ? 0x55 : 0x00; - if (answer[i] != expected) { + if (status != DC_STATUS_SUCCESS || answer[i] != expected) { device_event_emit (abstract, DC_EVENT_WAITING, NULL); i = 0; } else {