~ubuntu-branches/ubuntu/edgy/rxtx/edgy-201105201527

« back to all changes in this revision

Viewing changes to CNI/CommPortIdentifier.java

  • Committer: Bazaar Package Importer
  • Author(s): Mario Joussen
  • Date: 2006-03-01 18:56:52 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060301185652-ri9941gi01goklvz
Tags: 2.1.7-2
Fixed stupid bug in clean target.
(closes: Bug#354859)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
|   rxtx is a native interface to serial ports in java.
 
3
|   Copyright 1997-2004 by Trent Jarvi taj@www.linux.org.uk
 
4
|
 
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.
 
9
|
 
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.
 
14
|
 
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
--------------------------------------------------------------------------*/
 
19
package  gnu.io;
 
20
 
 
21
import  java.io.FileDescriptor;
 
22
import  java.util.Vector;
 
23
import  java.util.Enumeration;
 
24
 
 
25
/**
 
26
* @author Trent Jarvi
 
27
* @version %I%, %G%
 
28
* @since JDK1.0
 
29
*/
 
30
 
 
31
public class CommPortIdentifier extends Object /* extends Vector? */
 
32
{
 
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;    
 
40
        private String Owner;    
 
41
        private CommPort commport;
 
42
        private CommDriver RXTXDriver;
 
43
        static CommPortIdentifier   CommPortIndex;
 
44
        CommPortIdentifier next;
 
45
        private int PortType;
 
46
        private final static boolean debug = false;
 
47
        static Object Sync;
 
48
        Vector ownershipListener;
 
49
 
 
50
 
 
51
 
 
52
/*------------------------------------------------------------------------------
 
53
        static {}   aka initialization
 
54
        accept:       -
 
55
        perform:      load the rxtx driver
 
56
        return:       -
 
57
        exceptions:   Throwable
 
58
        comments:     static block to initialize the class
 
59
------------------------------------------------------------------------------*/
 
60
        // initialization only done once....
 
61
        static 
 
62
        {
 
63
                if(debug) System.out.println("CommPortIdentifier:static initialization()");
 
64
                Sync = new Object();
 
65
                try 
 
66
                {
 
67
                        CommDriver RXTXDriver = (CommDriver) Class.forName("gnu.io.RXTXCommDriver").newInstance();
 
68
                        RXTXDriver.initialize();
 
69
                } 
 
70
                catch (Throwable e) 
 
71
                {
 
72
                        System.err.println(e + " thrown while loading " + "gnu.io.RXTXCommDriver");
 
73
                }
 
74
 
 
75
                String OS;
 
76
 
 
77
                OS = System.getProperty("os.name");
 
78
                if(OS.toLowerCase().indexOf("linux") == -1)
 
79
                {
 
80
                        if (debug)
 
81
                                System.out.println("Have not implemented native_psmisc_report_owner(PortName)); in CommPortIdentifier");
 
82
                }
 
83
/*
 
84
                System.loadLibrary( "rxtxSerial" );
 
85
*/
 
86
        }
 
87
        CommPortIdentifier ( String pn, CommPort cp, int pt, CommDriver driver) 
 
88
        {
 
89
                PortName        = pn;
 
90
                commport        = cp;
 
91
                PortType        = pt;
 
92
                next            = null;
 
93
                RXTXDriver      = driver;
 
94
 
 
95
        }
 
96
 
 
97
/*------------------------------------------------------------------------------
 
98
        addPortName()
 
99
        accept:         Name of the port s, Port type, 
 
100
                        reverence to RXTXCommDriver.
 
101
        perform:        place a new CommPortIdentifier in the linked list
 
102
        return:         none.
 
103
        exceptions:     none.
 
104
        comments:
 
105
------------------------------------------------------------------------------*/
 
106
        public static void addPortName(String s, int type, CommDriver c) 
 
107
        { 
 
108
 
 
109
                if(debug) System.out.println("CommPortIdentifier:addPortName("+s+")");
 
110
                AddIdentifierToList(new CommPortIdentifier(s, null, type, c));
 
111
        }
 
112
/*------------------------------------------------------------------------------
 
113
        AddIdentifierToList()
 
114
        accept:        The cpi to add to the list. 
 
115
        perform:        
 
116
        return:         
 
117
        exceptions:    
 
118
        comments:
 
119
------------------------------------------------------------------------------*/
 
120
        private static void AddIdentifierToList( CommPortIdentifier cpi)
 
121
        {
 
122
                if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList()");
 
123
                synchronized (Sync) 
 
124
                {
 
125
                        if (CommPortIndex == null) 
 
126
                        {
 
127
                                CommPortIndex = cpi;
 
128
                                if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() null");
 
129
                        }
 
130
                        else
 
131
                        { 
 
132
                                CommPortIdentifier index  = CommPortIndex; 
 
133
                                while (index.next != null)
 
134
                                {
 
135
                                        index = index.next;
 
136
                                        if(debug) System.out.println("CommPortIdentifier:AddIdentifierToList() index.next");
 
137
                                }
 
138
                                index.next = cpi;
 
139
                        } 
 
140
                }
 
141
        }
 
142
/*------------------------------------------------------------------------------
 
143
        addPortOwnershipListener()
 
144
        accept:
 
145
        perform:
 
146
        return:
 
147
        exceptions:
 
148
        comments:   
 
149
------------------------------------------------------------------------------*/
 
150
        public void addPortOwnershipListener(CommPortOwnershipListener c) 
 
151
        { 
 
152
                if(debug) System.out.println("CommPortIdentifier:addPortOwnershipListener()");
 
153
 
 
154
                /*  is the Vector instantiated? */
 
155
 
 
156
                if( ownershipListener == null )
 
157
                {
 
158
                        ownershipListener = new Vector();
 
159
                }
 
160
 
 
161
                /* is the ownership listener already in the list? */
 
162
 
 
163
                if ( ownershipListener.contains(c) == false)
 
164
                {
 
165
                        ownershipListener.addElement(c);
 
166
                }
 
167
        }
 
168
/*------------------------------------------------------------------------------
 
169
        getCurrentOwner()
 
170
        accept:
 
171
        perform:
 
172
        return:
 
173
        exceptions:
 
174
        comments:    
 
175
------------------------------------------------------------------------------*/
 
176
        public String getCurrentOwner() 
 
177
        { 
 
178
                if(debug) System.out.println("CommPortIdentifier:getCurrentOwner()");
 
179
                return( Owner );
 
180
        }
 
181
/*------------------------------------------------------------------------------
 
182
        getName()
 
183
        accept:
 
184
        perform:
 
185
        return:
 
186
        exceptions:
 
187
        comments:
 
188
------------------------------------------------------------------------------*/
 
189
        public String getName() 
 
190
        { 
 
191
                if(debug) System.out.println("CommPortIdentifier:getName()");
 
192
                return( PortName );
 
193
        }
 
194
/*------------------------------------------------------------------------------
 
195
        getPortIdentifier()
 
196
        accept:
 
197
        perform:
 
198
        return:
 
199
        exceptions:
 
200
        comments:   
 
201
------------------------------------------------------------------------------*/
 
202
        static public CommPortIdentifier getPortIdentifier(String s) throws NoSuchPortException 
 
203
        { 
 
204
                if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(" + s +")");
 
205
                CommPortIdentifier index = CommPortIndex;
 
206
 
 
207
                synchronized (Sync) 
 
208
                {
 
209
                        while (index != null) 
 
210
                        {
 
211
                                if (index.PortName.equals(s)) break;
 
212
                                index = index.next;
 
213
                        }
 
214
                }
 
215
                if (index != null) return index;
 
216
                else
 
217
                {
 
218
                        if ( debug )
 
219
                                System.out.println("not found!" + s);
 
220
                        throw new NoSuchPortException();
 
221
                }
 
222
        }
 
223
/*------------------------------------------------------------------------------
 
224
        getPortIdentifier()
 
225
        accept:
 
226
        perform:
 
227
        return:
 
228
        exceptions:
 
229
        comments:    
 
230
------------------------------------------------------------------------------*/
 
231
        static public CommPortIdentifier getPortIdentifier(CommPort p) 
 
232
                throws NoSuchPortException      
 
233
        { 
 
234
                if(debug) System.out.println("CommPortIdentifier:getPortIdentifier(CommPort)");
 
235
                CommPortIdentifier c = CommPortIndex;
 
236
                synchronized( Sync )
 
237
                {
 
238
                        while ( c != null && c.commport != p )
 
239
                                c = c.next;
 
240
                }
 
241
                if ( c != null )
 
242
                        return (c);
 
243
 
 
244
                if ( debug )
 
245
                        System.out.println("not found!" + p.getName());
 
246
                throw new NoSuchPortException();
 
247
        }
 
248
/*------------------------------------------------------------------------------
 
249
        getPortIdentifiers()
 
250
        accept:
 
251
        perform:
 
252
        return:
 
253
        exceptions:
 
254
        comments:
 
255
------------------------------------------------------------------------------*/
 
256
        static public Enumeration getPortIdentifiers() 
 
257
        { 
 
258
                if(debug) System.out.println("static CommPortIdentifier:getPortIdentifiers()");
 
259
                return new CommPortEnumerator();
 
260
        }
 
261
/*------------------------------------------------------------------------------
 
262
        getPortType()
 
263
        accept:
 
264
        perform:
 
265
        return:
 
266
        exceptions:
 
267
        comments:
 
268
------------------------------------------------------------------------------*/
 
269
        public int getPortType() 
 
270
        { 
 
271
                if(debug) System.out.println("CommPortIdentifier:getPortType()");
 
272
                return( PortType );
 
273
        }
 
274
/*------------------------------------------------------------------------------
 
275
        isCurrentlyOwned()
 
276
        accept:
 
277
        perform:
 
278
        return:
 
279
        exceptions:
 
280
        comments:    
 
281
------------------------------------------------------------------------------*/
 
282
        public synchronized boolean isCurrentlyOwned() 
 
283
        { 
 
284
                if(debug) System.out.println("CommPortIdentifier:isCurrentlyOwned()");
 
285
                return(!Available);
 
286
        }
 
287
/*------------------------------------------------------------------------------
 
288
        open()
 
289
        accept:
 
290
        perform:
 
291
        return:
 
292
        exceptions:
 
293
        comments:
 
294
------------------------------------------------------------------------------*/
 
295
        public synchronized CommPort open(FileDescriptor f) throws UnsupportedCommOperationException 
 
296
        { 
 
297
                if(debug) System.out.println("CommPortIdentifier:open(FileDescriptor)");
 
298
                throw new UnsupportedCommOperationException();
 
299
        }
 
300
        private native String native_psmisc_report_owner(String PortName);
 
301
 
 
302
/*------------------------------------------------------------------------------
 
303
        open()
 
304
        accept:      application makeing the call and milliseconds to block
 
305
                     during open.
 
306
        perform:     open the port if possible
 
307
        return:      CommPort if successfull
 
308
        exceptions:  PortInUseException if in use.
 
309
        comments:
 
310
------------------------------------------------------------------------------*/
 
311
        private boolean HideOwnerEvents;
 
312
 
 
313
        public synchronized CommPort open(String TheOwner, int i) 
 
314
                throws gnu.io.PortInUseException 
 
315
        { 
 
316
                if(debug) System.out.println("CommPortIdentifier:open("+TheOwner + ", " +i+")");
 
317
                if (Available == false)
 
318
                {
 
319
                        synchronized (Sync)
 
320
                        {
 
321
                                fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED);
 
322
                                try
 
323
                                {
 
324
                                        wait(i);
 
325
                                }
 
326
                                catch ( InterruptedException e ) { }
 
327
                        }
 
328
                }
 
329
                if (Available == false)
 
330
                {
 
331
                        throw new gnu.io.PortInUseException(getCurrentOwner());
 
332
                }
 
333
                if(commport == null)
 
334
                {
 
335
                        commport = RXTXDriver.getCommPort(PortName,PortType);
 
336
                }
 
337
                if(commport != null)
 
338
                {
 
339
                        Owner = TheOwner;
 
340
                        Available=false;
 
341
                        fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED);
 
342
                        return commport;
 
343
                }
 
344
                else
 
345
                {
 
346
                        throw new gnu.io.PortInUseException(
 
347
                                        native_psmisc_report_owner(PortName));
 
348
                }
 
349
        }
 
