Switch AltosUI to libaltos for device access
[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 class AltosSerialReader implements Runnable {
41         SWIGTYPE_p_altos_file altos;
42         LinkedList<LinkedBlockingQueue<String>> monitors;
43         LinkedBlockingQueue<String> reply_queue;
44         Thread input_thread;
45         String line;
46
47         public void run () {
48                 int c;
49
50                 try {
51                         for (;;) {
52                                 c = libaltos.altos_getchar(altos, 0);
53                                 if (Thread.interrupted())
54                                         break;
55                                 if (c == -1)
56                                         continue;
57                                 if (c == '\r')
58                                         continue;
59                                 synchronized(this) {
60                                         if (c == '\n') {
61                                                 if (line != "") {
62                                                         if (line.startsWith("VERSION")) {
63                                                                 for (int e = 0; e < monitors.size(); e++) {
64                                                                         LinkedBlockingQueue<String> q = monitors.get(e);
65                                                                         q.put(line);
66                                                                 }
67                                                         } else
68                                                                 reply_queue.put(line);
69                                                         line = "";
70                                                 }
71                                         } else {
72                                                 line = line + (char) c;
73                                         }
74                                 }
75                         }
76                 } catch (InterruptedException e) {
77                 }
78         }
79
80         public String get_reply() throws InterruptedException {
81                 return reply_queue.take();
82         }
83
84         public void add_monitor(LinkedBlockingQueue<String> q) {
85                 monitors.add(q);
86         }
87
88         public void remove_monitor(LinkedBlockingQueue<String> q) {
89                 monitors.remove(q);
90         }
91
92         public void flush () {
93                 synchronized(this) {
94                         if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
95                                 line = "";
96                         reply_queue.clear();
97                 }
98         }
99
100         public boolean opened() {
101                 return altos != null;
102         }
103
104         public void close() {
105                 if (altos != null) {
106                         libaltos.altos_close(altos);
107                         altos = null;
108                 }
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         }
118
119         public void open(altos_device device) throws FileNotFoundException {
120                 close();
121                 altos = libaltos.altos_open(device);
122                 input_thread = new Thread(this);
123                 input_thread.start();
124         }
125         public AltosSerialReader () {
126                 altos = null;
127                 input_thread = null;
128                 line = "";
129                 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
130                 reply_queue = new LinkedBlockingQueue<String> ();
131         }
132 }
133
134 public class AltosSerial {
135         AltosSerialReader reader = null;
136
137         public void close() {
138                 reader.close();
139         }
140
141         public void open(altos_device device) throws FileNotFoundException {
142                 reader.open(device);
143         }
144
145         void init() {
146                 reader = new AltosSerialReader();
147         }
148
149         public void add_monitor(LinkedBlockingQueue<String> q) {
150                 reader.add_monitor(q);
151         }
152
153         public void remove_monitor(LinkedBlockingQueue<String> q) {
154                 reader.remove_monitor(q);
155         }
156
157         public AltosSerial() {
158                 init();
159         }
160
161         public AltosSerial(altos_device device) throws FileNotFoundException {
162                 init();
163                 open(device);
164         }
165 }