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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 * Deal with TeleDongle on a serial port
23 package org.altusmetrum.altosuilib_14;
29 import org.altusmetrum.altoslib_14.*;
33 * This class reads from the serial port and places each received
34 * line in a queue. Dealing with that queue is left up to other
38 public class AltosSerial extends AltosLink {
40 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
42 public AltosDevice device;
43 SWIGTYPE_p_altos_file altos;
50 public int getchar() {
53 return libaltos.altos_getchar(altos, 0);
56 public void flush_output() {
59 if (libaltos.altos_flush(altos) != 0)
64 JDialog timeout_dialog;
66 private void start_timeout_dialog_internal() {
68 Object[] options = { "Cancel" };
70 JOptionPane pane = new JOptionPane();
71 pane.setMessage(String.format("Connecting to %s, %7.3f MHz as %s", device.toShortString(), frequency, callsign));
72 pane.setOptions(options);
73 pane.setInitialValue(null);
75 timeout_dialog = pane.createDialog(frame, "Connecting...");
77 timeout_dialog.setVisible(true);
79 Object o = pane.getValue();
82 if (options[0].equals(o))
84 timeout_dialog.dispose();
85 timeout_dialog = null;
89 * These are required by the AltosLink implementation
92 public boolean can_cancel_reply() {
94 * Can cancel any replies not called from the dispatch thread
96 return !SwingUtilities.isEventDispatchThread();
99 public boolean show_reply_timeout() {
100 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
101 Runnable r = new Runnable() {
103 start_timeout_dialog_internal();
106 SwingUtilities.invokeLater(r);
112 public void hide_reply_timeout() {
113 Runnable r = new Runnable() {
115 timeout_dialog.setVisible(false);
118 SwingUtilities.invokeLater(r);
121 private synchronized void close_serial() {
122 synchronized (devices_opened) {
123 devices_opened.remove(device.getPath());
126 libaltos.altos_free(altos);
132 public void close() {
136 } catch (InterruptedException ie) {
140 System.out.printf("Uh-oh. Closing active serial device\n");
144 if (input_thread != null) {
146 input_thread.interrupt();
148 } catch (InterruptedException ie) {
153 System.out.printf("Closing %s\n", device.getPath());
156 private void putc(char c) {
158 if (libaltos.altos_putchar(altos, c) != 0)
162 public void putchar(byte c) {
165 System.out.printf(" %02x", (int) c & 0xff);
166 if (libaltos.altos_putchar(altos, (char) c) != 0)
171 public void print(String data) {
172 for (int i = 0; i < data.length(); i++)
173 putc(data.charAt(i));
176 private void open() throws FileNotFoundException, AltosSerialInUseException {
177 synchronized (devices_opened) {
178 if (devices_opened.contains(device.getPath()))
179 throw new AltosSerialInUseException(device);
180 devices_opened.add(device.getPath());
182 altos = device.open();
184 final String message = device.getErrorString();
186 throw new FileNotFoundException(String.format("%s (%s)",
187 device.toShortString(), message));
190 System.out.printf("Open %s\n", device.getPath());
191 input_thread = new Thread(this);
192 input_thread.start();
198 public void set_frame(Frame in_frame) {
202 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
205 serial = device.getSerial();
206 name = device.toShortString();