altosui,telegps: Do not allow radio calibration to be edited
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_9;
19
20 import java.util.*;
21 import java.text.*;
22 import java.util.concurrent.*;
23
24 public class AltosConfigData implements Iterable<String> {
25
26         /* Version information */
27         public String   manufacturer;
28         public String   product;
29         public int      serial;
30         public int      flight;
31         public int      log_format;
32         public int      log_space;
33         public String   version;
34         public int      altitude_32;
35
36         /* Strings returned */
37         public LinkedList<String>       lines;
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         public int      pad_orientation;
58
59         /* HAS_LOG */
60         public int      flight_log_max;
61         public int      log_fixed;
62
63         /* HAS_IGNITE */
64         public int      ignite_mode;
65
66         /* HAS_AES */
67         public String   aes_key;
68
69         /* AO_PYRO_NUM */
70         public AltosPyro[]      pyros;
71         public int              npyro;
72         public int              pyro;
73         public double           pyro_firing_time;
74
75         /* HAS_APRS */
76         public int              aprs_interval;
77         public int              aprs_ssid;
78         public int              aprs_format;
79
80         /* HAS_BEEP */
81         public int              beep;
82
83         /* Storage info replies */
84         public int      storage_size;
85         public int      storage_erase_unit;
86
87         /* Log listing replies */
88         public int      stored_flight;
89
90         /* HAS_TRACKER */
91         public int      tracker_motion;
92         public int      tracker_interval;
93
94         /* HAS_GYRO */
95         public int      accel_zero_along, accel_zero_across, accel_zero_through;
96
97         /* ms5607 data */
98         public int      ms5607_reserved;
99         public int      ms5607_sens;
100         public int      ms5607_off;
101         public int      ms5607_tcs;
102         public int      ms5607_tco;
103         public int      ms5607_tref;
104         public int      ms5607_tempsens;
105         public int      ms5607_crc;
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 Iterator<String> iterator() {
145                 return lines.iterator();
146         }
147
148         public int log_space() {
149                 if (log_space > 0)
150                         return log_space;
151
152                 if (storage_size > 0) {
153                         int     space = storage_size;
154
155                         if (storage_erase_unit > 0 && use_flash_for_config())
156                                 space -= storage_erase_unit;
157
158                         if (space > 0)
159                                 return space;
160                 }
161                 return 0;
162         }
163
164         public int log_available() {
165                 switch (log_format) {
166                 case AltosLib.AO_LOG_FORMAT_TINY:
167                         if (stored_flight == 0)
168                                 return 1;
169                         return 0;
170                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
171                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
172                         return 1;
173                 default:
174                         if (flight_log_max <= 0)
175                                 return 1;
176                         int     log_max = flight_log_max * 1024;
177                         int     log_space = log_space();
178                         int     log_used;
179
180                         if (stored_flight <= 0)
181                                 log_used = 0;
182                         else
183                                 log_used = stored_flight * log_max;
184                         int     log_avail;
185
186                         if (log_used >= log_space)
187                                 log_avail = 0;
188                         else
189                                 log_avail = (log_space - log_used) / log_max;
190
191                         return log_avail;
192                 }
193         }
194
195         public boolean has_monitor_battery() {
196                 if (product.startsWith("TeleBT"))
197                         return true;
198                 return false;
199         }
200
201         int[] parse_version(String v) {
202                 String[] parts = v.split("\\.");
203                 int r[] = new int[parts.length];
204
205                 for (int i = 0; i < parts.length; i++) {
206                         try {
207                                 r[i] = AltosLib.fromdec(parts[i]);
208                         } catch (NumberFormatException n) {
209                                 r[i] = 0;
210                         }
211                 }
212
213                 return r;
214         }
215
216         public int compare_version(String other) {
217                 int[]   me = parse_version(version);
218                 int[]   them = parse_version(other);
219
220                 int     l = Math.min(me.length, them.length);
221
222                 for (int i = 0; i < l; i++) {
223                         int     d = me[i] - them[i];
224                         if (d != 0)
225                                 return d;
226                 }
227                 if (me.length > l)
228                         return 1;
229                 if (them.length > l)
230                         return -1;
231                 return 0;
232         }
233
234         public void reset() {
235                 lines = new LinkedList<String>();
236
237                 manufacturer = "unknown";
238                 product = "unknown";
239                 serial = 0;
240                 flight = 0;
241                 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
242                 log_space = -1;
243                 version = "unknown";
244
245                 main_deploy = -1;
246                 apogee_delay = -1;
247                 apogee_lockout = -1;
248
249                 radio_frequency = -1;
250                 callsign = null;
251                 radio_enable = -1;
252                 radio_calibration = -1;
253                 radio_channel = -1;
254                 radio_setting = -1;
255                 telemetry_rate = -1;
256
257                 accel_cal_plus = -1;
258                 accel_cal_minus = -1;
259                 pad_orientation = -1;
260
261                 flight_log_max = -1;
262                 log_fixed = -1;
263                 ignite_mode = -1;
264
265                 aes_key = "";
266
267                 pyro = 0;
268                 npyro = 0;
269                 pyros = null;
270                 pyro_firing_time = -1;
271
272                 aprs_interval = -1;
273                 aprs_ssid = -1;
274                 aprs_format = -1;
275
276                 beep = -1;
277
278                 tracker_motion = -1;
279                 tracker_interval = -1;
280
281                 storage_size = -1;
282                 storage_erase_unit = -1;
283                 stored_flight = 0;
284
285                 accel_zero_along = -1;
286                 accel_zero_across = -1;
287                 accel_zero_through = -1;
288         }
289
290         public void parse_line(String line) {
291                 lines.add(line);
292                 /* Version replies */
293                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
294                 try { product = get_string(line, "product"); } catch (Exception e) {}
295                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
296                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
297                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
298                 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
299                 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
300                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
301
302                 /* Version also contains MS5607 info, which we ignore here */
303
304                 try { ms5607_reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
305                 try { ms5607_sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
306                 try { ms5607_off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
307                 try { ms5607_tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
308                 try { ms5607_tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
309                 try { ms5607_tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
310                 try { ms5607_tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
311                 try { ms5607_crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
312
313                 /* Config show replies */
314
315                 /* HAS_FLIGHT */
316                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
317                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
318                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
319
320                 /* HAS_RADIO */
321                 try {
322                         radio_frequency = get_int(line, "Frequency:");
323                         if (radio_frequency < 0)
324                                 radio_frequency = 434550;
325                 } catch (Exception e) {}
326                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
327                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
328                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
329                 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
330
331                 /* Old HAS_RADIO values */
332                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
333                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
334
335                 /* HAS_ACCEL */
336                 try {
337                         if (line.startsWith("Accel cal")) {
338                                 String[] bits = line.split("\\s+");
339                                 if (bits.length >= 6) {
340                                         accel_cal_plus = Integer.parseInt(bits[3]);
341                                         accel_cal_minus = Integer.parseInt(bits[5]);
342                                 }
343                         }
344                 } catch (Exception e) {}
345                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
346
347                 /* HAS_LOG */
348                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
349                 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
350
351                 /* HAS_IGNITE */
352                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
353
354                 /* HAS_AES */
355                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
356
357                 /* AO_PYRO_NUM */
358                 try {
359                         npyro = get_int(line, "Pyro-count:");
360                         pyros = new AltosPyro[npyro];
361                         pyro = 0;
362                 } catch (Exception e) {}
363                 if (npyro > 0) {
364                         try {
365                                 AltosPyro p = new AltosPyro(pyro, line);
366                                 if (pyro < npyro)
367                                         pyros[pyro++] = p;
368                         } catch (Exception e) {}
369                 }
370                 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
371
372                 /* HAS_APRS */
373                 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
374                 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
375                 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
376
377                 /* HAS_BEEP */
378                 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
379
380                 /* HAS_TRACKER */
381                 try {
382                         int[] values = get_values(line, "Tracker setting:");
383                         tracker_motion = values[0];
384                         tracker_interval = values[1];
385                 } catch (Exception e) {}
386
387                 /* Storage info replies */
388                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
389                 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
390
391                 /* Log listing replies */
392                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
393
394                 /* HAS_GYRO */
395                 try {
396                         if (line.startsWith("IMU call along")) {
397                                 String[] bits = line.split("\\s+");
398                                 if (bits.length >= 8) {
399                                         accel_zero_along = Integer.parseInt(bits[3]);
400                                         accel_zero_across = Integer.parseInt(bits[5]);
401                                         accel_zero_through = Integer.parseInt(bits[7]);
402                                 }
403                         }
404                 } catch (Exception e) {}
405         }
406
407         public AltosConfigData() {
408                 reset();
409         }
410
411         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
412                 for (;;) {
413                         String line = link.get_reply();
414                         if (line == null)
415                                 throw new TimeoutException();
416                         if (line.contains("Syntax error"))
417                                 continue;
418                         this.parse_line(line);
419
420                         /* signals the end of the version info */
421                         if (line.startsWith(finished))
422                                 break;
423                 }
424         }
425
426         public boolean has_frequency() {
427                 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
428         }
429
430         public boolean has_telemetry_rate() {
431                 return telemetry_rate >= 0;
432         }
433
434         public void set_frequency(double freq) {
435                 int     frequency = radio_frequency;
436                 int     setting = radio_setting;
437
438                 if (frequency > 0) {
439                         radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
440                         radio_channel = -1;
441                 } else if (setting > 0) {
442                         radio_setting =AltosConvert.radio_frequency_to_setting(freq,
443                                                                                     radio_calibration);
444                         radio_channel = -1;
445                 } else {
446                         radio_channel = AltosConvert.radio_frequency_to_channel(freq);
447                 }
448         }
449
450         public double frequency() {
451                 int     channel = radio_channel;
452                 int     setting = radio_setting;
453
454                 if (radio_frequency < 0 && channel < 0 && setting < 0)
455                         return -1;
456
457                 if (channel < 0)
458                         channel = 0;
459                 if (setting < 0)
460                         setting = 0;
461
462                 return AltosConvert.radio_to_frequency(radio_frequency,
463                                                        setting,
464                                                        radio_calibration,
465                                                        channel);
466         }
467
468         boolean use_flash_for_config() {
469                 if (product.startsWith("TeleMega"))
470                         return false;
471                 if (product.startsWith("TeleMetrum-v2"))
472                         return false;
473                 if (product.startsWith("EasyMega"))
474                         return false;
475                 return true;
476         }
477
478
479         public void get_values(AltosConfigValues source) throws AltosConfigDataException {
480
481                 /* HAS_FLIGHT */
482                 if (main_deploy >= 0)
483                         main_deploy = source.main_deploy();
484                 if (apogee_delay >= 0)
485                         apogee_delay = source.apogee_delay();
486                 if (apogee_lockout >= 0)
487                         apogee_lockout = source.apogee_lockout();
488
489                 /* HAS_RADIO */
490                 if (has_frequency())
491                         set_frequency(source.radio_frequency());
492                 if (radio_enable >= 0)
493                         radio_enable = source.radio_enable();
494                 if (callsign != null)
495                         callsign = source.callsign();
496                 if (telemetry_rate >= 0)
497                         telemetry_rate = source.telemetry_rate();
498
499                 /* HAS_ACCEL */
500                 if (pad_orientation >= 0)
501                         pad_orientation = source.pad_orientation();
502
503                 /* HAS_LOG */
504                 if (flight_log_max >= 0)
505                         flight_log_max = source.flight_log_max();
506
507                 /* HAS_IGNITE */
508                 if (ignite_mode >= 0)
509                         ignite_mode = source.ignite_mode();
510
511                 /* AO_PYRO_NUM */
512                 if (npyro > 0)
513                         pyros = source.pyros();
514                 if (pyro_firing_time >= 0)
515                         pyro_firing_time = source.pyro_firing_time();
516
517                 /* HAS_APRS */
518                 if (aprs_interval >= 0)
519                         aprs_interval = source.aprs_interval();
520                 if (aprs_ssid >= 0)
521                         aprs_ssid = source.aprs_ssid();
522                 if (aprs_format >= 0)
523                         aprs_format = source.aprs_format();
524
525                 /* HAS_BEEP */
526                 if (beep >= 0)
527                         beep = source.beep();
528                 /* HAS_TRACKER */
529                 if (tracker_motion >= 0)
530                         tracker_motion = source.tracker_motion();
531                 if (tracker_interval >= 0)
532                         tracker_interval = source.tracker_interval();
533         }
534
535         public void set_values(AltosConfigValues dest) {
536                 dest.set_serial(serial);
537                 dest.set_product(product);
538                 dest.set_version(version);
539                 dest.set_altitude_32(altitude_32);
540                 dest.set_main_deploy(main_deploy);
541                 dest.set_apogee_delay(apogee_delay);
542                 dest.set_apogee_lockout(apogee_lockout);
543                 dest.set_radio_calibration(radio_calibration);
544                 dest.set_radio_frequency(frequency());
545                 dest.set_telemetry_rate(telemetry_rate);
546                 boolean max_enabled = true;
547
548                 if (log_space() == 0)
549                         max_enabled = false;
550
551                 if (log_fixed > 0)
552                         max_enabled = false;
553
554                 switch (log_format) {
555                 case AltosLib.AO_LOG_FORMAT_TINY:
556                         max_enabled = false;
557                         break;
558                 default:
559                         if (stored_flight > 0)
560                                 max_enabled = false;
561                         break;
562                 }
563
564                 dest.set_flight_log_max_enabled(max_enabled);
565                 dest.set_radio_enable(radio_enable);
566                 dest.set_flight_log_max_limit(log_space() / 1024);
567                 dest.set_flight_log_max(flight_log_max);
568                 dest.set_ignite_mode(ignite_mode);
569                 dest.set_pad_orientation(pad_orientation);
570                 dest.set_callsign(callsign);
571                 if (npyro > 0)
572                         dest.set_pyros(pyros);
573                 else
574                         dest.set_pyros(null);
575                 dest.set_pyro_firing_time(pyro_firing_time);
576                 dest.set_aprs_interval(aprs_interval);
577                 dest.set_aprs_ssid(aprs_ssid);
578                 dest.set_aprs_format(aprs_format);
579                 dest.set_beep(beep);
580                 dest.set_tracker_motion(tracker_motion);
581                 dest.set_tracker_interval(tracker_interval);
582         }
583
584         public boolean log_has_state() {
585                 switch (log_format) {
586                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
587                         return false;
588                 }
589                 return true;
590         }
591
592         public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
593
594                 /* HAS_FLIGHT */
595                 if (main_deploy >= 0)
596                         link.printf("c m %d\n", main_deploy);
597                 if (apogee_delay >= 0)
598                         link.printf("c d %d\n", apogee_delay);
599                 if (apogee_lockout >= 0)
600                         link.printf("c L %d\n", apogee_lockout);
601
602                 /* HAS_RADIO */
603                 if (has_frequency()) {
604                         boolean has_frequency = radio_frequency >= 0;
605                         boolean has_setting = radio_setting > 0;
606                         double frequency = frequency();
607                         link.set_radio_frequency(frequency,
608                                                         has_frequency,
609                                                         has_setting,
610                                                         radio_calibration);
611                         /* When remote, reset the dongle frequency at the same time */
612                         if (remote) {
613                                 link.flush_output();
614                                 link.stop_remote();
615                                 link.set_radio_frequency(frequency);
616                                 link.flush_output();
617                                 link.start_remote();
618                         }
619                 }
620
621                 if (telemetry_rate >= 0) {
622                         link.printf("c T %d\n", telemetry_rate);
623                         if (remote) {
624                                 link.flush_output();
625                                 link.stop_remote();
626                                 link.set_telemetry_rate(telemetry_rate);
627                                 link.flush_output();
628                                 link.start_remote();
629                         }
630                 }
631
632                 if (callsign != null) {
633                         link.printf("c c %s\n", callsign);
634                         if (remote) {
635                                 link.flush_output();
636                                 link.stop_remote();
637                                 link.set_callsign(callsign);
638                                 link.flush_output();
639                                 link.start_remote();
640                         }
641                 }
642
643                 if (radio_enable >= 0)
644                         link.printf("c e %d\n", radio_enable);
645
646                 /* HAS_ACCEL */
647                 /* UI doesn't support accel cal */
648                 if (pad_orientation >= 0)
649                         link.printf("c o %d\n", pad_orientation);
650
651                 /* HAS_LOG */
652                 if (flight_log_max != 0)
653                         link.printf("c l %d\n", flight_log_max);
654
655                 /* HAS_IGNITE */
656                 if (ignite_mode >= 0)
657                         link.printf("c i %d\n", ignite_mode);
658
659                 /* HAS_AES */
660                 /* UI doesn't support AES key config */
661
662                 /* AO_PYRO_NUM */
663                 if (npyro > 0) {
664                         for (int p = 0; p < pyros.length; p++) {
665                                 link.printf("c P %s\n",
666                                                    pyros[p].toString());
667                         }
668                 }
669                 if (pyro_firing_time >= 0)
670                         link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
671
672                 /* HAS_APRS */
673                 if (aprs_interval >= 0)
674                         link.printf("c A %d\n", aprs_interval);
675                 if (aprs_ssid >= 0)
676                         link.printf("c S %d\n", aprs_ssid);
677                 if (aprs_format >= 0)
678                         link.printf("c C %d\n", aprs_format);
679
680                 /* HAS_BEEP */
681                 if (beep >= 0)
682                         link.printf("c b %d\n", beep);
683
684                 /* HAS_TRACKER */
685                 if (tracker_motion >= 0 && tracker_interval >= 0)
686                         link.printf("c t %d %d\n", tracker_motion, tracker_interval);
687
688                 /* HAS_GYRO */
689                 /* UI doesn't support accel cal */
690
691                 link.printf("c w\n");
692                 link.flush_output();
693         }
694
695         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
696                 reset();
697                 link.printf("c s\nf\nv\n");
698                 read_link(link, "software-version");
699                 switch (log_format) {
700                 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
701                 case AltosLib.AO_LOG_FORMAT_NONE:
702                         break;
703                 default:
704                         link.printf("l\n");
705                         read_link(link, "done");
706                         break;
707                 }
708         }
709
710 }