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_3;
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;
71 public int aprs_interval;
73 /* Storage info replies */
74 public int storage_size;
75 public int storage_erase_unit;
77 /* Log listing replies */
78 public int stored_flight;
80 public static String get_string(String line, String label) throws ParseException {
81 if (line.startsWith(label)) {
82 String quoted = line.substring(label.length()).trim();
84 if (quoted.startsWith("\""))
85 quoted = quoted.substring(1);
86 if (quoted.endsWith("\""))
87 quoted = quoted.substring(0,quoted.length()-1);
90 throw new ParseException("mismatch", 0);
93 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
94 if (line.startsWith(label)) {
95 String tail = line.substring(label.length()).trim();
96 String[] tokens = tail.split("\\s+");
97 if (tokens.length > 0)
98 return Integer.parseInt(tokens[0]);
100 throw new ParseException("mismatch", 0);
103 public Iterator<String> iterator() {
104 return lines.iterator();
107 public int log_available() {
108 switch (log_format) {
109 case AltosLib.AO_LOG_FORMAT_TINY:
110 if (stored_flight == 0)
113 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
114 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
117 if (flight_log_max <= 0)
119 int log_max = flight_log_max * 1024;
120 int log_space = storage_size - storage_erase_unit;
123 if (stored_flight <= 0)
126 log_used = stored_flight * log_max;
129 if (log_used >= log_space)
132 log_avail = (log_space - log_used) / log_max;
138 public boolean has_monitor_battery() {
139 if (product.startsWith("TeleBT"))
144 int[] parse_version(String v) {
145 String[] parts = v.split("\\.");
146 int r[] = new int[parts.length];
148 for (int i = 0; i < parts.length; i++) {
150 r[i] = AltosLib.fromdec(parts[i]);
151 } catch (NumberFormatException n) {
159 public int compare_version(String other) {
160 int[] me = parse_version(version);
161 int[] them = parse_version(other);
163 int l = Math.min(me.length, them.length);
165 for (int i = 0; i < l; i++) {
166 int d = me[i] - them[i];
177 public void reset() {
178 lines = new LinkedList<String>();
180 manufacturer = "unknown";
184 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
191 radio_frequency = -1;
194 radio_calibration = -1;
199 accel_cal_minus = -1;
200 pad_orientation = -1;
214 storage_erase_unit = -1;
218 public void parse_line(String line) {
220 /* Version replies */
221 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
222 try { product = get_string(line, "product"); } catch (Exception e) {}
223 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
224 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
225 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
226 try { version = get_string(line, "software-version"); } catch (Exception e) {}
228 /* Version also contains MS5607 info, which we ignore here */
230 /* Config show replies */
233 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
234 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
235 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
239 radio_frequency = get_int(line, "Frequency:");
240 if (radio_frequency < 0)
241 radio_frequency = 434550;
242 } catch (Exception e) {}
243 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
244 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
245 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
247 /* Old HAS_RADIO values */
248 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
249 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
253 if (line.startsWith("Accel cal")) {
254 String[] bits = line.split("\\s+");
255 if (bits.length >= 6) {
256 accel_cal_plus = Integer.parseInt(bits[3]);
257 accel_cal_minus = Integer.parseInt(bits[5]);
260 } catch (Exception e) {}
261 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
264 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
267 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
270 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
274 npyro = get_int(line, "Pyro-count:");
275 pyros = new AltosPyro[npyro];
277 } catch (Exception e) {}
280 AltosPyro p = new AltosPyro(pyro, line);
283 } catch (Exception e) {}
287 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
289 /* Storage info replies */
290 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
291 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
293 /* Log listing replies */
294 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
297 public AltosConfigData() {
301 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
303 String line = link.get_reply();
305 throw new TimeoutException();
306 if (line.contains("Syntax error"))
308 this.parse_line(line);
310 /* signals the end of the version info */
311 if (line.startsWith(finished))
316 public boolean has_frequency() {
317 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
320 public void set_frequency(double freq) {
321 int frequency = radio_frequency;
322 int setting = radio_setting;
325 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
327 } else if (setting > 0) {
328 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
332 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
336 public double frequency() {
337 int channel = radio_channel;
338 int setting = radio_setting;
340 if (radio_frequency < 0 && channel < 0 && setting < 0)
348 return AltosConvert.radio_to_frequency(radio_frequency,
354 public int log_limit() {
355 if (storage_size > 0 && storage_erase_unit > 0) {
356 int log_limit = storage_size - storage_erase_unit;
358 return log_limit / 1024;
363 public void get_values(AltosConfigValues source) {
366 if (main_deploy >= 0)
367 main_deploy = source.main_deploy();
368 if (apogee_delay >= 0)
369 apogee_delay = source.apogee_delay();
370 if (apogee_lockout >= 0)
371 apogee_lockout = source.apogee_lockout();
375 set_frequency(source.radio_frequency());
376 if (radio_enable >= 0)
377 radio_enable = source.radio_enable();
378 if (callsign != null)
379 callsign = source.callsign();
380 if (radio_calibration >= 0)
381 radio_calibration = source.radio_calibration();
384 if (pad_orientation >= 0)
385 pad_orientation = source.pad_orientation();
388 if (flight_log_max >= 0)
389 flight_log_max = source.flight_log_max();
392 if (ignite_mode >= 0)
393 ignite_mode = source.ignite_mode();
397 pyros = source.pyros();
399 if (aprs_interval >= 0)
400 aprs_interval = source.aprs_interval();
403 public void set_values(AltosConfigValues dest) {
404 dest.set_serial(serial);
405 dest.set_product(product);
406 dest.set_version(version);
407 dest.set_main_deploy(main_deploy);
408 dest.set_apogee_delay(apogee_delay);
409 dest.set_apogee_lockout(apogee_lockout);
410 dest.set_radio_calibration(radio_calibration);
411 dest.set_radio_frequency(frequency());
412 boolean max_enabled = true;
413 switch (log_format) {
414 case AltosLib.AO_LOG_FORMAT_TINY:
418 if (stored_flight >= 0)
422 dest.set_flight_log_max_enabled(max_enabled);
423 dest.set_radio_enable(radio_enable);
424 dest.set_flight_log_max_limit(log_limit());
425 dest.set_flight_log_max(flight_log_max);
426 dest.set_ignite_mode(ignite_mode);
427 dest.set_pad_orientation(pad_orientation);
428 dest.set_callsign(callsign);
430 dest.set_pyros(pyros);
432 dest.set_pyros(null);
433 dest.set_aprs_interval(aprs_interval);
436 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
439 if (main_deploy >= 0)
440 link.printf("c m %d\n", main_deploy);
441 if (apogee_delay >= 0)
442 link.printf("c d %d\n", apogee_delay);
443 if (apogee_lockout >= 0)
444 link.printf("c L %d\n", apogee_lockout);
446 /* Don't mess with radio calibration when remote */
447 if (radio_calibration > 0 && !remote)
448 link.printf("c f %d\n", radio_calibration);
451 if (has_frequency()) {
452 boolean has_frequency = radio_frequency >= 0;
453 boolean has_setting = radio_setting > 0;
454 double frequency = frequency();
455 link.set_radio_frequency(frequency,
459 /* When remote, reset the dongle frequency at the same time */
462 link.set_radio_frequency(frequency);
467 if (callsign != null)
468 link.printf("c c %s\n", callsign);
469 if (radio_enable >= 0)
470 link.printf("c e %d\n", radio_enable);
473 /* UI doesn't support accel cal */
474 if (pad_orientation >= 0)
475 link.printf("c o %d\n", pad_orientation);
478 if (flight_log_max != 0)
479 link.printf("c l %d\n", flight_log_max);
482 if (ignite_mode >= 0)
483 link.printf("c i %d\n", ignite_mode);
486 /* UI doesn't support AES key config */
490 for (int p = 0; p < pyros.length; p++) {
491 link.printf("c P %s\n",
492 pyros[p].toString());
497 if (aprs_interval >= 0)
498 link.printf("c A %d\n", aprs_interval);
500 link.printf("c w\n");
504 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
506 link.printf("c s\nf\nv\n");
507 read_link(link, "software-version");
508 switch (log_format) {
509 case AltosLib.AO_LOG_FORMAT_FULL:
510 case AltosLib.AO_LOG_FORMAT_TINY:
511 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
513 read_link(link, "done");