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

This commit is contained in:
Jef Driesen 2008-03-31 09:21:41 +00:00
parent 2a1cd50910
commit 4d12b4b491
3 changed files with 36 additions and 0 deletions

View File

@ -8,6 +8,8 @@
#define UWATEC_ERROR_PROTOCOL -4
#define UWATEC_ERROR_TIMEOUT -5
typedef void (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata);
#include "uwatec_aladin.h"
#include "uwatec_memomouse.h"

View File

@ -347,3 +347,35 @@ uwatec_memomouse_read (memomouse *device, unsigned char data[], unsigned int siz
return UWATEC_SUCCESS;
}
int
uwatec_memomouse_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{
unsigned int previous = 0;
unsigned int current = 5;
while (current + 18 <= size) {
// Memomouse sends all the data twice. The first time, it sends
// the data starting from the oldest dive towards the newest dive.
// Next, it send the same data in reverse order (newest to oldest).
// We abort the parsing once we detect the first duplicate dive.
if (previous && memcmp (data + previous, data + current, 18) == 0)
break;
// Get the length of the profile data.
unsigned int len = data[current + 16] + (data[current + 17] << 8);
// Check for a buffer overflow.
if (current + len + 18 > size)
return UWATEC_ERROR;
if (callback)
callback (data + current, len + 18, userdata);
// Move to the next dive.
previous = current;
current += len + 18;
}
return UWATEC_SUCCESS;
}

View File

@ -15,6 +15,8 @@ int uwatec_memomouse_close (memomouse *device);
int uwatec_memomouse_read (memomouse *device, unsigned char data[], unsigned int size);
int uwatec_memomouse_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);
#ifdef __cplusplus
}
#endif /* __cplusplus */