From f467f4fd8d4f47b89447f0ec148d12ac40575aff Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 14 Aug 2008 10:30:33 +0000 Subject: [PATCH] Automatically abort when a dive is older than the provided timestamp. --- src/reefnet_sensuspro.c | 37 ++++++++++++++++++++++++++++++++----- src/reefnet_sensuspro.h | 5 ++++- src/reefnet_sensusultra.c | 30 ++++++++++++++++++++++++++---- src/reefnet_sensusultra.h | 5 ++++- src/uwatec_aladin.c | 31 +++++++++++++++++++++++++++++-- src/uwatec_aladin.h | 5 ++++- 6 files changed, 99 insertions(+), 14 deletions(-) diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index a3086ce..0d1d2ba 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -23,6 +23,7 @@ typedef struct reefnet_sensuspro_device_t reefnet_sensuspro_device_t; struct reefnet_sensuspro_device_t { device_t base; struct serial *port; + unsigned int timestamp; }; static const device_backend_t reefnet_sensuspro_device_backend; @@ -55,6 +56,7 @@ reefnet_sensuspro_device_open (device_t **out, const char* name) // Set the default values. device->port = NULL; + device->timestamp = 0; // Open the device. int rc = serial_open (&device->port, name); @@ -111,6 +113,20 @@ reefnet_sensuspro_device_close (device_t *abstract) } +device_status_t +reefnet_sensuspro_device_set_timestamp (device_t *abstract, unsigned int timestamp) +{ + reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; + + if (! device_is_reefnet_sensuspro (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + device->timestamp = timestamp; + + return DEVICE_STATUS_SUCCESS; +} + + static device_status_t reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, unsigned int size) { @@ -224,13 +240,18 @@ reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned static device_status_t reefnet_sensuspro_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata) { + reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; + + if (! device_is_reefnet_sensuspro (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + unsigned char data[REEFNET_SENSUSPRO_MEMORY_SIZE] = {0}; int rc = reefnet_sensuspro_device_dump (abstract, data, sizeof (data)); if (rc < 0) return rc; - return reefnet_sensuspro_extract_dives (data, sizeof (data), callback, userdata); + return reefnet_sensuspro_extract_dives (data, sizeof (data), callback, userdata, device->timestamp); } @@ -265,7 +286,7 @@ reefnet_sensuspro_device_write_interval (device_t *abstract, unsigned char inter device_status_t -reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) +reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata, unsigned int timestamp) { const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; const unsigned char footer[2] = {0xFF, 0xFF}; @@ -283,9 +304,6 @@ reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, unsigned int offset = current + 10; // Skip non-sample data. while (offset + 2 <= previous) { if (memcmp (data + offset, footer, sizeof (footer)) == 0) { - if (callback && !callback (data + current, offset + 2 - current, userdata)) - return DEVICE_STATUS_SUCCESS; - found = 1; break; } else { @@ -297,6 +315,15 @@ reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, if (!found) return DEVICE_STATUS_ERROR; + // Automatically abort when a dive is older than the provided timestamp. + unsigned int datetime = data[current + 6] + (data[current + 7] << 8) + + (data[current + 8] << 16) + (data[current + 9] << 24); + if (datetime <= timestamp) + return DEVICE_STATUS_SUCCESS; + + if (callback && !callback (data + current, offset + 2 - current, userdata)) + return DEVICE_STATUS_SUCCESS; + // Prepare for the next dive. previous = current; current = (current >= 4 ? current - 4 : 0); diff --git a/src/reefnet_sensuspro.h b/src/reefnet_sensuspro.h index e21db94..1d4b952 100644 --- a/src/reefnet_sensuspro.h +++ b/src/reefnet_sensuspro.h @@ -13,11 +13,14 @@ extern "C" { device_status_t reefnet_sensuspro_device_open (device_t **device, const char* name); +device_status_t +reefnet_sensuspro_device_set_timestamp (device_t *device, unsigned int timestamp); + device_status_t reefnet_sensuspro_device_write_interval (device_t *device, unsigned char interval); device_status_t -reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata); +reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata, unsigned int timestamp); #ifdef __cplusplus } diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index db766c0..319f792 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -28,6 +28,7 @@ struct reefnet_sensusultra_device_t { device_t base; struct serial *port; unsigned int maxretries; + unsigned int timestamp; }; static const device_backend_t reefnet_sensusultra_device_backend; @@ -61,6 +62,7 @@ reefnet_sensusultra_device_open (device_t **out, const char* name) // Set the default values. device->port = NULL; device->maxretries = 2; + device->timestamp = 0; // Open the device. int rc = serial_open (&device->port, name); @@ -131,6 +133,20 @@ reefnet_sensusultra_device_set_maxretries (device_t *abstract, unsigned int maxr } +device_status_t +reefnet_sensusultra_device_set_timestamp (device_t *abstract, unsigned int timestamp) +{ + reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; + + if (! device_is_reefnet_sensusultra (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + device->timestamp = timestamp; + + return DEVICE_STATUS_SUCCESS; +} + + static int reefnet_sensusultra_isempty (const unsigned char *data, unsigned int size) { @@ -527,7 +543,7 @@ reefnet_sensusultra_device_sense (device_t *abstract, unsigned char *data, unsig static device_status_t reefnet_sensusultra_parse (const unsigned char data[], unsigned int begin, unsigned int end, unsigned int *pprevious, - dive_callback_t callback, void *userdata) + dive_callback_t callback, void *userdata, unsigned int timestamp) { const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF}; @@ -562,6 +578,12 @@ reefnet_sensusultra_parse (const unsigned char data[], unsigned int begin, unsig return DEVICE_STATUS_ERROR; } + // Automatically abort when a dive is older than the provided timestamp. + unsigned int datetime = data[current + 4] + (data[current + 5] << 8) + + (data[current + 6] << 16) + (data[current + 7] << 24); + if (datetime <= timestamp) + return 1; + if (callback && !callback (data + current, offset + 4 - current, userdata)) return 1; @@ -620,7 +642,7 @@ reefnet_sensusultra_device_foreach (device_t *abstract, dive_callback_t callback break; // Parse the page data. - rc = reefnet_sensusultra_parse (data, index, index + REEFNET_SENSUSULTRA_PACKET_SIZE, &previous, callback, userdata); + rc = reefnet_sensusultra_parse (data, index, index + REEFNET_SENSUSULTRA_PACKET_SIZE, &previous, callback, userdata, device->timestamp); if (rc != DEVICE_STATUS_SUCCESS) { if (rc > 0) break; // Aborted. @@ -646,9 +668,9 @@ reefnet_sensusultra_device_foreach (device_t *abstract, dive_callback_t callback device_status_t -reefnet_sensusultra_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) +reefnet_sensusultra_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata, unsigned int timestamp) { - int rc = reefnet_sensusultra_parse (data, 0, size, NULL, callback, userdata); + int rc = reefnet_sensusultra_parse (data, 0, size, NULL, callback, userdata, timestamp); if (rc != DEVICE_STATUS_SUCCESS) { if (rc > 0) return DEVICE_STATUS_SUCCESS; // Aborted. diff --git a/src/reefnet_sensusultra.h b/src/reefnet_sensusultra.h index 28eaa5c..1874bc1 100644 --- a/src/reefnet_sensusultra.h +++ b/src/reefnet_sensusultra.h @@ -20,6 +20,9 @@ reefnet_sensusultra_device_open (device_t **device, const char* name); device_status_t reefnet_sensusultra_device_set_maxretries (device_t *device, unsigned int maxretries); +device_status_t +reefnet_sensusultra_device_set_timestamp (device_t *device, unsigned int timestamp); + device_status_t reefnet_sensusultra_device_read_user (device_t *device, unsigned char *data, unsigned int size); @@ -42,7 +45,7 @@ device_status_t reefnet_sensusultra_device_sense (device_t *device, unsigned char *data, unsigned int size); device_status_t -reefnet_sensusultra_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata); +reefnet_sensusultra_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata, unsigned int timestamp); #ifdef __cplusplus } diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index ed47e84..c745840 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -27,6 +27,7 @@ typedef struct uwatec_aladin_device_t uwatec_aladin_device_t; struct uwatec_aladin_device_t { device_t base; struct serial *port; + unsigned int timestamp; }; static const device_backend_t uwatec_aladin_device_backend; @@ -59,6 +60,7 @@ uwatec_aladin_device_open (device_t **out, const char* name) // Set the default values. device->port = NULL; + device->timestamp = 0; // Open the device. int rc = serial_open (&device->port, name); @@ -121,6 +123,20 @@ uwatec_aladin_device_close (device_t *abstract) } +device_status_t +uwatec_aladin_device_set_timestamp (device_t *abstract, unsigned int timestamp) +{ + uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; + + if (! device_is_uwatec_aladin (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + + device->timestamp = timestamp; + + return DEVICE_STATUS_SUCCESS; +} + + static device_status_t uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned int size) { @@ -179,20 +195,25 @@ uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned in static device_status_t uwatec_aladin_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata) { + uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; + + if (! device_is_uwatec_aladin (abstract)) + return DEVICE_STATUS_TYPE_MISMATCH; + unsigned char data[UWATEC_ALADIN_MEMORY_SIZE] = {0}; int rc = uwatec_aladin_device_dump (abstract, data, sizeof (data)); if (rc < 0) return rc; - return uwatec_aladin_extract_dives (data, sizeof (data), callback, userdata); + return uwatec_aladin_extract_dives (data, sizeof (data), callback, userdata, device->timestamp); } #define HEADER 4 device_status_t -uwatec_aladin_extract_dives (const unsigned char* data, unsigned int size, dive_callback_t callback, void *userdata) +uwatec_aladin_extract_dives (const unsigned char* data, unsigned int size, dive_callback_t callback, void *userdata, unsigned int timestamp) { if (size < UWATEC_ALADIN_MEMORY_SIZE) return DEVICE_STATUS_ERROR; @@ -285,6 +306,12 @@ uwatec_aladin_extract_dives (const unsigned char* data, unsigned int size, dive_ profiles = 0; } + // Automatically abort when a dive is older than the provided timestamp. + unsigned int datetime = buffer[11] + (buffer[12] << 8) + + (buffer[13] << 16) + (buffer[14] << 24); + if (datetime <= timestamp) + return DEVICE_STATUS_SUCCESS; + if (callback && !callback (buffer, len + 18, userdata)) return DEVICE_STATUS_SUCCESS; } diff --git a/src/uwatec_aladin.h b/src/uwatec_aladin.h index b803485..1318328 100644 --- a/src/uwatec_aladin.h +++ b/src/uwatec_aladin.h @@ -13,7 +13,10 @@ device_status_t uwatec_aladin_device_open (device_t **device, const char* name); device_status_t -uwatec_aladin_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata); +uwatec_aladin_device_set_timestamp (device_t *device, unsigned int timestamp); + +device_status_t +uwatec_aladin_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata, unsigned int timestamp); #ifdef __cplusplus }