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