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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
23 import java.util.concurrent.*;
25 public class AltosConfigData implements Iterable<String> {
27 /* Version information */
28 public String manufacturer;
29 public String product;
32 public int log_format;
34 public String version;
35 public int altitude_32;
37 /* Strings returned */
38 public LinkedList<String> __lines;
40 /* Config information */
42 public int main_deploy;
43 public int apogee_delay;
44 public int apogee_lockout;
47 public int radio_frequency;
48 public String callsign;
49 public int radio_enable;
50 public int radio_calibration;
51 public int telemetry_rate;
52 /* Old HAS_RADIO values */
53 public int radio_channel;
54 public int radio_setting;
57 public int accel_cal_plus, accel_cal_minus;
58 public int pad_orientation;
61 public int flight_log_max;
65 public int ignite_mode;
68 public String aes_key;
71 public AltosPyro[] pyros;
74 public double pyro_firing_time;
77 public int aprs_interval;
79 public int aprs_format;
84 /* Storage info replies */
85 public int storage_size;
86 public int storage_erase_unit;
88 /* Log listing replies */
89 public int stored_flight;
92 public int tracker_motion;
93 public int tracker_interval;
96 public int accel_zero_along, accel_zero_across, accel_zero_through;
99 public int ms5607_reserved;
100 public int ms5607_sens;
101 public int ms5607_off;
102 public int ms5607_tcs;
103 public int ms5607_tco;
104 public int ms5607_tref;
105 public int ms5607_tempsens;
106 public int ms5607_crc;
108 public static String get_string(String line, String label) throws ParseException {
109 if (line.startsWith(label)) {
110 String quoted = line.substring(label.length()).trim();
112 if (quoted.startsWith("\""))
113 quoted = quoted.substring(1);
114 if (quoted.endsWith("\""))
115 quoted = quoted.substring(0,quoted.length()-1);
118 throw new ParseException("mismatch", 0);
121 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
122 if (line.startsWith(label)) {
123 String tail = line.substring(label.length()).trim();
124 String[] tokens = tail.split("\\s+");
125 if (tokens.length > 0)
126 return Integer.parseInt(tokens[0]);
128 throw new ParseException("mismatch", 0);
131 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
132 if (line.startsWith(label)) {
133 String tail = line.substring(label.length()).trim();
134 String[] tokens = tail.split("\\s+");
135 if (tokens.length > 1) {
136 int[] values = new int[2];
137 values[0] = Integer.parseInt(tokens[0]);
138 values[1] = Integer.parseInt(tokens[1]);
142 throw new ParseException("mismatch", 0);
145 public Iterator<String> iterator() {
146 return __lines.iterator();
149 public int log_space() {
153 if (storage_size > 0) {
154 int space = storage_size;
156 if (storage_erase_unit > 0 && use_flash_for_config())
157 space -= storage_erase_unit;
165 public int log_available() {
166 switch (log_format) {
167 case AltosLib.AO_LOG_FORMAT_TINY:
168 if (stored_flight == 0)
171 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
172 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
175 if (flight_log_max <= 0)
177 int log_max = flight_log_max * 1024;
178 int log_space = log_space();
181 if (stored_flight <= 0)
184 log_used = stored_flight * log_max;
187 if (log_used >= log_space)
190 log_avail = (log_space - log_used) / log_max;
196 public boolean has_monitor_battery() {
197 if (product.startsWith("TeleBT"))
202 int[] parse_version(String v) {
203 String[] parts = v.split("\\.");
204 int r[] = new int[parts.length];
206 for (int i = 0; i < parts.length; i++) {
208 r[i] = (int) AltosLib.fromdec(parts[i]);
209 } catch (NumberFormatException n) {
217 public boolean altitude_32() {
218 return altitude_32 == 1;
221 public int compare_version(String other) {
222 int[] me = parse_version(version);
223 int[] them = parse_version(other);
225 int l = Math.min(me.length, them.length);
227 for (int i = 0; i < l; i++) {
228 int d = me[i] - them[i];
239 public void reset() {
240 __lines = new LinkedList<String>();
244 serial = AltosLib.MISSING;
245 flight = AltosLib.MISSING;
246 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
247 log_space = AltosLib.MISSING;
250 main_deploy = AltosLib.MISSING;
251 apogee_delay = AltosLib.MISSING;
252 apogee_lockout = AltosLib.MISSING;
254 radio_frequency = AltosLib.MISSING;
256 radio_enable = AltosLib.MISSING;
257 radio_calibration = AltosLib.MISSING;
258 radio_channel = AltosLib.MISSING;
259 radio_setting = AltosLib.MISSING;
260 telemetry_rate = AltosLib.MISSING;
262 accel_cal_plus = AltosLib.MISSING;
263 accel_cal_minus = AltosLib.MISSING;
264 pad_orientation = AltosLib.MISSING;
266 flight_log_max = AltosLib.MISSING;
267 log_fixed = AltosLib.MISSING;
268 ignite_mode = AltosLib.MISSING;
275 pyro_firing_time = AltosLib.MISSING;
277 aprs_interval = AltosLib.MISSING;
278 aprs_ssid = AltosLib.MISSING;
279 aprs_format = AltosLib.MISSING;
281 beep = AltosLib.MISSING;
283 tracker_motion = AltosLib.MISSING;
284 tracker_interval = AltosLib.MISSING;
286 storage_size = AltosLib.MISSING;
287 storage_erase_unit = AltosLib.MISSING;
288 stored_flight = AltosLib.MISSING;
290 accel_zero_along = AltosLib.MISSING;
291 accel_zero_across = AltosLib.MISSING;
292 accel_zero_through = AltosLib.MISSING;
295 public void parse_line(String line) {
297 /* Version replies */
298 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
299 try { product = get_string(line, "product"); } catch (Exception e) {}
300 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
301 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
302 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
303 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
304 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
305 try { version = get_string(line, "software-version"); } catch (Exception e) {}
307 /* Version also contains MS5607 info, which we ignore here */
309 try { ms5607_reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
310 try { ms5607_sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
311 try { ms5607_off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
312 try { ms5607_tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
313 try { ms5607_tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
314 try { ms5607_tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
315 try { ms5607_tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
316 try { ms5607_crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
318 /* Config show replies */
321 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
322 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
323 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
327 radio_frequency = get_int(line, "Frequency:");
328 if (radio_frequency < 0)
329 radio_frequency = 434550;
330 } catch (Exception e) {}
331 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
332 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
333 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
334 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
336 /* Old HAS_RADIO values */
337 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
338 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
342 if (line.startsWith("Accel cal")) {
343 String[] bits = line.split("\\s+");
344 if (bits.length >= 6) {
345 accel_cal_plus = Integer.parseInt(bits[3]);
346 accel_cal_minus = Integer.parseInt(bits[5]);
349 } catch (Exception e) {}
350 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
353 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
354 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
357 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
360 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
364 npyro = get_int(line, "Pyro-count:");
365 pyros = new AltosPyro[npyro];
367 } catch (Exception e) {}
370 AltosPyro p = new AltosPyro(pyro, line);
373 } catch (Exception e) {}
375 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
378 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
379 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
380 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
383 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
387 int[] values = get_values(line, "Tracker setting:");
388 tracker_motion = values[0];
389 tracker_interval = values[1];
390 } catch (Exception e) {}
392 /* Storage info replies */
393 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
394 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
396 /* Log listing replies */
397 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
401 if (line.startsWith("IMU call along")) {
402 String[] bits = line.split("\\s+");
403 if (bits.length >= 8) {
404 accel_zero_along = Integer.parseInt(bits[3]);
405 accel_zero_across = Integer.parseInt(bits[5]);
406 accel_zero_through = Integer.parseInt(bits[7]);
409 } catch (Exception e) {}
412 public AltosConfigData() {
416 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
418 String line = link.get_reply();
420 throw new TimeoutException();
421 if (line.contains("Syntax error"))
423 this.parse_line(line);
425 /* signals the end of the version info */
426 if (line.startsWith(finished))
431 public boolean has_frequency() {
432 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
435 public boolean has_telemetry_rate() {
436 return telemetry_rate >= 0;
439 public void set_frequency(double freq) {
440 int frequency = radio_frequency;
441 int setting = radio_setting;
444 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
445 radio_channel = AltosLib.MISSING;
446 } else if (setting > 0) {
447 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
449 radio_channel = AltosLib.MISSING;
451 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
455 public double frequency() {
456 int channel = radio_channel;
457 int setting = radio_setting;
459 if (radio_frequency < 0 && channel < 0 && setting < 0)
460 return AltosLib.MISSING;
467 return AltosConvert.radio_to_frequency(radio_frequency,
473 boolean use_flash_for_config() {
474 if (product.startsWith("TeleMega"))
476 if (product.startsWith("TeleMetrum-v2"))
478 if (product.startsWith("EasyMega"))
484 public boolean mma655x_inverted() throws AltosUnknownProduct {
485 if (product.startsWith("EasyMega-v1"))
487 if (product.startsWith("TeleMetrum-v2"))
489 if (product.startsWith("TeleMega-v2"))
491 if (product.startsWith("TeleMega-v1"))
493 throw new AltosUnknownProduct(product);
496 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
499 if (main_deploy >= 0)
500 main_deploy = source.main_deploy();
501 if (apogee_delay >= 0)
502 apogee_delay = source.apogee_delay();
503 if (apogee_lockout >= 0)
504 apogee_lockout = source.apogee_lockout();
508 set_frequency(source.radio_frequency());
509 if (radio_enable >= 0)
510 radio_enable = source.radio_enable();
511 if (callsign != null)
512 callsign = source.callsign();
513 if (telemetry_rate >= 0)
514 telemetry_rate = source.telemetry_rate();
517 if (pad_orientation >= 0)
518 pad_orientation = source.pad_orientation();
521 if (flight_log_max >= 0)
522 flight_log_max = source.flight_log_max();
525 if (ignite_mode >= 0)
526 ignite_mode = source.ignite_mode();
530 pyros = source.pyros();
531 if (pyro_firing_time >= 0)
532 pyro_firing_time = source.pyro_firing_time();
535 if (aprs_interval >= 0)
536 aprs_interval = source.aprs_interval();
538 aprs_ssid = source.aprs_ssid();
539 if (aprs_format >= 0)
540 aprs_format = source.aprs_format();
544 beep = source.beep();
546 if (tracker_motion >= 0)
547 tracker_motion = source.tracker_motion();
548 if (tracker_interval >= 0)
549 tracker_interval = source.tracker_interval();
552 public void set_values(AltosConfigValues dest) {
553 dest.set_serial(serial);
554 dest.set_product(product);
555 dest.set_version(version);
556 dest.set_altitude_32(altitude_32);
557 dest.set_main_deploy(main_deploy);
558 dest.set_apogee_delay(apogee_delay);
559 dest.set_apogee_lockout(apogee_lockout);
560 dest.set_radio_calibration(radio_calibration);
561 dest.set_radio_frequency(frequency());
562 dest.set_telemetry_rate(telemetry_rate);
563 boolean max_enabled = true;
565 if (log_space() == 0)
571 switch (log_format) {
572 case AltosLib.AO_LOG_FORMAT_TINY:
576 if (stored_flight > 0)
581 dest.set_flight_log_max_enabled(max_enabled);
582 dest.set_radio_enable(radio_enable);
583 dest.set_flight_log_max_limit(log_space() / 1024);
584 dest.set_flight_log_max(flight_log_max);
585 dest.set_ignite_mode(ignite_mode);
586 dest.set_pad_orientation(pad_orientation);
587 dest.set_callsign(callsign);
589 dest.set_pyros(pyros);
591 dest.set_pyros(null);
592 dest.set_pyro_firing_time(pyro_firing_time);
593 dest.set_aprs_interval(aprs_interval);
594 dest.set_aprs_ssid(aprs_ssid);
595 dest.set_aprs_format(aprs_format);
597 dest.set_tracker_motion(tracker_motion);
598 dest.set_tracker_interval(tracker_interval);
601 public boolean log_has_state() {
602 switch (log_format) {
603 case AltosLib.AO_LOG_FORMAT_TELEGPS:
609 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
612 if (main_deploy >= 0)
613 link.printf("c m %d\n", main_deploy);
614 if (apogee_delay >= 0)
615 link.printf("c d %d\n", apogee_delay);
616 if (apogee_lockout >= 0)
617 link.printf("c L %d\n", apogee_lockout);
620 if (has_frequency()) {
621 boolean has_frequency = radio_frequency >= 0;
622 boolean has_setting = radio_setting > 0;
623 double frequency = frequency();
624 link.set_radio_frequency(frequency,
628 /* When remote, reset the dongle frequency at the same time */
632 link.set_radio_frequency(frequency);
638 if (telemetry_rate >= 0) {
639 link.printf("c T %d\n", telemetry_rate);
643 link.set_telemetry_rate(telemetry_rate);
649 if (callsign != null) {
650 link.printf("c c %s\n", callsign);
654 link.set_callsign(callsign);
660 if (radio_enable >= 0)
661 link.printf("c e %d\n", radio_enable);
664 /* UI doesn't support accel cal */
665 if (pad_orientation >= 0)
666 link.printf("c o %d\n", pad_orientation);
669 if (flight_log_max != 0)
670 link.printf("c l %d\n", flight_log_max);
673 if (ignite_mode >= 0)
674 link.printf("c i %d\n", ignite_mode);
677 /* UI doesn't support AES key config */
681 for (int p = 0; p < pyros.length; p++) {
682 link.printf("c P %s\n",
683 pyros[p].toString());
686 if (pyro_firing_time >= 0)
687 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
690 if (aprs_interval >= 0)
691 link.printf("c A %d\n", aprs_interval);
693 link.printf("c S %d\n", aprs_ssid);
694 if (aprs_format >= 0)
695 link.printf("c C %d\n", aprs_format);
699 link.printf("c b %d\n", beep);
702 if (tracker_motion >= 0 && tracker_interval >= 0)
703 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
706 /* UI doesn't support accel cal */
708 link.printf("c w\n");
712 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
714 link.printf("c s\nf\nv\n");
715 read_link(link, "software-version");
716 switch (log_format) {
717 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
718 case AltosLib.AO_LOG_FORMAT_NONE:
722 read_link(link, "done");