Debian Codegraph @

CNI/CommPortIdentifier.java

raw symbols SCIP
1 /*-------------------------------------------------------------------------
2 | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
3 | RXTX is a native interface to serial ports in java.
4 | Copyright 1997-2007 by Trent Jarvi [email protected] and others who
5 | actually wrote it. See individual source files for more information.
6 |
7 | A copy of the LGPL v 2.1 may be found at
8 | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is
9 | here for your convenience.
10 |
11 | This library is free software; you can redistribute it and/or
12 | modify it under the terms of the GNU Lesser General Public
13 | License as published by the Free Software Foundation; either
14 | version 2.1 of the License, or (at your option) any later version.
15 |
16 | This library is distributed in the hope that it will be useful,
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 | Lesser General Public License for more details.
20 |
21 | An executable that contains no derivative of any portion of RXTX, but
22 | is designed to work with RXTX by being dynamically linked with it,
23 | is considered a "work that uses the Library" subject to the terms and
24 | conditions of the GNU Lesser General Public License.
25 |
26 | The following has been added to the RXTX License to remove
27 | any confusion about linking to RXTX. We want to allow in part what
28 | section 5, paragraph 2 of the LGPL does not permit in the special
29 | case of linking over a controlled interface. The intent is to add a
30 | Java Specification Request or standards body defined interface in the
31 | future as another exception but one is not currently available.
32 |
33 | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
34 |
35 | As a special exception, the copyright holders of RXTX give you
36 | permission to link RXTX with independent modules that communicate with
37 | RXTX solely through the Sun Microsytems CommAPI interface version 2,
38 | regardless of the license terms of these independent modules, and to copy
39 | and distribute the resulting combined work under terms of your choice,
40 | provided that every copy of the combined work is accompanied by a complete
41 | copy of the source code of RXTX (the version of RXTX used to produce the
42 | combined work), being distributed under the terms of the GNU Lesser General
43 | Public License plus this exception. An independent module is a
44 | module which is not derived from or based on RXTX.
45 |
46 | Note that people who make modified versions of RXTX are not obligated
47 | to grant this special exception for their modified versions; it is
48 | their choice whether to do so. The GNU Lesser General Public License
49 | gives permission to release a modified version without this exception; this
50 | exception also makes it possible to release a modified version which
51 | carries forward this exception.
52 |
53 | You should have received a copy of the GNU Lesser General Public
54 | License along with this library; if not, write to the Free
55 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 | All trademarks belong to their respective owners.
57 --------------------------------------------------------------------------*/
58 package gnu.io;
59
60 import java.io.FileDescriptor;
61 import java.util.Vector;
62 import java.util.Enumeration;
63
64 /**
65 * @author Trent Jarvi
66 * @version %I%, %G%
67 * @since JDK1.0
68 */
69
70 public class CommPortIdentifier extends Object /* extends Vector? */
71 {
72 public static final int PORT_SERIAL = 1; // rs232 Port
73 public static final int PORT_PARALLEL = 2; // Parallel Port
74 public static final int PORT_I2C = 3; // i2c Port
75 public static final int PORT_RS485 = 4; // rs485 Port
76 public static final int PORT_RAW = 5; // Raw Port
77 private String PortName;
78 private boolean Available = true;
79 private String Owner;
80 private CommPort commport;
81 private CommDriver RXTXDriver;
82 static CommPortIdentifier CommPortIndex;
83 CommPortIdentifier next;
84 private int PortType;
85 private final static boolean debug = false;
86 static Object Sync;
87 Vector ownershipListener;
88
89
90
91 /*------------------------------------------------------------------------------
92 static {} aka initialization
93 accept: -
94 perform: load the rxtx driver
95 return: -
96 exceptions: Throwable
97 comments: static block to initialize the class
98 ------------------------------------------------------------------------------*/
99 // initialization only done once....
100 static
101 {
102 if(debug) System.out.println("CommPortIdentifier:static initialization()");
103 Sync = new Object();
104 try
105 {
106 CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
107 RXTXDriver.initialize();
108 }
109 catch (Throwable e)
110 {
111 System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
112 }
113
114 String OS;
115
116 OS = System.getProperty("os.name");
117 if(OS.toLowerCase().indexOf("linux") == -1)
118 {
119 if (debug)
120 System.out.println("Have not implemented native_psmisc_report_owner(PortName)); in CommPortIdentifier");
121 }
122 /*
123 System.loadLibrary( "rxtxSerial" );
124 */
125 }
126 CommPortIdentifier ( String pn, CommPort cp, int pt, CommDriver driver)
127 {
128 PortName = pn;
129 commport = cp;
130 PortType = pt;
131 next = null;
132 RXTXDriver = driver;
133
134 }
135
136 /*------------------------------------------------------------------------------
137 addPortName()
138 accept: Name of the port s, Port type,
139 reverence to RXTXCommDriver.
140 perform: place a new CommPortIdentifier in the linked list
141 return: none.
142 exceptions: none.
143 comments:
144 ------------------------------------------------------------------------------*/
145 public static void addPortName(String s, int type, CommDriver c)
146 {
147
148 if(debug) System.out.println("CommPortIdentifier:addPortName("+s+")");
149 AddIdentifierToList(new CommPortIdentifier(s, null, type, c));
150 }
151 /*------------------------------------------------------------------------------
152 AddIdentifierToList()
153 accept: The cpi to add to the list.
154 perform:
155 return:
156 exceptions:
157 comments:
158 ------------------------------------------------------------------------------*/
159 private static void AddIdentifierToList( CommPortIdentifier cpi)
160 {
161 if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList()");
162 synchronized (Sync)
163 {
164 if (CommPortIndex == null)
165 {
166 CommPortIndex = cpi;
167 if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() null");
168 }
169 else
170 {
171 CommPortIdentifier index = CommPortIndex;
172 while (index.next != null)
173 {
174 index = index.next;
175 if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() index.next");
176 }
177 index.next = cpi;
178 }
179 }
180 }
181 /*------------------------------------------------------------------------------
182 addPortOwnershipListener()
183 accept:
184 perform:
185 return:
186 exceptions:
187 comments:
188 ------------------------------------------------------------------------------*/
189 public void addPortOwnershipListener(CommPortOwnershipListener c)
190 {
191 if(debug) System.out.println("CommPortIdentifier:addPortOwnershipListener()");
192
193 /* is the Vector instantiated? */
194
195 if( ownershipListener == null )
196 {
197 ownershipListener = new Vector();
198 }
199
200 /* is the ownership listener already in the list? */
201
202 if ( ownershipListener.contains(c) == false)
203 {
204 ownershipListener.addElement(c);
205 }
206 }
207 /*------------------------------------------------------------------------------
208 getCurrentOwner()
209 accept:
210 perform:
211 return:
212 exceptions:
213 comments:
214 ------------------------------------------------------------------------------*/
215 public String getCurrentOwner()
216 {
217 if(debug) System.out.println("CommPortIdentifier:getCurrentOwner()");
218 return( Owner );
219 }
220 /*------------------------------------------------------------------------------
221 getName()
222 accept:
223 perform:
224 return:
225 exceptions:
226 comments:
227 ------------------------------------------------------------------------------*/
228 public String getName()
229 {
230 if(debug) System.out.println("CommPortIdentifier:getName()");
231 return( PortName );
232 }
233 /*------------------------------------------------------------------------------
234 getPortIdentifier()
235 accept:
236 perform:
237 return:
238 exceptions:
239 comments:
240 ------------------------------------------------------------------------------*/
241 static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException
242 {
243 if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(" + s +")");
244 CommPortIdentifier index = CommPortIndex;
245
246 synchronized (Sync)
247 {
248 while (index != null)
249 {
250 if (index.PortName.equals(s)) break;
251 index = index.next;
252 }
253 }
254 if (index != null) return index;
255 else
256 {
257 if ( debug )
258 System.out.println("not found!" + s);
259 throw new NoSuchPortException();
260 }
261 }
262 /*------------------------------------------------------------------------------
263 getPortIdentifier()
264 accept:
265 perform:
266 return:
267 exceptions:
268 comments:
269 ------------------------------------------------------------------------------*/
270 static public CommPortIdentifier getPortIdentifier(CommPort p)
271 throws NoSuchPortException
272 {
273 if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(CommPort)");
274 CommPortIdentifier c = CommPortIndex;
275 synchronized( Sync )
276 {
277 while ( c != null && c.commport != p )
278 c = c.next;
279 }
280 if ( c != null )
281 return (c);
282
283 if ( debug )
284 System.out.println("not found!" + p.getName());
285 throw new NoSuchPortException();
286 }
287 /*------------------------------------------------------------------------------
288 getPortIdentifiers()
289 accept:
290 perform:
291 return:
292 exceptions:
293 comments:
294 ------------------------------------------------------------------------------*/
295 static public Enumeration getPortIdentifiers()
296 {
297 if(debug) System.out.println("static CommPortIdentifier:getPortIdentifiers()");
298 return new CommPortEnumerator();
299 }
300 /*------------------------------------------------------------------------------
301 getPortType()
302 accept:
303 perform:
304 return:
305 exceptions:
306 comments:
307 ------------------------------------------------------------------------------*/
308 public int getPortType()
309 {
310 if(debug) System.out.println("CommPortIdentifier:getPortType()");
311 return( PortType );
312 }
313 /*------------------------------------------------------------------------------
314 isCurrentlyOwned()
315 accept:
316 perform:
317 return:
318 exceptions:
319 comments:
320 ------------------------------------------------------------------------------*/
321 public synchronized boolean isCurrentlyOwned()
322 {
323 if(debug) System.out.println("CommPortIdentifier:isCurrentlyOwned()");
324 return(!Available);
325 }
326 /*------------------------------------------------------------------------------
327 open()
328 accept:
329 perform:
330 return:
331 exceptions:
332 comments:
333 ------------------------------------------------------------------------------*/
334 public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException
335 {
336 if(debug) System.out.println("CommPortIdentifier:open(FileDescriptor)");
337 throw new UnsupportedCommOperationException();
338 }
339 private native String native_psmisc_report_owner(String PortName);
340
341 /*------------------------------------------------------------------------------
342 open()
343 accept: application makeing the call and milliseconds to block
344 during open.
345 perform: open the port if possible
346 return: CommPort if successfull
347 exceptions: PortInUseException if in use.
348 comments:
349 ------------------------------------------------------------------------------*/
350 private boolean HideOwnerEvents;
351
352 public synchronized CommPort open(String TheOwner, int i)
353 throws gnu.io.PortInUseException
354 {
355 if(debug) System.out.println("CommPortIdentifier:open("+TheOwner + ", " +i+")");
356 if (Available == false)
357 {
358 synchronized (Sync)
359 {
360 fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
361 try
362 {
363 wait(i);
364 }
365 catch ( InterruptedException e ) { }
366 }
367 }
368 if (Available == false)
369 {
370 throw new gnu.io.PortInUseException(getCurrentOwner());
371 }
372 if(commport == null)
373 {
374 commport = RXTXDriver.getCommPort(PortName,PortType);
375 }
376 if(commport != null)
377 {
378 Owner = TheOwner;
379 Available=false;
380 fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED);
381 return commport;
382 }
383 else
384 {
385 throw new gnu.io.PortInUseException(
386 native_psmisc_report_owner(PortName));
387 }
388 }
389 /*------------------------------------------------------------------------------
390 removePortOwnership()
391 accept:
392 perform:
393 return:
394 exceptions:
395 comments:
396 ------------------------------------------------------------------------------*/
397 public void removePortOwnershipListener(CommPortOwnershipListener c)
398 {
399 if(debug) System.out.println("CommPortIdentifier:removePortOwnershipListener()");
400 /* why is this called twice? */
401 if(ownershipListener != null)
402 ownershipListener.removeElement(c);
403 }
404
405 /*------------------------------------------------------------------------------
406 internalClosePort()
407 accept: None
408 perform: clean up the Ownership information and send the event
409 return: None
410 exceptions: None
411 comments: None
412 ------------------------------------------------------------------------------*/
413 synchronized void internalClosePort()
414 {
415 if(debug) System.out.println("CommPortIdentifier:internalClosePort()");
416 Owner = null;
417 Available = true;
418 commport = null;
419 /* this tosses null pointer?? */
420 notifyAll();
421 fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED);
422 }
423 /*------------------------------------------------------------------------------
424 fireOwnershipEvent()
425 accept:
426 perform:
427 return:
428 exceptions:
429 comments:
430 ------------------------------------------------------------------------------*/
431 void fireOwnershipEvent(int eventType)
432 {
433 if(debug) System.out.println("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )");
434 if (ownershipListener != null)
435 {
436 CommPortOwnershipListener c;
437 for ( Enumeration e = ownershipListener.elements();
438 e.hasMoreElements();
439 c.ownershipChange(eventType))
440 c = (CommPortOwnershipListener) e.nextElement();
441 }
442 }
443 }
444