altosui: Split eeprom download code apart
[fw/altos] / altosui / AltosConfigData.java
1 /*
2  * Copyright © 2011 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 AltosConfigData implements Iterable<String> {
34
35         /* Version information */
36         String  manufacturer;
37         String  product;
38         String  version;
39         int     serial;
40
41         /* Strings returned */
42         LinkedList<String>      lines;
43
44         /* Config information */
45         int     config_major;
46         int     config_minor;
47         int     main_deploy;
48         int     apogee_delay;
49         int     radio_channel;
50         String  callsign;
51         int     accel_cal_plus, accel_cal_minus;
52         int     radio_calibration;
53         int     flight_log_max;
54
55
56         static String get_string(String line, String label) throws  ParseException {
57                 if (line.startsWith(label)) {
58                         String  quoted = line.substring(label.length()).trim();
59
60                         if (quoted.startsWith("\""))
61                                 quoted = quoted.substring(1);
62                         if (quoted.endsWith("\""))
63                                 quoted = quoted.substring(0,quoted.length()-1);
64                         return quoted;
65                 }
66                 throw new ParseException("mismatch", 0);
67         }
68
69         static int get_int(String line, String label) throws NumberFormatException, ParseException {
70                 if (line.startsWith(label)) {
71                         String tail = line.substring(label.length()).trim();
72                         String[] tokens = tail.split("\\s+");
73                         if (tokens.length > 0)
74                                 return  Integer.parseInt(tokens[0]);
75                 }
76                 throw new ParseException("mismatch", 0);
77         }
78
79         public Iterator<String> iterator() {
80                 return lines.iterator();
81         }
82
83         public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
84                 serial_line.printf("c s\nv\n");
85                 lines = new LinkedList<String>();
86                 for (;;) {
87                         String line = serial_line.get_reply(5000);
88                         if (line == null)
89                                 throw new TimeoutException();
90                         if (line.contains("Syntax error"))
91                                 continue;
92                         lines.add(line);
93                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
94                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
95                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
96                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
97                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
98                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
99                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
100                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
101                         try { product = get_string(line,"product"); } catch (Exception e) {}
102
103                         /* signals the end of the version info */
104                         if (line.startsWith("software-version"))
105                                 break;
106                 }
107         }
108
109 }