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
28 import org.altusmetrum.altoslib_2.*;
29 import org.altusmetrum.altosuilib_1.*;
34 * This class reads from the serial port and places each received
35 * line in a queue. Dealing with that queue is left up to other
39 public class AltosSerial extends AltosLink {
41 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
44 SWIGTYPE_p_altos_file altos;
51 public int getchar() {
54 return libaltos.altos_getchar(altos, 0);
57 public void flush_output() {
60 if (libaltos.altos_flush(altos) != 0)
65 JDialog timeout_dialog;
67 private void start_timeout_dialog_internal() {
69 Object[] options = { "Cancel" };
71 JOptionPane pane = new JOptionPane();
72 pane.setMessage(String.format("Connecting to %s, %7.3f MHz as %s", device.toShortString(), frequency, callsign));
73 pane.setOptions(options);
74 pane.setInitialValue(null);
76 timeout_dialog = pane.createDialog(frame, "Connecting...");
78 timeout_dialog.setVisible(true);
80 Object o = pane.getValue();
83 if (options[0].equals(o))
85 timeout_dialog.dispose();
86 timeout_dialog = null;
90 * These are required by the AltosLink implementation
93 public boolean can_cancel_reply() {
95 * Can cancel any replies not called from the dispatch thread
97 return !SwingUtilities.isEventDispatchThread();
100 public boolean show_reply_timeout() {
101 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
102 Runnable r = new Runnable() {
104 start_timeout_dialog_internal();
107 SwingUtilities.invokeLater(r);
113 public void hide_reply_timeout() {
114 Runnable r = new Runnable() {
116 timeout_dialog.setVisible(false);
119 SwingUtilities.invokeLater(r);
122 private void close_serial() {
123 synchronized (devices_opened) {
124 devices_opened.remove(device.getPath());
127 libaltos.altos_free(altos);
133 public void close() {
137 } catch (InterruptedException ie) {
141 System.out.printf("Uh-oh. Closing active serial device\n");
145 if (input_thread != null) {
147 input_thread.interrupt();
149 } catch (InterruptedException ie) {
154 System.out.printf("Closing %s\n", device.getPath());
157 private void putc(char c) {
159 if (libaltos.altos_putchar(altos, c) != 0)
163 public void putchar(byte c) {
166 System.out.printf(" %02x", (int) c & 0xff);
167 if (libaltos.altos_putchar(altos, (char) c) != 0)
172 public void print(String data) {
173 for (int i = 0; i < data.length(); i++)
174 putc(data.charAt(i));
177 private void open() throws FileNotFoundException, AltosSerialInUseException {
178 synchronized (devices_opened) {
179 if (devices_opened.contains(device.getPath()))
180 throw new AltosSerialInUseException(device);
181 devices_opened.add(device.getPath());
183 altos = device.open();
185 final String message = device.getErrorString();
187 throw new FileNotFoundException(String.format("%s (%s)",
188 device.toShortString(), message));
191 System.out.printf("Open %s\n", device.getPath());
192 input_thread = new Thread(this);
193 input_thread.start();
199 public void set_frame(Frame in_frame) {
203 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
206 serial = device.getSerial();
207 name = device.toShortString();