1
/*-------------------------------------------------------------------------
2
| rxtx is a native interface to serial ports in java.
3
| Copyright 1997-2004 by Trent Jarvi taj@www.linux.org.uk
5
| This library is free software; you can redistribute it and/or
6
| modify it under the terms of the GNU Library General Public
7
| License as published by the Free Software Foundation; either
8
| version 2 of the License, or (at your option) any later version.
10
| This library is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
| Library General Public License for more details.
15
| You should have received a copy of the GNU Library General Public
16
| License along with this library; if not, write to the Free
17
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
--------------------------------------------------------------------------*/
21
import java.io.FileDescriptor;
22
import java.util.Vector;
23
import java.util.Enumeration;
31
public class CommPortIdentifier extends Object /* extends Vector? */
33
public static final int PORT_SERIAL = 1; // rs232 Port
34
public static final int PORT_PARALLEL = 2; // Parallel Port
35
public static final int PORT_I2C = 3; // i2c Port
36
public static final int PORT_RS485 = 4; // rs485 Port
37
public static final int PORT_RAW = 5; // Raw Port
38
private String PortName;
39
private boolean Available = true;
41
private CommPort commport;
42
private CommDriver RXTXDriver;
43
static CommPortIdentifier CommPortIndex;
44
CommPortIdentifier next;
46
private final static boolean debug = false;
48
Vector ownershipListener;
52
/*------------------------------------------------------------------------------
53
static {} aka initialization
55
perform: load the rxtx driver
58
comments: static block to initialize the class
59
------------------------------------------------------------------------------*/
60
// initialization only done once....
63
if(debug) System.out.println("CommPortIdentifier:static initialization()");
67
CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
68
RXTXDriver.initialize();
72
System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
77
OS = System.getProperty("os.name");
78
if(OS.toLowerCase().indexOf("linux") == -1)
81
System.out.println("Have not implemented native_psmisc_report_owner(PortName)); in CommPortIdentifier");
84
System.loadLibrary( "rxtxSerial" );
87
CommPortIdentifier ( String pn, CommPort cp, int pt, CommDriver driver)
97
/*------------------------------------------------------------------------------
99
accept: Name of the port s, Port type,
100
reverence to RXTXCommDriver.
101
perform: place a new CommPortIdentifier in the linked list
105
------------------------------------------------------------------------------*/
106
public static void addPortName(String s, int type, CommDriver c)
109
if(debug) System.out.println("CommPortIdentifier:addPortName("+s+")");
110
AddIdentifierToList(new CommPortIdentifier(s, null, type, c));
112
/*------------------------------------------------------------------------------
113
AddIdentifierToList()
114
accept: The cpi to add to the list.
119
------------------------------------------------------------------------------*/
120
private static void AddIdentifierToList( CommPortIdentifier cpi)
122
if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList()");
125
if (CommPortIndex == null)
128
if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() null");
132
CommPortIdentifier index = CommPortIndex;
133
while (index.next != null)
136
if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() index.next");
142
/*------------------------------------------------------------------------------
143
addPortOwnershipListener()
149
------------------------------------------------------------------------------*/
150
public void addPortOwnershipListener(CommPortOwnershipListener c)
152
if(debug) System.out.println("CommPortIdentifier:addPortOwnershipListener()");
154
/* is the Vector instantiated? */
156
if( ownershipListener == null )
158
ownershipListener = new Vector();
161
/* is the ownership listener already in the list? */
163
if ( ownershipListener.contains(c) == false)
165
ownershipListener.addElement(c);
168
/*------------------------------------------------------------------------------
175
------------------------------------------------------------------------------*/
176
public String getCurrentOwner()
178
if(debug) System.out.println("CommPortIdentifier:getCurrentOwner()");
181
/*------------------------------------------------------------------------------
188
------------------------------------------------------------------------------*/
189
public String getName()
191
if(debug) System.out.println("CommPortIdentifier:getName()");
194
/*------------------------------------------------------------------------------
201
------------------------------------------------------------------------------*/
202
static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException
204
if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(" + s +")");
205
CommPortIdentifier index = CommPortIndex;
209
while (index != null)
211
if (index.PortName.equals(s)) break;
215
if (index != null) return index;
219
System.out.println("not found!" + s);
220
throw new NoSuchPortException();
223
/*------------------------------------------------------------------------------
230
------------------------------------------------------------------------------*/
231
static public CommPortIdentifier getPortIdentifier(CommPort p)
232
throws NoSuchPortException
234
if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(CommPort)");
235
CommPortIdentifier c = CommPortIndex;
238
while ( c != null && c.commport != p )
245
System.out.println("not found!" + p.getName());
246
throw new NoSuchPortException();
248
/*------------------------------------------------------------------------------
255
------------------------------------------------------------------------------*/
256
static public Enumeration getPortIdentifiers()
258
if(debug) System.out.println("static CommPortIdentifier:getPortIdentifiers()");
259
return new CommPortEnumerator();
261
/*------------------------------------------------------------------------------
268
------------------------------------------------------------------------------*/
269
public int getPortType()
271
if(debug) System.out.println("CommPortIdentifier:getPortType()");
274
/*------------------------------------------------------------------------------
281
------------------------------------------------------------------------------*/
282
public synchronized boolean isCurrentlyOwned()
284
if(debug) System.out.println("CommPortIdentifier:isCurrentlyOwned()");
287
/*------------------------------------------------------------------------------
294
------------------------------------------------------------------------------*/
295
public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException
297
if(debug) System.out.println("CommPortIdentifier:open(FileDescriptor)");
298
throw new UnsupportedCommOperationException();
300
private native String native_psmisc_report_owner(String PortName);
302
/*------------------------------------------------------------------------------
304
accept: application makeing the call and milliseconds to block
306
perform: open the port if possible
307
return: CommPort if successfull
308
exceptions: PortInUseException if in use.
310
------------------------------------------------------------------------------*/
311
private boolean HideOwnerEvents;
313
public synchronized CommPort open(String TheOwner, int i)
314
throws gnu.io.PortInUseException
316
if(debug) System.out.println("CommPortIdentifier:open("+TheOwner + ", " +i+")");
317
if (Available == false)
321
fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
326
catch ( InterruptedException e ) { }
329
if (Available == false)
331
throw new gnu.io.PortInUseException(getCurrentOwner());
335
commport = RXTXDriver.getCommPort(PortName,PortType);
341
fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED);
346
throw new gnu.io.PortInUseException(
347
native_psmisc_report_owner(PortName));
350
/*------------------------------------------------------------------------------
351
removePortOwnership()
357
------------------------------------------------------------------------------*/
358
public void removePortOwnershipListener(CommPortOwnershipListener c)
360
if(debug) System.out.println("CommPortIdentifier:removePortOwnershipListener()");
361
/* why is this called twice? */
362
if(ownershipListener != null)
363
ownershipListener.removeElement(c);
366
/*------------------------------------------------------------------------------
369
perform: clean up the Ownership information and send the event
373
------------------------------------------------------------------------------*/
374
synchronized void internalClosePort()
376
if(debug) System.out.println("CommPortIdentifier:internalClosePort()");
380
/* this tosses null pointer?? */
382
fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED);
384
/*------------------------------------------------------------------------------
391
------------------------------------------------------------------------------*/
392
void fireOwnershipEvent(int eventType)
394
if(debug) System.out.println("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )");
395
if (ownershipListener != null)
397
CommPortOwnershipListener c;
398
for ( Enumeration e = ownershipListener.elements();
400
c.ownershipChange(eventType))
401
c = (CommPortOwnershipListener) e.nextElement();