From a87398b7c6bc318a623eb50886ea34d407178902 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 12 Nov 2009 08:04:34 +0000 Subject: [PATCH] Implement progress events for the device_dump() function. A helper function is added to simplify implementing the devic_dump() function on top of the device_read() function, and enable progress events automatically. --- src/device-private.h | 3 +++ src/device.c | 37 +++++++++++++++++++++++++++++++++++++ src/mares_puck.c | 8 ++------ src/oceanic_atom2.c | 8 ++------ src/oceanic_veo250.c | 8 ++------ src/oceanic_vtpro.c | 8 ++------ src/suunto_common2.c | 13 ++----------- src/suunto_vyper.c | 13 ++----------- 8 files changed, 52 insertions(+), 46 deletions(-) diff --git a/src/device-private.h b/src/device-private.h index 9e4aedf..662b46c 100644 --- a/src/device-private.h +++ b/src/device-private.h @@ -69,6 +69,9 @@ device_init (device_t *device, const device_backend_t *backend); void device_event_emit (device_t *device, device_event_t event, const void *data); +device_status_t +device_dump_read (device_t *device, unsigned char data[], unsigned int size, unsigned int blocksize); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/src/device.c b/src/device.c index 4439162..ff12d65 100644 --- a/src/device.c +++ b/src/device.c @@ -125,6 +125,43 @@ device_dump (device_t *device, dc_buffer_t *buffer) } +device_status_t +device_dump_read (device_t *device, unsigned char data[], unsigned int size, unsigned int blocksize) +{ + if (device == NULL) + return DEVICE_STATUS_UNSUPPORTED; + + if (device->backend->read == NULL) + return DEVICE_STATUS_UNSUPPORTED; + + // Enable progress notifications. + device_progress_t progress = DEVICE_PROGRESS_INITIALIZER; + progress.maximum = size; + device_event_emit (device, DEVICE_EVENT_PROGRESS, &progress); + + unsigned int nbytes = 0; + while (nbytes < size) { + // Calculate the packet size. + unsigned int len = size - nbytes; + if (len > blocksize) + len = blocksize; + + // Read the packet. + device_status_t rc = device->backend->read (device, nbytes, data + nbytes, len); + if (rc != DEVICE_STATUS_SUCCESS) + return rc; + + // Update and emit a progress event. + progress.current += len; + device_event_emit (device, DEVICE_EVENT_PROGRESS, &progress); + + nbytes += len; + } + + return DEVICE_STATUS_SUCCESS; +} + + device_status_t device_foreach (device_t *device, dive_callback_t callback, void *userdata) { diff --git a/src/mares_puck.c b/src/mares_puck.c index da7747a..5b1efb5 100644 --- a/src/mares_puck.c +++ b/src/mares_puck.c @@ -351,12 +351,8 @@ mares_puck_device_dump (device_t *abstract, dc_buffer_t *buffer) return DEVICE_STATUS_MEMORY; } - device_status_t rc = mares_puck_device_read (abstract, 0x00, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); - if (rc != DEVICE_STATUS_SUCCESS) - return rc; - - return DEVICE_STATUS_SUCCESS; + return device_dump_read (abstract, dc_buffer_get_data (buffer), + dc_buffer_get_size (buffer), MARES_PUCK_PACKET_SIZE); } diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index cbcdbdc..4b2a4e6 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -481,12 +481,8 @@ oceanic_atom2_device_dump (device_t *abstract, dc_buffer_t *buffer) return DEVICE_STATUS_MEMORY; } - device_status_t rc = oceanic_atom2_device_read (abstract, 0x00, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); - if (rc != DEVICE_STATUS_SUCCESS) - return rc; - - return DEVICE_STATUS_SUCCESS; + return device_dump_read (abstract, dc_buffer_get_data (buffer), + dc_buffer_get_size (buffer), OCEANIC_ATOM2_PACKET_SIZE); } diff --git a/src/oceanic_veo250.c b/src/oceanic_veo250.c index c800471..58c0150 100644 --- a/src/oceanic_veo250.c +++ b/src/oceanic_veo250.c @@ -457,12 +457,8 @@ oceanic_veo250_device_dump (device_t *abstract, dc_buffer_t *buffer) return DEVICE_STATUS_MEMORY; } - device_status_t rc = oceanic_veo250_device_read (abstract, 0x00, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); - if (rc != DEVICE_STATUS_SUCCESS) - return rc; - - return DEVICE_STATUS_SUCCESS; + return device_dump_read (abstract, dc_buffer_get_data (buffer), + dc_buffer_get_size (buffer), OCEANIC_VEO250_PACKET_SIZE); } diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c index 4727e67..095a284 100644 --- a/src/oceanic_vtpro.c +++ b/src/oceanic_vtpro.c @@ -533,12 +533,8 @@ oceanic_vtpro_device_dump (device_t *abstract, dc_buffer_t *buffer) return DEVICE_STATUS_MEMORY; } - device_status_t rc = oceanic_vtpro_device_read (abstract, 0x00, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer)); - if (rc != DEVICE_STATUS_SUCCESS) - return rc; - - return DEVICE_STATUS_SUCCESS; + return device_dump_read (abstract, dc_buffer_get_data (buffer), + dc_buffer_get_size (buffer), OCEANIC_VTPRO_PACKET_SIZE); } diff --git a/src/suunto_common2.c b/src/suunto_common2.c index 5afe6a3..e0f0a07 100644 --- a/src/suunto_common2.c +++ b/src/suunto_common2.c @@ -230,17 +230,8 @@ suunto_common2_device_dump (device_t *abstract, dc_buffer_t *buffer) return DEVICE_STATUS_MEMORY; } - // Enable progress notifications. - device_progress_t progress = DEVICE_PROGRESS_INITIALIZER; - progress.maximum = SZ_MEMORY; - device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); - - device_status_t rc = suunto_common2_read (abstract, 0x00, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), &progress); - if (rc != DEVICE_STATUS_SUCCESS) - return rc; - - return DEVICE_STATUS_SUCCESS; + return device_dump_read (abstract, dc_buffer_get_data (buffer), + dc_buffer_get_size (buffer), SZ_PACKET); } diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index c187911..4bc9503 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -542,17 +542,8 @@ suunto_vyper_device_dump (device_t *abstract, dc_buffer_t *buffer) return DEVICE_STATUS_MEMORY; } - // Enable progress notifications. - device_progress_t progress = DEVICE_PROGRESS_INITIALIZER; - progress.maximum = SUUNTO_VYPER_MEMORY_SIZE; - device_event_emit (abstract, DEVICE_EVENT_PROGRESS, &progress); - - device_status_t rc = suunto_vyper_read (abstract, 0x00, - dc_buffer_get_data (buffer), dc_buffer_get_size (buffer), &progress); - if (rc != DEVICE_STATUS_SUCCESS) - return rc; - - return DEVICE_STATUS_SUCCESS; + return device_dump_read (abstract, dc_buffer_get_data (buffer), + dc_buffer_get_size (buffer), SUUNTO_VYPER_PACKET_SIZE); }