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