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