altoslib: Make AltosConfigData parse all of the config data
[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         /* Storage info replies */
71         public int      storage_size;
72         public int      storage_erase_unit;
73
74         /* Log listing replies */
75         public int      stored_flight;
76
77
78         public static String get_string(String line, String label) throws  ParseException {
79                 if (line.startsWith(label)) {
80                         String  quoted = line.substring(label.length()).trim();
81
82                         if (quoted.startsWith("\""))
83                                 quoted = quoted.substring(1);
84                         if (quoted.endsWith("\""))
85                                 quoted = quoted.substring(0,quoted.length()-1);
86                         return quoted;
87                 }
88                 throw new ParseException("mismatch", 0);
89         }
90
91         public static int get_int(String line, String label) throws NumberFormatException, ParseException {
92                 if (line.startsWith(label)) {
93                         String tail = line.substring(label.length()).trim();
94                         String[] tokens = tail.split("\\s+");
95                         if (tokens.length > 0)
96                                 return  Integer.parseInt(tokens[0]);
97                 }
98                 throw new ParseException("mismatch", 0);
99         }
100
101         public Iterator<String> iterator() {
102                 return lines.iterator();
103         }
104
105         public int log_available() {
106                 switch (log_format) {
107                 case AltosLib.AO_LOG_FORMAT_TINY:
108                         if (stored_flight == 0)
109                                 return 1;
110                         return 0;
111                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
112                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
113                         return 1;
114                 default:
115                         if (flight_log_max <= 0)
116                                 return 1;
117                         int     log_space = storage_size - storage_erase_unit;
118                         int     log_used = stored_flight * flight_log_max;
119
120                         if (log_used >= log_space)
121                                 return 0;
122                         return (log_space - log_used) / flight_log_max;
123                 }
124         }
125
126         int[] parse_version(String v) {
127                 String[] parts = v.split("\\.");
128                 int r[] = new int[parts.length];
129
130                 for (int i = 0; i < parts.length; i++) {
131                         try {
132                                 r[i] = AltosLib.fromdec(parts[i]);
133                         } catch (NumberFormatException n) {
134                                 r[i] = 0;
135                         }
136                 }
137
138                 return r;
139         }
140         
141         public int compare_version(String other) {
142                 int[]   me = parse_version(version);
143                 int[]   them = parse_version(other);
144
145                 int     l = Math.min(me.length, them.length);
146
147                 for (int i = 0; i < l; i++) {
148                         int     d = me[i] - them[i];
149                         if (d != 0)
150                                 return d;
151                 }
152                 if (me.length > l)
153                         return 1;
154                 if (them.length > l)
155                         return -1;
156                 return 0;
157         }
158
159         public void reset() {
160                 lines = new LinkedList<String>();
161
162                 serial = -1;
163                 radio_setting = 0;
164                 radio_frequency = 0;
165                 pyros = null;
166                 npyro = 0;
167                 pyro = 0;
168         }
169         
170         public void parse_line(String line) {
171                 lines.add(line);
172                 /* Version replies */
173                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
174                 try { product = get_string(line, "product"); } catch (Exception e) {}
175                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
176                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
177                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
178                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
179
180                 /* Version also contains MS5607 info, which we ignore here */
181
182                 /* Config show replies */
183
184                 /* HAS_FLIGHT */
185                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
186                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
187                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
188
189                 /* HAS_RADIO */
190                 try {
191                         radio_frequency = get_int(line, "Frequency:");
192                         if (radio_frequency < 0)
193                                 radio_frequency = 434550;
194                 } catch (Exception e) {}
195                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
196                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
197                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
198
199                 /* Old HAS_RADIO values */
200                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
201                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
202
203                 /* HAS_ACCEL */
204                 try {
205                         if (line.startsWith("Accel cal")) {
206                                 String[] bits = line.split("\\s+");
207                                 if (bits.length >= 6) {
208                                         accel_cal_plus = Integer.parseInt(bits[3]);
209                                         accel_cal_minus = Integer.parseInt(bits[5]);
210                                 }
211                         }
212                 } catch (Exception e) {}
213                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
214
215                 /* HAS_LOG */
216                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
217
218                 /* HAS_IGNITE */
219                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
220
221                 /* HAS_AES */
222                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
223
224                 /* AO_PYRO_NUM */
225                 try {
226                         npyro = get_int(line, "Pyro-count:");
227                         pyros = new AltosPyro[npyro];
228                         pyro = 0;
229                 } catch (Exception e) {}
230                 if (npyro > 0) {
231                         try {
232                                 AltosPyro p = new AltosPyro(pyro, line);
233                                 if (pyro < npyro - 1)
234                                         pyros[pyro++] = p;
235                         } catch (Exception e) {}
236                 }
237                 
238                 /* Storage info replies */
239                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
240                 try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
241
242                 /* Log listing replies */
243                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
244         }
245
246         public AltosConfigData() {
247                 this.reset();
248         }
249
250         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
251                 for (;;) {
252                         String line = link.get_reply();
253                         if (line == null)
254                                 throw new TimeoutException();
255                         if (line.contains("Syntax error"))
256                                 continue;
257                         this.parse_line(line);
258
259                         /* signals the end of the version info */
260                         if (line.startsWith(finished))
261                                 break;
262                 }
263         }
264
265         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
266                 this.reset();
267                 link.printf("c s\nf\nv\n");
268                 read_link(link, "software-version");
269                 switch (log_format) {
270                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
271                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
272                         break;
273                 default:
274                         link.printf("l\n");
275                         read_link(link, "done");
276                 }
277         }
278
279 }