From f37740be7561ece96e09e62b9b214b2c127b728a Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 15 May 2008 13:43:54 +0000 Subject: [PATCH] Added a function to extract individual dives from the binary data. --- reefnet_sensusultra.c | 43 +++++++++++++++++++++++++++++++++++++++++++ reefnet_sensusultra.h | 2 ++ 2 files changed, 45 insertions(+) diff --git a/reefnet_sensusultra.c b/reefnet_sensusultra.c index b25a511..7401abc 100644 --- a/reefnet_sensusultra.c +++ b/reefnet_sensusultra.c @@ -478,3 +478,46 @@ reefnet_sensusultra_sense (sensusultra *device, unsigned char *data, unsigned in return REEFNET_SUCCESS; } + + +int +reefnet_sensusultra_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[4] = {0xFF, 0xFF, 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 + 16; // Skip non-sample data. + while (offset + 4 <= previous) { + if (memcmp (data + offset, footer, sizeof (footer)) == 0) { + if (callback) + callback (data + current, offset + 4 - 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_sensusultra.h b/reefnet_sensusultra.h index 750a4c2..644e2bb 100644 --- a/reefnet_sensusultra.h +++ b/reefnet_sensusultra.h @@ -32,6 +32,8 @@ int reefnet_sensusultra_write_averaging (sensusultra *device, unsigned int value int reefnet_sensusultra_sense (sensusultra *device, unsigned char *data, unsigned int size); +int reefnet_sensusultra_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata); + #ifdef __cplusplus } #endif /* __cplusplus */