altosui: Make teledongle callsign configurable
[fw/altos] / ao-tools / altosui / AltosSerial.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 /*
19  * Deal with TeleDongle on a serial port
20  */
21
22 package altosui;
23
24 import java.lang.*;
25 import java.io.*;
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29 import altosui.AltosSerialMonitor;
30 import libaltosJNI.libaltos;
31 import libaltosJNI.altos_device;
32 import libaltosJNI.SWIGTYPE_p_altos_file;
33 import libaltosJNI.SWIGTYPE_p_altos_list;
34
35 /*
36  * This class reads from the serial port and places each received
37  * line in a queue. Dealing with that queue is left up to other
38  * threads.
39  */
40
41 public class AltosSerial implements Runnable {
42
43         SWIGTYPE_p_altos_file altos;
44         LinkedList<LinkedBlockingQueue<String>> monitors;
45         LinkedBlockingQueue<String> reply_queue;
46         Thread input_thread;
47         String line;
48
49         public void run () {
50                 int c;
51
52                 try {
53                         for (;;) {
54                                 c = libaltos.altos_getchar(altos, 0);
55                                 if (Thread.interrupted())
56                                         break;
57                                 if (c == -1)
58                                         continue;
59                                 if (c == '\r')
60                                         continue;
61                                 synchronized(this) {
62                                         if (c == '\n') {
63                                                 if (line != "") {
64                                                         if (line.startsWith("VERSION")) {
65                                                                 for (int e = 0; e < monitors.size(); e++) {
66                                                                         LinkedBlockingQueue<String> q = monitors.get(e);
67                                                                         q.put(line);
68                                                                 }
69                                                         } else
70                                                                 reply_queue.put(line);
71                                                         line = "";
72                                                 }
73                                         } else {
74                                                 line = line + (char) c;
75                                         }
76                                 }
77                         }
78                 } catch (InterruptedException e) {
79                 }
80         }
81
82         public String get_reply() throws InterruptedException {
83                 return reply_queue.take();
84         }
85
86         public void add_monitor(LinkedBlockingQueue<String> q) {
87                 monitors.add(q);
88         }
89
90         public void remove_monitor(LinkedBlockingQueue<String> q) {
91                 monitors.remove(q);
92         }
93
94         public void flush () {
95                 synchronized(this) {
96                         if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
97                                 line = "";
98                         reply_queue.clear();
99                 }
100         }
101
102         public boolean opened() {
103                 return altos != null;
104         }
105
106         public void close() {
107                 if (altos != null)
108                         libaltos.altos_close(altos);
109                 if (input_thread != null) {
110                         try {
111                                 input_thread.interrupt();
112                                 input_thread.join();
113                         } catch (InterruptedException e) {
114                         }
115                         input_thread = null;
116                 }
117                 if (altos != null) {
118                         libaltos.altos_free(altos);
119                         altos = null;
120                 }
121         }
122
123         public void putc(char c) {
124                 if (altos != null)
125                         libaltos.altos_putchar(altos, c);
126         }
127
128         public void print(String data) {
129                 for (int i = 0; i < data.length(); i++)
130                         putc(data.charAt(i));
131         }
132
133         public void printf(String format, Object ... arguments) {
134                 print(String.format(format, arguments));
135         }
136
137         public void open(altos_device device) throws FileNotFoundException {
138                 close();
139                 altos = libaltos.altos_open(device);
140                 if (altos == null)
141                         throw new FileNotFoundException(device.getPath());
142                 input_thread = new Thread(this);
143                 input_thread.start();
144                 print("\nE 0\n");
145                 try {
146                         Thread.sleep(200);
147                 } catch (InterruptedException e) {
148                 }
149                 flush();
150         }
151
152         public void set_channel(int channel) {
153                 if (altos != null)
154                         printf("m 0\nc r %d\nm 1\n", channel);
155         }
156
157         public void set_callsign(String callsign) {
158                 if (altos != null)
159                         printf ("c c %s\n", callsign);
160         }
161
162         public AltosSerial() {
163                 altos = null;
164                 input_thread = null;
165                 line = "";
166                 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
167                 reply_queue = new LinkedBlockingQueue<String> ();
168         }
169 }