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) {
61 libaltos.altos_close(altos);
68 JDialog timeout_dialog;
70 private void start_timeout_dialog_internal() {
72 Object[] options = { "Cancel" };
74 JOptionPane pane = new JOptionPane();
75 pane.setMessage(String.format("Connecting to %s, %7.3f MHz as %s", device.toShortString(), frequency, callsign));
76 pane.setOptions(options);
77 pane.setInitialValue(null);
79 timeout_dialog = pane.createDialog(frame, "Connecting...");
81 timeout_dialog.setVisible(true);
83 Object o = pane.getValue();
86 if (options[0].equals(o))
88 timeout_dialog.dispose();
89 timeout_dialog = null;
93 * These are required by the AltosLink implementation
96 public boolean can_cancel_reply() {
98 * Can cancel any replies not called from the dispatch thread
100 return !SwingUtilities.isEventDispatchThread();
103 public boolean show_reply_timeout() {
104 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
105 Runnable r = new Runnable() {
107 start_timeout_dialog_internal();
110 SwingUtilities.invokeLater(r);
116 public void hide_reply_timeout() {
117 Runnable r = new Runnable() {
119 timeout_dialog.setVisible(false);
122 SwingUtilities.invokeLater(r);
125 public void close() {
129 } catch (InterruptedException ie) {
133 System.out.printf("Uh-oh. Closing active serial device\n");
136 libaltos.altos_close(altos);
138 if (input_thread != null) {
140 input_thread.interrupt();
142 } catch (InterruptedException e) {
147 libaltos.altos_free(altos);
150 synchronized (devices_opened) {
151 devices_opened.remove(device.getPath());
154 System.out.printf("Closing %s\n", device.getPath());
157 private void putc(char c) {
159 if (libaltos.altos_putchar(altos, c) != 0) {
160 libaltos.altos_close(altos);
166 public void print(String data) {
167 for (int i = 0; i < data.length(); i++)
168 putc(data.charAt(i));
171 private void open() throws FileNotFoundException, AltosSerialInUseException {
172 synchronized (devices_opened) {
173 if (devices_opened.contains(device.getPath()))
174 throw new AltosSerialInUseException(device);
175 devices_opened.add(device.getPath());
177 altos = device.open();
179 final String message = device.getErrorString();
181 throw new FileNotFoundException(String.format("%s (%s)",
182 device.toShortString(), message));
185 System.out.printf("Open %s\n", device.getPath());
186 input_thread = new Thread(this);
187 input_thread.start();
193 public void set_frame(Frame in_frame) {
197 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
200 serial = device.getSerial();
201 name = device.toShortString();