From 642996bfe6902640445ae30d3c49aead06929d07 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sun, 14 Nov 2010 22:19:58 +0100 Subject: [PATCH] Poll the serial line to avoid the use of an infinite timeout. Because the user needs to initiate the transfer on the device itself, we have to wait for an unknown amount of time. The infinite timeout works, but causes problems if the data never arrives. By polling the serial line, an application can at least cancel the operation. --- src/mares_nemo.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/mares_nemo.c b/src/mares_nemo.c index 2fcef93..165e4e7 100644 --- a/src/mares_nemo.c +++ b/src/mares_nemo.c @@ -122,7 +122,7 @@ mares_nemo_device_open (device_t **out, const char* name) } // Set the timeout for receiving data (1000 ms). - if (serial_set_timeout (device->port, -1) == -1) { + if (serial_set_timeout (device->port, 1000) == -1) { WARNING ("Failed to set the timeout."); serial_close (device->port); free (device); @@ -138,6 +138,9 @@ mares_nemo_device_open (device_t **out, const char* name) return DEVICE_STATUS_IO; } + // Make sure everything is in a sane state. + serial_flush (device->port, SERIAL_QUEUE_BOTH); + *out = (device_t*) device; return DEVICE_STATUS_SUCCESS; @@ -185,6 +188,15 @@ mares_nemo_device_dump (device_t *abstract, dc_buffer_t *buffer) progress.maximum = MEMORYSIZE + 20; device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); + // Wait until some data arrives. + while (serial_get_received (device->port) == 0) { + if (device_is_cancelled (abstract)) + return DEVICE_STATUS_CANCELLED; + + device_event_emit (abstract, DEVICE_EVENT_WAITING, NULL); + serial_sleep (100); + } + // Receive the header of the package. unsigned char header = 0x00; for (unsigned int i = 0; i < 20;) {