telegps-v1.0: Provide one log and append to it
authorKeith Packard <keithp@keithp.com>
Mon, 18 Aug 2014 03:59:45 +0000 (20:59 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 18 Aug 2014 03:59:45 +0000 (20:59 -0700)
Instead of creating per-flight logs, create a single log and append
data to it each time TeleGPS is powered on. This avoids potentially
running out of log space just because the device is powered off/on.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/kernel/ao_config.c
src/kernel/ao_config.h
src/kernel/ao_log.c
src/kernel/ao_log.h
src/kernel/ao_log_gps.c
src/telegps-v1.0/ao_pins.h

index d73a3733db1989bb95dbd1386d67114048c0ba76..d1b931221ae395094e79d34b67ec500fb60c17f4 100644 (file)
@@ -50,13 +50,19 @@ __xdata uint8_t ao_config_mutex;
 #error Please define USE_INTERNAL_FLASH
 #endif
 #endif
+
 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
-#if USE_INTERNAL_FLASH
-#define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX       ao_storage_config
-#else
-#define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX       ((uint32_t) 192 * (uint32_t) 1024)
-#endif
+# if FLIGHT_LOG_APPEND
+#  define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX     ao_storage_log_max
+# else
+#  if USE_INTERNAL_FLASH
+#   define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX    ao_storage_config
+#  else
+#   define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX    ((uint32_t) 192 * (uint32_t) 1024)
+#  endif
+# endif
 #endif
+
 #ifndef AO_CONFIG_DEFAULT_RADIO_POWER
 #define AO_CONFIG_DEFAULT_RADIO_POWER          0x60
 #endif
@@ -525,15 +531,36 @@ ao_config_radio_rate_set(void) __reentrant
 #endif
 
 #if HAS_LOG
+
 void
 ao_config_log_show(void) __reentrant
 {
        printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
+#if FLIGHT_LOG_APPEND
+       printf("Log fixed: 1\n");
+#endif
+}
+
+#if FLIGHT_LOG_APPEND
+void
+ao_config_log_fix_append(void)
+{
+       _ao_config_edit_start();
+       ao_config.flight_log_max = ao_storage_log_max;
+       _ao_config_edit_finish();
+       ao_mutex_get(&ao_config_mutex);
+       _ao_config_put();
+       ao_config_dirty = 0;
+       ao_mutex_put(&ao_config_mutex);
 }
+#endif
 
 void
 ao_config_log_set(void) __reentrant
 {
+#if FLIGHT_LOG_APPEND
+       printf("Flight log fixed size %d kB\n", ao_storage_log_max >> 10);
+#else
        uint16_t        block = (uint16_t) (ao_storage_block >> 10);
        uint16_t        log_max = (uint16_t) (ao_storage_log_max >> 10);
 
@@ -551,6 +578,7 @@ ao_config_log_set(void) __reentrant
                ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
                _ao_config_edit_finish();
        }
+#endif
 }
 #endif /* HAS_LOG */
 
index a650ffc63dafc91419c59686bdef27e5604431e7..0be3e14d1ee0b8a1fc7ca7447d1dbd89169e0371 100644 (file)
 #define USE_EEPROM_CONFIG 0
 #endif
 
+#ifndef FLIGHT_LOG_APPEND
+#define FLIGHT_LOG_APPEND 0
+#endif
+
 #if USE_STORAGE_CONFIG
 
 #include <ao_storage.h>
@@ -146,6 +150,9 @@ ao_config_put(void);
 void
 ao_config_set_radio(void);
 
+void
+ao_config_log_fix_append(void);
+
 void
 ao_config_init(void);
 
index 91617d93531842dfe50b8469d0304817c254eb04..dc3b6486caabea5355753cea8e446fd1eb213dac 100644 (file)
@@ -142,19 +142,39 @@ ao_log_max_flight(void)
        return max_flight;
 }
 
