| 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 |
#if defined(__MWERKS__) /* dima */ |
| 59 |
#include "I2C.h" /* dima */ |
| 60 |
#else /* dima */ |
| 61 |
#include "config.h" |
| 62 |
#include "gnu_io_I2C.h" |
| 63 |
#endif /* dima */ |
| 64 |
#include <time.h> |
| 65 |
#include <unistd.h> |
| 66 |
#include <stdio.h> |
| 67 |
#include <string.h> |
| 68 |
#include <stdlib.h> |
| 69 |
#include <errno.h> |
| 70 |
#ifndef WIN32 |
| 71 |
#include <sys/ioctl.h> |
| 72 |
#include <sys/param.h> |
| 73 |
#include <sys/utsname.h> |
| 74 |
#ifdef HAVE_TERMIOS_H |
| 75 |
# include <termios.h> |
| 76 |
#endif |
| 77 |
#ifdef HAVE_SYS_SIGNAL_H |
| 78 |
# include <sys/signal.h> |
| 79 |
#endif |
| 80 |
#else |
| 81 |
# include <win32termios.h> |
| 82 |
#endif |
| 83 |
#include <sys/types.h> |
| 84 |
#include <sys/stat.h> |
| 85 |
#include <sys/time.h> |
| 86 |
#ifdef HAVE_SYS_FCNTL_H |
| 87 |
# include <sys/fcntl.h> |
| 88 |
#endif |
| 89 |
#ifdef HAVE_SYS_FILE_H |
| 90 |
# include <sys/file.h> |
| 91 |
#endif |
| 92 |
|
| 93 |
#if defined(__linux__) |
| 94 |
# include <linux/types.h> /* fix for linux-2.3.4? kernels */ |
| 95 |
# include <linux/serial.h> |
| 96 |
# include <linux/version.h> |
| 97 |
#endif |
| 98 |
|
| 99 |
extern int errno; |
| 100 |
#include "I2CImp.h" |
| 101 |
/* #define DEBUG_TIMEOUT */ |
| 102 |
|
| 103 |
/*---------------------------------------------------------- |
| 104 |
I2CPort.Initialize |
| 105 |
|
| 106 |
accept: none |
| 107 |
perform: Initialize the native library |
| 108 |
return: none |
| 109 |
----------------------------------------------------------*/ |
| 110 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_Initialize( |
| 111 |
JNIEnv *env, |
| 112 |
jclass jclazz |
| 113 |
) |
| 114 |
{ |
| 115 |
#ifndef WIN32 |
| 116 |
struct utsname name; |
| 117 |
/* This bit of code checks to see if there is a signal handler installed |
| 118 |
for SIGIO, and installs SIG_IGN if there is not. This is necessary |
| 119 |
for the native threads jdk, but we don't want to do it with green |
| 120 |
threads, because it slows things down. Go figure. */ |
| 121 |
|
| 122 |
/* POSIX signal handling functions */ |
| 123 |
#if !defined(__FreeBSD___) |
| 124 |
struct sigaction handler; |
| 125 |
sigaction( SIGIO, NULL, &handler ); |
| 126 |
if( !handler.sa_handler ) signal( SIGIO, SIG_IGN ); |
| 127 |
#endif /* !__FreeBSD__ */ |
| 128 |
#if defined(__linux__) |
| 129 |
/* Lets let people who upgraded kernels know they may have problems */ |
| 130 |
if (uname (&name) == -1) |
| 131 |
{ |
| 132 |
fprintf(stderr,"RXTX WARNING: cannot get system name\n"); |
| 133 |
return; |
| 134 |
} |
| 135 |
if(strcmp(name.release,UTS_RELEASE)!=0) |
| 136 |
{ |
| 137 |
fprintf(stderr, "\n\n\nRXTX WARNING: This library was compiled to run with OS release %s and you are currently running OS release %s. In some cases this can be a problem. Try recompiling RXTX if you notice strange behavior. If you just compiled RXTX make sure /usr/include/linux is a symbolic link to the include files that came with the kernel source and not an older copy.\n\n\npress enter to continue\n",UTS_RELEASE,name.release); |
| 138 |
getchar(); |
| 139 |
} |
| 140 |
#endif /* __linux__ */ |
| 141 |
#endif /* WIN32 */ |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/*---------------------------------------------------------- |
| 146 |
I2CPort.open |
| 147 |
|
| 148 |
accept: The device to open. ie "/dev/ttyS0" |
| 149 |
perform: open the device, set the termios struct to sane settings and |
| 150 |
return the filedescriptor |
| 151 |
return: fd |
| 152 |
exceptions: IOExcepiton |
| 153 |
comments: Very often people complain about not being able to get past |
| 154 |
this function and it turns out to be permissions on the |
| 155 |
device file or bios has the device disabled. |
| 156 |
----------------------------------------------------------*/ |
| 157 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_open( |
| 158 |
JNIEnv *env, |
| 159 |
jobject jobj, |
| 160 |
jstring jstr |
| 161 |
) |
| 162 |
{ |
| 163 |
struct termios ttyset; |
| 164 |
int fd; |
| 165 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 166 |
|
| 167 |
do { |
| 168 |
fd=open (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 169 |
} while (fd < 0 && errno==EINTR); |
| 170 |
(*env)->ReleaseStringUTFChars( env, jstr, NULL ); |
| 171 |
if( fd < 0 ) goto fail; |
| 172 |
|
| 173 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 174 |
ttyset.c_iflag = INPCK; |
| 175 |
ttyset.c_lflag = 0; |
| 176 |
ttyset.c_oflag = 0; |
| 177 |
ttyset.c_cflag = CREAD | CS8 | CLOCAL; |
| 178 |
ttyset.c_cc[ VMIN ] = 0; |
| 179 |
ttyset.c_cc[ VTIME ] = 0; |
| 180 |
|
| 181 |
#ifdef __FreeBSD__ |
| 182 |
if( cfsetspeed( &ttyset, B9600 ) < 0 ) goto fail; |
| 183 |
#else |
| 184 |
if( cfsetispeed( &ttyset, B9600 ) < 0 ) goto fail; |
| 185 |
if( cfsetospeed( &ttyset, B9600 ) < 0 ) goto fail; |
| 186 |
#endif |
| 187 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 188 |
|
| 189 |
#ifndef WIN32 |
| 190 |
fcntl( fd, F_SETOWN, getpid() ); |
| 191 |
fcntl( fd, F_SETFL, FASYNC ); |
| 192 |
#endif /* WIN32 */ |
| 193 |
|
| 194 |
return (jint)fd; |
| 195 |
|
| 196 |
fail: |
| 197 |
throw_java_exception( env, IO_EXCEPTION, "open", strerror( errno ) ); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
/*---------------------------------------------------------- |
| 202 |
I2CPort.nativeClose |
| 203 |
|
| 204 |
accept: none |
| 205 |
perform: get the fd from the java end and close it |
| 206 |
return: none |
| 207 |
exceptions: none |
| 208 |
----------------------------------------------------------*/ |
| 209 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_nativeClose( JNIEnv *env, |
| 210 |
jobject jobj ) |
| 211 |
{ |
| 212 |
int result; |
| 213 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 214 |
|
| 215 |
do { |
| 216 |
result=close (fd); |
| 217 |
} while (result < 0 && errno==EINTR); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
/*---------------------------------------------------------- |
| 222 |
I2CPort.nativeSetI2CPortParams |
| 223 |
|
| 224 |
accept: speed, data bits, stop bits, parity |
| 225 |
perform: set the i2c port parameters |
| 226 |
return: void |
| 227 |
exceptions: UnsupportedCommOperationException |
| 228 |
----------------------------------------------------------*/ |
| 229 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_nativeSetI2CPortParams( |
| 230 |
JNIEnv *env, jobject jobj, jint speed, jint dataBits, jint stopBits, |
| 231 |
jint parity ) |
| 232 |
{ |
| 233 |
struct termios ttyset; |
| 234 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 235 |
int cspeed = translate_speed( env, speed ); |
| 236 |
if( !cspeed ) return; |
| 237 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 238 |
if( !translate_data_bits( env, (int *)&(ttyset.c_cflag), dataBits ) ) return; /* dima darwin defime c_cflag as unsigned long */ |
| 239 |
if( !translate_stop_bits( env, (int *)&(ttyset.c_cflag), stopBits ) ) return; /* dima darwin defime c_cflag as unsigned long */ |
| 240 |
if( !translate_parity( env, (int *)&(ttyset.c_cflag), parity ) ) return;/* dima darwin defime c_cflag as unsigned long */ |
| 241 |
#ifdef __FreeBSD__ |
| 242 |
if( cfsetspeed( &ttyset, cspeed ) < 0 ) goto fail; |
| 243 |
#else |
| 244 |
if( cfsetispeed( &ttyset, cspeed ) < 0 ) goto fail; |
| 245 |
if( cfsetospeed( &ttyset, cspeed ) < 0 ) goto fail; |
| 246 |
#endif |
| 247 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 248 |
/* dump_termios("set",*ttyset); */ |
| 249 |
return; |
| 250 |
|
| 251 |
fail: |
| 252 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 253 |
"nativeSetI2CPortParams", strerror( errno ) ); |
| 254 |
} |
| 255 |
|
| 256 |
/*---------------------------------------------------------- |
| 257 |
translate_speed |
| 258 |
|
| 259 |
accept: speed in bits-per-second |
| 260 |
perform: convert bits-per-second to a speed_t constant |
| 261 |
return: speed_t constant |
| 262 |
exceptions: UnsupportedCommOperationException |
| 263 |
comments: Only the lowest level code should know about |
| 264 |
the magic constants. |
| 265 |
----------------------------------------------------------*/ |
| 266 |
int translate_speed( JNIEnv *env, jint speed ) |
| 267 |
{ |
| 268 |
switch( speed ) { |
| 269 |
case 0: return B0; |
| 270 |
case 50: return B50; |
| 271 |
case 75: return B75; |
| 272 |
case 110: return B110; |
| 273 |
case 134: return B134; |
| 274 |
case 150: return B150; |
| 275 |
case 200: return B200; |
| 276 |
case 300: return B300; |
| 277 |
case 600: return B600; |
| 278 |
case 1200: return B1200; |
| 279 |
case 1800: return B1800; |
| 280 |
case 2400: return B2400; |
| 281 |
case 4800: return B4800; |
| 282 |
case 9600: return B9600; |
| 283 |
case 19200: return B19200; |
| 284 |
case 38400: return B38400; |
| 285 |
case 57600: return B57600; |
| 286 |
case 115200: return B115200; |
| 287 |
case 230400: return B230400; |
| 288 |
#ifdef B460800 |
| 289 |
case 460800: return B460800; |
| 290 |
#endif |
| 291 |
} |
| 292 |
|
| 293 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 294 |
"translate_speed", "speed" ); |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
/*---------------------------------------------------------- |
| 299 |
translate_data_bits |
| 300 |
|
| 301 |
accept: gnu.io.I2CPort.DATABITS_* constant |
| 302 |
perform: set proper termios c_cflag bits |
| 303 |
return: 1 if successful |
| 304 |
0 if an exception is thrown |
| 305 |
exceptions: UnsupportedCommOperationException |
| 306 |
----------------------------------------------------------*/ |
| 307 |
int translate_data_bits( JNIEnv *env, int *cflag, jint dataBits ) |
| 308 |
{ |
| 309 |
int temp = (*cflag) & ~CSIZE; |
| 310 |
|
| 311 |
switch( dataBits ) { |
| 312 |
case DATABITS_5: |
| 313 |
(*cflag) = temp | CS5; |
| 314 |
return 1; |
| 315 |
case DATABITS_6: |
| 316 |
(*cflag) = temp | CS6; |
| 317 |
return 1; |
| 318 |
case DATABITS_7: |
| 319 |
(*cflag) = temp | CS7; |
| 320 |
return 1; |
| 321 |
case DATABITS_8: |
| 322 |
(*cflag) = temp | CS8; |
| 323 |
return 1; |
| 324 |
} |
| 325 |
|
| 326 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 327 |
"translate_data_bits", "data bits" ); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
/*---------------------------------------------------------- |
| 332 |
translate_stop_bits |
| 333 |
|
| 334 |
accept: gnu.io.I2CPort.STOPBITS_* constant |
| 335 |
perform: set proper termios c_cflag bits |
| 336 |
return: 1 if successful |
| 337 |
0 if an exception is thrown |
| 338 |
exceptions: UnsupportedCommOperationException |
| 339 |
comments: If you specify 5 data bits and 2 stop bits, the port will |
| 340 |
allegedly use 1.5 stop bits. Does anyone care? |
| 341 |
----------------------------------------------------------*/ |
| 342 |
int translate_stop_bits( JNIEnv *env, int *cflag, jint stopBits ) |
| 343 |
{ |
| 344 |
switch( stopBits ) { |
| 345 |
case STOPBITS_1: |
| 346 |
(*cflag) &= ~CSTOPB; |
| 347 |
return 1; |
| 348 |
case STOPBITS_2: |
| 349 |
(*cflag) |= CSTOPB; |
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 354 |
"translate_stop_bits", "stop bits" ); |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
/*---------------------------------------------------------- |
| 359 |
translate_parity |
| 360 |
|
| 361 |
accept: gnu.io.I2CPort.PARITY_* constant |
| 362 |
perform: set proper termios c_cflag bits |
| 363 |
return: 1 if successful |
| 364 |
0 if an exception is thrown |
| 365 |
exceptions: UnsupportedCommOperationException |
| 366 |
comments: The CMSPAR bit should be used for 'mark' and 'space' parity, |
| 367 |
but it's not in glibc's includes. Oh well, rarely used anyway. |
| 368 |
----------------------------------------------------------*/ |
| 369 |
int translate_parity( JNIEnv *env, int *cflag, jint parity ) |
| 370 |
{ |
| 371 |
(*cflag) &= ~(PARENB | PARODD); |
| 372 |
switch( parity ) { |
| 373 |
case PARITY_NONE: |
| 374 |
return 1; |
| 375 |
#ifdef CMSPAR |
| 376 |
case PARITY_EVEN: |
| 377 |
(*cflag) |= PARENB; |
| 378 |
return 1; |
| 379 |
case PARITY_ODD: |
| 380 |
(*cflag) |= PARENB | PARODD; |
| 381 |
return 1; |
| 382 |
case PARITY_MARK: |
| 383 |
(*cflag) |= PARENB | PARODD | CMSPAR; |
| 384 |
return 1; |
| 385 |
case PARITY_SPACE: |
| 386 |
(*cflag) |= PARENB | CMSPAR; |
| 387 |
return 1; |
| 388 |
#else |
| 389 |
case PARITY_EVEN: |
| 390 |
(*cflag) |= PARENB; |
| 391 |
return 1; |
| 392 |
case PARITY_ODD: |
| 393 |
(*cflag) |= PARENB | PARODD; |
| 394 |
return 1; |
| 395 |
#endif |
| 396 |
} |
| 397 |
|
| 398 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 399 |
"translate_parity", "parity" ); |
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/*---------------------------------------------------------- |
| 405 |
I2CPort.writeByte |
| 406 |
|
| 407 |
accept: byte to write (passed as int) |
| 408 |
perform: write a single byte to the port |
| 409 |
return: none |
| 410 |
exceptions: IOException |
| 411 |
----------------------------------------------------------*/ |
| 412 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_writeByte( JNIEnv *env, |
| 413 |
jobject jobj, jint ji ) |
| 414 |
{ |
| 415 |
unsigned char byte = (unsigned char)ji; |
| 416 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 417 |
int result; |
| 418 |
|
| 419 |
do { |
| 420 |
result=write (fd, &byte, sizeof(unsigned char)); |
| 421 |
} while (result < 0 && errno==EINTR); |
| 422 |
if(result >= 0) |
| 423 |
return; |
| 424 |
throw_java_exception( env, IO_EXCEPTION, "writeByte", |
| 425 |
strerror( errno ) ); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
/*---------------------------------------------------------- |
| 430 |
I2CPort.writeArray |
| 431 |
|
| 432 |
accept: jbarray: bytes used for writing |
| 433 |
offset: offset in array to start writing |
| 434 |
count: Number of bytes to write |
| 435 |
perform: write length bytes of jbarray |
| 436 |
return: none |
| 437 |
exceptions: IOException |
| 438 |
----------------------------------------------------------*/ |
| 439 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_writeArray( JNIEnv *env, |
| 440 |
jobject jobj, jbyteArray jbarray, jint offset, jint count ) |
| 441 |
{ |
| 442 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 443 |
int result=0,total=0,i; |
| 444 |
|
| 445 |
unsigned char *bytes = (unsigned char *)malloc( count ); |
| 446 |
|
| 447 |
jbyte *body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 448 |
for( i = 0; i < count; i++ ) bytes[ i ] = body[ i + offset ]; |
| 449 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 450 |
do { |
| 451 |
result=write (fd, bytes + total + offset, count - total); /* dima */ |
| 452 |
if(result >0){ |
| 453 |
total += result; |
| 454 |
} |
| 455 |
} while ((total<count)||(result < 0 && errno==EINTR)); |
| 456 |
free( bytes ); |
| 457 |
if( result < 0 ) throw_java_exception( env, IO_EXCEPTION, |
| 458 |
"writeArray", strerror( errno ) ); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
/*---------------------------------------------------------- |
| 463 |
I2CPort.drain |
| 464 |
|
| 465 |
accept: none |
| 466 |
perform: wait until all data is transmitted |
| 467 |
return: none |
| 468 |
exceptions: IOException |
| 469 |
comments: java.io.OutputStream.flush() is equivalent to tcdrain, |
| 470 |
not tcflush, which throws away unsent bytes |
| 471 |
|
| 472 |
count logic added to avoid infinite loops when EINTR is |
| 473 |
true... Thread.yeild() was suggested. |
| 474 |
----------------------------------------------------------*/ |
| 475 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_drain( JNIEnv *env, |
| 476 |
jobject jobj ) |
| 477 |
{ |
| 478 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 479 |
int result, count=0; |
| 480 |
|
| 481 |
do { |
| 482 |
result=tcdrain (fd); |
| 483 |
count++; |
| 484 |
} while (result && errno==EINTR && count <5); |
| 485 |
|
| 486 |
if( result ) throw_java_exception( env, IO_EXCEPTION, "drain", |
| 487 |
strerror( errno ) ); |
| 488 |
} |
| 489 |
|
| 490 |
/*---------------------------------------------------------- |
| 491 |
I2CPort.sendBreak |
| 492 |
|
| 493 |
accept: duration in milliseconds. |
| 494 |
perform: send break for actual time. not less than 0.25 seconds. |
| 495 |
exceptions: none |
| 496 |
comments: not very precise |
| 497 |
----------------------------------------------------------*/ |
| 498 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_sendBreak( JNIEnv *env, |
| 499 |
jobject jobj, jint duration ) |
| 500 |
{ |
| 501 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 502 |
tcsendbreak( fd, (int)( duration / 250 ) ); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
/*---------------------------------------------------------- |
| 507 |
I2CPort.NativegetReceiveTimeout |
| 508 |
|
| 509 |
accept: none |
| 510 |
perform: get termios.c_cc[VTIME] |
| 511 |
return: VTIME |
| 512 |
comments: see NativeEnableReceiveTimeoutThreshold |
| 513 |
----------------------------------------------------------*/ |
| 514 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_NativegetReceiveTimeout( |
| 515 |
JNIEnv *env, |
| 516 |
jobject jobj |
| 517 |
) |
| 518 |
{ |
| 519 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 520 |
struct termios ttyset; |
| 521 |
|
| 522 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 523 |
return(ttyset.c_cc[ VTIME ] * 100); |
| 524 |
fail: |
| 525 |
throw_java_exception( env, IO_EXCEPTION, "getReceiveTimeout", strerror( errno ) ); |
| 526 |
return -1; |
| 527 |
} |
| 528 |
|
| 529 |
/*---------------------------------------------------------- |
| 530 |
I2CPort.NativeisReceiveTimeoutEnabled |
| 531 |
|
| 532 |
accept: none |
| 533 |
perform: determine if VTIME is none 0 |
| 534 |
return: JNI_TRUE if VTIME > 0 else JNI_FALSE |
| 535 |
comments: see NativeEnableReceiveTimeoutThreshold |
| 536 |
----------------------------------------------------------*/ |
| 537 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_NativeisReceiveTimeoutEnabled( |
| 538 |
JNIEnv *env, |
| 539 |
jobject jobj |
| 540 |
) |
| 541 |
{ |
| 542 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 543 |
struct termios ttyset; |
| 544 |
|
| 545 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 546 |
return(ttyset.c_cc[ VTIME ] > 0 ? JNI_TRUE:JNI_FALSE); |
| 547 |
fail: |
| 548 |
throw_java_exception( env, IO_EXCEPTION, "isReceiveTimeoutEnabled", strerror( errno ) ); |
| 549 |
return JNI_FALSE; |
| 550 |
} |
| 551 |
|
| 552 |
/*---------------------------------------------------------- |
| 553 |
I2CPort.isDSR |
| 554 |
|
| 555 |
accept: none |
| 556 |
perform: check status of DSR |
| 557 |
return: true if TIOCM_DSR is set |
| 558 |
false if TIOCM_DSR is not set |
| 559 |
exceptions: none |
| 560 |
comments: DSR stands for Data Set Ready |
| 561 |
----------------------------------------------------------*/ |
| 562 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_isDSR( JNIEnv *env, |
| 563 |
jobject jobj ) |
| 564 |
{ |
| 565 |
unsigned int result = 0; |
| 566 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 567 |
|
| 568 |
ioctl( fd, TIOCMGET, &result ); |
| 569 |
if( result & TIOCM_DSR ) return JNI_TRUE; |
| 570 |
else return JNI_FALSE; |
| 571 |
} |
| 572 |
|
| 573 |
/*---------------------------------------------------------- |
| 574 |
I2CPort.isCD |
| 575 |
|
| 576 |
accept: none |
| 577 |
perform: check status of CD |
| 578 |
return: true if TIOCM_CD is set |
| 579 |
false if TIOCM_CD is not set |
| 580 |
exceptions: none |
| 581 |
comments: CD stands for Carrier Detect |
| 582 |
The following comment has been made... |
| 583 |
"well, it works, there might ofcourse be a bug, but making DCD |
| 584 |
permanently on fixed it for me so I don't care" |
| 585 |
|
| 586 |
----------------------------------------------------------*/ |
| 587 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_isCD( JNIEnv *env, |
| 588 |
jobject jobj ) |
| 589 |
{ |
| 590 |
unsigned int result = 0; |
| 591 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 592 |
|
| 593 |
ioctl( fd, TIOCMGET, &result ); |
| 594 |
if( result & TIOCM_CD ) return JNI_TRUE; |
| 595 |
else return JNI_FALSE; |
| 596 |
} |
| 597 |
|
| 598 |
/*---------------------------------------------------------- |
| 599 |
I2CPort.isCTS |
| 600 |
|
| 601 |
accept: none |
| 602 |
perform: check status of CTS |
| 603 |
return: true if TIOCM_CTS is set |
| 604 |
false if TIOCM_CTS is not set |
| 605 |
exceptions: none |
| 606 |
comments: CTS stands for Clear To Send. |
| 607 |
----------------------------------------------------------*/ |
| 608 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_isCTS( JNIEnv *env, |
| 609 |
jobject jobj ) |
| 610 |
{ |
| 611 |
unsigned int result = 0; |
| 612 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 613 |
|
| 614 |
ioctl( fd, TIOCMGET, &result ); |
| 615 |
if( result & TIOCM_CTS ) return JNI_TRUE; |
| 616 |
else return JNI_FALSE; |
| 617 |
} |
| 618 |
|
| 619 |
/*---------------------------------------------------------- |
| 620 |
I2CPort.isRI |
| 621 |
|
| 622 |
accept: none |
| 623 |
perform: check status of RI |
| 624 |
return: true if TIOCM_RI is set |
| 625 |
false if TIOCM_RI is not set |
| 626 |
exceptions: none |
| 627 |
comments: RI stands for Ring Indicator |
| 628 |
----------------------------------------------------------*/ |
| 629 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_isRI( JNIEnv *env, |
| 630 |
jobject jobj ) |
| 631 |
{ |
| 632 |
unsigned int result = 0; |
| 633 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 634 |
|
| 635 |
ioctl( fd, TIOCMGET, &result ); |
| 636 |
if( result & TIOCM_RI ) return JNI_TRUE; |
| 637 |
else return JNI_FALSE; |
| 638 |
} |
| 639 |
|
| 640 |
/*---------------------------------------------------------- |
| 641 |
I2CPort.isRTS |
| 642 |
|
| 643 |
accept: none |
| 644 |
perform: check status of RTS |
| 645 |
return: true if TIOCM_RTS is set |
| 646 |
false if TIOCM_RTS is not set |
| 647 |
exceptions: none |
| 648 |
comments: tcgetattr with c_cflag CRTS_IFLOW |
| 649 |
----------------------------------------------------------*/ |
| 650 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_isRTS( JNIEnv *env, |
| 651 |
jobject jobj ) |
| 652 |
{ |
| 653 |
unsigned int result = 0; |
| 654 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 655 |
|
| 656 |
ioctl( fd, TIOCMGET, &result ); |
| 657 |
if( result & TIOCM_RTS ) return JNI_TRUE; |
| 658 |
else return JNI_FALSE; |
| 659 |
} |
| 660 |
|
| 661 |
/*---------------------------------------------------------- |
| 662 |
I2CPort.setRTS |
| 663 |
|
| 664 |
accept: state flag to set/unset. |
| 665 |
perform: depends on the state flag |
| 666 |
if true TIOCM_RTS is set |
| 667 |
if false TIOCM_RTS is unset |
| 668 |
return: none |
| 669 |
exceptions: none |
| 670 |
comments: tcsetattr with c_cflag CRTS_IFLOW |
| 671 |
----------------------------------------------------------*/ |
| 672 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_setRTS( JNIEnv *env, |
| 673 |
jobject jobj, jboolean state ) |
| 674 |
{ |
| 675 |
unsigned int result = 0; |
| 676 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 677 |
|
| 678 |
ioctl( fd, TIOCMGET, &result ); |
| 679 |
if( state == JNI_TRUE ) result |= TIOCM_RTS; |
| 680 |
else result &= ~TIOCM_RTS; |
| 681 |
ioctl( fd, TIOCMSET, &result ); |
| 682 |
return; |
| 683 |
} |
| 684 |
|
| 685 |
/*---------------------------------------------------------- |
| 686 |
I2CPort.setDSR |
| 687 |
|
| 688 |
accept: state flag to set/unset. |
| 689 |
perform: depends on the state flag |
| 690 |
if true TIOCM_DSR is set |
| 691 |
if false TIOCM_DSR is unset |
| 692 |
return: none |
| 693 |
exceptions: none |
| 694 |
comments: tcsetattr with c_cflag CRTS_IFLOW |
| 695 |
----------------------------------------------------------*/ |
| 696 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_setDSR( JNIEnv *env, |
| 697 |
jobject jobj, jboolean state ) |
| 698 |
{ |
| 699 |
unsigned int result = 0; |
| 700 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 701 |
|
| 702 |
ioctl( fd, TIOCMGET, &result ); |
| 703 |
if( state == JNI_TRUE ) result |= TIOCM_DSR; |
| 704 |
else result &= ~TIOCM_DSR; |
| 705 |
ioctl( fd, TIOCMSET, &result ); |
| 706 |
return; |
| 707 |
} |
| 708 |
|
| 709 |
/*---------------------------------------------------------- |
| 710 |
I2CPort.isDTR |
| 711 |
|
| 712 |
accept: none |
| 713 |
perform: check status of DTR |
| 714 |
return: true if TIOCM_DTR is set |
| 715 |
false if TIOCM_DTR is not set |
| 716 |
exceptions: none |
| 717 |
comments: DTR stands for Data Terminal Ready |
| 718 |
----------------------------------------------------------*/ |
| 719 |
JNIEXPORT jboolean JNICALL Java_gnu_io_I2CPort_isDTR( JNIEnv *env, |
| 720 |
jobject jobj ) |
| 721 |
{ |
| 722 |
unsigned int result = 0; |
| 723 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 724 |
|
| 725 |
ioctl( fd, TIOCMGET, &result ); |
| 726 |
if( result & TIOCM_DTR ) return JNI_TRUE; |
| 727 |
else return JNI_FALSE; |
| 728 |
} |
| 729 |
|
| 730 |
/*---------------------------------------------------------- |
| 731 |
I2CPort.setDTR |
| 732 |
|
| 733 |
accept: new DTR state |
| 734 |
perform: if state is true, TIOCM_DTR is set |
| 735 |
if state is false, TIOCM_DTR is unset |
| 736 |
return: none |
| 737 |
exceptions: none |
| 738 |
comments: DTR stands for Data Terminal Ready |
| 739 |
----------------------------------------------------------*/ |
| 740 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_setDTR( JNIEnv *env, |
| 741 |
jobject jobj, jboolean state ) |
| 742 |
{ |
| 743 |
unsigned int result = 0; |
| 744 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 745 |
|
| 746 |
ioctl( fd, TIOCMGET, &result ); |
| 747 |
if( state == JNI_TRUE ) result |= TIOCM_DTR; |
| 748 |
else result &= ~TIOCM_DTR; |
| 749 |
ioctl( fd, TIOCMSET, &result ); |
| 750 |
return; |
| 751 |
} |
| 752 |
|
| 753 |
/*---------------------------------------------------------- |
| 754 |
read_byte_array |
| 755 |
|
| 756 |
accept: int fd file descriptor to read from |
| 757 |
unsigned char *buffer buffer to read data into |
| 758 |
int length number of bytes to read |
| 759 |
int timeout milliseconds to wait before returning |
| 760 |
perform: read bytes from the port into a buffer |
| 761 |
return: status of read |
| 762 |
-1 fail (IOException) |
| 763 |
0 timeout |
| 764 |
>0 number of bytes read |
| 765 |
comments: According to the Communications API spec, a receive threshold |
| 766 |
of 1 is the same as having the threshold disabled. |
| 767 |
|
| 768 |
The nuts and bolts are documented in |
| 769 |
NativeEnableReceiveTimeoutThreshold() |
| 770 |
----------------------------------------------------------*/ |
| 771 |
int read_byte_array( int fd, unsigned char *buffer, int length, int timeout ) |
| 772 |
{ |
| 773 |
int ret, left, bytes = 0; |
| 774 |
fd_set rfds; |
| 775 |
struct timeval sleep; |
| 776 |
struct timeval *psleep=&sleep; |
| 777 |
|
| 778 |
left = length; |
| 779 |
FD_ZERO( &rfds ); |
| 780 |
FD_SET( fd, &rfds ); |
| 781 |
if( timeout != 0 ) |
| 782 |
{ |
| 783 |
sleep.tv_sec = timeout / 1000; |
| 784 |
sleep.tv_usec = 1000 * ( timeout % 1000 ); |
| 785 |
} |
| 786 |
while( bytes < length ) |
| 787 |
{ |
| 788 |
/* FIXME: In Linux, select updates the timeout automatically, so |
| 789 |
other OSes will need to update it manually if they want to have |
| 790 |
the same behavior. For those OSes, timeouts will occur after no |
| 791 |
data AT ALL is received for the timeout duration. No big deal. */ |
| 792 |
do { |
| 793 |
if( timeout == 0 ) psleep = NULL; |
| 794 |
ret=select( fd + 1, &rfds, NULL, NULL, psleep ); |
| 795 |
} while (ret < 0 && errno==EINTR); |
| 796 |
if( ret == 0 ) break; |
| 797 |
if( ret < 0 ) return -1; |
| 798 |
ret = read( fd, buffer + bytes, left ); |
| 799 |
if( ret == 0 ) break; |
| 800 |
if( ret < 0 ) return -1; |
| 801 |
bytes += ret; |
| 802 |
left -= ret; |
| 803 |
} |
| 804 |
return bytes; |
| 805 |
} |
| 806 |
|
| 807 |
/*---------------------------------------------------------- |
| 808 |
NativeEnableReceiveTimeoutThreshold |
| 809 |
accept: int threshold, int vtime,int buffer |
| 810 |
perform: Set c_cc->VMIN to threshold and c_cc=>VTIME to vtime |
| 811 |
return: void |
| 812 |
exceptions: IOException |
| 813 |
comments: This is actually all handled in read with select in |
| 814 |
canonical input mode. |
| 815 |
----------------------------------------------------------*/ |
| 816 |
|
| 817 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_NativeEnableReceiveTimeoutThreshold(JNIEnv *env, jobject jobj, jint vtime, jint threshold, jint buffer) |
| 818 |
{ |
| 819 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 820 |
struct termios ttyset; |
| 821 |
|
| 822 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 823 |
ttyset.c_cc[ VMIN ] = threshold; |
| 824 |
ttyset.c_cc[ VTIME ] = vtime/100; |
| 825 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 826 |
|
| 827 |
return; |
| 828 |
fail: |
| 829 |
throw_java_exception( env, IO_EXCEPTION, "TimeoutThreshold", strerror( errno ) ); |
| 830 |
return; |
| 831 |
} |
| 832 |
|
| 833 |
/*---------------------------------------------------------- |
| 834 |
I2CPort.readByte |
| 835 |
|
| 836 |
accept: none |
| 837 |
perform: Read a single byte from the port |
| 838 |
return: The byte read |
| 839 |
exceptions: IOException |
| 840 |
----------------------------------------------------------*/ |
| 841 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_readByte( JNIEnv *env, |
| 842 |
jobject jobj ) |
| 843 |
{ |
| 844 |
int bytes; |
| 845 |
unsigned char buffer[ 1 ]; |
| 846 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 847 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 848 |
|
| 849 |
bytes = read_byte_array( fd, buffer, 1, timeout ); |
| 850 |
if( bytes < 0 ) { |
| 851 |
throw_java_exception( env, IO_EXCEPTION, "readByte", |
| 852 |
strerror( errno ) ); |
| 853 |
return -1; |
| 854 |
} |
| 855 |
return (bytes ? (jint)buffer[ 0 ] : -1); |
| 856 |
} |
| 857 |
|
| 858 |
/*---------------------------------------------------------- |
| 859 |
I2CPort.readArray |
| 860 |
|
| 861 |
accept: offset (offset to start storing data in the jbarray) and |
| 862 |
Length (bytes to read) |
| 863 |
perform: read bytes from the port into a byte array |
| 864 |
return: bytes read on success |
| 865 |
0 on read timeout |
| 866 |
exceptions: IOException |
| 867 |
comments: throws ArrayIndexOutOfBoundsException if asked to |
| 868 |
read more than SSIZE_MAX bytes |
| 869 |
----------------------------------------------------------*/ |
| 870 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_readArray( JNIEnv *env, |
| 871 |
jobject jobj, jbyteArray jbarray, jint offset, jint length ) |
| 872 |
{ |
| 873 |
int bytes; |
| 874 |
jbyte *body; |
| 875 |
unsigned char *buffer; |
| 876 |
int fd = get_java_var( env, jobj, "fd", "I" ); |
| 877 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 878 |
|
| 879 |
if( (size_t) length > SSIZE_MAX || length < 0 ) { |
| 880 |
throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS, |
| 881 |
"readArray", "Invalid length" ); |
| 882 |
return -1; |
| 883 |
} |
| 884 |
|
| 885 |
buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length ); |
| 886 |
if( buffer == 0 ) { |
| 887 |
throw_java_exception( env, OUT_OF_MEMORY, "readArray", |
| 888 |
"Unable to allocate buffer" ); |
| 889 |
return -1; |
| 890 |
} |
| 891 |
|
| 892 |
bytes = read_byte_array( fd, buffer, length, timeout ); |
| 893 |
if( bytes < 0 ) { |
| 894 |
free( buffer ); |
| 895 |
throw_java_exception( env, IO_EXCEPTION, "readArray", |
| 896 |
strerror( errno ) ); |
| 897 |
return -1; |
| 898 |
} |
| 899 |
body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 900 |
memcpy(body + offset, buffer, bytes); |
| 901 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 902 |
free( buffer ); |
| 903 |
return (bytes ? bytes : -1); |
| 904 |
} |
| 905 |
|
| 906 |
/*---------------------------------------------------------- |
| 907 |
I2CPort.nativeavailable |
| 908 |
|
| 909 |
accept: none |
| 910 |
perform: find out the number of bytes available for reading |
| 911 |
return: available bytes |
| 912 |
-1 on error |
| 913 |
exceptions: none |
| 914 |
----------------------------------------------------------*/ |
| 915 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_nativeavailable( JNIEnv *env, |
| 916 |
jobject jobj ) |
| 917 |
{ |
| 918 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 919 |
int result; |
| 920 |
|
| 921 |
if( ioctl( fd, FIONREAD, &result ) ) |
| 922 |
{ |
| 923 |
throw_java_exception( env, IO_EXCEPTION, "nativeavailable", strerror( errno ) ); |
| 924 |
return -1; |
| 925 |
} |
| 926 |
else return (jint)result; |
| 927 |
|
| 928 |
} |
| 929 |
|
| 930 |
/*---------------------------------------------------------- |
| 931 |
I2CPort.setflowcontrol |
| 932 |
|
| 933 |
accept: flowmode |
| 934 |
FLOWCONTROL_NONE none |
| 935 |
FLOWCONTROL_RTSCTS_IN hardware flow control |
| 936 |
FLOWCONTROL_RTSCTS_OUT "" |
| 937 |
FLOWCONTROL_XONXOFF_IN input software flow control |
| 938 |
FLOWCONTROL_XONXOFF_OUT output software flow control |
| 939 |
perform: set flow control to flowmode |
| 940 |
return: none |
| 941 |
exceptions: IOException |
| 942 |
comments: there is no differentiation between input and output hardware |
| 943 |
flow control |
| 944 |
----------------------------------------------------------*/ |
| 945 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_setflowcontrol( JNIEnv *env, |
| 946 |
jobject jobj, jint flowmode ) |
| 947 |
{ |
| 948 |
struct termios ttyset; |
| 949 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 950 |
|
| 951 |
if( tcgetattr( fd, &ttyset ) ) goto fail; |
| 952 |
|
| 953 |
if ( flowmode & ( FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT ) ) |
| 954 |
ttyset.c_cflag |= HARDWARE_FLOW_CONTROL; |
| 955 |
else ttyset.c_cflag &= ~HARDWARE_FLOW_CONTROL; |
| 956 |
|
| 957 |
ttyset.c_iflag &= ~IXANY; |
| 958 |
|
| 959 |
if ( flowmode & FLOWCONTROL_XONXOFF_IN ) |
| 960 |
ttyset.c_iflag |= IXOFF; |
| 961 |
else ttyset.c_iflag &= ~IXOFF; |
| 962 |
|
| 963 |
if ( flowmode & FLOWCONTROL_XONXOFF_OUT ) |
| 964 |
ttyset.c_iflag |= IXON; |
| 965 |
else ttyset.c_iflag &= ~IXON; |
| 966 |
|
| 967 |
if( tcsetattr( fd, TCSANOW, &ttyset ) ) goto fail; |
| 968 |
return; |
| 969 |
fail: |
| 970 |
throw_java_exception( env, IO_EXCEPTION, "setHWFC", |
| 971 |
strerror( errno ) ); |
| 972 |
return; |
| 973 |
} |
| 974 |
|
| 975 |
/*---------------------------------------------------------- |
| 976 |
I2CPort.eventLoop |
| 977 |
|
| 978 |
accept: none |
| 979 |
perform: periodically check for I2CPortEvents |
| 980 |
return: none |
| 981 |
exceptions: none |
| 982 |
comments: FIXME This is probably wrong on bsd. |
| 983 |
----------------------------------------------------------*/ |
| 984 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_eventLoop( JNIEnv *env, |
| 985 |
jobject jobj ) |
| 986 |
{ |
| 987 |
int fd, ret, change; |
| 988 |
fd_set rfds; |
| 989 |
struct timeval tv_sleep; |
| 990 |
unsigned int mflags; |
| 991 |
#if defined(TIOCGICOUNT) |
| 992 |
struct serial_icounter_struct sis, osis; |
| 993 |
#endif /* TIOCGICOUNT */ |
| 994 |
unsigned int omflags; |
| 995 |
|
| 996 |
jmethodID method, interrupt; |
| 997 |
jboolean interrupted = 0; |
| 998 |
jclass jclazz, jthread; |
| 999 |
jclazz = (*env)->GetObjectClass( env, jobj ); |
| 1000 |
fd = get_java_var(env, jobj, "fd", "I"); |
| 1001 |
method = (*env)->GetMethodID( env, jclazz, "sendEvent", "(IZ)V" ); |
| 1002 |
jthread = (*env)->FindClass( env, "java/lang/Thread" ); |
| 1003 |
interrupt = (*env)->GetStaticMethodID( env, jthread, "interrupted", "()Z" ); |
| 1004 |
|
| 1005 |
/* Some multiport i2c cards do not implement TIOCGICOUNT ... */ |
| 1006 |
#if defined(TIOCGICOUNT) |
| 1007 |
if( ioctl( fd, TIOCGICOUNT, &osis ) < 0 ) { |
| 1008 |
fprintf( stderr, "Port does not support TIOCGICOUNT events\n" ); |
| 1009 |
return; |
| 1010 |
} |
| 1011 |
#else |
| 1012 |
fprintf( stderr, "Port does not support all Hardware events\n" ); |
| 1013 |
#endif /* TIOCGICOUNT */ |
| 1014 |
|
| 1015 |
if( ioctl( fd, TIOCMGET, &omflags) <0 ) { |
| 1016 |
fprintf( stderr, "Port does not support events\n" ); |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
|
| 1020 |
FD_ZERO( &rfds ); |
| 1021 |
while( !interrupted ) { |
| 1022 |
FD_SET( fd, &rfds ); |
| 1023 |
/* Check every 1 second, or on receive data */ |
| 1024 |
tv_sleep.tv_sec = 1; |
| 1025 |
tv_sleep.tv_usec = 0; |
| 1026 |
do { |
| 1027 |
ret=select( fd + 1, &rfds, NULL, NULL, &tv_sleep ); |
| 1028 |
} while (ret < 0 && errno==EINTR); |
| 1029 |
if( ret < 0 ) { |
| 1030 |
fprintf( stderr, "select() Failed\n" ); |
| 1031 |
break; |
| 1032 |
} |
| 1033 |
|
| 1034 |
#if defined TIOCSERGETLSR |
| 1035 |
if( ioctl( fd, TIOCSERGETLSR, &change ) ) { |
| 1036 |
fprintf( stderr, "TIOCSERGETLSR Failed\n" ); |
| 1037 |
break; |
| 1038 |
} |
| 1039 |
else if( change ) { |
| 1040 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1041 |
(jint)SPE_OUTPUT_BUFFER_EMPTY, JNI_TRUE ); |
| 1042 |
} |
| 1043 |
#endif /* TIOCSERGETLSR */ |
| 1044 |
#if defined(TIOCGICOUNT) |
| 1045 |
/* wait for RNG, DSR, CD or CTS but not DataAvailable |
| 1046 |
* The drawback here is it never times out so if someone |
| 1047 |
* reads there will be no chance to try again. |
| 1048 |
* This may make sense if the program does not want to |
| 1049 |
* be notified of data available or errors. |
| 1050 |
* ret=ioctl(fd,TIOCMIWAIT); |
| 1051 |
*/ |
| 1052 |
if( ioctl( fd, TIOCGICOUNT, &sis ) ) { |
| 1053 |
fprintf( stderr, "TIOCGICOUNT Failed\n" ); |
| 1054 |
break; |
| 1055 |
} |
| 1056 |
while( sis.frame != osis.frame ) { |
| 1057 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_FE, JNI_TRUE ); |
| 1058 |
osis.frame++; |
| 1059 |
} |
| 1060 |
while( sis.overrun != osis.overrun ) { |
| 1061 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_OE, JNI_TRUE ); |
| 1062 |
osis.overrun++; |
| 1063 |
} |
| 1064 |
while( sis.parity != osis.parity ) { |
| 1065 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_PE, JNI_TRUE ); |
| 1066 |
osis.parity++; |
| 1067 |
} |
| 1068 |
while( sis.brk != osis.brk ) { |
| 1069 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_BI, JNI_TRUE ); |
| 1070 |
osis.brk++; |
| 1071 |
} |
| 1072 |
osis = sis; |
| 1073 |
#endif /* TIOCGICOUNT */ |
| 1074 |
if( ioctl( fd, TIOCMGET, &mflags ) ) { |
| 1075 |
fprintf( stderr, "TIOCMGET Failed\n" ); |
| 1076 |
break; |
| 1077 |
} |
| 1078 |
interrupted = (*env)->CallStaticBooleanMethod( env, jthread, interrupt ); |
| 1079 |
/* A Portable implementation */ |
| 1080 |
change = (mflags&TIOCM_CTS) - (omflags&TIOCM_CTS); |
| 1081 |
if( change ) { |
| 1082 |
fprintf(stderr, "Sending SPE_CTS\n"); |
| 1083 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1084 |
(jint)SPE_CTS, JNI_TRUE ); |
| 1085 |
} |
| 1086 |
change = (mflags&TIOCM_DSR) - (omflags&TIOCM_DSR); |
| 1087 |
if( change ) { |
| 1088 |
fprintf(stderr, "Sending SPE_DSR\n"); |
| 1089 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1090 |
(jint)SPE_DSR, JNI_TRUE ); |
| 1091 |
} |
| 1092 |
change = (mflags&TIOCM_RNG) - (omflags&TIOCM_RNG); |
| 1093 |
if( change ) { |
| 1094 |
fprintf(stderr, "Sending SPE_RI\n"); |
| 1095 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1096 |
(jint)SPE_RI, JNI_TRUE ); |
| 1097 |
} |
| 1098 |
change = (mflags&TIOCM_CD) - (omflags&TIOCM_CD); |
| 1099 |
if( change ) { |
| 1100 |
fprintf(stderr, "Sending SPE_CD\n"); |
| 1101 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1102 |
(jint)SPE_CD, JNI_TRUE ); |
| 1103 |
} |
| 1104 |
omflags = mflags; |
| 1105 |
if( ioctl( fd, FIONREAD, &change ) ) { |
| 1106 |
fprintf( stderr, "FIONREAD Failed\n" ); |
| 1107 |
} |
| 1108 |
else if( change ) { |
| 1109 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1110 |
(jint)SPE_DATA_AVAILABLE, JNI_TRUE ); |
| 1111 |
usleep(1000); /* select wont block */ |
| 1112 |
} |
| 1113 |
} |
| 1114 |
return; |
| 1115 |
} |
| 1116 |
|
| 1117 |
/*---------------------------------------------------------- |
| 1118 |
send_modem_events |
| 1119 |
|
| 1120 |
accept: int event I2CPortEvent constant |
| 1121 |
int change Number of times this event happened |
| 1122 |
int state current state: 0 is false, nonzero is true |
| 1123 |
perform: Send the necessary events |
| 1124 |
return: none |
| 1125 |
exceptions: none |
| 1126 |
comments: Since the interrupt counters tell us how many times the |
| 1127 |
state has changed, we can send a I2CPortEvent for each |
| 1128 |
interrupt (change) that has occured. If we don't do this, |
| 1129 |
we'll miss a whole bunch of events. |
| 1130 |
----------------------------------------------------------*/ |
| 1131 |
void send_modem_events( JNIEnv *env, jobject jobj, jmethodID method, |
| 1132 |
int event, int change, int state ) |
| 1133 |
{ |
| 1134 |
int i, s; |
| 1135 |
jboolean flag; |
| 1136 |
if( state ) s = 1; |
| 1137 |
else s = 0; |
| 1138 |
|
| 1139 |
for( i = 0; i < change; i++ ) { |
| 1140 |
if( ( change + s + i ) % 2 ) flag = JNI_FALSE; |
| 1141 |
else flag = JNI_TRUE; |
| 1142 |
(*env)->CallVoidMethod( env, jobj, method, (jint)event, flag ); |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
/*---------------------------------------------------------- |
| 1147 |
get_java_fd |
| 1148 |
|
| 1149 |
accept: env (keyhole to java) |
| 1150 |
jobj (java I2CPort object) |
| 1151 |
return: the fd field from the java object |
| 1152 |
exceptions: none |
| 1153 |
comments: |
| 1154 |
----------------------------------------------------------*/ |
| 1155 |
int get_java_var( JNIEnv *env, jobject jobj, char *id, char *type ) |
| 1156 |
{ |
| 1157 |
int result = 0; |
| 1158 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 1159 |
jfieldID jfd = (*env)->GetFieldID( env, jclazz, id, type ); |
| 1160 |
if( !jfd ) { |
| 1161 |
(*env)->ExceptionDescribe( env ); |
| 1162 |
(*env)->ExceptionClear( env ); |
| 1163 |
return result; |
| 1164 |
} |
| 1165 |
result = (int)( (*env)->GetIntField( env, jobj, jfd ) ); |
| 1166 |
/* ct7 & gel * Added DeleteLocalRef */ |
| 1167 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 1168 |
return result; |
| 1169 |
} |
| 1170 |
|
| 1171 |
/*---------------------------------------------------------- |
| 1172 |
throw_java_exception |
| 1173 |
|
| 1174 |
accept: env (keyhole to java) |
| 1175 |
*exc (exception class name) |
| 1176 |
*foo (function name) |
| 1177 |
*msg (error message) |
| 1178 |
perform: Throw a new java exception |
| 1179 |
return: none |
| 1180 |
exceptions: haha! |
| 1181 |
comments: |
| 1182 |
----------------------------------------------------------*/ |
| 1183 |
void throw_java_exception( JNIEnv *env, char *exc, char *foo, char *msg ) |
| 1184 |
{ |
| 1185 |
char buf[ 60 ]; |
| 1186 |
jclass clazz = (*env)->FindClass( env, exc ); |
| 1187 |
if( !clazz ) { |
| 1188 |
(*env)->ExceptionDescribe( env ); |
| 1189 |
(*env)->ExceptionClear( env ); |
| 1190 |
return; |
| 1191 |
} |
| 1192 |
#if defined(_GNU_SOURCE) |
| 1193 |
snprintf( buf, 60, "%s in %s", msg, foo ); |
| 1194 |
#else |
| 1195 |
sprintf( buf,"%s in %s", msg, foo ); |
| 1196 |
#endif /* _GNU_SOURCE */ |
| 1197 |
(*env)->ThrowNew( env, clazz, buf ); |
| 1198 |
/* ct7 * Added DeleteLocalRef */ |
| 1199 |
(*env)->DeleteLocalRef( env, clazz ); |
| 1200 |
} |
| 1201 |
|
| 1202 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_IsDeviceGood(JNIEnv *env, |
| 1203 |
jobject jobj, jstring tty_name){ |
| 1204 |
|
| 1205 |
jboolean result; |
| 1206 |
static struct stat mystat; |
| 1207 |
char teststring[256]; |
| 1208 |
int fd,i; |
| 1209 |
const char *name = (*env)->GetStringUTFChars(env, tty_name, 0); |
| 1210 |
|
| 1211 |
#if defined(__linux__) |
| 1212 |
if(!strcmp(name,"tty0")|| !strcmp(name,"ttyd")|| |
| 1213 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1214 |
!strcmp(name,"ttyf")|| !strcmp(name,"cuaa") |
| 1215 |
) |
| 1216 |
{ |
| 1217 |
#ifdef DEBUG |
| 1218 |
fprintf(stderr,"DEBUG: Ignoring Port %s\*\n",name); |
| 1219 |
#endif |
| 1220 |
return(JNI_FALSE); |
| 1221 |
} |
| 1222 |
#endif |
| 1223 |
#if defined(__FreeBSD__) |
| 1224 |
if(!strcmp(name,"tty0")|| !strcmp(name,"ttyd")|| |
| 1225 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1226 |
!strcmp(name,"ttyf")|| !strcmp(name,"ttyS")|| |
| 1227 |
!strcmp(name,"ttyI")|| !strcmp(name,"ttyW")|| |
| 1228 |
!strcmp(name,"ttyC")|| !strcmp(name,"ttyR") |
| 1229 |
) |
| 1230 |
{ |
| 1231 |
#ifdef DEBUG |
| 1232 |
fprintf(stderr,"DEBUG: Ignoring Port %s*\n",name); |
| 1233 |
#endif |
| 1234 |
return(JNI_FALSE); |
| 1235 |
} |
| 1236 |
#endif |
| 1237 |
#if defined(__NetBSD__) |
| 1238 |
if( !strcmp(name,"ttyd")|| |
| 1239 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1240 |
!strcmp(name,"ttyf")|| !strcmp(name,"ttyS")|| |
| 1241 |
!strcmp(name,"ttyI")|| !strcmp(name,"ttyW")|| |
| 1242 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1243 |
!strcmp(name,"ttyf")|| !strcmp(name,"cuaa")|| |
| 1244 |
!strcmp(name,"ttyC")|| !strcmp(name,"ttyR")|| |
| 1245 |
!strcmp(name,"ttyM") |
| 1246 |
) |
| 1247 |
{ |
| 1248 |
#ifdef DEBUG |
| 1249 |
fprintf(stderr,"DEBUG: Ignoring Port %s*\n",name); |
| 1250 |
#endif |
| 1251 |
return(JNI_FALSE); |
| 1252 |
} |
| 1253 |
#endif |
| 1254 |
for(i=0;i<64;i++){ |
| 1255 |
sprintf(teststring,"/dev/%s%i",name, i); |
| 1256 |
stat(teststring,&mystat); |
| 1257 |
if(S_ISCHR(mystat.st_mode)){ |
| 1258 |
fd=open(teststring,O_RDONLY|O_NONBLOCK); |
| 1259 |
if (fd>0){ |
| 1260 |
close(fd); |
| 1261 |
result=JNI_TRUE; |
| 1262 |
break; |
| 1263 |
} |
| 1264 |
result=JNI_FALSE; |
| 1265 |
} |
| 1266 |
else result=JNI_FALSE; |
| 1267 |
} |
| 1268 |
sprintf(teststring,"/dev/%s",name); |
| 1269 |
stat(teststring,&mystat); |
| 1270 |
if(S_ISCHR(mystat.st_mode)){ |
| 1271 |
fd=open(teststring,O_RDONLY|O_NONBLOCK); |
| 1272 |
if (fd>0){ |
| 1273 |
close(fd); |
| 1274 |
result=JNI_TRUE; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
(*env)->ReleaseStringUTFChars(env, tty_name, name); |
| 1278 |
return(result); |
| 1279 |
} |
| 1280 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_setInputBufferSize(JNIEnv *env, jobject jobj, jint size ) |
| 1281 |
{ |
| 1282 |
#ifdef DEBUG |
| 1283 |
fprintf(stderr,"setInputBufferSize is not implemented\n"); |
| 1284 |
#endif |
| 1285 |
} |
| 1286 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_getInputBufferSize(JNIEnv *env, jobject jobj) |
| 1287 |
{ |
| 1288 |
#ifdef DEBUG |
| 1289 |
fprintf(stderr,"getInputBufferSize is not implemented\n"); |
| 1290 |
#endif |
| 1291 |
return(1); |
| 1292 |
} |
| 1293 |
JNIEXPORT void JNICALL Java_gnu_io_I2CPort_setOutputBufferSize(JNIEnv *env, jobject jobj, jint size ) |
| 1294 |
{ |
| 1295 |
#ifdef DEBUG |
| 1296 |
fprintf(stderr,"setOutputBufferSize is not implemented\n"); |
| 1297 |
#endif |
| 1298 |
} |
| 1299 |
JNIEXPORT jint JNICALL Java_gnu_io_I2CPort_getOutputBufferSize(JNIEnv *env, jobject jobj) |
| 1300 |
{ |
| 1301 |
#ifdef DEBUG |
| 1302 |
fprintf(stderr,"getOutputBufferSize is not implemented\n"); |
| 1303 |
#endif |
| 1304 |
return(1); |
| 1305 |
} |
| 1306 |
|
| 1307 |
void dump_termios(char *foo,struct termios *ttyset) |
| 1308 |
{ |
| 1309 |
int i; |
| 1310 |
|
| 1311 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_iflag); |
| 1312 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_lflag); |
| 1313 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_oflag); |
| 1314 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_cflag); |
| 1315 |
for(i=0;i<NCCS;i++) |
| 1316 |
{ |
| 1317 |
fprintf(stderr,"%s %o ",foo,ttyset->c_cc[i]); |
| 1318 |
} |
| 1319 |
fprintf(stderr,"\n"); |
| 1320 |
} |