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