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_9;
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;
78 public int aprs_format;
83 /* Storage info replies */
84 public int storage_size;
85 public int storage_erase_unit;
87 /* Log listing replies */
88 public int stored_flight;
91 public int tracker_motion;
92 public int tracker_interval;
95 public int accel_zero_along, accel_zero_across, accel_zero_through;
98 public int ms5607_reserved;
99 public int ms5607_sens;
100 public int ms5607_off;
101 public int ms5607_tcs;
102 public int ms5607_tco;
103 public int ms5607_tref;
104 public int ms5607_tempsens;
105 public int ms5607_crc;
107 public static String get_string(String line, String label) throws ParseException {
108 if (line.startsWith(label)) {
109 String quoted = line.substring(label.length()).trim();
111 if (quoted.startsWith("\""))
112 quoted = quoted.substring(1);
113 if (quoted.endsWith("\""))
114 quoted = quoted.substring(0,quoted.length()-1);
117 throw new ParseException("mismatch", 0);
120 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
121 if (line.startsWith(label)) {
122 String tail = line.substring(label.length()).trim();
123 String[] tokens = tail.split("\\s+");
124 if (tokens.length > 0)
125 return Integer.parseInt(tokens[0]);
127 throw new ParseException("mismatch", 0);
130 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
131 if (line.startsWith(label)) {
132 String tail = line.substring(label.length()).trim();
133 String[] tokens = tail.split("\\s+");
134 if (tokens.length > 1) {
135 int[] values = new int[2];
136 values[0] = Integer.parseInt(tokens[0]);
137 values[1] = Integer.parseInt(tokens[1]);
141 throw new ParseException("mismatch", 0);
144 public Iterator<String> iterator() {
145 return lines.iterator();
148 public int log_space() {
152 if (storage_size > 0) {
153 int space = storage_size;
155 if (storage_erase_unit > 0 && use_flash_for_config())
156 space -= storage_erase_unit;
164 public int log_available() {
165 switch (log_format) {
166 case AltosLib.AO_LOG_FORMAT_TINY:
167 if (stored_flight == 0)
170 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
171 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
174 if (flight_log_max <= 0)
176 int log_max = flight_log_max * 1024;
177 int log_space = log_space();
180 if (stored_flight <= 0)
183 log_used = stored_flight * log_max;
186 if (log_used >= log_space)
189 log_avail = (log_space - log_used) / log_max;
195 public boolean has_monitor_battery() {
196 if (product.startsWith("TeleBT"))
201 int[] parse_version(String v) {
202 String[] parts = v.split("\\.");
203 int r[] = new int[parts.length];
205 for (int i = 0; i < parts.length; i++) {
207 r[i] = AltosLib.fromdec(parts[i]);
208 } catch (NumberFormatException n) {
216 public int compare_version(String other) {
217 int[] me = parse_version(version);
218 int[] them = parse_version(other);
220 int l = Math.min(me.length, them.length);
222 for (int i = 0; i < l; i++) {
223 int d = me[i] - them[i];
234 public void reset() {
235 lines = new LinkedList<String>();
237 manufacturer = "unknown";
241 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
249 radio_frequency = -1;
252 radio_calibration = -1;
258 accel_cal_minus = -1;
259 pad_orientation = -1;
270 pyro_firing_time = -1;
279 tracker_interval = -1;
282 storage_erase_unit = -1;
285 accel_zero_along = -1;
286 accel_zero_across = -1;
287 accel_zero_through = -1;
290 public void parse_line(String line) {
292 /* Version replies */
293 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
294 try { product = get_string(line, "product"); } catch (Exception e) {}
295 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
296 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
297 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
298 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
299 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
300 try { version = get_string(line, "software-version"); } catch (Exception e) {}
302 /* Version also contains MS5607 info, which we ignore here */
304 try { ms5607_reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
305 try { ms5607_sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
306 try { ms5607_off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
307 try { ms5607_tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
308 try { ms5607_tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
309 try { ms5607_tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
310 try { ms5607_tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
311 try { ms5607_crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
313 /* Config show replies */
316 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
317 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
318 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
322 radio_frequency = get_int(line, "Frequency:");
323 if (radio_frequency < 0)
324 radio_frequency = 434550;
325 } catch (Exception e) {}
326 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
327 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
328 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
329 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
331 /* Old HAS_RADIO values */
332 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
333 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
337 if (line.startsWith("Accel cal")) {
338 String[] bits = line.split("\\s+");
339 if (bits.length >= 6) {
340 accel_cal_plus = Integer.parseInt(bits[3]);
341 accel_cal_minus = Integer.parseInt(bits[5]);
344 } catch (Exception e) {}
345 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
348 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
349 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
352 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
355 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
359 npyro = get_int(line, "Pyro-count:");
360 pyros = new AltosPyro[npyro];
362 } catch (Exception e) {}
365 AltosPyro p = new AltosPyro(pyro, line);
368 } catch (Exception e) {}
370 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
373 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
374 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
375 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
378 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
382 int[] values = get_values(line, "Tracker setting:");
383 tracker_motion = values[0];
384 tracker_interval = values[1];
385 } catch (Exception e) {}
387 /* Storage info replies */
388 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
389 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
391 /* Log listing replies */
392 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
396 if (line.startsWith("IMU call along")) {
397 String[] bits = line.split("\\s+");
398 if (bits.length >= 8) {
399 accel_zero_along = Integer.parseInt(bits[3]);
400 accel_zero_across = Integer.parseInt(bits[5]);
401 accel_zero_through = Integer.parseInt(bits[7]);
404 } catch (Exception e) {}
407 public AltosConfigData() {
411 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
413 String line = link.get_reply();
415 throw new TimeoutException();
416 if (line.contains("Syntax error"))
418 this.parse_line(line);
420 /* signals the end of the version info */
421 if (line.startsWith(finished))
426 public boolean has_frequency() {
427 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
430 public boolean has_telemetry_rate() {
431 return telemetry_rate >= 0;
434 public void set_frequency(double freq) {
435 int frequency = radio_frequency;
436 int setting = radio_setting;
439 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
441 } else if (setting > 0) {
442 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
446 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
450 public double frequency() {
451 int channel = radio_channel;
452 int setting = radio_setting;
454 if (radio_frequency < 0 && channel < 0 && setting < 0)
462 return AltosConvert.radio_to_frequency(radio_frequency,
468 boolean use_flash_for_config() {
469 if (product.startsWith("TeleMega"))
471 if (product.startsWith("TeleMetrum-v2"))
473 if (product.startsWith("EasyMega"))
479 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
482 if (main_deploy >= 0)
483 main_deploy = source.main_deploy();
484 if (apogee_delay >= 0)
485 apogee_delay = source.apogee_delay();
486 if (apogee_lockout >= 0)
487 apogee_lockout = source.apogee_lockout();
491 set_frequency(source.radio_frequency());
492 if (radio_enable >= 0)
493 radio_enable = source.radio_enable();
494 if (callsign != null)
495 callsign = source.callsign();
496 if (radio_calibration >= 0)
497 radio_calibration = source.radio_calibration();
498 if (telemetry_rate >= 0)
499 telemetry_rate = source.telemetry_rate();
502 if (pad_orientation >= 0)
503 pad_orientation = source.pad_orientation();
506 if (flight_log_max >= 0)
507 flight_log_max = source.flight_log_max();
510 if (ignite_mode >= 0)
511 ignite_mode = source.ignite_mode();
515 pyros = source.pyros();
516 if (pyro_firing_time >= 0)
517 pyro_firing_time = source.pyro_firing_time();
520 if (aprs_interval >= 0)
521 aprs_interval = source.aprs_interval();
523 aprs_ssid = source.aprs_ssid();
524 if (aprs_format >= 0)
525 aprs_format = source.aprs_format();
529 beep = source.beep();
531 if (tracker_motion >= 0)
532 tracker_motion = source.tracker_motion();
533 if (tracker_interval >= 0)
534 tracker_interval = source.tracker_interval();
537 public void set_values(AltosConfigValues dest) {
538 dest.set_serial(serial);
539 dest.set_product(product);
540 dest.set_version(version);
541 dest.set_altitude_32(altitude_32);
542 dest.set_main_deploy(main_deploy);
543 dest.set_apogee_delay(apogee_delay);
544 dest.set_apogee_lockout(apogee_lockout);
545 dest.set_radio_calibration(radio_calibration);
546 dest.set_radio_frequency(frequency());
547 dest.set_telemetry_rate(telemetry_rate);
548 boolean max_enabled = true;
550 if (log_space() == 0)
556 switch (log_format) {
557 case AltosLib.AO_LOG_FORMAT_TINY:
561 if (stored_flight > 0)
566 dest.set_flight_log_max_enabled(max_enabled);
567 dest.set_radio_enable(radio_enable);
568 dest.set_flight_log_max_limit(log_space() / 1024);
569 dest.set_flight_log_max(flight_log_max);
570 dest.set_ignite_mode(ignite_mode);
571 dest.set_pad_orientation(pad_orientation);
572 dest.set_callsign(callsign);
574 dest.set_pyros(pyros);
576 dest.set_pyros(null);
577 dest.set_pyro_firing_time(pyro_firing_time);
578 dest.set_aprs_interval(aprs_interval);
579 dest.set_aprs_ssid(aprs_ssid);
580 dest.set_aprs_format(aprs_format);
582 dest.set_tracker_motion(tracker_motion);
583 dest.set_tracker_interval(tracker_interval);
586 public boolean log_has_state() {
587 switch (log_format) {
588 case AltosLib.AO_LOG_FORMAT_TELEGPS:
594 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
597 if (main_deploy >= 0)
598 link.printf("c m %d\n", main_deploy);
599 if (apogee_delay >= 0)
600 link.printf("c d %d\n", apogee_delay);
601 if (apogee_lockout >= 0)
602 link.printf("c L %d\n", apogee_lockout);
604 /* Don't mess with radio calibration when remote */
605 if (radio_calibration > 0 && !remote)
606 link.printf("c f %d\n", radio_calibration);
609 if (has_frequency()) {
610 boolean has_frequency = radio_frequency >= 0;
611 boolean has_setting = radio_setting > 0;
612 double frequency = frequency();
613 link.set_radio_frequency(frequency,
617 /* When remote, reset the dongle frequency at the same time */
621 link.set_radio_frequency(frequency);
627 if (telemetry_rate >= 0) {
628 link.printf("c T %d\n", telemetry_rate);
632 link.set_telemetry_rate(telemetry_rate);
638 if (callsign != null) {
639 link.printf("c c %s\n", callsign);
643 link.set_callsign(callsign);
649 if (radio_enable >= 0)
650 link.printf("c e %d\n", radio_enable);
653 /* UI doesn't support accel cal */
654 if (pad_orientation >= 0)
655 link.printf("c o %d\n", pad_orientation);
658 if (flight_log_max != 0)
659 link.printf("c l %d\n", flight_log_max);
662 if (ignite_mode >= 0)
663 link.printf("c i %d\n", ignite_mode);
666 /* UI doesn't support AES key config */
670 for (int p = 0; p < pyros.length; p++) {
671 link.printf("c P %s\n",
672 pyros[p].toString());
675 if (pyro_firing_time >= 0)
676 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
679 if (aprs_interval >= 0)
680 link.printf("c A %d\n", aprs_interval);
682 link.printf("c S %d\n", aprs_ssid);
683 if (aprs_format >= 0)
684 link.printf("c C %d\n", aprs_format);
688 link.printf("c b %d\n", beep);
691 if (tracker_motion >= 0 && tracker_interval >= 0)
692 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
695 /* UI doesn't support accel cal */
697 link.printf("c w\n");
701 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
703 link.printf("c s\nf\nv\n");
704 read_link(link, "software-version");
705 switch (log_format) {
706 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
707 case AltosLib.AO_LOG_FORMAT_NONE:
711 read_link(link, "done");