altos: Eliminate height requirement for coast detect
[fw/altos] / altoslib / AltosConfigData.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_12;
20
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 public class AltosConfigData {
26
27         /* Version information */
28         public String   manufacturer;
29         public String   product;
30         public int      serial;
31         public int      flight;
32         public int      log_format;
33         public int      log_space;
34         public String   version;
35         public int      altitude_32;
36         public int      config_major, config_minor;
37
38         /* Config information */
39         /* HAS_FLIGHT*/
40         public int      main_deploy;
41         public int      apogee_delay;
42         public int      apogee_lockout;
43
44         /* HAS_RADIO */
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;
53
54         /* HAS_ACCEL */
55         private int     accel_cal_plus_raw, accel_cal_minus_raw;
56         private int     accel_cal_plus_cooked, accel_cal_minus_cooked;
57         private boolean accel_cal_adjusted;
58         public int      pad_orientation;
59
60         /* HAS_LOG */
61         public int      flight_log_max;
62         public int      log_fixed;
63
64         /* HAS_IGNITE */
65         public int      ignite_mode;
66
67         /* HAS_AES */
68         public String   aes_key;
69
70         /* AO_PYRO_NUM */
71         public AltosPyro[]      pyros;
72         public int              npyro;
73         public int              pyro;
74         public double           pyro_firing_time;
75
76         /* HAS_APRS */
77         public int              aprs_interval;
78         public int              aprs_ssid;
79         public int              aprs_format;
80
81         /* HAS_BEEP */
82         public int              beep;
83
84         /* Storage info replies */
85         public int      storage_size;
86         public int      storage_erase_unit;
87
88         /* Log listing replies */
89         public int      stored_flight;
90
91         /* HAS_TRACKER */
92         public int      tracker_motion;
93         public int      tracker_interval;
94
95         /* HAS_GYRO */
96         public int      accel_zero_along, accel_zero_across, accel_zero_through;
97
98         /* ms5607 data */
99         AltosMs5607     ms5607;
100
101         public AltosMs5607 ms5607() {
102                 if (ms5607 == null)
103                         ms5607 = new AltosMs5607();
104                 return ms5607;
105         }
106
107         public static String get_string(String line, String label) throws  ParseException {
108                 if (line.startsWith(label)) {
109                         String  quoted = line.substring(label.length()).trim();
110
111                         if (quoted.startsWith("\""))
112                                 quoted = quoted.substring(1);
113                         if (quoted.endsWith("\""))
114                                 quoted = quoted.substring(0,quoted.length()-1);
115                         return quoted;
116                 }
117                 throw new ParseException("mismatch", 0);
118         }
119
120         public static int get_int(String line, String label) throws NumberFormatException, ParseException {
121                 if (line.startsWith(label)) {
122                         String tail = line.substring(label.length()).trim();
123                         String[] tokens = tail.split("\\s+");
124                         if (tokens.length > 0)
125                                 return  Integer.parseInt(tokens[0]);
126                 }
127                 throw new ParseException("mismatch", 0);
128         }
129
130         public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
131                 if (line.startsWith(label)) {
132                         String tail = line.substring(label.length()).trim();
133                         String[] tokens = tail.split("\\s+");
134                         if (tokens.length > 1) {
135                                 int[]   values = new int[2];
136                                 values[0] = Integer.parseInt(tokens[0]);
137                                 values[1] = Integer.parseInt(tokens[1]);
138                                 return values;
139                         }
140                 }
141                 throw new ParseException("mismatch", 0);
142         }
143
144         public int log_space() {
145                 if (log_space != AltosLib.MISSING)
146                         return log_space;
147
148                 if (storage_size != AltosLib.MISSING) {
149                         int     space = storage_size;
150
151                         if (storage_erase_unit != AltosLib.MISSING && use_flash_for_config())
152                                 space -= storage_erase_unit;
153
154                         if (space != AltosLib.MISSING)
155                                 return space;
156                 }
157                 return 0;
158         }
159
160         public int log_available() {
161                 switch (log_format) {
162                 case AltosLib.AO_LOG_FORMAT_TINY:
163                         if (stored_flight == 0)
164                                 return 1;
165                         return 0;
166                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
167                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
168                         return 1;
169                 default:
170                         if (flight_log_max <= 0)
171                                 return 1;
172                         int     log_max = flight_log_max * 1024;
173                         int     log_space = log_space();
174                         int     log_used;
175
176                         if (stored_flight <= 0)
177                                 log_used = 0;
178                         else
179                                 log_used = stored_flight * log_max;
180                         int     log_avail;
181
182                         if (log_used >= log_space)
183                                 log_avail = 0;
184                         else
185                                 log_avail = (log_space - log_used) / log_max;
186
187                         return log_avail;
188                 }
189         }
190
191         public int invert_accel_value(int value) {
192                 if (value == AltosLib.MISSING)
193                         return AltosLib.MISSING;
194
195                 switch (log_format) {
196                 case AltosLib.AO_LOG_FORMAT_FULL:
197                         return 0x7fff - value;
198                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
199                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
200                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
201                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_3:
202                         return 4095 - value;
203                 default:
204                         return AltosLib.MISSING;
205                 }
206         }
207
208         public boolean has_monitor_battery() {
209                 if (product.startsWith("TeleBT"))
210                         return true;
211                 return false;
212         }
213
214         int[] parse_version(String v) {
215                 String[] parts = v.split("\\.");
216                 int r[] = new int[parts.length];
217
218                 for (int i = 0; i < parts.length; i++) {
219                         try {
220                                 r[i] = (int) AltosLib.fromdec(parts[i]);
221                         } catch (NumberFormatException n) {
222                                 r[i] = 0;
223                         }
224                 }
225
226                 return r;
227         }
228
229         public boolean altitude_32() {
230                 return altitude_32 == 1;
231         }
232
233         public int compare_version(String other) {
234                 int[]   me = parse_version(version);
235                 int[]   them = parse_version(other);
236
237                 int     l = Math.min(me.length, them.length);
238
239                 for (int i = 0; i < l; i++) {
240                         int     d = me[i] - them[i];
241                         if (d != 0)
242                                 return d;
243                 }
244                 if (me.length > l)
245                         return 1;
246                 if (them.length > l)
247                         return -1;
248                 return 0;
249         }
250
251         public void reset() {
252                 manufacturer = null;
253                 product = null;
254                 serial = AltosLib.MISSING;
255                 flight = AltosLib.MISSING;
256                 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
257                 log_space = AltosLib.MISSING;
258                 version = "unknown";
259                 config_major = AltosLib.MISSING;
260                 config_minor = AltosLib.MISSING;
261
262                 main_deploy = AltosLib.MISSING;
263                 apogee_delay = AltosLib.MISSING;
264                 apogee_lockout = AltosLib.MISSING;
265
266                 radio_frequency = AltosLib.MISSING;
267                 callsign = null;
268                 radio_enable = AltosLib.MISSING;
269                 radio_calibration = AltosLib.MISSING;
270                 radio_channel = AltosLib.MISSING;
271                 radio_setting = AltosLib.MISSING;
272                 telemetry_rate = AltosLib.MISSING;
273
274                 accel_cal_plus_cooked = AltosLib.MISSING;
275                 accel_cal_minus_cooked = AltosLib.MISSING;
276                 accel_cal_plus_raw = AltosLib.MISSING;
277                 accel_cal_minus_raw = AltosLib.MISSING;
278                 pad_orientation = AltosLib.MISSING;
279                 accel_cal_adjusted = false;
280
281                 flight_log_max = AltosLib.MISSING;
282                 log_fixed = AltosLib.MISSING;
283                 ignite_mode = AltosLib.MISSING;
284
285                 aes_key = null;
286
287                 pyro = AltosLib.MISSING;
288                 npyro = AltosLib.MISSING;
289                 pyros = null;
290                 pyro_firing_time = AltosLib.MISSING;
291
292                 aprs_interval = AltosLib.MISSING;
293                 aprs_ssid = AltosLib.MISSING;
294                 aprs_format = AltosLib.MISSING;
295
296                 beep = AltosLib.MISSING;
297
298                 tracker_motion = AltosLib.MISSING;
299                 tracker_interval = AltosLib.MISSING;
300
301                 storage_size = AltosLib.MISSING;
302                 storage_erase_unit = AltosLib.MISSING;
303                 stored_flight = AltosLib.MISSING;
304
305                 accel_zero_along = AltosLib.MISSING;
306                 accel_zero_across = AltosLib.MISSING;
307                 accel_zero_through = AltosLib.MISSING;
308         }
309
310         /* Return + accel calibration relative to a specific pad orientation */
311         public int accel_cal_plus(int pad_orientation) {
312                 switch (pad_orientation) {
313                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
314                         return accel_cal_plus_cooked;
315                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
316                         return invert_accel_value(accel_cal_minus_cooked);
317                 default:
318                         return AltosLib.MISSING;
319                 }
320         }
321
322         /* Return - accel calibration relative to a specific pad orientation */
323         public int accel_cal_minus(int pad_orientation) {
324                 switch (pad_orientation) {
325                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
326                         return accel_cal_minus_cooked;
327                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
328                         return invert_accel_value(accel_cal_plus_cooked);
329                 default:
330                         return AltosLib.MISSING;
331                 }
332         }
333
334         /* Once we have all of the values from the config data, compute the
335          * accel cal values relative to Antenna Up orientation.
336          */
337         private void adjust_accel_cal() {
338                 if (!accel_cal_adjusted &&
339                     pad_orientation != AltosLib.MISSING &&
340                     accel_cal_plus_raw != AltosLib.MISSING &&
341                     accel_cal_minus_raw != AltosLib.MISSING &&
342                     log_format != AltosLib.AO_LOG_FORMAT_UNKNOWN)
343                 {
344                         switch (pad_orientation) {
345                         case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
346                                 accel_cal_plus_cooked = accel_cal_plus_raw;
347                                 accel_cal_minus_cooked = accel_cal_minus_raw;
348                                 accel_cal_adjusted = true;
349                                 break;
350                         case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
351                                 accel_cal_plus_cooked = invert_accel_value(accel_cal_minus_raw);
352                                 accel_cal_minus_cooked = invert_accel_value(accel_cal_plus_raw);
353                                 accel_cal_adjusted = true;
354                                 break;
355                         default:
356                                 break;
357                         }
358                 }
359         }
360
361         public void parse_line(String line) {
362
363                 /* Version replies */
364                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
365                 try { product = get_string(line, "product"); } catch (Exception e) {}
366                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
367                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
368                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
369                 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
370                 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
371                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
372
373                 /* Version also contains MS5607 info, which we ignore here */
374
375                 try { ms5607().reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
376                 try { ms5607().sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
377                 try { ms5607().off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
378                 try { ms5607().tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
379                 try { ms5607().tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
380                 try { ms5607().tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
381                 try { ms5607().tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
382                 try { ms5607().crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
383
384                 /* Config show replies */
385
386                 try {
387                         if (line.startsWith("Config version")) {
388                                 String[] bits = line.split("\\s+");
389                                 if (bits.length >= 3) {
390                                         String[] cfg = bits[2].split("\\.");
391
392                                         if (cfg.length >= 2) {
393                                                 config_major = Integer.parseInt(cfg[0]);
394                                                 config_minor = Integer.parseInt(cfg[1]);
395                                         }
396                                 }
397                         }
398                 } catch (Exception e) {}
399
400                 /* HAS_FLIGHT */
401                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
402                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
403                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
404
405                 /* HAS_RADIO */
406                 try {
407                         radio_frequency = get_int(line, "Frequency:");
408                         if (radio_frequency < 0)
409                                 radio_frequency = 434550;
410                 } catch (Exception e) {}
411                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
412                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
413                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
414                 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
415
416                 /* Old HAS_RADIO values */
417                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
418                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
419
420                 /* HAS_ACCEL */
421                 try {
422                         if (line.startsWith("Accel cal")) {
423                                 String[] bits = line.split("\\s+");
424                                 if (bits.length >= 6) {
425                                         accel_cal_plus_raw = Integer.parseInt(bits[3]);
426                                         accel_cal_minus_raw = Integer.parseInt(bits[5]);
427                                         accel_cal_adjusted = false;
428                                 }
429                         }
430                 } catch (Exception e) {}
431                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
432
433                 /* HAS_LOG */
434                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
435                 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
436
437                 /* HAS_IGNITE */
438                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
439
440                 /* HAS_AES */
441                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
442
443                 /* AO_PYRO_NUM */
444                 try {
445                         npyro = get_int(line, "Pyro-count:");
446                         pyros = new AltosPyro[npyro];
447                         pyro = 0;
448                 } catch (Exception e) {}
449                 if (npyro != AltosLib.MISSING) {
450                         try {
451                                 AltosPyro p = new AltosPyro(pyro, line);
452                                 if (pyro < npyro)
453                                         pyros[pyro++] = p;
454                         } catch (Exception e) {}
455                 }
456                 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
457
458                 /* HAS_APRS */
459                 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
460                 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
461                 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
462
463                 /* HAS_BEEP */
464                 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
465
466                 /* HAS_TRACKER */
467                 try {
468                         int[] values = get_values(line, "Tracker setting:");
469                         tracker_motion = values[0];
470                         tracker_interval = values[1];
471                 } catch (Exception e) {}
472
473                 /* Storage info replies */
474                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
475                 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
476
477                 /* Log listing replies */
478                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
479
480                 /* HAS_GYRO */
481                 try {
482                         if (line.startsWith("IMU cal along")) {
483                                 String[] bits = line.split("\\s+");
484                                 if (bits.length >= 8) {
485                                         accel_zero_along = Integer.parseInt(bits[3]);
486                                         accel_zero_across = Integer.parseInt(bits[5]);
487                                         accel_zero_through = Integer.parseInt(bits[7]);
488                                 }
489                         }
490                 } catch (Exception e) {}
491
492                 /* Fix accel cal as soon as all of the necessary values appear */
493                 adjust_accel_cal();
494         }
495
496         public AltosConfigData() {
497                 reset();
498         }
499
500         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
501                 for (;;) {
502                         String line = link.get_reply();
503                         if (line == null)
504                                 throw new TimeoutException();
505                         if (line.contains("Syntax error"))
506                                 continue;
507                         this.parse_line(line);
508
509                         /* signals the end of the version info */
510                         if (line.startsWith(finished))
511                                 break;
512                 }
513         }
514
515         public boolean has_frequency() {
516                 return radio_frequency != AltosLib.MISSING || radio_setting != AltosLib.MISSING || radio_channel != AltosLib.MISSING;
517         }
518
519         public boolean has_telemetry_rate() {
520                 return telemetry_rate != AltosLib.MISSING;
521         }
522
523         public void set_frequency(double freq) {
524                 int     frequency = radio_frequency;
525                 int     setting = radio_setting;
526
527                 if (frequency != AltosLib.MISSING) {
528                         radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
529                         radio_channel = AltosLib.MISSING;
530                 } else if (setting != AltosLib.MISSING) {
531                         radio_setting =AltosConvert.radio_frequency_to_setting(freq, radio_calibration);
532                         radio_channel = AltosLib.MISSING;
533                 } else {
534                         radio_channel = AltosConvert.radio_frequency_to_channel(freq);
535                 }
536         }
537
538         public double frequency() {
539                 int     channel = radio_channel;
540                 int     setting = radio_setting;
541
542                 if (radio_frequency == AltosLib.MISSING && channel == AltosLib.MISSING && setting == AltosLib.MISSING)
543                         return AltosLib.MISSING;
544
545                 if (channel == AltosLib.MISSING)
546                         channel = 0;
547                 if (setting == AltosLib.MISSING)
548                         setting = 0;
549
550                 return AltosConvert.radio_to_frequency(radio_frequency,
551                                                        setting,
552                                                        radio_calibration,
553                                                        channel);
554         }
555
556         boolean use_flash_for_config() {
557                 if (product.startsWith("TeleMega"))
558                         return false;
559                 if (product.startsWith("TeleMetrum-v2"))
560                         return false;
561                 if (product.startsWith("EasyMega"))
562                         return false;
563                 return true;
564         }
565
566
567         public boolean mma655x_inverted() throws AltosUnknownProduct {
568                 if (product != null) {
569                         if (product.startsWith("EasyMega-v1"))
570                                 return false;
571                         if (product.startsWith("TeleMetrum-v2"))
572                                 return true;
573                         if (product.startsWith("TeleMega-v2"))
574                                 return false;
575                         if (product.startsWith("TeleMega-v1"))
576                                 return false;
577                 }
578                 throw new AltosUnknownProduct(product);
579         }
580
581         public void get_values(AltosConfigValues source) throws AltosConfigDataException {
582
583                 /* HAS_FLIGHT */
584                 if (main_deploy != AltosLib.MISSING)
585                         main_deploy = source.main_deploy();
586                 if (apogee_delay != AltosLib.MISSING)
587                         apogee_delay = source.apogee_delay();
588                 if (apogee_lockout != AltosLib.MISSING)
589                         apogee_lockout = source.apogee_lockout();
590
591                 /* HAS_RADIO */
592                 if (has_frequency())
593                         set_frequency(source.radio_frequency());
594                 if (radio_enable != AltosLib.MISSING)
595                         radio_enable = source.radio_enable();
596                 if (callsign != null)
597                         callsign = source.callsign();
598                 if (telemetry_rate != AltosLib.MISSING)
599                         telemetry_rate = source.telemetry_rate();
600
601                 /* HAS_ACCEL */
602                 if (pad_orientation != AltosLib.MISSING)
603                         pad_orientation = source.pad_orientation();
604
605                 if (accel_cal_plus_cooked != AltosLib.MISSING)
606                         accel_cal_plus_cooked = source.accel_cal_plus();
607
608                 if (accel_cal_minus_cooked != AltosLib.MISSING)
609                         accel_cal_minus_cooked = source.accel_cal_minus();
610
611                 /* HAS_LOG */
612                 if (flight_log_max != AltosLib.MISSING)
613                         flight_log_max = source.flight_log_max();
614
615                 /* HAS_IGNITE */
616                 if (ignite_mode != AltosLib.MISSING)
617                         ignite_mode = source.ignite_mode();
618
619                 /* AO_PYRO_NUM */
620                 if (npyro != AltosLib.MISSING)
621                         pyros = source.pyros();
622                 if (pyro_firing_time != AltosLib.MISSING)
623                         pyro_firing_time = source.pyro_firing_time();
624
625                 /* HAS_APRS */
626                 if (aprs_interval != AltosLib.MISSING)
627                         aprs_interval = source.aprs_interval();
628                 if (aprs_ssid != AltosLib.MISSING)
629                         aprs_ssid = source.aprs_ssid();
630                 if (aprs_format != AltosLib.MISSING)
631                         aprs_format = source.aprs_format();
632
633                 /* HAS_BEEP */
634                 if (beep != AltosLib.MISSING)
635                         beep = source.beep();
636                 /* HAS_TRACKER */
637                 if (tracker_motion != AltosLib.MISSING)
638                         tracker_motion = source.tracker_motion();
639                 if (tracker_interval != AltosLib.MISSING)
640                         tracker_interval = source.tracker_interval();
641         }
642
643         public void set_values(AltosConfigValues dest) {
644                 dest.set_serial(serial);
645                 dest.set_product(product);
646                 dest.set_version(version);
647                 dest.set_altitude_32(altitude_32);
648                 dest.set_main_deploy(main_deploy);
649                 dest.set_apogee_delay(apogee_delay);
650                 dest.set_apogee_lockout(apogee_lockout);
651                 dest.set_radio_calibration(radio_calibration);
652                 dest.set_radio_frequency(frequency());
653                 dest.set_telemetry_rate(telemetry_rate);
654                 boolean max_enabled = true;
655
656                 if (log_space() == 0)
657                         max_enabled = false;
658
659                 if (log_fixed != AltosLib.MISSING)
660                         max_enabled = false;
661
662                 switch (log_format) {
663                 case AltosLib.AO_LOG_FORMAT_TINY:
664                         max_enabled = false;
665                         break;
666                 default:
667                         if (stored_flight != AltosLib.MISSING)
668                                 max_enabled = false;
669                         break;
670                 }
671
672                 dest.set_flight_log_max_enabled(max_enabled);
673                 dest.set_radio_enable(radio_enable);
674                 dest.set_flight_log_max_limit(log_space() / 1024);
675                 dest.set_flight_log_max(flight_log_max);
676                 dest.set_ignite_mode(ignite_mode);
677                 dest.set_pad_orientation(pad_orientation);
678                 dest.set_accel_cal(accel_cal_plus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP),
679                                    accel_cal_minus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP));
680                 dest.set_callsign(callsign);
681                 if (npyro != AltosLib.MISSING)
682                         dest.set_pyros(pyros);
683                 else
684                         dest.set_pyros(null);
685                 dest.set_pyro_firing_time(pyro_firing_time);
686                 dest.set_aprs_interval(aprs_interval);
687                 dest.set_aprs_ssid(aprs_ssid);
688                 dest.set_aprs_format(aprs_format);
689                 dest.set_beep(beep);
690                 dest.set_tracker_motion(tracker_motion);
691                 dest.set_tracker_interval(tracker_interval);
692         }
693
694         public boolean log_has_state() {
695                 switch (log_format) {
696                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
697                         return false;
698                 }
699                 return true;
700         }
701
702         public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
703
704                 /* HAS_FLIGHT */
705                 if (main_deploy != AltosLib.MISSING)
706                         link.printf("c m %d\n", main_deploy);
707                 if (apogee_delay != AltosLib.MISSING)
708                         link.printf("c d %d\n", apogee_delay);
709                 if (apogee_lockout != AltosLib.MISSING)
710                         link.printf("c L %d\n", apogee_lockout);
711
712                 /* HAS_RADIO */
713                 if (has_frequency()) {
714                         boolean has_frequency = radio_frequency != AltosLib.MISSING;
715                         boolean has_setting = radio_setting != AltosLib.MISSING;
716                         double frequency = frequency();
717                         link.set_radio_frequency(frequency,
718                                                         has_frequency,
719                                                         has_setting,
720                                                         radio_calibration);
721                         /* When remote, reset the dongle frequency at the same time */
722                         if (remote) {
723                                 link.flush_output();
724                                 link.stop_remote();
725                                 link.set_radio_frequency(frequency);
726                                 link.flush_output();
727                                 link.start_remote();
728                         }
729                 }
730
731                 if (telemetry_rate != AltosLib.MISSING) {
732                         link.printf("c T %d\n", telemetry_rate);
733                         if (remote) {
734                                 link.flush_output();
735                                 link.stop_remote();
736                                 link.set_telemetry_rate(telemetry_rate);
737                                 link.flush_output();
738                                 link.start_remote();
739                         }
740                 }
741
742                 if (callsign != null) {
743                         link.printf("c c %s\n", callsign);
744                         if (remote) {
745                                 link.flush_output();
746                                 link.stop_remote();
747                                 link.set_callsign(callsign);
748                                 link.flush_output();
749                                 link.start_remote();
750                         }
751                 }
752
753                 if (radio_enable != AltosLib.MISSING)
754                         link.printf("c e %d\n", radio_enable);
755
756                 /* HAS_ACCEL */
757                 /* set orientation first so that we know how to set the accel cal */
758                 if (pad_orientation != AltosLib.MISSING)
759                         link.printf("c o %d\n", pad_orientation);
760                 int plus = accel_cal_plus(pad_orientation);
761                 int minus = accel_cal_minus(pad_orientation);
762                 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING)
763                         link.printf("c a %d %d\n", plus, minus);
764
765                 /* HAS_LOG */
766                 if (flight_log_max != 0)
767                         link.printf("c l %d\n", flight_log_max);
768
769                 /* HAS_IGNITE */
770                 if (ignite_mode != AltosLib.MISSING)
771                         link.printf("c i %d\n", ignite_mode);
772
773                 /* HAS_AES */
774                 /* UI doesn't support AES key config */
775
776                 /* AO_PYRO_NUM */
777                 if (npyro != AltosLib.MISSING) {
778                         for (int p = 0; p < pyros.length; p++) {
779                                 link.printf("c P %s\n",
780                                                    pyros[p].toString());
781                         }
782                 }
783                 if (pyro_firing_time != AltosLib.MISSING)
784                         link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
785
786                 /* HAS_APRS */
787                 if (aprs_interval != AltosLib.MISSING)
788                         link.printf("c A %d\n", aprs_interval);
789                 if (aprs_ssid != AltosLib.MISSING)
790                         link.printf("c S %d\n", aprs_ssid);
791                 if (aprs_format != AltosLib.MISSING)
792                         link.printf("c C %d\n", aprs_format);
793
794                 /* HAS_BEEP */
795                 if (beep != AltosLib.MISSING)
796                         link.printf("c b %d\n", beep);
797
798                 /* HAS_TRACKER */
799                 if (tracker_motion != AltosLib.MISSING && tracker_interval != AltosLib.MISSING)
800                         link.printf("c t %d %d\n", tracker_motion, tracker_interval);
801
802                 /* HAS_GYRO */
803                 /* UI doesn't support accel cal */
804
805                 link.printf("c w\n");
806                 link.flush_output();
807         }
808
809         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
810                 reset();
811                 link.printf("c s\nf\nv\n");
812                 read_link(link, "software-version");
813                 switch (log_format) {
814                 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
815                 case AltosLib.AO_LOG_FORMAT_NONE:
816                         break;
817                 default:
818                         link.printf("l\n");
819                         read_link(link, "done");
820                         break;
821                 }
822         }
823 }