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