Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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         static final String[] state_names = {
36                 "startup",
37                 "idle",
38                 "pad",
39                 "boost",
40                 "fast",
41                 "coast",
42                 "drogue",
43                 "main",
44                 "landed",
45                 "invalid",
46         };
47
48         int[] ParseHex(String line) {
49                 String[] tokens = line.split("\\s+");
50                 int[] array = new int[tokens.length];
51
52                 for (int i = 0; i < tokens.length; i++)
53                         try {
54                                 array[i] = Integer.parseInt(tokens[i], 16);
55                         } catch (NumberFormatException ne) {
56                                 return null;
57                         }
58                 return array;
59         }
60
61         int checksum(int[] line) {
62                 int     csum = 0x5a;
63                 for (int i = 1; i < line.length; i++)
64                         csum += line[i];
65                 return csum & 0xff;
66         }
67
68         void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
69                 while (!pending.isEmpty()) {
70                         file.write(pending.remove());
71                 }
72         }
73
74         JFrame                  frame;
75         AltosDevice             device;
76         AltosSerial             serial_line;
77         boolean                 remote;
78         Thread                  eeprom_thread;
79         AltosEepromMonitor      monitor;
80
81         void CaptureLog() throws IOException, InterruptedException, TimeoutException {
82                 int                     serial = 0;
83                 int                     block, state_block = 0;
84                 int                     addr;
85                 int                     flight = 0;
86                 int                     year = 0, month = 0, day = 0;
87                 int                     state = 0;
88                 boolean                 done = false;
89                 boolean                 want_file = false;
90                 boolean                 any_valid;
91                 FileWriter              eeprom_file = null;
92                 AltosFile               eeprom_name;
93                 LinkedList<String>      eeprom_pending = new LinkedList<String>();
94
95                 serial_line.printf("\nc s\nv\n");
96
97                 /* Pull the serial number out of the version information */
98
99                 for (;;) {
100                         String  line = serial_line.get_reply(5000);
101
102                         if (line == null)
103                                 throw new TimeoutException();
104                         if (line.startsWith("serial-number")) {
105                                 try {
106                                         serial = Integer.parseInt(line.substring(13).trim());
107                                 } catch (NumberFormatException ne) {
108                                         serial = 0;
109                                 }
110                         }
111
112                         eeprom_pending.add(String.format("%s\n", line));
113
114                         /* signals the end of the version info */
115                         if (line.startsWith("software-version"))
116                                 break;
117                 }
118                 if (serial == 0)
119                         throw new IOException("no serial number found");
120
121                 monitor.set_serial(serial);
122                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
123
124                 state = 0; state_block = 0;
125                 for (block = 0; !done && block < 511; block++) {
126                         serial_line.printf("e %x\n", block);
127                         any_valid = false;
128                         monitor.set_value(state_names[state], state, block - state_block);
129                         for (addr = 0; addr < 0x100;) {
130                                 String  line = serial_line.get_reply(5000);
131                                 if (line == null)
132                                         throw new TimeoutException();
133                                 int[] values = ParseHex(line);
134
135                                 if (values == null) {
136                                         System.out.printf("invalid line: %s\n", line);
137                                         continue;
138                                 } else if (values[0] != addr) {
139                                         System.out.printf("data address out of sync at 0x%x\n",
140                                                           block * 256 + values[0]);
141                                 } else if (checksum(values) != 0) {
142                                         System.out.printf("invalid checksum at 0x%x\n",
143                                                           block * 256 + values[0]);
144                                 } else {
145                                         any_valid = true;
146                                         int     cmd = values[1];
147                                         int     tick = values[3] + (values[4] << 8);
148                                         int     a = values[5] + (values[6] << 8);
149                                         int     b = values[7] + (values[8] << 8);
150
151                                         if (cmd == Altos.AO_LOG_FLIGHT) {
152                                                 flight = b;
153                                                 monitor.set_flight(flight);
154                                         }
155
156                                         /* Monitor state transitions to update display */
157                                         if (cmd == Altos.AO_LOG_STATE && a <= Altos.ao_flight_landed) {
158                                                 if (a > Altos.ao_flight_pad)
159                                                         want_file = true;
160                                                 if (a > state)
161                                                         state_block = block;
162                                                 state = a;
163                                         }
164
165                                         if (cmd == Altos.AO_LOG_GPS_DATE) {
166                                                 year = 2000 + (a & 0xff);
167                                                 month = (a >> 8) & 0xff;
168                                                 day = (b & 0xff);
169                                                 want_file = true;
170                                         }
171
172                                         if (eeprom_file == null) {
173                                                 if (serial != 0 && flight != 0 && want_file) {
174                                                         if (year != 0 && month != 0 && day != 0)
175                                                                 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom");
176                                                         else
177                                                                 eeprom_name = new AltosFile(serial, flight, "eeprom");
178
179                                                         monitor.set_file(eeprom_name.getName());
180                                                         eeprom_file = new FileWriter(eeprom_name);
181                                                         if (eeprom_file != null) {
182                                                                 FlushPending(eeprom_file, eeprom_pending);
183                                                                 eeprom_pending = null;
184                                                         }
185                                                 }
186                                         }
187
188                                         String log_line = String.format("%c %4x %4x %4x\n",
189                                                                         cmd, tick, a, b);
190                                         if (eeprom_file != null)
191                                                 eeprom_file.write(log_line);
192                                         else
193                                                 eeprom_pending.add(log_line);
194
195                                         if (cmd == Altos.AO_LOG_STATE && a == Altos.ao_flight_landed) {
196                                                 done = true;
197                                         }
198                                 }
199                                 addr += 8;
200                         }
201                         if (!any_valid)
202                                 done = true;
203                 }
204                 if (eeprom_file == null) {
205                         eeprom_name = new AltosFile(serial,flight,"eeprom");
206                         eeprom_file = new FileWriter(eeprom_name);
207                         if (eeprom_file != null) {
208                                 FlushPending(eeprom_file, eeprom_pending);
209                         }
210                 }
211                 if (eeprom_file != null) {
212                         eeprom_file.flush();
213                         eeprom_file.close();
214                 }
215         }
216
217         public void run () {
218                 if (remote) {
219                         serial_line.set_radio();
220                         serial_line.printf("p\nE 0\n");
221                         serial_line.flush_input();
222                 }
223
224                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
225                 monitor.addActionListener(new ActionListener() {
226                                 public void actionPerformed(ActionEvent e) {
227                                         eeprom_thread.interrupt();
228                                 }
229                         });
230                 try {
231                         CaptureLog();
232                 } catch (IOException ee) {
233                         JOptionPane.showMessageDialog(frame,
234                                                       device.toShortString(),
235                                                       ee.getLocalizedMessage(),
236                                                       JOptionPane.ERROR_MESSAGE);
237                 } catch (InterruptedException ie) {
238                 } catch (TimeoutException te) {
239                         JOptionPane.showMessageDialog(frame,
240                                                       String.format("Connection to \"%s\" failed",
241                                                                     device.toShortString()),
242                                                       "Connection Failed",
243                                                       JOptionPane.ERROR_MESSAGE);
244                 }
245                 if (remote)
246                         serial_line.printf("~");
247                 monitor.done();
248                 serial_line.flush_output();
249                 serial_line.close();
250         }
251
252         public AltosEepromDownload(JFrame given_frame) {
253                 frame = given_frame;
254                 device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
255
256                 remote = false;
257
258                 if (device != null) {
259                         try {
260                                 serial_line = new AltosSerial(device);
261                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
262                                         remote = true;
263                                 eeprom_thread = new Thread(this);
264                                 eeprom_thread.start();
265                         } catch (FileNotFoundException ee) {
266                                 JOptionPane.showMessageDialog(frame,
267                                                               String.format("Cannot open device \"%s\"",
268                                                                             device.toShortString()),
269                                                               "Cannot open target device",
270                                                               JOptionPane.ERROR_MESSAGE);
271                         } catch (AltosSerialInUseException si) {
272                                 JOptionPane.showMessageDialog(frame,
273                                                               String.format("Device \"%s\" already in use",
274                                                                             device.toShortString()),
275                                                               "Device in use",
276                                                               JOptionPane.ERROR_MESSAGE);
277                         } catch (IOException ee) {
278                                 JOptionPane.showMessageDialog(frame,
279                                                               device.toShortString(),
280                                                               ee.getLocalizedMessage(),
281                                                               JOptionPane.ERROR_MESSAGE);
282                         }
283                 }
284         }
285 }