clean up all existing lintian warnings
[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         int_ref         flight_log_max;
73         int_ref         ignite_mode;
74         int_ref         pad_orientation;
75         string_ref      version;
76         string_ref      product;
77         string_ref      callsign;
78         AltosConfigUI   config_ui;
79         boolean         serial_started;
80
81         boolean get_int(String line, String label, int_ref x) {
82                 if (line.startsWith(label)) {
83                         try {
84                                 String tail = line.substring(label.length()).trim();
85                                 String[] tokens = tail.split("\\s+");
86                                 if (tokens.length > 0) {
87                                         int     i = Integer.parseInt(tokens[0]);
88                                         x.set(i);
89                                         return true;
90                                 }
91                         } catch (NumberFormatException ne) {
92                         }
93                 }
94                 return false;
95         }
96
97         boolean get_string(String line, String label, string_ref s) {
98                 if (line.startsWith(label)) {
99                         String  quoted = line.substring(label.length()).trim();
100
101                         if (quoted.startsWith("\""))
102                                 quoted = quoted.substring(1);
103                         if (quoted.endsWith("\""))
104                                 quoted = quoted.substring(0,quoted.length()-1);
105                         s.set(quoted);
106                         return true;
107                 } else {
108                         return false;
109                 }
110         }
111
112         void start_serial() throws InterruptedException {
113                 serial_started = true;
114                 if (remote)
115                         serial_line.start_remote();
116         }
117
118         void stop_serial() throws InterruptedException {
119                 if (!serial_started)
120                         return;
121                 serial_started = false;
122                 if (remote)
123                         serial_line.stop_remote();
124         }
125
126         void update_ui() {
127                 config_ui.set_serial(serial.get());
128                 config_ui.set_product(product.get());
129                 config_ui.set_version(version.get());
130                 config_ui.set_main_deploy(main_deploy.get());
131                 config_ui.set_apogee_delay(apogee_delay.get());
132                 config_ui.set_radio_channel(radio_channel.get());
133                 config_ui.set_radio_calibration(radio_calibration.get());
134                 config_ui.set_flight_log_max(flight_log_max.get());
135                 config_ui.set_ignite_mode(ignite_mode.get());
136                 config_ui.set_pad_orientation(pad_orientation.get());
137                 config_ui.set_callsign(callsign.get());
138                 config_ui.set_clean();
139                 config_ui.make_visible();
140         }
141
142         void process_line(String line) {
143                 if (line == null) {
144                         abort();
145                         return;
146                 }
147                 if (line.equals("done")) {
148                         if (serial_line != null)
149                                 update_ui();
150                         return;
151                 }
152                 get_int(line, "serial-number", serial);
153                 get_int(line, "Main deploy:", main_deploy);
154                 get_int(line, "Apogee delay:", apogee_delay);
155                 get_int(line, "Radio channel:", radio_channel);
156                 get_int(line, "Radio cal:", radio_calibration);
157                 get_int(line, "Max flight log:", flight_log_max);
158                 get_int(line, "Ignite mode:", ignite_mode);
159                 get_int(line, "Pad orientation:", pad_orientation);
160                 get_string(line, "Callsign:", callsign);
161                 get_string(line,"software-version", version);
162                 get_string(line,"product", product);
163         }
164
165         final static int        serial_mode_read = 0;
166         final static int        serial_mode_save = 1;
167         final static int        serial_mode_reboot = 2;
168
169         class SerialData implements Runnable {
170                 AltosConfig     config;
171                 int             serial_mode;
172
173                 void process_line(String line) {
174                         config.process_line(line);
175                 }
176                 void callback(String in_line) {
177                         final String line = in_line;
178                         Runnable r = new Runnable() {
179                                         public void run() {
180                                                 process_line(line);
181                                         }
182                                 };
183                         SwingUtilities.invokeLater(r);
184                 }
185
186                 void get_data() {
187                         try {
188                                 config.start_serial();
189                                 config.serial_line.printf("c s\nv\n");
190                                 for (;;) {
191                                         try {
192                                                 String line = config.serial_line.get_reply(5000);
193                                                 if (line == null)
194                                                         stop_serial();
195                                                 callback(line);
196                                                 if (line.startsWith("software-version"))
197                                                         break;
198                                         } catch (Exception e) {
199                                                 break;
200                                         }
201                                 }
202                         } catch (InterruptedException ie) {
203                         } finally {
204                                 try {
205                                         stop_serial();
206                                 } catch (InterruptedException ie) {
207                                 }
208                         }
209                         callback("done");
210                 }
211
212                 void save_data() {
213                         try {
214                                 int     channel;
215                                 start_serial();
216                                 serial_line.printf("c m %d\n", main_deploy.get());
217                                 serial_line.printf("c d %d\n", apogee_delay.get());
218                                 channel = radio_channel.get();
219                                 serial_line.printf("c r %d\n", channel);
220                                 if (remote) {
221                                         serial_line.stop_remote();
222                                         serial_line.set_channel(channel);
223                                         AltosPreferences.set_channel(device.getSerial(), channel);
224                                         serial_line.start_remote();
225                                 }
226                                 if (!remote)
227                                         serial_line.printf("c f %d\n", radio_calibration.get());
228                                 serial_line.printf("c c %s\n", callsign.get());
229                                 if (flight_log_max.get() != 0)
230                                         serial_line.printf("c l %d\n", flight_log_max.get());
231                                 if (ignite_mode.get() >= 0)
232                                         serial_line.printf("c i %d\n", ignite_mode.get());
233                                 if (pad_orientation.get() >= 0)
234                                         serial_line.printf("c o %d\n", pad_orientation.get());
235                                 serial_line.printf("c w\n");
236                         } catch (InterruptedException ie) {
237                         } finally {
238                                 try {
239                                         stop_serial();
240                                 } catch (InterruptedException ie) {
241                                 }
242                         }
243                 }
244
245                 void reboot() {
246                         try {
247                                 start_serial();
248                                 serial_line.printf("r eboot\n");
249                                 serial_line.flush_output();
250                         } catch (InterruptedException ie) {
251                         } finally {
252                                 try {
253                                         stop_serial();
254                                 } catch (InterruptedException ie) {
255                                 }
256                                 serial_line.close();
257                         }
258                 }
259
260                 public void run () {
261                         switch (serial_mode) {
262                         case serial_mode_save:
263                                 save_data();
264                                 /* fall through ... */
265                         case serial_mode_read:
266                                 get_data();
267                                 break;
268                         case serial_mode_reboot:
269                                 reboot();
270                                 break;
271                         }
272                 }
273
274                 public SerialData(AltosConfig in_config, int in_serial_mode) {
275                         config = in_config;
276                         serial_mode = in_serial_mode;
277                 }
278         }
279
280         void run_serial_thread(int serial_mode) {
281                 SerialData      sd = new SerialData(this, serial_mode);
282                 Thread          st = new Thread(sd);
283                 st.start();
284         }
285
286         void init_ui () throws InterruptedException, TimeoutException {
287                 config_ui = new AltosConfigUI(owner, remote);
288                 config_ui.addActionListener(this);
289                 serial_line.set_frame(owner);
290                 set_ui();
291         }
292
293         void abort() {
294                 serial_line.close();
295                 serial_line = null;
296                 JOptionPane.showMessageDialog(owner,
297                                               String.format("Connection to \"%s\" failed",
298                                                             device.toShortString()),
299                                               "Connection Failed",
300                                               JOptionPane.ERROR_MESSAGE);
301                 config_ui.setVisible(false);
302         }
303
304         void set_ui() throws InterruptedException, TimeoutException {
305                 if (serial_line != null)
306                         run_serial_thread(serial_mode_read);
307                 else
308                         update_ui();
309         }
310
311         void save_data() {
312                 main_deploy.set(config_ui.main_deploy());
313                 apogee_delay.set(config_ui.apogee_delay());
314                 radio_channel.set(config_ui.radio_channel());
315                 radio_calibration.set(config_ui.radio_calibration());
316                 flight_log_max.set(config_ui.flight_log_max());
317                 ignite_mode.set(config_ui.ignite_mode());
318                 pad_orientation.set(config_ui.pad_orientation());
319                 callsign.set(config_ui.callsign());
320                 run_serial_thread(serial_mode_save);
321         }
322
323         public void actionPerformed(ActionEvent e) {
324                 String  cmd = e.getActionCommand();
325                 try {
326                         if (cmd.equals("Save")) {
327                                 save_data();
328                         } else if (cmd.equals("Reset")) {
329                                 set_ui();
330                         } else if (cmd.equals("Reboot")) {
331                                 if (serial_line != null)
332                                         run_serial_thread(serial_mode_reboot);
333                         } else if (cmd.equals("Close")) {
334                                 if (serial_line != null)
335                                         serial_line.close();
336                         }
337                 } catch (InterruptedException ie) {
338                         abort();
339                 } catch (TimeoutException te) {
340                         abort();
341                 }
342         }
343
344         public AltosConfig(JFrame given_owner) {
345                 owner = given_owner;
346
347                 serial = new int_ref(0);
348                 main_deploy = new int_ref(250);
349                 apogee_delay = new int_ref(0);
350                 radio_channel = new int_ref(0);
351                 radio_calibration = new int_ref(1186611);
352                 flight_log_max = new int_ref(0);
353                 ignite_mode = new int_ref(-1);
354                 pad_orientation = new int_ref(-1);
355                 callsign = new string_ref("N0CALL");
356                 version = new string_ref("unknown");
357                 product = new string_ref("unknown");
358
359                 device = AltosDeviceDialog.show(owner, Altos.product_any);
360                 if (device != null) {
361                         try {
362                                 serial_line = new AltosSerial(device);
363                                 if (!device.matchProduct(Altos.product_telemetrum))
364                                         remote = true;
365                                 try {
366                                         init_ui();
367                                 } catch (InterruptedException ie) {
368                                         abort();
369                                 } catch (TimeoutException te) {
370                                         abort();
371                                 }
372                         } catch (FileNotFoundException ee) {
373                                 JOptionPane.showMessageDialog(owner,
374                                                               String.format("Cannot open device \"%s\"",
375                                                                             device.toShortString()),
376                                                               "Cannot open target device",
377                                                               JOptionPane.ERROR_MESSAGE);
378                         } catch (AltosSerialInUseException si) {
379                                 JOptionPane.showMessageDialog(owner,
380                                                               String.format("Device \"%s\" already in use",
381                                                                             device.toShortString()),
382                                                               "Device in use",
383                                                               JOptionPane.ERROR_MESSAGE);
384                         } catch (IOException ee) {
385                                 JOptionPane.showMessageDialog(owner,
386                                                               device.toShortString(),
387                                                               ee.getLocalizedMessage(),
388                                                               JOptionPane.ERROR_MESSAGE);
389                         }
390                 }
391         }
392 }