-void
-ao_log_scan(void) __reentrant
+static void
+ao_log_erase(uint8_t slot) __reentrant
 {
-       uint8_t         log_slot;
-       uint8_t         log_slots;
-       uint8_t         log_want;
+       uint32_t log_current_pos, log_end_pos;
 
-       ao_config_get();
+       ao_log_erase_mark();
+       log_current_pos = ao_log_pos(slot);
+       log_end_pos = log_current_pos + ao_config.flight_log_max;
+       while (log_current_pos < log_end_pos) {
+               uint8_t i;
+               static __xdata uint8_t b;
+
+               /*
+                * Check to see if we've reached the end of
+                * the used memory to avoid re-erasing the same
+                * memory over and over again
+                */
+               for (i = 0; i < 16; i++) {
+                       if (ao_storage_read(log_current_pos + i, &b, 1))
+                               if (b != 0xff)
+                                       break;
+               }
+               if (i == 16)
+                       break;
+               ao_storage_erase(log_current_pos);
+               log_current_pos += ao_storage_block;
+       }
+}
 
-       ao_flight_number = ao_log_max_flight();
-       if (ao_flight_number)
-               if (++ao_flight_number == 0)
-                       ao_flight_number = 1;
+static void
+ao_log_find_max_erase_flight(void) __reentrant
+{
+       uint8_t log_slot;
 
        /* Now look through the log of flight numbers from erase operations and
         * see if the last one is bigger than what we found above
@@ -170,6 +190,74 @@ ao_log_scan(void) __reentrant
        }
        if (ao_flight_number == 0)
                ao_flight_number = 1;
+}
+
+void
+ao_log_scan(void) __reentrant
+{
+       uint8_t         log_slot;
+       uint8_t         log_slots;
+#if !FLIGHT_LOG_APPEND
+       uint8_t         log_want;
+#endif
+
+       ao_config_get();
+
+       /* Get any existing flight number */
+       ao_flight_number = ao_log_max_flight();
+
+#if FLIGHT_LOG_APPEND
+
+       /* Deal with older OS versions which stored multiple
+        * flights in rom by erasing everything after the first
+        * slot
+        */
+       if (ao_config.flight_log_max != ao_storage_log_max) {
+               log_slots = ao_log_slots();
+               for (log_slot = 1; log_slot < log_slots; log_slot++) {
+                       if (ao_log_flight(log_slot) != 0)
+                               ao_log_erase(log_slot);
+               }
+               ao_config_log_fix_append();
+       }
+       ao_log_current_pos = ao_log_pos(0);
+       ao_log_end_pos = ao_log_current_pos + ao_storage_log_max;
+
+       if (ao_flight_number) {
+               uint32_t        full = ao_log_current_pos;
+               uint32_t        empty = ao_log_end_pos - ao_log_size;
+
+               /* If there's already a flight started, then find the
+                * end of it
+                */
+               for (;;) {
+                       ao_log_current_pos = (full + empty) >> 1;
+                       ao_log_current_pos -= ao_log_current_pos % ao_log_size;
+
+                       if (ao_log_current_pos == full) {
+                               if (ao_log_check(ao_log_current_pos))
+                                       ao_log_current_pos += ao_log_size;
+                               break;
+                       }
+                       if (ao_log_current_pos == empty)
+                               break;
+
+                       if (ao_log_check(ao_log_current_pos)) {
+                               full = ao_log_current_pos;
+                       } else {
+                               empty = ao_log_current_pos;
+                       }
+               }
+       } else {
+               ao_log_find_max_erase_flight();
+       }
+#else
+
+       if (ao_flight_number)
+               if (++ao_flight_number == 0)
+                       ao_flight_number = 1;
+
+       ao_log_find_max_erase_flight();
 
        /* With a flight number in hand, find a place to write a new log,
         * use the target flight number to index the available log slots so
@@ -190,7 +278,7 @@ ao_log_scan(void) __reentrant
                if (++log_slot >= log_slots)
                        log_slot = 0;
        } while (log_slot != log_want);
-
+#endif
        ao_wakeup(&ao_flight_number);
 }
 
@@ -254,7 +342,6 @@ ao_log_delete(void) __reentrant
 {
        uint8_t slot;
        uint8_t slots;
-       uint32_t log_current_pos, log_end_pos;
 
        ao_cmd_decimal();
        if (ao_cmd_status != ao_cmd_success)
@@ -268,28 +355,7 @@ ao_log_delete(void) __reentrant
 #if HAS_TRACKER
                                ao_tracker_erase_start(ao_cmd_lex_i);
 #endif
-                               ao_log_erase_mark();
-                               log_current_pos = ao_log_pos(slot);
-                               log_end_pos = log_current_pos + ao_config.flight_log_max;
-                               while (log_current_pos < log_end_pos) {
-                                       uint8_t i;
-                                       static __xdata uint8_t b;
-
-                                       /*
-                                        * Check to see if we've reached the end of
-                                        * the used memory to avoid re-erasing the same
-                                        * memory over and over again
-                                        */
-                                       for (i = 0; i < 16; i++) {
-                                               if (ao_storage_read(log_current_pos + i, &b, 1))
-                                                       if (b != 0xff)
-                                                               break;
-                                       }
-                                       if (i == 16)
-                                               break;
-                                       ao_storage_erase(log_current_pos);
-                                       log_current_pos += ao_storage_block;
-                               }
+                               ao_log_erase(slot);
 #if HAS_TRACKER
                                ao_tracker_erase_end();
 #endif
