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