altosui: Add preference for serial debugging.
[fw/altos] / 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.*;
27 import java.util.*;
28
29 import libaltosJNI.*;
30
31 /*
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
34  * threads.
35  */
36
37 public class AltosSerial implements Runnable {
38
39         static List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
40
41         AltosDevice device;
42         SWIGTYPE_p_altos_file altos;
43         LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
44         LinkedBlockingQueue<AltosLine> reply_queue;
45         Thread input_thread;
46         String line;
47         byte[] line_bytes;
48         int line_count;
49         boolean monitor_mode;
50         static boolean debug;
51
52         static void set_debug(boolean new_debug) {
53                 debug = new_debug;
54         }
55
56         public void run () {
57                 int c;
58
59                 try {
60                         for (;;) {
61                                 c = libaltos.altos_getchar(altos, 0);
62                                 if (Thread.interrupted())
63                                         break;
64                                 if (c == libaltosConstants.LIBALTOS_ERROR) {
65                                         for (int e = 0; e < monitors.size(); e++) {
66                                                 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
67                                                 q.put(new AltosLine());
68                                         }
69                                         reply_queue.put (new AltosLine());
70                                         break;
71                                 }
72                                 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
73                                         continue;
74                                 if (c == '\r')
75                                         continue;
76                                 synchronized(this) {
77                                         if (c == '\n') {
78                                                 if (line_count != 0) {
79                                                         try {
80                                                                 line = new String(line_bytes, 0, line_count, "UTF-8");
81                                                         } catch (UnsupportedEncodingException ue) {
82                                                                 line = "";
83                                                                 for (int i = 0; i < line_count; i++)
84                                                                         line = line + line_bytes[i];
85                                                         }
86                                                         if (line.startsWith("VERSION") || line.startsWith("CRC")) {
87                                                                 for (int e = 0; e < monitors.size(); e++) {
88                                                                         LinkedBlockingQueue<AltosLine> q = monitors.get(e);
89                                                                         q.put(new AltosLine (line));
90                                                                 }
91                                                         } else {
92                                                                 if (debug)
93                                                                         System.out.printf("\t\t\t\t\t%s\n", line);
94                                                                 reply_queue.put(new AltosLine (line));
95                                                         }
96                                                         line_count = 0;
97                                                         line = "";
98                                                 }
99                                         } else {
100                                                 if (line_bytes == null) {
101                                                         line_bytes = new byte[256];
102                                                 } else if (line_count == line_bytes.length) {
103                                                         byte[] new_line_bytes = new byte[line_count * 2];
104                                                         System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
105                                                         line_bytes = new_line_bytes;
106                                                 }
107                                                 line_bytes[line_count] = (byte) c;
108                                                 line_count++;
109                                         }
110                                 }
111                         }
112                 } catch (InterruptedException e) {
113                 }
114         }
115
116         public void flush_output() {
117                 if (altos != null)
118                         libaltos.altos_flush(altos);
119         }
120
121         public void flush_input() {
122                 flush_output();
123                 boolean got_some;
124                 do {
125                         try {
126                                 Thread.sleep(100);
127                         } catch (InterruptedException ie) {
128                         }
129                         got_some = !reply_queue.isEmpty();
130                         synchronized(this) {
131                                 if (!"VERSION".startsWith(line) &&
132                                     !line.startsWith("VERSION"))
133                                         line = "";
134                                 reply_queue.clear();
135                         }
136                 } while (got_some);
137         }
138
139         public String get_reply() throws InterruptedException {
140                 flush_output();
141                 AltosLine line = reply_queue.take();
142                 return line.line;
143         }
144
145         public String get_reply(int timeout) throws InterruptedException {
146                 flush_output();
147                 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
148                 if (line == null)
149                         return null;
150                 return line.line;
151         }
152
153         public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
154                 set_monitor(true);
155                 monitors.add(q);
156         }
157
158         public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
159                 monitors.remove(q);
160                 if (monitors.isEmpty())
161                         set_monitor(false);
162         }
163
164         public void close() {
165                 if (altos != null) {
166                         libaltos.altos_close(altos);
167                 }
168                 if (input_thread != null) {
169                         try {
170                                 input_thread.interrupt();
171                                 input_thread.join();
172                         } catch (InterruptedException e) {
173                         }
174                         input_thread = null;
175                 }
176                 if (altos != null) {
177                         libaltos.altos_free(altos);
178                         altos = null;
179                 }
180                 synchronized (devices_opened) {
181                         devices_opened.remove(device.getPath());
182                 }
183         }
184
185         private void putc(char c) {
186                 if (altos != null)
187                         libaltos.altos_putchar(altos, c);
188         }
189
190         public void print(String data) {
191                 if (debug)
192                         System.out.printf("%s", data);
193                 for (int i = 0; i < data.length(); i++)
194                         putc(data.charAt(i));
195         }
196
197         public void printf(String format, Object ... arguments) {
198                 print(String.format(format, arguments));
199         }
200
201         private void open() throws FileNotFoundException, AltosSerialInUseException {
202                 synchronized (devices_opened) {
203                         if (devices_opened.contains(device.getPath()))
204                                 throw new AltosSerialInUseException(device);
205                         devices_opened.add(device.getPath());
206                 }
207                 altos = libaltos.altos_open(device);
208                 if (altos == null) {
209                         close();
210                         throw new FileNotFoundException(device.toShortString());
211                 }
212                 input_thread = new Thread(this);
213                 input_thread.start();
214                 print("~\nE 0\n");
215                 set_monitor(false);
216                 flush_output();
217         }
218
219         public void set_radio() {
220                 set_channel(AltosPreferences.channel(device.getSerial()));
221                 set_callsign(AltosPreferences.callsign());
222         }
223
224         public void set_channel(int channel) {
225                 if (altos != null) {
226                         if (monitor_mode)
227                                 printf("m 0\nc r %d\nm 1\n", channel);
228                         else
229                                 printf("c r %d\n", channel);
230                         flush_output();
231                 }
232         }
233
234         void set_monitor(boolean monitor) {
235                 monitor_mode = monitor;
236                 if (altos != null) {
237                         if (monitor)
238                                 printf("m 1\n");
239                         else
240                                 printf("m 0\n");
241                         flush_output();
242                 }
243         }
244
245         public void set_callsign(String callsign) {
246                 if (altos != null) {
247                         printf ("c c %s\n", callsign);
248                         flush_output();
249                 }
250         }
251
252         public void start_remote() {
253                 set_radio();
254                 printf("p\nE 0\n");
255                 flush_input();
256         }
257
258         public void stop_remote() {
259                 printf ("~");
260                 flush_output();
261         }
262
263         public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
264                 device = in_device;
265                 line = "";
266                 monitor_mode = false;
267                 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
268                 reply_queue = new LinkedBlockingQueue<AltosLine> ();
269                 open();
270         }
271 }