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_4;
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;
33 public String version;
35 /* Strings returned */
36 public LinkedList<String> lines;
38 /* Config information */
40 public int main_deploy;
41 public int apogee_delay;
42 public int apogee_lockout;
45 public int radio_frequency;
46 public String callsign;
47 public int radio_enable;
48 public int radio_calibration;
49 /* Old HAS_RADIO values */
50 public int radio_channel;
51 public int radio_setting;
54 public int accel_cal_plus, accel_cal_minus;
55 public int pad_orientation;
58 public int flight_log_max;
61 public int ignite_mode;
64 public String aes_key;
67 public AltosPyro[] pyros;
72 public int aprs_interval;
77 /* Storage info replies */
78 public int storage_size;
79 public int storage_erase_unit;
81 /* Log listing replies */
82 public int stored_flight;
85 public int tracker_motion;
86 public int tracker_interval;
88 public static String get_string(String line, String label) throws ParseException {
89 if (line.startsWith(label)) {
90 String quoted = line.substring(label.length()).trim();
92 if (quoted.startsWith("\""))
93 quoted = quoted.substring(1);
94 if (quoted.endsWith("\""))
95 quoted = quoted.substring(0,quoted.length()-1);
98 throw new ParseException("mismatch", 0);
101 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
102 if (line.startsWith(label)) {
103 String tail = line.substring(label.length()).trim();
104 String[] tokens = tail.split("\\s+");
105 if (tokens.length > 0)
106 return Integer.parseInt(tokens[0]);
108 throw new ParseException("mismatch", 0);
111 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
112 if (line.startsWith(label)) {
113 String tail = line.substring(label.length()).trim();
114 String[] tokens = tail.split("\\s+");
115 if (tokens.length > 1) {
116 int[] values = new int[2];
117 values[0] = Integer.parseInt(tokens[0]);
118 values[1] = Integer.parseInt(tokens[1]);
122 throw new ParseException("mismatch", 0);
125 public Iterator<String> iterator() {
126 return lines.iterator();
129 public int log_space() {
133 if (storage_size > 0) {
134 int space = storage_size;
136 if (storage_erase_unit > 0 && use_flash_for_config())
137 space -= storage_erase_unit;
145 public int log_available() {
146 switch (log_format) {
147 case AltosLib.AO_LOG_FORMAT_TINY:
148 if (stored_flight == 0)
151 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
152 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
155 if (flight_log_max <= 0)
157 int log_max = flight_log_max * 1024;
158 int log_space = log_space();
161 if (stored_flight <= 0)
164 log_used = stored_flight * log_max;
167 if (log_used >= log_space)
170 log_avail = (log_space - log_used) / log_max;
176 public boolean has_monitor_battery() {
177 if (product.startsWith("TeleBT"))
182 int[] parse_version(String v) {
183 String[] parts = v.split("\\.");
184 int r[] = new int[parts.length];
186 for (int i = 0; i < parts.length; i++) {
188 r[i] = AltosLib.fromdec(parts[i]);
189 } catch (NumberFormatException n) {
197 public int compare_version(String other) {
198 int[] me = parse_version(version);
199 int[] them = parse_version(other);
201 int l = Math.min(me.length, them.length);
203 for (int i = 0; i < l; i++) {
204 int d = me[i] - them[i];
215 public void reset() {
216 lines = new LinkedList<String>();
218 manufacturer = "unknown";
222 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
230 radio_frequency = -1;
233 radio_calibration = -1;
238 accel_cal_minus = -1;
239 pad_orientation = -1;
255 tracker_interval = -1;
258 storage_erase_unit = -1;
262 public void parse_line(String line) {
264 /* Version replies */
265 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
266 try { product = get_string(line, "product"); } catch (Exception e) {}
267 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
268 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
269 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
270 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
271 try { version = get_string(line, "software-version"); } catch (Exception e) {}
273 /* Version also contains MS5607 info, which we ignore here */
275 /* Config show replies */
278 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
279 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
280 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
284 radio_frequency = get_int(line, "Frequency:");
285 if (radio_frequency < 0)
286 radio_frequency = 434550;
287 } catch (Exception e) {}
288 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
289 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
290 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
292 /* Old HAS_RADIO values */
293 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
294 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
298 if (line.startsWith("Accel cal")) {
299 String[] bits = line.split("\\s+");
300 if (bits.length >= 6) {
301 accel_cal_plus = Integer.parseInt(bits[3]);
302 accel_cal_minus = Integer.parseInt(bits[5]);
305 } catch (Exception e) {}
306 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
309 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
312 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
315 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
319 npyro = get_int(line, "Pyro-count:");
320 pyros = new AltosPyro[npyro];
322 } catch (Exception e) {}
325 AltosPyro p = new AltosPyro(pyro, line);
328 } catch (Exception e) {}
332 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
335 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
339 int[] values = get_values(line, "Tracker setting:");
340 tracker_motion = values[0];
341 tracker_interval = values[1];
342 } catch (Exception e) {}
344 /* Storage info replies */
345 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
346 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
348 /* Log listing replies */
349 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
352 public AltosConfigData() {
356 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
358 String line = link.get_reply();
360 throw new TimeoutException();
361 if (line.contains("Syntax error"))
363 this.parse_line(line);
365 /* signals the end of the version info */
366 if (line.startsWith(finished))
371 public boolean has_frequency() {
372 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
375 public void set_frequency(double freq) {
376 int frequency = radio_frequency;
377 int setting = radio_setting;
380 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
382 } else if (setting > 0) {
383 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
387 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
391 public double frequency() {
392 int channel = radio_channel;
393 int setting = radio_setting;
395 if (radio_frequency < 0 && channel < 0 && setting < 0)
403 return AltosConvert.radio_to_frequency(radio_frequency,
409 boolean use_flash_for_config() {
410 if (product.startsWith("TeleMega"))
412 if (product.startsWith("TeleMetrum-v2"))
418 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
421 if (main_deploy >= 0)
422 main_deploy = source.main_deploy();
423 if (apogee_delay >= 0)
424 apogee_delay = source.apogee_delay();
425 if (apogee_lockout >= 0)
426 apogee_lockout = source.apogee_lockout();
430 set_frequency(source.radio_frequency());
431 if (radio_enable >= 0)
432 radio_enable = source.radio_enable();
433 if (callsign != null)
434 callsign = source.callsign();
435 if (radio_calibration >= 0)
436 radio_calibration = source.radio_calibration();
439 if (pad_orientation >= 0)
440 pad_orientation = source.pad_orientation();
443 if (flight_log_max >= 0)
444 flight_log_max = source.flight_log_max();
447 if (ignite_mode >= 0)
448 ignite_mode = source.ignite_mode();
452 pyros = source.pyros();
455 if (aprs_interval >= 0)
456 aprs_interval = source.aprs_interval();
460 beep = source.beep();
462 if (tracker_motion >= 0)
463 tracker_motion = source.tracker_motion();
464 if (tracker_interval >= 0)
465 tracker_interval = source.tracker_interval();
468 public void set_values(AltosConfigValues dest) {
469 dest.set_serial(serial);
470 dest.set_product(product);
471 dest.set_version(version);
472 dest.set_main_deploy(main_deploy);
473 dest.set_apogee_delay(apogee_delay);
474 dest.set_apogee_lockout(apogee_lockout);
475 dest.set_radio_calibration(radio_calibration);
476 dest.set_radio_frequency(frequency());
477 boolean max_enabled = true;
479 if (log_space() == 0)
482 switch (log_format) {
483 case AltosLib.AO_LOG_FORMAT_TINY:
487 if (stored_flight > 0)
492 dest.set_flight_log_max_enabled(max_enabled);
493 dest.set_radio_enable(radio_enable);
494 dest.set_flight_log_max_limit(log_space() / 1024);
495 dest.set_flight_log_max(flight_log_max);
496 dest.set_ignite_mode(ignite_mode);
497 dest.set_pad_orientation(pad_orientation);
498 dest.set_callsign(callsign);
500 dest.set_pyros(pyros);
502 dest.set_pyros(null);
503 dest.set_aprs_interval(aprs_interval);
505 dest.set_tracker_motion(tracker_motion);
506 dest.set_tracker_interval(tracker_interval);
509 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
512 if (main_deploy >= 0)
513 link.printf("c m %d\n", main_deploy);
514 if (apogee_delay >= 0)
515 link.printf("c d %d\n", apogee_delay);
516 if (apogee_lockout >= 0)
517 link.printf("c L %d\n", apogee_lockout);
519 /* Don't mess with radio calibration when remote */
520 if (radio_calibration > 0 && !remote)
521 link.printf("c f %d\n", radio_calibration);
524 if (has_frequency()) {
525 boolean has_frequency = radio_frequency >= 0;
526 boolean has_setting = radio_setting > 0;
527 double frequency = frequency();
528 link.set_radio_frequency(frequency,
532 /* When remote, reset the dongle frequency at the same time */
535 link.set_radio_frequency(frequency);
540 if (callsign != null)
541 link.printf("c c %s\n", callsign);
542 if (radio_enable >= 0)
543 link.printf("c e %d\n", radio_enable);
546 /* UI doesn't support accel cal */
547 if (pad_orientation >= 0)
548 link.printf("c o %d\n", pad_orientation);
551 if (flight_log_max != 0)
552 link.printf("c l %d\n", flight_log_max);
555 if (ignite_mode >= 0)
556 link.printf("c i %d\n", ignite_mode);
559 /* UI doesn't support AES key config */
563 for (int p = 0; p < pyros.length; p++) {
564 link.printf("c P %s\n",
565 pyros[p].toString());
570 if (aprs_interval >= 0)
571 link.printf("c A %d\n", aprs_interval);
575 link.printf("c b %d\n", beep);
578 if (tracker_motion >= 0 && tracker_interval >= 0)
579 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
581 link.printf("c w\n");
585 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
587 link.printf("c s\nf\nv\n");
588 read_link(link, "software-version");
589 switch (log_format) {
590 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
591 case AltosLib.AO_LOG_FORMAT_NONE:
595 read_link(link, "done");