altosui: AltosSerial and AltosLink both tried to provide frequency setting
[fw/altos] / altosui / AltosSerial.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 /*
19  * Deal with TeleDongle on a serial port
20  */
21
22 package altosui;
23
24 import java.lang.*;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import java.util.*;
28 import java.text.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.*;
32 import javax.swing.filechooser.FileNameExtensionFilter;
33 import javax.swing.table.*;
34 import org.altusmetrum.AltosLib.*;
35
36 import libaltosJNI.*;
37
38 /*
39  * This class reads from the serial port and places each received
40  * line in a queue. Dealing with that queue is left up to other
41  * threads.
42  */
43
44 public class AltosSerial extends AltosLink implements Runnable {
45
46         static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
47
48         AltosDevice device;
49         SWIGTYPE_p_altos_file altos;
50         Thread input_thread;
51         String line;
52         byte[] line_bytes;
53         int line_count;
54         Frame frame;
55
56         public void run () {
57                 int c;
58                 byte[] line_bytes = null;
59                 int line_count = 0;
60
61                 try {
62                         for (;;) {
63                                 c = libaltos.altos_getchar(altos, 0);
64                                 if (Thread.interrupted())
65                                         break;
66                                 if (c == libaltosConstants.LIBALTOS_ERROR) {
67                                         add_telem (new AltosLine());
68                                         add_reply (new AltosLine());
69                                         break;
70                                 }
71                                 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
72                                         continue;
73                                 if (c == '\r')
74                                         continue;
75                                 synchronized(this) {
76                                         if (c == '\n') {
77                                                 if (line_count != 0) {
78                                                         add_bytes(line_bytes, line_count);
79                                                         line_count = 0;
80                                                 }
81                                         } else {
82                                                 if (line_bytes == null) {
83                                                         line_bytes = new byte[256];
84                                                 } else if (line_count == line_bytes.length) {
85                                                         byte[] new_line_bytes = new byte[line_count * 2];
86                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
87                                                         line_bytes = new_line_bytes;
88                                                 }
89                                                 line_bytes[line_count] = (byte) c;
90                                                 line_count++;
91                                         }
92                                 }
93                         }
94                 } catch (InterruptedException e) {
95                 }
96         }
97
98         public void flush_output() {
99                 super.flush_output();
100                 if (altos != null) {
101                         libaltos.altos_flush(altos);
102                 }
103         }
104
105         boolean         abort;
106         JDialog         timeout_dialog;
107         boolean timeout_started = false;
108
109         private void stop_timeout_dialog() {
110                 if (timeout_started) {
111                         timeout_started = false;
112                         Runnable r = new Runnable() {
113                                         public void run() {
114                                                 timeout_dialog.setVisible(false);
115                                         }
116                                 };
117                         SwingUtilities.invokeLater(r);
118                 }
119         }
120
121         private void start_timeout_dialog_internal() {
122
123                 Object[] options = { "Cancel" };
124
125                 JOptionPane     pane = new JOptionPane();
126                 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
127                 pane.setOptions(options);
128                 pane.setInitialValue(null);
129
130                 timeout_dialog = pane.createDialog(frame, "Connecting...");
131
132                 timeout_dialog.setVisible(true);
133
134                 Object o = pane.getValue();
135                 if (o == null)
136                         return;
137                 if (options[0].equals(o))
138                         abort = true;
139                 timeout_dialog.dispose();
140                 timeout_dialog = null;
141         }
142
143         private boolean check_timeout() {
144                 if (!timeout_started && frame != null) {
145                         if (!SwingUtilities.isEventDispatchThread()) {
146                                 timeout_started = true;
147                                 Runnable r = new Runnable() {
148                                                 public void run() {
149                                                         start_timeout_dialog_internal();
150                                                 }
151                                         };
152                                 SwingUtilities.invokeLater(r);
153                         }
154                 }
155                 return abort;
156         }
157
158         public void flush_input() throws InterruptedException {
159                 if (remote)
160                         flush_input(500);
161                 else
162                         flush_input(100);
163         }
164
165         int     in_reply;
166
167         public String get_reply(int timeout) throws InterruptedException {
168                 boolean can_cancel = true;
169                 String  reply = null;
170
171                 try {
172                         ++in_reply;
173
174                         if (SwingUtilities.isEventDispatchThread()) {
175                                 can_cancel = false;
176                                 if (remote)
177                                         System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
178                         }
179                         flush_output();
180                         if (remote && can_cancel) {
181                                 timeout = 500;
182                         }
183                         abort = false;
184                         timeout_started = false;
185                         for (;;) {
186                                 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
187                                 if (line != null) {
188                                         stop_timeout_dialog();
189                                         reply = line.line;
190                                         break;
191                                 }
192                                 if (!remote || !can_cancel || check_timeout()) {
193                                         reply = null;
194                                         break;
195                                 }
196                         }
197                 } finally {
198                         --in_reply;
199                 }
200                 return reply;
201         }
202
203         public void close() {
204                 if (remote) {
205                         try {
206                                 stop_remote();
207                         } catch (InterruptedException ie) {
208                         }
209                 }
210                 if (in_reply != 0)
211                         System.out.printf("Uh-oh. Closing active serial device\n");
212
213                 if (altos != null) {
214                         libaltos.altos_close(altos);
215                 }
216                 if (input_thread != null) {
217                         try {
218                                 input_thread.interrupt();
219                                 input_thread.join();
220                         } catch (InterruptedException e) {
221                         }
222                         input_thread = null;
223                 }
224                 if (altos != null) {
225                         libaltos.altos_free(altos);
226                         altos = null;
227                 }
228                 synchronized (devices_opened) {
229                         devices_opened.remove(device.getPath());
230                 }
231                 if (debug)
232                         System.out.printf("Closing %s\n", device.getPath());
233         }
234
235         private void putc(char c) {
236                 if (altos != null)
237                         libaltos.altos_putchar(altos, c);
238         }
239
240         public void print(String data) {
241                 for (int i = 0; i < data.length(); i++)
242                         putc(data.charAt(i));
243         }
244
245         private void open() throws FileNotFoundException, AltosSerialInUseException {
246                 synchronized (devices_opened) {
247                         if (devices_opened.contains(device.getPath()))
248                                 throw new AltosSerialInUseException(device);
249                         devices_opened.add(device.getPath());
250                 }
251                 altos = device.open();
252                 if (altos == null) {
253                         final String    message = device.getErrorString();
254                         close();
255                         throw new FileNotFoundException(String.format("%s (%s)",
256                                                                       device.toShortString(), message));
257                 }
258                 if (debug)
259                         System.out.printf("Open %s\n", device.getPath());
260                 input_thread = new Thread(this);
261                 input_thread.start();
262                 print("~\nE 0\n");
263                 set_monitor(false);
264                 flush_output();
265         }
266
267         public void set_frame(Frame in_frame) {
268                 frame = in_frame;
269         }
270
271         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
272                 device = in_device;
273                 frame = null;
274                 serial = device.getSerial();
275                 name = device.toShortString();
276                 open();
277         }
278 }