altosui: Add support for downloading TeleMini/TeleNano flight logs
[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                     year, month, day;
43         boolean                 want_file;
44         FileWriter              eeprom_file;
45         LinkedList<String>      eeprom_pending;
46
47         AltosEepromList         flights;
48         ActionListener          listener;
49         boolean                 success;
50         ParseException          parse_exception;
51
52         private void FlushPending() throws IOException {
53                 for (String s : flights.config_data) {
54                         eeprom_file.write(s);
55                         eeprom_file.write('\n');
56                 }
57
58                 for (String s : eeprom_pending)
59                         eeprom_file.write(s);
60         }
61
62         private void CheckFile(boolean force) throws IOException {
63                 if (eeprom_file != null)
64                         return;
65                 if (force || (flight != 0 && want_file)) {
66                         AltosFile               eeprom_name;
67                         if (year != 0 && month != 0 && day != 0)
68                                 eeprom_name = new AltosFile(year, month, day, flights.config_data.serial, flight, "eeprom");
69                         else
70                                 eeprom_name = new AltosFile(flights.config_data.serial, flight, "eeprom");
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         static final int        log_full = 1;
93         static final int        log_tiny = 2;
94
95         boolean                 done;
96         int                     state;
97
98         void CaptureFull(AltosEepromChunk eechunk) throws IOException {
99                 AltosEepromBlock        eeblock = new AltosEepromBlock(eechunk);
100
101                 if (eeblock.has_flight) {
102                         flight = eeblock.flight;
103                         monitor.set_flight(flight);
104                 }
105                 if (eeblock.has_date) {
106                         year = eeblock.year;
107                         month = eeblock.month;
108                         day = eeblock.day;
109                         want_file = true;
110                 }
111
112                 if (eeblock.size() == 0 ||
113                     eeblock.has_state && eeblock.state == Altos.ao_flight_landed)
114                         done = true;
115
116                 /* Monitor state transitions to update display */
117                 if (eeblock.has_state) {
118                         if (eeblock.state > Altos.ao_flight_pad)
119                                 want_file = true;
120                         if (eeblock.state > state)
121                                 state = eeblock.state;
122                 }
123
124                 if (parse_exception == null && eeblock.parse_exception != null)
125                         parse_exception = eeblock.parse_exception;
126
127                 CheckFile(false);
128
129                 for (int record = 0; record < eeblock.size(); record++)
130                         Log(eeblock.get(record));
131         }
132
133         boolean start;
134         int     tiny_tick;
135
136         void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
137                 boolean some_reasonable_data = false;
138
139                 for (int i = 0; i < eechunk.data.length; i += 2) {
140                         int     v = eechunk.data16(i);
141
142                         if (i == 0 && start) {
143                                 tiny_tick = 0;
144                                 start = false;
145                                 flight = v;
146                                 Log(new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v));
147                                 some_reasonable_data = true;
148                         } else {
149                                 int     s = v ^ 0x8000;
150                                 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
151                                         Log(new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, s, 0));
152                                         if (s == Altos.ao_flight_landed) {
153                                                 done = true;
154                                                 break;
155                                         }
156                                         some_reasonable_data = true;
157                                 } else {
158                                         if (v != 0xffff)
159                                                 some_reasonable_data = true;
160                                         Log(new AltosEepromRecord(Altos.AO_LOG_HEIGHT, tiny_tick, v, 0));
161                                         if (state < Altos.ao_flight_drogue)
162                                                 tiny_tick += 10;
163                                         else
164                                                 tiny_tick += 100;
165                                 }
166                         }
167                 }
168                 CheckFile(false);
169                 if (!some_reasonable_data)
170                         done = true;
171         }
172
173         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
174                 int                     block, state_block = 0;
175                 int                     log_style = 0;
176
177                 state = 0;
178                 done = false;
179                 start = true;
180
181                 if (flights.config_data.serial == 0)
182                         throw new IOException("no serial number found");
183
184                 /* Reset per-capture variables */
185                 flight = 0;
186                 year = 0;
187                 month = 0;
188                 day = 0;
189                 want_file = false;
190                 eeprom_file = null;
191                 eeprom_pending = new LinkedList<String>();
192
193                 /* Set serial number in the monitor dialog window */
194                 monitor.set_serial(flights.config_data.serial);
195                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
196
197                 state = 0; state_block = log.start_block;
198                 for (block = log.start_block; !done && block < log.end_block; block++) {
199                         monitor.set_value(Altos.state_to_string[state], state, block - state_block);
200
201                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block);
202
203                         /*
204                          * Figure out what kind of data is there
205                          */
206
207                         if (block == log.start_block) {
208                                 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
209                                         log_style = log_full;
210                                 else
211                                         log_style = log_tiny;
212                         }
213
214                         switch (log_style) {
215                         case log_full:
216                                 CaptureFull(eechunk);
217                                 break;
218                         case log_tiny:
219                                 CaptureTiny(eechunk);
220                                 break;
221                         }
222                 }
223                 CheckFile(true);
224                 if (eeprom_file != null) {
225                         eeprom_file.flush();
226                         eeprom_file.close();
227                 }
228         }
229
230         private void show_message_internal(String message, String title, int message_type) {
231                 JOptionPane.showMessageDialog(frame,
232                                               message,
233                                               title,
234                                               message_type);
235         }
236
237         private void show_message(String in_message, String in_title, int in_message_type) {
238                 final String message = in_message;
239                 final String title = in_title;
240                 final int message_type = in_message_type;
241                 Runnable r = new Runnable() {
242                                 public void run() {
243                                         try {
244                                                 show_message_internal(message, title, message_type);
245                                         } catch (Exception ex) {
246                                         }
247                                 }
248                         };
249                 SwingUtilities.invokeLater(r);
250         }
251
252         public void run () {
253                 if (remote)
254                         serial_line.start_remote();
255
256                 try {
257                         boolean failed = false;
258                         for (AltosEepromLog log : flights) {
259                                 parse_exception = null;
260                                 if (log.download) {
261                                         monitor.reset();
262                                         CaptureLog(log);
263                                 }
264                                 if (parse_exception != null) {
265                                         failed = true;
266                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
267                                                                    log.flight,
268                                                                    parse_exception.getMessage()),
269                                                      serial_line.device.toShortString(),
270                                                      JOptionPane.WARNING_MESSAGE);
271                                 }
272                         }
273                         success = !failed;
274                 } catch (IOException ee) {
275                         show_message(ee.getLocalizedMessage(),
276                                      serial_line.device.toShortString(),
277                                      JOptionPane.ERROR_MESSAGE);
278                 } catch (InterruptedException ie) {
279                 } catch (TimeoutException te) {
280                         show_message(String.format("Connection to \"%s\" failed",
281                                                    serial_line.device.toShortString()),
282                                      "Connection Failed",
283                                      JOptionPane.ERROR_MESSAGE);
284                 }
285                 if (remote)
286                         serial_line.stop_remote();
287                 monitor.done();
288                 serial_line.flush_output();
289                 if (listener != null) {
290                         Runnable r = new Runnable() {
291                                         public void run() {
292                                                 try {
293                                                         listener.actionPerformed(new ActionEvent(this,
294                                                                                                  success ? 1 : 0,
295                                                                                                  "download"));
296                                                 } catch (Exception ex) {
297                                                 }
298                                         }
299                                 };
300                         SwingUtilities.invokeLater(r);
301                 }
302         }
303
304         public void start() {
305                 eeprom_thread = new Thread(this);
306                 eeprom_thread.start();
307         }
308
309         public void addActionListener(ActionListener l) {
310                 listener = l;
311         }
312
313         public AltosEepromDownload(JFrame given_frame,
314                                    AltosSerial given_serial_line,
315                                    boolean given_remote,
316                                    AltosEepromList given_flights) {
317
318                 frame = given_frame;
319                 serial_line = given_serial_line;
320                 remote = given_remote;
321                 flights = given_flights;
322                 success = false;
323
324                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
325                 monitor.addActionListener(new ActionListener() {
326                                 public void actionPerformed(ActionEvent e) {
327                                         if (eeprom_thread != null)
328                                                 eeprom_thread.interrupt();
329                                 }
330                         });
331         }
332 }