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