4048166cef83e44f3e42710e57e4c630a9bcfccc
[fw/altos] / altosui / AltosConfigTD.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 import org.altusmetrum.AltosLib.*;
34
35 public class AltosConfigTD implements ActionListener {
36
37         class int_ref {
38                 int     value;
39
40                 public int get() {
41                         return value;
42                 }
43                 public void set(int i) {
44                         value = i;
45                 }
46                 public int_ref(int i) {
47                         value = i;
48                 }
49         }
50
51         class string_ref {
52                 String  value;
53
54                 public String get() {
55                         return value;
56                 }
57                 public void set(String i) {
58                         value = i;
59                 }
60                 public string_ref(String i) {
61                         value = i;
62                 }
63         }
64
65         JFrame          owner;
66         AltosDevice     device;
67         AltosSerial     serial_line;
68         int_ref         serial;
69         int_ref         radio_channel;
70         int_ref         radio_calibration;
71         int_ref         radio_setting;
72         int_ref         radio_frequency;
73         string_ref      config_version;
74         string_ref      version;
75         string_ref      product;
76         AltosConfigTDUI config_ui;
77         boolean         serial_started;
78         boolean         made_visible;
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, TimeoutException {
112                 serial_started = true;
113         }
114
115         void stop_serial() throws InterruptedException {
116                 if (!serial_started)
117                         return;
118                 serial_started = false;
119         }
120
121         void update_ui() {
122                 config_ui.set_serial(serial.get());
123                 config_ui.set_product(product.get());
124                 config_ui.set_version(version.get());
125                 config_ui.set_radio_frequency(frequency());
126                 config_ui.set_radio_calibration(radio_calibration.get());
127                 config_ui.set_clean();
128                 if (!made_visible) {
129                         made_visible = true;
130                         config_ui.make_visible();
131                 }
132         }
133
134         void process_line(String line) {
135                 if (line == null) {
136                         abort();
137                         return;
138                 }
139                 if (line.equals("all finished")) {
140                         if (serial_line != null)
141                                 update_ui();
142                         return;
143                 }
144                 get_string(line, "Config version", config_version);
145                 get_int(line, "serial-number", serial);
146                 get_int(line, "Radio channel:", radio_channel);
147                 if (get_int(line, "Radio cal:", radio_calibration))
148                         System.out.printf("got radio cal %d\n", radio_calibration.get());
149                 if (get_int(line, "Frequency:", radio_frequency))
150                         System.out.printf("got radio freq %d\n", radio_frequency.get());
151                 get_int(line, "Radio setting:", radio_setting);
152                 get_string(line,"software-version", version);
153                 get_string(line,"product", product);
154         }
155
156         final static int        serial_mode_read = 0;
157         final static int        serial_mode_save = 1;
158         final static int        serial_mode_reboot = 2;
159
160         class SerialData implements Runnable {
161                 AltosConfigTD   config;
162                 int             serial_mode;
163
164                 void process_line(String line) {
165                         config.process_line(line);
166                 }
167                 void callback(String in_line) {
168                         final String line = in_line;
169                         Runnable r = new Runnable() {
170                                         public void run() {
171                                                 process_line(line);
172                                         }
173                                 };
174                         SwingUtilities.invokeLater(r);
175                 }
176
177                 void reset_data() {
178                         serial.set(0);
179                         radio_channel.set(0);
180                         radio_setting.set(0);
181                         radio_frequency.set(0);
182                         radio_calibration.set(1186611);
183                         config_version.set("0.0");
184                         version.set("unknown");
185                         product.set("unknown");
186                 }
187
188                 void get_data() {
189                         try {
190                                 boolean been_there = false;
191                                 config.start_serial();
192                                 reset_data();
193
194                                 for (;;) {
195                                         config.serial_line.printf("c s\nf\nl\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                                         System.out.printf("config_version %s\n", config_version.get());
209                                         if (been_there)
210                                                 break;
211                                         if (!config_version.get().equals("0.0"))
212                                                 break;
213                                         been_there = true;
214                                         config.serial_line.printf("C\n ");
215                                         config.serial_line.flush_input();
216                                 }
217                         } catch (InterruptedException ie) {
218                         } catch (TimeoutException te) {
219                         } finally {
220                                 try {
221                                         stop_serial();
222                                 } catch (InterruptedException ie) {
223                                 }
224                         }
225                         double  pref_frequency = AltosPreferences.frequency(serial.get());
226                         if (pref_frequency != 0)
227                                 radio_frequency.set((int) Math.floor (pref_frequency * 1000 + 0.5));
228                         callback("all finished");
229                 }
230
231                 void save_data() {
232                         double frequency = frequency();
233                         if (frequency != 0)
234                                 AltosPreferences.set_frequency(serial.get(),
235                                                                frequency);
236                 }
237
238                 public void run () {
239                         switch (serial_mode) {
240                         case serial_mode_save:
241                                 save_data();
242                                 /* fall through ... */
243                         case serial_mode_read:
244                                 get_data();
245                                 break;
246                         }
247                 }
248
249                 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
250                         config = in_config;
251                         serial_mode = in_serial_mode;
252                 }
253         }
254
255         void run_serial_thread(int serial_mode) {
256                 SerialData      sd = new SerialData(this, serial_mode);
257                 Thread          st = new Thread(sd);
258                 st.start();
259         }
260
261         void init_ui () throws InterruptedException, TimeoutException {
262                 config_ui = new AltosConfigTDUI(owner);
263                 config_ui.addActionListener(this);
264                 serial_line.set_frame(owner);
265                 set_ui();
266         }
267
268         void abort() {
269                 serial_line.close();
270                 serial_line = null;
271                 JOptionPane.showMessageDialog(owner,
272                                               String.format("Connection to \"%s\" failed",
273                                                             device.toShortString()),
274                                               "Connection Failed",
275                                               JOptionPane.ERROR_MESSAGE);
276                 config_ui.setVisible(false);
277         }
278
279         void set_ui() throws InterruptedException, TimeoutException {
280                 if (serial_line != null)
281                         run_serial_thread(serial_mode_read);
282                 else
283                         update_ui();
284         }
285
286         double frequency() {
287                 return AltosConvert.radio_to_frequency(radio_frequency.get(),
288                                                        radio_setting.get(),
289                                                        radio_calibration.get(),
290                                                        radio_channel.get());
291         }
292
293         void set_frequency(double freq) {
294                 int     frequency = radio_frequency.get();
295                 int     setting = radio_setting.get();
296
297                 if (frequency > 0) {
298                         radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
299                 } else if (setting > 0) {
300                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
301                                                                                   radio_calibration.get()));
302                         radio_channel.set(0);
303                 } else {
304                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
305                 }
306         }
307
308         void save_data() {
309
310                 set_frequency(config_ui.radio_frequency());
311                 run_serial_thread(serial_mode_save);
312         }
313
314         public void actionPerformed(ActionEvent e) {
315                 String  cmd = e.getActionCommand();
316                 try {
317                         if (cmd.equals("Save")) {
318                                 save_data();
319                         } else if (cmd.equals("Reset")) {
320                                 set_ui();
321                         } else if (cmd.equals("Reboot")) {
322                                 if (serial_line != null)
323                                         run_serial_thread(serial_mode_reboot);
324                         } else if (cmd.equals("Close")) {
325                                 if (serial_line != null)
326                                         serial_line.close();
327                         }
328                 } catch (InterruptedException ie) {
329                         abort();
330                 } catch (TimeoutException te) {
331                         abort();
332                 }
333         }
334
335         public AltosConfigTD(JFrame given_owner) {
336                 owner = given_owner;
337
338                 serial = new int_ref(0);
339                 radio_channel = new int_ref(0);
340                 radio_setting = new int_ref(0);
341                 radio_frequency = new int_ref(0);
342                 radio_calibration = new int_ref(1186611);
343                 config_version = new string_ref("0.0");
344                 version = new string_ref("unknown");
345                 product = new string_ref("unknown");
346
347                 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
348                 if (device != null) {
349                         try {
350                                 serial_line = new AltosSerial(device);
351                                 try {
352                                         init_ui();
353                                 } catch (InterruptedException ie) {
354                                         abort();
355                                 } catch (TimeoutException te) {
356                                         abort();
357                                 }
358                         } catch (FileNotFoundException ee) {
359                                 JOptionPane.showMessageDialog(owner,
360                                                               ee.getMessage(),
361                                                               "Cannot open target device",
362                                                               JOptionPane.ERROR_MESSAGE);
363                         } catch (AltosSerialInUseException si) {
364                                 JOptionPane.showMessageDialog(owner,
365                                                               String.format("Device \"%s\" already in use",
366                                                                             device.toShortString()),
367                                                               "Device in use",
368                                                               JOptionPane.ERROR_MESSAGE);
369                         } catch (IOException ee) {
370                                 JOptionPane.showMessageDialog(owner,
371                                                               device.toShortString(),
372                                                               ee.getLocalizedMessage(),
373                                                               JOptionPane.ERROR_MESSAGE);
374                         }
375                 }
376         }
377 }