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 /* 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;
79 /* Storage info replies */
80 public int storage_size;
81 public int storage_erase_unit;
83 /* Log listing replies */
84 public int stored_flight;
87 public int tracker_motion;
88 public int tracker_interval;
90 public static String get_string(String line, String label) throws ParseException {
91 if (line.startsWith(label)) {
92 String quoted = line.substring(label.length()).trim();
94 if (quoted.startsWith("\""))
95 quoted = quoted.substring(1);
96 if (quoted.endsWith("\""))
97 quoted = quoted.substring(0,quoted.length()-1);
100 throw new ParseException("mismatch", 0);
103 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
104 if (line.startsWith(label)) {
105 String tail = line.substring(label.length()).trim();
106 String[] tokens = tail.split("\\s+");
107 if (tokens.length > 0)
108 return Integer.parseInt(tokens[0]);
110 throw new ParseException("mismatch", 0);
113 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
114 if (line.startsWith(label)) {
115 String tail = line.substring(label.length()).trim();
116 String[] tokens = tail.split("\\s+");
117 if (tokens.length > 1) {
118 int[] values = new int[2];
119 values[0] = Integer.parseInt(tokens[0]);
120 values[1] = Integer.parseInt(tokens[1]);
124 throw new ParseException("mismatch", 0);
127 public Iterator<String> iterator() {
128 return lines.iterator();
131 public int log_space() {
135 if (storage_size > 0) {
136 int space = storage_size;
138 if (storage_erase_unit > 0 && use_flash_for_config())
139 space -= storage_erase_unit;
147 public int log_available() {
148 switch (log_format) {
149 case AltosLib.AO_LOG_FORMAT_TINY:
150 if (stored_flight == 0)
153 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
154 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
157 if (flight_log_max <= 0)
159 int log_max = flight_log_max * 1024;
160 int log_space = log_space();
163 if (stored_flight <= 0)
166 log_used = stored_flight * log_max;
169 if (log_used >= log_space)
172 log_avail = (log_space - log_used) / log_max;
178 public boolean has_monitor_battery() {
179 if (product.startsWith("TeleBT"))
184 int[] parse_version(String v) {
185 String[] parts = v.split("\\.");
186 int r[] = new int[parts.length];
188 for (int i = 0; i < parts.length; i++) {
190 r[i] = AltosLib.fromdec(parts[i]);
191 } catch (NumberFormatException n) {
199 public int compare_version(String other) {
200 int[] me = parse_version(version);
201 int[] them = parse_version(other);
203 int l = Math.min(me.length, them.length);
205 for (int i = 0; i < l; i++) {
206 int d = me[i] - them[i];
217 public void reset() {
218 lines = new LinkedList<String>();
220 manufacturer = "unknown";
224 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
232 radio_frequency = -1;
235 radio_calibration = -1;
240 accel_cal_minus = -1;
241 pad_orientation = -1;
251 pyro_firing_time = -1;
259 tracker_interval = -1;
262 storage_erase_unit = -1;
266 public void parse_line(String line) {
268 /* Version replies */
269 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
270 try { product = get_string(line, "product"); } catch (Exception e) {}
271 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
272 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
273 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
274 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
275 try { version = get_string(line, "software-version"); } catch (Exception e) {}
277 /* Version also contains MS5607 info, which we ignore here */
279 /* Config show replies */
282 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
283 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
284 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
288 radio_frequency = get_int(line, "Frequency:");
289 if (radio_frequency < 0)
290 radio_frequency = 434550;
291 } catch (Exception e) {}
292 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
293 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
294 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
296 /* Old HAS_RADIO values */
297 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
298 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
302 if (line.startsWith("Accel cal")) {
303 String[] bits = line.split("\\s+");
304 if (bits.length >= 6) {
305 accel_cal_plus = Integer.parseInt(bits[3]);
306 accel_cal_minus = Integer.parseInt(bits[5]);
309 } catch (Exception e) {}
310 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
313 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
316 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
319 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
323 npyro = get_int(line, "Pyro-count:");
324 pyros = new AltosPyro[npyro];
326 } catch (Exception e) {}
329 AltosPyro p = new AltosPyro(pyro, line);
332 } catch (Exception e) {}
334 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
337 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
338 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
341 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
345 int[] values = get_values(line, "Tracker setting:");
346 tracker_motion = values[0];
347 tracker_interval = values[1];
348 } catch (Exception e) {}
350 /* Storage info replies */
351 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
352 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
354 /* Log listing replies */
355 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
358 public AltosConfigData() {
362 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
364 String line = link.get_reply();
366 throw new TimeoutException();
367 if (line.contains("Syntax error"))
369 this.parse_line(line);
371 /* signals the end of the version info */
372 if (line.startsWith(finished))
377 public boolean has_frequency() {
378 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
381 public void set_frequency(double freq) {
382 int frequency = radio_frequency;
383 int setting = radio_setting;
386 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
388 } else if (setting > 0) {
389 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
393 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
397 public double frequency() {
398 int channel = radio_channel;
399 int setting = radio_setting;
401 if (radio_frequency < 0 && channel < 0 && setting < 0)
409 return AltosConvert.radio_to_frequency(radio_frequency,
415 boolean use_flash_for_config() {
416 if (product.startsWith("TeleMega"))
418 if (product.startsWith("TeleMetrum-v2"))
424 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
427 if (main_deploy >= 0)
428 main_deploy = source.main_deploy();
429 if (apogee_delay >= 0)
430 apogee_delay = source.apogee_delay();
431 if (apogee_lockout >= 0)
432 apogee_lockout = source.apogee_lockout();
436 set_frequency(source.radio_frequency());
437 if (radio_enable >= 0)
438 radio_enable = source.radio_enable();
439 if (callsign != null)
440 callsign = source.callsign();
441 if (radio_calibration >= 0)
442 radio_calibration = source.radio_calibration();
445 if (pad_orientation >= 0)
446 pad_orientation = source.pad_orientation();
449 if (flight_log_max >= 0)
450 flight_log_max = source.flight_log_max();
453 if (ignite_mode >= 0)
454 ignite_mode = source.ignite_mode();
458 pyros = source.pyros();
459 if (pyro_firing_time >= 0)
460 pyro_firing_time = source.pyro_firing_time();
463 if (aprs_interval >= 0)
464 aprs_interval = source.aprs_interval();
466 aprs_ssid = source.aprs_ssid();
470 beep = source.beep();
472 if (tracker_motion >= 0)
473 tracker_motion = source.tracker_motion();
474 if (tracker_interval >= 0)
475 tracker_interval = source.tracker_interval();
478 public void set_values(AltosConfigValues dest) {
479 dest.set_serial(serial);
480 dest.set_product(product);
481 dest.set_version(version);
482 dest.set_main_deploy(main_deploy);
483 dest.set_apogee_delay(apogee_delay);
484 dest.set_apogee_lockout(apogee_lockout);
485 dest.set_radio_calibration(radio_calibration);
486 dest.set_radio_frequency(frequency());
487 boolean max_enabled = true;
489 if (log_space() == 0)
492 switch (log_format) {
493 case AltosLib.AO_LOG_FORMAT_TINY:
497 if (stored_flight > 0)
502 dest.set_flight_log_max_enabled(max_enabled);
503 dest.set_radio_enable(radio_enable);
504 dest.set_flight_log_max_limit(log_space() / 1024);
505 dest.set_flight_log_max(flight_log_max);
506 dest.set_ignite_mode(ignite_mode);
507 dest.set_pad_orientation(pad_orientation);
508 dest.set_callsign(callsign);
510 dest.set_pyros(pyros);
512 dest.set_pyros(null);
513 dest.set_pyro_firing_time(pyro_firing_time);
514 dest.set_aprs_interval(aprs_interval);
515 dest.set_aprs_ssid(aprs_ssid);
517 dest.set_tracker_motion(tracker_motion);
518 dest.set_tracker_interval(tracker_interval);
521 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
524 if (main_deploy >= 0)
525 link.printf("c m %d\n", main_deploy);
526 if (apogee_delay >= 0)
527 link.printf("c d %d\n", apogee_delay);
528 if (apogee_lockout >= 0)
529 link.printf("c L %d\n", apogee_lockout);
531 /* Don't mess with radio calibration when remote */
532 if (radio_calibration > 0 && !remote)
533 link.printf("c f %d\n", radio_calibration);
536 if (has_frequency()) {
537 boolean has_frequency = radio_frequency >= 0;
538 boolean has_setting = radio_setting > 0;
539 double frequency = frequency();
540 link.set_radio_frequency(frequency,
544 /* When remote, reset the dongle frequency at the same time */
547 link.set_radio_frequency(frequency);
552 if (callsign != null)
553 link.printf("c c %s\n", callsign);
554 if (radio_enable >= 0)
555 link.printf("c e %d\n", radio_enable);
558 /* UI doesn't support accel cal */
559 if (pad_orientation >= 0)
560 link.printf("c o %d\n", pad_orientation);
563 if (flight_log_max != 0)
564 link.printf("c l %d\n", flight_log_max);
567 if (ignite_mode >= 0)
568 link.printf("c i %d\n", ignite_mode);
571 /* UI doesn't support AES key config */
575 for (int p = 0; p < pyros.length; p++) {
576 link.printf("c P %s\n",
577 pyros[p].toString());
580 if (pyro_firing_time >= 0)
581 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
584 if (aprs_interval >= 0)
585 link.printf("c A %d\n", aprs_interval);
587 link.printf("c S %d\n", aprs_ssid);
591 link.printf("c b %d\n", beep);
594 if (tracker_motion >= 0 && tracker_interval >= 0)
595 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
597 link.printf("c w\n");
601 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
603 link.printf("c s\nf\nv\n");
604 read_link(link, "software-version");
605 switch (log_format) {
606 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
607 case AltosLib.AO_LOG_FORMAT_NONE:
611 read_link(link, "done");