Debian Codegraph @

src/gnu/io/I2C.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 import java.io.*;
60 import java.util.*;
61 import java.lang.Math;
62
63 /**
64 * @author Trent Jarvi
65 * @version,
66 * @since JDK1.0
67 */
68
69
70 /**
71 * I2C
72 */
73 final class I2C extends I2CPort {
74
75 static
76 {
77 System.loadLibrary( "rxtxI2C" );
78 Initialize();
79 }
80
81
82 /** Initialize the native library */
83 private native static void Initialize();
84
85
86 /** Actual I2CPort wrapper class */
87
88
89 /** Open the named port */
90 public I2C( String name ) throws PortInUseException {
91 fd = open( name );
92 }
93 private native int open( String name ) throws PortInUseException;
94
95
96 /** File descriptor */
97 private int fd;
98
99 /** DSR flag **/
100 static boolean dsrFlag = false;
101
102 /** Output stream */
103 private final I2COutputStream out = new I2COutputStream();
104 public OutputStream getOutputStream() { return out; }
105
106
107 /** Input stream */
108 private final I2CInputStream in = new I2CInputStream();
109 public InputStream getInputStream() { return in; }
110
111
112
113
114 /** Set the I2CPort parameters */
115 public void setI2CPortParams( int b, int d, int s, int p )
116 throws UnsupportedCommOperationException
117 {
118 nativeSetI2CPortParams( b, d, s, p );
119 speed = b;
120 dataBits = d;
121 stopBits = s;
122 parity = p;
123 }
124
125 /** Set the native I2C port parameters */
126 private native void nativeSetI2CPortParams( int speed, int dataBits,
127 int stopBits, int parity ) throws UnsupportedCommOperationException;
128
129 /** Line speed in bits-per-second */
130 private int speed=9600;
131 public int getBaudRate() { return speed; }
132
133 /** Data bits port parameter */
134 private int dataBits=DATABITS_8;
135 public int getDataBits() { return dataBits; }
136
137 /** Stop bits port parameter */
138 private int stopBits=I2CPort.STOPBITS_1;
139 public int getStopBits() { return stopBits; }
140
141 /** Parity port parameter */
142 private int parity= I2CPort.PARITY_NONE;
143 public int getParity() { return parity; }
144
145
146 /** Flow control */
147 private int flowmode = I2CPort.FLOWCONTROL_NONE;
148 public void setFlowControlMode( int flowcontrol ) {
149 try { setflowcontrol( flowcontrol ); }
150 catch( IOException e ) {
151 e.printStackTrace();
152 return;
153 }
154 flowmode=flowcontrol;
155 }
156 public int getFlowControlMode() { return flowmode; }
157 native void setflowcontrol( int flowcontrol ) throws IOException;
158
159
160 /*
161 linux/drivers/char/n_hdlc.c? FIXME
162 [email protected]
163 */
164 /** Receive framing control
165 */
166 public void enableReceiveFraming( int f )
167 throws UnsupportedCommOperationException
168 {
169 throw new UnsupportedCommOperationException( "Not supported" );
170 }
171 public void disableReceiveFraming() {}
172 public boolean isReceiveFramingEnabled() { return false; }
173 public int getReceiveFramingByte() { return 0; }
174
175
176 /** Receive timeout control */
177 private int timeout = 0;
178
179 public native int NativegetReceiveTimeout();
180 public native boolean NativeisReceiveTimeoutEnabled();
181 public native void NativeEnableReceiveTimeoutThreshold(int time, int threshold,int InputBuffer);
182 public void disableReceiveTimeout(){
183 enableReceiveTimeout(0);
184 }
185 public void enableReceiveTimeout( int time ){
186 if( time >= 0 ) {
187 timeout = time;
188 NativeEnableReceiveTimeoutThreshold( time , threshold, InputBuffer );
189 }
190 else {
191 System.out.println("Invalid timeout");
192 }
193 }
194 public boolean isReceiveTimeoutEnabled(){
195 return(NativeisReceiveTimeoutEnabled());
196 }
197 public int getReceiveTimeout(){
198 return(NativegetReceiveTimeout( ));
199 }
200
201 /** Receive threshold control */
202
203 private int threshold = 0;
204
205 public void enableReceiveThreshold( int thresh ){
206 if(thresh >=0)
207 {
208 threshold=thresh;
209 NativeEnableReceiveTimeoutThreshold(timeout, threshold, InputBuffer);
210 }
211 else /* invalid thresh */
212 {
213 System.out.println("Invalid Threshold");
214 }
215 }
216 public void disableReceiveThreshold() {
217 enableReceiveThreshold(0);
218 }
219 public int getReceiveThreshold(){
220 return threshold;
221 }
222 public boolean isReceiveThresholdEnabled(){
223 return(threshold>0);
224 }
225
226 /** Input/output buffers */
227 /** FIXME I think this refers to
228 FOPEN(3)/SETBUF(3)/FREAD(3)/FCLOSE(3)
229 [email protected]
230
231 These are native stubs...
232 */
233 private int InputBuffer=0;
234 private int OutputBuffer=0;
235 public void setInputBufferSize( int size )
236 {
237 InputBuffer=size;
238 }
239 public int getInputBufferSize()
240 {
241 return(InputBuffer);
242 }
243 public void setOutputBufferSize( int size )
244 {
245 OutputBuffer=size;
246 }
247 public int getOutputBufferSize()
248 {
249 return(OutputBuffer);
250 }
251
252 /** Line status methods */
253 public native boolean isDTR();
254 public native void setDTR( boolean state );
255 public native void setRTS( boolean state );
256 private native void setDSR( boolean state );
257 public native boolean isCTS();
258 public native boolean isDSR();
259 public native boolean isCD();
260 public native boolean isRI();
261 public native boolean isRTS();
262
263
264 /** Write to the port */
265 public native void sendBreak( int duration );
266 private native void writeByte( int b ) throws IOException;
267 private native void writeArray( byte b[], int off, int len )
268 throws IOException;
269 private native void drain() throws IOException;
270
271
272 /** I2C read methods */
273 private native int nativeavailable() throws IOException;
274 private native int readByte() throws IOException;
275 private native int readArray( byte b[], int off, int len )
276 throws IOException;
277
278
279 /** I2C Port Event listener */
280 private I2CPortEventListener SPEventListener;
281
282 /** Thread to monitor data */
283 private MonitorThread monThread;
284
285 /** Process I2CPortEvents */
286 native void eventLoop();
287 private int dataAvailable=0;
288 public void sendEvent( int event, boolean state ) {
289 switch( event ) {
290 case I2CPortEvent.DATA_AVAILABLE:
291 dataAvailable=1;
292 if( monThread.Data ) break;
293 return;
294 case I2CPortEvent.OUTPUT_BUFFER_EMPTY:
295 if( monThread.Output ) break;
296 return;
297 /*
298 if( monThread.DSR ) break;
299 return;
300 if (isDSR())
301 {
302 if (!dsrFlag)
303 {
304 dsrFlag = true;
305 I2CPortEvent e = new I2CPortEvent(this, I2CPortEvent.DSR, !dsrFlag, dsrFlag );
306 }
307 }
308 else if (dsrFlag)
309 {
310 dsrFlag = false;
311 I2CPortEvent e = new I2CPortEvent(this, I2CPortEvent.DSR, !dsrFlag, dsrFlag );
312 }
313 */
314 case I2CPortEvent.CTS:
315 if( monThread.CTS ) break;
316 return;
317 case I2CPortEvent.DSR:
318 if( monThread.DSR ) break;
319 return;
320 case I2CPortEvent.RI:
321 if( monThread.RI ) break;
322 return;
323 case I2CPortEvent.CD:
324 if( monThread.CD ) break;
325 return;
326 case I2CPortEvent.OE:
327 if( monThread.OE ) break;
328 return;
329 case I2CPortEvent.PE:
330 if( monThread.PE ) break;
331 return;
332 case I2CPortEvent.FE:
333 if( monThread.FE ) break;
334 return;
335 case I2CPortEvent.BI:
336 if( monThread.BI ) break;
337 return;
338 default:
339 System.err.println("unknown event:"+event);
340 return;
341 }
342 I2CPortEvent e = new I2CPortEvent(this, event, !state, state );
343 if( SPEventListener != null ) SPEventListener.I2CEvent( e );
344 }
345
346 /** Add an event listener */
347 public void addEventListener( I2CPortEventListener lsnr )
348 throws TooManyListenersException
349 {
350 if( SPEventListener != null ) throw new TooManyListenersException();
351 SPEventListener = lsnr;
352 monThread = new MonitorThread();
353 monThread.start();
354 }
355 /** Remove the I2C port event listener */
356 public void removeEventListener() {
357 SPEventListener = null;
358 if( monThread != null ) {
359 monThread.interrupt();
360 monThread = null;
361 }
362 }
363
364 public void notifyOnDataAvailable( boolean enable ) { monThread.Data = enable; }
365
366 public void notifyOnOutputEmpty( boolean enable ) { monThread.Output = enable; }
367
368 public void notifyOnCTS( boolean enable ) { monThread.CTS = enable; }
369 public void notifyOnDSR( boolean enable ) { monThread.DSR = enable; }
370 public void notifyOnRingIndicator( boolean enable ) { monThread.RI = enable; }
371 public void notifyOnCarrierDetect( boolean enable ) { monThread.CD = enable; }
372 public void notifyOnOverrunError( boolean enable ) { monThread.OE = enable; }
373 public void notifyOnParityError( boolean enable ) { monThread.PE = enable; }
374 public void notifyOnFramingError( boolean enable ) { monThread.FE = enable; }
375 public void notifyOnBreakInterrupt( boolean enable ) { monThread.BI = enable; }
376
377
378 /** Close the port */
379 private native void nativeClose();
380 public void close() {
381 setDTR(false);
382 setDSR(false);
383 nativeClose();
384 super.close();
385 fd = 0;
386 }
387
388
389 /** Finalize the port */
390 protected void finalize() {
391 if( fd > 0 ) close();
392 }
393
394
395 /** Inner class for I2COutputStream */
396 class I2COutputStream extends OutputStream {
397 public void write( int b ) throws IOException {
398 writeByte( b );
399 }
400 public void write( byte b[] ) throws IOException {
401 writeArray( b, 0, b.length );
402 }
403 public void write( byte b[], int off, int len ) throws IOException {
404 writeArray( b, off, len );
405 }
406 public void flush() throws IOException {
407 drain();
408 }
409 }
410
411 /** Inner class for I2CInputStream */
412 class I2CInputStream extends InputStream {
413 public int read() throws IOException {
414 dataAvailable=0;
415 return readByte();
416 }
417 public int read( byte b[] ) throws IOException
418 {
419 return read ( b, 0, b.length);
420 }
421 public int read( byte b[], int off, int len ) throws IOException
422 {
423 dataAvailable=0;
424 int i=0, Minimum=0;
425 int intArray[] =
426 {
427 b.length,
428 InputBuffer,
429 len
430 };
431 /*
432 find the lowest nonzero value
433 timeout and threshold are handled on the native side
434 see NativeEnableReceiveTimeoutThreshold in
435 I2CImp.c
436 */
437 while(intArray[i]==0 && i < intArray.length) i++;
438 Minimum=intArray[i];
439 while( i < intArray.length )
440 {
441 if(intArray[i] > 0 )
442 {
443 Minimum=Math.min(Minimum,intArray[i]);
444 }
445 i++;
446 }
447 Minimum=Math.min(Minimum,threshold);
448 if(Minimum == 0) Minimum=1;
449 int Available=available();
450 int Ret = readArray( b, off, Minimum);
451 return Ret;
452 }
453 public int available() throws IOException {
454 return nativeavailable();
455 }
456 }
457 class MonitorThread extends Thread {
458 /** Note: these have to be separate boolean flags because the
459 I2CPortEvent constants are NOT bit-flags, they are just
460 defined as integers from 1 to 10 -DPL */
461 private boolean CTS=false;
462 private boolean DSR=false;
463 private boolean RI=false;
464 private boolean CD=false;
465 private boolean OE=false;
466 private boolean PE=false;
467 private boolean FE=false;
468 private boolean BI=false;
469 private boolean Data=false;
470 private boolean Output=false;
471 MonitorThread() { }
472 public void run() {
473 eventLoop();
474 }
475 }
476 }