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