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