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