altos: Note that Lithium battery may be included with MicroPeak
[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 String   version;
30         public int      log_format;
31         public int      serial;
32
33         /* Strings returned */
34         public LinkedList<String>       lines;
35
36         /* Config information */
37         public int      config_major;
38         public int      config_minor;
39         public int      main_deploy;
40         public int      apogee_delay;
41         public int      radio_channel;
42         public int      radio_setting;
43         public int      radio_frequency;
44         public String   callsign;
45         public int      accel_cal_plus, accel_cal_minus;
46         public int      radio_calibration;
47         public int      flight_log_max;
48         public int      ignite_mode;
49         public int      stored_flight;
50         public int      storage_size;
51         public int      storage_erase_unit;
52
53         public AltosPyro[]      pyros;
54
55         public static String get_string(String line, String label) throws  ParseException {
56                 if (line.startsWith(label)) {
57                         String  quoted = line.substring(label.length()).trim();
58
59                         if (quoted.startsWith("\""))
60                                 quoted = quoted.substring(1);
61                         if (quoted.endsWith("\""))
62                                 quoted = quoted.substring(0,quoted.length()-1);
63                         return quoted;
64                 }
65                 throw new ParseException("mismatch", 0);
66         }
67
68         public static int get_int(String line, String label) throws NumberFormatException, ParseException {
69                 if (line.startsWith(label)) {
70                         String tail = line.substring(label.length()).trim();
71                         String[] tokens = tail.split("\\s+");
72                         if (tokens.length > 0)
73                                 return  Integer.parseInt(tokens[0]);
74                 }
75                 throw new ParseException("mismatch", 0);
76         }
77
78         public Iterator<String> iterator() {
79                 return lines.iterator();
80         }
81
82         public int log_available() {
83                 switch (log_format) {
84                 case AltosLib.AO_LOG_FORMAT_TINY:
85                         if (stored_flight == 0)
86                                 return 1;
87                         return 0;
88                 default:
89                         if (flight_log_max <= 0)
90                                 return 1;
91                         int     log_space = storage_size - storage_erase_unit;
92                         int     log_used = stored_flight * flight_log_max;
93
94                         if (log_used >= log_space)
95                                 return 0;
96                         return (log_space - log_used) / flight_log_max;
97                 }
98         }
99
100         int[] parse_version(String v) {
101                 String[] parts = v.split("\\.");
102                 int r[] = new int[parts.length];
103
104                 for (int i = 0; i < parts.length; i++) {
105                         try {
106                                 r[i] = AltosLib.fromdec(parts[i]);
107                         } catch (NumberFormatException n) {
108                                 r[i] = 0;
109                         }
110                 }
111
112                 return r;
113         }
114         
115         public int compare_version(String other) {
116                 int[]   me = parse_version(version);
117                 int[]   them = parse_version(other);
118
119                 int     l = Math.min(me.length, them.length);
120
121                 for (int i = 0; i < l; i++) {
122                         int     d = me[i] - them[i];
123                         if (d != 0)
124                                 return d;
125                 }
126                 if (me.length > l)
127                         return 1;
128                 if (them.length > l)
129                         return -1;
130                 return 0;
131         }
132
133         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
134                 link.printf("c s\nf\nl\nv\n");
135                 lines = new LinkedList<String>();
136                 radio_setting = 0;
137                 radio_frequency = 0;
138                 stored_flight = 0;
139                 serial = -1;
140                 pyros = null;
141
142                 int npyro = 0;
143                 int pyro = 0;
144                 for (;;) {
145                         String line = link.get_reply();
146                         if (line == null)
147                                 throw new TimeoutException();
148                         if (line.contains("Syntax error"))
149                                 continue;
150                         lines.add(line);
151                         if (pyro < npyro - 1) {
152                                 if (pyros == null)
153                                         pyros = new AltosPyro[npyro];
154                                 try {
155                                         pyros[pyro] = new AltosPyro(pyro, line);
156                                 } catch (ParseException e) {
157                                 }
158                                 ++pyro;
159                                 continue;
160                         }
161                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
162                         try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
163                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
164                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
165                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
166                         try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
167                         try {
168                                 radio_frequency = get_int(line, "Frequency:");
169                                 if (radio_frequency < 0)
170                                         radio_frequency = 434550;
171                         } catch (Exception e) {}
172                         try {
173                                 if (line.startsWith("Accel cal")) {
174                                         String[] bits = line.split("\\s+");
175                                         if (bits.length >= 6) {
176                                                 accel_cal_plus = Integer.parseInt(bits[3]);
177                                                 accel_cal_minus = Integer.parseInt(bits[5]);
178                                         }
179                                 }
180                         } catch (Exception e) {}
181                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
182                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
183                         try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
184                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
185                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
186                         try { product = get_string(line,"product"); } catch (Exception e) {}
187                         try { manufacturer = get_string(line,"manufacturer"); } catch (Exception e) {}
188
189                         try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
190                         try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
191                         try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
192                         try { npyro = get_int(line, "Pyro-count:"); pyro = 0; } catch (Exception e) {}
193
194                         /* signals the end of the version info */
195                         if (line.startsWith("software-version"))
196                                 break;
197                 }
198         }
199
200 }