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