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.*;
29 import java.awt.event.*;
31 import javax.swing.filechooser.FileNameExtensionFilter;
32 import javax.swing.table.*;
37 * This class reads from the serial port and places each received
38 * line in a queue. Dealing with that queue is left up to other
42 public class AltosSerial implements Runnable {
44 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
47 SWIGTYPE_p_altos_file altos;
48 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
49 LinkedBlockingQueue<AltosLine> reply_queue;
59 LinkedList<String> pending_output = new LinkedList<String>();
62 static void set_debug(boolean new_debug) {
71 c = libaltos.altos_getchar(altos, 0);
72 if (Thread.interrupted())
74 if (c == libaltosConstants.LIBALTOS_ERROR) {
75 for (int e = 0; e < monitors.size(); e++) {
76 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
77 q.put(new AltosLine());
79 reply_queue.put (new AltosLine());
82 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
88 if (line_count != 0) {
90 line = new String(line_bytes, 0, line_count, "UTF-8");
91 } catch (UnsupportedEncodingException ue) {
93 for (int i = 0; i < line_count; i++)
94 line = line + line_bytes[i];
97 System.out.printf("\t\t\t\t\t%s\n", line);
98 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
99 for (int e = 0; e < monitors.size(); e++) {
100 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
101 q.put(new AltosLine (line));
104 reply_queue.put(new AltosLine (line));
110 if (line_bytes == null) {
111 line_bytes = new byte[256];
112 } else if (line_count == line_bytes.length) {
113 byte[] new_line_bytes = new byte[line_count * 2];
114 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
115 line_bytes = new_line_bytes;
117 line_bytes[line_count] = (byte) c;
122 } catch (InterruptedException e) {
126 public void flush_output() {
128 for (String s : pending_output)
130 pending_output.clear();
131 libaltos.altos_flush(altos);
136 JDialog timeout_dialog;
137 boolean timeout_started = false;
139 private void stop_timeout_dialog() {
140 Runnable r = new Runnable() {
142 if (timeout_dialog != null)
143 timeout_dialog.setVisible(false);
146 SwingUtilities.invokeLater(r);
149 private void start_timeout_dialog_internal() {
150 Object[] options = { "Cancel" };
152 JOptionPane pane = new JOptionPane();
153 pane.setMessage(String.format("Connecting to %s", device.getPath()));
154 pane.setOptions(options);
155 pane.setInitialValue(null);
157 timeout_dialog = pane.createDialog(frame, "Connecting...");
159 timeout_dialog.setVisible(true);
161 Object o = pane.getValue();
164 if (options[0].equals(o))
168 private boolean check_timeout() {
169 if (!timeout_started && frame != null) {
170 timeout_started = true;
171 if (!SwingUtilities.isEventDispatchThread()) {
172 Runnable r = new Runnable() {
174 start_timeout_dialog_internal();
177 SwingUtilities.invokeLater(r);
183 public void flush_input() {
192 Thread.sleep(timeout);
193 } catch (InterruptedException ie) {
195 got_some = !reply_queue.isEmpty();
197 if (!"VERSION".startsWith(line) &&
198 !line.startsWith("VERSION"))
205 public String get_reply() throws InterruptedException {
206 if (SwingUtilities.isEventDispatchThread())
207 System.out.printf("Uh-oh, reading serial device from swing thread\n");
209 AltosLine line = reply_queue.take();
213 public String get_reply(int timeout) throws InterruptedException {
214 boolean can_cancel = true;
215 if (SwingUtilities.isEventDispatchThread()) {
217 System.out.printf("Uh-oh, reading serial device from swing thread\n");
220 if (remote && can_cancel) {
224 timeout_started = false;
226 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
228 stop_timeout_dialog();
231 if (!remote || !can_cancel || check_timeout())
236 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
241 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
243 if (monitors.isEmpty())
247 public void close() {
249 libaltos.altos_close(altos);
251 if (input_thread != null) {
253 input_thread.interrupt();
255 } catch (InterruptedException e) {
260 libaltos.altos_free(altos);
263 synchronized (devices_opened) {
264 devices_opened.remove(device.getPath());
267 System.out.printf("Closing %s\n", device.getPath());
270 private void putc(char c) {
272 libaltos.altos_putchar(altos, c);
275 public void print(String data) {
277 pending_output.add(data);
278 for (int i = 0; i < data.length(); i++)
279 putc(data.charAt(i));
282 public void printf(String format, Object ... arguments) {
283 print(String.format(format, arguments));
286 private void open() throws FileNotFoundException, AltosSerialInUseException {
287 synchronized (devices_opened) {
288 if (devices_opened.contains(device.getPath()))
289 throw new AltosSerialInUseException(device);
290 devices_opened.add(device.getPath());
292 altos = libaltos.altos_open(device);
295 throw new FileNotFoundException(device.toShortString());
298 System.out.printf("Open %s\n", device.getPath());
299 input_thread = new Thread(this);
300 input_thread.start();
306 public void set_radio() {
307 telemetry = AltosPreferences.telemetry(device.getSerial());
308 channel = AltosPreferences.channel(device.getSerial());
309 set_channel(channel);
310 set_callsign(AltosPreferences.callsign());
313 public void set_channel(int in_channel) {
314 channel = in_channel;
317 printf("m 0\nc r %d\nm %d\n", channel, telemetry);
319 printf("c r %d\n", channel);
324 public void set_telemetry(int in_telemetry) {
325 telemetry = in_telemetry;
328 printf("m 0\nm %d\n", telemetry);
333 void set_monitor(boolean monitor) {
334 monitor_mode = monitor;
337 printf("m %d\n", telemetry);
344 public void set_callsign(String callsign) {
346 printf ("c c %s\n", callsign);
351 public void start_remote() {
353 System.out.printf("start remote\n");
360 public void stop_remote() {
362 System.out.printf("stop remote\n");
372 public void set_frame(Frame in_frame) {
376 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
379 monitor_mode = false;
381 telemetry = Altos.ao_telemetry_full;
382 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
383 reply_queue = new LinkedBlockingQueue<AltosLine> ();