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;
51 LinkedList<String> pending_output = new LinkedList<String>();
53 static void set_debug(boolean new_debug) {
62 c = libaltos.altos_getchar(altos, 0);
63 if (Thread.interrupted())
65 if (c == libaltosConstants.LIBALTOS_ERROR) {
66 for (int e = 0; e < monitors.size(); e++) {
67 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
68 q.put(new AltosLine());
70 reply_queue.put (new AltosLine());
73 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
79 if (line_count != 0) {
81 line = new String(line_bytes, 0, line_count, "UTF-8");
82 } catch (UnsupportedEncodingException ue) {
84 for (int i = 0; i < line_count; i++)
85 line = line + line_bytes[i];
88 System.out.printf("\t\t\t\t\t%s\n", line);
89 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
90 for (int e = 0; e < monitors.size(); e++) {
91 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
92 q.put(new AltosLine (line));
95 reply_queue.put(new AltosLine (line));
101 if (line_bytes == null) {
102 line_bytes = new byte[256];
103 } else if (line_count == line_bytes.length) {
104 byte[] new_line_bytes = new byte[line_count * 2];
105 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
106 line_bytes = new_line_bytes;
108 line_bytes[line_count] = (byte) c;
113 } catch (InterruptedException e) {
117 public void flush_output() {
119 for (String s : pending_output)
121 pending_output.clear();
122 libaltos.altos_flush(altos);
126 public void flush_input() {
132 } catch (InterruptedException ie) {
134 got_some = !reply_queue.isEmpty();
136 if (!"VERSION".startsWith(line) &&
137 !line.startsWith("VERSION"))
144 public String get_reply() throws InterruptedException {
146 AltosLine line = reply_queue.take();
150 public String get_reply(int timeout) throws InterruptedException {
152 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
158 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
163 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
165 if (monitors.isEmpty())
169 public void close() {
171 libaltos.altos_close(altos);
173 if (input_thread != null) {
175 input_thread.interrupt();
177 } catch (InterruptedException e) {
182 libaltos.altos_free(altos);
185 synchronized (devices_opened) {
186 devices_opened.remove(device.getPath());
189 System.out.printf("Closing %s\n", device.getPath());
192 private void putc(char c) {
194 libaltos.altos_putchar(altos, c);
197 public void print(String data) {
199 pending_output.add(data);
200 for (int i = 0; i < data.length(); i++)
201 putc(data.charAt(i));
204 public void printf(String format, Object ... arguments) {
205 print(String.format(format, arguments));
208 private void open() throws FileNotFoundException, AltosSerialInUseException {
209 synchronized (devices_opened) {
210 if (devices_opened.contains(device.getPath()))
211 throw new AltosSerialInUseException(device);
212 devices_opened.add(device.getPath());
214 altos = libaltos.altos_open(device);
217 throw new FileNotFoundException(device.toShortString());
220 System.out.printf("Open %s\n", device.getPath());
221 input_thread = new Thread(this);
222 input_thread.start();
228 public void set_radio() {
229 set_channel(AltosPreferences.channel(device.getSerial()));
230 set_callsign(AltosPreferences.callsign());
233 public void set_channel(int channel) {
236 printf("m 0\nc r %d\nm 1\n", channel);
238 printf("c r %d\n", channel);
243 void set_monitor(boolean monitor) {
244 monitor_mode = monitor;
254 public void set_callsign(String callsign) {
256 printf ("c c %s\n", callsign);
261 public void start_remote() {
267 public void stop_remote() {
272 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
275 monitor_mode = false;
276 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
277 reply_queue = new LinkedBlockingQueue<AltosLine> ();