index da20e067148d23467915fb71b05ef91ed5436265..c5fa7faba6d867bee81ab990fcdf35eafa0d69c1 100644 (file)
@@ -51,11 +51,16 @@ extern __pdata enum ao_flight_state ao_log_state;
 #define AO_LOG_FORMAT_NONE             127     /* No log at all */
 
 extern __code uint8_t ao_log_format;
+extern __code uint8_t ao_log_size;
 
 /* Return the flight number from the given log slot, 0 if none */
 uint16_t
 ao_log_flight(uint8_t slot);
 
+/* Check if there is valid log data at the specified location */
+uint8_t
+ao_log_check(uint32_t pos);
+
 /* Flush the log */
 void
 ao_log_flush(void);
index a5a6358bf7fd9267ddb414357a0f5fbd220eab8d..7643091cdaa759964cdb5b3e7a214eb8c2df55f5 100644 (file)
@@ -26,6 +26,7 @@
 static __xdata struct ao_log_gps log;
 
 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEGPS;
+__code uint8_t ao_log_size = sizeof (struct ao_log_gps);
 
 static uint8_t
 ao_log_csum(__xdata uint8_t *b) __reentrant
@@ -136,3 +137,16 @@ ao_log_flight(uint8_t slot)
                return log.u.flight.flight;
        return 0;
 }
+
+uint8_t
+ao_log_check(uint32_t pos)
+{
+       if (!ao_storage_read(pos,
+                            &log,
+                            sizeof (struct ao_log_gps)))
+               return 0;
+
+       if (ao_log_dump_check_data())
+               return 1;
+       return 0;
+}
index 5f53dd9dd39dc745ba2396b9d7b17c61f4ff16a5..d2382a56318c3075e76f636a8cddb1164a346ff9 100644 (file)
 #define HAS_GPS                        1
 #define HAS_FLIGHT             0
 #define HAS_LOG                        1
+#define FLIGHT_LOG_APPEND      1
 #define HAS_TRACKER            1
 
 #define AO_CONFIG_DEFAULT_APRS_INTERVAL                0
 #define AO_CONFIG_DEFAULT_RADIO_POWER          0xc0
-#define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX       496 * 1024
 
 /*
  * GPS