Added a function to extract individual dives from the binary data.

This commit is contained in:
Jef Driesen 2008-05-15 13:22:26 +00:00
parent 311170f2b8
commit e5ad376764
3 changed files with 47 additions and 0 deletions

View File

@ -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"

View File

@ -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;
}

View File

@ -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 */