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
22 package org.altusmetrum.altosuilib_8;
28 import org.altusmetrum.altoslib_8.*;
32 * This class reads from the serial port and places each received
33 * line in a queue. Dealing with that queue is left up to other
37 public class AltosSerial extends AltosLink {
39 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
41 public AltosDevice device;
42 SWIGTYPE_p_altos_file altos;
49 public int getchar() {
52 return libaltos.altos_getchar(altos, 0);
55 public void flush_output() {
58 if (libaltos.altos_flush(altos) != 0)
63 JDialog timeout_dialog;
65 private void start_timeout_dialog_internal() {
67 Object[] options = { "Cancel" };
69 JOptionPane pane = new JOptionPane();
70 pane.setMessage(String.format("Connecting to %s, %7.3f MHz as %s", device.toShortString(), frequency, callsign));
71 pane.setOptions(options);
72 pane.setInitialValue(null);
74 timeout_dialog = pane.createDialog(frame, "Connecting...");
76 timeout_dialog.setVisible(true);
78 Object o = pane.getValue();
81 if (options[0].equals(o))
83 timeout_dialog.dispose();
84 timeout_dialog = null;
88 * These are required by the AltosLink implementation
91 public boolean can_cancel_reply() {
93 * Can cancel any replies not called from the dispatch thread
95 return !SwingUtilities.isEventDispatchThread();
98 public boolean show_reply_timeout() {
99 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
100 Runnable r = new Runnable() {
102 start_timeout_dialog_internal();
105 SwingUtilities.invokeLater(r);
111 public void hide_reply_timeout() {
112 Runnable r = new Runnable() {
114 timeout_dialog.setVisible(false);
117 SwingUtilities.invokeLater(r);
120 private synchronized void close_serial() {
121 synchronized (devices_opened) {
122 devices_opened.remove(device.getPath());
125 libaltos.altos_free(altos);
131 public void close() {
135 } catch (InterruptedException ie) {
139 System.out.printf("Uh-oh. Closing active serial device\n");
143 if (input_thread != null) {
145 input_thread.interrupt();
147 } catch (InterruptedException ie) {
152 System.out.printf("Closing %s\n", device.getPath());
155 private void putc(char c) {
157 if (libaltos.altos_putchar(altos, c) != 0)
161 public void putchar(byte c) {
164 System.out.printf(" %02x", (int) c & 0xff);
165 if (libaltos.altos_putchar(altos, (char) c) != 0)
170 public void print(String data) {
171 for (int i = 0; i < data.length(); i++)
172 putc(data.charAt(i));
175 private void open() throws FileNotFoundException, AltosSerialInUseException {
176 synchronized (devices_opened) {
177 if (devices_opened.contains(device.getPath()))
178 throw new AltosSerialInUseException(device);
179 devices_opened.add(device.getPath());
181 altos = device.open();
183 final String message = device.getErrorString();
185 throw new FileNotFoundException(String.format("%s (%s)",
186 device.toShortString(), message));
189 System.out.printf("Open %s\n", device.getPath());
190 input_thread = new Thread(this);
191 input_thread.start();
197 public void set_frame(Frame in_frame) {
201 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
204 serial = device.getSerial();
205 name = device.toShortString();