altos/telegps-*: Fix log end discovery at startup
authorKeith Packard <keithp@keithp.com>
Tue, 18 May 2021 05:33:21 +0000 (22:33 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 19 May 2021 05:53:51 +0000 (22:53 -0700)
We need to find the first unwritten log block to start appending data,
but the code was actually looking for the first empty 256-byte chunk,
which meant that we'd leave a gap of erased data after the previous
log. AltosUI would stop at that point and not recover the remaining
stored data.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/kernel/ao_log_gps.c
src/kernel/ao_storage.c
src/kernel/ao_storage.h

index bf326c1a7fb972b7e0d601adfaab767b806586a0..2b45f35e06ba11b07a35f3d4c84f791e6336606a 100644 (file)
@@ -81,18 +81,31 @@ ao_log_gps_tracking(uint16_t tick, struct ao_telemetry_satellite *gps_tracking_d
        ao_log_write(&ao_log_data);
 }
 
+static uint8_t
+ao_log_check_empty(void)
+{
+       uint8_t *b = (void *) &ao_log_data;
+       unsigned i;
+
+       for (i = 0; i < sizeof (ao_log_type); i++)
+               if (*b++ != AO_STORAGE_ERASED_BYTE)
+                       return 0;
+       return 1;
+}
+
 int8_t
 ao_log_check(uint32_t pos)
 {
-       if (ao_storage_is_erased(pos & ~(ao_storage_block - 1)))
-               return 0;
-
        if (!ao_storage_read(pos,
                             &ao_log_data,
                             sizeof (struct ao_log_gps)))
                return AO_LOG_INVALID;
 
+       if (ao_log_check_empty())
+               return AO_LOG_EMPTY;
+
        if (!ao_log_check_data())
                return AO_LOG_INVALID;
+
        return AO_LOG_VALID;
 }
index 213ec2d6872825cda21173ff15a13bfd8d6a5a6b..43abc43c1ec9e24396aff9a0c5dcc13dce44e386 100644 (file)
@@ -86,10 +86,6 @@ ao_storage_write(ao_pos_t pos, void *v_buf, uint16_t len)
        return 1;
 }
 
-#ifndef AO_STORAGE_ERASED_BYTE
-#define AO_STORAGE_ERASED_BYTE 0xff
-#endif
-
 uint8_t
 ao_storage_is_erased(uint32_t pos)
 {
index 026074b5d2db4c2bd30768a2e1fcd2de780bc636..ff8548eb150d71bb73a92ddf4457e7079a7a760f 100644 (file)
@@ -44,6 +44,10 @@ extern ao_pos_t      ao_storage_block;
 #define USE_STORAGE_CONFIG 1
 #endif
 
+#ifndef AO_STORAGE_ERASED_BYTE
+#define AO_STORAGE_ERASED_BYTE 0xff
+#endif
+
 #if USE_STORAGE_CONFIG
 /* Byte offset of config block. Will be ao_storage_block bytes long */
 extern ao_pos_t        ao_storage_config;