Debian Codegraph @

src/gnu/io/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.HashMap;
62 import java.util.Vector;
63 import java.util.Enumeration;
64
65 /**
66 * @author Trent Jarvi
67 * @version %I%, %G%
68 * @since JDK1.0
69 */
70
71 public class CommPortIdentifier extends Object /* extends Vector? */
72 {
73 public static final int PORT_SERIAL = 1; // rs232 Port
74 public static final int PORT_PARALLEL = 2; // Parallel Port
75 public static final int PORT_I2C = 3; // i2c Port
76 public static final int PORT_RS485 = 4; // rs485 Port
77 public static final int PORT_RAW = 5; // Raw Port
78 private String PortName;
79 private boolean Available = true;
80 private String Owner;
81 private CommPort commport;
82 private CommDriver RXTXDriver;
83 static CommPortIdentifier CommPortIndex;
84 CommPortIdentifier next;
85 private int PortType;
86 private final static boolean debug = false;
87 static Object Sync;
88 Vector ownershipListener;
89
90
91
92 /*------------------------------------------------------------------------------
93 static {} aka initialization
94 accept: -
95 perform: load the rxtx driver
96 return: -
97 exceptions: Throwable
98 comments: static block to initialize the class
99 ------------------------------------------------------------------------------*/
100 // initialization only done once....
101 static
102 {
103 if(debug) System.out.println("CommPortIdentifier:static initialization()");
104 Sync = new Object();
105 try
106 {
107 CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
108 RXTXDriver.initialize();
109 }
110 catch (Throwable e)
111 {
112 System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
113 }
114
115 String OS;
116
117 OS = System.getProperty("os.name");
118 if(OS.toLowerCase().indexOf("linux") == -1)
119 {
120 if (debug)
121 System.out.println("Have not implemented native_psmisc_report_owner(PortName)); in CommPortIdentifier");
122 }
123 System.loadLibrary( "rxtxSerial" );
124 }
125 CommPortIdentifier ( String pn, CommPort cp, int pt, CommDriver driver)
126 {
127 PortName = pn;
128 commport = cp;
129 PortType = pt;
130 next = null;
131 RXTXDriver = driver;
132
133 }
134
135 /*------------------------------------------------------------------------------
136 addPortName()
137 accept: Name of the port s, Port type,
138 reverence to RXTXCommDriver.
139 perform: place a new CommPortIdentifier in the linked list
140 return: none.
141 exceptions: none.
142 comments:
143 ------------------------------------------------------------------------------*/
144 public static void addPortName(String s, int type, CommDriver c)
145 {
146
147 if(debug) System.out.println("CommPortIdentifier:addPortName("+s+")");
148 AddIdentifierToList(new CommPortIdentifier(s, null, type, c));
149 }
150 /*------------------------------------------------------------------------------
151 AddIdentifierToList()
152 accept: The cpi to add to the list.
153 perform:
154 return:
155 exceptions:
156 comments:
157 ------------------------------------------------------------------------------*/
158 private static void AddIdentifierToList( CommPortIdentifier cpi)
159 {
160 if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList()");
161 synchronized (Sync)
162 {
163 if (CommPortIndex == null)
164 {
165 CommPortIndex = cpi;
166 if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() null");
167 }
168 else
169 {
170 CommPortIdentifier index = CommPortIndex;
171 while (index.next != null)
172 {
173 index = index.next;
174 if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() index.next");
175 }
176 index.next = cpi;
177 }
178 }
179 }
180 /*------------------------------------------------------------------------------
181 addPortOwnershipListener()
182 accept:
183 perform:
184 return:
185 exceptions:
186 comments:
187 ------------------------------------------------------------------------------*/
188 public void addPortOwnershipListener(CommPortOwnershipListener c)
189 {
190 if(debug) System.out.println("CommPortIdentifier:addPortOwnershipListener()");
191
192 /* is the Vector instantiated? */
193
194 if( ownershipListener == null )
195 {
196 ownershipListener = new Vector();
197 }
198
199 /* is the ownership listener already in the list? */
200
201 if ( ownershipListener.contains(c) == false)
202 {
203 ownershipListener.addElement(c);
204 }
205 }
206 /*------------------------------------------------------------------------------
207 getCurrentOwner()
208 accept:
209 perform:
210 return:
211 exceptions:
212 comments:
213 ------------------------------------------------------------------------------*/
214 public String getCurrentOwner()
215 {
216 if(debug) System.out.println("CommPortIdentifier:getCurrentOwner()");
217 return( Owner );
218 }
219 /*------------------------------------------------------------------------------
220 getName()
221 accept:
222 perform:
223 return:
224 exceptions:
225 comments:
226 ------------------------------------------------------------------------------*/
227 public String getName()
228 {
229 if(debug) System.out.println("CommPortIdentifier:getName()");
230 return( PortName );
231 }
232 /*------------------------------------------------------------------------------
233 getPortIdentifier()
234 accept:
235 perform:
236 return:
237 exceptions:
238 comments:
239 ------------------------------------------------------------------------------*/
240 static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException
241 {
242 if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(" + s +")");
243 CommPortIdentifier index;
244
245 synchronized (Sync)
246 {
247 index = CommPortIndex;
248 while (index != null && !index.PortName.equals(s)) {
249 index = index.next;
250 }
251 if (index == null) {
252 /* This may slow things down but if you pass the string for the port after
253 a device is plugged in, you can find it now.
254
255 http://bugzilla.qbang.org/show_bug.cgi?id=48
256 */
257 getPortIdentifiers();
258 index = CommPortIndex;
259 while (index != null && !index.PortName.equals(s)) {
260 index = index.next;
261 }
262 }
263 }
264 if (index != null) return index;
265 else
266 {
267 if ( debug )
268 System.out.println("not found!" + s);
269 throw new NoSuchPortException();
270 }
271 }
272 /*------------------------------------------------------------------------------
273 getPortIdentifier()
274 accept:
275 perform:
276 return:
277 exceptions:
278 comments:
279 ------------------------------------------------------------------------------*/
280 static public CommPortIdentifier getPortIdentifier(CommPort p)
281 throws NoSuchPortException
282 {
283 if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(CommPort)");
284 CommPortIdentifier c;
285 synchronized( Sync )
286 {
287 c = CommPortIndex;
288 while ( c != null && c.commport != p )
289 c = c.next;
290 }
291 if ( c != null )
292 return (c);
293
294 if ( debug )
295 System.out.println("not found!" + p.getName());
296 throw new NoSuchPortException();
297 }
298 /*------------------------------------------------------------------------------
299 getPortIdentifiers()
300 accept:
301 perform:
302 return:
303 exceptions:
304 comments:
305 ------------------------------------------------------------------------------*/
306 static public Enumeration getPortIdentifiers()
307 {
308 if(debug) System.out.println("static CommPortIdentifier:getPortIdentifiers()");
309 //Do not allow anybody get any ports while we are re-initializing
310 //because the CommPortIndex points to invalid instances during that time
311 synchronized(Sync) {
312 //Remember old ports in order to restore them for ownership events later
313 HashMap oldPorts = new HashMap();
314 CommPortIdentifier p = CommPortIndex;
315 while(p!=null) {
316 oldPorts.put(p.PortName, p);
317 p = p.next;
318 }
319 CommPortIndex = null;
320 try
321 {
322 //Initialize RXTX: This leads to detecting all ports
323 //and writing them into our CommPortIndex through our method
324 //{@link #addPortName(java.lang.String, int, gnu.io.CommDriver)}
325 //This works while lock on Sync is held
326 CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
327 RXTXDriver.initialize();
328 //Restore old CommPortIdentifier objects where possible,
329 //in order to support proper ownership event handling.
330 //Clients might still have references to old identifiers!
331 CommPortIdentifier curPort = CommPortIndex;
332 CommPortIdentifier prevPort = null;
333 while(curPort!=null) {
334 CommPortIdentifier matchingOldPort = (CommPortIdentifier)oldPorts.get(curPort.PortName);
335 if(matchingOldPort!=null && matchingOldPort.PortType == curPort.PortType) {
336 //replace new port by old one
337 matchingOldPort.RXTXDriver = curPort.RXTXDriver;
338 matchingOldPort.next = curPort.next;
339 if(prevPort==null) {
340 CommPortIndex = matchingOldPort;
341 } else {
342 prevPort.next = matchingOldPort;
343 }
344 prevPort = matchingOldPort;
345 } else {
346 prevPort = curPort;
347 }
348 curPort = curPort.next;
349 }
350 }
351 catch (Throwable e)
352 {
353 System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
354 System.err.flush();
355 }
356 }
357 return new CommPortEnumerator();
358 }
359 /*------------------------------------------------------------------------------
360 getPortType()
361 accept:
362 perform:
363 return:
364 exceptions:
365 comments:
366 ------------------------------------------------------------------------------*/
367 public int getPortType()
368 {
369 if(debug) System.out.println("CommPortIdentifier:getPortType()");
370 return( PortType );
371 }
372 /*------------------------------------------------------------------------------
373 isCurrentlyOwned()
374 accept:
375 perform:
376 return:
377 exceptions:
378 comments:
379 ------------------------------------------------------------------------------*/
380 public synchronized boolean isCurrentlyOwned()
381 {
382 if(debug) System.out.println("CommPortIdentifier:isCurrentlyOwned()");
383 return(!Available);
384 }
385 /*------------------------------------------------------------------------------
386 open()
387 accept:
388 perform:
389 return:
390 exceptions:
391 comments:
392 ------------------------------------------------------------------------------*/
393 public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException
394 {
395 if(debug) System.out.println("CommPortIdentifier:open(FileDescriptor)");
396 throw new UnsupportedCommOperationException();
397 }
398 private native String native_psmisc_report_owner(String PortName);
399
400 /*------------------------------------------------------------------------------
401 open()
402 accept: application making the call and milliseconds to block
403 during open.
404 perform: open the port if possible
405 return: CommPort if successful
406 exceptions: PortInUseException if in use.
407 comments:
408 ------------------------------------------------------------------------------*/
409 private boolean HideOwnerEvents;
410
411 public CommPort open(String TheOwner, int i)
412 throws gnu.io.PortInUseException
413 {
414 if(debug) System.out.println("CommPortIdentifier:open("+TheOwner + ", " +i+")");
415 boolean isAvailable;
416 synchronized(this) {
417 isAvailable = this.Available;
418 if (isAvailable) {
419 //assume ownership inside the synchronized block
420 this.Available = false;
421 this.Owner = TheOwner;
422 }
423 }
424 if (!isAvailable)
425 {
426 long waitTimeEnd = System.currentTimeMillis() + i;
427 //fire the ownership event outside the synchronized block
428 fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
429 long waitTimeCurr;
430 synchronized(this) {
431 while(!Available && (waitTimeCurr=System.currentTimeMillis()) < waitTimeEnd) {
432 try
433 {
434 wait(waitTimeEnd - waitTimeCurr);
435 }
436 catch ( InterruptedException e )
437 {
438 Thread.currentThread().interrupt();
439 break;
440 }
441 }
442 isAvailable = this.Available;
443 if (isAvailable) {
444 //assume ownership inside the synchronized block
445 this.Available = false;
446 this.Owner = TheOwner;
447 }
448 }
449 }
450 if (!isAvailable)
451 {
452 throw new gnu.io.PortInUseException(getCurrentOwner());
453 }
454 //At this point, the CommPortIdentifier is owned by us.
455 try {
456 if(commport == null)
457 {
458 commport = RXTXDriver.getCommPort(PortName,PortType);
459 }
460 if(commport != null)
461 {
462 fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED);
463 return commport;
464 }
465 else
466 {
467 throw new gnu.io.PortInUseException(
468 native_psmisc_report_owner(PortName));
469 }
470 } finally {
471 if(commport == null) {
472 //something went wrong reserving the commport -> unown the port
473 synchronized(this) {
474 this.Available = true;
475 this.Owner = null;
476 }
477 }
478 }
479 }
480 /*------------------------------------------------------------------------------
481 removePortOwnership()
482 accept:
483 perform:
484 return:
485 exceptions:
486 comments:
487 ------------------------------------------------------------------------------*/
488 public void removePortOwnershipListener(CommPortOwnershipListener c)
489 {
490 if(debug) System.out.println("CommPortIdentifier:removePortOwnershipListener()");
491 /* why is this called twice? */
492 if(ownershipListener != null)
493 ownershipListener.removeElement(c);
494 }
495
496 /*------------------------------------------------------------------------------
497 internalClosePort()
498 accept: None
499 perform: clean up the Ownership information and send the event
500 return: None
501 exceptions: None
502 comments: None
503 ------------------------------------------------------------------------------*/
504 void internalClosePort()
505 {
506 synchronized(this) {
507 if(debug) System.out.println("CommPortIdentifier:internalClosePort()");
508 Owner = null;
509 Available = true;
510 commport = null;
511 /* this tosses null pointer?? */
512 notifyAll();
513 }
514 fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED);
515 }
516 /*------------------------------------------------------------------------------
517 fireOwnershipEvent()
518 accept:
519 perform:
520 return:
521 exceptions:
522 comments:
523 ------------------------------------------------------------------------------*/
524 void fireOwnershipEvent(int eventType)
525 {
526 if(debug) System.out.println("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )");
527 if (ownershipListener != null)
528 {
529 CommPortOwnershipListener c;
530 for ( Enumeration e = ownershipListener.elements();
531 e.hasMoreElements();
532 c.ownershipChange(eventType))
533 c = (CommPortOwnershipListener) e.nextElement();
534 }
535 }
536 }
537