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