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_4;
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 /* Old HAS_RADIO values */
50 public int radio_channel;
51 public int radio_setting;
54 public int accel_cal_plus, accel_cal_minus;
55 public int pad_orientation;
58 public int flight_log_max;
61 public int ignite_mode;
64 public String aes_key;
67 public AltosPyro[] pyros;
70 public double pyro_firing_time;
73 public int aprs_interval;
78 /* Storage info replies */
79 public int storage_size;
80 public int storage_erase_unit;
82 /* Log listing replies */
83 public int stored_flight;
86 public int tracker_motion;
87 public int tracker_interval;
89 public static String get_string(String line, String label) throws ParseException {
90 if (line.startsWith(label)) {
91 String quoted = line.substring(label.length()).trim();
93 if (quoted.startsWith("\""))
94 quoted = quoted.substring(1);
95 if (quoted.endsWith("\""))
96 quoted = quoted.substring(0,quoted.length()-1);
99 throw new ParseException("mismatch", 0);
102 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
103 if (line.startsWith(label)) {
104 String tail = line.substring(label.length()).trim();
105 String[] tokens = tail.split("\\s+");
106 if (tokens.length > 0)
107 return Integer.parseInt(tokens[0]);
109 throw new ParseException("mismatch", 0);
112 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
113 if (line.startsWith(label)) {
114 String tail = line.substring(label.length()).trim();
115 String[] tokens = tail.split("\\s+");
116 if (tokens.length > 1) {
117 int[] values = new int[2];
118 values[0] = Integer.parseInt(tokens[0]);
119 values[1] = Integer.parseInt(tokens[1]);
123 throw new ParseException("mismatch", 0);
126 public Iterator<String> iterator() {
127 return lines.iterator();
130 public int log_space() {
134 if (storage_size > 0) {
135 int space = storage_size;
137 if (storage_erase_unit > 0 && use_flash_for_config())
138 space -= storage_erase_unit;
146 public int log_available() {
147 switch (log_format) {
148 case AltosLib.AO_LOG_FORMAT_TINY:
149 if (stored_flight == 0)
152 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
153 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
156 if (flight_log_max <= 0)
158 int log_max = flight_log_max * 1024;
159 int log_space = log_space();
162 if (stored_flight <= 0)
165 log_used = stored_flight * log_max;
168 if (log_used >= log_space)
171 log_avail = (log_space - log_used) / log_max;
177 public boolean has_monitor_battery() {
178 if (product.startsWith("TeleBT"))
183 int[] parse_version(String v) {
184 String[] parts = v.split("\\.");
185 int r[] = new int[parts.length];
187 for (int i = 0; i < parts.length; i++) {
189 r[i] = AltosLib.fromdec(parts[i]);
190 } catch (NumberFormatException n) {
198 public int compare_version(String other) {
199 int[] me = parse_version(version);
200 int[] them = parse_version(other);
202 int l = Math.min(me.length, them.length);
204 for (int i = 0; i < l; i++) {
205 int d = me[i] - them[i];
216 public void reset() {
217 lines = new LinkedList<String>();
219 manufacturer = "unknown";
223 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
231 radio_frequency = -1;
234 radio_calibration = -1;
239 accel_cal_minus = -1;
240 pad_orientation = -1;
250 pyro_firing_time = -1;
257 tracker_interval = -1;
260 storage_erase_unit = -1;
264 public void parse_line(String line) {
266 /* Version replies */
267 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
268 try { product = get_string(line, "product"); } catch (Exception e) {}
269 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
270 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
271 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
272 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
273 try { version = get_string(line, "software-version"); } catch (Exception e) {}
275 /* Version also contains MS5607 info, which we ignore here */
277 /* Config show replies */
280 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
281 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
282 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
286 radio_frequency = get_int(line, "Frequency:");
287 if (radio_frequency < 0)
288 radio_frequency = 434550;
289 } catch (Exception e) {}
290 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
291 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
292 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
294 /* Old HAS_RADIO values */
295 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
296 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
300 if (line.startsWith("Accel cal")) {
301 String[] bits = line.split("\\s+");
302 if (bits.length >= 6) {
303 accel_cal_plus = Integer.parseInt(bits[3]);
304 accel_cal_minus = Integer.parseInt(bits[5]);
307 } catch (Exception e) {}
308 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
311 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
314 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
317 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
321 npyro = get_int(line, "Pyro-count:");
322 pyros = new AltosPyro[npyro];
324 } catch (Exception e) {}
327 AltosPyro p = new AltosPyro(pyro, line);
330 } catch (Exception e) {}
332 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
335 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
338 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
342 int[] values = get_values(line, "Tracker setting:");
343 tracker_motion = values[0];
344 tracker_interval = values[1];
345 } catch (Exception e) {}
347 /* Storage info replies */
348 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
349 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
351 /* Log listing replies */
352 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
355 public AltosConfigData() {
359 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
361 String line = link.get_reply();
363 throw new TimeoutException();
364 if (line.contains("Syntax error"))
366 this.parse_line(line);
368 /* signals the end of the version info */
369 if (line.startsWith(finished))
374 public boolean has_frequency() {
375 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
378 public void set_frequency(double freq) {
379 int frequency = radio_frequency;
380 int setting = radio_setting;
383 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
385 } else if (setting > 0) {
386 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
390 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
394 public double frequency() {
395 int channel = radio_channel;
396 int setting = radio_setting;
398 if (radio_frequency < 0 && channel < 0 && setting < 0)
406 return AltosConvert.radio_to_frequency(radio_frequency,
412 boolean use_flash_for_config() {
413 if (product.startsWith("TeleMega"))
415 if (product.startsWith("TeleMetrum-v2"))
421 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
424 if (main_deploy >= 0)
425 main_deploy = source.main_deploy();
426 if (apogee_delay >= 0)
427 apogee_delay = source.apogee_delay();
428 if (apogee_lockout >= 0)
429 apogee_lockout = source.apogee_lockout();
433 set_frequency(source.radio_frequency());
434 if (radio_enable >= 0)
435 radio_enable = source.radio_enable();
436 if (callsign != null)
437 callsign = source.callsign();
438 if (radio_calibration >= 0)
439 radio_calibration = source.radio_calibration();
442 if (pad_orientation >= 0)
443 pad_orientation = source.pad_orientation();
446 if (flight_log_max >= 0)
447 flight_log_max = source.flight_log_max();
450 if (ignite_mode >= 0)
451 ignite_mode = source.ignite_mode();
455 pyros = source.pyros();
456 if (pyro_firing_time >= 0)
457 pyro_firing_time = source.pyro_firing_time();
460 if (aprs_interval >= 0)
461 aprs_interval = source.aprs_interval();
465 beep = source.beep();
467 if (tracker_motion >= 0)
468 tracker_motion = source.tracker_motion();
469 if (tracker_interval >= 0)
470 tracker_interval = source.tracker_interval();
473 public void set_values(AltosConfigValues dest) {
474 dest.set_serial(serial);
475 dest.set_product(product);
476 dest.set_version(version);
477 dest.set_main_deploy(main_deploy);
478 dest.set_apogee_delay(apogee_delay);
479 dest.set_apogee_lockout(apogee_lockout);
480 dest.set_radio_calibration(radio_calibration);
481 dest.set_radio_frequency(frequency());
482 boolean max_enabled = true;
484 if (log_space() == 0)
487 switch (log_format) {
488 case AltosLib.AO_LOG_FORMAT_TINY:
492 if (stored_flight > 0)
497 dest.set_flight_log_max_enabled(max_enabled);
498 dest.set_radio_enable(radio_enable);
499 dest.set_flight_log_max_limit(log_space() / 1024);
500 dest.set_flight_log_max(flight_log_max);
501 dest.set_ignite_mode(ignite_mode);
502 dest.set_pad_orientation(pad_orientation);
503 dest.set_callsign(callsign);
505 dest.set_pyros(pyros);
507 dest.set_pyros(null);
508 dest.set_pyro_firing_time(pyro_firing_time);
509 dest.set_aprs_interval(aprs_interval);
511 dest.set_tracker_motion(tracker_motion);
512 dest.set_tracker_interval(tracker_interval);
515 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
518 if (main_deploy >= 0)
519 link.printf("c m %d\n", main_deploy);
520 if (apogee_delay >= 0)
521 link.printf("c d %d\n", apogee_delay);
522 if (apogee_lockout >= 0)
523 link.printf("c L %d\n", apogee_lockout);
525 /* Don't mess with radio calibration when remote */
526 if (radio_calibration > 0 && !remote)
527 link.printf("c f %d\n", radio_calibration);
530 if (has_frequency()) {
531 boolean has_frequency = radio_frequency >= 0;
532 boolean has_setting = radio_setting > 0;
533 double frequency = frequency();
534 link.set_radio_frequency(frequency,
538 /* When remote, reset the dongle frequency at the same time */
541 link.set_radio_frequency(frequency);
546 if (callsign != null)
547 link.printf("c c %s\n", callsign);
548 if (radio_enable >= 0)
549 link.printf("c e %d\n", radio_enable);
552 /* UI doesn't support accel cal */
553 if (pad_orientation >= 0)
554 link.printf("c o %d\n", pad_orientation);
557 if (flight_log_max != 0)
558 link.printf("c l %d\n", flight_log_max);
561 if (ignite_mode >= 0)
562 link.printf("c i %d\n", ignite_mode);
565 /* UI doesn't support AES key config */
569 for (int p = 0; p < pyros.length; p++) {
570 link.printf("c P %s\n",
571 pyros[p].toString());
574 if (pyro_firing_time >= 0)
575 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
578 if (aprs_interval >= 0)
579 link.printf("c A %d\n", aprs_interval);
583 link.printf("c b %d\n", beep);
586 if (tracker_motion >= 0 && tracker_interval >= 0)
587 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
589 link.printf("c w\n");
593 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
595 link.printf("c s\nf\nv\n");
596 read_link(link, "software-version");
597 switch (log_format) {
598 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
599 case AltosLib.AO_LOG_FORMAT_NONE:
603 read_link(link, "done");