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