From 863890aa238b4a70b90d8cf81c88f8522c90eb93 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 12 May 2009 19:58:01 +0000 Subject: [PATCH] Add the end-of-profile pointer to the layout descriptor structure. --- src/suunto_common.c | 27 ++++++++++++++++++++++++--- src/suunto_common.h | 4 +++- src/suunto_eon.c | 12 ++---------- src/suunto_vyper.c | 8 ++++---- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/suunto_common.c b/src/suunto_common.c index ff2fdb8..4fe8cff 100644 --- a/src/suunto_common.c +++ b/src/suunto_common.c @@ -25,6 +25,7 @@ #include "suunto_common.h" #include "ringbuffer.h" +#include "array.h" #define RB_PROFILE_DISTANCE(a,b,l) ringbuffer_distance (a, b, l->rb_profile_begin, l->rb_profile_end) #define RB_PROFILE_PEEK(a,l) ringbuffer_decrement (a, l->peek, l->rb_profile_begin, l->rb_profile_end) @@ -60,12 +61,32 @@ suunto_common_device_set_fingerprint (suunto_common_device_t *device, const unsi device_status_t -suunto_common_extract_dives (suunto_common_device_t *device, const suunto_common_layout_t *layout, const unsigned char data[], unsigned int eop, dive_callback_t callback, void *userdata) +suunto_common_extract_dives (suunto_common_device_t *device, const suunto_common_layout_t *layout, const unsigned char data[], dive_callback_t callback, void *userdata) { assert (layout != NULL); - assert (eop >= layout->rb_profile_begin && eop < layout->rb_profile_end); - assert (data[eop] == 0x82); + unsigned int eop; + if (layout->eop) { + // Get the end-of-profile pointer directly from the header. + eop = array_uint16_be (data + layout->eop); + } else { + // Get the end-of-profile pointer by searching for the + // end-of-profile marker in the profile ringbuffer. + eop = layout->rb_profile_begin; + while (eop < layout->rb_profile_end) { + if (data[eop] == 0x82) + break; + eop++; + } + } + + // Validate the end-of-profile pointer. + if (eop < layout->rb_profile_begin || + eop >= layout->rb_profile_end || + data[eop] != 0x82) + { + return DEVICE_STATUS_ERROR; + } // Memory buffer for the profile ringbuffer. unsigned int length = layout->rb_profile_end - layout->rb_profile_begin; diff --git a/src/suunto_common.h b/src/suunto_common.h index 063bf06..65b2830 100644 --- a/src/suunto_common.h +++ b/src/suunto_common.h @@ -34,6 +34,8 @@ typedef struct suunto_common_device_t { } suunto_common_device_t; typedef struct suunto_common_layout_t { + // End-of-profile marker + unsigned int eop; // Profile ringbuffer unsigned int rb_profile_begin; unsigned int rb_profile_end; @@ -50,7 +52,7 @@ device_status_t suunto_common_device_set_fingerprint (suunto_common_device_t *device, const unsigned char data[], unsigned int size); device_status_t -suunto_common_extract_dives (suunto_common_device_t *device, const suunto_common_layout_t *layout, const unsigned char data[], unsigned int eop, dive_callback_t callback, void *userdata); +suunto_common_extract_dives (suunto_common_device_t *device, const suunto_common_layout_t *layout, const unsigned char data[], dive_callback_t callback, void *userdata); #ifdef __cplusplus } diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 4ecc0ed..a332e55 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -63,6 +63,7 @@ static const device_backend_t suunto_eon_device_backend = { }; static const suunto_common_layout_t suunto_eon_layout = { + 0, /* eop */ 0x100, /* rb_profile_begin */ SUUNTO_EON_MEMORY_SIZE, /* rb_profile_end */ 6, /* fp_offset */ @@ -301,14 +302,5 @@ suunto_eon_extract_dives (device_t *abstract, const unsigned char data[], unsign if (size < SUUNTO_EON_MEMORY_SIZE) return DEVICE_STATUS_ERROR; - // Search the end-of-profile marker. - unsigned int eop = 0x100; - while (eop < SUUNTO_EON_MEMORY_SIZE) { - if (data[eop] == 0x82) { - break; - } - eop++; - } - - return suunto_common_extract_dives (device, &suunto_eon_layout, data, eop, callback, userdata); + return suunto_common_extract_dives (device, &suunto_eon_layout, data, callback, userdata); } diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index 98b35b6..08cdd7f 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -69,6 +69,7 @@ static const device_backend_t suunto_vyper_device_backend = { }; static const suunto_common_layout_t suunto_vyper_layout = { + 0x51, /* eop */ 0x71, /* rb_profile_begin */ SUUNTO_VYPER_MEMORY_SIZE, /* rb_profile_end */ 9, /* fp_offset */ @@ -76,6 +77,7 @@ static const suunto_common_layout_t suunto_vyper_layout = { }; static const suunto_common_layout_t suunto_spyder_layout = { + 0x1C, /* eop */ 0x4C, /* rb_profile_begin */ SUUNTO_VYPER_MEMORY_SIZE, /* rb_profile_end */ 7, /* fp_offset */ @@ -650,10 +652,8 @@ suunto_vyper_extract_dives (device_t *abstract, const unsigned char data[], unsi vyper = 0; if (vyper) { - unsigned int eop = array_uint16_be (data + 0x51); - return suunto_common_extract_dives (device, &suunto_vyper_layout, data, eop, callback, userdata); + return suunto_common_extract_dives (device, &suunto_vyper_layout, data, callback, userdata); } else { - unsigned int eop = array_uint16_be (data + 0x1C); - return suunto_common_extract_dives (device, &suunto_spyder_layout, data, eop, callback, userdata); + return suunto_common_extract_dives (device, &suunto_spyder_layout, data, callback, userdata); } }