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_6;
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;
34 public int altitude_32;
36 /* Strings returned */
37 public LinkedList<String> lines;
39 /* Config information */
41 public int main_deploy;
42 public int apogee_delay;
43 public int apogee_lockout;
46 public int radio_frequency;
47 public String callsign;
48 public int radio_enable;
49 public int radio_calibration;
50 public int telemetry_rate;
51 /* Old HAS_RADIO values */
52 public int radio_channel;
53 public int radio_setting;
56 public int accel_cal_plus, accel_cal_minus;
57 public int pad_orientation;
60 public int flight_log_max;
64 public int ignite_mode;
67 public String aes_key;
70 public AltosPyro[] pyros;
73 public double pyro_firing_time;
76 public int aprs_interval;
82 /* Storage info replies */
83 public int storage_size;
84 public int storage_erase_unit;
86 /* Log listing replies */
87 public int stored_flight;
90 public int tracker_motion;
91 public int tracker_interval;
94 public int accel_zero_along, accel_zero_across, accel_zero_through;
97 public int ms5607_reserved;
98 public int ms5607_sens;
99 public int ms5607_off;
100 public int ms5607_tcs;
101 public int ms5607_tco;
102 public int ms5607_tref;
103 public int ms5607_tempsens;
104 public int ms5607_crc;
106 public static String get_string(String line, String label) throws ParseException {
107 if (line.startsWith(label)) {
108 String quoted = line.substring(label.length()).trim();
110 if (quoted.startsWith("\""))
111 quoted = quoted.substring(1);
112 if (quoted.endsWith("\""))
113 quoted = quoted.substring(0,quoted.length()-1);
116 throw new ParseException("mismatch", 0);
119 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
120 if (line.startsWith(label)) {
121 String tail = line.substring(label.length()).trim();
122 String[] tokens = tail.split("\\s+");
123 if (tokens.length > 0)
124 return Integer.parseInt(tokens[0]);
126 throw new ParseException("mismatch", 0);
129 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
130 if (line.startsWith(label)) {
131 String tail = line.substring(label.length()).trim();
132 String[] tokens = tail.split("\\s+");
133 if (tokens.length > 1) {
134 int[] values = new int[2];
135 values[0] = Integer.parseInt(tokens[0]);
136 values[1] = Integer.parseInt(tokens[1]);
140 throw new ParseException("mismatch", 0);
143 public Iterator<String> iterator() {
144 return lines.iterator();
147 public int log_space() {
151 if (storage_size > 0) {
152 int space = storage_size;
154 if (storage_erase_unit > 0 && use_flash_for_config())
155 space -= storage_erase_unit;
163 public int log_available() {
164 switch (log_format) {
165 case AltosLib.AO_LOG_FORMAT_TINY:
166 if (stored_flight == 0)
169 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
170 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
173 if (flight_log_max <= 0)
175 int log_max = flight_log_max * 1024;
176 int log_space = log_space();
179 if (stored_flight <= 0)
182 log_used = stored_flight * log_max;
185 if (log_used >= log_space)
188 log_avail = (log_space - log_used) / log_max;
194 public boolean has_monitor_battery() {
195 if (product.startsWith("TeleBT"))
200 int[] parse_version(String v) {
201 String[] parts = v.split("\\.");
202 int r[] = new int[parts.length];
204 for (int i = 0; i < parts.length; i++) {
206 r[i] = AltosLib.fromdec(parts[i]);
207 } catch (NumberFormatException n) {
215 public int compare_version(String other) {
216 int[] me = parse_version(version);
217 int[] them = parse_version(other);
219 int l = Math.min(me.length, them.length);
221 for (int i = 0; i < l; i++) {
222 int d = me[i] - them[i];
233 public void reset() {
234 lines = new LinkedList<String>();
236 manufacturer = "unknown";
240 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
248 radio_frequency = -1;
251 radio_calibration = -1;
257 accel_cal_minus = -1;
258 pad_orientation = -1;
269 pyro_firing_time = -1;
277 tracker_interval = -1;
280 storage_erase_unit = -1;
283 accel_zero_along = -1;
284 accel_zero_across = -1;
285 accel_zero_through = -1;
288 public void parse_line(String line) {
290 /* Version replies */
291 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
292 try { product = get_string(line, "product"); } catch (Exception e) {}
293 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
294 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
295 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
296 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
297 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
298 try { version = get_string(line, "software-version"); } catch (Exception e) {}
300 /* Version also contains MS5607 info, which we ignore here */
302 try { ms5607_reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
303 try { ms5607_sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
304 try { ms5607_off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
305 try { ms5607_tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
306 try { ms5607_tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
307 try { ms5607_tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
308 try { ms5607_tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
309 try { ms5607_crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
311 /* Config show replies */
314 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
315 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
316 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
320 radio_frequency = get_int(line, "Frequency:");
321 if (radio_frequency < 0)
322 radio_frequency = 434550;
323 } catch (Exception e) {}
324 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
325 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
326 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
327 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
329 /* Old HAS_RADIO values */
330 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
331 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
335 if (line.startsWith("Accel cal")) {
336 String[] bits = line.split("\\s+");
337 if (bits.length >= 6) {
338 accel_cal_plus = Integer.parseInt(bits[3]);
339 accel_cal_minus = Integer.parseInt(bits[5]);
342 } catch (Exception e) {}
343 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
346 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
347 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
350 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
353 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
357 npyro = get_int(line, "Pyro-count:");
358 pyros = new AltosPyro[npyro];
360 } catch (Exception e) {}
363 AltosPyro p = new AltosPyro(pyro, line);
366 } catch (Exception e) {}
368 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
371 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
372 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
375 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
379 int[] values = get_values(line, "Tracker setting:");
380 tracker_motion = values[0];
381 tracker_interval = values[1];
382 } catch (Exception e) {}
384 /* Storage info replies */
385 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
386 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
388 /* Log listing replies */
389 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
393 if (line.startsWith("IMU call along")) {
394 String[] bits = line.split("\\s+");
395 if (bits.length >= 8) {
396 accel_zero_along = Integer.parseInt(bits[3]);
397 accel_zero_across = Integer.parseInt(bits[5]);
398 accel_zero_through = Integer.parseInt(bits[7]);
401 } catch (Exception e) {}
404 public AltosConfigData() {
408 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
410 String line = link.get_reply();
412 throw new TimeoutException();
413 if (line.contains("Syntax error"))
415 this.parse_line(line);
417 /* signals the end of the version info */
418 if (line.startsWith(finished))
423 public boolean has_frequency() {
424 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
427 public boolean has_telemetry_rate() {
428 return telemetry_rate >= 0;
431 public void set_frequency(double freq) {
432 int frequency = radio_frequency;
433 int setting = radio_setting;
436 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
438 } else if (setting > 0) {
439 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
443 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
447 public double frequency() {
448 int channel = radio_channel;
449 int setting = radio_setting;
451 if (radio_frequency < 0 && channel < 0 && setting < 0)
459 return AltosConvert.radio_to_frequency(radio_frequency,
465 boolean use_flash_for_config() {
466 if (product.startsWith("TeleMega"))
468 if (product.startsWith("TeleMetrum-v2"))
470 if (product.startsWith("EasyMega"))
476 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
479 if (main_deploy >= 0)
480 main_deploy = source.main_deploy();
481 if (apogee_delay >= 0)
482 apogee_delay = source.apogee_delay();
483 if (apogee_lockout >= 0)
484 apogee_lockout = source.apogee_lockout();
488 set_frequency(source.radio_frequency());
489 if (radio_enable >= 0)
490 radio_enable = source.radio_enable();
491 if (callsign != null)
492 callsign = source.callsign();
493 if (radio_calibration >= 0)
494 radio_calibration = source.radio_calibration();
495 if (telemetry_rate >= 0)
496 telemetry_rate = source.telemetry_rate();
499 if (pad_orientation >= 0)
500 pad_orientation = source.pad_orientation();
503 if (flight_log_max >= 0)
504 flight_log_max = source.flight_log_max();
507 if (ignite_mode >= 0)
508 ignite_mode = source.ignite_mode();
512 pyros = source.pyros();
513 if (pyro_firing_time >= 0)
514 pyro_firing_time = source.pyro_firing_time();
517 if (aprs_interval >= 0)
518 aprs_interval = source.aprs_interval();
520 aprs_ssid = source.aprs_ssid();
524 beep = source.beep();
526 if (tracker_motion >= 0)
527 tracker_motion = source.tracker_motion();
528 if (tracker_interval >= 0)
529 tracker_interval = source.tracker_interval();
532 public void set_values(AltosConfigValues dest) {
533 dest.set_serial(serial);
534 dest.set_product(product);
535 dest.set_version(version);
536 dest.set_altitude_32(altitude_32);
537 dest.set_main_deploy(main_deploy);
538 dest.set_apogee_delay(apogee_delay);
539 dest.set_apogee_lockout(apogee_lockout);
540 dest.set_radio_calibration(radio_calibration);
541 dest.set_radio_frequency(frequency());
542 dest.set_telemetry_rate(telemetry_rate);
543 boolean max_enabled = true;
545 if (log_space() == 0)
551 switch (log_format) {
552 case AltosLib.AO_LOG_FORMAT_TINY:
556 if (stored_flight > 0)
561 dest.set_flight_log_max_enabled(max_enabled);
562 dest.set_radio_enable(radio_enable);
563 dest.set_flight_log_max_limit(log_space() / 1024);
564 dest.set_flight_log_max(flight_log_max);
565 dest.set_ignite_mode(ignite_mode);
566 dest.set_pad_orientation(pad_orientation);
567 dest.set_callsign(callsign);
569 dest.set_pyros(pyros);
571 dest.set_pyros(null);
572 dest.set_pyro_firing_time(pyro_firing_time);
573 dest.set_aprs_interval(aprs_interval);
574 dest.set_aprs_ssid(aprs_ssid);
576 dest.set_tracker_motion(tracker_motion);
577 dest.set_tracker_interval(tracker_interval);
580 public boolean log_has_state() {
581 switch (log_format) {
582 case AltosLib.AO_LOG_FORMAT_TELEGPS:
588 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
591 if (main_deploy >= 0)
592 link.printf("c m %d\n", main_deploy);
593 if (apogee_delay >= 0)
594 link.printf("c d %d\n", apogee_delay);
595 if (apogee_lockout >= 0)
596 link.printf("c L %d\n", apogee_lockout);
598 /* Don't mess with radio calibration when remote */
599 if (radio_calibration > 0 && !remote)
600 link.printf("c f %d\n", radio_calibration);
603 if (has_frequency()) {
604 boolean has_frequency = radio_frequency >= 0;
605 boolean has_setting = radio_setting > 0;
606 double frequency = frequency();
607 link.set_radio_frequency(frequency,
611 /* When remote, reset the dongle frequency at the same time */
615 link.set_radio_frequency(frequency);
621 if (telemetry_rate >= 0) {
622 link.printf("c T %d\n", telemetry_rate);
626 link.set_telemetry_rate(telemetry_rate);
632 if (callsign != null) {
633 link.printf("c c %s\n", callsign);
637 link.set_callsign(callsign);
643 if (radio_enable >= 0)
644 link.printf("c e %d\n", radio_enable);
647 /* UI doesn't support accel cal */
648 if (pad_orientation >= 0)
649 link.printf("c o %d\n", pad_orientation);
652 if (flight_log_max != 0)
653 link.printf("c l %d\n", flight_log_max);
656 if (ignite_mode >= 0)
657 link.printf("c i %d\n", ignite_mode);
660 /* UI doesn't support AES key config */
664 for (int p = 0; p < pyros.length; p++) {
665 link.printf("c P %s\n",
666 pyros[p].toString());
669 if (pyro_firing_time >= 0)
670 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
673 if (aprs_interval >= 0)
674 link.printf("c A %d\n", aprs_interval);
676 link.printf("c S %d\n", aprs_ssid);
680 link.printf("c b %d\n", beep);
683 if (tracker_motion >= 0 && tracker_interval >= 0)
684 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
687 /* UI doesn't support accel cal */
689 link.printf("c w\n");
693 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
695 link.printf("c s\nf\nv\n");
696 read_link(link, "software-version");
697 switch (log_format) {
698 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
699 case AltosLib.AO_LOG_FORMAT_NONE:
703 read_link(link, "done");