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