+#ifndef AO_LOG_UNCOMMON
+/*
+ * Common logging functions which depend on the type of the log data
+ * structure.
+ */
+
+__xdata ao_log_type log;
+
+static uint8_t
+ao_log_csum(__xdata uint8_t *b) __reentrant
+{
+ uint8_t sum = 0x5a;
+ uint8_t i;
+
+ for (i = 0; i < sizeof (ao_log_type); i++)
+ sum += *b++;
+ return -sum;
+}
+
+uint8_t
+ao_log_write(__xdata ao_log_type *log) __reentrant
+{
+ uint8_t wrote = 0;
+ /* set checksum */
+ log->csum = 0;
+ log->csum = ao_log_csum((__xdata uint8_t *) log);
+ ao_mutex_get(&ao_log_mutex); {
+ if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
+ ao_log_stop();
+ if (ao_log_running) {
+ wrote = 1;
+ ao_storage_write(ao_log_current_pos,
+ log,
+ sizeof (ao_log_type));
+ ao_log_current_pos += sizeof (ao_log_type);
+ }
+ } ao_mutex_put(&ao_log_mutex);
+ return wrote;
+}
+
+uint8_t
+ao_log_check_data(void)
+{
+ if (ao_log_csum((uint8_t *) &log) != 0)
+ return 0;
+ return 1;
+}
+
+uint8_t
+ao_log_check_clear(void)
+{
+ uint8_t *b = (uint8_t *) &log;
+ uint8_t i;
+
+ for (i = 0; i < sizeof (ao_log_type); i++) {
+ if (*b++ != 0xff)
+ return 0;
+ }
+ return 1;
+}
+
+int16_t
+ao_log_flight(uint8_t slot)
+{
+ if (!ao_storage_read(ao_log_pos(slot),
+ &log,
+ sizeof (ao_log_type)))
+ return -(int16_t) (slot + 1);
+
+ if (ao_log_check_clear())
+ return 0;
+
+ if (!ao_log_check_data() || log.type != AO_LOG_FLIGHT)
+ return -(int16_t) (slot + 1);
+
+ return log.u.flight.flight;
+}
+#endif
+