Bump java library versions
[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; 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 org.altusmetrum.altoslib_6;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 public class AltosEepromDownload implements Runnable {
26
27         AltosLink               link;
28         boolean                 remote;
29         Thread                  eeprom_thread;
30         AltosEepromMonitor      monitor;
31
32         boolean                 want_file;
33         FileWriter              eeprom_file;
34         LinkedList<String>      eeprom_pending;
35
36         AltosEepromList         flights;
37         boolean                 success;
38         ParseException          parse_exception;
39         AltosState              state;
40
41         private void FlushPending() throws IOException {
42                 for (String s : flights.config_data) {
43                         eeprom_file.write(s);
44                         eeprom_file.write('\n');
45                 }
46
47                 for (String s : eeprom_pending)
48                         eeprom_file.write(s);
49         }
50
51         private void CheckFile(boolean force) throws IOException {
52                 if (eeprom_file != null)
53                         return;
54                 if (force || (state.flight != 0 && want_file)) {
55                         AltosFile               eeprom_name;
56                         AltosGPS                gps = state.gps;
57
58                         if (gps != null &&
59                             gps.year != AltosLib.MISSING &&
60                             gps.month != AltosLib.MISSING &&
61                             gps.day != AltosLib.MISSING)
62                         {
63                                 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
64                                                             state.serial, state.flight, "eeprom");
65                         } else
66                                 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
67
68                         eeprom_file = new FileWriter(eeprom_name);
69                         if (eeprom_file != null) {
70                                 monitor.set_filename(eeprom_name.getName());
71                                 FlushPending();
72                                 eeprom_pending = null;
73                         }
74                 }
75         }
76
77         boolean                 done;
78         int                     prev_state;
79         int                     state_block;
80
81         void LogEeprom(AltosEeprom r) throws IOException {
82                 if (r.cmd != AltosLib.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 > AltosLib.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
145                 if (flights.config_data.serial < 0)
146                         throw new IOException("no serial number found");
147
148                 /* Reset per-capture variables */
149                 want_file = false;
150                 eeprom_file = null;
151                 eeprom_pending = new LinkedList<String>();
152
153                 /* Set serial number in the monitor dialog window */
154                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
155
156                 state_block = log.start_block;
157                 prev_state = AltosLib.ao_flight_startup;
158                 for (block = log.start_block; !done && block < log.end_block; block++) {
159                         AltosEepromChunk        eechunk = new AltosEepromChunk(link, block, block == log.start_block);
160
161                         /*
162                          * Guess what kind of data is there if the device
163                          * didn't tell us
164                          */
165
166                         if (log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN) {
167                                 if (block == log.start_block) {
168                                         if (eechunk.data(0) == AltosLib.AO_LOG_FLIGHT)
169                                                 log_format = AltosLib.AO_LOG_FORMAT_FULL;
170                                         else
171                                                 log_format = AltosLib.AO_LOG_FORMAT_TINY;
172                                 }
173                         }
174
175                         CaptureEeprom (eechunk, log_format);
176
177                         if (state.state != prev_state && state.state != AltosLib.ao_flight_invalid) {
178                                 state_block = block;
179                                 prev_state = state.state;
180                         }
181
182                         monitor.set_value(state.state_name(),
183                                           state.state,
184                                           block - state_block,
185                                           block - log.start_block);
186                 }
187                 CheckFile(true);
188                 if (eeprom_file != null) {
189                         eeprom_file.flush();
190                         eeprom_file.close();
191                 }
192         }
193
194         public void run () {
195                 try {
196                         boolean failed = false;
197                         if (remote)
198                                 link.start_remote();
199
200                         for (AltosEepromLog log : flights) {
201                                 parse_exception = null;
202                                 if (log.selected) {
203                                         monitor.reset();
204                                         try {
205                                                 CaptureLog(log);
206                                         } catch (ParseException e) {
207                                                 parse_exception = e;
208                                         }
209                                 }
210                                 if (parse_exception != null) {
211                                         failed = true;
212                                         monitor.show_message(String.format("Flight %d download error\n%s\nValid log data saved",
213                                                                            log.flight,
214                                                                            parse_exception.getMessage()),
215                                                              link.name,
216                                                              AltosEepromMonitor.WARNING_MESSAGE);
217                                 }
218                         }
219                         success = !failed;
220                 } catch (IOException ee) {
221                         monitor.show_message(ee.getLocalizedMessage(),
222                                              link.name,
223                                              AltosEepromMonitor.ERROR_MESSAGE);
224                 } catch (InterruptedException ie) {
225                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
226                                                            link.name),
227                                              "Connection Interrupted",
228                                              AltosEepromMonitor.ERROR_MESSAGE);
229                 } catch (TimeoutException te) {
230                         monitor.show_message(String.format("Connection to \"%s\" failed",
231                                                            link.name),
232                                              "Connection Failed",
233                                              AltosEepromMonitor.ERROR_MESSAGE);
234                 } finally {
235                         if (remote) {
236                                 try {
237                                         link.stop_remote();
238                                 } catch (InterruptedException ie) {
239                                 }
240                         }
241                         link.flush_output();
242                 }
243                 monitor.done(success);
244         }
245
246         public void start() {
247                 eeprom_thread = new Thread(this);
248                 monitor.set_thread(eeprom_thread);
249                 eeprom_thread.start();
250         }
251
252         public AltosEepromDownload(AltosEepromMonitor given_monitor,
253                                    AltosLink given_link,
254                                    boolean given_remote,
255                                    AltosEepromList given_flights) {
256
257                 monitor = given_monitor;
258                 link = given_link;
259                 remote = given_remote;
260                 flights = given_flights;
261                 success = false;
262
263                 if (flights.config_data.log_has_state())
264                         monitor.set_states(AltosLib.ao_flight_boost, AltosLib.ao_flight_landed);
265                 else
266                         monitor.set_states(AltosLib.ao_flight_invalid, AltosLib.ao_flight_invalid);
267
268                 monitor.start();
269         }
270 }