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