altosui: Move AltosConfigData.java to library
[fw/altos] / altosui / altoslib / src / org / altusmetrum / 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 altosui;
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         String  manufacturer;
31         String  product;
32         String  version;
33         int     log_format;
34         int     serial;
35
36         /* Strings returned */
37         LinkedList<String>      lines;
38
39         /* Config information */
40         int     config_major;
41         int     config_minor;
42         int     main_deploy;
43         int     apogee_delay;
44         int     radio_channel;
45         int     radio_setting;
46         String  callsign;
47         int     accel_cal_plus, accel_cal_minus;
48         int     radio_calibration;
49         int     flight_log_max;
50         int     ignite_mode;
51         int     stored_flight;
52         int     storage_size;
53         int     storage_erase_unit;
54
55         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         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 Altos.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         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
101                 link.printf("c s\nf\nl\nv\n");
102                 lines = new LinkedList<String>();
103                 radio_setting = 0;
104                 stored_flight = 0;
105                 for (;;) {
106                         String line = link.get_reply();
107                         if (line == null)
108                                 throw new TimeoutException();
109                         if (line.contains("Syntax error"))
110                                 continue;
111                         lines.add(line);
112                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
113                         try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
114                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
115                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
116                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
117                         try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
118                         try {
119                                 if (line.startsWith("Accel cal")) {
120                                         String[] bits = line.split("\\s+");
121                                         if (bits.length >= 6) {
122                                                 accel_cal_plus = Integer.parseInt(bits[3]);
123                                                 accel_cal_minus = Integer.parseInt(bits[5]);
124                                         }
125                                 }
126                         } catch (Exception e) {}
127                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
128                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
129                         try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
130                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
131                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
132                         try { product = get_string(line,"product"); } catch (Exception e) {}
133
134                         try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
135                         try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
136                         try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
137
138                         /* signals the end of the version info */
139                         if (line.startsWith("software-version"))
140                                 break;
141                 }
142         }
143
144 }