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