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;
52 LinkedList<String> pending_output = new LinkedList<String>();
54 static void set_debug(boolean new_debug) {
63 c = libaltos.altos_getchar(altos, 0);
64 if (Thread.interrupted())
66 if (c == libaltosConstants.LIBALTOS_ERROR) {
67 for (int e = 0; e < monitors.size(); e++) {
68 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
69 q.put(new AltosLine());
71 reply_queue.put (new AltosLine());
74 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
80 if (line_count != 0) {
82 line = new String(line_bytes, 0, line_count, "UTF-8");
83 } catch (UnsupportedEncodingException ue) {
85 for (int i = 0; i < line_count; i++)
86 line = line + line_bytes[i];
89 System.out.printf("\t\t\t\t\t%s\n", line);
90 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
91 for (int e = 0; e < monitors.size(); e++) {
92 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
93 q.put(new AltosLine (line));
96 reply_queue.put(new AltosLine (line));
102 if (line_bytes == null) {
103 line_bytes = new byte[256];
104 } else if (line_count == line_bytes.length) {
105 byte[] new_line_bytes = new byte[line_count * 2];
106 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
107 line_bytes = new_line_bytes;
109 line_bytes[line_count] = (byte) c;
114 } catch (InterruptedException e) {
118 public void flush_output() {
120 for (String s : pending_output)
122 pending_output.clear();
123 libaltos.altos_flush(altos);
127 public void flush_input() {
136 Thread.sleep(timeout);
137 } catch (InterruptedException ie) {
139 got_some = !reply_queue.isEmpty();
141 if (!"VERSION".startsWith(line) &&
142 !line.startsWith("VERSION"))
149 public String get_reply() throws InterruptedException {
151 AltosLine line = reply_queue.take();
155 public String get_reply(int timeout) throws InterruptedException {
157 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
163 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
168 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
170 if (monitors.isEmpty())
174 public void close() {
176 libaltos.altos_close(altos);
178 if (input_thread != null) {
180 input_thread.interrupt();
182 } catch (InterruptedException e) {
187 libaltos.altos_free(altos);
190 synchronized (devices_opened) {
191 devices_opened.remove(device.getPath());
194 System.out.printf("Closing %s\n", device.getPath());
197 private void putc(char c) {
199 libaltos.altos_putchar(altos, c);
202 public void print(String data) {
204 pending_output.add(data);
205 for (int i = 0; i < data.length(); i++)
206 putc(data.charAt(i));
209 public void printf(String format, Object ... arguments) {
210 print(String.format(format, arguments));
213 private void open() throws FileNotFoundException, AltosSerialInUseException {
214 synchronized (devices_opened) {
215 if (devices_opened.contains(device.getPath()))
216 throw new AltosSerialInUseException(device);
217 devices_opened.add(device.getPath());
219 altos = libaltos.altos_open(device);
222 throw new FileNotFoundException(device.toShortString());
225 System.out.printf("Open %s\n", device.getPath());
226 input_thread = new Thread(this);
227 input_thread.start();
233 public void set_radio() {
234 set_channel(AltosPreferences.channel(device.getSerial()));
235 set_callsign(AltosPreferences.callsign());
238 public void set_channel(int channel) {
241 printf("m 0\nc r %d\nm 1\n", channel);
243 printf("c r %d\n", channel);
248 void set_monitor(boolean monitor) {
249 monitor_mode = monitor;
259 public void set_callsign(String callsign) {
261 printf ("c c %s\n", callsign);
266 public void start_remote() {
268 System.out.printf("start remote\n");
275 public void stop_remote() {
277 System.out.printf("stop remote\n");
284 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
287 monitor_mode = false;
288 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
289 reply_queue = new LinkedBlockingQueue<AltosLine> ();