ef34dd3ee06747ed6e2e0260e54bd670c2f816cb
[fw/altos] / altosui / altoslib / src / org / altusmetrum / AltosLib / 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 import org.altusmetrum.AltosLib.*;
31
32 import libaltosJNI.*;
33
34 public class AltosConfigData implements Iterable<String> {
35
36         /* Version information */
37         String  manufacturer;
38         String  product;
39         String  version;
40         int     log_format;
41         int     serial;
42
43         /* Strings returned */
44         LinkedList<String>      lines;
45
46         /* Config information */
47         int     config_major;
48         int     config_minor;
49         int     main_deploy;
50         int     apogee_delay;
51         int     radio_channel;
52         int     radio_setting;
53         int     radio_frequency;
54         String  callsign;
55         int     accel_cal_plus, accel_cal_minus;
56         int     radio_calibration;
57         int     flight_log_max;
58         int     ignite_mode;
59         int     stored_flight;
60         int     storage_size;
61         int     storage_erase_unit;
62
63         static String get_string(String line, String label) throws  ParseException {
64                 if (line.startsWith(label)) {
65                         String  quoted = line.substring(label.length()).trim();
66
67                         if (quoted.startsWith("\""))
68                                 quoted = quoted.substring(1);
69                         if (quoted.endsWith("\""))
70                                 quoted = quoted.substring(0,quoted.length()-1);
71                         return quoted;
72                 }
73                 throw new ParseException("mismatch", 0);
74         }
75
76         static int get_int(String line, String label) throws NumberFormatException, ParseException {
77                 if (line.startsWith(label)) {
78                         String tail = line.substring(label.length()).trim();
79                         String[] tokens = tail.split("\\s+");
80                         if (tokens.length > 0)
81                                 return  Integer.parseInt(tokens[0]);
82                 }
83                 throw new ParseException("mismatch", 0);
84         }
85
86         public Iterator<String> iterator() {
87                 return lines.iterator();
88         }
89
90         public int log_available() {
91                 switch (log_format) {
92                 case Altos.AO_LOG_FORMAT_TINY:
93                         if (stored_flight == 0)
94                                 return 1;
95                         return 0;
96                 default:
97                         if (flight_log_max <= 0)
98                                 return 1;
99                         int     log_space = storage_size - storage_erase_unit;
100                         int     log_used = stored_flight * flight_log_max;
101
102                         if (log_used >= log_space)
103                                 return 0;
104                         return (log_space - log_used) / flight_log_max;
105                 }
106         }
107
108         int[] parse_version(String v) {
109                 String[] parts = v.split("\\.");
110                 int r[] = new int[parts.length];
111
112                 for (int i = 0; i < parts.length; i++) {
113                         try {
114                                 r[i] = Altos.fromdec(parts[i]);
115                         } catch (NumberFormatException n) {
116                                 r[i] = 0;
117                         }
118                 }
119
120                 return r;
121         }
122         
123         public int compare_version(String other) {
124                 int[]   me = parse_version(version);
125                 int[]   them = parse_version(other);
126
127                 int     l = Math.min(me.length, them.length);
128
129                 for (int i = 0; i < l; i++) {
130                         int     d = me[i] - them[i];
131                         if (d != 0)
132                                 return d;
133                 }
134                 if (me.length > l)
135                         return 1;
136                 if (them.length > l)
137                         return -1;
138                 return 0;
139         }
140
141         public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
142                 serial_line.printf("c s\np\nf\nl\nv\n");
143                 lines = new LinkedList<String>();
144                 radio_setting = 0;
145                 radio_frequency = 0;
146                 stored_flight = 0;
147                 for (;;) {
148                         String line = serial_line.get_reply();
149                         if (line == null)
150                                 throw new TimeoutException();
151                         if (line.contains("Syntax error"))
152                                 continue;
153                         lines.add(line);
154                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
155                         try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
156                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
157                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
158                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
159                         try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
160                         try {
161                                 radio_frequency = get_int(line, "Frequency:");
162                                 if (radio_frequency < 0)
163                                         radio_frequency = 434550;
164                         } catch (Exception e) {}
165                         try {
166                                 if (line.startsWith("Accel cal")) {
167                                         String[] bits = line.split("\\s+");
168                                         if (bits.length >= 6) {
169                                                 accel_cal_plus = Integer.parseInt(bits[3]);
170                                                 accel_cal_minus = Integer.parseInt(bits[5]);
171                                         }
172                                 }
173                         } catch (Exception e) {}
174                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
175                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
176                         try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
177                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
178                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
179                         try { product = get_string(line,"product"); } catch (Exception e) {}
180
181                         try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
182                         try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
183                         try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
184
185                         /* signals the end of the version info */
186                         if (line.startsWith("software-version"))
187                                 break;
188                 }
189         }
190
191 }