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