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