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