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