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