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.*;
32 * This class reads from the serial port and places each received
33 * line in a queue. Dealing with that queue is left up to other
37 public class AltosSerial implements Runnable {
39 static List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
42 SWIGTYPE_p_altos_file altos;
43 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
44 LinkedBlockingQueue<AltosLine> reply_queue;
56 c = libaltos.altos_getchar(altos, 0);
57 if (Thread.interrupted())
59 if (c == libaltosConstants.LIBALTOS_ERROR) {
60 for (int e = 0; e < monitors.size(); e++) {
61 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
62 q.put(new AltosLine());
64 reply_queue.put (new AltosLine());
67 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
73 if (line_count != 0) {
75 line = new String(line_bytes, 0, line_count, "UTF-8");
76 } catch (UnsupportedEncodingException ue) {
78 for (int i = 0; i < line_count; i++)
79 line = line + line_bytes[i];
81 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
82 for (int e = 0; e < monitors.size(); e++) {
83 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
84 q.put(new AltosLine (line));
87 // System.out.printf("GOT: %s\n", line);
88 reply_queue.put(new AltosLine (line));
94 if (line_bytes == null) {
95 line_bytes = new byte[256];
96 } else if (line_count == line_bytes.length) {
97 byte[] new_line_bytes = new byte[line_count * 2];
98 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
99 line_bytes = new_line_bytes;
101 line_bytes[line_count] = (byte) c;
106 } catch (InterruptedException e) {
110 public void flush_output() {
112 libaltos.altos_flush(altos);
115 public void flush_input() {
121 } catch (InterruptedException ie) {
123 got_some = !reply_queue.isEmpty();
125 if (!"VERSION".startsWith(line) &&
126 !line.startsWith("VERSION"))
133 public String get_reply() throws InterruptedException {
135 AltosLine line = reply_queue.take();
139 public String get_reply(int timeout) throws InterruptedException {
141 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
147 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
152 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
154 if (monitors.isEmpty())
158 public void close() {
160 libaltos.altos_close(altos);
162 if (input_thread != null) {
164 input_thread.interrupt();
166 } catch (InterruptedException e) {
171 libaltos.altos_free(altos);
174 synchronized (devices_opened) {
175 devices_opened.remove(device.getPath());
179 public void putc(char c) {
181 libaltos.altos_putchar(altos, c);
184 public void print(String data) {
185 // System.out.printf("\"%s\" ", data);
186 for (int i = 0; i < data.length(); i++)
187 putc(data.charAt(i));
190 public void printf(String format, Object ... arguments) {
191 print(String.format(format, arguments));
194 private void open() throws FileNotFoundException, AltosSerialInUseException {
195 synchronized (devices_opened) {
196 if (devices_opened.contains(device.getPath()))
197 throw new AltosSerialInUseException(device);
198 devices_opened.add(device.getPath());
200 altos = libaltos.altos_open(device);
203 throw new FileNotFoundException(device.toShortString());
205 input_thread = new Thread(this);
206 input_thread.start();
212 public void set_radio() {
213 set_channel(AltosPreferences.channel(device.getSerial()));
214 set_callsign(AltosPreferences.callsign());
217 public void set_channel(int channel) {
220 printf("m 0\nc r %d\nm 1\n", channel);
222 printf("c r %d\n", channel);
227 void set_monitor(boolean monitor) {
228 monitor_mode = monitor;
238 public void set_callsign(String callsign) {
240 printf ("c c %s\n", callsign);
245 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
248 monitor_mode = false;
249 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
250 reply_queue = new LinkedBlockingQueue<AltosLine> ();