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