350
/*------------------------------------------------------------------------------
 
351
        removePortOwnership()
 
352
        accept:
 
353
        perform:
 
354
        return:
 
355
        exceptions:
 
356
        comments:
 
357
------------------------------------------------------------------------------*/
 
358
        public void removePortOwnershipListener(CommPortOwnershipListener c) 
 
359
        { 
 
360
                if(debug) System.out.println("CommPortIdentifier:removePortOwnershipListener()");
 
361
                /* why is this called twice? */
 
362
                if(ownershipListener != null)
 
363
                        ownershipListener.removeElement(c);
 
364
        }
 
365
 
 
366
/*------------------------------------------------------------------------------
 
367
        internalClosePort()
 
368
        accept:       None
 
369
        perform:      clean up the Ownership information and send the event
 
370
        return:       None
 
371
        exceptions:   None
 
372
        comments:     None
 
373
------------------------------------------------------------------------------*/
 
374
        synchronized void internalClosePort() 
 
375
        {
 
376
                if(debug) System.out.println("CommPortIdentifier:internalClosePort()");
 
377
                Owner = null;
 
378
                Available = true;
 
379
                commport = null;
 
380
                /*  this tosses null pointer?? */
 
381
                notifyAll();
 
382
                fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED);
 
383
        }
 
384
/*------------------------------------------------------------------------------
 
385
        fireOwnershipEvent()
 
386
        accept:
 
387
        perform:
 
388
        return:
 
389
        exceptions:
 
390
        comments:
 
391
------------------------------------------------------------------------------*/
 
392
        void fireOwnershipEvent(int eventType)
 
393
        {
 
394
                if(debug) System.out.println("CommPortIdentifier:fireOwnershipEvent( " + eventType + " )");
 
395
                if (ownershipListener != null)
 
396
                {
 
397
                        CommPortOwnershipListener c;
 
398
                        for ( Enumeration e = ownershipListener.elements();
 
399
                                e.hasMoreElements(); 
 
400
                                c.ownershipChange(eventType))
 
401
                                c = (CommPortOwnershipListener) e.nextElement();
 
402
                }
 
403
        }
 
404
}
 
405