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