d8f4d945e466bf5d9d8e61f35b721bd414e6ae31
[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_6;
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
79         /* HAS_BEEP */
80         public int              beep;
81
82         /* Storage info replies */
83         public int      storage_size;
84         public int      storage_erase_unit;
85
86         /* Log listing replies */
87         public int      stored_flight;
88
89         /* HAS_TRACKER */
90         public int      tracker_motion;
91         public int      tracker_interval;
92
93         /* HAS_GYRO */
94         public int      accel_zero_along, accel_zero_across, accel_zero_through;
95
96         /* ms5607 data */
97         public int      ms5607_reserved;
98         public int      ms5607_sens;
99         public int      ms5607_off;
100         public int      ms5607_tcs;
101         public int      ms5607_tco;
102         public int      ms5607_tref;
103         public int      ms5607_tempsens;
104         public int      ms5607_crc;
105
106         public static String get_string(String line, String label) throws  ParseException {
107                 if (line.startsWith(label)) {
108                         String  quoted = line.substring(label.length()).trim();
109
110                         if (quoted.startsWith("\""))
111                                 quoted = quoted.substring(1);
112                         if (quoted.endsWith("\""))
113                                 quoted = quoted.substring(0,quoted.length()-1);
114                         return quoted;
115                 }
116                 throw new ParseException("mismatch", 0);
117         }
118
119         public static int get_int(String line, String label) throws NumberFormatException, ParseException {
120                 if (line.startsWith(label)) {
121                         String tail = line.substring(label.length()).trim();
122                         String[] tokens = tail.split("\\s+");
123                         if (tokens.length > 0)
124                                 return  Integer.parseInt(tokens[0]);
125                 }
126                 throw new ParseException("mismatch", 0);
127         }
128
129         public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
130                 if (line.startsWith(label)) {
131                         String tail = line.substring(label.length()).trim();
132                         String[] tokens = tail.split("\\s+");
133                         if (tokens.length > 1) {
134                                 int[]   values = new int[2];
135                                 values[0] = Integer.parseInt(tokens[0]);
136                                 values[1] = Integer.parseInt(tokens[1]);
137                                 return values;
138                         }
139                 }
140                 throw new ParseException("mismatch", 0);
141         }
142
143         public Iterator<String> iterator() {
144                 return lines.iterator();
145         }
146
147         public int log_space() {
148                 if (log_space > 0)
149                         return log_space;
150
151                 if (storage_size > 0) {
152                         int     space = storage_size;
153
154                         if (storage_erase_unit > 0 && use_flash_for_config())
155                                 space -= storage_erase_unit;
156
157                         if (space > 0)
158                                 return space;
159                 }
160                 return 0;
161         }
162
163         public int log_available() {
164                 switch (log_format) {
165                 case AltosLib.AO_LOG_FORMAT_TINY:
166                         if (stored_flight == 0)
167                                 return 1;
168                         return 0;
169                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
170                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
171                         return 1;
172                 default:
173                         if (flight_log_max <= 0)
174                                 return 1;
175                         int     log_max = flight_log_max * 1024;
176                         int     log_space = log_space();
177                         int     log_used;
178
179                         if (stored_flight <= 0)
180                                 log_used = 0;
181                         else
182                                 log_used = stored_flight * log_max;
183                         int     log_avail;
184
185                         if (log_used >= log_space)
186                                 log_avail = 0;
187                         else
188                                 log_avail = (log_space - log_used) / log_max;
189
190                         return log_avail;
191                 }
192         }
193
194         public boolean has_monitor_battery() {
195                 if (product.startsWith("TeleBT"))
196                         return true;
197                 return false;
198         }
199
200         int[] parse_version(String v) {
201                 String[] parts = v.split("\\.");
202                 int r[] = new int[parts.length];
203
204                 for (int i = 0; i < parts.length; i++) {
205                         try {
206                                 r[i] = AltosLib.fromdec(parts[i]);
207                         } catch (NumberFormatException n) {
208                                 r[i] = 0;
209                         }
210                 }
211
212                 return r;
213         }
214
215         public int compare_version(String other) {
216                 int[]   me = parse_version(version);
217                 int[]   them = parse_version(other);
218
219                 int     l = Math.min(me.length, them.length);
220
221                 for (int i = 0; i < l; i++) {
222                         int     d = me[i] - them[i];
223                         if (d != 0)
224                                 return d;
225                 }
226                 if (me.length > l)
227                         return 1;
228                 if (them.length > l)
229                         return -1;
230                 return 0;
231         }
232
233         public void reset() {
234                 lines = new LinkedList<String>();
235
236                 manufacturer = "unknown";
237                 product = "unknown";
238                 serial = 0;
239                 flight = 0;
240                 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
241                 log_space = -1;
242                 version = "unknown";
243
244                 main_deploy = -1;
245                 apogee_delay = -1;
246                 apogee_lockout = -1;
247
248                 radio_frequency = -1;
249                 callsign = null;
250                 radio_enable = -1;
251                 radio_calibration = -1;
252                 radio_channel = -1;
253                 radio_setting = -1;
254                 telemetry_rate = -1;
255
256                 accel_cal_plus = -1;
257                 accel_cal_minus = -1;
258                 pad_orientation = -1;
259
260                 flight_log_max = -1;
261                 log_fixed = -1;
262                 ignite_mode = -1;
263
264                 aes_key = "";
265
266                 pyro = 0;
267                 npyro = 0;
268                 pyros = null;
269                 pyro_firing_time = -1;
270
271                 aprs_interval = -1;
272                 aprs_ssid = -1;
273
274                 beep = -1;
275
276                 tracker_motion = -1;
277                 tracker_interval = -1;
278
279                 storage_size = -1;
280                 storage_erase_unit = -1;
281                 stored_flight = 0;
282
283                 accel_zero_along = -1;
284                 accel_zero_across = -1;
285                 accel_zero_through = -1;
286         }
287
288         public void parse_line(String line) {
289                 lines.add(line);
290                 /* Version replies */
291                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
292                 try { product = get_string(line, "product"); } catch (Exception e) {}
293                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
294                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
295                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
296                 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
297                 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
298                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
299
300                 /* Version also contains MS5607 info, which we ignore here */
301
302                 try { ms5607_reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
303                 try { ms5607_sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
304                 try { ms5607_off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
305                 try { ms5607_tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
306                 try { ms5607_tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
307                 try { ms5607_tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
308                 try { ms5607_tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
309                 try { ms5607_crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
310
311                 /* Config show replies */
312
313                 /* HAS_FLIGHT */
314                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
315                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
316                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
317
318                 /* HAS_RADIO */
319                 try {
320                         radio_frequency = get_int(line, "Frequency:");
321                         if (radio_frequency < 0)
322                                 radio_frequency = 434550;
323                 } catch (Exception e) {}
324                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
325                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
326                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
327                 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
328
329                 /* Old HAS_RADIO values */
330                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
331                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
332
333                 /* HAS_ACCEL */
334                 try {
335                         if (line.startsWith("Accel cal")) {
336                                 String[] bits = line.split("\\s+");
337                                 if (bits.length >= 6) {
338                                         accel_cal_plus = Integer.parseInt(bits[3]);
339                                         accel_cal_minus = Integer.parseInt(bits[5]);
340                                 }
341                         }
342                 } catch (Exception e) {}
343                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
344
345                 /* HAS_LOG */
346                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
347                 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
348
349                 /* HAS_IGNITE */
350                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
351
352                 /* HAS_AES */
353                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
354
355                 /* AO_PYRO_NUM */
356                 try {
357                         npyro = get_int(line, "Pyro-count:");
358                         pyros = new AltosPyro[npyro];
359                         pyro = 0;
360                 } catch (Exception e) {}
361                 if (npyro > 0) {
362                         try {
363                                 AltosPyro p = new AltosPyro(pyro, line);
364                                 if (pyro < npyro)
365                                         pyros[pyro++] = p;
366                         } catch (Exception e) {}
367                 }
368                 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
369
370                 /* HAS_APRS */
371                 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
372                 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
373
374                 /* HAS_BEEP */
375                 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
376
377                 /* HAS_TRACKER */
378                 try {
379                         int[] values = get_values(line, "Tracker setting:");
380                         tracker_motion = values[0];
381                         tracker_interval = values[1];
382                 } catch (Exception e) {}
383
384                 /* Storage info replies */
385                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
386                 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
387
388                 /* Log listing replies */
389                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
390
391                 /* HAS_GYRO */
392                 try {
393                         if (line.startsWith("IMU call along")) {
394                                 String[] bits = line.split("\\s+");
395                                 if (bits.length >= 8) {
396                                         accel_zero_along = Integer.parseInt(bits[3]);
397                                         accel_zero_across = Integer.parseInt(bits[5]);
398                                         accel_zero_through = Integer.parseInt(bits[7]);
399                                 }
400                         }
401                 } catch (Exception e) {}
402         }
403
404         public AltosConfigData() {
405                 reset();
406         }
407
408         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
409                 for (;;) {
410                         String line = link.get_reply();
411                         if (line == null)
412                                 throw new TimeoutException();
413                         if (line.contains("Syntax error"))
414                                 continue;
415                         this.parse_line(line);
416
417                         /* signals the end of the version info */
418                         if (line.startsWith(finished))
419                                 break;
420                 }
421         }
422
423         public boolean has_frequency() {
424                 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
425         }
426
427         public boolean has_telemetry_rate() {
428                 return telemetry_rate >= 0;
429         }
430
431         public void set_frequency(double freq) {
432                 int     frequency = radio_frequency;
433                 int     setting = radio_setting;
434
435                 if (frequency > 0) {
436                         radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
437                         radio_channel = -1;
438                 } else if (setting > 0) {
439                         radio_setting =AltosConvert.radio_frequency_to_setting(freq,
440                                                                                     radio_calibration);
441                         radio_channel = -1;
442                 } else {
443                         radio_channel = AltosConvert.radio_frequency_to_channel(freq);
444                 }
445         }
446
447         public double frequency() {
448                 int     channel = radio_channel;
449                 int     setting = radio_setting;
450
451                 if (radio_frequency < 0 && channel < 0 && setting < 0)
452                         return -1;
453
454                 if (channel < 0)
455                         channel = 0;
456                 if (setting < 0)
457                         setting = 0;
458
459                 return AltosConvert.radio_to_frequency(radio_frequency,
460                                                        setting,
461                                                        radio_calibration,
462                                                        channel);
463         }
464
465         boolean use_flash_for_config() {
466                 if (product.startsWith("TeleMega"))
467                         return false;
468                 if (product.startsWith("TeleMetrum-v2"))
469                         return false;
470                 if (product.startsWith("EasyMega"))
471                         return false;
472                 return true;
473         }
474
475
476         public void get_values(AltosConfigValues source) throws AltosConfigDataException {
477
478                 /* HAS_FLIGHT */
479                 if (main_deploy >= 0)
480                         main_deploy = source.main_deploy();
481                 if (apogee_delay >= 0)
482                         apogee_delay = source.apogee_delay();
483                 if (apogee_lockout >= 0)
484                         apogee_lockout = source.apogee_lockout();
485
486                 /* HAS_RADIO */
487                 if (has_frequency())
488                         set_frequency(source.radio_frequency());
489                 if (radio_enable >= 0)
490                         radio_enable = source.radio_enable();
491                 if (callsign != null)
492                         callsign = source.callsign();
493                 if (radio_calibration >= 0)
494                         radio_calibration = source.radio_calibration();
495                 if (telemetry_rate >= 0)
496                         telemetry_rate = source.telemetry_rate();
497
498                 /* HAS_ACCEL */
499                 if (pad_orientation >= 0)
500                         pad_orientation = source.pad_orientation();
501
502                 /* HAS_LOG */
503                 if (flight_log_max >= 0)
504                         flight_log_max = source.flight_log_max();
505
506                 /* HAS_IGNITE */
507                 if (ignite_mode >= 0)
508                         ignite_mode = source.ignite_mode();
509
510                 /* AO_PYRO_NUM */
511                 if (npyro > 0)
512                         pyros = source.pyros();
513                 if (pyro_firing_time >= 0)
514                         pyro_firing_time = source.pyro_firing_time();
515
516                 /* HAS_APRS */
517                 if (aprs_interval >= 0)
518                         aprs_interval = source.aprs_interval();
519                 if (aprs_ssid >= 0)
520                         aprs_ssid = source.aprs_ssid();
521
522                 /* HAS_BEEP */
523                 if (beep >= 0)
524                         beep = source.beep();
525                 /* HAS_TRACKER */
526                 if (tracker_motion >= 0)
527                         tracker_motion = source.tracker_motion();
528                 if (tracker_interval >= 0)
529                         tracker_interval = source.tracker_interval();
530         }
531
532         public void set_values(AltosConfigValues dest) {
533                 dest.set_serial(serial);
534                 dest.set_product(product);
535                 dest.set_version(version);
536                 dest.set_altitude_32(altitude_32);
537                 dest.set_main_deploy(main_deploy);
538                 dest.set_apogee_delay(apogee_delay);
539                 dest.set_apogee_lockout(apogee_lockout);
540                 dest.set_radio_calibration(radio_calibration);
541                 dest.set_radio_frequency(frequency());
542                 dest.set_telemetry_rate(telemetry_rate);
543                 boolean max_enabled = true;
544
545                 if (log_space() == 0)
546                         max_enabled = false;
547
548                 if (log_fixed > 0)
549                         max_enabled = false;
550
551                 switch (log_format) {
552                 case AltosLib.AO_LOG_FORMAT_TINY:
553                         max_enabled = false;
554                         break;
555                 default:
556                         if (stored_flight > 0)
557                                 max_enabled = false;
558                         break;
559                 }
560
561                 dest.set_flight_log_max_enabled(max_enabled);
562                 dest.set_radio_enable(radio_enable);
563                 dest.set_flight_log_max_limit(log_space() / 1024);
564                 dest.set_flight_log_max(flight_log_max);
565                 dest.set_ignite_mode(ignite_mode);
566                 dest.set_pad_orientation(pad_orientation);
567                 dest.set_callsign(callsign);
568                 if (npyro > 0)
569                         dest.set_pyros(pyros);
570                 else
571                         dest.set_pyros(null);
572                 dest.set_pyro_firing_time(pyro_firing_time);
573                 dest.set_aprs_interval(aprs_interval);
574                 dest.set_aprs_ssid(aprs_ssid);
575                 dest.set_beep(beep);
576                 dest.set_tracker_motion(tracker_motion);
577                 dest.set_tracker_interval(tracker_interval);
578         }
579
580         public boolean log_has_state() {
581                 switch (log_format) {
582                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
583                         return false;
584                 }
585                 return true;
586         }
587
588         public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
589
590                 /* HAS_FLIGHT */
591                 if (main_deploy >= 0)
592                         link.printf("c m %d\n", main_deploy);
593                 if (apogee_delay >= 0)
594                         link.printf("c d %d\n", apogee_delay);
595                 if (apogee_lockout >= 0)
596                         link.printf("c L %d\n", apogee_lockout);
597
598                 /* Don't mess with radio calibration when remote */
599                 if (radio_calibration > 0 && !remote)
600                         link.printf("c f %d\n", radio_calibration);
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
678                 /* HAS_BEEP */
679                 if (beep >= 0)
680                         link.printf("c b %d\n", beep);
681
682                 /* HAS_TRACKER */
683                 if (tracker_motion >= 0 && tracker_interval >= 0)
684                         link.printf("c t %d %d\n", tracker_motion, tracker_interval);
685
686                 /* HAS_GYRO */
687                 /* UI doesn't support accel cal */
688
689                 link.printf("c w\n");
690                 link.flush_output();
691         }
692
693         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
694                 reset();
695                 link.printf("c s\nf\nv\n");
696                 read_link(link, "software-version");
697                 switch (log_format) {
698                 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
699                 case AltosLib.AO_LOG_FORMAT_NONE:
700                         break;
701                 default:
702                         link.printf("l\n");
703                         read_link(link, "done");
704                         break;
705                 }
706         }
707
708 }