2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
24 import java.util.concurrent.*;
26 public class AltosEepromDownload implements Runnable {
31 AltosEepromMonitor monitor;
34 FileWriter eeprom_file;
35 LinkedList<String> eeprom_pending;
37 AltosEepromList flights;
42 private void FlushPending() throws IOException {
43 for (String s : flights.config_data) {
45 eeprom_file.write('\n');
48 for (String s : eeprom_pending)
52 private void CheckFile(boolean force) throws IOException {
53 if (eeprom_file != null)
55 if (force || (state.flight != 0 && want_file)) {
56 AltosFile eeprom_name;
57 AltosGPS gps = state.gps;
60 gps.year != AltosLib.MISSING &&
61 gps.month != AltosLib.MISSING &&
62 gps.day != AltosLib.MISSING)
64 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
65 state.serial, state.flight, "eeprom");
67 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
69 eeprom_file = new FileWriter(eeprom_name);
70 if (eeprom_file != null) {
71 monitor.set_filename(eeprom_name.getName());
73 eeprom_pending = null;
82 void LogEeprom(AltosEeprom r) throws IOException {
83 if (r.cmd != AltosLib.AO_LOG_INVALID) {
84 String line = r.string();
85 if (eeprom_file != null)
86 eeprom_file.write(line);
88 eeprom_pending.add(line);
92 void LogError(String error) {
93 if (parse_errors != null)
94 parse_errors.concat(error.concat("\n"));
99 void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
100 boolean any_valid = false;
101 boolean got_flight = false;
103 int record_length = 8;
105 state.set_serial(flights.config_data.serial);
106 monitor.set_serial(flights.config_data.serial);
108 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
109 AltosEeprom r = null;
112 r = eechunk.eeprom(i, log_format, state);
113 } catch (ParseException pe) {
114 LogError(pe.getMessage());
121 record_length = r.record_length();
123 r.update_state(state);
125 if (!got_flight && state.flight != AltosLib.MISSING)
126 monitor.set_flight(state.flight);
128 /* Monitor state transitions to update display */
129 if (state.state() != AltosLib.ao_flight_invalid &&
130 state.state() <= AltosLib.ao_flight_landed)
132 if (state.state() > AltosLib.ao_flight_pad)
134 if (state.state() == AltosLib.ao_flight_landed)
138 if (state.gps != null)
152 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
153 int block, state_block = 0;
154 int log_format = flights.config_data.log_format;
156 state = new AltosState();
160 if (flights.config_data.serial < 0)
161 throw new IOException("no serial number found");
163 /* Reset per-capture variables */
165 eeprom_pending = new LinkedList<String>();
167 /* Set serial number in the monitor dialog window */
168 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
170 state_block = log.start_block;
171 prev_state = AltosLib.ao_flight_startup;
172 for (block = log.start_block; !done && block < log.end_block; block++) {
173 AltosEepromChunk eechunk = new AltosEepromChunk(link, block, block == log.start_block);
176 * Guess what kind of data is there if the device
180 if (log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
181 if (block == log.start_block) {
182 if (eechunk.data(0) == AltosLib.AO_LOG_FLIGHT)
183 log_format = AltosLib.AO_LOG_FORMAT_FULL;
185 log_format = AltosLib.AO_LOG_FORMAT_TINY;
189 CaptureEeprom (eechunk, log_format);
191 if (state.state() != prev_state && state.state() != AltosLib.ao_flight_invalid) {
193 prev_state = state.state();
196 monitor.set_value(state.state_name(),
199 block - log.start_block);
206 boolean failed = false;
210 for (AltosEepromLog log : flights) {
217 } catch (ParseException e) {
218 LogError(e.getMessage());
220 if (eeprom_file != null) {
225 if (parse_errors != null) {
227 monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
231 AltosEepromMonitor.WARNING_MESSAGE);
235 } catch (IOException ee) {
236 monitor.show_message(ee.getLocalizedMessage(),
238 AltosEepromMonitor.ERROR_MESSAGE);
239 } catch (InterruptedException ie) {
240 monitor.show_message(String.format("Connection to \"%s\" interrupted",
242 "Connection Interrupted",
243 AltosEepromMonitor.ERROR_MESSAGE);
244 } catch (TimeoutException te) {
245 monitor.show_message(String.format("Connection to \"%s\" failed",
248 AltosEepromMonitor.ERROR_MESSAGE);
253 } catch (InterruptedException ie) {
258 monitor.done(success);
261 public void start() {
262 eeprom_thread = new Thread(this);
263 monitor.set_thread(eeprom_thread);
264 eeprom_thread.start();
267 public AltosEepromDownload(AltosEepromMonitor given_monitor,
268 AltosLink given_link,
269 boolean given_remote,
270 AltosEepromList given_flights) {
272 monitor = given_monitor;
274 remote = given_remote;
275 flights = given_flights;
278 if (flights.config_data.log_has_state())
279 monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
281 monitor.set_states(AltosLib.ao_flight_invalid, AltosLib.ao_flight_invalid);