diff --git a/src/device-private.h b/src/device-private.h index 597a5ef..1c85aba 100644 --- a/src/device-private.h +++ b/src/device-private.h @@ -19,8 +19,8 @@ struct device_t { void *userdata; }; -struct device_backend_t { - device_type_t type; +struct device_backend_t { + device_type_t type; device_status_t (*handshake) (device_t *device, unsigned char data[], unsigned int size); @@ -34,7 +34,7 @@ struct device_backend_t { device_status_t (*foreach) (device_t *device, dive_callback_t callback, void *userdata); - device_status_t (*close) (device_t *device); + device_status_t (*close) (device_t *device); }; #define INFINITE ((unsigned int)-1) diff --git a/src/suunto_common.c b/src/suunto_common.c index 32af80a..be4330a 100644 --- a/src/suunto_common.c +++ b/src/suunto_common.c @@ -1,53 +1,53 @@ -#include -#include - -#include "suunto_common.h" -#include "ringbuffer.h" - +#include +#include -device_status_t +#include "suunto_common.h" +#include "ringbuffer.h" + + +device_status_t suunto_common_extract_dives (const unsigned char data[], unsigned int begin, unsigned int end, unsigned int eop, unsigned int peek, dive_callback_t callback, void *userdata) { - assert (eop >= begin && eop < end); - assert (data[eop] == 0x82); + assert (eop >= begin && eop < end); + assert (data[eop] == 0x82); unsigned char buffer[0x2000 - 0x4C] = {0}; assert (sizeof (buffer) >= end - begin); - - unsigned int current = eop; - unsigned int previous = eop; - for (unsigned int i = 0; i < end - begin; ++i) { - // Move backwards through the ringbuffer. - if (current == begin) - current = end; - current--; - - // Check for an end of profile marker. - if (data[current] == 0x82) - break; - - // Check for an end of dive marker (of the next dive), + + unsigned int current = eop; + unsigned int previous = eop; + for (unsigned int i = 0; i < end - begin; ++i) { + // Move backwards through the ringbuffer. + if (current == begin) + current = end; + current--; + + // Check for an end of profile marker. + if (data[current] == 0x82) + break; + + // Check for an end of dive marker (of the next dive), // to find the start of the current dive. - unsigned int index = ringbuffer_decrement (current, peek, begin, end); - if (data[index] == 0x80) { - unsigned int len = ringbuffer_distance (current, previous, begin, end); - if (current + len > end) { - unsigned int a = end - current; - unsigned int b = (current + len) - end; - memcpy (buffer + 0, data + current, a); - memcpy (buffer + a, data + begin, b); - } else { - memcpy (buffer, data + current, len); - } - - if (callback && !callback (buffer, len, userdata)) - return DEVICE_STATUS_SUCCESS; - - previous = current; - } - } - - assert (data[current] == 0x82); - + unsigned int index = ringbuffer_decrement (current, peek, begin, end); + if (data[index] == 0x80) { + unsigned int len = ringbuffer_distance (current, previous, begin, end); + if (current + len > end) { + unsigned int a = end - current; + unsigned int b = (current + len) - end; + memcpy (buffer + 0, data + current, a); + memcpy (buffer + a, data + begin, b); + } else { + memcpy (buffer, data + current, len); + } + + if (callback && !callback (buffer, len, userdata)) + return DEVICE_STATUS_SUCCESS; + + previous = current; + } + } + + assert (data[current] == 0x82); + return DEVICE_STATUS_SUCCESS; }