altosui: Serialize data access in TD config code
[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         made_visible;
78
79         boolean get_int(String line, String label, int_ref x) {
80                 if (line.startsWith(label)) {
81                         try {
82                                 String tail = line.substring(label.length()).trim();
83                                 String[] tokens = tail.split("\\s+");
84                                 if (tokens.length > 0) {
85                                         int     i = Integer.parseInt(tokens[0]);
86                                         x.set(i);
87                                         return true;
88                                 }
89                         } catch (NumberFormatException ne) {
90                         }
91                 }
92                 return false;
93         }
94
95         boolean get_string(String line, String label, string_ref s) {
96                 if (line.startsWith(label)) {
97                         String  quoted = line.substring(label.length()).trim();
98
99                         if (quoted.startsWith("\""))
100                                 quoted = quoted.substring(1);
101                         if (quoted.endsWith("\""))
102                                 quoted = quoted.substring(0,quoted.length()-1);
103                         s.set(quoted);
104                         return true;
105                 } else {
106                         return false;
107                 }
108         }
109
110         synchronized void update_ui() {
111                 config_ui.set_serial(serial.get());
112                 config_ui.set_product(product.get());
113                 config_ui.set_version(version.get());
114                 config_ui.set_radio_frequency(frequency());
115                 config_ui.set_radio_calibration(radio_calibration.get());
116                 config_ui.set_clean();
117                 if (!made_visible) {
118                         made_visible = true;
119                         config_ui.make_visible();
120                 }
121         }
122
123         void finish_input(String line) {
124                 if (line == null) {
125                         abort();
126                         return;
127                 }
128                 if (line.equals("all finished")) {
129                         if (serial_line != null)
130                                 update_ui();
131                         return;
132                 }
133         }
134
135         synchronized void process_line(String line) {
136                 if (line == null || line.equals("all finished")) {
137                         final String last_line = line;
138                         Runnable r = new Runnable() {
139                                         public void run() {
140                                                 finish_input(last_line);
141                                         }
142                                 };
143                         SwingUtilities.invokeLater(r);
144                 } else {
145                         get_string(line, "Config version", config_version);
146                         get_int(line, "serial-number", serial);
147                         get_int(line, "Radio channel:", radio_channel);
148                         get_int(line, "Radio cal:", radio_calibration);
149                         get_int(line, "Frequency:", radio_frequency);
150                         get_int(line, "Radio setting:", radio_setting);
151                         get_string(line,"software-version", version);
152                         get_string(line,"product", product);
153                 }
154         }
155
156         synchronized void reset_data() {
157                 serial.set(0);
158                 radio_channel.set(0);
159                 radio_setting.set(0);
160                 radio_frequency.set(0);
161                 radio_calibration.set(1186611);
162                 config_version.set("0.0");
163                 version.set("unknown");
164                 product.set("unknown");
165         }
166
167         synchronized double frequency() {
168                 return AltosConvert.radio_to_frequency(radio_frequency.get(),
169                                                        radio_setting.get(),
170                                                        radio_calibration.get(),
171                                                        radio_channel.get());
172         }
173
174         synchronized void set_frequency(double freq) {
175                 int     frequency = radio_frequency.get();
176                 int     setting = radio_setting.get();
177
178                 if (frequency > 0) {
179                         radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
180                 } else if (setting > 0) {
181                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
182                                                                                   radio_calibration.get()));
183                         radio_channel.set(0);
184                 } else {
185                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
186                 }
187         }
188
189         final static int        serial_mode_read = 0;
190         final static int        serial_mode_save = 1;
191         final static int        serial_mode_reboot = 2;
192
193         class SerialData implements Runnable {
194                 AltosConfigTD   config;
195                 int             serial_mode;
196
197                 void get_data() {
198                         try {
199                                 boolean been_there = false;
200                                 config.reset_data();
201
202                                 for (;;) {
203                                         config.serial_line.printf("c s\nf\nv\n");
204                                         for (;;) {
205                                                 try {
206                                                         String line = config.serial_line.get_reply(5000);
207                                                         config.process_line(line);
208                                                         if (line != null && line.startsWith("software-version"))
209                                                                 break;
210                                                 } catch (Exception e) {
211                                                         break;
212                                                 }
213                                         }
214                                         if (been_there)
215                                                 break;
216                                         if (!config_version.get().equals("0.0"))
217                                                 break;
218                                         been_there = true;
219                                         config.serial_line.printf("C\n ");
220                                         config.serial_line.flush_input();
221                                 }
222                         } catch (InterruptedException ie) {
223                         }
224                         /*
225                          * This makes sure the displayed frequency respects the limits that the
226                          * available firmware version might place on the actual frequency
227                          */
228                         config.set_frequency(AltosPreferences.frequency(serial.get()));
229                         config.process_line("all finished");
230                 }
231
232                 void save_data() {
233                         double frequency = frequency();
234                         if (frequency != 0)
235                                 AltosPreferences.set_frequency(serial.get(),
236                                                                frequency);
237                 }
238
239                 public void run () {
240                         switch (serial_mode) {
241                         case serial_mode_save:
242                                 save_data();
243                                 /* fall through ... */
244                         case serial_mode_read:
245                                 get_data();
246                                 break;
247                         }
248                 }
249
250                 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
251                         config = in_config;
252                         serial_mode = in_serial_mode;
253                 }
254         }
255
256         void run_serial_thread(int serial_mode) {
257                 SerialData      sd = new SerialData(this, serial_mode);
258                 Thread          st = new Thread(sd);
259                 st.start();
260         }
261
262         void init_ui () throws InterruptedException, TimeoutException {
263                 config_ui = new AltosConfigTDUI(owner);
264                 config_ui.addActionListener(this);
265                 serial_line.set_frame(owner);
266                 set_ui();
267         }
268
269         void abort() {
270                 serial_line.close();
271                 serial_line = null;
272                 JOptionPane.showMessageDialog(owner,
273                                               String.format("Connection to \"%s\" failed",
274                                                             device.toShortString()),
275                                               "Connection Failed",
276                                               JOptionPane.ERROR_MESSAGE);
277                 config_ui.setVisible(false);
278         }
279
280         void set_ui() throws InterruptedException, TimeoutException {
281                 if (serial_line != null)
282                         run_serial_thread(serial_mode_read);
283                 else
284                         update_ui();
285         }
286
287         void save_data() {
288                 double  freq = config_ui.radio_frequency();
289                 set_frequency(freq);
290                 run_serial_thread(serial_mode_save);
291         }
292
293         public void actionPerformed(ActionEvent e) {
294                 String  cmd = e.getActionCommand();
295                 try {
296                         if (cmd.equals("Save")) {
297                                 save_data();
298                         } else if (cmd.equals("Reset")) {
299                                 set_ui();
300                         } else if (cmd.equals("Reboot")) {
301                                 if (serial_line != null)
302                                         run_serial_thread(serial_mode_reboot);
303                         } else if (cmd.equals("Close")) {
304                                 if (serial_line != null)
305                                         serial_line.close();
306                         }
307                 } catch (InterruptedException ie) {
308                         abort();
309                 } catch (TimeoutException te) {
310                         abort();
311                 }
312         }
313
314         public AltosConfigTD(JFrame given_owner) {
315                 owner = given_owner;
316
317                 serial = new int_ref(0);
318                 radio_channel = new int_ref(0);
319                 radio_setting = new int_ref(0);
320                 radio_frequency = new int_ref(0);
321                 radio_calibration = new int_ref(1186611);
322                 config_version = new string_ref("0.0");
323                 version = new string_ref("unknown");
324                 product = new string_ref("unknown");
325
326                 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
327                 if (device != null) {
328                         try {
329                                 serial_line = new AltosSerial(device);
330                                 try {
331                                         init_ui();
332                                 } catch (InterruptedException ie) {
333                                         abort();
334                                 } catch (TimeoutException te) {
335                                         abort();
336                                 }
337                         } catch (FileNotFoundException ee) {
338                                 JOptionPane.showMessageDialog(owner,
339                                                               ee.getMessage(),
340                                                               "Cannot open target device",
341                                                               JOptionPane.ERROR_MESSAGE);
342                         } catch (AltosSerialInUseException si) {
343                                 JOptionPane.showMessageDialog(owner,
344                                                               String.format("Device \"%s\" already in use",
345                                                                             device.toShortString()),
346                                                               "Device in use",
347                                                               JOptionPane.ERROR_MESSAGE);
348                         } catch (IOException ee) {
349                                 JOptionPane.showMessageDialog(owner,
350                                                               device.toShortString(),
351                                                               ee.getLocalizedMessage(),
352                                                               JOptionPane.ERROR_MESSAGE);
353                         }
354                 }
355         }
356 }