2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 public class AltosEepromDownload implements Runnable {
35 static final String[] state_names = {
48 int[] ParseHex(String line) {
49 String[] tokens = line.split("\\s+");
50 int[] array = new int[tokens.length];
52 for (int i = 0; i < tokens.length; i++)
54 array[i] = Integer.parseInt(tokens[i], 16);
55 } catch (NumberFormatException ne) {
61 int checksum(int[] line) {
63 for (int i = 1; i < line.length; i++)
68 void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
69 while (!pending.isEmpty()) {
70 file.write(pending.remove());
76 AltosSerial serial_line;
79 AltosEepromMonitor monitor;
81 void CaptureLog() throws IOException, InterruptedException, TimeoutException {
83 int block, state_block = 0;
86 int year = 0, month = 0, day = 0;
89 boolean want_file = false;
91 FileWriter eeprom_file = null;
92 AltosFile eeprom_name;
93 LinkedList<String> eeprom_pending = new LinkedList<String>();
95 serial_line.printf("\nc s\nv\n");
97 /* Pull the serial number out of the version information */
100 String line = serial_line.get_reply(1000);
103 throw new TimeoutException();
104 if (line.startsWith("serial-number")) {
106 serial = Integer.parseInt(line.substring(13).trim());
107 } catch (NumberFormatException ne) {
112 eeprom_pending.add(String.format("%s\n", line));
114 /* signals the end of the version info */
115 if (line.startsWith("software-version"))
119 throw new IOException("no serial number found");
121 monitor.set_serial(serial);
122 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
124 state = 0; state_block = 0;
125 for (block = 0; !done && block < 511; block++) {
126 serial_line.printf("e %x\n", block);
128 monitor.set_value(state_names[state], state, block - state_block);
129 for (addr = 0; addr < 0x100;) {
130 String line = serial_line.get_reply(1000);
132 throw new TimeoutException();
133 int[] values = ParseHex(line);
135 if (values == null) {
136 System.out.printf("invalid line: %s\n", line);
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]);
147 int tick = values[3] + (values[4] << 8);
148 int a = values[5] + (values[6] << 8);
149 int b = values[7] + (values[8] << 8);
151 if (cmd == Altos.AO_LOG_FLIGHT) {
153 monitor.set_flight(flight);
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)
165 if (cmd == Altos.AO_LOG_GPS_DATE) {
166 year = 2000 + (a & 0xff);
167 month = (a >> 8) & 0xff;
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");
177 eeprom_name = new AltosFile(serial, flight, "eeprom");
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;
188 String log_line = String.format("%c %4x %4x %4x\n",
190 if (eeprom_file != null)
191 eeprom_file.write(log_line);
193 eeprom_pending.add(log_line);
195 if (cmd == Altos.AO_LOG_STATE && a == Altos.ao_flight_landed) {
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);
211 if (eeprom_file != null) {
219 serial_line.set_channel(AltosPreferences.channel(device.getSerial()));
220 serial_line.set_callsign(AltosPreferences.callsign());
221 serial_line.printf("p\nE 0\n");
222 serial_line.flush_input();
225 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
226 monitor.addActionListener(new ActionListener() {
227 public void actionPerformed(ActionEvent e) {
228 eeprom_thread.interrupt();
233 } catch (IOException ee) {
234 JOptionPane.showMessageDialog(frame,
235 device.toShortString(),
236 ee.getLocalizedMessage(),
237 JOptionPane.ERROR_MESSAGE);
238 } catch (InterruptedException ie) {
239 } catch (TimeoutException te) {
240 JOptionPane.showMessageDialog(frame,
241 String.format("Connection to \"%s\" failed",
242 device.toShortString()),
244 JOptionPane.ERROR_MESSAGE);
247 serial_line.printf("~");
249 serial_line.flush_output();
253 public AltosEepromDownload(JFrame given_frame) {
255 device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
259 if (device != null) {
261 serial_line = new AltosSerial(device);
262 if (!device.matchProduct(AltosDevice.product_telemetrum))
264 eeprom_thread = new Thread(this);
265 eeprom_thread.start();
266 } catch (FileNotFoundException ee) {
267 JOptionPane.showMessageDialog(frame,
268 String.format("Cannot open device \"%s\"",
269 device.toShortString()),
270 "Cannot open target device",
271 JOptionPane.ERROR_MESSAGE);
272 } catch (AltosSerialInUseException si) {
273 JOptionPane.showMessageDialog(frame,
274 String.format("Device \"%s\" already in use",
275 device.toShortString()),
277 JOptionPane.ERROR_MESSAGE);
278 } catch (IOException ee) {
279 JOptionPane.showMessageDialog(frame,
280 device.toShortString(),
281 ee.getLocalizedMessage(),
282 JOptionPane.ERROR_MESSAGE);