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