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.LinkedBlockingQueue;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29 import altosui.AltosSerialMonitor;
30 import altosui.AltosLine;
31 import libaltosJNI.libaltos;
32 import libaltosJNI.altos_device;
33 import libaltosJNI.SWIGTYPE_p_altos_file;
34 import libaltosJNI.SWIGTYPE_p_altos_list;
35 import libaltosJNI.libaltosConstants;
38 * This class reads from the serial port and places each received
39 * line in a queue. Dealing with that queue is left up to other
43 public class AltosSerial implements Runnable {
45 SWIGTYPE_p_altos_file altos;
46 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
47 LinkedBlockingQueue<AltosLine> reply_queue;
59 c = libaltos.altos_getchar(altos, 0);
60 if (Thread.interrupted())
62 if (c == libaltosConstants.LIBALTOS_ERROR) {
63 for (int e = 0; e < monitors.size(); e++) {
64 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
65 q.put(new AltosLine());
67 reply_queue.put (new AltosLine());
70 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
76 if (line_count != 0) {
78 line = new String(line_bytes, 0, line_count, "UTF-8");
79 } catch (UnsupportedEncodingException ue) {
81 for (int i = 0; i < line_count; i++)
82 line = line + line_bytes[i];
84 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
85 for (int e = 0; e < monitors.size(); e++) {
86 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
87 q.put(new AltosLine (line));
90 // System.out.printf("GOT: %s\n", line);
91 reply_queue.put(new AltosLine (line));
97 if (line_bytes == null) {
98 line_bytes = new byte[256];
99 } else if (line_count == line_bytes.length) {
100 byte[] new_line_bytes = new byte[line_count * 2];
101 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
102 line_bytes = new_line_bytes;
104 line_bytes[line_count] = (byte) c;
109 } catch (InterruptedException e) {
113 public void flush_output() {
115 libaltos.altos_flush(altos);
118 public void flush_input() {
122 } catch (InterruptedException ie) {
125 if (!"VERSION".startsWith(line) &&
126 !line.startsWith("VERSION"))
132 public String get_reply() throws InterruptedException {
134 AltosLine line = reply_queue.take();
138 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
143 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
145 if (monitors.isEmpty())
149 public boolean opened() {
150 return altos != null;
153 public void close() {
155 libaltos.altos_close(altos);
157 if (input_thread != null) {
159 input_thread.interrupt();
161 } catch (InterruptedException e) {
166 libaltos.altos_free(altos);
171 public void putc(char c) {
173 libaltos.altos_putchar(altos, c);
176 public void print(String data) {
177 // System.out.printf("\"%s\" ", data);
178 for (int i = 0; i < data.length(); i++)
179 putc(data.charAt(i));
182 public void printf(String format, Object ... arguments) {
183 print(String.format(format, arguments));
186 public void open(altos_device device) throws FileNotFoundException {
188 altos = libaltos.altos_open(device);
190 throw new FileNotFoundException(device.getPath());
191 input_thread = new Thread(this);
192 input_thread.start();
195 set_monitor(monitor_mode);
198 public void set_channel(int channel) {
201 printf("m 0\nc r %d\nm 1\n", channel);
203 printf("c r %d\n", channel);
208 void set_monitor(boolean monitor) {
209 monitor_mode = monitor;
219 public void set_callsign(String callsign) {
221 printf ("c c %s\n", callsign);
226 public AltosSerial() {
230 monitor_mode = false;
231 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
232 reply_queue = new LinkedBlockingQueue<AltosLine> ();