altos/altosui: Report log format in the version command
[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     log_format;
40         int     serial;
41
42         /* Strings returned */
43         LinkedList<String>      lines;
44
45         /* Config information */
46         int     config_major;
47         int     config_minor;
48         int     main_deploy;
49         int     apogee_delay;
50         int     radio_channel;
51         int     radio_setting;
52         String  callsign;
53         int     accel_cal_plus, accel_cal_minus;
54         int     radio_calibration;
55         int     flight_log_max;
56         int     ignite_mode;
57
58
59         static String get_string(String line, String label) throws  ParseException {
60                 if (line.startsWith(label)) {
61                         String  quoted = line.substring(label.length()).trim();
62
63                         if (quoted.startsWith("\""))
64                                 quoted = quoted.substring(1);
65                         if (quoted.endsWith("\""))
66                                 quoted = quoted.substring(0,quoted.length()-1);
67                         return quoted;
68                 }
69                 throw new ParseException("mismatch", 0);
70         }
71
72         static int get_int(String line, String label) throws NumberFormatException, ParseException {
73                 if (line.startsWith(label)) {
74                         String tail = line.substring(label.length()).trim();
75                         String[] tokens = tail.split("\\s+");
76                         if (tokens.length > 0)
77                                 return  Integer.parseInt(tokens[0]);
78                 }
79                 throw new ParseException("mismatch", 0);
80         }
81
82         public Iterator<String> iterator() {
83                 return lines.iterator();
84         }
85
86         public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
87                 serial_line.printf("c s\nv\n");
88                 lines = new LinkedList<String>();
89                 radio_setting = 0;
90                 for (;;) {
91                         String line = serial_line.get_reply();
92                         if (line == null)
93                                 throw new TimeoutException();
94                         if (line.contains("Syntax error"))
95                                 continue;
96                         lines.add(line);
97                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
98                         try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
99                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
100                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
101                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
102                         try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
103                         try {
104                                 if (line.startsWith("Accel cal")) {
105                                         String[] bits = line.split("\\s+");
106                                         if (bits.length >= 6) {
107                                                 accel_cal_plus = Integer.parseInt(bits[3]);
108                                                 accel_cal_minus = Integer.parseInt(bits[5]);
109                                         }
110                                 }
111                         } catch (Exception e) {}
112                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
113                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
114                         try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
115                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
116                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
117                         try { product = get_string(line,"product"); } catch (Exception e) {}
118
119                         /* signals the end of the version info */
120                         if (line.startsWith("software-version"))
121                                 break;
122                 }
123         }
124
125 }