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