From e5ad3767645888cf296f67adc47ced33f8664a15 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 15 May 2008 13:22:26 +0000 Subject: [PATCH] Added a function to extract individual dives from the binary data. --- reefnet.h | 2 ++ reefnet_sensuspro.c | 43 +++++++++++++++++++++++++++++++++++++++++++ reefnet_sensuspro.h | 2 ++ 3 files changed, 47 insertions(+) diff --git a/reefnet.h b/reefnet.h index 09a33d5..ab9240b 100644 --- a/reefnet.h +++ b/reefnet.h @@ -8,6 +8,8 @@ #define REEFNET_ERROR_PROTOCOL -4 #define REEFNET_ERROR_TIMEOUT -5 +typedef void (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata); + #include "reefnet_sensuspro.h" #include "reefnet_sensusultra.h" diff --git a/reefnet_sensuspro.c b/reefnet_sensuspro.c index 6157b82..9b9efd5 100644 --- a/reefnet_sensuspro.c +++ b/reefnet_sensuspro.c @@ -264,3 +264,46 @@ reefnet_sensuspro_write_interval (sensuspro *device, unsigned char interval) return REEFNET_SUCCESS; } + + +int +reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) +{ + const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; + const unsigned char footer[2] = {0xFF, 0xFF}; + + // Search the entire data stream for start markers. + unsigned int previous = size; + unsigned int current = (size >= 4 ? size - 4 : 0); + while (current > 0) { + current--; + if (memcmp (data + current, header, sizeof (header)) == 0) { + // Once a start marker is found, start searching + // for the corresponding stop marker. The search is + // now limited to the start of the previous dive. + int found = 0; + 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); + + found = 1; + break; + } else { + offset++; + } + } + + // Report an error if no stop marker was found. + if (!found) + return REEFNET_ERROR; + + // Prepare for the next dive. + previous = current; + current = (current >= 4 ? current - 4 : 0); + } + } + + return REEFNET_SUCCESS; +} diff --git a/reefnet_sensuspro.h b/reefnet_sensuspro.h index 697088d..c195af2 100644 --- a/reefnet_sensuspro.h +++ b/reefnet_sensuspro.h @@ -20,6 +20,8 @@ int reefnet_sensuspro_read (sensuspro *device, unsigned char data[], unsigned in int reefnet_sensuspro_write_interval (sensuspro *device, unsigned char interval); +int reefnet_sensuspro_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata); + #ifdef __cplusplus } #endif /* __cplusplus */