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;
33 * This class reads from the serial port and places each received
34 * line in a queue. Dealing with that queue is left up to other
38 public class AltosSerial implements Runnable {
40 SWIGTYPE_p_altos_file altos;
41 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
42 LinkedBlockingQueue<AltosLine> reply_queue;
54 c = libaltos.altos_getchar(altos, 0);
55 if (Thread.interrupted())
57 if (c == libaltosConstants.LIBALTOS_ERROR) {
58 for (int e = 0; e < monitors.size(); e++) {
59 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
60 q.put(new AltosLine());
62 reply_queue.put (new AltosLine());
65 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
71 if (line_count != 0) {
73 line = new String(line_bytes, 0, line_count, "UTF-8");
74 } catch (UnsupportedEncodingException ue) {
76 for (int i = 0; i < line_count; i++)
77 line = line + line_bytes[i];
79 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
80 for (int e = 0; e < monitors.size(); e++) {
81 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
82 q.put(new AltosLine (line));
85 // System.out.printf("GOT: %s\n", line);
86 reply_queue.put(new AltosLine (line));
92 if (line_bytes == null) {
93 line_bytes = new byte[256];
94 } else if (line_count == line_bytes.length) {
95 byte[] new_line_bytes = new byte[line_count * 2];
96 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
97 line_bytes = new_line_bytes;
99 line_bytes[line_count] = (byte) c;
104 } catch (InterruptedException e) {
108 public void flush_output() {
110 libaltos.altos_flush(altos);
113 public void flush_input() {
117 } catch (InterruptedException ie) {
120 if (!"VERSION".startsWith(line) &&
121 !line.startsWith("VERSION"))
127 public String get_reply() throws InterruptedException {
129 AltosLine line = reply_queue.take();
133 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
138 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
140 if (monitors.isEmpty())
144 public boolean opened() {
145 return altos != null;
148 public void close() {
150 libaltos.altos_close(altos);
152 if (input_thread != null) {
154 input_thread.interrupt();
156 } catch (InterruptedException e) {
161 libaltos.altos_free(altos);
166 public void putc(char c) {
168 libaltos.altos_putchar(altos, c);
171 public void print(String data) {
172 // System.out.printf("\"%s\" ", data);
173 for (int i = 0; i < data.length(); i++)
174 putc(data.charAt(i));
177 public void printf(String format, Object ... arguments) {
178 print(String.format(format, arguments));
181 public void open(altos_device device) throws FileNotFoundException {
183 altos = libaltos.altos_open(device);
185 throw new FileNotFoundException(device.getPath());
186 input_thread = new Thread(this);
187 input_thread.start();
190 set_monitor(monitor_mode);
191 set_channel(AltosPreferences.channel(device.getSerial()));
192 set_callsign(AltosPreferences.callsign());
195 public void set_channel(int channel) {
198 printf("m 0\nc r %d\nm 1\n", channel);
200 printf("c r %d\n", channel);
205 void set_monitor(boolean monitor) {
206 monitor_mode = monitor;
216 public void set_callsign(String callsign) {
218 printf ("c c %s\n", callsign);
223 public AltosSerial() {
227 monitor_mode = false;
228 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
229 reply_queue = new LinkedBlockingQueue<AltosLine> ();