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;
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;
93 public static String get_string(String line, String label) throws ParseException {
94 if (line.startsWith(label)) {
95 String quoted = line.substring(label.length()).trim();
97 if (quoted.startsWith("\""))
98 quoted = quoted.substring(1);
99 if (quoted.endsWith("\""))
100 quoted = quoted.substring(0,quoted.length()-1);
103 throw new ParseException("mismatch", 0);
106 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
107 if (line.startsWith(label)) {
108 String tail = line.substring(label.length()).trim();
109 String[] tokens = tail.split("\\s+");
110 if (tokens.length > 0)
111 return Integer.parseInt(tokens[0]);
113 throw new ParseException("mismatch", 0);
116 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
117 if (line.startsWith(label)) {
118 String tail = line.substring(label.length()).trim();
119 String[] tokens = tail.split("\\s+");
120 if (tokens.length > 1) {
121 int[] values = new int[2];
122 values[0] = Integer.parseInt(tokens[0]);
123 values[1] = Integer.parseInt(tokens[1]);
127 throw new ParseException("mismatch", 0);
130 public Iterator<String> iterator() {
131 return lines.iterator();
134 public int log_space() {
138 if (storage_size > 0) {
139 int space = storage_size;
141 if (storage_erase_unit > 0 && use_flash_for_config())
142 space -= storage_erase_unit;
150 public int log_available() {
151 switch (log_format) {
152 case AltosLib.AO_LOG_FORMAT_TINY:
153 if (stored_flight == 0)
156 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
157 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
160 if (flight_log_max <= 0)
162 int log_max = flight_log_max * 1024;
163 int log_space = log_space();
166 if (stored_flight <= 0)
169 log_used = stored_flight * log_max;
172 if (log_used >= log_space)
175 log_avail = (log_space - log_used) / log_max;
181 public boolean has_monitor_battery() {
182 if (product.startsWith("TeleBT"))
187 int[] parse_version(String v) {
188 String[] parts = v.split("\\.");
189 int r[] = new int[parts.length];
191 for (int i = 0; i < parts.length; i++) {
193 r[i] = AltosLib.fromdec(parts[i]);
194 } catch (NumberFormatException n) {
202 public int compare_version(String other) {
203 int[] me = parse_version(version);
204 int[] them = parse_version(other);
206 int l = Math.min(me.length, them.length);
208 for (int i = 0; i < l; i++) {
209 int d = me[i] - them[i];
220 public void reset() {
221 lines = new LinkedList<String>();
223 manufacturer = "unknown";
227 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
235 radio_frequency = -1;
238 radio_calibration = -1;
244 accel_cal_minus = -1;
245 pad_orientation = -1;
256 pyro_firing_time = -1;
264 tracker_interval = -1;
267 storage_erase_unit = -1;
271 public void parse_line(String line) {
273 /* Version replies */
274 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
275 try { product = get_string(line, "product"); } catch (Exception e) {}
276 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
277 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
278 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
279 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
280 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
281 try { version = get_string(line, "software-version"); } catch (Exception e) {}
283 /* Version also contains MS5607 info, which we ignore here */
285 /* Config show replies */
288 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
289 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
290 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
294 radio_frequency = get_int(line, "Frequency:");
295 if (radio_frequency < 0)
296 radio_frequency = 434550;
297 } catch (Exception e) {}
298 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
299 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
300 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
301 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
303 /* Old HAS_RADIO values */
304 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
305 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
309 if (line.startsWith("Accel cal")) {
310 String[] bits = line.split("\\s+");
311 if (bits.length >= 6) {
312 accel_cal_plus = Integer.parseInt(bits[3]);
313 accel_cal_minus = Integer.parseInt(bits[5]);
316 } catch (Exception e) {}
317 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
320 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
321 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
324 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
327 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
331 npyro = get_int(line, "Pyro-count:");
332 pyros = new AltosPyro[npyro];
334 } catch (Exception e) {}
337 AltosPyro p = new AltosPyro(pyro, line);
340 } catch (Exception e) {}
342 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
345 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
346 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
349 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
353 int[] values = get_values(line, "Tracker setting:");
354 tracker_motion = values[0];
355 tracker_interval = values[1];
356 } catch (Exception e) {}
358 /* Storage info replies */
359 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
360 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
362 /* Log listing replies */
363 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
366 public AltosConfigData() {
370 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
372 String line = link.get_reply();
374 throw new TimeoutException();
375 if (line.contains("Syntax error"))
377 this.parse_line(line);
379 /* signals the end of the version info */
380 if (line.startsWith(finished))
385 public boolean has_frequency() {
386 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
389 public boolean has_telemetry_rate() {
390 return telemetry_rate >= 0;
393 public void set_frequency(double freq) {
394 int frequency = radio_frequency;
395 int setting = radio_setting;
398 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
400 } else if (setting > 0) {
401 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
405 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
409 public double frequency() {
410 int channel = radio_channel;
411 int setting = radio_setting;
413 if (radio_frequency < 0 && channel < 0 && setting < 0)
421 return AltosConvert.radio_to_frequency(radio_frequency,
427 boolean use_flash_for_config() {
428 if (product.startsWith("TeleMega"))
430 if (product.startsWith("TeleMetrum-v2"))
436 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
439 if (main_deploy >= 0)
440 main_deploy = source.main_deploy();
441 if (apogee_delay >= 0)
442 apogee_delay = source.apogee_delay();
443 if (apogee_lockout >= 0)
444 apogee_lockout = source.apogee_lockout();
448 set_frequency(source.radio_frequency());
449 if (radio_enable >= 0)
450 radio_enable = source.radio_enable();
451 if (callsign != null)
452 callsign = source.callsign();
453 if (radio_calibration >= 0)
454 radio_calibration = source.radio_calibration();
455 if (telemetry_rate >= 0)
456 telemetry_rate = source.telemetry_rate();
459 if (pad_orientation >= 0)
460 pad_orientation = source.pad_orientation();
463 if (flight_log_max >= 0)
464 flight_log_max = source.flight_log_max();
467 if (ignite_mode >= 0)
468 ignite_mode = source.ignite_mode();
472 pyros = source.pyros();
473 if (pyro_firing_time >= 0)
474 pyro_firing_time = source.pyro_firing_time();
477 if (aprs_interval >= 0)
478 aprs_interval = source.aprs_interval();
480 aprs_ssid = source.aprs_ssid();
484 beep = source.beep();
486 if (tracker_motion >= 0)
487 tracker_motion = source.tracker_motion();
488 if (tracker_interval >= 0)
489 tracker_interval = source.tracker_interval();
492 public void set_values(AltosConfigValues dest) {
493 dest.set_serial(serial);
494 dest.set_product(product);
495 dest.set_version(version);
496 dest.set_altitude_32(altitude_32);
497 dest.set_main_deploy(main_deploy);
498 dest.set_apogee_delay(apogee_delay);
499 dest.set_apogee_lockout(apogee_lockout);
500 dest.set_radio_calibration(radio_calibration);
501 dest.set_radio_frequency(frequency());
502 dest.set_telemetry_rate(telemetry_rate);
503 boolean max_enabled = true;
505 if (log_space() == 0)
511 switch (log_format) {
512 case AltosLib.AO_LOG_FORMAT_TINY:
516 if (stored_flight > 0)
521 dest.set_flight_log_max_enabled(max_enabled);
522 dest.set_radio_enable(radio_enable);
523 dest.set_flight_log_max_limit(log_space() / 1024);
524 dest.set_flight_log_max(flight_log_max);
525 dest.set_ignite_mode(ignite_mode);
526 dest.set_pad_orientation(pad_orientation);
527 dest.set_callsign(callsign);
529 dest.set_pyros(pyros);
531 dest.set_pyros(null);
532 dest.set_pyro_firing_time(pyro_firing_time);
533 dest.set_aprs_interval(aprs_interval);
534 dest.set_aprs_ssid(aprs_ssid);
536 dest.set_tracker_motion(tracker_motion);
537 dest.set_tracker_interval(tracker_interval);
540 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
543 if (main_deploy >= 0)
544 link.printf("c m %d\n", main_deploy);
545 if (apogee_delay >= 0)
546 link.printf("c d %d\n", apogee_delay);
547 if (apogee_lockout >= 0)
548 link.printf("c L %d\n", apogee_lockout);
550 /* Don't mess with radio calibration when remote */
551 if (radio_calibration > 0 && !remote)
552 link.printf("c f %d\n", radio_calibration);
555 if (has_frequency()) {
556 boolean has_frequency = radio_frequency >= 0;
557 boolean has_setting = radio_setting > 0;
558 double frequency = frequency();
559 link.set_radio_frequency(frequency,
563 /* When remote, reset the dongle frequency at the same time */
567 link.set_radio_frequency(frequency);
573 if (telemetry_rate >= 0) {
574 link.printf("c T %d\n", telemetry_rate);
578 link.set_telemetry_rate(telemetry_rate);
584 if (callsign != null) {
585 link.printf("c c %s\n", callsign);
589 link.set_callsign(callsign);
595 if (radio_enable >= 0)
596 link.printf("c e %d\n", radio_enable);
599 /* UI doesn't support accel cal */
600 if (pad_orientation >= 0)
601 link.printf("c o %d\n", pad_orientation);
604 if (flight_log_max != 0)
605 link.printf("c l %d\n", flight_log_max);
608 if (ignite_mode >= 0)
609 link.printf("c i %d\n", ignite_mode);
612 /* UI doesn't support AES key config */
616 for (int p = 0; p < pyros.length; p++) {
617 link.printf("c P %s\n",
618 pyros[p].toString());
621 if (pyro_firing_time >= 0)
622 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
625 if (aprs_interval >= 0)
626 link.printf("c A %d\n", aprs_interval);
628 link.printf("c S %d\n", aprs_ssid);
632 link.printf("c b %d\n", beep);
635 if (tracker_motion >= 0 && tracker_interval >= 0)
636 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
638 link.printf("c w\n");
642 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
644 link.printf("c s\nf\nv\n");
645 read_link(link, "software-version");
646 switch (log_format) {
647 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
648 case AltosLib.AO_LOG_FORMAT_NONE:
652 read_link(link, "done");