altosui: Stop downloading eeprom data on a block full of invalid data
[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.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30 import org.altusmetrum.AltosLib.*;
31
32 import libaltosJNI.*;
33
34 public class AltosEepromDownload implements Runnable {
35
36         JFrame                  frame;
37         AltosSerial             serial_line;
38         boolean                 remote;
39         Thread                  eeprom_thread;
40         AltosEepromMonitor      monitor;
41
42         int                     flight;
43         int                     serial;
44         int                     year, month, day;
45         boolean                 want_file;
46         FileWriter              eeprom_file;
47         LinkedList<String>      eeprom_pending;
48
49         AltosEepromList         flights;
50         ActionListener          listener;
51         boolean                 success;
52         ParseException          parse_exception;
53         String                  extension;
54
55         private void FlushPending() throws IOException {
56                 for (String s : flights.config_data) {
57                         eeprom_file.write(s);
58                         eeprom_file.write('\n');
59                 }
60
61                 for (String s : eeprom_pending)
62                         eeprom_file.write(s);
63         }
64
65         private void CheckFile(boolean force) throws IOException {
66                 if (eeprom_file != null)
67                         return;
68                 if (force || (flight != 0 && want_file)) {
69                         AltosFile               eeprom_name;
70
71                         if (extension == null)
72                                 extension = "data";
73                         if (year != 0 && month != 0 && day != 0)
74                                 eeprom_name = new AltosFile(year, month, day, serial, flight, extension);
75                         else
76                                 eeprom_name = new AltosFile(serial, flight, extension);
77
78                         eeprom_file = new FileWriter(eeprom_name);
79                         if (eeprom_file != null) {
80                                 monitor.set_file(eeprom_name.getName());
81                                 FlushPending();
82                                 eeprom_pending = null;
83                         }
84                 }
85         }
86
87         void Log(AltosEepromRecord r) throws IOException {
88                 if (r.cmd != Altos.AO_LOG_INVALID) {
89                         String log_line = String.format("%c %4x %4x %4x\n",
90                                                         r.cmd, r.tick, r.a, r.b);
91                         if (eeprom_file != null)
92                                 eeprom_file.write(log_line);
93                         else
94                                 eeprom_pending.add(log_line);
95                 }
96         }
97
98         void set_serial(int in_serial) {
99                 serial = in_serial;
100                 monitor.set_serial(serial);
101         }
102
103         void set_flight(int in_flight) {
104                 flight = in_flight;
105                 monitor.set_flight(flight);
106         }
107                 
108         boolean                 done;
109         int                     state;
110
111         void CaptureFull(AltosEepromChunk eechunk) throws IOException {
112                 boolean any_valid = false;
113
114                 extension = "eeprom";
115                 set_serial(flights.config_data.serial);
116                 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromRecord.record_length) {
117                         try {
118                                 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
119                                 if (r.cmd == Altos.AO_LOG_FLIGHT)
120                                         set_flight(r.b);
121
122                                 /* Monitor state transitions to update display */
123                                 if (r.cmd == Altos.AO_LOG_STATE && r.a <= Altos.ao_flight_landed) {
124                                         state = r.a;
125                                         if (state > Altos.ao_flight_pad)
126                                                 want_file = true;
127                                 }
128
129                                 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
130                                         year = 2000 + (r.a & 0xff);
131                                         month = (r.a >> 8) & 0xff;
132                                         day = (r.b & 0xff);
133                                         want_file = true;
134                                 }
135                                 if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
136                                         done = true;
137                                 if (r.cmd != AltosLib.AO_LOG_INVALID)
138                                         any_valid = true;
139                                 Log(r);
140                         } catch (ParseException pe) {
141                                 if (parse_exception == null)
142                                         parse_exception = pe;
143                         }
144                 }
145
146                 if (!any_valid)
147                         done = true;
148
149                 CheckFile(false);
150         }
151
152         boolean start;
153         int     tiny_tick;
154
155         void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
156                 boolean any_valid = false;
157
158                 extension = "eeprom";
159                 set_serial(flights.config_data.serial);
160                 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
161                         int                     v = eechunk.data16(i);
162                         AltosEepromRecord       r;
163
164                         if (i == 0 && start) {
165                                 tiny_tick = 0;
166                                 start = false;
167                                 set_flight(v);
168                                 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
169                                 any_valid = true;
170                         } else {
171                                 int     s = v ^ 0x8000;
172
173                                 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
174                                         state = s;
175                                         r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, state, 0);
176                                         if (state == Altos.ao_flight_landed)
177                                                 done = true;
178                                         state = s;
179                                         any_valid = true;
180                                 } else {
181                                         if (v != 0xffff)
182                                                 any_valid = true;
183
184                                         r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
185
186                                         /*
187                                          * The flight software records ascent data every 100ms, and descent
188                                          * data every 1s.
189                                          */
190                                         if (state < Altos.ao_flight_drogue)
191                                                 tiny_tick += 10;
192                                         else
193                                                 tiny_tick += 100;
194                                 }
195                         }
196                         Log(r);
197                 }
198                 CheckFile(false);
199                 if (!any_valid)
200                         done = true;
201         }
202
203         void LogTeleScience(AltosEepromTeleScience r) throws IOException {
204                 if (r.type != Altos.AO_LOG_INVALID) {
205                         String log_line = String.format("%c %4x %4x %d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
206                                                         r.type, r.tick, r.tm_tick, r.tm_state,
207                                                         r.data[0], r.data[1], r.data[2], r.data[3], 
208                                                         r.data[4], r.data[5], r.data[6], r.data[7], 
209                                                         r.data[8], r.data[9], r.data[10], r.data[11]);
210                         if (eeprom_file != null)
211                                 eeprom_file.write(log_line);
212                         else
213                                 eeprom_pending.add(log_line);
214                 }
215         }
216         
217         boolean telescience_start;
218
219         void CaptureTeleScience (AltosEepromChunk eechunk) throws IOException {
220                 boolean any_valid = false;
221
222                 extension = "science";
223                 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromTeleScience.record_length) {
224                         try {
225                                 AltosEepromTeleScience r = new AltosEepromTeleScience(eechunk, i);
226                                 if (r.type == AltosEepromTeleScience.AO_LOG_TELESCIENCE_START) {
227                                         if (telescience_start) {
228                                                 done = true;
229                                                 break;
230                                         }
231                                         set_serial(r.data[0]);
232                                         set_flight(r.data[1]);
233                                         telescience_start = true;
234                                 } else {
235                                         if (!telescience_start)
236                                                 break;
237                                 }
238                                 state = r.tm_state;
239                                 want_file =true;
240                                 any_valid = true;
241                                 LogTeleScience(r);
242                         } catch (ParseException pe) {
243                                 if (parse_exception == null)
244                                         parse_exception = pe;
245                         }
246                 }
247
248                 CheckFile(false);
249                 if (!any_valid)
250                         done = true;
251         }
252
253         void LogMega(AltosEepromMega r) throws IOException {
254                 if (r.cmd != Altos.AO_LOG_INVALID) {
255                         String log_line = String.format("%c %4x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
256                                                         r.cmd, r.tick,
257                                                         r.data8[0], r.data8[1], r.data8[2], r.data8[3],
258                                                         r.data8[4], r.data8[5], r.data8[6], r.data8[7],
259                                                         r.data8[8], r.data8[9], r.data8[10], r.data8[11],
260                                                         r.data8[12], r.data8[13], r.data8[14], r.data8[15],
261                                                         r.data8[16], r.data8[17], r.data8[18], r.data8[19],
262                                                         r.data8[20], r.data8[21], r.data8[22], r.data8[23],
263                                                         r.data8[24], r.data8[25], r.data8[26], r.data8[27]);
264                         if (eeprom_file != null)
265                                 eeprom_file.write(log_line);
266                         else
267                                 eeprom_pending.add(log_line);
268                 }
269         }
270
271         void CaptureMega(AltosEepromChunk eechunk) throws IOException {
272                 boolean any_valid = false;
273
274                 extension = "mega";
275                 set_serial(flights.config_data.serial);
276                 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromMega.record_length) {
277                         try {
278                                 AltosEepromMega r = new AltosEepromMega(eechunk, i);
279                                 if (r.cmd == Altos.AO_LOG_FLIGHT)
280                                         set_flight(r.data16(0));
281
282                                 /* Monitor state transitions to update display */
283                                 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) <= Altos.ao_flight_landed) {
284                                         state = r.data16(0);
285                                         if (state > Altos.ao_flight_pad)
286                                                 want_file = true;
287                                 }
288
289                                 if (r.cmd == Altos.AO_LOG_GPS_TIME) {
290                                         year = 2000 + r.data8(14);
291                                         month = r.data8(15);
292                                         day = r.data8(14);
293                                         want_file = true;
294                                 }
295
296                                 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) == Altos.ao_flight_landed)
297                                         done = true;
298                                 any_valid = true;
299                                 LogMega(r);
300                         } catch (ParseException pe) {
301                                 if (parse_exception == null)
302                                         parse_exception = pe;
303                         }
304                 }
305                 if (!any_valid)
306                         done = true;
307
308                 CheckFile(false);
309         }
310         
311         void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
312                 
313         }
314
315         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
316                 int                     block, state_block = 0;
317                 int                     log_format = flights.config_data.log_format;
318
319                 state = 0;
320                 done = false;
321                 start = true;
322
323                 if (flights.config_data.serial == 0)
324                         throw new IOException("no serial number found");
325
326                 /* Reset per-capture variables */
327                 flight = 0;
328                 year = 0;
329                 month = 0;
330                 day = 0;
331                 want_file = false;
332                 eeprom_file = null;
333                 eeprom_pending = new LinkedList<String>();
334
335                 /* Set serial number in the monitor dialog window */
336                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
337
338                 state = 0; state_block = log.start_block;
339                 for (block = log.start_block; !done && block < log.end_block; block++) {
340                         monitor.set_value(AltosLib.state_name(state), state, block - state_block);
341
342                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
343
344                         /*
345                          * Guess what kind of data is there if the device
346                          * didn't tell us
347                          */
348
349                         if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
350                                 if (block == log.start_block) {
351                                         if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
352                                                 log_format = Altos.AO_LOG_FORMAT_FULL;
353                                         else
354                                                 log_format = Altos.AO_LOG_FORMAT_TINY;
355                                 }
356                         }
357
358                         switch (log_format) {
359                         case AltosLib.AO_LOG_FORMAT_FULL:
360                                 extension = "eeprom";
361                                 CaptureFull(eechunk);
362                                 break;
363                         case AltosLib.AO_LOG_FORMAT_TINY:
364                                 extension = "eeprom";
365                                 CaptureTiny(eechunk);
366                                 break;
367                         case AltosLib.AO_LOG_FORMAT_TELEMETRY:
368                                 extension = "telem";
369                                 CaptureTelemetry(eechunk);
370                                 break;
371                         case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
372                                 extension = "science";
373                                 CaptureTeleScience(eechunk);
374                                 break;
375                         case AltosLib.AO_LOG_FORMAT_MEGAMETRUM:
376                                 extension = "mega";
377                                 CaptureMega(eechunk);
378                         }
379                 }
380                 CheckFile(true);
381                 if (eeprom_file != null) {
382                         eeprom_file.flush();
383                         eeprom_file.close();
384                 }
385         }
386
387         private void show_message_internal(String message, String title, int message_type) {
388                 JOptionPane.showMessageDialog(frame,
389                                               message,
390                                               title,
391                                               message_type);
392         }
393
394         private void show_message(String in_message, String in_title, int in_message_type) {
395                 final String message = in_message;
396                 final String title = in_title;
397                 final int message_type = in_message_type;
398                 Runnable r = new Runnable() {
399                                 public void run() {
400                                         try {
401                                                 show_message_internal(message, title, message_type);
402                                         } catch (Exception ex) {
403                                         }
404                                 }
405                         };
406                 SwingUtilities.invokeLater(r);
407         }
408
409         public void run () {
410                 try {
411                         boolean failed = false;
412                         if (remote)
413                                 serial_line.start_remote();
414
415                         for (AltosEepromLog log : flights) {
416                                 parse_exception = null;
417                                 if (log.selected) {
418                                         monitor.reset();
419                                         CaptureLog(log);
420                                 }
421                                 if (parse_exception != null) {
422                                         failed = true;
423                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
424                                                                    log.flight,
425                                                                    parse_exception.getMessage()),
426                                                      serial_line.device.toShortString(),
427                                                      JOptionPane.WARNING_MESSAGE);
428                                 }
429                         }
430                         success = !failed;
431                 } catch (IOException ee) {
432                         show_message(ee.getLocalizedMessage(),
433                                      serial_line.device.toShortString(),
434                                      JOptionPane.ERROR_MESSAGE);
435                 } catch (InterruptedException ie) {
436                         System.out.printf("download interrupted\n");
437                 } catch (TimeoutException te) {
438                         show_message(String.format("Connection to \"%s\" failed",
439                                                    serial_line.device.toShortString()),
440                                      "Connection Failed",
441                                      JOptionPane.ERROR_MESSAGE);
442                 } finally {
443                         if (remote) {
444                                 try {
445                                         serial_line.stop_remote();
446                                 } catch (InterruptedException ie) {
447                                 }
448                         }
449                         serial_line.flush_output();
450                 }
451                 monitor.done();
452                 if (listener != null) {
453                         Runnable r = new Runnable() {
454                                         public void run() {
455                                                 try {
456                                                         listener.actionPerformed(new ActionEvent(this,
457                                                                                                  success ? 1 : 0,
458                                                                                                  "download"));
459                                                 } catch (Exception ex) {
460                                                 }
461                                         }
462                                 };
463                         SwingUtilities.invokeLater(r);
464                 }
465         }
466
467         public void start() {
468                 eeprom_thread = new Thread(this);
469                 eeprom_thread.start();
470         }
471
472         public void addActionListener(ActionListener l) {
473                 listener = l;
474         }
475
476         public AltosEepromDownload(JFrame given_frame,
477                                    AltosSerial given_serial_line,
478                                    boolean given_remote,
479                                    AltosEepromList given_flights) {
480
481                 frame = given_frame;
482                 serial_line = given_serial_line;
483                 serial_line.set_frame(frame);
484                 remote = given_remote;
485                 flights = given_flights;
486                 success = false;
487
488                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
489                 monitor.addActionListener(new ActionListener() {
490                                 public void actionPerformed(ActionEvent e) {
491                                         if (eeprom_thread != null)
492                                                 eeprom_thread.interrupt();
493                                 }
494                         });
495         }
496 }