Merge remote-tracking branch 'origin/master'
[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_2.*;
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         boolean                 want_file;
37         FileWriter              eeprom_file;
38         LinkedList<String>      eeprom_pending;
39
40         AltosEepromList         flights;
41         ActionListener          listener;
42         boolean                 success;
43         ParseException          parse_exception;
44         AltosState              state;
45
46         private void FlushPending() throws IOException {
47                 for (String s : flights.config_data) {
48                         eeprom_file.write(s);
49                         eeprom_file.write('\n');
50                 }
51
52                 for (String s : eeprom_pending)
53                         eeprom_file.write(s);
54         }
55
56         private void CheckFile(boolean force) throws IOException {
57                 if (eeprom_file != null)
58                         return;
59                 if (force || (state.flight != 0 && want_file)) {
60                         AltosFile               eeprom_name;
61                         AltosGPS                gps = state.gps;
62
63                         if (gps != null &&
64                             gps.year != AltosLib.MISSING &&
65                             gps.month != AltosLib.MISSING &&
66                             gps.day != AltosLib.MISSING)
67                         {
68                                 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
69                                                             state.serial, state.flight, "eeprom");
70                         } else
71                                 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
72
73                         eeprom_file = new FileWriter(eeprom_name);
74                         if (eeprom_file != null) {
75                                 monitor.set_file(eeprom_name.getName());
76                                 FlushPending();
77                                 eeprom_pending = null;
78                         }
79                 }
80         }
81
82         boolean                 done;
83         boolean                 start;
84
85         void LogEeprom(AltosEeprom r) throws IOException {
86                 if (r.cmd != Altos.AO_LOG_INVALID) {
87                         String line = r.string();
88                         if (eeprom_file != null)
89                                 eeprom_file.write(line);
90                         else
91                                 eeprom_pending.add(line);
92                 }
93         }
94
95         void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException {
96                 boolean any_valid = false;
97                 boolean got_flight = false;
98
99                 int record_length = 8;
100
101                 state.set_serial(flights.config_data.serial);
102                 monitor.set_serial(flights.config_data.serial);
103
104                 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
105                         AltosEeprom r = eechunk.eeprom(i, log_format, state);
106
107                         if (r == null)
108                                 continue;
109
110                         record_length = r.record_length();
111
112                         r.update_state(state);
113
114                         if (!got_flight && state.flight != AltosLib.MISSING)
115                                 monitor.set_flight(state.flight);
116
117                         /* Monitor state transitions to update display */
118                         if (state.state != AltosLib.ao_flight_invalid &&
119                             state.state <= AltosLib.ao_flight_landed)
120                         {
121                                 if (state.state > Altos.ao_flight_pad)
122                                         want_file = true;
123                                 if (state.state == AltosLib.ao_flight_landed)
124                                         done = true;
125                         }
126
127                         if (state.gps != null)
128                                 want_file = true;
129
130                         if (r.valid) {
131                                 any_valid = true;
132                                 LogEeprom(r);
133                         }
134                 }
135                 if (!any_valid)
136                         done = true;
137
138                 CheckFile(false);
139         }
140         
141         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
142                 int                     block, state_block = 0;
143                 int                     log_format = flights.config_data.log_format;
144
145                 state = new AltosState();
146
147                 done = false;
148                 start = true;
149
150                 if (flights.config_data.serial < 0)
151                         throw new IOException("no serial number found");
152
153                 /* Reset per-capture variables */
154                 want_file = false;
155                 eeprom_file = null;
156                 eeprom_pending = new LinkedList<String>();
157
158                 /* Set serial number in the monitor dialog window */
159                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
160
161                 state_block = log.start_block;
162                 for (block = log.start_block; !done && block < log.end_block; block++) {
163                         monitor.set_value(state.state_name(),
164                                           state.state,
165                                           block - state_block,
166                                           block - log.start_block);
167
168                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
169
170                         /*
171                          * Guess what kind of data is there if the device
172                          * didn't tell us
173                          */
174
175                         if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
176                                 if (block == log.start_block) {
177                                         if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
178                                                 log_format = Altos.AO_LOG_FORMAT_FULL;
179                                         else
180                                                 log_format = Altos.AO_LOG_FORMAT_TINY;
181                                 }
182                         }
183
184                         CaptureEeprom (eechunk, log_format);
185                 }
186                 CheckFile(true);
187                 if (eeprom_file != null) {
188                         eeprom_file.flush();
189                         eeprom_file.close();
190                 }
191         }
192
193         private void show_message_internal(String message, String title, int message_type) {
194                 JOptionPane.showMessageDialog(frame,
195                                               message,
196                                               title,
197                                               message_type);
198         }
199
200         private void show_message(String in_message, String in_title, int in_message_type) {
201                 final String message = in_message;
202                 final String title = in_title;
203                 final int message_type = in_message_type;
204                 Runnable r = new Runnable() {
205                                 public void run() {
206                                         try {
207                                                 show_message_internal(message, title, message_type);
208                                         } catch (Exception ex) {
209                                         }
210                                 }
211                         };
212                 SwingUtilities.invokeLater(r);
213         }
214
215         public void run () {
216                 try {
217                         boolean failed = false;
218                         if (remote)
219                                 serial_line.start_remote();
220
221                         for (AltosEepromLog log : flights) {
222                                 parse_exception = null;
223                                 if (log.selected) {
224                                         monitor.reset();
225                                         CaptureLog(log);
226                                 }
227                                 if (parse_exception != null) {
228                                         failed = true;
229                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
230                                                                    log.flight,
231                                                                    parse_exception.getMessage()),
232                                                      serial_line.device.toShortString(),
233                                                      JOptionPane.WARNING_MESSAGE);
234                                 }
235                         }
236                         success = !failed;
237                 } catch (IOException ee) {
238                         show_message(ee.getLocalizedMessage(),
239                                      serial_line.device.toShortString(),
240                                      JOptionPane.ERROR_MESSAGE);
241                 } catch (InterruptedException ie) {
242                         show_message(String.format("Connection to \"%s\" interrupted",
243                                                    serial_line.device.toShortString()),
244                                      "Connection Interrupted",
245                                      JOptionPane.ERROR_MESSAGE);
246                 } catch (TimeoutException te) {
247                         show_message(String.format("Connection to \"%s\" failed",
248                                                    serial_line.device.toShortString()),
249                                      "Connection Failed",
250                                      JOptionPane.ERROR_MESSAGE);
251                 } finally {
252                         if (remote) {
253                                 try {
254                                         serial_line.stop_remote();
255                                 } catch (InterruptedException ie) {
256                                 }
257                         }
258                         serial_line.flush_output();
259                 }
260                 monitor.done();
261                 if (listener != null) {
262                         Runnable r = new Runnable() {
263                                         public void run() {
264                                                 try {
265                                                         listener.actionPerformed(new ActionEvent(this,
266                                                                                                  success ? 1 : 0,
267                                                                                                  "download"));
268                                                 } catch (Exception ex) {
269                                                 }
270                                         }
271                                 };
272                         SwingUtilities.invokeLater(r);
273                 }
274         }
275
276         public void start() {
277                 eeprom_thread = new Thread(this);
278                 eeprom_thread.start();
279         }
280
281         public void addActionListener(ActionListener l) {
282                 listener = l;
283         }
284
285         public AltosEepromDownload(JFrame given_frame,
286                                    AltosSerial given_serial_line,
287                                    boolean given_remote,
288                                    AltosEepromList given_flights) {
289
290                 frame = given_frame;
291                 serial_line = given_serial_line;
292                 serial_line.set_frame(frame);
293                 remote = given_remote;
294                 flights = given_flights;
295                 success = false;
296
297                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
298                 monitor.addActionListener(new ActionListener() {
299                                 public void actionPerformed(ActionEvent e) {
300                                         if (eeprom_thread != null)
301                                                 eeprom_thread.interrupt();
302                                 }
303                         });
304         }
305 }