2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 * Deal with TeleDongle on a serial port
26 import java.util.concurrent.*;
30 import java.awt.event.*;
32 import javax.swing.filechooser.FileNameExtensionFilter;
33 import javax.swing.table.*;
38 * This class reads from the serial port and places each received
39 * line in a queue. Dealing with that queue is left up to other
43 public class AltosSerial implements Runnable {
45 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
48 SWIGTYPE_p_altos_file altos;
49 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
50 LinkedBlockingQueue<AltosLine> reply_queue;
60 LinkedList<String> pending_output = new LinkedList<String>();
63 static void set_debug(boolean new_debug) {
72 c = libaltos.altos_getchar(altos, 0);
73 if (Thread.interrupted())
75 if (c == libaltosConstants.LIBALTOS_ERROR) {
76 for (int e = 0; e < monitors.size(); e++) {
77 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
78 q.put(new AltosLine());
80 reply_queue.put (new AltosLine());
83 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
89 if (line_count != 0) {
91 line = new String(line_bytes, 0, line_count, "UTF-8");
92 } catch (UnsupportedEncodingException ue) {
94 for (int i = 0; i < line_count; i++)
95 line = line + line_bytes[i];
98 System.out.printf("\t\t\t\t\t%s\n", line);
99 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
100 for (int e = 0; e < monitors.size(); e++) {
101 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
102 q.put(new AltosLine (line));
105 reply_queue.put(new AltosLine (line));
111 if (line_bytes == null) {
112 line_bytes = new byte[256];
113 } else if (line_count == line_bytes.length) {
114 byte[] new_line_bytes = new byte[line_count * 2];
115 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
116 line_bytes = new_line_bytes;
118 line_bytes[line_count] = (byte) c;
123 } catch (InterruptedException e) {
127 public void flush_output() {
129 for (String s : pending_output)
131 pending_output.clear();
132 libaltos.altos_flush(altos);
137 JDialog timeout_dialog;
138 boolean timeout_started = false;
140 private void stop_timeout_dialog() {
141 if (timeout_started) {
142 timeout_started = false;
143 Runnable r = new Runnable() {
145 timeout_dialog.setVisible(false);
148 SwingUtilities.invokeLater(r);
152 private void start_timeout_dialog_internal() {
154 Object[] options = { "Cancel" };
156 JOptionPane pane = new JOptionPane();
157 pane.setMessage(String.format("Connecting to %s", device.getPath()));
158 pane.setOptions(options);
159 pane.setInitialValue(null);
161 timeout_dialog = pane.createDialog(frame, "Connecting...");
163 timeout_dialog.setVisible(true);
165 Object o = pane.getValue();
168 if (options[0].equals(o))
170 timeout_dialog.dispose();
171 timeout_dialog = null;
174 private boolean check_timeout() {
175 if (!timeout_started && frame != null) {
176 if (!SwingUtilities.isEventDispatchThread()) {
177 timeout_started = true;
178 Runnable r = new Runnable() {
180 start_timeout_dialog_internal();
183 SwingUtilities.invokeLater(r);
189 public void flush_input() {
198 Thread.sleep(timeout);
199 } catch (InterruptedException ie) {
201 got_some = !reply_queue.isEmpty();
203 if (!"VERSION".startsWith(line) &&
204 !line.startsWith("VERSION"))
211 public String get_reply() throws InterruptedException {
212 if (SwingUtilities.isEventDispatchThread())
213 System.out.printf("Uh-oh, reading serial device from swing thread\n");
215 AltosLine line = reply_queue.take();
221 public String get_reply(int timeout) throws InterruptedException {
222 boolean can_cancel = true;
225 if (SwingUtilities.isEventDispatchThread()) {
227 System.out.printf("Uh-oh, reading serial device from swing thread\n");
230 if (remote && can_cancel) {
234 timeout_started = false;
236 System.out.printf("timeout %d\n", timeout);
237 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
239 stop_timeout_dialog();
243 System.out.printf("no line remote %b can_cancel %b\n", remote, can_cancel);
244 if (!remote || !can_cancel || check_timeout()) {
251 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
253 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
259 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
264 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
266 if (monitors.isEmpty())
270 public void close() {
272 System.out.printf("Uh-oh. Closing active serial device\n");
275 libaltos.altos_close(altos);
277 if (input_thread != null) {
279 input_thread.interrupt();
281 } catch (InterruptedException e) {
286 libaltos.altos_free(altos);
289 synchronized (devices_opened) {
290 devices_opened.remove(device.getPath());
293 System.out.printf("Closing %s\n", device.getPath());
296 private void putc(char c) {
298 libaltos.altos_putchar(altos, c);
301 public void print(String data) {
303 pending_output.add(data);
304 for (int i = 0; i < data.length(); i++)
305 putc(data.charAt(i));
308 public void printf(String format, Object ... arguments) {
309 print(String.format(format, arguments));
312 private void open() throws FileNotFoundException, AltosSerialInUseException {
313 synchronized (devices_opened) {
314 if (devices_opened.contains(device.getPath()))
315 throw new AltosSerialInUseException(device);
316 devices_opened.add(device.getPath());
318 altos = device.open();
321 throw new FileNotFoundException(device.toShortString());
324 System.out.printf("Open %s\n", device.getPath());
325 input_thread = new Thread(this);
326 input_thread.start();
332 public void set_radio() {
333 telemetry = AltosPreferences.telemetry(device.getSerial());
334 channel = AltosPreferences.channel(device.getSerial());
335 set_channel(channel);
336 set_callsign(AltosPreferences.callsign());
339 private int telemetry_len() {
340 return Altos.telemetry_len(telemetry);
343 public void set_channel(int in_channel) {
344 channel = in_channel;
347 printf("m 0\nc r %d\nm %x\n",
348 channel, telemetry_len());
350 printf("c r %d\n", channel);
355 public void set_telemetry(int in_telemetry) {
356 telemetry = in_telemetry;
359 printf("m 0\nm %x\n", telemetry_len());
364 void set_monitor(boolean monitor) {
365 monitor_mode = monitor;
368 printf("m %x\n", telemetry_len());
375 public void set_callsign(String callsign) {
377 printf ("c c %s\n", callsign);
382 public void start_remote() {
384 System.out.printf("start remote\n");
391 public void stop_remote() {
393 System.out.printf("stop remote\n");
403 public void set_frame(Frame in_frame) {
407 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
410 monitor_mode = false;
412 telemetry = Altos.ao_telemetry_standard;
413 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
414 reply_queue = new LinkedBlockingQueue<AltosLine> ();