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_5;
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 public int telemetry_rate;
50 /* Old HAS_RADIO values */
51 public int radio_channel;
52 public int radio_setting;
55 public int accel_cal_plus, accel_cal_minus;
56 public int pad_orientation;
59 public int flight_log_max;
62 public int ignite_mode;
65 public String aes_key;
68 public AltosPyro[] pyros;
71 public double pyro_firing_time;
74 public int aprs_interval;
80 /* Storage info replies */
81 public int storage_size;
82 public int storage_erase_unit;
84 /* Log listing replies */
85 public int stored_flight;
88 public int tracker_motion;
89 public int tracker_interval;
91 public static String get_string(String line, String label) throws ParseException {
92 if (line.startsWith(label)) {
93 String quoted = line.substring(label.length()).trim();
95 if (quoted.startsWith("\""))
96 quoted = quoted.substring(1);
97 if (quoted.endsWith("\""))
98 quoted = quoted.substring(0,quoted.length()-1);
101 throw new ParseException("mismatch", 0);
104 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
105 if (line.startsWith(label)) {
106 String tail = line.substring(label.length()).trim();
107 String[] tokens = tail.split("\\s+");
108 if (tokens.length > 0)
109 return Integer.parseInt(tokens[0]);
111 throw new ParseException("mismatch", 0);
114 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
115 if (line.startsWith(label)) {
116 String tail = line.substring(label.length()).trim();
117 String[] tokens = tail.split("\\s+");
118 if (tokens.length > 1) {
119 int[] values = new int[2];
120 values[0] = Integer.parseInt(tokens[0]);
121 values[1] = Integer.parseInt(tokens[1]);
125 throw new ParseException("mismatch", 0);
128 public Iterator<String> iterator() {
129 return lines.iterator();
132 public int log_space() {
136 if (storage_size > 0) {
137 int space = storage_size;
139 if (storage_erase_unit > 0 && use_flash_for_config())
140 space -= storage_erase_unit;
148 public int log_available() {
149 switch (log_format) {
150 case AltosLib.AO_LOG_FORMAT_TINY:
151 if (stored_flight == 0)
154 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
155 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
158 if (flight_log_max <= 0)
160 int log_max = flight_log_max * 1024;
161 int log_space = log_space();
164 if (stored_flight <= 0)
167 log_used = stored_flight * log_max;
170 if (log_used >= log_space)
173 log_avail = (log_space - log_used) / log_max;
179 public boolean has_monitor_battery() {
180 if (product.startsWith("TeleBT"))
185 int[] parse_version(String v) {
186 String[] parts = v.split("\\.");
187 int r[] = new int[parts.length];
189 for (int i = 0; i < parts.length; i++) {
191 r[i] = AltosLib.fromdec(parts[i]);
192 } catch (NumberFormatException n) {
200 public int compare_version(String other) {
201 int[] me = parse_version(version);
202 int[] them = parse_version(other);
204 int l = Math.min(me.length, them.length);
206 for (int i = 0; i < l; i++) {
207 int d = me[i] - them[i];
218 public void reset() {
219 lines = new LinkedList<String>();
221 manufacturer = "unknown";
225 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
233 radio_frequency = -1;
236 radio_calibration = -1;
242 accel_cal_minus = -1;
243 pad_orientation = -1;
253 pyro_firing_time = -1;
261 tracker_interval = -1;
264 storage_erase_unit = -1;
268 public void parse_line(String line) {
270 /* Version replies */
271 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
272 try { product = get_string(line, "product"); } catch (Exception e) {}
273 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
274 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
275 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
276 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
277 try { version = get_string(line, "software-version"); } catch (Exception e) {}
279 /* Version also contains MS5607 info, which we ignore here */
281 /* Config show replies */
284 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
285 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
286 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
290 radio_frequency = get_int(line, "Frequency:");
291 if (radio_frequency < 0)
292 radio_frequency = 434550;
293 } catch (Exception e) {}
294 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
295 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
296 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
297 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
299 /* Old HAS_RADIO values */
300 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
301 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
305 if (line.startsWith("Accel cal")) {
306 String[] bits = line.split("\\s+");
307 if (bits.length >= 6) {
308 accel_cal_plus = Integer.parseInt(bits[3]);
309 accel_cal_minus = Integer.parseInt(bits[5]);
312 } catch (Exception e) {}
313 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
316 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
319 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
322 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
326 npyro = get_int(line, "Pyro-count:");
327 pyros = new AltosPyro[npyro];
329 } catch (Exception e) {}
332 AltosPyro p = new AltosPyro(pyro, line);
335 } catch (Exception e) {}
337 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
340 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
341 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
344 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
348 int[] values = get_values(line, "Tracker setting:");
349 tracker_motion = values[0];
350 tracker_interval = values[1];
351 } catch (Exception e) {}
353 /* Storage info replies */
354 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
355 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
357 /* Log listing replies */
358 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
361 public AltosConfigData() {
365 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
367 String line = link.get_reply();
369 throw new TimeoutException();
370 if (line.contains("Syntax error"))
372 this.parse_line(line);
374 /* signals the end of the version info */
375 if (line.startsWith(finished))
380 public boolean has_frequency() {
381 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
384 public void set_frequency(double freq) {
385 int frequency = radio_frequency;
386 int setting = radio_setting;
389 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
391 } else if (setting > 0) {
392 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
396 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
400 public double frequency() {
401 int channel = radio_channel;
402 int setting = radio_setting;
404 if (radio_frequency < 0 && channel < 0 && setting < 0)
412 return AltosConvert.radio_to_frequency(radio_frequency,
418 boolean use_flash_for_config() {
419 if (product.startsWith("TeleMega"))
421 if (product.startsWith("TeleMetrum-v2"))
427 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
430 if (main_deploy >= 0)
431 main_deploy = source.main_deploy();
432 if (apogee_delay >= 0)
433 apogee_delay = source.apogee_delay();
434 if (apogee_lockout >= 0)
435 apogee_lockout = source.apogee_lockout();
439 set_frequency(source.radio_frequency());
440 if (radio_enable >= 0)
441 radio_enable = source.radio_enable();
442 if (callsign != null)
443 callsign = source.callsign();
444 if (radio_calibration >= 0)
445 radio_calibration = source.radio_calibration();
446 if (telemetry_rate >= 0)
447 telemetry_rate = source.telemetry_rate();
450 if (pad_orientation >= 0)
451 pad_orientation = source.pad_orientation();
454 if (flight_log_max >= 0)
455 flight_log_max = source.flight_log_max();
458 if (ignite_mode >= 0)
459 ignite_mode = source.ignite_mode();
463 pyros = source.pyros();
464 if (pyro_firing_time >= 0)
465 pyro_firing_time = source.pyro_firing_time();
468 if (aprs_interval >= 0)
469 aprs_interval = source.aprs_interval();
471 aprs_ssid = source.aprs_ssid();
475 beep = source.beep();
477 if (tracker_motion >= 0)
478 tracker_motion = source.tracker_motion();
479 if (tracker_interval >= 0)
480 tracker_interval = source.tracker_interval();
483 public void set_values(AltosConfigValues dest) {
484 dest.set_serial(serial);
485 dest.set_product(product);
486 dest.set_version(version);
487 dest.set_main_deploy(main_deploy);
488 dest.set_apogee_delay(apogee_delay);
489 dest.set_apogee_lockout(apogee_lockout);
490 dest.set_radio_calibration(radio_calibration);
491 dest.set_radio_frequency(frequency());
492 dest.set_telemetry_rate(telemetry_rate);
493 boolean max_enabled = true;
495 if (log_space() == 0)
498 switch (log_format) {
499 case AltosLib.AO_LOG_FORMAT_TINY:
503 if (stored_flight > 0)
508 dest.set_flight_log_max_enabled(max_enabled);
509 dest.set_radio_enable(radio_enable);
510 dest.set_flight_log_max_limit(log_space() / 1024);
511 dest.set_flight_log_max(flight_log_max);
512 dest.set_ignite_mode(ignite_mode);
513 dest.set_pad_orientation(pad_orientation);
514 dest.set_callsign(callsign);
516 dest.set_pyros(pyros);
518 dest.set_pyros(null);
519 dest.set_pyro_firing_time(pyro_firing_time);
520 dest.set_aprs_interval(aprs_interval);
521 dest.set_aprs_ssid(aprs_ssid);
523 dest.set_tracker_motion(tracker_motion);
524 dest.set_tracker_interval(tracker_interval);
527 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
530 if (main_deploy >= 0)
531 link.printf("c m %d\n", main_deploy);
532 if (apogee_delay >= 0)
533 link.printf("c d %d\n", apogee_delay);
534 if (apogee_lockout >= 0)
535 link.printf("c L %d\n", apogee_lockout);
537 /* Don't mess with radio calibration when remote */
538 if (radio_calibration > 0 && !remote)
539 link.printf("c f %d\n", radio_calibration);
542 if (has_frequency()) {
543 boolean has_frequency = radio_frequency >= 0;
544 boolean has_setting = radio_setting > 0;
545 double frequency = frequency();
546 link.set_radio_frequency(frequency,
550 /* When remote, reset the dongle frequency at the same time */
553 link.set_radio_frequency(frequency);
558 if (callsign != null)
559 link.printf("c c %s\n", callsign);
560 if (radio_enable >= 0)
561 link.printf("c e %d\n", radio_enable);
563 if (telemetry_rate >= 0)
564 link.printf("c T %d\n", telemetry_rate);
567 /* UI doesn't support accel cal */
568 if (pad_orientation >= 0)
569 link.printf("c o %d\n", pad_orientation);
572 if (flight_log_max != 0)
573 link.printf("c l %d\n", flight_log_max);
576 if (ignite_mode >= 0)
577 link.printf("c i %d\n", ignite_mode);
580 /* UI doesn't support AES key config */
584 for (int p = 0; p < pyros.length; p++) {
585 link.printf("c P %s\n",
586 pyros[p].toString());
589 if (pyro_firing_time >= 0)
590 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
593 if (aprs_interval >= 0)
594 link.printf("c A %d\n", aprs_interval);
596 link.printf("c S %d\n", aprs_ssid);
600 link.printf("c b %d\n", beep);
603 if (tracker_motion >= 0 && tracker_interval >= 0)
604 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
606 link.printf("c w\n");
610 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
612 link.printf("c s\nf\nv\n");
613 read_link(link, "software-version");
614 switch (log_format) {
615 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
616 case AltosLib.AO_LOG_FORMAT_NONE:
620 read_link(link, "done");