fix bashism that prevents building with /bin/sh->/bin/dash
[fw/altos] / altosui / 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.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30
31 import libaltosJNI.*;
32
33 public class AltosConfigData implements Iterable<String> {
34
35         /* Version information */
36         String  manufacturer;
37         String  product;
38         String  version;
39         int     log_format;
40         int     serial;
41
42         /* Strings returned */
43         LinkedList<String>      lines;
44
45         /* Config information */
46         int     config_major;
47         int     config_minor;
48         int     main_deploy;
49         int     apogee_delay;
50         int     radio_channel;
51         int     radio_setting;
52         String  callsign;
53         int     accel_cal_plus, accel_cal_minus;
54         int     radio_calibration;
55         int     flight_log_max;
56         int     ignite_mode;
57         int     stored_flight;
58         int     storage_size;
59         int     storage_erase_unit;
60
61         static String get_string(String line, String label) throws  ParseException {
62                 if (line.startsWith(label)) {
63                         String  quoted = line.substring(label.length()).trim();
64
65                         if (quoted.startsWith("\""))
66                                 quoted = quoted.substring(1);
67                         if (quoted.endsWith("\""))
68                                 quoted = quoted.substring(0,quoted.length()-1);
69                         return quoted;
70                 }
71                 throw new ParseException("mismatch", 0);
72         }
73
74         static int get_int(String line, String label) throws NumberFormatException, ParseException {
75                 if (line.startsWith(label)) {
76                         String tail = line.substring(label.length()).trim();
77                         String[] tokens = tail.split("\\s+");
78                         if (tokens.length > 0)
79                                 return  Integer.parseInt(tokens[0]);
80                 }
81                 throw new ParseException("mismatch", 0);
82         }
83
84         public Iterator<String> iterator() {
85                 return lines.iterator();
86         }
87
88         public int log_available() {
89                 switch (log_format) {
90                 case Altos.AO_LOG_FORMAT_TINY:
91                         if (stored_flight == 0)
92                                 return 1;
93                         return 0;
94                 default:
95                         if (flight_log_max <= 0)
96                                 return 1;
97                         int     log_space = storage_size - storage_erase_unit;
98                         int     log_used = stored_flight * flight_log_max;
99
100                         if (log_used >= log_space)
101                                 return 0;
102                         return (log_space - log_used) / flight_log_max;
103                 }
104         }
105
106         public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
107                 serial_line.printf("c s\nf\nl\nv\n");
108                 lines = new LinkedList<String>();
109                 radio_setting = 0;
110                 stored_flight = 0;
111                 for (;;) {
112                         String line = serial_line.get_reply();
113                         if (line == null)
114                                 throw new TimeoutException();
115                         if (line.contains("Syntax error"))
116                                 continue;
117                         lines.add(line);
118                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
119                         try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
120                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
121                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
122                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
123                         try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
124                         try {
125                                 if (line.startsWith("Accel cal")) {
126                                         String[] bits = line.split("\\s+");
127                                         if (bits.length >= 6) {
128                                                 accel_cal_plus = Integer.parseInt(bits[3]);
129                                                 accel_cal_minus = Integer.parseInt(bits[5]);
130                                         }
131                                 }
132                         } catch (Exception e) {}
133                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
134                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
135                         try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
136                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
137                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
138                         try { product = get_string(line,"product"); } catch (Exception e) {}
139
140                         try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
141                         try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
142                         try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
143
144                         /* signals the end of the version info */
145                         if (line.startsWith("software-version"))
146                                 break;
147                 }
148         }
149
150 }