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 libaltosJNI.libaltos;
31 import libaltosJNI.altos_device;
32 import libaltosJNI.SWIGTYPE_p_altos_file;
33 import libaltosJNI.SWIGTYPE_p_altos_list;
36 * This class reads from the serial port and places each received
37 * line in a queue. Dealing with that queue is left up to other
41 public class AltosSerial implements Runnable {
43 SWIGTYPE_p_altos_file altos;
44 LinkedList<LinkedBlockingQueue<String>> monitors;
45 LinkedBlockingQueue<String> reply_queue;
57 c = libaltos.altos_getchar(altos, 0);
58 if (Thread.interrupted())
66 if (line_count != 0) {
68 line = new String(line_bytes, 0, line_count, "UTF-8");
69 } catch (UnsupportedEncodingException ue) {
71 for (int i = 0; i < line_count; i++)
72 line = line + line_bytes[i];
74 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
75 for (int e = 0; e < monitors.size(); e++) {
76 LinkedBlockingQueue<String> q = monitors.get(e);
80 // System.out.printf("GOT: %s\n", line);
81 reply_queue.put(line);
87 if (line_bytes == null) {
88 line_bytes = new byte[256];
89 } else if (line_count == line_bytes.length) {
90 byte[] new_line_bytes = new byte[line_count * 2];
91 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
92 line_bytes = new_line_bytes;
94 line_bytes[line_count] = (byte) c;
99 } catch (InterruptedException e) {
103 public void flush_output() {
104 libaltos.altos_flush(altos);
107 public void flush_input() {
111 } catch (InterruptedException ie) {
114 if (!"VERSION".startsWith(line) &&
115 !line.startsWith("VERSION"))
121 public String get_reply() throws InterruptedException {
123 String line = reply_queue.take();
127 public void add_monitor(LinkedBlockingQueue<String> q) {
132 public void remove_monitor(LinkedBlockingQueue<String> q) {
134 if (monitors.isEmpty())
138 public boolean opened() {
139 return altos != null;
142 public void close() {
144 libaltos.altos_close(altos);
146 if (input_thread != null) {
148 input_thread.interrupt();
150 } catch (InterruptedException e) {
155 libaltos.altos_free(altos);
160 public void putc(char c) {
162 libaltos.altos_putchar(altos, c);
165 public void print(String data) {
166 // System.out.printf("\"%s\" ", data);
167 for (int i = 0; i < data.length(); i++)
168 putc(data.charAt(i));
171 public void printf(String format, Object ... arguments) {
172 print(String.format(format, arguments));
175 public void open(altos_device device) throws FileNotFoundException {
177 altos = libaltos.altos_open(device);
179 throw new FileNotFoundException(device.getPath());
180 input_thread = new Thread(this);
181 input_thread.start();
183 set_monitor(monitor_mode);
187 public void set_channel(int channel) {
193 printf("c r %d\n", channel);
200 void set_monitor(boolean monitor) {
201 monitor_mode = monitor;
212 public void set_callsign(String callsign) {
214 printf ("c c %s\n", callsign);
217 public AltosSerial() {
221 monitor_mode = false;
222 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
223 reply_queue = new LinkedBlockingQueue<String> ();