Merge branch 'master' into new-state
[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_1.*;
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(16);
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                                 if (r.cmd != AltosLib.AO_LOG_INVALID)
293                                         any_valid = true;
294                                 LogMega(r);
295                         } catch (ParseException pe) {
296                                 if (parse_exception == null)
297                                         parse_exception = pe;
298                         }
299                 }
300                 if (!any_valid)
301                         done = true;
302
303                 CheckFile(false);
304         }
305         
306         void LogMini(AltosEepromMini r) throws IOException {
307                 if (r.cmd != Altos.AO_LOG_INVALID) {
308                         String log_line = String.format("%c %4x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
309                                                         r.cmd, r.tick,
310                                                         r.data8[0], r.data8[1], r.data8[2], r.data8[3],
311                                                         r.data8[4], r.data8[5], r.data8[6], r.data8[7],
312                                                         r.data8[8], r.data8[9], r.data8[10], r.data8[11]);
313                         if (eeprom_file != null)
314                                 eeprom_file.write(log_line);
315                         else
316                                 eeprom_pending.add(log_line);
317                 }
318         }
319
320         void CaptureMini(AltosEepromChunk eechunk) throws IOException {
321                 boolean any_valid = false;
322
323                 extension = "mini";
324                 set_serial(flights.config_data.serial);
325                 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += AltosEepromMini.record_length) {
326                         try {
327                                 AltosEepromMini r = new AltosEepromMini(eechunk, i);
328                                 if (r.cmd == Altos.AO_LOG_FLIGHT)
329                                         set_flight(r.data16(0));
330
331                                 /* Monitor state transitions to update display */
332                                 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) <= Altos.ao_flight_landed) {
333                                         state = r.data16(0);
334                                         if (state > Altos.ao_flight_pad)
335                                                 want_file = true;
336                                 }
337
338                                 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) == Altos.ao_flight_landed)
339                                         done = true;
340                                 any_valid = true;
341                                 LogMini(r);
342                         } catch (ParseException pe) {
343                                 if (parse_exception == null)
344                                         parse_exception = pe;
345                         }
346                 }
347                 if (!any_valid)
348                         done = true;
349
350                 CheckFile(false);
351         }
352         
353         void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
354                 
355         }
356
357         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
358                 int                     block, state_block = 0;
359                 int                     log_format = flights.config_data.log_format;
360
361                 state = 0;
362                 done = false;
363                 start = true;
364
365                 if (flights.config_data.serial < 0)
366                         throw new IOException("no serial number found");
367
368                 /* Reset per-capture variables */
369                 flight = 0;
370                 year = 0;
371                 month = 0;
372                 day = 0;
373                 want_file = false;
374                 eeprom_file = null;
375                 eeprom_pending = new LinkedList<String>();
376
377                 /* Set serial number in the monitor dialog window */
378                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
379
380                 state = 0; state_block = log.start_block;
381                 for (block = log.start_block; !done && block < log.end_block; block++) {
382                         monitor.set_value(AltosLib.state_name(state), state, block - state_block, block - log.start_block);
383
384                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
385
386                         /*
387                          * Guess what kind of data is there if the device
388                          * didn't tell us
389                          */
390
391                         if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
392                                 if (block == log.start_block) {
393                                         if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
394                                                 log_format = Altos.AO_LOG_FORMAT_FULL;
395                                         else
396                                                 log_format = Altos.AO_LOG_FORMAT_TINY;
397                                 }
398                         }
399
400                         switch (log_format) {
401                         case AltosLib.AO_LOG_FORMAT_FULL:
402                                 extension = "eeprom";
403                                 CaptureFull(eechunk);
404                                 break;
405                         case AltosLib.AO_LOG_FORMAT_TINY:
406                                 extension = "eeprom";
407                                 CaptureTiny(eechunk);
408                                 break;
409                         case AltosLib.AO_LOG_FORMAT_TELEMETRY:
410                                 extension = "telem";
411                                 CaptureTelemetry(eechunk);
412                                 break;
413                         case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
414                                 extension = "science";
415                                 CaptureTeleScience(eechunk);
416                                 break;
417                         case AltosLib.AO_LOG_FORMAT_TELEMEGA:
418                                 extension = "mega";
419                                 CaptureMega(eechunk);
420                                 break;
421                         case AltosLib.AO_LOG_FORMAT_EASYMINI:
422                         case AltosLib.AO_LOG_FORMAT_TELEMINI:
423                                 extension = "eeprom";
424                                 CaptureMini(eechunk);
425                                 break;
426                         }
427                 }
428                 CheckFile(true);
429                 if (eeprom_file != null) {
430                         eeprom_file.flush();
431                         eeprom_file.close();
432                 }
433         }
434
435         private void show_message_internal(String message, String title, int message_type) {
436                 JOptionPane.showMessageDialog(frame,
437                                               message,
438                                               title,
439                                               message_type);
440         }
441
442         private void show_message(String in_message, String in_title, int in_message_type) {
443                 final String message = in_message;
444                 final String title = in_title;
445                 final int message_type = in_message_type;
446                 Runnable r = new Runnable() {
447                                 public void run() {
448                                         try {
449                                                 show_message_internal(message, title, message_type);
450                                         } catch (Exception ex) {
451                                         }
452                                 }
453                         };
454                 SwingUtilities.invokeLater(r);
455         }
456
457         public void run () {
458                 try {
459                         boolean failed = false;
460                         if (remote)
461                                 serial_line.start_remote();
462
463                         for (AltosEepromLog log : flights) {
464                                 parse_exception = null;
465                                 if (log.selected) {
466                                         monitor.reset();
467                                         CaptureLog(log);
468                                 }
469                                 if (parse_exception != null) {
470                                         failed = true;
471                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
472                                                                    log.flight,
473                                                                    parse_exception.getMessage()),
474                                                      serial_line.device.toShortString(),
475                                                      JOptionPane.WARNING_MESSAGE);
476                                 }
477                         }
478                         success = !failed;
479                 } catch (IOException ee) {
480                         show_message(ee.getLocalizedMessage(),
481                                      serial_line.device.toShortString(),
482                                      JOptionPane.ERROR_MESSAGE);
483                 } catch (InterruptedException ie) {
484                         System.out.printf("download interrupted\n");
485                 } catch (TimeoutException te) {
486                         show_message(String.format("Connection to \"%s\" failed",
487                                                    serial_line.device.toShortString()),
488                                      "Connection Failed",
489                                      JOptionPane.ERROR_MESSAGE);
490                 } finally {
491                         if (remote) {
492                                 try {
493                                         serial_line.stop_remote();
494                                 } catch (InterruptedException ie) {
495                                 }
496                         }
497                         serial_line.flush_output();
498                 }
499                 monitor.done();
500                 if (listener != null) {
501                         Runnable r = new Runnable() {
502                                         public void run() {
503                                                 try {
504                                                         listener.actionPerformed(new ActionEvent(this,
505                                                                                                  success ? 1 : 0,
506                                                                                                  "download"));
507                                                 } catch (Exception ex) {
508                                                 }
509                                         }
510                                 };
511                         SwingUtilities.invokeLater(r);
512                 }
513         }
514
515         public void start() {
516                 eeprom_thread = new Thread(this);
517                 eeprom_thread.start();
518         }
519
520         public void addActionListener(ActionListener l) {
521                 listener = l;
522         }
523
524         public AltosEepromDownload(JFrame given_frame,
525                                    AltosSerial given_serial_line,
526                                    boolean given_remote,
527                                    AltosEepromList given_flights) {
528
529                 frame = given_frame;
530                 serial_line = given_serial_line;
531                 serial_line.set_frame(frame);
532                 remote = given_remote;
533                 flights = given_flights;
534                 success = false;
535
536                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
537                 monitor.addActionListener(new ActionListener() {
538                                 public void actionPerformed(ActionEvent e) {
539                                         if (eeprom_thread != null)
540                                                 eeprom_thread.interrupt();
541                                 }
542                         });
543         }
544 }