altoslib: Add set_config_data to AltosState
[fw/altos] / altoslib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 public class AltosEepromDownload implements Runnable {
27
28         AltosLink               link;
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         String                  parse_errors;
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         int                     prev_state;
80         int                     state_block;
81
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);
87                         else
88                                 eeprom_pending.add(line);
89                 }
90         }
91
92         void LogError(String error) {
93                 if (parse_errors != null)
94                         parse_errors.concat(error.concat("\n"));
95                 else
96                         parse_errors = error;
97         }
98
99         void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException, ParseException {
100                 boolean any_valid = false;
101                 boolean got_flight = false;
102
103                 int record_length = 8;
104
105                 state.set_serial(flights.config_data.serial);
106                 monitor.set_serial(flights.config_data.serial);
107
108                 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
109                         AltosEeprom r = null;
110
111                         try {
112                                 r = eechunk.eeprom(i, log_format, state);
113                         } catch (ParseException pe) {
114                                 LogError(pe.getMessage());
115                                 r = null;
116                         }
117
118                         if (r == null)
119                                 continue;
120
121                         record_length = r.record_length();
122
123                         r.update_state(state);
124
125                         if (!got_flight && state.flight != AltosLib.MISSING)
126                                 monitor.set_flight(state.flight);
127
128                         /* Monitor state transitions to update display */
129                         if (state.state() != AltosLib.ao_flight_invalid &&
130                             state.state() <= AltosLib.ao_flight_landed)
131                         {
132                                 if (state.state() > AltosLib.ao_flight_pad)
133                                         want_file = true;
134                                 if (state.state() == AltosLib.ao_flight_landed)
135                                         done = true;
136                         }
137
138                         if (state.gps != null)
139                                 want_file = true;
140
141                         if (r.valid) {
142                                 any_valid = true;
143                                 LogEeprom(r);
144                         }
145                 }
146                 if (!any_valid)
147                         done = true;
148
149                 CheckFile(false);
150         }
151
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;
155
156                 state = new AltosState();
157
158                 done = false;
159
160                 if (flights.config_data.serial < 0)
161                         throw new IOException("no serial number found");
162
163                 /* Reset per-capture variables */
164                 want_file = false;
165                 eeprom_pending = new LinkedList<String>();
166
167                 /* Set serial number in the monitor dialog window */
168                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
169
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);
174
175                         /*
176                          * Guess what kind of data is there if the device
177                          * didn't tell us
178                          */
179
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;
184                                         else
185                                                 log_format = AltosLib.AO_LOG_FORMAT_TINY;
186                                 }
187                         }
188
189                         CaptureEeprom (eechunk, log_format);
190
191                         if (state.state() != prev_state && state.state() != AltosLib.ao_flight_invalid) {
192                                 state_block = block;
193                                 prev_state = state.state();
194                         }
195
196                         monitor.set_value(state.state_name(),
197                                           state.state(),
198                                           block - state_block,
199                                           block - log.start_block);
200                 }
201                 CheckFile(true);
202         }
203
204         public void run () {
205                 try {
206                         boolean failed = false;
207                         if (remote)
208                                 link.start_remote();
209
210                         for (AltosEepromLog log : flights) {
211                                 parse_errors = null;
212                                 if (log.selected) {
213                                         monitor.reset();
214                                         eeprom_file = null;
215                                         try {
216                                                 CaptureLog(log);
217                                         } catch (ParseException e) {
218                                                 LogError(e.getMessage());
219                                         }
220                                         if (eeprom_file != null) {
221                                                 eeprom_file.flush();
222                                                 eeprom_file.close();
223                                         }
224                                 }
225                                 if (parse_errors != null) {
226                                         failed = true;
227                                         monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
228                                                                            log.flight,
229                                                                            parse_errors),
230                                                              link.name,
231                                                              AltosEepromMonitor.WARNING_MESSAGE);
232                                 }
233                         }
234                         success = !failed;
235                 } catch (IOException ee) {
236                         monitor.show_message(ee.getLocalizedMessage(),
237                                              link.name,
238                                              AltosEepromMonitor.ERROR_MESSAGE);
239                 } catch (InterruptedException ie) {
240                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
241                                                            link.name),
242                                              "Connection Interrupted",
243                                              AltosEepromMonitor.ERROR_MESSAGE);
244                 } catch (TimeoutException te) {
245                         monitor.show_message(String.format("Connection to \"%s\" failed",
246                                                            link.name),
247                                              "Connection Failed",
248                                              AltosEepromMonitor.ERROR_MESSAGE);
249                 } finally {
250                         if (remote) {
251                                 try {
252                                         link.stop_remote();
253                                 } catch (InterruptedException ie) {
254                                 }
255                         }
256                         link.flush_output();
257                 }
258                 monitor.done(success);
259         }
260
261         public void start() {
262                 eeprom_thread = new Thread(this);
263                 monitor.set_thread(eeprom_thread);
264                 eeprom_thread.start();
265         }
266
267         public AltosEepromDownload(AltosEepromMonitor given_monitor,
268                                    AltosLink given_link,
269                                    boolean given_remote,
270                                    AltosEepromList given_flights) {
271
272                 monitor = given_monitor;
273                 link = given_link;
274                 remote = given_remote;
275                 flights = given_flights;
276                 success = false;
277
278                 if (flights.config_data.log_has_state())
279                         monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
280                 else
281                         monitor.set_states(AltosLib.ao_flight_invalid, AltosLib.ao_flight_invalid);
282
283                 monitor.start();
284         }
285 }