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