Switch from GPLv2 to GPLv2+
[fw/altos] / altoslib / AltosEepromDownload.java
index 1b04316715809b1e42be683748112761b6e720aa..c38fcfdf10432008e758a0c4fc73b6309db8cee6 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,7 +16,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altoslib_2;
+package org.altusmetrum.altoslib_11;
 
 import java.io.*;
 import java.util.*;
@@ -35,7 +36,7 @@ public class AltosEepromDownload implements Runnable {
 
        AltosEepromList         flights;
        boolean                 success;
-       ParseException          parse_exception;
+       String                  parse_errors;
        AltosState              state;
 
        private void FlushPending() throws IOException {
@@ -75,7 +76,8 @@ public class AltosEepromDownload implements Runnable {
        }
 
        boolean                 done;
-       boolean                 start;
+       int                     prev_state;
+       int                     state_block;
 
        void LogEeprom(AltosEeprom r) throws IOException {
                if (r.cmd != AltosLib.AO_LOG_INVALID) {
@@ -87,6 +89,13 @@ public class AltosEepromDownload implements Runnable {
                }
        }
 
+       void LogError(String error) {
+               if (parse_errors != null)
+                       parse_errors.concat(error.concat("\n"));
+               else
+                       parse_errors = error;
+       }
+
        void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
                boolean any_valid = false;
                boolean got_flight = false;
@@ -97,7 +106,14 @@ public class AltosEepromDownload implements Runnable {
                monitor.set_serial(flights.config_data.serial);
 
                for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
-                       AltosEeprom r = eechunk.eeprom(i, log_format, state);
+                       AltosEeprom r = null;
+
+                       try {
+                               r = eechunk.eeprom(i, log_format, state);
+                       } catch (ParseException pe) {
+                               LogError(pe.getMessage());
+                               r = null;
+                       }
 
                        if (r == null)
                                continue;
@@ -110,12 +126,12 @@ public class AltosEepromDownload implements Runnable {
                                monitor.set_flight(state.flight);
 
                        /* Monitor state transitions to update display */
-                       if (state.state != AltosLib.ao_flight_invalid &&
-                           state.state <= AltosLib.ao_flight_landed)
+                       if (state.state() != AltosLib.ao_flight_invalid &&
+                           state.state() <= AltosLib.ao_flight_landed)
                        {
-                               if (state.state > AltosLib.ao_flight_pad)
+                               if (state.state() > AltosLib.ao_flight_pad)
                                        want_file = true;
-                               if (state.state == AltosLib.ao_flight_landed)
+                               if (state.state() == AltosLib.ao_flight_landed)
                                        done = true;
                        }
 
@@ -132,7 +148,7 @@ public class AltosEepromDownload implements Runnable {
 
                CheckFile(false);
        }
-       
+
        void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
                int                     block, state_block = 0;
                int                     log_format = flights.config_data.log_format;
@@ -140,26 +156,20 @@ public class AltosEepromDownload implements Runnable {
                state = new AltosState();
 
                done = false;
-               start = true;
 
                if (flights.config_data.serial < 0)
                        throw new IOException("no serial number found");
 
                /* Reset per-capture variables */
                want_file = false;
-               eeprom_file = null;
                eeprom_pending = new LinkedList<String>();
 
                /* Set serial number in the monitor dialog window */
                /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
 
                state_block = log.start_block;
+               prev_state = AltosLib.ao_flight_startup;
                for (block = log.start_block; !done && block < log.end_block; block++) {
-                       monitor.set_value(state.state_name(),
-                                         state.state,
-                                         block - state_block,
-                                         block - log.start_block);
-
                        AltosEepromChunk        eechunk = new AltosEepromChunk(link, block, block == log.start_block);
 
                        /*
@@ -177,12 +187,18 @@ public class AltosEepromDownload implements Runnable {
                        }
 
                        CaptureEeprom (eechunk, log_format);
+
+                       if (state.state() != prev_state && state.state() != AltosLib.ao_flight_invalid) {
+                               state_block = block;
+                               prev_state = state.state();
+                       }
+
+                       monitor.set_value(state.state_name(),
+                                         state.state(),
+                                         block - state_block,
+                                         block - log.start_block);
                }
                CheckFile(true);
-               if (eeprom_file != null) {
-                       eeprom_file.flush();
-                       eeprom_file.close();
-               }
        }
 
        public void run () {
@@ -192,20 +208,25 @@ public class AltosEepromDownload implements Runnable {
                                link.start_remote();
 
                        for (AltosEepromLog log : flights) {
-                               parse_exception = null;
+                               parse_errors = null;
                                if (log.selected) {
                                        monitor.reset();
+                                       eeprom_file = null;
                                        try {
                                                CaptureLog(log);
                                        } catch (ParseException e) {
-                                               parse_exception = e;
+                                               LogError(e.getMessage());
+                                       }
+                                       if (eeprom_file != null) {
+                                               eeprom_file.flush();
+                                               eeprom_file.close();
                                        }
                                }
-                               if (parse_exception != null) {
+                               if (parse_errors != null) {
                                        failed = true;
-                                       monitor.show_message(String.format("Flight %d download error\n%s\nValid log data saved",
+                                       monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
                                                                           log.flight,
-                                                                          parse_exception.getMessage()),
+                                                                          parse_errors),
                                                             link.name,
                                                             AltosEepromMonitor.WARNING_MESSAGE);
                                }
@@ -254,7 +275,10 @@ public class AltosEepromDownload implements Runnable {
                flights = given_flights;
                success = false;
 
-               monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
+               if (flights.config_data.log_has_state())
+                       monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
+               else
+                       monitor.set_states(AltosLib.ao_flight_invalid, AltosLib.ao_flight_invalid);
 
                monitor.start();
        }