6ce420d323523eb40d6277d0f5617f3cfb78b744
[fw/altos] / altosui / AltosEepromDownload.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.altoslib_2.*;
25
26 public class AltosEepromDownload implements Runnable {
27
28         AltosSerial             serial_line;
29         boolean                 remote;
30         Thread                  eeprom_thread;
31         AltosEepromMonitor      monitor;
32
33         boolean                 want_file;
34         FileWriter              eeprom_file;
35         LinkedList<String>      eeprom_pending;
36
37         AltosEepromList         flights;
38         boolean                 success;
39         ParseException          parse_exception;
40         AltosState              state;
41
42         private void FlushPending() throws IOException {
43                 for (String s : flights.config_data) {
44                         eeprom_file.write(s);
45                         eeprom_file.write('\n');
46                 }
47
48                 for (String s : eeprom_pending)
49                         eeprom_file.write(s);
50         }
51
52         private void CheckFile(boolean force) throws IOException {
53                 if (eeprom_file != null)
54                         return;
55                 if (force || (state.flight != 0 && want_file)) {
56                         AltosFile               eeprom_name;
57                         AltosGPS                gps = state.gps;
58
59                         if (gps != null &&
60                             gps.year != AltosLib.MISSING &&
61                             gps.month != AltosLib.MISSING &&
62                             gps.day != AltosLib.MISSING)
63                         {
64                                 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
65                                                             state.serial, state.flight, "eeprom");
66                         } else
67                                 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
68
69                         eeprom_file = new FileWriter(eeprom_name);
70                         if (eeprom_file != null) {
71                                 monitor.set_filename(eeprom_name.getName());
72                                 FlushPending();
73                                 eeprom_pending = null;
74                         }
75                 }
76         }
77
78         boolean                 done;
79         boolean                 start;
80
81         void LogEeprom(AltosEeprom r) throws IOException {
82                 if (r.cmd != Altos.AO_LOG_INVALID) {
83                         String line = r.string();
84                         if (eeprom_file != null)
85                                 eeprom_file.write(line);
86                         else
87                                 eeprom_pending.add(line);
88                 }
89         }
90
91         void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
92                 boolean any_valid = false;
93                 boolean got_flight = false;
94
95                 int record_length = 8;
96
97                 state.set_serial(flights.config_data.serial);
98                 monitor.set_serial(flights.config_data.serial);
99
100                 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
101                         AltosEeprom r = eechunk.eeprom(i, log_format, state);
102
103                         if (r == null)
104                                 continue;
105
106                         record_length = r.record_length();
107
108                         r.update_state(state);
109
110                         if (!got_flight && state.flight != AltosLib.MISSING)
111                                 monitor.set_flight(state.flight);
112
113                         /* Monitor state transitions to update display */
114                         if (state.state != AltosLib.ao_flight_invalid &&
115                             state.state <= AltosLib.ao_flight_landed)
116                         {
117                                 if (state.state > Altos.ao_flight_pad)
118                                         want_file = true;
119                                 if (state.state == AltosLib.ao_flight_landed)
120                                         done = true;
121                         }
122
123                         if (state.gps != null)
124                                 want_file = true;
125
126                         if (r.valid) {
127                                 any_valid = true;
128                                 LogEeprom(r);
129                         }
130                 }
131                 if (!any_valid)
132                         done = true;
133
134                 CheckFile(false);
135         }
136         
137         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
138                 int                     block, state_block = 0;
139                 int                     log_format = flights.config_data.log_format;
140
141                 state = new AltosState();
142
143                 done = false;
144                 start = true;
145
146                 if (flights.config_data.serial < 0)
147                         throw new IOException("no serial number found");
148
149                 /* Reset per-capture variables */
150                 want_file = false;
151                 eeprom_file = null;
152                 eeprom_pending = new LinkedList<String>();
153
154                 /* Set serial number in the monitor dialog window */
155                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
156
157                 state_block = log.start_block;
158                 for (block = log.start_block; !done && block < log.end_block; block++) {
159                         monitor.set_value(state.state_name(),
160                                           state.state,
161                                           block - state_block,
162                                           block - log.start_block);
163
164                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
165
166                         /*
167                          * Guess what kind of data is there if the device
168                          * didn't tell us
169                          */
170
171                         if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
172                                 if (block == log.start_block) {
173                                         if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
174                                                 log_format = Altos.AO_LOG_FORMAT_FULL;
175                                         else
176                                                 log_format = Altos.AO_LOG_FORMAT_TINY;
177                                 }
178                         }
179
180                         CaptureEeprom (eechunk, log_format);
181                 }
182                 CheckFile(true);
183                 if (eeprom_file != null) {
184                         eeprom_file.flush();
185                         eeprom_file.close();
186                 }
187         }
188
189         public void run () {
190                 try {
191                         boolean failed = false;
192                         if (remote)
193                                 serial_line.start_remote();
194
195                         for (AltosEepromLog log : flights) {
196                                 parse_exception = null;
197                                 if (log.selected) {
198                                         monitor.reset();
199                                         try {
200                                                 CaptureLog(log);
201                                         } catch (ParseException e) {
202                                                 parse_exception = e;
203                                         }
204                                 }
205                                 if (parse_exception != null) {
206                                         failed = true;
207                                         monitor.show_message(String.format("Flight %d download error\n%s\nValid log data saved",
208                                                                            log.flight,
209                                                                            parse_exception.getMessage()),
210                                                              serial_line.device.toShortString(),
211                                                              AltosEepromMonitor.WARNING_MESSAGE);
212                                 }
213                         }
214                         success = !failed;
215                 } catch (IOException ee) {
216                         monitor.show_message(ee.getLocalizedMessage(),
217                                              serial_line.device.toShortString(),
218                                              AltosEepromMonitor.ERROR_MESSAGE);
219                 } catch (InterruptedException ie) {
220                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
221                                                            serial_line.device.toShortString()),
222                                              "Connection Interrupted",
223                                              AltosEepromMonitor.ERROR_MESSAGE);
224                 } catch (TimeoutException te) {
225                         monitor.show_message(String.format("Connection to \"%s\" failed",
226                                                            serial_line.device.toShortString()),
227                                              "Connection Failed",
228                                              AltosEepromMonitor.ERROR_MESSAGE);
229                 } finally {
230                         if (remote) {
231                                 try {
232                                         serial_line.stop_remote();
233                                 } catch (InterruptedException ie) {
234                                 }
235                         }
236                         serial_line.flush_output();
237                 }
238                 monitor.done(success);
239         }
240
241         public void start() {
242                 eeprom_thread = new Thread(this);
243                 eeprom_thread.start();
244         }
245
246         public AltosEepromDownload(AltosEepromMonitor given_monitor,
247                                    AltosSerial given_serial_line,
248                                    boolean given_remote,
249                                    AltosEepromList given_flights) {
250
251                 monitor = given_monitor;
252                 serial_line = given_serial_line;
253                 remote = given_remote;
254                 flights = given_flights;
255                 success = false;
256
257                 monitor.set_states(Altos.ao_flight_boost, Altos.ao_flight_landed);
258
259                 monitor.set_thread(eeprom_thread);
260                 monitor.start();
261         }
262 }