From ae733fd8a82f2c6f331a869325ac7428dad30851 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 13 Nov 2019 21:27:18 +0100 Subject: [PATCH] Ignore all empty logbook entries The logbook ringbuffer is always updated sequentially. Therefore, emtpy entries can only be present after the oldest dive. However it appears that under some special conditions (for example an empty battery during the dive), the logbook entry is not always stored correctly, which can result in an empty entry after all. I suspect that at the start of each dive, the OSTC erases the next available entry in the logbook ringbuffer and updates the internal write pointer. Once the dive is finished, the actual content of the erased logbook is written. Thus, when the OSTC runs out of battery power during the dive, that last step never happens, and the erased entry remains in place. As a workaround, ignore all empty logbook entries instead of assuming we reached the last dive. --- src/hw_ostc3.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c index 187a9e4..0521e2e 100644 --- a/src/hw_ostc3.c +++ b/src/hw_ostc3.c @@ -713,7 +713,6 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi // The device maintains an internal counter which is incremented for every // dive, and the current value at the time of the dive is stored in the // dive header. Thus the most recent dive will have the highest value. - unsigned int count = 0; unsigned int latest = 0; unsigned int maximum = 0; for (unsigned int i = 0; i < RB_LOGBOOK_COUNT; ++i) { @@ -729,24 +728,21 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi maximum = current; latest = i; } - - count++; } // Calculate the total and maximum size. unsigned int ndives = 0; unsigned int size = 0; unsigned int maxsize = 0; - for (unsigned int i = 0; i < count; ++i) { + unsigned char dive[RB_LOGBOOK_COUNT] = {0}; + for (unsigned int i = 0; i < RB_LOGBOOK_COUNT; ++i) { unsigned int idx = (latest + RB_LOGBOOK_COUNT - i) % RB_LOGBOOK_COUNT; unsigned int offset = idx * logbook->size; - // Uninitialized header entries should no longer be present at this - // stage, unless the dives are interleaved with empty entries. But - // that's something we don't support at all. + // Ignore uninitialized header entries. if (array_isequal (header + offset, logbook->size, 0xFF)) { WARNING (abstract->context, "Unexpected empty header found."); - break; + continue; } // Calculate the profile length. @@ -770,6 +766,7 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi if (length > maxsize) maxsize = length; size += length; + dive[ndives] = idx; ndives++; } @@ -793,7 +790,7 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi // Download the dives. for (unsigned int i = 0; i < ndives; ++i) { - unsigned int idx = (latest + RB_LOGBOOK_COUNT - i) % RB_LOGBOOK_COUNT; + unsigned int idx = dive[i]; unsigned int offset = idx * logbook->size; // Calculate the profile length.