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