altoslib/altosui: Add TeleMini-v1.0 eeprom support
[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
98                 int record_length = 8;
99
100                 state.set_serial(flights.config_data.serial);
101
102                 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
103                         AltosEeprom r = eechunk.eeprom(i, log_format, state);
104
105                         if (r == null)
106                                 continue;
107
108                         record_length = r.record_length();
109
110                         r.update_state(state);
111
112                         /* Monitor state transitions to update display */
113                         if (state.state != AltosLib.ao_flight_invalid &&
114                             state.state <= AltosLib.ao_flight_landed)
115                         {
116                                 if (state.state > Altos.ao_flight_pad)
117                                         want_file = true;
118                                 if (state.state == AltosLib.ao_flight_landed)
119                                         done = true;
120                         }
121
122                         if (state.gps != null)
123                                 want_file = true;
124
125                         if (r.valid) {
126                                 any_valid = true;
127                                 LogEeprom(r);
128                         }
129                 }
130                 if (!any_valid)
131                         done = true;
132
133                 CheckFile(false);
134         }
135         
136         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
137                 int                     block, state_block = 0;
138                 int                     log_format = flights.config_data.log_format;
139
140                 state = new AltosState();
141
142                 done = false;
143                 start = true;
144
145                 if (flights.config_data.serial < 0)
146                         throw new IOException("no serial number found");
147
148                 /* Reset per-capture variables */
149                 want_file = false;
150                 eeprom_file = null;
151                 eeprom_pending = new LinkedList<String>();
152
153                 /* Set serial number in the monitor dialog window */
154                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
155
156                 state_block = log.start_block;
157                 for (block = log.start_block; !done && block < log.end_block; block++) {
158                         monitor.set_value(state.state_name(),
159                                           state.state,
160                                           block - state_block,
161                                           block - log.start_block);
162
163                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
164
165                         /*
166                          * Guess what kind of data is there if the device
167                          * didn't tell us
168                          */
169
170                         if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
171                                 if (block == log.start_block) {
172                                         if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
173                                                 log_format = Altos.AO_LOG_FORMAT_FULL;
174                                         else
175                                                 log_format = Altos.AO_LOG_FORMAT_TINY;
176                                 }
177                         }
178
179                         CaptureEeprom (eechunk, log_format);
180                 }
181                 CheckFile(true);
182                 if (eeprom_file != null) {
183                         eeprom_file.flush();
184                         eeprom_file.close();
185                 }
186         }
187
188         private void show_message_internal(String message, String title, int message_type) {
189                 JOptionPane.showMessageDialog(frame,
190                                               message,
191                                               title,
192                                               message_type);
193         }
194
195         private void show_message(String in_message, String in_title, int in_message_type) {
196                 final String message = in_message;
197                 final String title = in_title;
198                 final int message_type = in_message_type;
199                 Runnable r = new Runnable() {
200                                 public void run() {
201                                         try {
202                                                 show_message_internal(message, title, message_type);
203                                         } catch (Exception ex) {
204                                         }
205                                 }
206                         };
207                 SwingUtilities.invokeLater(r);
208         }
209
210         public void run () {
211                 try {
212                         boolean failed = false;
213                         if (remote)
214                                 serial_line.start_remote();
215
216                         for (AltosEepromLog log : flights) {
217                                 parse_exception = null;
218                                 if (log.selected) {
219                                         monitor.reset();
220                                         CaptureLog(log);
221                                 }
222                                 if (parse_exception != null) {
223                                         failed = true;
224                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
225                                                                    log.flight,
226                                                                    parse_exception.getMessage()),
227                                                      serial_line.device.toShortString(),
228                                                      JOptionPane.WARNING_MESSAGE);
229                                 }
230                         }
231                         success = !failed;
232                 } catch (IOException ee) {
233                         show_message(ee.getLocalizedMessage(),
234                                      serial_line.device.toShortString(),
235                                      JOptionPane.ERROR_MESSAGE);
236                 } catch (InterruptedException ie) {
237                         System.out.printf("download interrupted\n");
238                 } catch (TimeoutException te) {
239                         show_message(String.format("Connection to \"%s\" failed",
240                                                    serial_line.device.toShortString()),
241                                      "Connection Failed",
242                                      JOptionPane.ERROR_MESSAGE);
243                 } finally {
244                         if (remote) {
245                                 try {
246                                         serial_line.stop_remote();
247                                 } catch (InterruptedException ie) {
248                                 }
249                         }
250                         serial_line.flush_output();
251                 }
252                 monitor.done();
253                 if (listener != null) {
254                         Runnable r = new Runnable() {
255                                         public void run() {
256                                                 try {
257                                                         listener.actionPerformed(new ActionEvent(this,
258                                                                                                  success ? 1 : 0,
259                                                                                                  "download"));
260                                                 } catch (Exception ex) {
261                                                 }
262                                         }
263                                 };
264                         SwingUtilities.invokeLater(r);
265                 }
266         }
267
268         public void start() {
269                 eeprom_thread = new Thread(this);
270                 eeprom_thread.start();
271         }
272
273         public void addActionListener(ActionListener l) {
274                 listener = l;
275         }
276
277         public AltosEepromDownload(JFrame given_frame,
278                                    AltosSerial given_serial_line,
279                                    boolean given_remote,
280                                    AltosEepromList given_flights) {
281
282                 frame = given_frame;
283                 serial_line = given_serial_line;
284                 serial_line.set_frame(frame);
285                 remote = given_remote;
286                 flights = given_flights;
287                 success = false;
288
289                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
290                 monitor.addActionListener(new ActionListener() {
291                                 public void actionPerformed(ActionEvent e) {
292                                         if (eeprom_thread != null)
293                                                 eeprom_thread.interrupt();
294                                 }
295                         });
296         }
297 }