update changelogs for Debian build
[fw/altos] / altosui / AltosConfig.java
1 /*
2  * Copyright © 2010 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 AltosConfig implements ActionListener {
34
35         class int_ref {
36                 int     value;
37
38                 public int get() {
39                         return value;
40                 }
41                 public void set(int i) {
42                         value = i;
43                 }
44                 public int_ref(int i) {
45                         value = i;
46                 }
47         }
48
49         class string_ref {
50                 String  value;
51
52                 public String get() {
53                         return value;
54                 }
55                 public void set(String i) {
56                         value = i;
57                 }
58                 public string_ref(String i) {
59                         value = i;
60                 }
61         }
62
63         JFrame          owner;
64         AltosDevice     device;
65         AltosSerial     serial_line;
66         boolean         remote;
67         int_ref         serial;
68         int_ref         main_deploy;
69         int_ref         apogee_delay;
70         int_ref         radio_channel;
71         int_ref         radio_calibration;
72         string_ref      version;
73         string_ref      product;
74         string_ref      callsign;
75         AltosConfigUI   config_ui;
76         boolean         serial_started;
77
78         boolean get_int(String line, String label, int_ref x) {
79                 if (line.startsWith(label)) {
80                         try {
81                                 String tail = line.substring(label.length()).trim();
82                                 String[] tokens = tail.split("\\s+");
83                                 if (tokens.length > 0) {
84                                         int     i = Integer.parseInt(tokens[0]);
85                                         x.set(i);
86                                         return true;
87                                 }
88                         } catch (NumberFormatException ne) {
89                         }
90                 }
91                 return false;
92         }
93
94         boolean get_string(String line, String label, string_ref s) {
95                 if (line.startsWith(label)) {
96                         String  quoted = line.substring(label.length()).trim();
97
98                         if (quoted.startsWith("\""))
99                                 quoted = quoted.substring(1);
100                         if (quoted.endsWith("\""))
101                                 quoted = quoted.substring(0,quoted.length()-1);
102                         s.set(quoted);
103                         return true;
104                 } else {
105                         return false;
106                 }
107         }
108
109         void start_serial() throws InterruptedException {
110                 serial_started = true;
111                 if (remote) {
112                         serial_line.set_radio();
113                         serial_line.printf("p\nE 0\n");
114                         serial_line.flush_input();
115                 }
116         }
117
118         void stop_serial() throws InterruptedException {
119                 if (!serial_started)
120                         return;
121                 serial_started = false;
122                 if (remote) {
123                         serial_line.printf("~");
124                         serial_line.flush_output();
125                 }
126         }
127
128         void get_data() throws InterruptedException, TimeoutException {
129                 try {
130                         start_serial();
131                         serial_line.printf("c s\nv\n");
132                         for (;;) {
133                                 String line = serial_line.get_reply(5000);
134                                 if (line == null)
135                                         throw new TimeoutException();
136                                 get_int(line, "serial-number", serial);
137                                 get_int(line, "Main deploy:", main_deploy);
138                                 get_int(line, "Apogee delay:", apogee_delay);
139                                 get_int(line, "Radio channel:", radio_channel);
140                                 get_int(line, "Radio cal:", radio_calibration);
141                                 get_string(line, "Callsign:", callsign);
142                                 get_string(line,"software-version", version);
143                                 get_string(line,"product", product);
144
145                                 /* signals the end of the version info */
146                                 if (line.startsWith("software-version"))
147                                         break;
148                         }
149                 } finally {
150                         stop_serial();
151                 }
152         }
153
154         void init_ui () throws InterruptedException, TimeoutException {
155                 config_ui = new AltosConfigUI(owner, remote);
156                 config_ui.addActionListener(this);
157                 set_ui();
158         }
159
160         void abort() {
161                 JOptionPane.showMessageDialog(owner,
162                                               String.format("Connection to \"%s\" failed",
163                                                             device.toShortString()),
164                                               "Connection Failed",
165                                               JOptionPane.ERROR_MESSAGE);
166                 try {
167                         stop_serial();
168                 } catch (InterruptedException ie) {
169                 }
170                 serial_line.close();
171                 serial_line = null;
172         }
173
174         void set_ui() throws InterruptedException, TimeoutException {
175                 if (serial_line != null)
176                         get_data();
177                 config_ui.set_serial(serial.get());
178                 config_ui.set_product(product.get());
179                 config_ui.set_version(version.get());
180                 config_ui.set_main_deploy(main_deploy.get());
181                 config_ui.set_apogee_delay(apogee_delay.get());
182                 config_ui.set_radio_channel(radio_channel.get());
183                 config_ui.set_radio_calibration(radio_calibration.get());
184                 config_ui.set_callsign(callsign.get());
185                 config_ui.set_clean();
186         }
187
188         void run_dialog() {
189         }
190
191         void save_data() {
192                 main_deploy.set(config_ui.main_deploy());
193                 apogee_delay.set(config_ui.apogee_delay());
194                 radio_channel.set(config_ui.radio_channel());
195                 radio_calibration.set(config_ui.radio_calibration());
196                 callsign.set(config_ui.callsign());
197                 try {
198                         start_serial();
199                         serial_line.printf("c m %d\n", main_deploy.get());
200                         serial_line.printf("c d %d\n", apogee_delay.get());
201                         if (!remote) {
202                                 serial_line.printf("c r %d\n", radio_channel.get());
203                                 serial_line.printf("c f %d\n", radio_calibration.get());
204                         }
205                         serial_line.printf("c c %s\n", callsign.get());
206                         serial_line.printf("c w\n");
207                 } catch (InterruptedException ie) {
208                 } finally {
209                         try {
210                                 stop_serial();
211                         } catch (InterruptedException ie) {
212                         }
213                 }
214         }
215
216         public void actionPerformed(ActionEvent e) {
217                 String  cmd = e.getActionCommand();
218                 try {
219                         if (cmd.equals("Save")) {
220                                 save_data();
221                                 set_ui();
222                         } else if (cmd.equals("Reset")) {
223                                 set_ui();
224                         } else if (cmd.equals("Reboot")) {
225                                 if (serial_line != null) {
226                                         start_serial();
227                                         serial_line.printf("r eboot\n");
228                                         serial_line.flush_output();
229                                         stop_serial();
230                                         serial_line.close();
231                                 }
232                         } else if (cmd.equals("Close")) {
233                                 if (serial_line != null)
234                                         serial_line.close();
235                         }
236                 } catch (InterruptedException ie) {
237                         abort();
238                 } catch (TimeoutException te) {
239                         abort();
240                 }
241         }
242
243         public AltosConfig(JFrame given_owner) {
244                 owner = given_owner;
245
246                 serial = new int_ref(0);
247                 main_deploy = new int_ref(250);
248                 apogee_delay = new int_ref(0);
249                 radio_channel = new int_ref(0);
250                 radio_calibration = new int_ref(1186611);
251                 callsign = new string_ref("N0CALL");
252                 version = new string_ref("unknown");
253                 product = new string_ref("unknown");
254
255                 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
256                 if (device != null) {
257                         try {
258                                 serial_line = new AltosSerial(device);
259                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
260                                         remote = true;
261                                 try {
262                                         init_ui();
263                                         config_ui.make_visible();
264                                 } catch (InterruptedException ie) {
265                                         abort();
266                                 } catch (TimeoutException te) {
267                                         abort();
268                                 }
269                         } catch (FileNotFoundException ee) {
270                                 JOptionPane.showMessageDialog(owner,
271                                                               String.format("Cannot open device \"%s\"",
272                                                                             device.toShortString()),
273                                                               "Cannot open target device",
274                                                               JOptionPane.ERROR_MESSAGE);
275                         } catch (AltosSerialInUseException si) {
276                                 JOptionPane.showMessageDialog(owner,
277                                                               String.format("Device \"%s\" already in use",
278                                                                             device.toShortString()),
279                                                               "Device in use",
280                                                               JOptionPane.ERROR_MESSAGE);
281                         } catch (IOException ee) {
282                                 JOptionPane.showMessageDialog(owner,
283                                                               device.toShortString(),
284                                                               ee.getLocalizedMessage(),
285                                                               JOptionPane.ERROR_MESSAGE);
286                         }
287                 }
288         }
289 }