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