2 * Copyright © 2011 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.
18 package org.altusmetrum.AltosLib;
22 import java.util.concurrent.*;
24 public class AltosConfigData implements Iterable<String> {
26 /* Version information */
27 public String manufacturer;
28 public String product;
31 public int log_format;
32 public String version;
34 /* Strings returned */
35 public LinkedList<String> lines;
37 /* Config information */
39 public int main_deploy;
40 public int apogee_delay;
41 public int apogee_lockout;
44 public int radio_frequency;
45 public String callsign;
46 public int radio_enable;
47 public int radio_calibration;
48 /* Old HAS_RADIO values */
49 public int radio_channel;
50 public int radio_setting;
53 public int accel_cal_plus, accel_cal_minus;
54 public int pad_orientation;
57 public int flight_log_max;
60 public int ignite_mode;
63 public String aes_key;
66 public AltosPyro[] pyros;
70 /* Storage info replies */
71 public int storage_size;
72 public int storage_erase_unit;
74 /* Log listing replies */
75 public int stored_flight;
77 public static String get_string(String line, String label) throws ParseException {
78 if (line.startsWith(label)) {
79 String quoted = line.substring(label.length()).trim();
81 if (quoted.startsWith("\""))
82 quoted = quoted.substring(1);
83 if (quoted.endsWith("\""))
84 quoted = quoted.substring(0,quoted.length()-1);
87 throw new ParseException("mismatch", 0);
90 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
91 if (line.startsWith(label)) {
92 String tail = line.substring(label.length()).trim();
93 String[] tokens = tail.split("\\s+");
94 if (tokens.length > 0)
95 return Integer.parseInt(tokens[0]);
97 throw new ParseException("mismatch", 0);
100 public Iterator<String> iterator() {
101 return lines.iterator();
104 public int log_available() {
105 switch (log_format) {
106 case AltosLib.AO_LOG_FORMAT_TINY:
107 if (stored_flight == 0)
110 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
111 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
114 if (flight_log_max <= 0)
116 int log_space = storage_size - storage_erase_unit;
117 int log_used = stored_flight * flight_log_max;
119 if (log_used >= log_space)
121 return (log_space - log_used) / flight_log_max;
125 int[] parse_version(String v) {
126 String[] parts = v.split("\\.");
127 int r[] = new int[parts.length];
129 for (int i = 0; i < parts.length; i++) {
131 r[i] = AltosLib.fromdec(parts[i]);
132 } catch (NumberFormatException n) {
140 public int compare_version(String other) {
141 int[] me = parse_version(version);
142 int[] them = parse_version(other);
144 int l = Math.min(me.length, them.length);
146 for (int i = 0; i < l; i++) {
147 int d = me[i] - them[i];
158 public void reset() {
159 lines = new LinkedList<String>();
161 manufacturer = "unknown";
165 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
175 radio_calibration = 0;
180 accel_cal_minus = -1;
181 pad_orientation = -1;
193 storage_erase_unit = -1;
197 public void parse_line(String line) {
199 /* Version replies */
200 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
201 try { product = get_string(line, "product"); } catch (Exception e) {}
202 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
203 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
204 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
205 try { version = get_string(line, "software-version"); } catch (Exception e) {}
207 /* Version also contains MS5607 info, which we ignore here */
209 /* Config show replies */
212 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
213 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
214 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
218 radio_frequency = get_int(line, "Frequency:");
219 if (radio_frequency < 0)
220 radio_frequency = 434550;
221 } catch (Exception e) {}
222 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
223 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
224 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
226 /* Old HAS_RADIO values */
227 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
228 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
232 if (line.startsWith("Accel cal")) {
233 String[] bits = line.split("\\s+");
234 if (bits.length >= 6) {
235 accel_cal_plus = Integer.parseInt(bits[3]);
236 accel_cal_minus = Integer.parseInt(bits[5]);
239 } catch (Exception e) {}
240 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
243 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
246 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
249 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
253 npyro = get_int(line, "Pyro-count:");
254 pyros = new AltosPyro[npyro];
256 } catch (Exception e) {}
259 AltosPyro p = new AltosPyro(pyro, line);
260 if (pyro < npyro - 1)
262 } catch (Exception e) {}
265 /* Storage info replies */
266 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
267 try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
269 /* Log listing replies */
270 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
273 public AltosConfigData() {
277 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
279 String line = link.get_reply();
281 throw new TimeoutException();
282 if (line.contains("Syntax error"))
284 this.parse_line(line);
286 /* signals the end of the version info */
287 if (line.startsWith(finished))
292 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
294 link.printf("c s\nf\nv\n");
295 read_link(link, "software-version");
296 switch (log_format) {
297 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
298 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
302 read_link(link, "done");