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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_4;
23 import java.util.concurrent.*;
25 public class AltosEepromDownload implements Runnable {
30 AltosEepromMonitor monitor;
33 FileWriter eeprom_file;
34 LinkedList<String> eeprom_pending;
36 AltosEepromList flights;
38 ParseException parse_exception;
41 private void FlushPending() throws IOException {
42 for (String s : flights.config_data) {
44 eeprom_file.write('\n');
47 for (String s : eeprom_pending)
51 private void CheckFile(boolean force) throws IOException {
52 if (eeprom_file != null)
54 if (force || (state.flight != 0 && want_file)) {
55 AltosFile eeprom_name;
56 AltosGPS gps = state.gps;
59 gps.year != AltosLib.MISSING &&
60 gps.month != AltosLib.MISSING &&
61 gps.day != AltosLib.MISSING)
63 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
64 state.serial, state.flight, "eeprom");
66 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
68 eeprom_file = new FileWriter(eeprom_name);
69 if (eeprom_file != null) {
70 monitor.set_filename(eeprom_name.getName());
72 eeprom_pending = null;
80 void LogEeprom(AltosEeprom r) throws IOException {
81 if (r.cmd != AltosLib.AO_LOG_INVALID) {
82 String line = r.string();
83 if (eeprom_file != null)
84 eeprom_file.write(line);
86 eeprom_pending.add(line);
90 void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
91 boolean any_valid = false;
92 boolean got_flight = false;
94 int record_length = 8;
96 state.set_serial(flights.config_data.serial);
97 monitor.set_serial(flights.config_data.serial);
99 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
100 AltosEeprom r = eechunk.eeprom(i, log_format, state);
105 record_length = r.record_length();
107 r.update_state(state);
109 if (!got_flight && state.flight != AltosLib.MISSING)
110 monitor.set_flight(state.flight);
112 /* Monitor state transitions to update display */
113 if (state.state != AltosLib.ao_flight_invalid &&
114 state.state <= AltosLib.ao_flight_landed)
116 if (state.state > AltosLib.ao_flight_pad)
118 if (state.state == AltosLib.ao_flight_landed)
122 if (state.gps != null)
136 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
137 int block, state_block = 0;
138 int log_format = flights.config_data.log_format;
140 state = new AltosState();
145 if (flights.config_data.serial < 0)
146 throw new IOException("no serial number found");
148 /* Reset per-capture variables */
151 eeprom_pending = new LinkedList<String>();
153 /* Set serial number in the monitor dialog window */
154 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
156 state_block = log.start_block;
157 for (block = log.start_block; !done && block < log.end_block; block++) {
158 monitor.set_value(state.state_name(),
161 block - log.start_block);
163 AltosEepromChunk eechunk = new AltosEepromChunk(link, block, block == log.start_block);
166 * Guess what kind of data is there if the device
170 if (log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
171 if (block == log.start_block) {
172 if (eechunk.data(0) == AltosLib.AO_LOG_FLIGHT)
173 log_format = AltosLib.AO_LOG_FORMAT_FULL;
175 log_format = AltosLib.AO_LOG_FORMAT_TINY;
179 CaptureEeprom (eechunk, log_format);
182 if (eeprom_file != null) {
190 boolean failed = false;
194 for (AltosEepromLog log : flights) {
195 parse_exception = null;
200 } catch (ParseException e) {
204 if (parse_exception != null) {
206 monitor.show_message(String.format("Flight %d download error\n%s\nValid log data saved",
208 parse_exception.getMessage()),
210 AltosEepromMonitor.WARNING_MESSAGE);
214 } catch (IOException ee) {
215 monitor.show_message(ee.getLocalizedMessage(),
217 AltosEepromMonitor.ERROR_MESSAGE);
218 } catch (InterruptedException ie) {
219 monitor.show_message(String.format("Connection to \"%s\" interrupted",
221 "Connection Interrupted",
222 AltosEepromMonitor.ERROR_MESSAGE);
223 } catch (TimeoutException te) {
224 monitor.show_message(String.format("Connection to \"%s\" failed",
227 AltosEepromMonitor.ERROR_MESSAGE);
232 } catch (InterruptedException ie) {
237 monitor.done(success);
240 public void start() {
241 eeprom_thread = new Thread(this);
242 monitor.set_thread(eeprom_thread);
243 eeprom_thread.start();
246 public AltosEepromDownload(AltosEepromMonitor given_monitor,
247 AltosLink given_link,
248 boolean given_remote,
249 AltosEepromList given_flights) {
251 monitor = given_monitor;
253 remote = given_remote;
254 flights = given_flights;
257 monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);