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;
54 LinkedList<String> pending_output = new LinkedList<String>();
56 static void set_debug(boolean new_debug) {
65 c = libaltos.altos_getchar(altos, 0);
66 if (Thread.interrupted())
68 if (c == libaltosConstants.LIBALTOS_ERROR) {
69 for (int e = 0; e < monitors.size(); e++) {
70 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
71 q.put(new AltosLine());
73 reply_queue.put (new AltosLine());
76 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
82 if (line_count != 0) {
84 line = new String(line_bytes, 0, line_count, "UTF-8");
85 } catch (UnsupportedEncodingException ue) {
87 for (int i = 0; i < line_count; i++)
88 line = line + line_bytes[i];
91 System.out.printf("\t\t\t\t\t%s\n", line);
92 if (line.startsWith("VERSION") || line.startsWith("CRC")) {
93 for (int e = 0; e < monitors.size(); e++) {
94 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
95 q.put(new AltosLine (line));
98 reply_queue.put(new AltosLine (line));
104 if (line_bytes == null) {
105 line_bytes = new byte[256];
106 } else if (line_count == line_bytes.length) {
107 byte[] new_line_bytes = new byte[line_count * 2];
108 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
109 line_bytes = new_line_bytes;
111 line_bytes[line_count] = (byte) c;
116 } catch (InterruptedException e) {
120 public void flush_output() {
122 for (String s : pending_output)
124 pending_output.clear();
125 libaltos.altos_flush(altos);
129 public void flush_input() {
138 Thread.sleep(timeout);
139 } catch (InterruptedException ie) {
141 got_some = !reply_queue.isEmpty();
143 if (!"VERSION".startsWith(line) &&
144 !line.startsWith("VERSION"))
151 public String get_reply() throws InterruptedException {
153 AltosLine line = reply_queue.take();
157 public String get_reply(int timeout) throws InterruptedException {
159 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
165 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
170 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
172 if (monitors.isEmpty())
176 public void close() {
178 libaltos.altos_close(altos);
180 if (input_thread != null) {
182 input_thread.interrupt();
184 } catch (InterruptedException e) {
189 libaltos.altos_free(altos);
192 synchronized (devices_opened) {
193 devices_opened.remove(device.getPath());
196 System.out.printf("Closing %s\n", device.getPath());
199 private void putc(char c) {
201 libaltos.altos_putchar(altos, c);
204 public void print(String data) {
206 pending_output.add(data);
207 for (int i = 0; i < data.length(); i++)
208 putc(data.charAt(i));
211 public void printf(String format, Object ... arguments) {
212 print(String.format(format, arguments));
215 private void open() throws FileNotFoundException, AltosSerialInUseException {
216 synchronized (devices_opened) {
217 if (devices_opened.contains(device.getPath()))
218 throw new AltosSerialInUseException(device);
219 devices_opened.add(device.getPath());
221 altos = libaltos.altos_open(device);
224 throw new FileNotFoundException(device.toShortString());
227 System.out.printf("Open %s\n", device.getPath());
228 input_thread = new Thread(this);
229 input_thread.start();
235 public void set_radio() {
236 telemetry = AltosPreferences.telemetry(device.getSerial());
237 channel = AltosPreferences.channel(device.getSerial());
238 set_channel(channel);
239 set_callsign(AltosPreferences.callsign());
242 public void set_channel(int in_channel) {
243 channel = in_channel;
246 printf("m 0\nc r %d\nm %d\n", channel, telemetry);
248 printf("c r %d\n", channel);
253 public void set_telemetry(int in_telemetry) {
254 telemetry = in_telemetry;
257 printf("m 0\nm %d\n", telemetry);
262 void set_monitor(boolean monitor) {
263 monitor_mode = monitor;
266 printf("m %d\n", telemetry);
273 public void set_callsign(String callsign) {
275 printf ("c c %s\n", callsign);
280 public void start_remote() {
282 System.out.printf("start remote\n");
289 public void stop_remote() {
291 System.out.printf("stop remote\n");
298 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
301 monitor_mode = false;
302 telemetry = Altos.ao_telemetry_full;
303 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
304 reply_queue = new LinkedBlockingQueue<AltosLine> ();