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