| 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 "RS485.h" |
| 60 |
#else /* dima */ |
| 61 |
#include "config.h" |
| 62 |
#include "gnu_io_RS485.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 "RS485Imp.h" |
| 101 |
/* #define DEBUG_TIMEOUT */ |
| 102 |
|
| 103 |
/*---------------------------------------------------------- |
| 104 |
RS485Port.Initialize |
| 105 |
|
| 106 |
accept: none |
| 107 |
perform: Initialize the native library |
| 108 |
return: none |
| 109 |
----------------------------------------------------------*/ |
| 110 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_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 |
RS485Port.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_RS485Port_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 |
RS485Port.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_RS485Port_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 |
RS485Port.nativeSetRS485PortParams |
| 223 |
|
| 224 |
accept: speed, data bits, stop bits, parity |
| 225 |
perform: set the RS485 port parameters |
| 226 |
return: void |
| 227 |
exceptions: UnsupportedCommOperationException |
| 228 |
----------------------------------------------------------*/ |
| 229 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_nativeSetRS485PortParams( |
| 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 c_cflag in darwin is unsigned long */ |
| 239 |
if( !translate_stop_bits( env, (int *)&(ttyset.c_cflag), stopBits ) ) return; /* dima c_cflag in darwin is unsigned long */ |
| 240 |
if( !translate_parity( env, (int *)&(ttyset.c_cflag), parity ) ) return;/* dima c_cflag in darwin is 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 |
"nativeSetRS485PortParams", 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.RS485Port.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.RS485Port.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.RS485Port.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 |
RS485Port.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_RS485Port_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=0,count=0; |
| 418 |
|
| 419 |
/* turn on the DTR */ |
| 420 |
ioctl(fd, TIOCMGET, &result); |
| 421 |
result |= TIOCM_DTR; |
| 422 |
ioctl(fd, TIOCMSET, &result); |
| 423 |
|
| 424 |
/* send the data */ |
| 425 |
do { |
| 426 |
result=write (fd, &byte, sizeof(unsigned char)); |
| 427 |
} while (result < 0 && errno==EINTR); |
| 428 |
if(result < 0) goto fail; |
| 429 |
/* wait for the last bit to go |
| 430 |
* see get_lsr_info in linux/driver/char/serial.c |
| 431 |
* */ |
| 432 |
#if defined(TIOCSERGETLSR) /* dima ????????? I did it in exactly the same way as in SerialImp.c */ |
| 433 |
do |
| 434 |
{ |
| 435 |
result=ioctl(fd, TIOCSERGETLSR); |
| 436 |
/* FIXME this should work but its a hack */ |
| 437 |
if (result != TIOCSER_TEMT) |
| 438 |
{ |
| 439 |
usleep(100); |
| 440 |
} |
| 441 |
} while(result != TIOCSER_TEMT); |
| 442 |
#endif /* TIOCSERGETLSR */ |
| 443 |
/* shut down the DTR */ |
| 444 |
ioctl(fd, TIOCMGET, &result); |
| 445 |
result &= ~TIOCM_DTR; |
| 446 |
ioctl(fd, TIOCMSET, &result); |
| 447 |
|
| 448 |
/* flush input (we dont want to get our own message */ |
| 449 |
do { |
| 450 |
result=tcflush(fd, TCIFLUSH); |
| 451 |
} while (result && errno==EINTR && count <5); |
| 452 |
|
| 453 |
if(result) goto fail; |
| 454 |
|
| 455 |
return; |
| 456 |
fail: |
| 457 |
throw_java_exception( env, IO_EXCEPTION, "writeByte", |
| 458 |
strerror( errno ) ); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
/*---------------------------------------------------------- |
| 463 |
RS485Port.writeArray |
| 464 |
|
| 465 |
accept: jbarray: bytes used for writing |
| 466 |
offset: offset in array to start writing |
| 467 |
count: Number of bytes to write |
| 468 |
perform: write length bytes of jbarray |
| 469 |
return: none |
| 470 |
exceptions: IOException |
| 471 |
----------------------------------------------------------*/ |
| 472 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_writeArray( JNIEnv *env, |
| 473 |
jobject jobj, jbyteArray jbarray, jint offset, jint count ) |
| 474 |
{ |
| 475 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 476 |
int result=0,total=0,i; |
| 477 |
|
| 478 |
unsigned char *bytes = (unsigned char *)malloc( count ); |
| 479 |
|
| 480 |
jbyte *body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 481 |
for( i = 0; i < count; i++ ) bytes[ i ] = body[ i + offset ]; |
| 482 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 483 |
|
| 484 |
/* turn on the DTR */ |
| 485 |
ioctl(fd, TIOCMGET, &result); |
| 486 |
result |= TIOCM_DTR; |
| 487 |
ioctl(fd, TIOCMSET, &result); |
| 488 |
|
| 489 |
/* send the data */ |
| 490 |
do { |
| 491 |
result=write (fd, bytes + total, count - total); |
| 492 |
if(result >0){ |
| 493 |
total += result; |
| 494 |
} |
| 495 |
} while ((total<count)||(result < 0 && errno==EINTR)); |
| 496 |
if (result < 0) goto fail; |
| 497 |
/* wait for the last bit to go |
| 498 |
* see get_lsr_info in linux/driver/char/serial.c |
| 499 |
* */ |
| 500 |
#if defined(TIOCSERGETLSR) /* dima ????????? I did it in exactly the same way as in SerialImp.c */ |
| 501 |
do |
| 502 |
{ |
| 503 |
result=ioctl(fd, TIOCSERGETLSR); |
| 504 |
/* FIXME this should work but its a hack */ |
| 505 |
if (result != TIOCSER_TEMT) |
| 506 |
{ |
| 507 |
usleep(100); |
| 508 |
} |
| 509 |
} while(result != TIOCSER_TEMT); |
| 510 |
#endif /* TIOCSERGETLSR */ |
| 511 |
/* shut down the DTR */ |
| 512 |
ioctl(fd, TIOCMGET, &result); |
| 513 |
result &= ~TIOCM_DTR; |
| 514 |
ioctl(fd, TIOCMSET, &result); |
| 515 |
|
| 516 |
/* flush input (we dont want to get our own message */ |
| 517 |
do { |
| 518 |
result=tcflush(fd, TCIFLUSH); |
| 519 |
} while (result && errno==EINTR && count <5); |
| 520 |
|
| 521 |
if(result) goto fail; |
| 522 |
free( bytes ); |
| 523 |
return; |
| 524 |
fail: |
| 525 |
free( bytes ); |
| 526 |
throw_java_exception( env, IO_EXCEPTION, "writeArray", |
| 527 |
strerror( errno ) ); |
| 528 |
|
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
/*---------------------------------------------------------- |
| 533 |
RS485Port.drain |
| 534 |
|
| 535 |
accept: none |
| 536 |
perform: wait until all data is transmitted |
| 537 |
return: none |
| 538 |
exceptions: IOException |
| 539 |
comments: java.io.OutputStream.flush() is equivalent to tcdrain, |
| 540 |
not tcflush, which throws away unsent bytes |
| 541 |
|
| 542 |
count logic added to avoid infinite loops when EINTR is |
| 543 |
true... Thread.yeild() was suggested. |
| 544 |
----------------------------------------------------------*/ |
| 545 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_drain( JNIEnv *env, |
| 546 |
jobject jobj ) |
| 547 |
{ |
| 548 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 549 |
int result, count=0; |
| 550 |
|
| 551 |
do { |
| 552 |
result=tcdrain (fd); |
| 553 |
count++; |
| 554 |
} while (result && errno==EINTR && count <5); |
| 555 |
|
| 556 |
if( result ) throw_java_exception( env, IO_EXCEPTION, "drain", |
| 557 |
strerror( errno ) ); |
| 558 |
} |
| 559 |
|
| 560 |
/*---------------------------------------------------------- |
| 561 |
RS485Port.sendBreak |
| 562 |
|
| 563 |
accept: duration in milliseconds. |
| 564 |
perform: send break for actual time. not less than 0.25 seconds. |
| 565 |
exceptions: none |
| 566 |
comments: not very precise |
| 567 |
----------------------------------------------------------*/ |
| 568 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_sendBreak( JNIEnv *env, |
| 569 |
jobject jobj, jint duration ) |
| 570 |
{ |
| 571 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 572 |
tcsendbreak( fd, (int)( duration / 250 ) ); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
/*---------------------------------------------------------- |
| 577 |
RS485Port.NativegetReceiveTimeout |
| 578 |
|
| 579 |
accept: none |
| 580 |
perform: get termios.c_cc[VTIME] |
| 581 |
return: VTIME |
| 582 |
comments: see NativeEnableReceiveTimeoutThreshold |
| 583 |
----------------------------------------------------------*/ |
| 584 |
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_NativegetReceiveTimeout( |
| 585 |
JNIEnv *env, |
| 586 |
jobject jobj |
| 587 |
) |
| 588 |
{ |
| 589 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 590 |
struct termios ttyset; |
| 591 |
|
| 592 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 593 |
return(ttyset.c_cc[ VTIME ] * 100); |
| 594 |
fail: |
| 595 |
throw_java_exception( env, IO_EXCEPTION, "getReceiveTimeout", strerror( errno ) ); |
| 596 |
return -1; |
| 597 |
} |
| 598 |
|
| 599 |
/*---------------------------------------------------------- |
| 600 |
RS485Port.NativeisReceiveTimeoutEnabled |
| 601 |
|
| 602 |
accept: none |
| 603 |
perform: determine if VTIME is none 0 |
| 604 |
return: JNI_TRUE if VTIME > 0 else JNI_FALSE |
| 605 |
comments: see NativeEnableReceiveTimeoutThreshold |
| 606 |
----------------------------------------------------------*/ |
| 607 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_NativeisReceiveTimeoutEnabled( |
| 608 |
JNIEnv *env, |
| 609 |
jobject jobj |
| 610 |
) |
| 611 |
{ |
| 612 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 613 |
struct termios ttyset; |
| 614 |
|
| 615 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 616 |
return(ttyset.c_cc[ VTIME ] > 0 ? JNI_TRUE:JNI_FALSE); |
| 617 |
fail: |
| 618 |
throw_java_exception( env, IO_EXCEPTION, "isReceiveTimeoutEnabled", strerror( errno ) ); |
| 619 |
return JNI_FALSE; |
| 620 |
} |
| 621 |
|
| 622 |
/*---------------------------------------------------------- |
| 623 |
RS485Port.isDSR |
| 624 |
|
| 625 |
accept: none |
| 626 |
perform: check status of DSR |
| 627 |
return: true if TIOCM_DSR is set |
| 628 |
false if TIOCM_DSR is not set |
| 629 |
exceptions: none |
| 630 |
comments: DSR stands for Data Set Ready |
| 631 |
----------------------------------------------------------*/ |
| 632 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isDSR( JNIEnv *env, |
| 633 |
jobject jobj ) |
| 634 |
{ |
| 635 |
unsigned int result = 0; |
| 636 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 637 |
|
| 638 |
ioctl( fd, TIOCMGET, &result ); |
| 639 |
if( result & TIOCM_DSR ) return JNI_TRUE; |
| 640 |
else return JNI_FALSE; |
| 641 |
} |
| 642 |
|
| 643 |
/*---------------------------------------------------------- |
| 644 |
RS485Port.isCD |
| 645 |
|
| 646 |
accept: none |
| 647 |
perform: check status of CD |
| 648 |
return: true if TIOCM_CD is set |
| 649 |
false if TIOCM_CD is not set |
| 650 |
exceptions: none |
| 651 |
comments: CD stands for Carrier Detect |
| 652 |
The following comment has been made... |
| 653 |
"well, it works, there might ofcourse be a bug, but making DCD |
| 654 |
permanently on fixed it for me so I don't care" |
| 655 |
|
| 656 |
----------------------------------------------------------*/ |
| 657 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isCD( JNIEnv *env, |
| 658 |
jobject jobj ) |
| 659 |
{ |
| 660 |
unsigned int result = 0; |
| 661 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 662 |
|
| 663 |
ioctl( fd, TIOCMGET, &result ); |
| 664 |
if( result & TIOCM_CD ) return JNI_TRUE; |
| 665 |
else return JNI_FALSE; |
| 666 |
} |
| 667 |
|
| 668 |
/*---------------------------------------------------------- |
| 669 |
RS485Port.isCTS |
| 670 |
|
| 671 |
accept: none |
| 672 |
perform: check status of CTS |
| 673 |
return: true if TIOCM_CTS is set |
| 674 |
false if TIOCM_CTS is not set |
| 675 |
exceptions: none |
| 676 |
comments: CTS stands for Clear To Send. |
| 677 |
----------------------------------------------------------*/ |
| 678 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isCTS( JNIEnv *env, |
| 679 |
jobject jobj ) |
| 680 |
{ |
| 681 |
unsigned int result = 0; |
| 682 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 683 |
|
| 684 |
ioctl( fd, TIOCMGET, &result ); |
| 685 |
if( result & TIOCM_CTS ) return JNI_TRUE; |
| 686 |
else return JNI_FALSE; |
| 687 |
} |
| 688 |
|
| 689 |
/*---------------------------------------------------------- |
| 690 |
RS485Port.isRI |
| 691 |
|
| 692 |
accept: none |
| 693 |
perform: check status of RI |
| 694 |
return: true if TIOCM_RI is set |
| 695 |
false if TIOCM_RI is not set |
| 696 |
exceptions: none |
| 697 |
comments: RI stands for Ring Indicator |
| 698 |
----------------------------------------------------------*/ |
| 699 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isRI( JNIEnv *env, |
| 700 |
jobject jobj ) |
| 701 |
{ |
| 702 |
unsigned int result = 0; |
| 703 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 704 |
|
| 705 |
ioctl( fd, TIOCMGET, &result ); |
| 706 |
if( result & TIOCM_RI ) return JNI_TRUE; |
| 707 |
else return JNI_FALSE; |
| 708 |
} |
| 709 |
|
| 710 |
/*---------------------------------------------------------- |
| 711 |
RS485Port.isRTS |
| 712 |
|
| 713 |
accept: none |
| 714 |
perform: check status of RTS |
| 715 |
return: true if TIOCM_RTS is set |
| 716 |
false if TIOCM_RTS is not set |
| 717 |
exceptions: none |
| 718 |
comments: tcgetattr with c_cflag CRTS_IFLOW |
| 719 |
----------------------------------------------------------*/ |
| 720 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isRTS( JNIEnv *env, |
| 721 |
jobject jobj ) |
| 722 |
{ |
| 723 |
unsigned int result = 0; |
| 724 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 725 |
|
| 726 |
ioctl( fd, TIOCMGET, &result ); |
| 727 |
if( result & TIOCM_RTS ) return JNI_TRUE; |
| 728 |
else return JNI_FALSE; |
| 729 |
} |
| 730 |
|
| 731 |
/*---------------------------------------------------------- |
| 732 |
RS485Port.setRTS |
| 733 |
|
| 734 |
accept: state flag to set/unset. |
| 735 |
perform: depends on the state flag |
| 736 |
if true TIOCM_RTS is set |
| 737 |
if false TIOCM_RTS is unset |
| 738 |
return: none |
| 739 |
exceptions: none |
| 740 |
comments: tcsetattr with c_cflag CRTS_IFLOW |
| 741 |
----------------------------------------------------------*/ |
| 742 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setRTS( JNIEnv *env, |
| 743 |
jobject jobj, jboolean state ) |
| 744 |
{ |
| 745 |
unsigned int result = 0; |
| 746 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 747 |
|
| 748 |
ioctl( fd, TIOCMGET, &result ); |
| 749 |
if( state == JNI_TRUE ) result |= TIOCM_RTS; |
| 750 |
else result &= ~TIOCM_RTS; |
| 751 |
ioctl( fd, TIOCMSET, &result ); |
| 752 |
return; |
| 753 |
} |
| 754 |
|
| 755 |
/*---------------------------------------------------------- |
| 756 |
RS485Port.setDSR |
| 757 |
|
| 758 |
accept: state flag to set/unset. |
| 759 |
perform: depends on the state flag |
| 760 |
if true TIOCM_DSR is set |
| 761 |
if false TIOCM_DSR is unset |
| 762 |
return: none |
| 763 |
exceptions: none |
| 764 |
comments: tcsetattr with c_cflag CRTS_IFLOW |
| 765 |
----------------------------------------------------------*/ |
| 766 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setDSR( JNIEnv *env, |
| 767 |
jobject jobj, jboolean state ) |
| 768 |
{ |
| 769 |
unsigned int result = 0; |
| 770 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 771 |
|
| 772 |
ioctl( fd, TIOCMGET, &result ); |
| 773 |
if( state == JNI_TRUE ) result |= TIOCM_DSR; |
| 774 |
else result &= ~TIOCM_DSR; |
| 775 |
ioctl( fd, TIOCMSET, &result ); |
| 776 |
return; |
| 777 |
} |
| 778 |
|
| 779 |
/*---------------------------------------------------------- |
| 780 |
RS485Port.isDTR |
| 781 |
|
| 782 |
accept: none |
| 783 |
perform: check status of DTR |
| 784 |
return: true if TIOCM_DTR is set |
| 785 |
false if TIOCM_DTR is not set |
| 786 |
exceptions: none |
| 787 |
comments: DTR stands for Data Terminal Ready |
| 788 |
----------------------------------------------------------*/ |
| 789 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isDTR( JNIEnv *env, |
| 790 |
jobject jobj ) |
| 791 |
{ |
| 792 |
unsigned int result = 0; |
| 793 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 794 |
|
| 795 |
ioctl( fd, TIOCMGET, &result ); |
| 796 |
if( result & TIOCM_DTR ) return JNI_TRUE; |
| 797 |
else return JNI_FALSE; |
| 798 |
} |
| 799 |
|
| 800 |
/*---------------------------------------------------------- |
| 801 |
RS485Port.setDTR |
| 802 |
|
| 803 |
accept: new DTR state |
| 804 |
perform: if state is true, TIOCM_DTR is set |
| 805 |
if state is false, TIOCM_DTR is unset |
| 806 |
return: none |
| 807 |
exceptions: none |
| 808 |
comments: DTR stands for Data Terminal Ready |
| 809 |
----------------------------------------------------------*/ |
| 810 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setDTR( JNIEnv *env, |
| 811 |
jobject jobj, jboolean state ) |
| 812 |
{ |
| 813 |
unsigned int result = 0; |
| 814 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 815 |
|
| 816 |
ioctl( fd, TIOCMGET, &result ); |
| 817 |
if( state == JNI_TRUE ) result |= TIOCM_DTR; |
| 818 |
else result &= ~TIOCM_DTR; |
| 819 |
ioctl( fd, TIOCMSET, &result ); |
| 820 |
return; |
| 821 |
} |
| 822 |
|
| 823 |
/*---------------------------------------------------------- |
| 824 |
read_byte_array |
| 825 |
|
| 826 |
accept: int fd file descriptor to read from |
| 827 |
unsigned char *buffer buffer to read data into |
| 828 |
int length number of bytes to read |
| 829 |
int timeout milliseconds to wait before returning |
| 830 |
perform: read bytes from the port into a buffer |
| 831 |
return: status of read |
| 832 |
-1 fail (IOException) |
| 833 |
0 timeout |
| 834 |
>0 number of bytes read |
| 835 |
comments: According to the Communications API spec, a receive threshold |
| 836 |
of 1 is the same as having the threshold disabled. |
| 837 |
|
| 838 |
The nuts and bolts are documented in |
| 839 |
NativeEnableReceiveTimeoutThreshold() |
| 840 |
----------------------------------------------------------*/ |
| 841 |
int read_byte_array( int fd, unsigned char *buffer, int length, int timeout ) |
| 842 |
{ |
| 843 |
int ret, left, bytes = 0; |
| 844 |
fd_set rfds; |
| 845 |
struct timeval sleep; |
| 846 |
struct timeval *psleep=&sleep; |
| 847 |
|
| 848 |
left = length; |
| 849 |
FD_ZERO( &rfds ); |
| 850 |
FD_SET( fd, &rfds ); |
| 851 |
if( timeout != 0 ) |
| 852 |
{ |
| 853 |
sleep.tv_sec = timeout / 1000; |
| 854 |
sleep.tv_usec = 1000 * ( timeout % 1000 ); |
| 855 |
} |
| 856 |
while( bytes < length ) |
| 857 |
{ |
| 858 |
/* FIXME: In Linux, select updates the timeout automatically, so |
| 859 |
other OSes will need to update it manually if they want to have |
| 860 |
the same behavior. For those OSes, timeouts will occur after no |
| 861 |
data AT ALL is received for the timeout duration. No big deal. */ |
| 862 |
do { |
| 863 |
if( timeout == 0 ) psleep = NULL; |
| 864 |
ret=select( fd + 1, &rfds, NULL, NULL, psleep ); |
| 865 |
} while (ret < 0 && errno==EINTR); |
| 866 |
if( ret == 0 ) break; |
| 867 |
if( ret < 0 ) return -1; |
| 868 |
ret = read( fd, buffer + bytes, left ); |
| 869 |
if( ret == 0 ) break; |
| 870 |
if( ret < 0 ) return -1; |
| 871 |
bytes += ret; |
| 872 |
left -= ret; |
| 873 |
} |
| 874 |
return bytes; |
| 875 |
} |
| 876 |
|
| 877 |
/*---------------------------------------------------------- |
| 878 |
NativeEnableReceiveTimeoutThreshold |
| 879 |
accept: int threshold, int vtime,int buffer |
| 880 |
perform: Set c_cc->VMIN to threshold and c_cc=>VTIME to vtime |
| 881 |
return: void |
| 882 |
exceptions: IOException |
| 883 |
comments: This is actually all handled in read with select in |
| 884 |
canonical input mode. |
| 885 |
----------------------------------------------------------*/ |
| 886 |
|
| 887 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_NativeEnableReceiveTimeoutThreshold(JNIEnv *env, jobject jobj, jint vtime, jint threshold, jint buffer) |
| 888 |
{ |
| 889 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 890 |
struct termios ttyset; |
| 891 |
|
| 892 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 893 |
ttyset.c_cc[ VMIN ] = threshold; |
| 894 |
ttyset.c_cc[ VTIME ] = vtime/100; |
| 895 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 896 |
|
| 897 |
return; |
| 898 |
fail: |
| 899 |
throw_java_exception( env, IO_EXCEPTION, "TimeoutThreshold", strerror( errno ) ); |
| 900 |
return; |
| 901 |
} |
| 902 |
|
| 903 |
/*---------------------------------------------------------- |
| 904 |
RS485Port.readByte |
| 905 |
|
| 906 |
accept: none |
| 907 |
perform: Read a single byte from the port |
| 908 |
return: The byte read |
| 909 |
exceptions: IOException |
| 910 |
----------------------------------------------------------*/ |
| 911 |
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_readByte( JNIEnv *env, |
| 912 |
jobject jobj ) |
| 913 |
{ |
| 914 |
int bytes; |
| 915 |
unsigned char buffer[ 1 ]; |
| 916 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 917 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 918 |
|
| 919 |
bytes = read_byte_array( fd, buffer, 1, timeout ); |
| 920 |
if( bytes < 0 ) { |
| 921 |
throw_java_exception( env, IO_EXCEPTION, "readByte", |
| 922 |
strerror( errno ) ); |
| 923 |
return -1; |
| 924 |
} |
| 925 |
return (bytes ? (jint)buffer[ 0 ] : -1); |
| 926 |
} |
| 927 |
|
| 928 |
/*---------------------------------------------------------- |
| 929 |
RS485Port.readArray |
| 930 |
|
| 931 |
accept: offset (offset to start storing data in the jbarray) and |
| 932 |
Length (bytes to read) |
| 933 |
perform: read bytes from the port into a byte array |
| 934 |
return: bytes read on success |
| 935 |
0 on read timeout |
| 936 |
exceptions: IOException |
| 937 |
comments: throws ArrayIndexOutOfBoundsException if asked to |
| 938 |
read more than SSIZE_MAX bytes |
| 939 |
----------------------------------------------------------*/ |
| 940 |
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_readArray( JNIEnv *env, |
| 941 |
jobject jobj, jbyteArray jbarray, jint offset, jint length ) |
| 942 |
{ |
| 943 |
int bytes; |
| 944 |
jbyte *body; |
| 945 |
unsigned char *buffer; |
| 946 |
int fd = get_java_var( env, jobj, "fd", "I" ); |
| 947 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 948 |
|
| 949 |
if( (size_t) length > SSIZE_MAX || (size_t) length < 0 ) { |
| 950 |
throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS, |
| 951 |
"readArray", "Invalid length" ); |
| 952 |
return -1; |
| 953 |
} |
| 954 |
|
| 955 |
buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length ); |
| 956 |
if( buffer == 0 ) { |
| 957 |
throw_java_exception( env, OUT_OF_MEMORY, "readArray", |
| 958 |
"Unable to allocate buffer" ); |
| 959 |
return -1; |
| 960 |
} |
| 961 |
|
| 962 |
bytes = read_byte_array( fd, buffer, length, timeout ); |
| 963 |
if( bytes < 0 ) { |
| 964 |
free( buffer ); |
| 965 |
throw_java_exception( env, IO_EXCEPTION, "readArray", |
| 966 |
strerror( errno ) ); |
| 967 |
return -1; |
| 968 |
} |
| 969 |
body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 970 |
memcpy(body + offset, buffer, bytes); |
| 971 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 972 |
free( buffer ); |
| 973 |
return (bytes ? bytes : -1); |
| 974 |
} |
| 975 |
|
| 976 |
/*---------------------------------------------------------- |
| 977 |
RS485Port.nativeavailable |
| 978 |
|
| 979 |
accept: none |
| 980 |
perform: find out the number of bytes available for reading |
| 981 |
return: available bytes |
| 982 |
-1 on error |
| 983 |
exceptions: none |
| 984 |
----------------------------------------------------------*/ |
| 985 |
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_nativeavailable( JNIEnv *env, |
| 986 |
jobject jobj ) |
| 987 |
{ |
| 988 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 989 |
int result; |
| 990 |
|
| 991 |
if( ioctl( fd, FIONREAD, &result ) ) |
| 992 |
{ |
| 993 |
throw_java_exception( env, IO_EXCEPTION, "nativeavailable", strerror( errno ) ); |
| 994 |
return -1; |
| 995 |
} |
| 996 |
else return (jint)result; |
| 997 |
|
| 998 |
} |
| 999 |
|
| 1000 |
/*---------------------------------------------------------- |
| 1001 |
RS485Port.setflowcontrol |
| 1002 |
|
| 1003 |
accept: flowmode |
| 1004 |
FLOWCONTROL_NONE none |
| 1005 |
FLOWCONTROL_RTSCTS_IN hardware flow control |
| 1006 |
FLOWCONTROL_RTSCTS_OUT "" |
| 1007 |
FLOWCONTROL_XONXOFF_IN input software flow control |
| 1008 |
FLOWCONTROL_XONXOFF_OUT output software flow control |
| 1009 |
perform: set flow control to flowmode |
| 1010 |
return: none |
| 1011 |
exceptions: IOException |
| 1012 |
comments: there is no differentiation between input and output hardware |
| 1013 |
flow control |
| 1014 |
----------------------------------------------------------*/ |
| 1015 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setflowcontrol( JNIEnv *env, |
| 1016 |
jobject jobj, jint flowmode ) |
| 1017 |
{ |
| 1018 |
struct termios ttyset; |
| 1019 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1020 |
|
| 1021 |
if( tcgetattr( fd, &ttyset ) ) goto fail; |
| 1022 |
|
| 1023 |
if ( flowmode & ( FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT ) ) |
| 1024 |
ttyset.c_cflag |= HARDWARE_FLOW_CONTROL; |
| 1025 |
else ttyset.c_cflag &= ~HARDWARE_FLOW_CONTROL; |
| 1026 |
|
| 1027 |
ttyset.c_iflag &= ~IXANY; |
| 1028 |
|
| 1029 |
if ( flowmode & FLOWCONTROL_XONXOFF_IN ) |
| 1030 |
ttyset.c_iflag |= IXOFF; |
| 1031 |
else ttyset.c_iflag &= ~IXOFF; |
| 1032 |
|
| 1033 |
if ( flowmode & FLOWCONTROL_XONXOFF_OUT ) |
| 1034 |
ttyset.c_iflag |= IXON; |
| 1035 |
else ttyset.c_iflag &= ~IXON; |
| 1036 |
|
| 1037 |
if( tcsetattr( fd, TCSANOW, &ttyset ) ) goto fail; |
| 1038 |
return; |
| 1039 |
fail: |
| 1040 |
throw_java_exception( env, IO_EXCEPTION, "setHWFC", |
| 1041 |
strerror( errno ) ); |
| 1042 |
return; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/*---------------------------------------------------------- |
| 1046 |
RS485Port.eventLoop |
| 1047 |
|
| 1048 |
accept: none |
| 1049 |
perform: periodically check for RS485PortEvents |
| 1050 |
return: none |
| 1051 |
exceptions: none |
| 1052 |
comments: FIXME This is probably wrong on bsd. |
| 1053 |
----------------------------------------------------------*/ |
| 1054 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_eventLoop( JNIEnv *env, |
| 1055 |
jobject jobj ) |
| 1056 |
{ |
| 1057 |
int fd, ret, change; |
| 1058 |
fd_set rfds; |
| 1059 |
struct timeval tv_sleep; |
| 1060 |
unsigned int mflags; |
| 1061 |
#if defined(TIOCGICOUNT) |
| 1062 |
struct serial_icounter_struct sis, osis; |
| 1063 |
#endif /* TIOCGICOUNT */ |
| 1064 |
unsigned int omflags; |
| 1065 |
|
| 1066 |
jmethodID method, interrupt; |
| 1067 |
jboolean interrupted = 0; |
| 1068 |
jclass jclazz, jthread; |
| 1069 |
jclazz = (*env)->GetObjectClass( env, jobj ); |
| 1070 |
fd = get_java_var(env, jobj, "fd", "I"); |
| 1071 |
method = (*env)->GetMethodID( env, jclazz, "sendEvent", "(IZ)V" ); |
| 1072 |
jthread = (*env)->FindClass( env, "java/lang/Thread" ); |
| 1073 |
interrupt = (*env)->GetStaticMethodID( env, jthread, "interrupted", "()Z" ); |
| 1074 |
|
| 1075 |
/* Some multiport RS485 cards do not implement TIOCGICOUNT ... */ |
| 1076 |
#if defined(TIOCGICOUNT) |
| 1077 |
if( ioctl( fd, TIOCGICOUNT, &osis ) < 0 ) { |
| 1078 |
fprintf( stderr, "Port does not support TIOCGICOUNT events\n" ); |
| 1079 |
return; |
| 1080 |
} |
| 1081 |
#else |
| 1082 |
fprintf( stderr, "Port does not support all Hardware events\n" ); |
| 1083 |
#endif /* TIOCGICOUNT */ |
| 1084 |
|
| 1085 |
if( ioctl( fd, TIOCMGET, &omflags) <0 ) { |
| 1086 |
fprintf( stderr, "Port does not support events\n" ); |
| 1087 |
return; |
| 1088 |
} |
| 1089 |
|
| 1090 |
FD_ZERO( &rfds ); |
| 1091 |
while( !interrupted ) { |
| 1092 |
FD_SET( fd, &rfds ); |
| 1093 |
/* Check every 1 second, or on receive data */ |
| 1094 |
tv_sleep.tv_sec = 1; |
| 1095 |
tv_sleep.tv_usec = 0; |
| 1096 |
do { |
| 1097 |
ret=select( fd + 1, &rfds, NULL, NULL, &tv_sleep ); |
| 1098 |
} while (ret < 0 && errno==EINTR); |
| 1099 |
if( ret < 0 ) { |
| 1100 |
fprintf( stderr, "select() Failed\n" ); |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
|
| 1104 |
#if defined TIOCSERGETLSR |
| 1105 |
if( ioctl( fd, TIOCSERGETLSR, &change ) ) { |
| 1106 |
fprintf( stderr, "TIOCSERGETLSR Failed\n" ); |
| 1107 |
break; |
| 1108 |
} |
| 1109 |
else if( change ) { |
| 1110 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1111 |
(jint)SPE_OUTPUT_BUFFER_EMPTY, JNI_TRUE ); |
| 1112 |
} |
| 1113 |
#endif /* TIOCSERGETLSR */ |
| 1114 |
#if defined(TIOCGICOUNT) |
| 1115 |
/* wait for RNG, DSR, CD or CTS but not DataAvailable |
| 1116 |
* The drawback here is it never times out so if someone |
| 1117 |
* reads there will be no chance to try again. |
| 1118 |
* This may make sense if the program does not want to |
| 1119 |
* be notified of data available or errors. |
| 1120 |
* ret=ioctl(fd,TIOCMIWAIT); |
| 1121 |
*/ |
| 1122 |
if( ioctl( fd, TIOCGICOUNT, &sis ) ) { |
| 1123 |
fprintf( stderr, "TIOCGICOUNT Failed\n" ); |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
while( sis.frame != osis.frame ) { |
| 1127 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_FE, JNI_TRUE ); |
| 1128 |
osis.frame++; |
| 1129 |
} |
| 1130 |
while( sis.overrun != osis.overrun ) { |
| 1131 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_OE, JNI_TRUE ); |
| 1132 |
osis.overrun++; |
| 1133 |
} |
| 1134 |
while( sis.parity != osis.parity ) { |
| 1135 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_PE, JNI_TRUE ); |
| 1136 |
osis.parity++; |
| 1137 |
} |
| 1138 |
while( sis.brk != osis.brk ) { |
| 1139 |
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_BI, JNI_TRUE ); |
| 1140 |
osis.brk++; |
| 1141 |
} |
| 1142 |
osis = sis; |
| 1143 |
#endif /* TIOCGICOUNT */ |
| 1144 |
if( ioctl( fd, TIOCMGET, &mflags ) ) { |
| 1145 |
fprintf( stderr, "TIOCMGET Failed\n" ); |
| 1146 |
break; |
| 1147 |
} |
| 1148 |
interrupted = (*env)->CallStaticBooleanMethod( env, jthread, interrupt ); |
| 1149 |
/* A Portable implementation */ |
| 1150 |
change = (mflags&TIOCM_CTS) - (omflags&TIOCM_CTS); |
| 1151 |
if( change ) { |
| 1152 |
fprintf(stderr, "Sending SPE_CTS\n"); |
| 1153 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1154 |
(jint)SPE_CTS, JNI_TRUE ); |
| 1155 |
} |
| 1156 |
change = (mflags&TIOCM_DSR) - (omflags&TIOCM_DSR); |
| 1157 |
if( change ) { |
| 1158 |
fprintf(stderr, "Sending SPE_DSR\n"); |
| 1159 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1160 |
(jint)SPE_DSR, JNI_TRUE ); |
| 1161 |
} |
| 1162 |
change = (mflags&TIOCM_RNG) - (omflags&TIOCM_RNG); |
| 1163 |
if( change ) { |
| 1164 |
fprintf(stderr, "Sending SPE_RI\n"); |
| 1165 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1166 |
(jint)SPE_RI, JNI_TRUE ); |
| 1167 |
} |
| 1168 |
change = (mflags&TIOCM_CD) - (omflags&TIOCM_CD); |
| 1169 |
if( change ) { |
| 1170 |
fprintf(stderr, "Sending SPE_CD\n"); |
| 1171 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1172 |
(jint)SPE_CD, JNI_TRUE ); |
| 1173 |
} |
| 1174 |
omflags = mflags; |
| 1175 |
if( ioctl( fd, FIONREAD, &change ) ) { |
| 1176 |
fprintf( stderr, "FIONREAD Failed\n" ); |
| 1177 |
} |
| 1178 |
else if( change ) { |
| 1179 |
(*env)->CallVoidMethod( env, jobj, method, |
| 1180 |
(jint)SPE_DATA_AVAILABLE, JNI_TRUE ); |
| 1181 |
usleep(1000); /* select wont block */ |
| 1182 |
} |
| 1183 |
} |
| 1184 |
return; |
| 1185 |
} |
| 1186 |
|
| 1187 |
/*---------------------------------------------------------- |
| 1188 |
send_modem_events |
| 1189 |
|
| 1190 |
accept: int event RS485PortEvent constant |
| 1191 |
int change Number of times this event happened |
| 1192 |
int state current state: 0 is false, nonzero is true |
| 1193 |
perform: Send the necessary events |
| 1194 |
return: none |
| 1195 |
exceptions: none |
| 1196 |
comments: Since the interrupt counters tell us how many times the |
| 1197 |
state has changed, we can send a RS485PortEvent for each |
| 1198 |
interrupt (change) that has occured. If we don't do this, |
| 1199 |
we'll miss a whole bunch of events. |
| 1200 |
----------------------------------------------------------*/ |
| 1201 |
void send_modem_events( JNIEnv *env, jobject jobj, jmethodID method, |
| 1202 |
int event, int change, int state ) |
| 1203 |
{ |
| 1204 |
int i, s; |
| 1205 |
jboolean flag; |
| 1206 |
if( state ) s = 1; |
| 1207 |
else s = 0; |
| 1208 |
|
| 1209 |
for( i = 0; i < change; i++ ) { |
| 1210 |
if( ( change + s + i ) % 2 ) flag = JNI_FALSE; |
| 1211 |
else flag = JNI_TRUE; |
| 1212 |
(*env)->CallVoidMethod( env, jobj, method, (jint)event, flag ); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
/*---------------------------------------------------------- |
| 1217 |
get_java_fd |
| 1218 |
|
| 1219 |
accept: env (keyhole to java) |
| 1220 |
jobj (java RS485Port object) |
| 1221 |
return: the fd field from the java object |
| 1222 |
exceptions: none |
| 1223 |
comments: |
| 1224 |
----------------------------------------------------------*/ |
| 1225 |
int get_java_var( JNIEnv *env, jobject jobj, char *id, char *type ) |
| 1226 |
{ |
| 1227 |
int result = 0; |
| 1228 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 1229 |
jfieldID jfd = (*env)->GetFieldID( env, jclazz, id, type ); |
| 1230 |
if( !jfd ) { |
| 1231 |
(*env)->ExceptionDescribe( env ); |
| 1232 |
(*env)->ExceptionClear( env ); |
| 1233 |
return result; |
| 1234 |
} |
| 1235 |
result = (int)( (*env)->GetIntField( env, jobj, jfd ) ); |
| 1236 |
/* ct7 & gel * Added DeleteLocalRef */ |
| 1237 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 1238 |
return result; |
| 1239 |
} |
| 1240 |
|
| 1241 |
/*---------------------------------------------------------- |
| 1242 |
throw_java_exception |
| 1243 |
|
| 1244 |
accept: env (keyhole to java) |
| 1245 |
*exc (exception class name) |
| 1246 |
*foo (function name) |
| 1247 |
*msg (error message) |
| 1248 |
perform: Throw a new java exception |
| 1249 |
return: none |
| 1250 |
exceptions: haha! |
| 1251 |
comments: |
| 1252 |
----------------------------------------------------------*/ |
| 1253 |
void throw_java_exception( JNIEnv *env, char *exc, char *foo, char *msg ) |
| 1254 |
{ |
| 1255 |
char buf[ 60 ]; |
| 1256 |
jclass clazz = (*env)->FindClass( env, exc ); |
| 1257 |
if( !clazz ) { |
| 1258 |
(*env)->ExceptionDescribe( env ); |
| 1259 |
(*env)->ExceptionClear( env ); |
| 1260 |
return; |
| 1261 |
} |
| 1262 |
#if defined(_GNU_SOURCE) |
| 1263 |
snprintf( buf, 60, "%s in %s", msg, foo ); |
| 1264 |
#else |
| 1265 |
sprintf( buf,"%s in %s", msg, foo ); |
| 1266 |
#endif /* _GNU_SOURCE */ |
| 1267 |
(*env)->ThrowNew( env, clazz, buf ); |
| 1268 |
/* ct7 * Added DeleteLocalRef */ |
| 1269 |
(*env)->DeleteLocalRef( env, clazz ); |
| 1270 |
} |
| 1271 |
|
| 1272 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_IsDeviceGood(JNIEnv *env, |
| 1273 |
jobject jobj, jstring tty_name){ |
| 1274 |
|
| 1275 |
jboolean result; |
| 1276 |
static struct stat mystat; |
| 1277 |
char teststring[256]; |
| 1278 |
int fd,i; |
| 1279 |
const char *name = (*env)->GetStringUTFChars(env, tty_name, 0); |
| 1280 |
|
| 1281 |
#if defined(__linux__) |
| 1282 |
if(!strcmp(name,"tty0")|| !strcmp(name,"ttyd")|| |
| 1283 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1284 |
!strcmp(name,"ttyf")|| !strcmp(name,"cuaa") |
| 1285 |
) |
| 1286 |
{ |
| 1287 |
#ifdef DEBUG |
| 1288 |
fprintf(stderr,"DEBUG: Ignoring Port %s\*\n",name); |
| 1289 |
#endif |
| 1290 |
return(JNI_FALSE); |
| 1291 |
} |
| 1292 |
#endif |
| 1293 |
#if defined(__FreeBSD__) |
| 1294 |
if(!strcmp(name,"tty0")|| !strcmp(name,"ttyd")|| |
| 1295 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1296 |
!strcmp(name,"ttyf")|| !strcmp(name,"ttyS")|| |
| 1297 |
!strcmp(name,"ttyI")|| !strcmp(name,"ttyW")|| |
| 1298 |
!strcmp(name,"ttyC")|| !strcmp(name,"ttyR") |
| 1299 |
) |
| 1300 |
{ |
| 1301 |
#ifdef DEBUG |
| 1302 |
fprintf(stderr,"DEBUG: Ignoring Port %s*\n",name); |
| 1303 |
#endif |
| 1304 |
return(JNI_FALSE); |
| 1305 |
} |
| 1306 |
#endif |
| 1307 |
#if defined(__NetBSD__) |
| 1308 |
if( !strcmp(name,"ttyd")|| |
| 1309 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1310 |
!strcmp(name,"ttyf")|| !strcmp(name,"ttyS")|| |
| 1311 |
!strcmp(name,"ttyI")|| !strcmp(name,"ttyW")|| |
| 1312 |
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")|| |
| 1313 |
!strcmp(name,"ttyf")|| !strcmp(name,"cuaa")|| |
| 1314 |
!strcmp(name,"ttyC")|| !strcmp(name,"ttyR")|| |
| 1315 |
!strcmp(name,"ttyM") |
| 1316 |
) |
| 1317 |
{ |
| 1318 |
#ifdef DEBUG |
| 1319 |
fprintf(stderr,"DEBUG: Ignoring Port %s*\n",name); |
| 1320 |
#endif |
| 1321 |
return(JNI_FALSE); |
| 1322 |
} |
| 1323 |
#endif |
| 1324 |
for(i=0;i<64;i++){ |
| 1325 |
sprintf(teststring,"/dev/%s%i",name, i); |
| 1326 |
stat(teststring,&mystat); |
| 1327 |
if(S_ISCHR(mystat.st_mode)){ |
| 1328 |
fd=open(teststring,O_RDONLY|O_NONBLOCK); |
| 1329 |
if (fd>0){ |
| 1330 |
close(fd); |
| 1331 |
result=JNI_TRUE; |
| 1332 |
break; |
| 1333 |
} |
| 1334 |
result=JNI_FALSE; |
| 1335 |
} |
| 1336 |
else result=JNI_FALSE; |
| 1337 |
} |
| 1338 |
sprintf(teststring,"/dev/%s",name); |
| 1339 |
stat(teststring,&mystat); |
| 1340 |
if(S_ISCHR(mystat.st_mode)){ |
| 1341 |
fd=open(teststring,O_RDONLY|O_NONBLOCK); |
| 1342 |
if (fd>0){ |
| 1343 |
close(fd); |
| 1344 |
result=JNI_TRUE; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
(*env)->ReleaseStringUTFChars(env, tty_name, name); |
| 1348 |
return(result); |
| 1349 |
} |
| 1350 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setInputBufferSize(JNIEnv *env, jobject jobj, jint size ) |
| 1351 |
{ |
| 1352 |
#ifdef DEBUG |
| 1353 |
fprintf(stderr,"setInputBufferSize is not implemented\n"); |
| 1354 |
#endif |
| 1355 |
} |
| 1356 |
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_getInputBufferSize(JNIEnv *env, jobject jobj) |
| 1357 |
{ |
| 1358 |
#ifdef DEBUG |
| 1359 |
fprintf(stderr,"getInputBufferSize is not implemented\n"); |
| 1360 |
#endif |
| 1361 |
return(1); |
| 1362 |
} |
| 1363 |
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setOutputBufferSize(JNIEnv *env, jobject jobj, jint size ) |
| 1364 |
{ |
| 1365 |
#ifdef DEBUG |
| 1366 |
fprintf(stderr,"setOutputBufferSize is not implemented\n"); |
| 1367 |
#endif |
| 1368 |
} |
| 1369 |
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_getOutputBufferSize(JNIEnv *env, jobject jobj) |
| 1370 |
{ |
| 1371 |
#ifdef DEBUG |
| 1372 |
fprintf(stderr,"getOutputBufferSize is not implemented\n"); |
| 1373 |
#endif |
| 1374 |
return(1); |
| 1375 |
} |
| 1376 |
|
| 1377 |
void dump_termios(char *foo,struct termios *ttyset) |
| 1378 |
{ |
| 1379 |
int i; |
| 1380 |
|
| 1381 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_iflag); |
| 1382 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_lflag); |
| 1383 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_oflag); |
| 1384 |
fprintf(stderr,"%s %o\n",foo,ttyset->c_cflag); |
| 1385 |
for(i=0;i<NCCS;i++) |
| 1386 |
{ |
| 1387 |
fprintf(stderr,"%s %o ",foo,ttyset->c_cc[i]); |
| 1388 |
} |
| 1389 |
fprintf(stderr,"\n"); |
| 1390 |
} |