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