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;
72 public int aprs_interval;
77 /* Storage info replies */
78 public int storage_size;
79 public int storage_erase_unit;
81 /* Log listing replies */
82 public int stored_flight;
85 public int[] tracker_distances;
87 public static String get_string(String line, String label) throws ParseException {
88 if (line.startsWith(label)) {
89 String quoted = line.substring(label.length()).trim();
91 if (quoted.startsWith("\""))
92 quoted = quoted.substring(1);
93 if (quoted.endsWith("\""))
94 quoted = quoted.substring(0,quoted.length()-1);
97 throw new ParseException("mismatch", 0);
100 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
101 if (line.startsWith(label)) {
102 String tail = line.substring(label.length()).trim();
103 String[] tokens = tail.split("\\s+");
104 if (tokens.length > 0)
105 return Integer.parseInt(tokens[0]);
107 throw new ParseException("mismatch", 0);
110 public static int[] get_distances(String line, String label) throws NumberFormatException, ParseException {
111 if (line.startsWith(label)) {
112 String tail = line.substring(label.length()).trim();
113 String[] tokens = tail.split("\\s+");
114 if (tokens.length > 1) {
115 int[] distances = new int[2];
116 distances[0] = Integer.parseInt(tokens[0]);
117 distances[1] = Integer.parseInt(tokens[1]);
121 throw new ParseException("mismatch", 0);
124 public Iterator<String> iterator() {
125 return lines.iterator();
128 public int log_space() {
132 if (storage_size > 0) {
133 int space = storage_size;
135 if (storage_erase_unit > 0 && use_flash_for_config())
136 space -= storage_erase_unit;
144 public int log_available() {
145 switch (log_format) {
146 case AltosLib.AO_LOG_FORMAT_TINY:
147 if (stored_flight == 0)
150 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
151 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
154 if (flight_log_max <= 0)
156 int log_max = flight_log_max * 1024;
157 int log_space = log_space();
160 if (stored_flight <= 0)
163 log_used = stored_flight * log_max;
166 if (log_used >= log_space)
169 log_avail = (log_space - log_used) / log_max;
175 public boolean has_monitor_battery() {
176 if (product.startsWith("TeleBT"))
181 int[] parse_version(String v) {
182 String[] parts = v.split("\\.");
183 int r[] = new int[parts.length];
185 for (int i = 0; i < parts.length; i++) {
187 r[i] = AltosLib.fromdec(parts[i]);
188 } catch (NumberFormatException n) {
196 public int compare_version(String other) {
197 int[] me = parse_version(version);
198 int[] them = parse_version(other);
200 int l = Math.min(me.length, them.length);
202 for (int i = 0; i < l; i++) {
203 int d = me[i] - them[i];
214 public void reset() {
215 lines = new LinkedList<String>();
217 manufacturer = "unknown";
221 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
229 radio_frequency = -1;
232 radio_calibration = -1;
237 accel_cal_minus = -1;
238 pad_orientation = -1;
253 tracker_distances = null;
256 storage_erase_unit = -1;
260 public void parse_line(String line) {
262 /* Version replies */
263 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
264 try { product = get_string(line, "product"); } catch (Exception e) {}
265 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
266 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
267 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
268 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
269 try { version = get_string(line, "software-version"); } catch (Exception e) {}
271 /* Version also contains MS5607 info, which we ignore here */
273 /* Config show replies */
276 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
277 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
278 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
282 radio_frequency = get_int(line, "Frequency:");
283 if (radio_frequency < 0)
284 radio_frequency = 434550;
285 } catch (Exception e) {}
286 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
287 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
288 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
290 /* Old HAS_RADIO values */
291 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
292 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
296 if (line.startsWith("Accel cal")) {
297 String[] bits = line.split("\\s+");
298 if (bits.length >= 6) {
299 accel_cal_plus = Integer.parseInt(bits[3]);
300 accel_cal_minus = Integer.parseInt(bits[5]);
303 } catch (Exception e) {}
304 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
307 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
310 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
313 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
317 npyro = get_int(line, "Pyro-count:");
318 pyros = new AltosPyro[npyro];
320 } catch (Exception e) {}
323 AltosPyro p = new AltosPyro(pyro, line);
326 } catch (Exception e) {}
330 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
333 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
336 try { tracker_distances = get_distances(line, "Tracker setting:"); } catch (Exception e) {}
338 /* Storage info replies */
339 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
340 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
342 /* Log listing replies */
343 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
346 public AltosConfigData() {
350 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
352 String line = link.get_reply();
354 throw new TimeoutException();
355 if (line.contains("Syntax error"))
357 this.parse_line(line);
359 /* signals the end of the version info */
360 if (line.startsWith(finished))
365 public boolean has_frequency() {
366 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
369 public void set_frequency(double freq) {
370 int frequency = radio_frequency;
371 int setting = radio_setting;
374 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
376 } else if (setting > 0) {
377 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
381 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
385 public double frequency() {
386 int channel = radio_channel;
387 int setting = radio_setting;
389 if (radio_frequency < 0 && channel < 0 && setting < 0)
397 return AltosConvert.radio_to_frequency(radio_frequency,
403 boolean use_flash_for_config() {
404 if (product.startsWith("TeleMega"))
406 if (product.startsWith("TeleMetrum-v2"))
412 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
415 if (main_deploy >= 0)
416 main_deploy = source.main_deploy();
417 if (apogee_delay >= 0)
418 apogee_delay = source.apogee_delay();
419 if (apogee_lockout >= 0)
420 apogee_lockout = source.apogee_lockout();
424 set_frequency(source.radio_frequency());
425 if (radio_enable >= 0)
426 radio_enable = source.radio_enable();
427 if (callsign != null)
428 callsign = source.callsign();
429 if (radio_calibration >= 0)
430 radio_calibration = source.radio_calibration();
433 if (pad_orientation >= 0)
434 pad_orientation = source.pad_orientation();
437 if (flight_log_max >= 0)
438 flight_log_max = source.flight_log_max();
441 if (ignite_mode >= 0)
442 ignite_mode = source.ignite_mode();
446 pyros = source.pyros();
449 if (aprs_interval >= 0)
450 aprs_interval = source.aprs_interval();
454 beep = source.beep();
456 if (tracker_distances != null)
457 tracker_distances = source.tracker_distances();
460 public void set_values(AltosConfigValues dest) {
461 dest.set_serial(serial);
462 dest.set_product(product);
463 dest.set_version(version);
464 dest.set_main_deploy(main_deploy);
465 dest.set_apogee_delay(apogee_delay);
466 dest.set_apogee_lockout(apogee_lockout);
467 dest.set_radio_calibration(radio_calibration);
468 dest.set_radio_frequency(frequency());
469 boolean max_enabled = true;
471 if (log_space() == 0)
474 switch (log_format) {
475 case AltosLib.AO_LOG_FORMAT_TINY:
479 if (stored_flight > 0)
484 dest.set_flight_log_max_enabled(max_enabled);
485 dest.set_radio_enable(radio_enable);
486 dest.set_flight_log_max_limit(log_space() / 1024);
487 dest.set_flight_log_max(flight_log_max);
488 dest.set_ignite_mode(ignite_mode);
489 dest.set_pad_orientation(pad_orientation);
490 dest.set_callsign(callsign);
492 dest.set_pyros(pyros);
494 dest.set_pyros(null);
495 dest.set_aprs_interval(aprs_interval);
497 dest.set_tracker_distances(tracker_distances);
500 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
503 if (main_deploy >= 0)
504 link.printf("c m %d\n", main_deploy);
505 if (apogee_delay >= 0)
506 link.printf("c d %d\n", apogee_delay);
507 if (apogee_lockout >= 0)
508 link.printf("c L %d\n", apogee_lockout);
510 /* Don't mess with radio calibration when remote */
511 if (radio_calibration > 0 && !remote)
512 link.printf("c f %d\n", radio_calibration);
515 if (has_frequency()) {
516 boolean has_frequency = radio_frequency >= 0;
517 boolean has_setting = radio_setting > 0;
518 double frequency = frequency();
519 link.set_radio_frequency(frequency,
523 /* When remote, reset the dongle frequency at the same time */
526 link.set_radio_frequency(frequency);
531 if (callsign != null)
532 link.printf("c c %s\n", callsign);
533 if (radio_enable >= 0)
534 link.printf("c e %d\n", radio_enable);
537 /* UI doesn't support accel cal */
538 if (pad_orientation >= 0)
539 link.printf("c o %d\n", pad_orientation);
542 if (flight_log_max != 0)
543 link.printf("c l %d\n", flight_log_max);
546 if (ignite_mode >= 0)
547 link.printf("c i %d\n", ignite_mode);
550 /* UI doesn't support AES key config */
554 for (int p = 0; p < pyros.length; p++) {
555 link.printf("c P %s\n",
556 pyros[p].toString());
561 if (aprs_interval >= 0)
562 link.printf("c A %d\n", aprs_interval);
566 link.printf("c b %d\n", beep);
569 if (tracker_distances != null)
570 link.printf("c t %d %d\n", tracker_distances[0], tracker_distances[1]);
572 link.printf("c w\n");
576 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
578 link.printf("c s\nf\nv\n");
579 read_link(link, "software-version");
580 switch (log_format) {
581 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
582 case AltosLib.AO_LOG_FORMAT_NONE:
586 read_link(link, "done");