| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| •58 |
#define _GNU_SOURCE |
| 59 |
#if defined(__MWERKS__) |
| 60 |
#include "RXTXPort.h" |
| 61 |
#else |
| 62 |
#ifndef WIN32 |
| 63 |
# include "config.h" |
| 64 |
#endif |
| 65 |
#include "gnu_io_RXTXPort.h" |
| 66 |
#endif |
| 67 |
#ifdef __LCC__ |
| 68 |
# include<winsock.h> |
| 69 |
#endif |
| 70 |
#include <time.h> |
| 71 |
#include <stdio.h> |
| 72 |
#include <string.h> |
| 73 |
#include <stdlib.h> |
| 74 |
#include <errno.h> |
| 75 |
#include <limits.h> |
| 76 |
#include <sys/stat.h> |
| 77 |
#ifndef WIN32 |
| 78 |
#include <unistd.h> |
| 79 |
#include <sys/ioctl.h> |
| 80 |
#include <sys/param.h> |
| 81 |
#include <sys/utsname.h> |
| 82 |
#include <pthread.h> |
| 83 |
#else |
| 84 |
# include "win32termios.h" |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
# if !defined(S_ISCHR) |
| 89 |
# define S_ISCHR(m) (1) |
| 90 |
# endif |
| 91 |
#endif |
| 92 |
#ifdef HAVE_TERMIOS_H |
| 93 |
# include <termios.h> |
| 94 |
#endif |
| 95 |
# include <signal.h> |
| 96 |
#ifdef HAVE_SIGNAL_H |
| 97 |
# include <signal.h> |
| 98 |
#endif |
| 99 |
#ifdef HAVE_SYS_SIGNAL_H |
| 100 |
# include <sys/signal.h> |
| 101 |
#endif |
| 102 |
#include <sys/types.h> |
| 103 |
#ifdef HAVE_SYS_TIME_H |
| 104 |
# include <sys/time.h> |
| 105 |
#endif |
| 106 |
# include <fcntl.h> |
| 107 |
#ifdef HAVE_SYS_FCNTL_H |
| 108 |
# include <sys/fcntl.h> |
| 109 |
#endif |
| 110 |
#ifdef HAVE_SYS_FILE_H |
| 111 |
# include <sys/file.h> |
| 112 |
#endif |
| 113 |
#ifdef LFS |
| 114 |
# include <sys/socket.h> |
| 115 |
# include <netinet/in.h> |
| 116 |
# include <arpa/inet.h> |
| 117 |
#endif |
| 118 |
#if defined(__linux__) |
| 119 |
# include <linux/types.h> |
| 120 |
# include <linux/serial.h> |
| 121 |
# include <linux/version.h> |
| 122 |
#endif |
| 123 |
#if defined(__sun__) |
| 124 |
# include <sys/filio.h> |
| 125 |
# include <sys/mkdev.h> |
| 126 |
#endif |
| 127 |
#if defined(__hpux__) |
| 128 |
# include <sys/modem.h> |
| 129 |
#endif |
| 130 |
|
| 131 |
#if defined(__APPLE__) |
| 132 |
# include <CoreFoundation/CoreFoundation.h> |
| 133 |
# include <IOKit/IOKitLib.h> |
| 134 |
# include <IOKit/serial/IOSerialKeys.h> |
| 135 |
# include <IOKit/IOBSD.h> |
| 136 |
#endif |
| 137 |
#ifdef __unixware__ |
| 138 |
# include <sys/filio.h> |
| 139 |
#endif |
| 140 |
#ifdef HAVE_PWD_H |
| 141 |
#include <pwd.h> |
| 142 |
#endif |
| 143 |
#ifdef HAVE_GRP_H |
| 144 |
#include <grp.h> |
| 145 |
#endif |
| 146 |
#include <math.h> |
| 147 |
#ifdef LIBLOCKDEV |
| 148 |
#include <lockdev.h> |
| 149 |
#endif |
| •150 |
#include <sys/sysmacros.h> |
| 151 |
|
| 152 |
extern int errno; |
| 153 |
|
| 154 |
#include "SerialImp.h" |
| 155 |
|
| 156 |
JavaVM *javaVM = NULL; |
| 157 |
|
| 158 |
|
| 159 |
struct preopened *preopened_port = NULL; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
#define RXTXPort(foo) Java_gnu_io_RXTXPort_ ## foo |
| 165 |
#define RXTXVersion(foo) Java_gnu_io_RXTXVersion_ ## foo |
| 166 |
#define RXTXCommDriver(foo) Java_gnu_io_RXTXCommDriver_ ## foo |
| 167 |
|
| 168 |
#if defined(__sun__) || defined(__hpux__) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
int cfmakeraw ( struct termios *term ) |
| 181 |
{ |
| 182 |
ENTER( "cfmakeraw" ); |
| 183 |
term->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
| 184 |
term->c_oflag &= ~OPOST; |
| 185 |
term->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
| 186 |
term->c_cflag &= ~(CSIZE|PARENB); |
| 187 |
term->c_cflag |= CS8; |
| 188 |
LEAVE( "cfmakeraw" ); |
| 189 |
return( 0 ); |
| 190 |
} |
| 191 |
#endif |
| 192 |
|
| 193 |
#ifdef DEBUG_TIMING |
| 194 |
struct timeval snow, enow, seloop, eeloop; |
| 195 |
#define report_time_eventLoop( ) { \ |
| 196 |
if ( seloop.tv_sec == eeloop.tv_sec && seloop.tv_usec == eeloop.tv_usec ) \ |
| 197 |
{ \ |
| 198 |
gettimeofday(&eeloop, NULL); \ |
| 199 |
seloop.tv_sec = eeloop.tv_sec; \ |
| 200 |
seloop.tv_usec = eeloop.tv_usec; \ |
| 201 |
printf("%8i sec : %8i usec\n", eeloop.tv_sec - seloop.tv_sec, eeloop.tv_usec - seloop.tv_usec); \ |
| 202 |
} \ |
| 203 |
} |
| 204 |
#define report_time( ) \ |
| 205 |
{ \ |
| 206 |
struct timeval now; \ |
| 207 |
gettimeofday(&now, NULL); \ |
| 208 |
mexPrintf("%8s : %5i : %8i sec : %8i usec\n", __TIME__, __LINE__, now.tv_sec, now.tv_usec); \ |
| 209 |
} |
| 210 |
#define report_time_start( ) \ |
| 211 |
{ \ |
| 212 |
gettimeofday(&snow, NULL); \ |
| 213 |
mexPrintf("%8s : %5i : %8i sec : %8i usec", __TIME__, __LINE__, snow.tv_sec, snow.tv_usec); \ |
| 214 |
} |
| 215 |
#define report_time_end( ) \ |
| 216 |
{ \ |
| 217 |
gettimeofday(&enow, NULL); \ |
| 218 |
mexPrintf("%8i sec : %8i usec\n", enow.tv_sec - snow.tv_sec, enow.tv_sec - snow.tv_sec?snow.tv_usec-enow.tv_usec:enow.tv_usec - snow.tv_usec); \ |
| 219 |
} |
| 220 |
#else |
| 221 |
#define report_time_eventLoop( ){}; |
| 222 |
#define report_time( ) {} |
| 223 |
#define report_time_start( ) {} |
| 224 |
#define report_time_end( ) {} |
| 225 |
#endif |
| 226 |
|
| 227 |
|
| 228 |
struct event_info_struct *master_index = NULL; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
struct event_info_struct build_threadsafe_eis( |
| 246 |
JNIEnv *env, |
| 247 |
jobject *jobj, |
| 248 |
struct event_info_struct *eis |
| 249 |
) |
| 250 |
{ |
| 251 |
struct event_info_struct myeis = *eis; |
| 252 |
|
| 253 |
myeis.env = env; |
| 254 |
myeis.jclazz = (*env)->GetObjectClass( env, *jobj ); |
| 255 |
myeis.jobj = jobj; |
| 256 |
myeis.fd = get_java_var( env, *jobj, "fd", "I" ); |
| 257 |
myeis.send_event = (*env)->GetMethodID( |
| 258 |
env, |
| 259 |
myeis.jclazz, |
| 260 |
"sendEvent", |
| 261 |
"(IZ)Z" |
| 262 |
); |
| 263 |
return( myeis ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
JNIEXPORT void JNICALL RXTXPort(Initialize)( |
| 280 |
JNIEnv *env, |
| 281 |
jclass jclazz |
| 282 |
) |
| 283 |
{ |
| 284 |
#if defined DEBUG && defined(__linux__) |
| 285 |
struct utsname name; |
| 286 |
char message[80]; |
| 287 |
#endif |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
#if !defined(WIN32) |
| 295 |
struct sigaction old_action; |
| 296 |
sigaction(SIGIO, NULL, &old_action); |
| 297 |
|
| 298 |
if (old_action.sa_handler == NULL) { |
| 299 |
|
| 300 |
struct sigaction new_action; |
| 301 |
sigset_t block_mask; |
| 302 |
sigemptyset(&block_mask); |
| 303 |
new_action.sa_handler = SIG_IGN; |
| 304 |
#ifdef SA_RESTART |
| 305 |
new_action.sa_flags = SA_RESTART; |
| 306 |
#endif |
| 307 |
new_action.sa_mask = block_mask; |
| 308 |
sigaction(SIGIO, &new_action, NULL); |
| 309 |
} |
| 310 |
#endif |
| 311 |
ENTER( "RXTXPort:Initialize" ); |
| 312 |
#ifdef PRERELEASE |
| 313 |
|
| 314 |
#ifdef DEBUG_MW |
| 315 |
mexPrintf("RXTX Prerelease for testing Tue Feb 19 18:00:27 EST 2002\n"); |
| 316 |
#else |
| 317 |
printf("RXTX Prerelease for testing Thu Feb 21 19:31:38\n"); |
| 318 |
#endif |
| 319 |
#endif |
| 320 |
#if DEBUG_TIMING |
| 321 |
gettimeofday(&seloop, NULL); |
| 322 |
#endif |
| 323 |
#if defined(DEBUG) && defined(__linux__) |
| 324 |
|
| •325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| •339 |
|
| 340 |
LEAVE( "RXTXPort:Initialize" ); |
| 341 |
#endif |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
int find_preopened_ports( const char *filename ) |
| 359 |
{ |
| 360 |
int fd; |
| 361 |
struct preopened *p = preopened_port; |
| 362 |
|
| 363 |
if( !p ) |
| 364 |
{ |
| 365 |
return(0); |
| 366 |
} |
| 367 |
for(;;) |
| 368 |
{ |
| 369 |
if( !strcmp( p->filename, filename) ) |
| 370 |
{ |
| 371 |
fd = p->fd; |
| 372 |
if( p->prev && p->next ) |
| 373 |
{ |
| 374 |
p->prev->next = p->next; |
| 375 |
p->next->prev = p->prev; |
| 376 |
} |
| 377 |
else if ( p->prev ) |
| 378 |
{ |
| 379 |
p->prev->next = NULL; |
| 380 |
} |
| 381 |
else if ( p->next ) |
| 382 |
{ |
| 383 |
p->next->prev = NULL; |
| 384 |
} |
| 385 |
else |
| 386 |
{ |
| 387 |
free( p ); |
| 388 |
preopened_port = NULL; |
| 389 |
return( fd ); |
| 390 |
} |
| 391 |
free( p ); |
| 392 |
return( fd ); |
| 393 |
} |
| 394 |
if( p->next ) |
| 395 |
{ |
| 396 |
p = p->next; |
| 397 |
} |
| 398 |
else |
| 399 |
{ |
| 400 |
return(0); |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
int configure_port( int fd ) |
| 417 |
{ |
| 418 |
struct termios ttyset; |
| 419 |
|
| 420 |
if( fd < 0 ) goto fail; |
| 421 |
|
| 422 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 423 |
ttyset.c_iflag = INPCK; |
| 424 |
ttyset.c_lflag = 0; |
| 425 |
ttyset.c_oflag = 0; |
| 426 |
ttyset.c_cflag = CREAD | CS8 | CLOCAL; |
| 427 |
ttyset.c_cc[ VMIN ] = 0; |
| 428 |
ttyset.c_cc[ VTIME ] = 0; |
| 429 |
|
| •430 |
#ifdef __FreeBSD_kernel__ |
| 431 |
if( cfsetspeed( &ttyset, B9600 ) < 0 ) goto fail; |
| 432 |
#else |
| 433 |
if( cfsetispeed( &ttyset, B9600 ) < 0 ) goto fail; |
| 434 |
if( cfsetospeed( &ttyset, B9600 ) < 0 ) goto fail; |
| 435 |
#endif |
| 436 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 437 |
#ifndef WIN32 |
| 438 |
fcntl( fd, F_SETOWN, getpid() ); |
| 439 |
#endif |
| 440 |
#ifdef FASYNC |
| 441 |
fcntl( fd, F_SETFL, FASYNC ); |
| 442 |
#endif |
| 443 |
|
| 444 |
return 0; |
| 445 |
|
| 446 |
fail: |
| 447 |
return 1; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
int get_java_baudrate( int native_speed ) |
| 461 |
{ |
| 462 |
switch( native_speed ) |
| 463 |
{ |
| 464 |
case B0: return 0; |
| 465 |
case B50: return 50; |
| 466 |
case B75: return 75; |
| 467 |
case B110: return 110; |
| 468 |
case B134: return 134; |
| 469 |
case B150: return 150; |
| 470 |
case B200: return 200; |
| 471 |
case B300: return 300; |
| 472 |
case B600: return 600; |
| 473 |
case B1200: return 1200; |
| 474 |
case B1800: return 1800; |
| 475 |
case B2400: return 2400; |
| 476 |
case B4800: return 4800; |
| 477 |
case B9600: return 9600; |
| 478 |
#ifdef B14400 |
| 479 |
case B14400: return 14400; |
| 480 |
#endif |
| 481 |
case B19200: return 19200; |
| 482 |
#ifdef B28800 |
| 483 |
case B28800: return 28800; |
| 484 |
#endif |
| 485 |
case B38400: return 38400; |
| 486 |
case B57600: return 57600; |
| 487 |
|
| 488 |
#ifdef B115200 |
| 489 |
case B115200: return 115200; |
| 490 |
#endif |
| 491 |
#ifdef B128000 |
| 492 |
case B128000: return 128000; |
| 493 |
#endif |
| 494 |
#ifdef B230400 |
| 495 |
case B230400: return 230400; |
| 496 |
#endif |
| 497 |
#ifdef B256000 |
| 498 |
case B256000: return 256000; |
| 499 |
#endif |
| 500 |
#ifdef B460800 |
| 501 |
case B460800: return 460800; |
| 502 |
#endif |
| 503 |
#ifdef B500000 |
| 504 |
case B500000: return 500000; |
| 505 |
#endif |
| 506 |
#ifdef B576000 |
| 507 |
case B576000: return 576000; |
| 508 |
#endif |
| 509 |
#ifdef B921600 |
| 510 |
case B921600: return 921600; |
| 511 |
#endif |
| 512 |
#ifdef B1000000 |
| 513 |
case B1000000: return 1000000; |
| 514 |
#endif |
| 515 |
#ifdef B1152000 |
| 516 |
case B1152000: return 1152000; |
| 517 |
#endif |
| 518 |
#ifdef B1500000 |
| 519 |
case B1500000: return 1500000; |
| 520 |
#endif |
| 521 |
#ifdef B2000000 |
| 522 |
case B2000000: return 2000000; |
| 523 |
#endif |
| 524 |
#ifdef B2500000 |
| 525 |
case B2500000: return 2500000; |
| 526 |
#endif |
| 527 |
#ifdef B3000000 |
| 528 |
case B3000000: return 3000000; |
| 529 |
#endif |
| 530 |
#ifdef B3500000 |
| 531 |
case B3500000: return 3500000; |
| 532 |
#endif |
| 533 |
#ifdef B4000000 |
| 534 |
case B4000000: return 4000000; |
| 535 |
#endif |
| 536 |
default: return -1; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
void set_java_vars( JNIEnv *env, jobject jobj, int fd ) |
| 553 |
{ |
| 554 |
struct termios ttyset; |
| 555 |
int databits = -1; |
| 556 |
int jparity = -1; |
| 557 |
int stop_bits = STOPBITS_1_5; |
| 558 |
int baudrate; |
| 559 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 560 |
jfieldID jfspeed = (*env)->GetFieldID( env, jclazz, "speed", "I" ); |
| 561 |
jfieldID jfdataBits = |
| 562 |
(*env)->GetFieldID( env, jclazz, "dataBits", "I" ); |
| 563 |
jfieldID jfstopBits = |
| 564 |
(*env)->GetFieldID( env, jclazz, "stopBits", "I" ); |
| 565 |
jfieldID jfparity = |
| 566 |
(*env)->GetFieldID( env, jclazz, "parity", "I" ); |
| 567 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 568 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 569 |
{ |
| 570 |
report( "Cannot Get Serial Port Settings\n" ); |
| 571 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 572 |
return; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
switch( ttyset.c_cflag&CSIZE ) { |
| 577 |
case CS5: databits = JDATABITS_5; break; |
| 578 |
case CS6: databits = JDATABITS_6; break; |
| 579 |
case CS7: databits = JDATABITS_7; break; |
| 580 |
case CS8: databits = JDATABITS_8; break; |
| 581 |
} |
| 582 |
#ifdef CMSPAR |
| 583 |
switch( ttyset.c_cflag&(PARENB|PARODD|CMSPAR ) ) { |
| 584 |
#else |
| 585 |
switch( ttyset.c_cflag&(PARENB|PARODD) ) { |
| 586 |
#endif |
| 587 |
case 0: jparity = JPARITY_NONE; break; |
| 588 |
case PARENB: jparity = JPARITY_EVEN; break; |
| 589 |
case PARENB | PARODD: jparity = JPARITY_ODD; break; |
| 590 |
#ifdef CMSPAR |
| 591 |
case PARENB | PARODD | CMSPAR: jparity = JPARITY_MARK; break; |
| 592 |
case PARENB | CMSPAR: jparity = JPARITY_SPACE; break; |
| 593 |
#endif |
| 594 |
} |
| 595 |
switch( ttyset.c_cflag&(CSTOPB) ) { |
| 596 |
case 0: stop_bits = STOPBITS_1; break; |
| 597 |
case CSTOPB: |
| 598 |
if ( (ttyset.c_cflag & CSIZE) == CS5 ) { |
| 599 |
stop_bits = STOPBITS_1_5; |
| 600 |
} |
| 601 |
else { |
| 602 |
stop_bits = STOPBITS_2; |
| 603 |
} |
| 604 |
break; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
#if defined(CBAUD) |
| 623 |
baudrate = ttyset.c_cflag&CBAUD; |
| 624 |
#else |
| 625 |
baudrate = cfgetispeed(&ttyset); |
| 626 |
#endif |
| 627 |
(*env)->SetIntField(env, jobj, jfspeed, |
| 628 |
( jint ) get_java_baudrate(baudrate) ); |
| 629 |
(*env)->SetIntField(env, jobj, jfdataBits, ( jint ) databits ); |
| 630 |
(*env)->SetIntField(env, jobj, jfstopBits, ( jint ) stop_bits ); |
| 631 |
(*env)->SetIntField(env, jobj, jfparity, ( jint ) jparity ); |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
JNIEXPORT jint JNICALL RXTXPort(open)( |
| 646 |
JNIEnv *env, |
| 647 |
jobject jobj, |
| 648 |
jstring jstr |
| 649 |
) |
| 650 |
{ |
| 651 |
int fd; |
| 652 |
int pid = -1; |
| 653 |
char message[80]; |
| 654 |
const char *filename; |
| 655 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 656 |
jfieldID jfid = (*env)->GetFieldID( env, jclazz, "pid", "I" ); |
| 657 |
report_time_start( ); |
| 658 |
|
| 659 |
if( !jfid ) { |
| 660 |
(*env)->ExceptionDescribe( env ); |
| 661 |
(*env)->ExceptionClear( env ); |
| 662 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 663 |
return -1; |
| 664 |
} |
| 665 |
|
| 666 |
#ifndef WIN32 |
| 667 |
pid = getpid(); |
| 668 |
#endif |
| 669 |
|
| 670 |
(*env)->SetIntField(env, jobj, jfid, ( jint ) pid ); |
| 671 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 672 |
|
| 673 |
filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
ENTER( "RXTXPort:open" ); |
| 684 |
if ( LOCK( filename, pid ) ) |
| 685 |
{ |
| 686 |
sprintf( message, "open: locking has failed for %s\n", |
| 687 |
filename ); |
| 688 |
report( message ); |
| 689 |
goto fail; |
| 690 |
} |
| 691 |
else |
| 692 |
{ |
| 693 |
sprintf( message, "open: locking worked for %s\n", filename ); |
| 694 |
report( message ); |
| 695 |
} |
| 696 |
|
| 697 |
fd = find_preopened_ports( filename ); |
| 698 |
if( fd ) |
| 699 |
{ |
| 700 |
set_java_vars( env, jobj, fd ); |
| 701 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 702 |
return (jint)fd; |
| 703 |
} |
| 704 |
|
| 705 |
do { |
| 706 |
fd=OPEN (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 707 |
} while (fd < 0 && errno==EINTR); |
| 708 |
|
| 709 |
#ifdef OPEN_EXCL |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
if (fd >= 0 && (ioctl(fd, TIOCEXCL) == -1)) |
| 718 |
{ |
| 719 |
sprintf( message, "open: exclusive access denied for %s\n", |
| 720 |
filename ); |
| 721 |
report( message ); |
| 722 |
report_error( message ); |
| 723 |
|
| 724 |
close(fd); |
| 725 |
goto fail; |
| 726 |
} |
| 727 |
#endif |
| 728 |
|
| 729 |
if( configure_port( fd ) ) goto fail; |
| 730 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 731 |
sprintf( message, "open: fd returned is %i\n", fd ); |
| 732 |
report( message ); |
| 733 |
LEAVE( "RXTXPort:open" ); |
| 734 |
report_time_end( ); |
| 735 |
return (jint)fd; |
| 736 |
|
| 737 |
fail: |
| 738 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 739 |
LEAVE( "RXTXPort:open" ); |
| 740 |
throw_java_exception( env, PORT_IN_USE_EXCEPTION, "open", |
| 741 |
strerror( errno ) ); |
| 742 |
return -1; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
JNIEXPORT void JNICALL RXTXPort(nativeClose)( JNIEnv *env, |
| 754 |
jobject jobj,jstring jstr ) |
| 755 |
{ |
| 756 |
int result, pid; |
| 757 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 758 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 759 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 760 |
report_time_start( ); |
| 761 |
pid = get_java_var( env, jobj,"pid","I" ); |
| 762 |
|
| 763 |
report(">nativeClose pid\n"); |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
if( !pid ) { |
| 768 |
(*env)->ExceptionDescribe( env ); |
| 769 |
(*env)->ExceptionClear( env ); |
| 770 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 771 |
report("nativeClose: Close not detecting thread pid"); |
| 772 |
return; |
| 773 |
} |
| 774 |
report("<nativeClose: pid\n"); |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
ENTER( "RXTXPort:nativeClose" ); |
| 785 |
if (fd > 0) |
| 786 |
{ |
| 787 |
report("nativeClose: discarding remaining data (tcflush)\n"); |
| 788 |
|
| 789 |
tcflush(fd, TCIOFLUSH); |
| 790 |
do { |
| 791 |
report("nativeClose: calling close\n"); |
| 792 |
result=CLOSE (fd); |
| 793 |
} while ( result < 0 && errno == EINTR ); |
| 794 |
UNLOCK( filename, pid ); |
| 795 |
} |
| 796 |
report("nativeClose: Delete jclazz\n"); |
| 797 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 798 |
report("nativeClose: release filename\n"); |
| 799 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 800 |
LEAVE( "RXTXPort:nativeClose" ); |
| 801 |
report_time_end( ); |
| 802 |
return; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
int set_port_params( JNIEnv *env, int fd, int cspeed, int dataBits, |
| 819 |
int stopBits, int parity ) |
| 820 |
{ |
| 821 |
struct termios ttyset; |
| 822 |
int result = 0; |
| 823 |
#if defined(TIOCGSERIAL) |
| 824 |
struct serial_struct sstruct; |
| 825 |
#endif |
| 826 |
|
| 827 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 828 |
{ |
| 829 |
report( "set_port_params: Cannot Get Serial Port Settings\n" ); |
| 830 |
return(1); |
| 831 |
} |
| 832 |
|
| 833 |
if( translate_data_bits( env, &(ttyset.c_cflag), dataBits ) ) |
| 834 |
{ |
| 835 |
report( "set_port_params: Invalid Data Bits Selected\n" ); |
| 836 |
return(1); |
| 837 |
} |
| 838 |
|
| 839 |
if( translate_stop_bits( env, &(ttyset.c_cflag), stopBits ) ) |
| 840 |
{ |
| 841 |
report( "set_port_params: Invalid Stop Bits Selected\n" ); |
| 842 |
return(1); |
| 843 |
} |
| 844 |
|
| 845 |
if( translate_parity( env, &(ttyset.c_cflag), parity ) ) |
| 846 |
{ |
| 847 |
report( "set_port_params: Invalid Parity Selected\n" ); |
| 848 |
return(1); |
| 849 |
} |
| 850 |
|
| •851 |
#ifdef __FreeBSD_kernel__ |
| 852 |
if( cfsetspeed( &ttyset, cspeed ) < 0 ) |
| 853 |
{ |
| 854 |
report( "set_port_params: Cannot Set Speed\n" ); |
| 855 |
return( 1 ); |
| 856 |
} |
| •857 |
#endif |
| 858 |
if( !cspeed ) |
| 859 |
{ |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
ioctl( fd, TIOCMGET, &result ); |
| 867 |
result &= ~TIOCM_DTR; |
| 868 |
ioctl( fd, TIOCMSET, &result ); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| •877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
#if defined(TIOCGSERIAL) |
| •884 |
if ( cspeed == B38400 && ioctl( fd, TIOCGSERIAL, &sstruct ) == 0 ) |
| 885 |
cspeed = 38400; |
| 886 |
#endif |
| 887 |
if( cfsetispeed( &ttyset, cspeed ) < 0 || |
| 888 |
cfsetospeed( &ttyset, cspeed ) < 0 ) |
| 889 |
{ |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
#if defined(TIOCGSERIAL) |
| 908 |
if ( ioctl( fd, TIOCGSERIAL, &sstruct ) < 0 ) |
| 909 |
{ |
| 910 |
report( "set_port_params: Cannot Get Serial Port Settings\n" ); |
| 911 |
return(1); |
| 912 |
} |
| 913 |
sstruct.custom_divisor = ( sstruct.baud_base/cspeed ); |
| 914 |
#endif |
| •915 |
if( cfsetispeed( &ttyset, B38400 ) < 0 || |
| 916 |
cfsetospeed( &ttyset, B38400 ) < 0 ) |
| 917 |
{ |
| 918 |
|
| 919 |
report( "nativeSetSerialPortParams: Cannot Set Speed\n" ); |
| 920 |
return( 1 ); |
| 921 |
} |
| 922 |
#if defined(TIOCSSERIAL) |
| 923 |
|
| •924 |
|
| 925 |
|
| 926 |
|
| 927 |
if ( sstruct.baud_base < 1 || |
| •928 |
ioctl( fd, TIOCSSERIAL, &sstruct ) < 0 && cspeed != 38400 ) |
| 929 |
{ |
| 930 |
return( 1 ); |
| 931 |
} |
| 932 |
#endif |
| 933 |
} |
| 934 |
|
| 935 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) |
| 936 |
{ |
| 937 |
report("tcsetattr returns nonzero value!\n"); |
| 938 |
return( 1 ); |
| 939 |
} |
| 940 |
return(0); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeSetSerialPortParams)( |
| 952 |
JNIEnv *env, jobject jobj, jint speed, jint dataBits, jint stopBits, |
| 953 |
jint parity ) |
| 954 |
{ |
| 955 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 956 |
int cspeed = translate_speed( env, speed ); |
| 957 |
|
| 958 |
ENTER( "RXTXPort:nativeSetSerialPortParams" ); |
| 959 |
report_time_start( ); |
| 960 |
|
| 961 |
if (cspeed < 0 ) |
| 962 |
{ |
| 963 |
report(" invalid cspeed\n"); |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
return(1); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
if( set_port_params( env, fd, cspeed, dataBits, stopBits, parity ) ) |
| 975 |
{ |
| 976 |
report("set_port_params failed\n"); |
| 977 |
LEAVE( "RXTXPort:nativeSetSerialPortParams" ); |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
return(1); |
| 985 |
} |
| 986 |
|
| 987 |
LEAVE( "RXTXPort:nativeSetSerialPortParams" ); |
| 988 |
report_time_end( ); |
| 989 |
return(0); |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
int translate_speed( JNIEnv *env, jint speed ) |
| 1004 |
{ |
| 1005 |
LEAVE( "RXTXPort:translate_speed" ); |
| 1006 |
switch( speed ) { |
| 1007 |
case 0: return B0; |
| 1008 |
case 50: return B50; |
| 1009 |
case 75: return B75; |
| 1010 |
case 110: return B110; |
| 1011 |
case 134: return B134; |
| 1012 |
case 150: return B150; |
| 1013 |
case 200: return B200; |
| 1014 |
case 300: return B300; |
| 1015 |
case 600: return B600; |
| 1016 |
case 1200: return B1200; |
| 1017 |
case 1800: return B1800; |
| 1018 |
case 2400: return B2400; |
| 1019 |
case 4800: return B4800; |
| 1020 |
case 9600: return B9600; |
| 1021 |
#ifdef B14400 |
| 1022 |
case 14400: return B14400; |
| 1023 |
#endif |
| 1024 |
case 19200: return B19200; |
| 1025 |
#ifdef B28800 |
| 1026 |
case 28800: return B28800; |
| 1027 |
#endif |
| 1028 |
case 38400: return B38400; |
| 1029 |
case 57600: return B57600; |
| 1030 |
|
| 1031 |
#ifdef B115200 |
| 1032 |
case 115200: return B115200; |
| 1033 |
#endif |
| 1034 |
#ifdef B128000 |
| 1035 |
case 128000: return B128000; |
| 1036 |
#endif |
| 1037 |
#ifdef B230400 |
| 1038 |
case 230400: return B230400; |
| 1039 |
#endif |
| 1040 |
#ifdef B256000 |
| 1041 |
case 256000: return B256000; |
| 1042 |
#endif |
| 1043 |
#ifdef B460800 |
| 1044 |
case 460800: return B460800; |
| 1045 |
#endif |
| 1046 |
#ifdef B500000 |
| 1047 |
case 500000: return B500000; |
| 1048 |
#endif |
| 1049 |
#ifdef B576000 |
| 1050 |
case 576000: return B576000; |
| 1051 |
#endif |
| 1052 |
#ifdef B921600 |
| 1053 |
case 921600: return B921600; |
| 1054 |
#endif |
| 1055 |
#ifdef B1000000 |
| 1056 |
case 1000000: return B1000000; |
| 1057 |
#endif |
| 1058 |
#ifdef B1152000 |
| 1059 |
case 1152000: return B1152000; |
| 1060 |
#endif |
| 1061 |
#ifdef B1500000 |
| 1062 |
case 1500000: return B1500000; |
| 1063 |
#endif |
| 1064 |
#ifdef B2000000 |
| 1065 |
case 2000000: return B2000000; |
| 1066 |
#endif |
| 1067 |
#ifdef B2500000 |
| 1068 |
case 2500000: return B2500000; |
| 1069 |
#endif |
| 1070 |
#ifdef B3000000 |
| 1071 |
case 3000000: return B3000000; |
| 1072 |
#endif |
| 1073 |
#ifdef B3500000 |
| 1074 |
case 3500000: return B3500000; |
| 1075 |
#endif |
| 1076 |
#ifdef B4000000 |
| 1077 |
case 4000000: return B4000000; |
| 1078 |
#endif |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
if( speed >= 0 ) return speed; |
| 1083 |
else |
| 1084 |
{ |
| 1085 |
LEAVE( "RXTXPort:translate_speed: Error condition" ); |
| 1086 |
return -1; |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
int translate_data_bits( JNIEnv *env, tcflag_t *cflag, jint dataBits ) |
| 1099 |
{ |
| 1100 |
int temp = (*cflag) & ~CSIZE; |
| 1101 |
|
| 1102 |
ENTER( "translate_date_bits" ); |
| 1103 |
switch( dataBits ) { |
| 1104 |
case JDATABITS_5: |
| 1105 |
(*cflag) = temp | CS5; |
| 1106 |
return 0; |
| 1107 |
case JDATABITS_6: |
| 1108 |
(*cflag) = temp | CS6; |
| 1109 |
return 0; |
| 1110 |
case JDATABITS_7: |
| 1111 |
(*cflag) = temp | CS7; |
| 1112 |
return 0; |
| 1113 |
case JDATABITS_8: |
| 1114 |
(*cflag) = temp | CS8; |
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
LEAVE( "RXTXPort:translate_date_bits" ); |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
return 1; |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
int translate_stop_bits( JNIEnv *env, tcflag_t *cflag, jint stopBits ) |
| 1139 |
{ |
| 1140 |
ENTER( "translate_stop_bits" ); |
| 1141 |
switch( stopBits ) { |
| 1142 |
case STOPBITS_1: |
| 1143 |
(*cflag) &= ~CSTOPB; |
| 1144 |
LEAVE( "RXTXPort:translate_stop_bits" ); |
| 1145 |
return 0; |
| 1146 |
|
| 1147 |
case STOPBITS_1_5: |
| 1148 |
(*cflag) |= CSTOPB; |
| 1149 |
if ( translate_data_bits( env, cflag, JDATABITS_5 ) ) |
| 1150 |
return( 1 ); |
| 1151 |
return 0; |
| 1152 |
case STOPBITS_2: |
| 1153 |
(*cflag) |= CSTOPB; |
| 1154 |
LEAVE( "RXTXPort:translate_stop_bits" ); |
| 1155 |
return 0; |
| 1156 |
} |
| 1157 |
|
| 1158 |
LEAVE( "RXTXPort:translate_stop_bits" ); |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
return 1; |
| 1166 |
} |
| 1167 |
JNIEXPORT jint JNICALL RXTXPort(nativeGetFlowControlMode)(JNIEnv *env, jobject jobj, jint fd) |
| 1168 |
{ |
| 1169 |
struct termios ttyset; |
| 1170 |
int ret = 0; |
| 1171 |
|
| 1172 |
tcgetattr( fd, &ttyset ); |
| 1173 |
|
| 1174 |
if( ttyset.c_cflag & HARDWARE_FLOW_CONTROL ) |
| 1175 |
{ |
| 1176 |
ret |= ( FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT ); |
| 1177 |
} |
| 1178 |
if ( ttyset.c_iflag & IXOFF ) |
| 1179 |
{ |
| 1180 |
ret |= FLOWCONTROL_XONXOFF_IN; |
| 1181 |
} |
| 1182 |
if ( ttyset.c_iflag & IXON ) |
| 1183 |
{ |
| 1184 |
ret |= FLOWCONTROL_XONXOFF_OUT; |
| 1185 |
} |
| 1186 |
return( (jint) ret ); |
| 1187 |
} |
| 1188 |
JNIEXPORT jint JNICALL RXTXPort(nativeGetParity)(JNIEnv *env, jobject jobj, jint fd) |
| 1189 |
{ |
| 1190 |
struct termios ttyset; |
| 1191 |
|
| 1192 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 1193 |
{ |
| 1194 |
report("nativeGetParity: tcgetattr failed\n"); |
| 1195 |
return( -1 ); |
| 1196 |
} |
| 1197 |
#ifdef CMSPAR |
| 1198 |
if ( ( ttyset.c_cflag & PARENB ) && |
| 1199 |
( ttyset.c_cflag & PARODD ) && |
| 1200 |
( ttyset.c_cflag & CMSPAR ) ) |
| 1201 |
{ |
| 1202 |
return( JPARITY_MARK ); |
| 1203 |
} |
| 1204 |
else if ( ttyset.c_cflag & ( PARENB && |
| 1205 |
ttyset.c_cflag & CMSPAR ) ) |
| 1206 |
{ |
| 1207 |
return( JPARITY_SPACE ); |
| 1208 |
} |
| 1209 |
#endif |
| 1210 |
if ( ttyset.c_cflag & PARENB && |
| 1211 |
ttyset.c_cflag & PARODD ) |
| 1212 |
{ |
| 1213 |
return( JPARITY_ODD ); |
| 1214 |
} |
| 1215 |
else if ( ttyset.c_cflag & PARENB ) |
| 1216 |
{ |
| 1217 |
return( JPARITY_EVEN ); |
| 1218 |
} |
| 1219 |
else |
| 1220 |
{ |
| 1221 |
return( JPARITY_NONE ); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
int translate_parity( JNIEnv *env, tcflag_t *cflag, jint parity ) |
| 1236 |
{ |
| 1237 |
ENTER( "translate_parity" ); |
| 1238 |
#ifdef CMSPAR |
| 1239 |
(*cflag) &= ~(PARENB | PARODD | CMSPAR ); |
| 1240 |
#endif |
| 1241 |
switch( parity ) { |
| 1242 |
case JPARITY_NONE: |
| 1243 |
LEAVE( "translate_parity" ); |
| 1244 |
return 0; |
| 1245 |
case JPARITY_EVEN: |
| 1246 |
(*cflag) |= PARENB; |
| 1247 |
LEAVE( "translate_parity" ); |
| 1248 |
return 0; |
| 1249 |
case JPARITY_ODD: |
| 1250 |
(*cflag) |= PARENB | PARODD; |
| 1251 |
LEAVE( "translate_parity" ); |
| 1252 |
return 0; |
| 1253 |
#ifdef CMSPAR |
| 1254 |
case JPARITY_MARK: |
| 1255 |
(*cflag) |= PARENB | PARODD | CMSPAR; |
| 1256 |
LEAVE( "translate_parity" ); |
| 1257 |
return 0; |
| 1258 |
case JPARITY_SPACE: |
| 1259 |
(*cflag) |= PARENB | CMSPAR; |
| 1260 |
LEAVE( "translate_parity" ); |
| 1261 |
return 0; |
| 1262 |
#endif |
| 1263 |
default: |
| 1264 |
printf("Parity missed %i\n", (int) parity ); |
| 1265 |
} |
| 1266 |
|
| 1267 |
LEAVE( "translate_parity" ); |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
return 1; |
| 1275 |
} |
| 1276 |
#if !defined(TIOCSERGETLSR) && !defined(WIN32) |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
void *drain_loop( void *arg ) |
| 1287 |
{ |
| 1288 |
struct event_info_struct *eis = ( struct event_info_struct * ) arg; |
| 1289 |
|
| 1290 |
int i; |
| 1291 |
pthread_detach( pthread_self() ); |
| 1292 |
|
| 1293 |
for(i=0;;i++) |
| 1294 |
{ |
| 1295 |
report_verbose("drain_loop: looping\n"); |
| 1296 |
if( eis->eventloop_interrupted ) |
| 1297 |
{ |
| 1298 |
goto end; |
| 1299 |
} |
| 1300 |
#if defined(__sun__) |
| 1301 |
|
| 1302 |
if (usleep(5000)) { |
| 1303 |
report("drain_loop: received EINTR"); |
| 1304 |
} |
| 1305 |
#else |
| 1306 |
if (usleep(1000000)) { |
| 1307 |
report("drain_loop: received EINTR"); |
| 1308 |
} |
| 1309 |
#endif |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
if( eis->eventloop_interrupted ) |
| 1314 |
{ |
| 1315 |
goto end; |
| 1316 |
} |
| 1317 |
if( tcdrain( eis->fd ) == 0 ) |
| 1318 |
{ |
| 1319 |
if( eis && eis->writing ) |
| 1320 |
{ |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
eis->output_buffer_empty_flag = 1; |
| 1326 |
eis->writing=JNI_FALSE; |
| 1327 |
} |
| 1328 |
else |
| 1329 |
{ |
| 1330 |
if( !eis ) |
| 1331 |
{ |
| 1332 |
goto end; |
| 1333 |
} |
| 1334 |
report_verbose("drain_loop: writing not set\n"); |
| 1335 |
} |
| 1336 |
} |
| 1337 |
else if (errno != EINTR) |
| 1338 |
{ |
| 1339 |
report("drain_loop: tcdrain bad fd\n"); |
| 1340 |
goto end; |
| 1341 |
} |
| 1342 |
else |
| 1343 |
{ |
| 1344 |
report("drain_loop: received EINTR in tcdrain\n"); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
end: |
| 1348 |
report("------------------ drain_loop exiting ---------------------\n"); |
| 1349 |
eis->closing = 1; |
| 1350 |
pthread_exit( NULL ); |
| 1351 |
return( NULL ); |
| 1352 |
} |
| 1353 |
#endif |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
void finalize_threads( struct event_info_struct *eis ) |
| 1373 |
{ |
| 1374 |
#if !defined(TIOCSERGETLSR) && !defined( WIN32 ) |
| 1375 |
|
| 1376 |
|
| 1377 |
eis->output_buffer_empty_flag = 0; |
| 1378 |
ENTER("finalize_threads\n"); |
| 1379 |
|
| 1380 |
|
| 1381 |
LEAVE("---------------- finalize_threads ---------------"); |
| 1382 |
#endif |
| 1383 |
} |
| 1384 |
|
| 1385 |
#if !defined(TIOCSERGETLSR) && !defined( WIN32 ) |
| 1386 |
static void warn_sig_abort( int signo ) |
| 1387 |
{ |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
} |
| 1394 |
#endif |
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
int init_threads( struct event_info_struct *eis ) |
| 1409 |
{ |
| 1410 |
jfieldID jeis; |
| 1411 |
#if !defined(TIOCSERGETLSR) & !defined(WIN32) |
| 1412 |
sigset_t newmask, oldmask; |
| 1413 |
struct sigaction newaction, oldaction; |
| 1414 |
pthread_t tid; |
| 1415 |
|
| 1416 |
report_time_start( ); |
| 1417 |
report("init_threads: start\n"); |
| 1418 |
|
| 1419 |
sigemptyset(&newmask); |
| 1420 |
sigaddset(&newmask, SIGCHLD); |
| 1421 |
|
| 1422 |
|
| 1423 |
newaction.sa_handler = warn_sig_abort; |
| 1424 |
sigemptyset( &newaction.sa_mask ); |
| 1425 |
#ifdef SA_INTERRUPT |
| 1426 |
newaction.sa_flags = SA_INTERRUPT; |
| 1427 |
#endif |
| 1428 |
#ifdef SA_RESTART |
| 1429 |
|
| 1430 |
|
| 1431 |
#endif |
| 1432 |
|
| 1433 |
sigaction(SIGABRT, &newaction, &oldaction); |
| 1434 |
sigaction(SIGCHLD, &newaction, &oldaction); |
| 1435 |
sigaction(SIGALRM, &newaction, &oldaction); |
| 1436 |
sigaction(SIGCONT, &newaction, &oldaction); |
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
sigprocmask( SIG_SETMASK, &newmask, &oldmask ); |
| 1450 |
|
| 1451 |
report("init_threads: creating drain_loop\n"); |
| 1452 |
pthread_create( &tid, NULL, drain_loop, (void *) eis ); |
| 1453 |
pthread_detach( tid ); |
| 1454 |
eis->drain_tid = tid; |
| 1455 |
#endif |
| 1456 |
report("init_threads: get eis\n"); |
| 1457 |
jeis = (*eis->env)->GetFieldID( eis->env, eis->jclazz, "eis", "J" ); |
| 1458 |
report("init_threads: set eis\n"); |
| 1459 |
(*eis->env)->SetIntField(eis->env, *eis->jobj, jeis, ( size_t ) eis ); |
| 1460 |
report("init_threads: stop\n"); |
| 1461 |
report_time_end( ); |
| 1462 |
return( 1 ); |
| 1463 |
} |
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
JNIEXPORT void JNICALL RXTXPort(writeByte)( JNIEnv *env, |
| 1475 |
jobject jobj, jint ji, jboolean interrupted ) |
| 1476 |
{ |
| 1477 |
#ifndef TIOCSERGETLSR |
| 1478 |
struct event_info_struct *index = master_index; |
| 1479 |
#endif |
| 1480 |
unsigned char byte = (unsigned char)ji; |
| 1481 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1482 |
int result; |
| 1483 |
char msg[80]; |
| 1484 |
#if defined ( __sun__ ) |
| 1485 |
int count; |
| 1486 |
#endif |
| 1487 |
|
| 1488 |
report_time_start(); |
| 1489 |
ENTER( "RXTXPort:writeByte" ); |
| 1490 |
do { |
| 1491 |
sprintf( msg, "writeByte %c>>\n", byte ); |
| 1492 |
report( msg ); |
| 1493 |
result=WRITE (fd, (void * ) &byte, sizeof(unsigned char)); |
| 1494 |
} while (result < 0 && errno==EINTR); |
| 1495 |
if( result < 0 ) |
| 1496 |
{ |
| 1497 |
|
| 1498 |
goto fail; |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
#ifndef TIOCSERGETLSR |
| 1510 |
if( ! interrupted ) |
| 1511 |
{ |
| 1512 |
index = master_index; |
| 1513 |
if( index ) |
| 1514 |
{ |
| 1515 |
while( index->fd != fd && |
| 1516 |
index->next ) index = index->next; |
| 1517 |
} |
| 1518 |
index->writing = 1; |
| 1519 |
report( "writeByte: index->writing = 1" ); |
| 1520 |
} |
| 1521 |
#endif |
| 1522 |
sprintf( msg, "RXTXPort:writeByte %i\n", result ); |
| 1523 |
report( msg ); |
| 1524 |
LEAVE( "RXTXPort:writeByte" ); |
| 1525 |
if(result >= 0) |
| 1526 |
{ |
| 1527 |
report_time_end(); |
| 1528 |
return; |
| 1529 |
} |
| 1530 |
fail: |
| 1531 |
throw_java_exception( env, IO_EXCEPTION, "writeByte", |
| 1532 |
strerror( errno ) ); |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
JNIEXPORT void JNICALL RXTXPort(writeArray)( JNIEnv *env, |
| 1547 |
jobject jobj, jbyteArray jbarray, jint offset, jint count, |
| 1548 |
jboolean interrupted ) |
| 1549 |
{ |
| 1550 |
#ifndef TIOCSERGETLSR |
| 1551 |
struct event_info_struct *index = master_index; |
| 1552 |
#endif |
| 1553 |
int fd; |
| 1554 |
int result=0,total=0; |
| 1555 |
jbyte *body; |
| 1556 |
#if defined ( __sun__ ) |
| 1557 |
int icount; |
| 1558 |
#endif |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
#if defined ( __sun__ ) |
| 1563 |
struct timespec retspec; |
| 1564 |
|
| 1565 |
retspec.tv_sec = 0; |
| 1566 |
retspec.tv_nsec = 50000; |
| 1567 |
#endif |
| 1568 |
fd = get_java_var( env, jobj,"fd","I" ); |
| 1569 |
body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
report_time_start(); |
| 1575 |
ENTER( "writeArray" ); |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
do { |
| 1583 |
result=WRITE (fd, (void * ) ((char *) body + total + offset), count - total); |
| 1584 |
if(result >0){ |
| 1585 |
total += result; |
| 1586 |
} |
| 1587 |
report("writeArray()\n"); |
| 1588 |
} while ( ( total < count ) && (result < 0 && errno==EINTR ) ); |
| 1589 |
if( result < 0 ) |
| 1590 |
{ |
| 1591 |
|
| 1592 |
goto fail; |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 1604 |
#ifndef TIOCSERGETLSR |
| 1605 |
if( !interrupted ) |
| 1606 |
{ |
| 1607 |
if( index ) |
| 1608 |
{ |
| 1609 |
while( index->fd != fd && |
| 1610 |
index->next ) index = index->next; |
| 1611 |
} |
| 1612 |
index->writing = 1; |
| 1613 |
report( "writeArray: index->writing = 1" ); |
| 1614 |
} |
| 1615 |
#endif |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
LEAVE( "RXTXPort:writeArray" ); |
| 1629 |
report_time_end(); |
| 1630 |
fail: |
| 1631 |
if( result < 0 ) throw_java_exception( env, IO_EXCEPTION, |
| 1632 |
"writeArray", strerror( errno ) ); |
| 1633 |
} |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeDrain)( JNIEnv *env, |
| 1649 |
jobject jobj, jboolean interrupted ) |
| 1650 |
{ |
| 1651 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1652 |
struct event_info_struct *eis = ( struct event_info_struct * ) get_java_var( env, jobj, "eis", "J" ); |
| 1653 |
int result, count=0; |
| 1654 |
|
| 1655 |
char message[80]; |
| 1656 |
|
| 1657 |
ENTER( "SerialImp.c:drain()" ); |
| 1658 |
report_time_start( ); |
| 1659 |
do { |
| 1660 |
report_verbose( "nativeDrain: trying tcdrain\n" ); |
| 1661 |
result=tcdrain(fd); |
| 1662 |
count++; |
| 1663 |
} while (result && errno==EINTR && count <3); |
| 1664 |
|
| 1665 |
sprintf( message, "RXTXPort:drain() returns: %i\n", result ); |
| 1666 |
report_verbose( message ); |
| 1667 |
#if defined(__sun__) |
| 1668 |
|
| 1669 |
return( JNI_TRUE ); |
| 1670 |
#endif |
| 1671 |
LEAVE( "RXTXPort:drain()" ); |
| 1672 |
if( result ) throw_java_exception( env, IO_EXCEPTION, "nativeDrain", |
| 1673 |
strerror( errno ) ); |
| 1674 |
if( interrupted ) return( JNI_FALSE ); |
| 1675 |
#if !defined(TIOCSERGETLSR) && !defined(WIN32) |
| 1676 |
if( eis && eis->writing ) |
| 1677 |
{ |
| 1678 |
eis->writing=JNI_FALSE; |
| 1679 |
eis->output_buffer_empty_flag = 0; |
| 1680 |
} |
| 1681 |
#endif |
| 1682 |
if( eis && eis->eventflags[SPE_OUTPUT_BUFFER_EMPTY] ) |
| 1683 |
{ |
| 1684 |
struct event_info_struct myeis = |
| 1685 |
build_threadsafe_eis( env, &jobj, eis ); |
| 1686 |
send_event( &myeis, SPE_OUTPUT_BUFFER_EMPTY, 1 ); |
| 1687 |
} |
| 1688 |
report_time_end( ); |
| 1689 |
return( JNI_FALSE ); |
| 1690 |
} |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
JNIEXPORT void JNICALL RXTXPort(sendBreak)( JNIEnv *env, |
| 1701 |
jobject jobj, jint duration ) |
| 1702 |
{ |
| 1703 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1704 |
report_time_start( ); |
| 1705 |
ENTER( "RXTXPort:sendBreak()" ); |
| 1706 |
tcsendbreak( fd, (int)( duration / 250 ) ); |
| 1707 |
report_time_end( ); |
| 1708 |
LEAVE( "RXTXPort:sendBreak()" ); |
| 1709 |
} |
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
JNIEXPORT jint JNICALL RXTXPort(NativegetReceiveTimeout)( |
| 1720 |
JNIEnv *env, |
| 1721 |
jobject jobj |
| 1722 |
) |
| 1723 |
{ |
| 1724 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1725 |
struct termios ttyset; |
| 1726 |
|
| 1727 |
ENTER( "RXTXPort:nativegetRecieveTimeout()" ); |
| 1728 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 1729 |
LEAVE( "RXTXPort:nativegetRecieveTimeout()" ); |
| 1730 |
return(ttyset.c_cc[ VTIME ] * 100); |
| 1731 |
fail: |
| 1732 |
LEAVE( "RXTXPort:nativegetRecieveTimeout()" ); |
| 1733 |
throw_java_exception( env, IO_EXCEPTION, "getReceiveTimeout", |
| 1734 |
strerror( errno ) ); |
| 1735 |
return -1; |
| 1736 |
} |
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
JNIEXPORT jboolean JNICALL RXTXPort(NativeisReceiveTimeoutEnabled)( |
| 1747 |
JNIEnv *env, |
| 1748 |
jobject jobj |
| 1749 |
) |
| 1750 |
{ |
| 1751 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1752 |
struct termios ttyset; |
| 1753 |
ENTER( "RXTXPort:NativeisRecieveTimeoutEnabled()" ); |
| 1754 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 1755 |
LEAVE( "RXTXPort:NativeisRecieveTimeoutEnabled()" ); |
| 1756 |
return(ttyset.c_cc[ VTIME ] > 0 ? JNI_TRUE:JNI_FALSE); |
| 1757 |
fail: |
| 1758 |
LEAVE( "RXTXPort:NativeisRecieveTimeoutEnabled()" ); |
| 1759 |
throw_java_exception( env, IO_EXCEPTION, "isReceiveTimeoutEnabled", |
| 1760 |
strerror( errno ) ); |
| 1761 |
return JNI_FALSE; |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
JNIEXPORT jboolean JNICALL RXTXPort(isDSR)( JNIEnv *env, |
| 1775 |
jobject jobj ) |
| 1776 |
{ |
| 1777 |
unsigned int result = 0; |
| 1778 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1779 |
char message[80]; |
| 1780 |
|
| 1781 |
ENTER( "RXTXPort:isDSR" ); |
| 1782 |
ioctl( fd, TIOCMGET, &result ); |
| 1783 |
sprintf( message, "RXTXPort:isDSR returns %i\n", result & TIOCM_DSR ); |
| 1784 |
report( message ); |
| 1785 |
LEAVE( "RXTXPort:isDSR" ); |
| 1786 |
if( result & TIOCM_DSR ) return JNI_TRUE; |
| 1787 |
else return JNI_FALSE; |
| 1788 |
} |
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
JNIEXPORT jboolean JNICALL RXTXPort(isCD)( JNIEnv *env, |
| 1805 |
jobject jobj ) |
| 1806 |
{ |
| 1807 |
unsigned int result = 0; |
| 1808 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1809 |
char message[80]; |
| 1810 |
|
| 1811 |
ENTER( "RXTXPort:isCD" ); |
| 1812 |
ioctl( fd, TIOCMGET, &result ); |
| 1813 |
sprintf( message, "RXTXPort:isCD returns %i\n", result & TIOCM_CD ); |
| 1814 |
LEAVE( "RXTXPort:isCD" ); |
| 1815 |
if( result & TIOCM_CD ) return JNI_TRUE; |
| 1816 |
else return JNI_FALSE; |
| 1817 |
} |
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
JNIEXPORT jboolean JNICALL RXTXPort(isCTS)( JNIEnv *env, |
| 1830 |
jobject jobj ) |
| 1831 |
{ |
| 1832 |
unsigned int result = 0; |
| 1833 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1834 |
char message[80]; |
| 1835 |
|
| 1836 |
ENTER( "RXTXPort:isCTS" ); |
| 1837 |
ioctl( fd, TIOCMGET, &result ); |
| 1838 |
sprintf( message, "RXTXPort:isCTS returns %i\n", result & TIOCM_CTS ); |
| 1839 |
report( message ); |
| 1840 |
LEAVE( "RXTXPort:isCTS" ); |
| 1841 |
if( result & TIOCM_CTS ) return JNI_TRUE; |
| 1842 |
else return JNI_FALSE; |
| 1843 |
} |
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
JNIEXPORT jboolean JNICALL RXTXPort(isRI)( JNIEnv *env, |
| 1856 |
jobject jobj ) |
| 1857 |
{ |
| 1858 |
unsigned int result = 0; |
| 1859 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1860 |
char message[80]; |
| 1861 |
|
| 1862 |
ENTER( "RXTXPort:isRI" ); |
| 1863 |
ioctl( fd, TIOCMGET, &result ); |
| 1864 |
sprintf( message, "RXTXPort:isRI returns %i\n", result & TIOCM_RI ); |
| 1865 |
report( message ); |
| 1866 |
LEAVE( "RXTXPort:isRI" ); |
| 1867 |
if( result & TIOCM_RI ) return JNI_TRUE; |
| 1868 |
else return JNI_FALSE; |
| 1869 |
} |
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
JNIEXPORT jboolean JNICALL RXTXPort(isRTS)( JNIEnv *env, |
| 1882 |
jobject jobj ) |
| 1883 |
{ |
| 1884 |
unsigned int result = 0; |
| 1885 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1886 |
char message[80]; |
| 1887 |
|
| 1888 |
ENTER( "RXTXPort:isRTS" ); |
| 1889 |
ioctl( fd, TIOCMGET, &result ); |
| 1890 |
sprintf( message, "RXTXPort:isRTS returns %i\n", result & TIOCM_RTS ); |
| 1891 |
report( message ); |
| 1892 |
LEAVE( "RXTXPort:isRTS" ); |
| 1893 |
if( result & TIOCM_RTS ) return JNI_TRUE; |
| 1894 |
else return JNI_FALSE; |
| 1895 |
} |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
JNIEXPORT void JNICALL RXTXPort(setRTS)( JNIEnv *env, |
| 1909 |
jobject jobj, jboolean state ) |
| 1910 |
{ |
| 1911 |
unsigned int result = 0; |
| 1912 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1913 |
char message[80]; |
| 1914 |
|
| 1915 |
ENTER( "RXTXPort:setRTS" ); |
| 1916 |
ioctl( fd, TIOCMGET, &result ); |
| 1917 |
if( state == JNI_TRUE ) result |= TIOCM_RTS; |
| 1918 |
else result &= ~TIOCM_RTS; |
| 1919 |
ioctl( fd, TIOCMSET, &result ); |
| 1920 |
sprintf( message, "setRTS( %i )\n", state ); |
| 1921 |
report( message ); |
| 1922 |
LEAVE( "RXTXPort:setRTS" ); |
| 1923 |
return; |
| 1924 |
} |
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
JNIEXPORT void JNICALL RXTXPort(setDSR)( JNIEnv *env, |
| 1938 |
jobject jobj, jboolean state ) |
| 1939 |
{ |
| 1940 |
unsigned int result = 0; |
| 1941 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1942 |
char message[80]; |
| 1943 |
|
| 1944 |
ENTER( "RXTXPort:setDSR()" ); |
| 1945 |
ioctl( fd, TIOCMGET, &result ); |
| 1946 |
|
| 1947 |
sprintf( message, "setDSR( %i )\n", state ); |
| 1948 |
if( state == JNI_TRUE ) result |= TIOCM_DSR; |
| 1949 |
else result &= ~TIOCM_DSR; |
| 1950 |
ioctl( fd, TIOCMSET, &result ); |
| 1951 |
sprintf( message, "setDSR( %i )\n", state ); |
| 1952 |
report( message ); |
| 1953 |
LEAVE( "RXTXPort:setDSR()" ); |
| 1954 |
return; |
| 1955 |
} |
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
JNIEXPORT jboolean JNICALL RXTXPort(isDTR)( JNIEnv *env, |
| 1968 |
jobject jobj ) |
| 1969 |
{ |
| 1970 |
unsigned int result = 0; |
| 1971 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1972 |
char message[80]; |
| 1973 |
|
| 1974 |
ENTER( "RXTXPort:isDTR" ); |
| 1975 |
ioctl( fd, TIOCMGET, &result ); |
| 1976 |
sprintf( message, "isDTR( ) returns %i\n", result& TIOCM_DTR ); |
| 1977 |
report( message ); |
| 1978 |
LEAVE( "RXTXPort:isDTR" ); |
| 1979 |
if( result & TIOCM_DTR ) return JNI_TRUE; |
| 1980 |
else return JNI_FALSE; |
| 1981 |
} |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
JNIEXPORT void JNICALL RXTXPort(setDTR)( JNIEnv *env, |
| 1994 |
jobject jobj, jboolean state ) |
| 1995 |
{ |
| 1996 |
unsigned int result = 0; |
| 1997 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 1998 |
char message[80]; |
| 1999 |
|
| 2000 |
ENTER( "RXTXPort:setDTR" ); |
| 2001 |
ioctl( fd, TIOCMGET, &result ); |
| 2002 |
if( state == JNI_TRUE ) result |= TIOCM_DTR; |
| 2003 |
else result &= ~TIOCM_DTR; |
| 2004 |
ioctl( fd, TIOCMSET, &result ); |
| 2005 |
sprintf( message, "setDTR( %i )\n", state ); |
| 2006 |
report( message ); |
| 2007 |
LEAVE( "RXTXPort:setDTR" ); |
| 2008 |
return; |
| 2009 |
} |
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
void static_add_filename( const char *filename, int fd) |
| 2029 |
{ |
| 2030 |
struct preopened *newp, *p = preopened_port; |
| 2031 |
|
| 2032 |
newp = malloc( sizeof( struct preopened ) ); |
| 2033 |
strcpy( newp->filename, filename ); |
| 2034 |
newp->fd = fd; |
| 2035 |
|
| 2036 |
if( !p ) |
| 2037 |
{ |
| 2038 |
newp->next = NULL; |
| 2039 |
newp->prev = NULL; |
| 2040 |
preopened_port = newp; |
| 2041 |
return; |
| 2042 |
} |
| 2043 |
for(;;) |
| 2044 |
{ |
| 2045 |
if( !strcmp( p->filename, filename) ) |
| 2046 |
{ |
| 2047 |
|
| 2048 |
return; |
| 2049 |
} |
| 2050 |
if( p->next ) |
| 2051 |
{ |
| 2052 |
p = p->next; |
| 2053 |
} |
| 2054 |
else |
| 2055 |
{ |
| 2056 |
|
| 2057 |
newp->next = NULL; |
| 2058 |
newp->prev = p; |
| 2059 |
p->next = newp; |
| 2060 |
preopened_port = p; |
| 2061 |
return; |
| 2062 |
} |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeSetBaudBase)( |
| 2078 |
JNIEnv *env, |
| 2079 |
jobject jobj, |
| 2080 |
jint BaudBase |
| 2081 |
) |
| 2082 |
{ |
| 2083 |
|
| 2084 |
#if defined(TIOCGSERIAL) |
| 2085 |
|
| 2086 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 2087 |
struct serial_struct sstruct; |
| 2088 |
|
| 2089 |
if ( ioctl( fd, TIOCGSERIAL, &sstruct ) < 0 ) |
| 2090 |
{ |
| 2091 |
goto fail; |
| 2092 |
} |
| 2093 |
|
| 2094 |
sstruct.baud_base = (int) BaudBase; |
| 2095 |
|
| 2096 |
if ( sstruct.baud_base < 1 || |
| 2097 |
ioctl( fd, TIOCSSERIAL, &sstruct ) < 0 ) |
| 2098 |
{ |
| 2099 |
goto fail; |
| 2100 |
} |
| 2101 |
return( ( jboolean ) 0 ); |
| 2102 |
fail: |
| 2103 |
throw_java_exception( env, IO_EXCEPTION, "nativeSetBaudBase", |
| 2104 |
strerror( errno ) ); |
| 2105 |
return( ( jboolean ) 1 ); |
| 2106 |
#else |
| 2107 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2108 |
"nativeSetBaudBase", strerror( errno ) ); |
| 2109 |
return( ( jboolean ) 1 ); |
| 2110 |
#endif |
| 2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
JNIEXPORT jint JNICALL RXTXPort(nativeGetBaudBase)( |
| 2125 |
JNIEnv *env, |
| 2126 |
jobject jobj |
| 2127 |
) |
| 2128 |
{ |
| 2129 |
|
| 2130 |
#if defined(TIOCGSERIAL) |
| 2131 |
|
| 2132 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 2133 |
struct serial_struct sstruct; |
| 2134 |
|
| 2135 |
if ( ioctl( fd, TIOCGSERIAL, &sstruct ) < 0 ) |
| 2136 |
{ |
| 2137 |
goto fail; |
| 2138 |
} |
| 2139 |
return( ( jint ) ( sstruct.baud_base ) ); |
| 2140 |
fail: |
| 2141 |
throw_java_exception( env, IO_EXCEPTION, "nativeGetBaudBase", |
| 2142 |
strerror( errno ) ); |
| 2143 |
return( ( jint ) -1 ); |
| 2144 |
#else |
| 2145 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2146 |
"nativeGetBaudBase", strerror( errno ) ); |
| 2147 |
return( ( jint ) -1 ); |
| 2148 |
#endif |
| 2149 |
} |
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeSetDivisor)( |
| 2164 |
JNIEnv *env, |
| 2165 |
jobject jobj, |
| 2166 |
jint Divisor |
| 2167 |
) |
| 2168 |
{ |
| 2169 |
|
| 2170 |
#if defined(TIOCGSERIAL) |
| 2171 |
|
| 2172 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 2173 |
struct serial_struct sstruct; |
| 2174 |
|
| 2175 |
if ( ioctl( fd, TIOCGSERIAL, &sstruct ) < 0 ) |
| 2176 |
{ |
| 2177 |
goto fail; |
| 2178 |
} |
| 2179 |
|
| 2180 |
if ( sstruct.custom_divisor < 1 || |
| 2181 |
ioctl( fd, TIOCSSERIAL, &sstruct ) < 0 ) |
| 2182 |
{ |
| 2183 |
goto fail; |
| 2184 |
} |
| 2185 |
return( ( jboolean ) 0 ); |
| 2186 |
fail: |
| 2187 |
throw_java_exception( env, IO_EXCEPTION, "nativeSetDivisor", |
| 2188 |
strerror( errno ) ); |
| 2189 |
return( ( jboolean ) 1 ); |
| 2190 |
#else |
| 2191 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2192 |
"nativeSetDivisor", strerror( errno ) ); |
| 2193 |
return( ( jboolean ) 1 ); |
| 2194 |
#endif |
| 2195 |
} |
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
JNIEXPORT jint JNICALL RXTXPort(nativeGetDivisor)( |
| 2209 |
JNIEnv *env, |
| 2210 |
jobject jobj |
| 2211 |
) |
| 2212 |
{ |
| 2213 |
|
| 2214 |
#if defined(TIOCGSERIAL) |
| 2215 |
|
| 2216 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 2217 |
struct serial_struct sstruct; |
| 2218 |
|
| 2219 |
if ( ioctl( fd, TIOCGSERIAL, &sstruct ) < 0 ) |
| 2220 |
{ |
| 2221 |
goto fail; |
| 2222 |
} |
| 2223 |
|
| 2224 |
return( ( jint ) sstruct.custom_divisor ); |
| 2225 |
fail: |
| 2226 |
throw_java_exception( env, IO_EXCEPTION, "nativeGetDivisor", |
| 2227 |
strerror( errno ) ); |
| 2228 |
return( ( jint ) -1 ); |
| 2229 |
#else |
| 2230 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2231 |
"nativeGetDivisor", strerror( errno ) ); |
| 2232 |
return( ( jint ) -1 ); |
| 2233 |
#endif |
| 2234 |
} |
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticSetDSR) (JNIEnv *env, |
| 2253 |
jclass jclazz, jstring jstr, jboolean flag) |
| 2254 |
{ |
| 2255 |
int fd; |
| 2256 |
int pid = -1; |
| 2257 |
int result; |
| 2258 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2259 |
|
| 2260 |
ENTER( "RXTXPort:nativeStaticSetDSR" ); |
| 2261 |
#ifndef WIN32 |
| 2262 |
pid = getpid(); |
| 2263 |
#endif |
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
if ( LOCK( filename, pid ) ) goto fail; |
| 2268 |
|
| 2269 |
fd = find_preopened_ports( filename ); |
| 2270 |
if( !fd ) |
| 2271 |
{ |
| 2272 |
do { |
| 2273 |
fd = OPEN (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 2274 |
} while (fd < 0 && errno==EINTR); |
| 2275 |
if ( configure_port( fd ) ) goto fail; |
| 2276 |
} |
| 2277 |
if ( fd < 0 ) goto fail; |
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
ioctl( fd, TIOCMGET, &result ); |
| 2282 |
if( flag == JNI_TRUE ) result |= TIOCM_DSR; |
| 2283 |
else result &= ~TIOCM_DSR; |
| 2284 |
ioctl( fd, TIOCMSET, &result ); |
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
UNLOCK( filename, pid ); |
| 2289 |
|
| 2290 |
static_add_filename( filename, fd ); |
| 2291 |
|
| 2292 |
|
| 2293 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2294 |
LEAVE( "RXTXPort:nativeStaticSetDSR" ); |
| 2295 |
return( JNI_TRUE ); |
| 2296 |
fail: |
| 2297 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2298 |
LEAVE( "RXTXPort:nativeStaticSetDSR" ); |
| 2299 |
return( JNI_FALSE ); |
| 2300 |
} |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticSetRTS) (JNIEnv *env, |
| 2319 |
jclass jclazz, jstring jstr, jboolean flag) |
| 2320 |
{ |
| 2321 |
int fd; |
| 2322 |
int pid = -1; |
| 2323 |
int result; |
| 2324 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2325 |
|
| 2326 |
ENTER( "RXTXPort:nativeStaticSetRTS" ); |
| 2327 |
#ifndef WIN32 |
| 2328 |
pid = getpid(); |
| 2329 |
#endif |
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
if ( LOCK( filename, pid ) ) goto fail;; |
| 2334 |
|
| 2335 |
fd = find_preopened_ports( filename ); |
| 2336 |
if( !fd ) |
| 2337 |
{ |
| 2338 |
do { |
| 2339 |
fd = OPEN (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 2340 |
} while (fd < 0 && errno==EINTR); |
| 2341 |
if ( configure_port( fd ) ) goto fail; |
| 2342 |
} |
| 2343 |
if ( fd < 0 ) goto fail; |
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
ioctl( fd, TIOCMGET, &result ); |
| 2348 |
if( flag == JNI_TRUE ) result |= TIOCM_RTS; |
| 2349 |
else result &= ~TIOCM_RTS; |
| 2350 |
ioctl( fd, TIOCMSET, &result ); |
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
UNLOCK( filename, pid ); |
| 2355 |
|
| 2356 |
static_add_filename( filename, fd ); |
| 2357 |
|
| 2358 |
|
| 2359 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2360 |
LEAVE( "RXTXPort:nativeStaticSetRTS" ); |
| 2361 |
return( JNI_TRUE ); |
| 2362 |
fail: |
| 2363 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2364 |
LEAVE( "RXTXPort:nativeStaticSetRTS" ); |
| 2365 |
return( JNI_FALSE ); |
| 2366 |
} |
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
JNIEXPORT void JNICALL RXTXPort(nativeStaticSetSerialPortParams) (JNIEnv *env, |
| 2385 |
jclass jclazz, jstring jstr, jint baudrate, jint dataBits, jint stopBits, jint parity ) |
| 2386 |
{ |
| 2387 |
int fd; |
| 2388 |
int pid = -1; |
| 2389 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2390 |
int cspeed = translate_speed( env, baudrate ); |
| 2391 |
|
| 2392 |
|
| 2393 |
ENTER( "RXTXPort:nativeStaticSetSerialPortParams" ); |
| 2394 |
#ifndef WIN32 |
| 2395 |
pid = getpid(); |
| 2396 |
#endif |
| 2397 |
|
| 2398 |
|
| 2399 |
if ( LOCK( filename, pid ) ) goto fail; |
| 2400 |
|
| 2401 |
fd = find_preopened_ports( filename ); |
| 2402 |
if( !fd ) |
| 2403 |
{ |
| 2404 |
do { |
| 2405 |
fd = OPEN (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 2406 |
} while (fd < 0 && errno==EINTR); |
| 2407 |
if ( configure_port( fd ) ) goto fail; |
| 2408 |
} |
| 2409 |
|
| 2410 |
if ( fd < 0 ) |
| 2411 |
{ |
| 2412 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2413 |
LEAVE( "RXTXPort:nativeStaticSetSerialPortParams" ); |
| 2414 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2415 |
"nativeStaticSetSerialPortParams", strerror( errno ) ); |
| 2416 |
return; |
| 2417 |
} |
| 2418 |
|
| 2419 |
if (cspeed == -1) |
| 2420 |
{ |
| 2421 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2422 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2423 |
"", "BaudRate could not be set to the specified value" ); |
| 2424 |
return; |
| 2425 |
} |
| 2426 |
|
| 2427 |
if( set_port_params( env, fd, cspeed, dataBits, stopBits, parity ) ) |
| 2428 |
{ |
| 2429 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2430 |
LEAVE( "RXTXPort:nativeStatic SetSerialPortParams" ); |
| 2431 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2432 |
"nativeStaticSetSerialPortParams", strerror( errno ) ); |
| 2433 |
return; |
| 2434 |
} |
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
UNLOCK( filename, pid ); |
| 2439 |
|
| 2440 |
static_add_filename( filename, fd ); |
| 2441 |
|
| 2442 |
|
| 2443 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2444 |
LEAVE( "RXTXPort:nativeStaticSetSerialPortParams" ); |
| 2445 |
return; |
| 2446 |
fail: |
| 2447 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2448 |
LEAVE( "RXTXPort:nativeStaticSetSerialPortParams" ); |
| 2449 |
return; |
| 2450 |
} |
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticSetDTR) (JNIEnv *env, |
| 2469 |
jclass jclazz, jstring jstr, jboolean flag) |
| 2470 |
{ |
| 2471 |
int fd; |
| 2472 |
int pid = -1; |
| 2473 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2474 |
int result; |
| 2475 |
|
| 2476 |
ENTER( "RXTXPort:nativeStaticSetDTR" ); |
| 2477 |
#ifndef WIN32 |
| 2478 |
pid = getpid(); |
| 2479 |
#endif |
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
if ( LOCK( filename, pid ) ) goto fail;; |
| 2484 |
|
| 2485 |
fd = find_preopened_ports( filename ); |
| 2486 |
if( !fd ) |
| 2487 |
{ |
| 2488 |
do { |
| 2489 |
fd = OPEN (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 2490 |
} while (fd < 0 && errno==EINTR); |
| 2491 |
if ( configure_port( fd ) ) goto fail; |
| 2492 |
} |
| 2493 |
if ( fd < 0 ) goto fail; |
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
ioctl( fd, TIOCMGET, &result ); |
| 2498 |
if( flag == JNI_TRUE ) result |= TIOCM_DTR; |
| 2499 |
else result &= ~TIOCM_DTR; |
| 2500 |
ioctl( fd, TIOCMSET, &result ); |
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
UNLOCK( filename, pid ); |
| 2505 |
|
| 2506 |
static_add_filename( filename, fd ); |
| 2507 |
|
| 2508 |
|
| 2509 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2510 |
LEAVE( "RXTXPort:nativeStaticSetDTR" ); |
| 2511 |
return( JNI_TRUE ); |
| 2512 |
fail: |
| 2513 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2514 |
LEAVE( "RXTXPort:nativeStaticSetDTR" ); |
| 2515 |
return( JNI_FALSE ); |
| 2516 |
} |
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticIsRTS)( JNIEnv *env, |
| 2530 |
jobject jobj, jstring jstr ) |
| 2531 |
{ |
| 2532 |
unsigned int result = 0; |
| 2533 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2534 |
int fd = find_preopened_ports( filename ); |
| 2535 |
char message[80]; |
| 2536 |
|
| 2537 |
ENTER( "RXTXPort:nativeStaticIsRTS" ); |
| 2538 |
if( !fd ) |
| 2539 |
{ |
| 2540 |
|
| 2541 |
return JNI_FALSE; |
| 2542 |
} |
| 2543 |
ioctl( fd, TIOCMGET, &result ); |
| 2544 |
sprintf( message, "nativeStaticIsRTS( ) returns %i\n", result& TIOCM_RTS ); |
| 2545 |
report( message ); |
| 2546 |
LEAVE( "RXTXPort:nativeStaticIsRTS" ); |
| 2547 |
if( result & TIOCM_RTS ) return JNI_TRUE; |
| 2548 |
else return JNI_FALSE; |
| 2549 |
} |
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticIsDSR)( JNIEnv *env, |
| 2562 |
jobject jobj, jstring jstr ) |
| 2563 |
{ |
| 2564 |
unsigned int result = 0; |
| 2565 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2566 |
int fd = find_preopened_ports( filename ); |
| 2567 |
char message[80]; |
| 2568 |
|
| 2569 |
ENTER( "RXTXPort:nativeStaticIsDSR" ); |
| 2570 |
if( !fd ) |
| 2571 |
{ |
| 2572 |
|
| 2573 |
return JNI_FALSE; |
| 2574 |
} |
| 2575 |
ioctl( fd, TIOCMGET, &result ); |
| 2576 |
sprintf( message, "nativeStaticIsDSR( ) returns %i\n", result& TIOCM_DSR ); |
| 2577 |
report( message ); |
| 2578 |
LEAVE( "RXTXPort:nativeStaticIsDSR" ); |
| 2579 |
if( result & TIOCM_DSR ) return JNI_TRUE; |
| 2580 |
else return JNI_FALSE; |
| 2581 |
} |
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticIsDTR)( JNIEnv *env, |
| 2594 |
jobject jobj, jstring jstr ) |
| 2595 |
{ |
| 2596 |
unsigned int result = 0; |
| 2597 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2598 |
int fd = find_preopened_ports( filename ); |
| 2599 |
char message[80]; |
| 2600 |
|
| 2601 |
ENTER( "RXTXPort:nativeStaticIsDTR" ); |
| 2602 |
if( !fd ) |
| 2603 |
{ |
| 2604 |
|
| 2605 |
return JNI_FALSE; |
| 2606 |
} |
| 2607 |
ioctl( fd, TIOCMGET, &result ); |
| 2608 |
sprintf( message, "nativeStaticIsDTR( ) returns %i\n", result& TIOCM_DTR ); |
| 2609 |
report( message ); |
| 2610 |
LEAVE( "RXTXPort:nativeStaticIsDTR" ); |
| 2611 |
if( result & TIOCM_DTR ) return JNI_TRUE; |
| 2612 |
else return JNI_FALSE; |
| 2613 |
} |
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticIsCD)( JNIEnv *env, |
| 2626 |
jobject jobj, jstring jstr ) |
| 2627 |
{ |
| 2628 |
unsigned int result = 0; |
| 2629 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2630 |
int fd = find_preopened_ports( filename ); |
| 2631 |
char message[80]; |
| 2632 |
|
| 2633 |
ENTER( "RXTXPort:nativeStaticIsCD" ); |
| 2634 |
if( !fd ) |
| 2635 |
{ |
| 2636 |
|
| 2637 |
return JNI_FALSE; |
| 2638 |
} |
| 2639 |
ioctl( fd, TIOCMGET, &result ); |
| 2640 |
sprintf( message, "nativeStaticIsCD( ) returns %i\n", result& TIOCM_CD ); |
| 2641 |
report( message ); |
| 2642 |
LEAVE( "RXTXPort:nativeStaticIsCD" ); |
| 2643 |
if( result & TIOCM_CD ) return JNI_TRUE; |
| 2644 |
else return JNI_FALSE; |
| 2645 |
} |
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticIsCTS)( JNIEnv *env, |
| 2658 |
jobject jobj, jstring jstr ) |
| 2659 |
{ |
| 2660 |
unsigned int result = 0; |
| 2661 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2662 |
int fd = find_preopened_ports( filename ); |
| 2663 |
char message[80]; |
| 2664 |
|
| 2665 |
ENTER( "RXTXPort:nativeStaticIsCTS" ); |
| 2666 |
if( !fd ) |
| 2667 |
{ |
| 2668 |
|
| 2669 |
return JNI_FALSE; |
| 2670 |
} |
| 2671 |
ioctl( fd, TIOCMGET, &result ); |
| 2672 |
sprintf( message, "nativeStaticIsCTS( ) returns %i\n", result& TIOCM_CTS ); |
| 2673 |
report( message ); |
| 2674 |
LEAVE( "RXTXPort:nativeStaticIsCTS" ); |
| 2675 |
if( result & TIOCM_CTS ) return JNI_TRUE; |
| 2676 |
else return JNI_FALSE; |
| 2677 |
} |
| 2678 |
|
| 2679 |
|
| 2680 |
|
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeStaticIsRI)( JNIEnv *env, |
| 2690 |
jobject jobj, jstring jstr ) |
| 2691 |
{ |
| 2692 |
unsigned int result = 0; |
| 2693 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2694 |
int fd = find_preopened_ports( filename ); |
| 2695 |
char message[80]; |
| 2696 |
|
| 2697 |
ENTER( "RXTXPort:nativeStaticIsRI" ); |
| 2698 |
if( !fd ) |
| 2699 |
{ |
| 2700 |
|
| 2701 |
return JNI_FALSE; |
| 2702 |
} |
| 2703 |
ioctl( fd, TIOCMGET, &result ); |
| 2704 |
sprintf( message, "nativeStaticRI( ) returns %i\n", result& TIOCM_RI ); |
| 2705 |
report( message ); |
| 2706 |
LEAVE( "RXTXPort:nativeStaticIsRI" ); |
| 2707 |
if( result & TIOCM_RI ) return JNI_TRUE; |
| 2708 |
else return JNI_FALSE; |
| 2709 |
} |
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
JNIEXPORT jint JNICALL RXTXPort(nativeStaticGetBaudRate)( JNIEnv *env, jobject jobj, jstring jstr ) |
| 2721 |
{ |
| 2722 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2723 |
int fd = find_preopened_ports( filename ); |
| 2724 |
struct termios ttyset; |
| 2725 |
int baudrate; |
| 2726 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2727 |
|
| 2728 |
ENTER( "RXTXPort:nativeStaticGetBaudRate" ); |
| 2729 |
if( !fd ) |
| 2730 |
{ |
| 2731 |
|
| 2732 |
return -1; |
| 2733 |
} |
| 2734 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 2735 |
{ |
| 2736 |
report( "nativeStaticGetBaudRate: Cannot Get Serial Port Settings\n" ); |
| 2737 |
return(-1); |
| 2738 |
} |
| 2739 |
|
| 2740 |
|
| 2741 |
|
| 2742 |
|
| 2743 |
|
| 2744 |
|
| 2745 |
|
| 2746 |
|
| 2747 |
|
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
#if defined(CBAUD) |
| 2756 |
baudrate = ttyset.c_cflag&CBAUD; |
| 2757 |
#else |
| 2758 |
if(cfgetispeed(&ttyset) != cfgetospeed(&ttyset)) return -1; |
| 2759 |
baudrate = cfgetispeed(&ttyset); |
| 2760 |
#endif |
| 2761 |
return( get_java_baudrate(baudrate) ); |
| 2762 |
} |
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
|
| 2772 |
JNIEXPORT jint JNICALL RXTXPort(nativeStaticGetDataBits)( JNIEnv *env, jobject jobj, jstring jstr ) |
| 2773 |
{ |
| 2774 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2775 |
int fd = find_preopened_ports( filename ); |
| 2776 |
struct termios ttyset; |
| 2777 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2778 |
|
| 2779 |
ENTER( "RXTXPort:nativeStaticGetDataBits" ); |
| 2780 |
if( !fd ) |
| 2781 |
{ |
| 2782 |
|
| 2783 |
return -1; |
| 2784 |
} |
| 2785 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 2786 |
{ |
| 2787 |
report( "nativeStaticGetDataBits: Cannot Get Serial Port Settings\n" ); |
| 2788 |
return(-1); |
| 2789 |
} |
| 2790 |
switch( ttyset.c_cflag&CSIZE ) { |
| 2791 |
case CS5: return JDATABITS_5; |
| 2792 |
case CS6: return JDATABITS_6; |
| 2793 |
case CS7: return JDATABITS_7; |
| 2794 |
case CS8: return JDATABITS_8; |
| 2795 |
default: return(-1); |
| 2796 |
} |
| 2797 |
} |
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
JNIEXPORT jint JNICALL RXTXPort(nativeStaticGetParity)( JNIEnv *env, jobject jobj, jstring jstr ) |
| 2808 |
{ |
| 2809 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2810 |
int fd = find_preopened_ports( filename ); |
| 2811 |
struct termios ttyset; |
| 2812 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2813 |
|
| 2814 |
ENTER( "RXTXPort:nativeStaticGetParity" ); |
| 2815 |
if( !fd ) |
| 2816 |
{ |
| 2817 |
|
| 2818 |
return -1; |
| 2819 |
} |
| 2820 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 2821 |
{ |
| 2822 |
report( "nativeStaticGetParity: Cannot Get Serial Port Settings\n" ); |
| 2823 |
return(-1); |
| 2824 |
} |
| 2825 |
#ifdef CMSPAR |
| 2826 |
switch( ttyset.c_cflag&(PARENB|PARODD|CMSPAR ) ) { |
| 2827 |
#else |
| 2828 |
switch( ttyset.c_cflag&(PARENB|PARODD) ) { |
| 2829 |
#endif |
| 2830 |
case 0: return JPARITY_NONE; |
| 2831 |
case PARENB: return JPARITY_EVEN; |
| 2832 |
case PARENB | PARODD: return JPARITY_ODD; |
| 2833 |
#ifdef CMSPAR |
| 2834 |
case PARENB | PARODD | CMSPAR: return JPARITY_MARK; |
| 2835 |
case PARENB | CMSPAR: return JPARITY_SPACE; |
| 2836 |
#endif |
| 2837 |
default: return(-1); |
| 2838 |
} |
| 2839 |
} |
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
JNIEXPORT jint JNICALL RXTXPort(nativeStaticGetStopBits)( JNIEnv *env, jobject jobj, jstring jstr ) |
| 2850 |
{ |
| 2851 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 2852 |
int fd = find_preopened_ports( filename ); |
| 2853 |
struct termios ttyset; |
| 2854 |
(*env)->ReleaseStringUTFChars( env, jstr, filename ); |
| 2855 |
|
| 2856 |
ENTER( "RXTXPort:nativeStaticGetStopBits" ); |
| 2857 |
if( !fd ) |
| 2858 |
{ |
| 2859 |
|
| 2860 |
return -1; |
| 2861 |
} |
| 2862 |
if( tcgetattr( fd, &ttyset ) < 0 ) |
| 2863 |
{ |
| 2864 |
report( "nativeStaticGetStopBits: Cannot Get Serial Port Settings\n" ); |
| 2865 |
return(-1); |
| 2866 |
} |
| 2867 |
switch( ttyset.c_cflag&(CSTOPB) ) { |
| 2868 |
case 0: |
| 2869 |
return STOPBITS_1; |
| 2870 |
case CSTOPB: |
| 2871 |
if( ttyset.c_cflag & CS5 ) { |
| 2872 |
return STOPBITS_1_5; |
| 2873 |
} |
| 2874 |
else { |
| 2875 |
return STOPBITS_2; |
| 2876 |
} |
| 2877 |
default: |
| 2878 |
return -1; |
| 2879 |
} |
| 2880 |
} |
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
|
| 2885 |
|
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
JNIEXPORT jbyte JNICALL RXTXPort(nativeGetParityErrorChar)( JNIEnv *env, |
| 2897 |
jobject jobj ) |
| 2898 |
{ |
| 2899 |
unsigned int result = 0; |
| 2900 |
|
| 2901 |
ENTER( "nativeGetParityErrorChar" ); |
| 2902 |
#ifdef WIN32 |
| 2903 |
result = ( jbyte ) termiosGetParityErrorChar( |
| 2904 |
get_java_var(env, jobj, "fd", "I" ) ); |
| 2905 |
#else |
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
result = ( jint ) '\0'; |
| 2911 |
|
| 2912 |
#endif |
| 2913 |
LEAVE( "nativeGetParityErrorChar" ); |
| 2914 |
return( ( jbyte ) result ); |
| 2915 |
} |
| 2916 |
|
| 2917 |
|
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
|
| 2924 |
|
| 2925 |
|
| 2926 |
JNIEXPORT jbyte JNICALL RXTXPort(nativeGetEndOfInputChar)( JNIEnv *env, |
| 2927 |
jobject jobj ) |
| 2928 |
{ |
| 2929 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 2930 |
struct termios ttyset; |
| 2931 |
|
| 2932 |
ENTER( "nativeGetEndOfInputChar" ); |
| 2933 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 2934 |
LEAVE( "nativeGetEndOfInputChar" ); |
| 2935 |
return( (jbyte) ttyset.c_cc[VEOF] ); |
| 2936 |
fail: |
| 2937 |
LEAVE( "nativeGetEndOfInputChar" ); |
| 2938 |
report( "nativeGetEndOfInputChar failed\n" ); |
| 2939 |
return( ( jbyte ) -1 ); |
| 2940 |
} |
| 2941 |
|
| 2942 |
|
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeSetParityErrorChar)( JNIEnv *env, |
| 2957 |
jobject jobj, jbyte value ) |
| 2958 |
{ |
| 2959 |
|
| 2960 |
#ifdef WIN32 |
| 2961 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 2962 |
ENTER( "nativeSetParityErrorChar" ); |
| 2963 |
termiosSetParityError( fd, ( char ) value ); |
| 2964 |
LEAVE( "nativeSetParityErrorChar" ); |
| 2965 |
return( JNI_TRUE ); |
| 2966 |
#else |
| 2967 |
ENTER( "nativeSetParityErrorChar" ); |
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
|
| 2972 |
|
| 2973 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 2974 |
"Not implemented... yet", |
| 2975 |
strerror( errno ) ); |
| 2976 |
LEAVE( "nativeSetParityErrorChar" ); |
| 2977 |
return( JNI_FALSE ); |
| 2978 |
#endif |
| 2979 |
} |
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
|
| 2984 |
|
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeSetEndOfInputChar)( JNIEnv *env, |
| 2997 |
jobject jobj, jbyte value ) |
| 2998 |
{ |
| 2999 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 3000 |
struct termios ttyset; |
| 3001 |
|
| 3002 |
ENTER( "nativeSetEndOfInputChar" ); |
| 3003 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 3004 |
ttyset.c_cc[VEOF] = ( char ) value; |
| 3005 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 3006 |
LEAVE( "nativeSetEndOfInputChar" ); |
| 3007 |
return( JNI_TRUE ); |
| 3008 |
fail: |
| 3009 |
throw_java_exception( env, IO_EXCEPTION, "nativeSetEndOfInputChar", |
| 3010 |
strerror( errno ) ); |
| 3011 |
report( "nativeSetEndOfInputChar failed\n" ); |
| 3012 |
LEAVE( "nativeSetEndOfInputChar" ); |
| 3013 |
return( JNI_FALSE ); |
| 3014 |
} |
| 3015 |
|
| 3016 |
#ifndef WIN32 |
| 3017 |
long |
| 3018 |
GetTickCount() |
| 3019 |
{ |
| 3020 |
|
| 3021 |
struct timeval now; |
| 3022 |
|
| 3023 |
gettimeofday(&now, NULL); |
| 3024 |
report_verbose("gettimeofday\n"); |
| 3025 |
|
| 3026 |
#ifdef __QNX__ |
| 3027 |
return now.tv_sec * 1000 + now.tv_usec / 1000; |
| 3028 |
#else |
| 3029 |
return (now.tv_sec * 1000) + ceil(now.tv_usec / 1000); |
| 3030 |
#endif |
| 3031 |
} |
| 3032 |
|
| 3033 |
#endif |
| 3034 |
|
| 3035 |
|
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
|
| 3053 |
|
| 3054 |
int read_byte_array( JNIEnv *env, |
| 3055 |
jobject *jobj, |
| 3056 |
int fd, |
| 3057 |
unsigned char *buffer, |
| 3058 |
int length, |
| 3059 |
int timeout ) |
| 3060 |
{ |
| 3061 |
int ret, left, bytes = 0; |
| 3062 |
long timeLeft, now = 0, start = 0; |
| 3063 |
|
| 3064 |
struct timeval tv, *tvP; |
| 3065 |
fd_set rset; |
| 3066 |
|
| 3067 |
int flag, count = 0; |
| 3068 |
struct event_info_struct *eis = ( struct event_info_struct * ) |
| 3069 |
get_java_var( env, *jobj,"eis","J" ); |
| 3070 |
|
| 3071 |
report_time_start(); |
| 3072 |
flag = eis->eventflags[SPE_DATA_AVAILABLE]; |
| 3073 |
eis->eventflags[SPE_DATA_AVAILABLE] = 0; |
| 3074 |
|
| 3075 |
|
| 3076 |
|
| 3077 |
|
| 3078 |
|
| 3079 |
left = length; |
| 3080 |
if (timeout >= 0) |
| 3081 |
start = GetTickCount(); |
| 3082 |
while( bytes < length && count++ < 20 ) |
| 3083 |
{ |
| 3084 |
if (timeout >= 0) { |
| 3085 |
now = GetTickCount(); |
| 3086 |
if ( now-start >= timeout ) |
| 3087 |
{ |
| 3088 |
eis->eventflags[SPE_DATA_AVAILABLE] = flag; |
| 3089 |
return bytes; |
| 3090 |
} |
| 3091 |
} |
| 3092 |
|
| 3093 |
FD_ZERO(&rset); |
| 3094 |
FD_SET(fd, &rset); |
| 3095 |
|
| 3096 |
if (timeout >= 0){ |
| 3097 |
timeLeft = timeout - (now - start); |
| 3098 |
tv.tv_sec = timeLeft / 1000; |
| 3099 |
tv.tv_usec = 1000 * ( timeLeft % 1000 ); |
| 3100 |
tvP = &tv; |
| 3101 |
} |
| 3102 |
else{ |
| 3103 |
tvP = NULL; |
| 3104 |
} |
| 3105 |
|
| 3106 |
#ifndef WIN32 |
| 3107 |
do { |
| 3108 |
ret = SELECT( fd + 1, &rset, NULL, NULL, tvP ); |
| 3109 |
} while (ret < 0 && errno==EINTR); |
| 3110 |
#else |
| 3111 |
ret = 1; |
| 3112 |
#endif |
| 3113 |
if (ret == -1){ |
| 3114 |
report( "read_byte_array: select returned -1\n" ); |
| 3115 |
LEAVE( "read_byte_array" ); |
| 3116 |
eis->eventflags[SPE_DATA_AVAILABLE] = flag; |
| 3117 |
return -1; |
| 3118 |
} |
| 3119 |
else if (ret > 0) |
| 3120 |
{ |
| 3121 |
if ((ret = READ( fd, buffer + bytes, left )) < 0 ){ |
| 3122 |
if (errno != EINTR && errno != EAGAIN){ |
| 3123 |
report( "read_byte_array: read returned -1\n" ); |
| 3124 |
LEAVE( "read_byte_array" ); |
| 3125 |
eis->eventflags[SPE_DATA_AVAILABLE] = flag; |
| 3126 |
return -1; |
| 3127 |
} |
| 3128 |
eis->eventflags[SPE_DATA_AVAILABLE] = flag; |
| 3129 |
return -1; |
| 3130 |
} |
| 3131 |
else if ( ret ) { |
| 3132 |
bytes += ret; |
| 3133 |
left -= ret; |
| 3134 |
} |
| 3135 |
|
| 3136 |
|
| 3137 |
|
| 3138 |
|
| 3139 |
|
| 3140 |
|
| 3141 |
|
| 3142 |
|
| 3143 |
|
| 3144 |
|
| 3145 |
else { |
| 3146 |
|
| 3147 |
usleep(1000); |
| 3148 |
} |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
|
| 3153 |
|
| 3154 |
|
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
|
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
eis->eventflags[SPE_DATA_AVAILABLE] = flag; |
| 3165 |
return bytes; |
| 3166 |
} |
| 3167 |
|
| 3168 |
#ifdef asdf |
| 3169 |
int read_byte_array( JNIEnv *env, |
| 3170 |
jobject *jobj, |
| 3171 |
int fd, |
| 3172 |
unsigned char *buffer, |
| 3173 |
int length, |
| 3174 |
int timeout ) |
| 3175 |
{ |
| 3176 |
int ret, left, bytes = 0; |
| 3177 |
long now, start = 0; |
| 3178 |
char msg[80]; |
| 3179 |
|
| 3180 |
report_time_start(); |
| 3181 |
ENTER( "read_byte_array" ); |
| 3182 |
sprintf(msg, "read_byte_array requests %i\n", length); |
| 3183 |
report( msg ); |
| 3184 |
left = length; |
| 3185 |
if (timeout >= 0) |
| 3186 |
start = GetTickCount(); |
| 3187 |
while( bytes < length ) |
| 3188 |
{ |
| 3189 |
if (timeout >= 0) { |
| 3190 |
now = GetTickCount(); |
| 3191 |
if (now-start >= timeout) |
| 3192 |
return bytes; |
| 3193 |
} |
| 3194 |
RETRY: if ((ret = READ( fd, buffer + bytes, left )) < 0 ) |
| 3195 |
{ |
| 3196 |
if (errno == EINTR) |
| 3197 |
goto RETRY; |
| 3198 |
report( "read_byte_array: read returned -1\n" ); |
| 3199 |
LEAVE( "read_byte_array" ); |
| 3200 |
return -1; |
| 3201 |
} |
| 3202 |
bytes += ret; |
| 3203 |
left -= ret; |
| 3204 |
} |
| 3205 |
sprintf(msg, "read_byte_array returns %i\n", bytes); |
| 3206 |
report( msg ); |
| 3207 |
LEAVE( "read_byte_array" ); |
| 3208 |
report_time_end(); |
| 3209 |
return bytes; |
| 3210 |
} |
| 3211 |
|
| 3212 |
|
| 3213 |
int read_byte_array( JNIEnv *env, |
| 3214 |
jobject *jobj, |
| 3215 |
int fd, |
| 3216 |
unsigned char *buffer, |
| 3217 |
int length, |
| 3218 |
int timeout ) |
| 3219 |
{ |
| 3220 |
int ret, left, bytes = 0; |
| 3221 |
|
| 3222 |
fd_set rfds; |
| 3223 |
struct timeval sleep; |
| 3224 |
struct event_info_struct *eis = find_eis( fd ); |
| 3225 |
|
| 3226 |
#ifndef WIN32 |
| 3227 |
struct timeval *psleep=&sleep; |
| 3228 |
#endif |
| 3229 |
|
| 3230 |
ENTER( "read_byte_array" ); |
| 3231 |
left = length; |
| 3232 |
FD_ZERO( &rfds ); |
| 3233 |
FD_SET( fd, &rfds ); |
| 3234 |
if( timeout != 0 ) |
| 3235 |
{ |
| 3236 |
sleep.tv_sec = timeout / 1000; |
| 3237 |
sleep.tv_usec = 1000 * ( timeout % 1000 ); |
| 3238 |
} |
| 3239 |
while( bytes < length ) |
| 3240 |
{ |
| 3241 |
|
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
#ifndef WIN32 |
| 3246 |
do { |
| 3247 |
if( timeout == 0 ) psleep = NULL; |
| 3248 |
ret=SELECT( fd + 1, &rfds, NULL, NULL, psleep ); |
| 3249 |
} while (ret < 0 && errno==EINTR); |
| 3250 |
#else |
| 3251 |
|
| 3252 |
|
| 3253 |
|
| 3254 |
|
| 3255 |
|
| 3256 |
|
| 3257 |
ret = RXTXPort(nativeavailable)( env, *jobj ); |
| 3258 |
#endif |
| 3259 |
if( ret == 0 ) |
| 3260 |
{ |
| 3261 |
report( "read_byte_array: select returned 0\n" ); |
| 3262 |
LEAVE( "read_byte_array" ); |
| 3263 |
break; |
| 3264 |
} |
| 3265 |
if( ret < 0 ) |
| 3266 |
{ |
| 3267 |
report( "read_byte_array: select returned -1\n" ); |
| 3268 |
LEAVE( "read_byte_array" ); |
| 3269 |
return -1; |
| 3270 |
} |
| 3271 |
ret = READ( fd, buffer + bytes, left ); |
| 3272 |
if( ret == 0 ) |
| 3273 |
{ |
| 3274 |
report( "read_byte_array: read returned 0 bytes\n" ); |
| 3275 |
LEAVE( "read_byte_array" ); |
| 3276 |
break; |
| 3277 |
} |
| 3278 |
else if( ret < 0 ) |
| 3279 |
{ |
| 3280 |
report( "read_byte_array: read returned -1\n" ); |
| 3281 |
LEAVE( "read_byte_array" ); |
| 3282 |
return -1; |
| 3283 |
} |
| 3284 |
bytes += ret; |
| 3285 |
left -= ret; |
| 3286 |
} |
| 3287 |
LEAVE( "read_byte_array" ); |
| 3288 |
return bytes; |
| 3289 |
} |
| 3290 |
#endif |
| 3291 |
|
| 3292 |
|
| 3293 |
|
| 3294 |
|
| 3295 |
|
| 3296 |
|
| 3297 |
|
| 3298 |
|
| 3299 |
|
| 3300 |
|
| 3301 |
JNIEXPORT void JNICALL RXTXPort(NativeEnableReceiveTimeoutThreshold)( |
| 3302 |
JNIEnv *env, jobject jobj, jint vtime, jint threshold, jint buffer) |
| 3303 |
{ |
| 3304 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 3305 |
struct termios ttyset; |
| 3306 |
int timeout; |
| 3307 |
|
| 3308 |
if (vtime < 0){ |
| 3309 |
timeout = 0; |
| 3310 |
} |
| 3311 |
else if (vtime == 0){ |
| 3312 |
timeout = 1; |
| 3313 |
} |
| 3314 |
else{ |
| 3315 |
timeout = vtime; |
| 3316 |
} |
| 3317 |
|
| 3318 |
ENTER( "RXTXPort:NativeEnableRecieveTimeoutThreshold" ); |
| 3319 |
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail; |
| 3320 |
ttyset.c_cc[ VMIN ] = threshold; |
| 3321 |
ttyset.c_cc[ VTIME ] = timeout/100; |
| 3322 |
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail; |
| 3323 |
|
| 3324 |
LEAVE( "RXTXPort:NativeEnableRecieveTimeoutThreshold" ); |
| 3325 |
return; |
| 3326 |
fail: |
| 3327 |
LEAVE( "RXTXPort:NativeEnableRecieveTimeoutThreshold" ); |
| 3328 |
throw_java_exception( env, IO_EXCEPTION, "TimeoutThreshold", |
| 3329 |
strerror( errno ) ); |
| 3330 |
return; |
| 3331 |
} |
| 3332 |
|
| 3333 |
|
| 3334 |
|
| 3335 |
|
| 3336 |
|
| 3337 |
|
| 3338 |
|
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
|
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
|
| 3349 |
|
| 3350 |
|
| 3351 |
|
| 3352 |
|
| 3353 |
|
| 3354 |
|
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
|
| 3362 |
|
| 3363 |
|
| 3364 |
|
| 3365 |
|
| 3366 |
|
| 3367 |
|
| 3368 |
|
| 3369 |
|
| 3370 |
|
| 3371 |
|
| 3372 |
|
| 3373 |
|
| 3374 |
|
| 3375 |
|
| 3376 |
|
| 3377 |
|
| 3378 |
|
| 3379 |
|
| 3380 |
|
| 3381 |
|
| 3382 |
|
| 3383 |
|
| 3384 |
|
| 3385 |
|
| 3386 |
|
| 3387 |
|
| 3388 |
|
| 3389 |
|
| 3390 |
|
| 3391 |
|
| 3392 |
|
| 3393 |
|
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
|
| 3399 |
|
| 3400 |
|
| 3401 |
|
| 3402 |
|
| 3403 |
|
| 3404 |
|
| 3405 |
|
| 3406 |
|
| 3407 |
|
| 3408 |
|
| 3409 |
|
| 3410 |
|
| 3411 |
|
| 3412 |
|
| 3413 |
|
| 3414 |
|
| 3415 |
|
| 3416 |
|
| 3417 |
|
| 3418 |
|
| 3419 |
|
| 3420 |
|
| 3421 |
|
| 3422 |
|
| 3423 |
|
| 3424 |
|
| 3425 |
|
| 3426 |
|
| 3427 |
|
| 3428 |
|
| 3429 |
|
| 3430 |
|
| 3431 |
|
| 3432 |
|
| 3433 |
|
| 3434 |
|
| 3435 |
|
| 3436 |
|
| 3437 |
|
| 3438 |
|
| 3439 |
|
| 3440 |
|
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
|
| 3447 |
|
| 3448 |
|
| 3449 |
|
| 3450 |
|
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
|
| 3455 |
|
| 3456 |
|
| 3457 |
|
| 3458 |
|
| 3459 |
|
| 3460 |
|
| 3461 |
|
| 3462 |
|
| 3463 |
|
| 3464 |
|
| 3465 |
|
| 3466 |
|
| 3467 |
JNIEXPORT jint JNICALL RXTXPort(readByte)( JNIEnv *env, |
| 3468 |
jobject jobj ) |
| 3469 |
{ |
| 3470 |
int bytes; |
| 3471 |
unsigned char buffer[ 1 ]; |
| 3472 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 3473 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 3474 |
|
| 3475 |
|
| 3476 |
|
| 3477 |
|
| 3478 |
|
| 3479 |
|
| 3480 |
bytes = read_byte_array( env, &jobj, fd, buffer, 1, timeout ); |
| 3481 |
if( bytes < 0 ) { |
| 3482 |
LEAVE( "RXTXPort:readByte" ); |
| 3483 |
throw_java_exception( env, IO_EXCEPTION, "readByte", |
| 3484 |
strerror( errno ) ); |
| 3485 |
return -1; |
| 3486 |
} |
| 3487 |
|
| 3488 |
|
| 3489 |
|
| 3490 |
|
| 3491 |
|
| 3492 |
|
| 3493 |
return (bytes ? (jint)buffer[ 0 ] : -1); |
| 3494 |
} |
| 3495 |
|
| 3496 |
|
| 3497 |
|
| 3498 |
|
| 3499 |
|
| 3500 |
|
| 3501 |
|
| 3502 |
|
| 3503 |
|
| 3504 |
|
| 3505 |
|
| 3506 |
|
| 3507 |
|
| 3508 |
JNIEXPORT jint JNICALL RXTXPort(readArray)( JNIEnv *env, |
| 3509 |
jobject jobj, jbyteArray jbarray, jint offset, jint length ) |
| 3510 |
{ |
| 3511 |
int bytes; |
| 3512 |
jbyte *body; |
| 3513 |
|
| 3514 |
int fd = get_java_var( env, jobj, "fd", "I" ); |
| 3515 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
if( (size_t) length > SSIZE_MAX || (size_t) length < 0 ) { |
| 3522 |
report( "RXTXPort:readArray length > SSIZE_MAX" ); |
| 3523 |
LEAVE( "RXTXPort:readArray" ); |
| 3524 |
throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS, |
| 3525 |
"readArray", "Invalid length" ); |
| 3526 |
return -1; |
| 3527 |
} |
| 3528 |
body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 3529 |
bytes = read_byte_array( env, &jobj, fd, (unsigned char *)(body+offset), length, timeout ); |
| 3530 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 3531 |
if( bytes < 0 ) { |
| 3532 |
report( "RXTXPort:readArray bytes < 0" ); |
| 3533 |
LEAVE( "RXTXPort:readArray" ); |
| 3534 |
throw_java_exception( env, IO_EXCEPTION, "readArray", |
| 3535 |
strerror( errno ) ); |
| 3536 |
return -1; |
| 3537 |
} |
| 3538 |
|
| 3539 |
|
| 3540 |
|
| 3541 |
|
| 3542 |
|
| 3543 |
|
| 3544 |
return (bytes); |
| 3545 |
} |
| 3546 |
|
| 3547 |
|
| 3548 |
|
| 3549 |
|
| 3550 |
|
| 3551 |
|
| 3552 |
|
| 3553 |
|
| 3554 |
|
| 3555 |
|
| 3556 |
JNIEXPORT jboolean JNICALL RXTXPort(nativeClearCommInput)( JNIEnv *env, |
| 3557 |
jobject jobj ) |
| 3558 |
{ |
| 3559 |
int fd = get_java_var( env, jobj, "fd", "I" ); |
| 3560 |
if ( tcflush( fd, TCIFLUSH ) ) |
| 3561 |
return( JNI_FALSE ); |
| 3562 |
return( JNI_TRUE ); |
| 3563 |
} |
| 3564 |
|
| 3565 |
|
| 3566 |
|
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
|
| 3573 |
|
| 3574 |
|
| 3575 |
|
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
|
| 3580 |
JNIEXPORT jint JNICALL RXTXPort(readTerminatedArray)( JNIEnv *env, |
| 3581 |
jobject jobj, jbyteArray jbarray, jint offset, jint length, |
| 3582 |
jbyteArray jterminator ) |
| 3583 |
{ |
| 3584 |
int bytes, total = 0; |
| 3585 |
jbyte *body, *terminator; |
| 3586 |
|
| 3587 |
int fd = get_java_var( env, jobj, "fd", "I" ); |
| 3588 |
int timeout = get_java_var( env, jobj, "timeout", "I" ); |
| 3589 |
|
| 3590 |
|
| 3591 |
|
| 3592 |
|
| 3593 |
|
| 3594 |
if( (size_t) length > SSIZE_MAX || (size_t) length < 0 ) { |
| 3595 |
report( "RXTXPort:readArray length > SSIZE_MAX" ); |
| 3596 |
LEAVE( "RXTXPort:readArray" ); |
| 3597 |
throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS, |
| 3598 |
"readArray", "Invalid length" ); |
| 3599 |
return -1; |
| 3600 |
} |
| 3601 |
body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 3602 |
terminator = (*env)->GetByteArrayElements( env, jterminator, 0 ); |
| 3603 |
do |
| 3604 |
{ |
| 3605 |
bytes = read_byte_array( env, &jobj, fd, (unsigned char *)(body+offset + total ), 1 , timeout ); |
| 3606 |
total += bytes; |
| 3607 |
if( bytes < 0 ) { |
| 3608 |
report( "RXTXPort:readArray bytes < 0" ); |
| 3609 |
LEAVE( "RXTXPort:readArray" ); |
| 3610 |
throw_java_exception( env, IO_EXCEPTION, "readArray", |
| 3611 |
strerror( errno ) ); |
| 3612 |
return -1; |
| 3613 |
} |
| 3614 |
if ( total > 1 && terminator[1] == body[total -1] && |
| 3615 |
terminator[ 0 ] == body[ total -2 ] |
| 3616 |
) |
| 3617 |
{ |
| 3618 |
report("Got terminator!\n" ); |
| 3619 |
break; |
| 3620 |
} |
| 3621 |
|
| 3622 |
} while ( bytes > 0 && total < length ); |
| 3623 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 3624 |
|
| 3625 |
|
| 3626 |
|
| 3627 |
|
| 3628 |
|
| 3629 |
|
| 3630 |
return (bytes); |
| 3631 |
} |
| 3632 |
|
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
|
| 3639 |
|
| 3640 |
|
| 3641 |
|
| 3642 |
JNIEXPORT jint JNICALL RXTXPort(nativeavailable)( JNIEnv *env, |
| 3643 |
jobject jobj ) |
| 3644 |
{ |
| 3645 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 3646 |
int result; |
| 3647 |
|
| 3648 |
|
| 3649 |
|
| 3650 |
|
| 3651 |
|
| 3652 |
|
| 3653 |
|
| 3654 |
|
| 3655 |
|
| 3656 |
|
| 3657 |
|
| 3658 |
|
| 3659 |
#ifdef FIORDCHK |
| 3660 |
result = ioctl(fd, FIORDCHK, 0); |
| 3661 |
#else |
| 3662 |
if( ioctl( fd, FIONREAD, &result ) < 0 ) |
| 3663 |
{ |
| 3664 |
goto fail; |
| 3665 |
} |
| 3666 |
#endif |
| 3667 |
if (result == -1) { |
| 3668 |
goto fail; |
| 3669 |
} |
| 3670 |
|
| 3671 |
|
| 3672 |
|
| 3673 |
|
| 3674 |
|
| 3675 |
|
| 3676 |
|
| 3677 |
|
| 3678 |
|
| 3679 |
|
| 3680 |
|
| 3681 |
|
| 3682 |
return (jint)result; |
| 3683 |
fail: |
| 3684 |
report("RXTXPort:nativeavailable: ioctl() failed\n"); |
| 3685 |
|
| 3686 |
|
| 3687 |
|
| 3688 |
throw_java_exception( env, IO_EXCEPTION, "nativeavailable", |
| 3689 |
strerror( errno ) ); |
| 3690 |
return (jint)result; |
| 3691 |
} |
| 3692 |
|
| 3693 |
|
| 3694 |
|
| 3695 |
|
| 3696 |
|
| 3697 |
|
| 3698 |
|
| 3699 |
|
| 3700 |
|
| 3701 |
|
| 3702 |
|
| 3703 |
|
| 3704 |
|
| 3705 |
|
| 3706 |
|
| 3707 |
|
| 3708 |
JNIEXPORT void JNICALL RXTXPort(setflowcontrol)( JNIEnv *env, |
| 3709 |
jobject jobj, jint flowmode ) |
| 3710 |
{ |
| 3711 |
struct termios ttyset; |
| 3712 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 3713 |
|
| 3714 |
ENTER( "RXTXPort:setflowcontrol" ); |
| 3715 |
if( tcgetattr( fd, &ttyset ) ) goto fail; |
| 3716 |
|
| 3717 |
if ( flowmode & ( FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT ) ) |
| 3718 |
{ |
| 3719 |
ttyset.c_cflag |= HARDWARE_FLOW_CONTROL; |
| 3720 |
} |
| 3721 |
else ttyset.c_cflag &= ~HARDWARE_FLOW_CONTROL; |
| 3722 |
|
| 3723 |
ttyset.c_iflag &= ~IXANY; |
| 3724 |
|
| 3725 |
if ( flowmode & FLOWCONTROL_XONXOFF_IN ) |
| 3726 |
{ |
| 3727 |
ttyset.c_iflag |= IXOFF; |
| 3728 |
} |
| 3729 |
else ttyset.c_iflag &= ~IXOFF; |
| 3730 |
|
| 3731 |
if ( flowmode & FLOWCONTROL_XONXOFF_OUT ) |
| 3732 |
{ |
| 3733 |
|
| 3734 |
ttyset.c_iflag |= IXON; |
| 3735 |
} |
| 3736 |
else ttyset.c_iflag &= ~IXON; |
| 3737 |
|
| 3738 |
if( tcsetattr( fd, TCSANOW, &ttyset ) ) goto fail; |
| 3739 |
LEAVE( "RXTXPort:setflowcontrol" ); |
| 3740 |
return; |
| 3741 |
fail: |
| 3742 |
LEAVE( "RXTXPort:setflowcontrol" ); |
| 3743 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, "", |
| 3744 |
"flow control type not supported" ); |
| 3745 |
return; |
| 3746 |
} |
| 3747 |
|
| 3748 |
|
| 3749 |
|
| 3750 |
|
| 3751 |
|
| 3752 |
|
| 3753 |
|
| 3754 |
|
| 3755 |
|
| 3756 |
|
| 3757 |
|
| 3758 |
void unlock_monitor_thread( struct event_info_struct *eis ) |
| 3759 |
{ |
| 3760 |
JNIEnv *env = eis->env; |
| 3761 |
jobject jobj = *(eis->jobj); |
| 3762 |
|
| 3763 |
jfieldID jfid = (*env)->GetFieldID( env, (*env)->GetObjectClass( env, jobj ), "MonitorThreadLock", "Z" ); |
| 3764 |
(*env)->SetBooleanField( env, jobj, jfid, (jboolean) 0 ); |
| 3765 |
} |
| 3766 |
|
| 3767 |
|
| 3768 |
|
| 3769 |
|
| 3770 |
|
| 3771 |
|
| 3772 |
|
| 3773 |
|
| 3774 |
|
| 3775 |
|
| 3776 |
int check_line_status_register( struct event_info_struct *eis ) |
| 3777 |
{ |
| 3778 |
#ifdef TIOCSERGETLSR |
| 3779 |
struct stat fstatbuf; |
| 3780 |
|
| 3781 |
if( ! eis->eventflags[SPE_OUTPUT_BUFFER_EMPTY] ) |
| 3782 |
{ |
| 3783 |
|
| 3784 |
|
| 3785 |
|
| 3786 |
return 0; |
| 3787 |
} |
| 3788 |
if ( fstat( eis->fd, &fstatbuf ) ) |
| 3789 |
{ |
| 3790 |
report( "check_line_status_register: fstat\n" ); |
| 3791 |
return( 1 ); |
| 3792 |
} |
| 3793 |
if( ioctl( eis->fd, TIOCSERGETLSR, &eis->change ) ) |
| 3794 |
{ |
| 3795 |
report( "check_line_status_register: TIOCSERGETLSR\n is nonnull\n" ); |
| 3796 |
return( 1 ); |
| 3797 |
} |
| 3798 |
else if( eis && eis->change ) |
| 3799 |
{ |
| 3800 |
report_verbose( "check_line_status_register: sending OUTPUT_BUFFER_EMPTY\n" ); |
| 3801 |
send_event( eis, SPE_OUTPUT_BUFFER_EMPTY, 1 ); |
| 3802 |
} |
| 3803 |
#else |
| 3804 |
|
| 3805 |
|
| 3806 |
|
| 3807 |
if( eis && eis->output_buffer_empty_flag == 1 && |
| 3808 |
eis->eventflags[SPE_OUTPUT_BUFFER_EMPTY] ) |
| 3809 |
{ |
| 3810 |
report_verbose("check_line_status_register: sending SPE_OUTPUT_BUFFER_EMPTY\n"); |
| 3811 |
send_event( eis, SPE_OUTPUT_BUFFER_EMPTY, 1 ); |
| 3812 |
|
| 3813 |
|
| 3814 |
|
| 3815 |
eis->output_buffer_empty_flag = 0; |
| 3816 |
} |
| 3817 |
#endif |
| 3818 |
return( 0 ); |
| 3819 |
} |
| 3820 |
|
| 3821 |
|
| 3822 |
|
| 3823 |
|
| 3824 |
|
| 3825 |
|
| 3826 |
|
| 3827 |
|
| 3828 |
|
| 3829 |
|
| 3830 |
|
| 3831 |
|
| 3832 |
int has_line_status_register_access( int fd ) |
| 3833 |
{ |
| 3834 |
#if defined(TIOCSERGETLSR) |
| 3835 |
int change; |
| 3836 |
|
| 3837 |
if( !ioctl( fd, TIOCSERGETLSR, &change ) ) { |
| 3838 |
return(1); |
| 3839 |
} |
| 3840 |
#endif |
| 3841 |
report( "has_line_status_register_acess: Port does not support TIOCSERGETLSR\n" ); |
| 3842 |
return( 0 ); |
| 3843 |
} |
| 3844 |
|
| 3845 |
|
| 3846 |
|
| 3847 |
|
| 3848 |
|
| 3849 |
|
| 3850 |
|
| 3851 |
|
| 3852 |
|
| 3853 |
|
| 3854 |
|
| 3855 |
|
| 3856 |
|
| 3857 |
|
| 3858 |
|
| 3859 |
|
| 3860 |
void check_cgi_count( struct event_info_struct *eis ) |
| 3861 |
{ |
| 3862 |
#if defined(TIOCGICOUNT) |
| 3863 |
|
| 3864 |
|
| 3865 |
|
| 3866 |
struct serial_icounter_struct sis; |
| 3867 |
memcpy( &sis, &eis->osis, sizeof( struct serial_icounter_struct ) ); |
| 3868 |
|
| 3869 |
if( ioctl( eis->fd, TIOCGICOUNT, &sis ) ) |
| 3870 |
{ |
| 3871 |
report( "check_cgi_count: TIOCGICOUNT\n is not 0\n" ); |
| 3872 |
return; |
| 3873 |
} |
| 3874 |
while( eis && sis.frame != eis->osis.frame ) { |
| 3875 |
send_event( eis, SPE_FE, 1); |
| 3876 |
eis->osis.frame++; |
| 3877 |
} |
| 3878 |
while( eis && sis.overrun != eis->osis.overrun ) { |
| 3879 |
send_event( eis, SPE_OE, 1); |
| 3880 |
eis->osis.overrun++; |
| 3881 |
} |
| 3882 |
while( eis && sis.parity != eis->osis.parity ) { |
| 3883 |
send_event( eis, SPE_PE, 1); |
| 3884 |
eis->osis.parity++; |
| 3885 |
} |
| 3886 |
while( eis && sis.brk != eis->osis.brk ) { |
| 3887 |
send_event( eis, SPE_BI, 1); |
| 3888 |
eis->osis.brk++; |
| 3889 |
} |
| 3890 |
if( eis ) |
| 3891 |
memcpy( &eis->osis, &sis, sizeof( struct serial_icounter_struct ) ); |
| 3892 |
#endif |
| 3893 |
} |
| 3894 |
|
| 3895 |
|
| 3896 |
|
| 3897 |
|
| 3898 |
|
| 3899 |
|
| 3900 |
|
| 3901 |
|
| 3902 |
|
| 3903 |
|
| 3904 |
int port_has_changed_fionread( struct event_info_struct *eis ) |
| 3905 |
{ |
| 3906 |
int change, rc; |
| 3907 |
char message[80]; |
| 3908 |
|
| 3909 |
rc = ioctl( eis->fd, FIONREAD, &change ); |
| 3910 |
sprintf( message, "port_has_changed_fionread: change is %i ret is %i\n", change, eis->ret ); |
| 3911 |
#if defined(__unixware__) || defined(__sun__) |
| 3912 |
|
| 3913 |
|
| 3914 |
|
| 3915 |
|
| 3916 |
|
| 3917 |
|
| 3918 |
if( (rc != -1 && change) || (rc == -1 && eis->ret > 0) ) |
| 3919 |
return( 1 ); |
| 3920 |
#else |
| 3921 |
sprintf( message, "port_has_changed_fionread: change is %i\n", change ); |
| 3922 |
report_verbose( message ); |
| 3923 |
if( change ) |
| 3924 |
return( 1 ); |
| 3925 |
#endif |
| 3926 |
return( 0 ); |
| 3927 |
} |
| 3928 |
|
| 3929 |
|
| 3930 |
|
| 3931 |
|
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
|
| 3938 |
void check_tiocmget_changes( struct event_info_struct * eis ) |
| 3939 |
{ |
| 3940 |
unsigned int mflags = 0; |
| 3941 |
int change; |
| 3942 |
|
| 3943 |
|
| 3944 |
if( !eis ) return; |
| 3945 |
change = eis->change; |
| 3946 |
|
| 3947 |
report_verbose("entering check_tiocmget_changes\n"); |
| 3948 |
if( ioctl( eis->fd, TIOCMGET, &mflags ) ) |
| 3949 |
{ |
| 3950 |
report( "=======================================\n"); |
| 3951 |
report( "check_tiocmget_changes: ioctl(TIOCMGET)\n" ); |
| 3952 |
return; |
| 3953 |
} |
| 3954 |
|
| 3955 |
change = (mflags&TIOCM_CTS) - (eis->omflags&TIOCM_CTS); |
| 3956 |
if( eis && change ) send_event( eis, SPE_CTS, change ); |
| 3957 |
|
| 3958 |
change = (mflags&TIOCM_DSR) - (eis->omflags&TIOCM_DSR); |
| 3959 |
if( eis && change ) |
| 3960 |
{ |
| 3961 |
report( "sending DSR ===========================\n"); |
| 3962 |
send_event( eis, SPE_DSR, change ); |
| 3963 |
} |
| 3964 |
|
| 3965 |
change = (mflags&TIOCM_RNG) - (eis->omflags&TIOCM_RNG); |
| 3966 |
if( eis && change ) send_event( eis, SPE_RI, change ); |
| 3967 |
|
| 3968 |
change = (mflags&TIOCM_CD) - (eis->omflags&TIOCM_CD); |
| 3969 |
if( eis && change ) send_event( eis, SPE_CD, change ); |
| 3970 |
|
| 3971 |
if( eis ) |
| 3972 |
eis->omflags = mflags; |
| 3973 |
report_verbose("leaving check_tiocmget_changes\n"); |
| 3974 |
} |
| 3975 |
|
| 3976 |
|
| 3977 |
|
| 3978 |
|
| 3979 |
|
| 3980 |
|
| 3981 |
|
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
void system_wait() |
| 3986 |
{ |
| 3987 |
#if defined (__sun__ ) |
| 3988 |
struct timespec retspec, tspec; |
| 3989 |
retspec.tv_sec = 0; |
| 3990 |
retspec.tv_nsec = 100000000; |
| 3991 |
do { |
| 3992 |
tspec = retspec; |
| 3993 |
nanosleep( &tspec, &retspec ); |
| 3994 |
} while( tspec.tv_nsec != 0 ); |
| 3995 |
|
| 3996 |
|
| 3997 |
#else |
| 3998 |
#ifdef TRENT_IS_HERE_DEBUGGING_THREADS |
| 3999 |
|
| 4000 |
|
| 4001 |
|
| 4002 |
|
| 4003 |
|
| 4004 |
|
| 4005 |
#endif |
| 4006 |
#endif |
| 4007 |
} |
| 4008 |
|
| 4009 |
|
| 4010 |
|
| 4011 |
|
| 4012 |
|
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
|
| 4017 |
|
| 4018 |
|
| 4019 |
|
| 4020 |
int driver_has_tiocgicount( struct event_info_struct * eis ) |
| 4021 |
{ |
| 4022 |
#if defined(TIOCGICOUNT) |
| 4023 |
|
| 4024 |
|
| 4025 |
|
| 4026 |
|
| 4027 |
if( ioctl( eis->fd, TIOCGICOUNT, &eis->osis ) < 0 ) { |
| 4028 |
report_verbose( " driver_has_tiocgicount: Port does not support TIOCGICOUNT events\n" ); |
| 4029 |
return(0); |
| 4030 |
} |
| 4031 |
else |
| 4032 |
return(1); |
| 4033 |
#endif |
| 4034 |
return(0); |
| 4035 |
|
| 4036 |
} |
| 4037 |
|
| 4038 |
|
| 4039 |
|
| 4040 |
|
| 4041 |
|
| 4042 |
|
| 4043 |
|
| 4044 |
|
| 4045 |
|
| 4046 |
|
| 4047 |
void report_serial_events( struct event_info_struct *eis ) |
| 4048 |
{ |
| 4049 |
|
| 4050 |
|
| 4051 |
if( check_line_status_register( eis ) ) |
| 4052 |
return; |
| 4053 |
|
| 4054 |
if ( eis && eis->has_tiocgicount ) |
| 4055 |
check_cgi_count( eis ); |
| 4056 |
#ifndef WIN32 |
| 4057 |
#endif |
| 4058 |
|
| 4059 |
check_tiocmget_changes( eis ); |
| 4060 |
if( eis && port_has_changed_fionread( eis ) ) |
| 4061 |
{ |
| 4062 |
if(!eis->eventflags[SPE_DATA_AVAILABLE] ) |
| 4063 |
{ |
| 4064 |
report_verbose("report_serial_events: ignoring DATA_AVAILABLE\n"); |
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
usleep(20000); |
| 4069 |
#if !defined(__sun__) |
| 4070 |
|
| 4071 |
usleep(20000); |
| 4072 |
#endif |
| 4073 |
return; |
| 4074 |
} |
| 4075 |
report("report_serial_events: sending DATA_AVAILABLE\n"); |
| 4076 |
if(!send_event( eis, SPE_DATA_AVAILABLE, 1 )) |
| 4077 |
{ |
| 4078 |
|
| 4079 |
|
| 4080 |
|
| 4081 |
#if !defined(__sun__) |
| 4082 |
#endif |
| 4083 |
} |
| 4084 |
usleep(20000); |
| 4085 |
} |
| 4086 |
} |
| 4087 |
|
| 4088 |
|
| 4089 |
|
| 4090 |
|
| 4091 |
|
| 4092 |
|
| 4093 |
|
| 4094 |
|
| 4095 |
|
| 4096 |
|
| 4097 |
int initialise_event_info_struct( struct event_info_struct *eis ) |
| 4098 |
{ |
| 4099 |
int i; |
| 4100 |
jobject jobj = *eis->jobj; |
| 4101 |
JNIEnv *env = eis->env; |
| 4102 |
struct event_info_struct *index = master_index; |
| 4103 |
|
| 4104 |
if ( eis->initialised == 1 ) |
| 4105 |
goto end; |
| 4106 |
|
| 4107 |
#ifdef TIOCGICOUNT |
| 4108 |
memset(&eis->osis,0,sizeof(eis->osis)); |
| 4109 |
#endif |
| 4110 |
|
| 4111 |
if( index ) |
| 4112 |
{ |
| 4113 |
while( index->next ) |
| 4114 |
{ |
| 4115 |
index = index->next; |
| 4116 |
} |
| 4117 |
index->next = eis; |
| 4118 |
eis->prev = index; |
| 4119 |
eis->next = NULL; |
| 4120 |
} |
| 4121 |
else |
| 4122 |
{ |
| 4123 |
master_index = eis; |
| 4124 |
master_index->next = NULL; |
| 4125 |
master_index->prev = NULL; |
| 4126 |
} |
| 4127 |
|
| 4128 |
for( i = 0; i < 11; i++ ) eis->eventflags[i] = 0; |
| 4129 |
#if !defined(TIOCSERGETLSR) && !defined(WIN32) |
| 4130 |
eis->output_buffer_empty_flag = 0; |
| 4131 |
eis->writing = 0; |
| 4132 |
#endif |
| 4133 |
eis->eventloop_interrupted = 0; |
| 4134 |
eis->closing = 0; |
| 4135 |
|
| 4136 |
eis->fd = get_java_var( env, jobj, "fd", "I" ); |
| 4137 |
eis->has_tiocsergetlsr = has_line_status_register_access( eis->fd ); |
| 4138 |
eis->has_tiocgicount = driver_has_tiocgicount( eis ); |
| 4139 |
|
| 4140 |
if( ioctl( eis->fd, TIOCMGET, &eis->omflags) < 0 ) { |
| 4141 |
report( "initialise_event_info_struct: Port does not support events\n" ); |
| 4142 |
} |
| 4143 |
|
| 4144 |
eis->send_event = (*env)->GetMethodID( env, eis->jclazz, "sendEvent", |
| 4145 |
"(IZ)Z" ); |
| 4146 |
if(eis->send_event == NULL) |
| 4147 |
goto fail; |
| 4148 |
end: |
| 4149 |
FD_ZERO( &eis->rfds ); |
| 4150 |
FD_SET( eis->fd, &eis->rfds ); |
| 4151 |
eis->tv_sleep.tv_sec = 0; |
| 4152 |
eis->tv_sleep.tv_usec = 1000; |
| 4153 |
eis->initialised = 1; |
| 4154 |
return( 1 ); |
| 4155 |
fail: |
| 4156 |
report_error("initialise_event_info_struct: initialise failed!\n"); |
| 4157 |
finalize_event_info_struct( eis ); |
| 4158 |
return( 0 ); |
| 4159 |
} |
| 4160 |
|
| 4161 |
|
| 4162 |
|
| 4163 |
|
| 4164 |
|
| 4165 |
|
| 4166 |
|
| 4167 |
|
| 4168 |
|
| 4169 |
|
| 4170 |
void finalize_event_info_struct( struct event_info_struct *eis ) |
| 4171 |
{ |
| 4172 |
if( eis->jclazz) |
| 4173 |
{ |
| 4174 |
(*eis->env)->DeleteLocalRef( eis->env, eis->jclazz ); |
| 4175 |
} |
| 4176 |
if( eis->next && eis->prev ) |
| 4177 |
{ |
| 4178 |
eis->prev->next = eis->next; |
| 4179 |
eis->next->prev = eis->prev; |
| 4180 |
} |
| 4181 |
else if( eis->next ) |
| 4182 |
{ |
| 4183 |
eis->next->prev = NULL; |
| 4184 |
master_index = eis->next; |
| 4185 |
} |
| 4186 |
else if( eis->prev ) |
| 4187 |
eis->prev->next = NULL; |
| 4188 |
else master_index = NULL; |
| 4189 |
} |
| 4190 |
|
| 4191 |
|
| 4192 |
|
| 4193 |
|
| 4194 |
|
| 4195 |
|
| 4196 |
|
| 4197 |
|
| 4198 |
|
| 4199 |
|
| 4200 |
JNIEXPORT void JNICALL RXTXPort(eventLoop)( JNIEnv *env, jobject jobj ) |
| 4201 |
{ |
| 4202 |
#ifdef WIN32 |
| 4203 |
int i = 0; |
| 4204 |
#endif |
| 4205 |
struct event_info_struct eis; |
| 4206 |
eis.jclazz = (*env)->GetObjectClass( env, jobj ); |
| 4207 |
eis.env = env; |
| 4208 |
eis.jobj = &jobj; |
| 4209 |
eis.initialised = 0; |
| 4210 |
|
| 4211 |
ENTER( "eventLoop\n" ); |
| 4212 |
if ( !initialise_event_info_struct( &eis ) ) goto end; |
| 4213 |
if ( !init_threads( &eis ) ) goto end; |
| 4214 |
unlock_monitor_thread( &eis ); |
| 4215 |
do{ |
| 4216 |
report_time_eventLoop( ); |
| 4217 |
do { |
| 4218 |
|
| 4219 |
if( eis.closing ) |
| 4220 |
{ |
| 4221 |
report("eventLoop: got interrupt\n"); |
| 4222 |
finalize_threads( &eis ); |
| 4223 |
finalize_event_info_struct( &eis ); |
| 4224 |
LEAVE("eventLoop"); |
| 4225 |
return; |
| 4226 |
} |
| 4227 |
#ifndef WIN32 |
| 4228 |
|
| 4229 |
do { |
| 4230 |
eis.ret = SELECT( eis.fd + 1, &eis.rfds, NULL, NULL, |
| 4231 |
&eis.tv_sleep ); |
| 4232 |
} while (eis.ret < 0 && errno==EINTR); |
| 4233 |
#else |
| 4234 |
|
| 4235 |
|
| 4236 |
|
| 4237 |
|
| 4238 |
|
| 4239 |
|
| 4240 |
|
| 4241 |
|
| 4242 |
|
| 4243 |
eis.ret=1; |
| 4244 |
while( i++ < 5 ) |
| 4245 |
{ |
| 4246 |
if(eis.eventflags[SPE_DATA_AVAILABLE] ) |
| 4247 |
{ |
| 4248 |
if( port_has_changed_fionread( &eis ) ) |
| 4249 |
{ |
| 4250 |
send_event( &eis, SPE_DATA_AVAILABLE, 1 ); |
| 4251 |
} |
| 4252 |
} |
| 4253 |
usleep(1000); |
| 4254 |
} |
| 4255 |
i = 0; |
| 4256 |
#endif |
| 4257 |
} while ( eis.ret < 0 && errno == EINTR ); |
| 4258 |
if( eis.ret >= 0 ) |
| 4259 |
{ |
| 4260 |
report_serial_events( &eis ); |
| 4261 |
} |
| 4262 |
initialise_event_info_struct( &eis ); |
| 4263 |
} while( 1 ); |
| 4264 |
end: |
| 4265 |
LEAVE( "eventLoop: Bailing!\n" ); |
| 4266 |
} |
| 4267 |
|
| 4268 |
|
| 4269 |
|
| 4270 |
|
| 4271 |
|
| 4272 |
|
| 4273 |
|
| 4274 |
|
| 4275 |
|
| 4276 |
|
| 4277 |
|
| 4278 |
|
| 4279 |
|
| 4280 |
|
| 4281 |
JNIEXPORT jstring JNICALL RXTXVersion(nativeGetVersion) (JNIEnv *env, |
| 4282 |
jclass jclazz ) |
| 4283 |
{ |
| 4284 |
return (*env)->NewStringUTF( env, "RXTX-2.2" ); |
| 4285 |
} |
| 4286 |
|
| 4287 |
|
| 4288 |
|
| 4289 |
|
| 4290 |
|
| 4291 |
|
| 4292 |
|
| 4293 |
|
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
|
| 4299 |
JNIEXPORT jboolean JNICALL RXTXCommDriver(testRead)( |
| 4300 |
JNIEnv *env, |
| 4301 |
jobject jobj, |
| 4302 |
jstring tty_name, |
| 4303 |
jint port_type |
| 4304 |
) |
| 4305 |
{ |
| 4306 |
struct termios ttyset; |
| 4307 |
char c; |
| 4308 |
#ifdef TRENT_IS_HERE_DEBUGGING_ENUMERATION |
| 4309 |
char message[80]; |
| 4310 |
#endif |
| 4311 |
int fd; |
| 4312 |
const char *name = (*env)->GetStringUTFChars(env, tty_name, 0); |
| 4313 |
int ret = JNI_TRUE; |
| 4314 |
int pid = -1; |
| 4315 |
|
| 4316 |
#ifndef WIN32 |
| 4317 |
pid = getpid(); |
| 4318 |
#else |
| 4319 |
char full_windows_name[80]; |
| 4320 |
#endif |
| 4321 |
|
| 4322 |
ENTER( "RXTXPort:testRead" ); |
| 4323 |
#ifdef TRENT_IS_HERE_DEBUGGING_ENUMERATION |
| 4324 |
|
| 4325 |
|
| 4326 |
if( !strcmp( name, "COM1" ) || !strcmp( name, "COM2") ) |
| 4327 |
{ |
| 4328 |
printf("%s is good\n",name); |
| 4329 |
sprintf( message, "testRead: %s is good!\n", name ); |
| 4330 |
report( message ); |
| 4331 |
(*env)->ReleaseStringUTFChars( env, tty_name, name ); |
| 4332 |
return( JNI_TRUE ); |
| 4333 |
} |
| 4334 |
(*env)->ReleaseStringUTFChars( env, tty_name, name ); |
| 4335 |
return( JNI_FALSE ); |
| 4336 |
#endif |
| 4337 |
#ifdef WIN32 |
| 4338 |
strcpy( full_windows_name, DEVICEDIR ); |
| 4339 |
strcat( full_windows_name, name ); |
| 4340 |
ret = serial_test((char *) full_windows_name ); |
| 4341 |
(*env)->ReleaseStringUTFChars( env, tty_name, name ); |
| 4342 |
return(ret); |
| 4343 |
#endif |
| 4344 |
|
| 4345 |
|
| 4346 |
|
| 4347 |
|
| 4348 |
|
| 4349 |
|
| 4350 |
|
| 4351 |
|
| 4352 |
|
| 4353 |
if ( LOCK( name, pid ) ) |
| 4354 |
{ |
| 4355 |
(*env)->ReleaseStringUTFChars(env, tty_name, name); |
| 4356 |
LEAVE( "RXTXPort:testRead no lock" ); |
| 4357 |
return JNI_FALSE; |
| 4358 |
} |
| 4359 |
|
| 4360 |
|
| 4361 |
|
| 4362 |
|
| 4363 |
|
| 4364 |
do { |
| 4365 |
fd=OPEN ( name, O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 4366 |
} while ( fd < 0 && errno==EINTR ); |
| 4367 |
|
| 4368 |
if( fd < 0 ) |
| 4369 |
{ |
| •4370 |
report_verbose( "testRead() open \"" ); |
| 4371 |
report_verbose( name ); |
| 4372 |
report_verbose( "\" failed: " ); |
| 4373 |
report_verbose( strerror(errno) ); |
| 4374 |
report_verbose( "\n" ); |
| 4375 |
ret = JNI_FALSE; |
| 4376 |
goto END; |
| 4377 |
} |
| 4378 |
|
| 4379 |
if ( port_type == PORT_SERIAL ) |
| 4380 |
{ |
| 4381 |
int saved_flags; |
| 4382 |
struct termios saved_termios; |
| 4383 |
|
| 4384 |
if (tcgetattr(fd, &ttyset) < 0) { |
| 4385 |
ret = JNI_FALSE; |
| 4386 |
goto END; |
| 4387 |
} |
| 4388 |
|
| 4389 |
|
| 4390 |
if ( ( saved_flags = fcntl(fd, F_GETFL ) ) < 0 ) |
| 4391 |
{ |
| 4392 |
report( "testRead() fcntl(F_GETFL) failed\n" ); |
| 4393 |
ret = JNI_FALSE; |
| 4394 |
goto END; |
| 4395 |
} |
| 4396 |
|
| 4397 |
memcpy( &saved_termios, &ttyset, sizeof( struct termios ) ); |
| 4398 |
|
| 4399 |
if ( fcntl( fd, F_SETFL, O_NONBLOCK ) < 0 ) |
| 4400 |
{ |
| 4401 |
report( "testRead() fcntl(F_SETFL) failed\n" ); |
| 4402 |
ret = JNI_FALSE; |
| 4403 |
goto END; |
| 4404 |
} |
| 4405 |
|
| 4406 |
cfmakeraw(&ttyset); |
| 4407 |
ttyset.c_cc[VMIN] = ttyset.c_cc[VTIME] = 0; |
| 4408 |
|
| 4409 |
if ( tcsetattr( fd, TCSANOW, &ttyset) < 0 ) |
| 4410 |
{ |
| 4411 |
report( "testRead() tcsetattr failed\n" ); |
| 4412 |
ret = JNI_FALSE; |
| 4413 |
tcsetattr( fd, TCSANOW, &saved_termios ); |
| 4414 |
goto END; |
| 4415 |
} |
| 4416 |
|
| 4417 |
|
| 4418 |
|
| 4419 |
|
| 4420 |
|
| 4421 |
|
| 4422 |
|
| 4423 |
|
| 4424 |
|
| 4425 |
|
| 4426 |
|
| 4427 |
� |
| 4428 |
|
| 4429 |
� |
| 4430 |
|
| 4431 |
|
| 4432 |
|
| 4433 |
|
| 4434 |
|
| 4435 |
|
| 4436 |
|
| 4437 |
|
| 4438 |
|
| 4439 |
|
| 4440 |
|
| 4441 |
|
| 4442 |
|
| 4443 |
|
| 4444 |
|
| 4445 |
|
| 4446 |
|
| 4447 |
|
| 4448 |
|
| 4449 |
|
| 4450 |
|
| 4451 |
|
| 4452 |
|
| 4453 |
|
| 4454 |
|
| 4455 |
|
| 4456 |
|
| 4457 |
|
| 4458 |
|
| 4459 |
|
| 4460 |
|
| 4461 |
|
| 4462 |
|
| 4463 |
|
| 4464 |
|
| 4465 |
|
| 4466 |
|
| 4467 |
|
| 4468 |
|
| 4469 |
|
| 4470 |
|
| 4471 |
|
| 4472 |
|
| 4473 |
|
| 4474 |
|
| 4475 |
|
| 4476 |
|
| 4477 |
|
| 4478 |
|
| 4479 |
|
| 4480 |
|
| 4481 |
|
| 4482 |
|
| 4483 |
if ( READ( fd, &c, 1 ) < 0 ) |
| 4484 |
{ |
| 4485 |
#ifdef EAGAIN |
| 4486 |
if ( errno != EAGAIN ) { |
| 4487 |
report( "testRead() read failed\n" ); |
| 4488 |
ret = JNI_FALSE; |
| 4489 |
} |
| 4490 |
#else |
| 4491 |
#ifdef EWOULDBLOCK |
| 4492 |
if ( errno != EWOULDBLOCK ) |
| 4493 |
{ |
| 4494 |
report( "testRead() read failed\n" ); |
| 4495 |
ret = JNI_FALSE; |
| 4496 |
} |
| 4497 |
#else |
| 4498 |
ret = JNI_FALSE; |
| 4499 |
#endif |
| 4500 |
#endif |
| 4501 |
} |
| 4502 |
|
| 4503 |
|
| 4504 |
tcsetattr( fd, TCSANOW, &saved_termios ); |
| 4505 |
fcntl( fd, F_SETFL, saved_flags ); |
| 4506 |
} |
| 4507 |
|
| 4508 |
|
| 4509 |
|
| 4510 |
|
| 4511 |
|
| 4512 |
|
| 4513 |
|
| 4514 |
|
| 4515 |
|
| 4516 |
END: |
| 4517 |
UNLOCK(name, pid ); |
| 4518 |
(*env)->ReleaseStringUTFChars( env, tty_name, name ); |
| 4519 |
CLOSE( fd ); |
| 4520 |
LEAVE( "RXTXPort:testRead" ); |
| 4521 |
return ret; |
| 4522 |
} |
| 4523 |
|
| 4524 |
#if defined(__APPLE__) |
| 4525 |
|
| 4526 |
|
| 4527 |
|
| 4528 |
|
| 4529 |
|
| 4530 |
|
| 4531 |
|
| 4532 |
|
| 4533 |
|
| 4534 |
|
| 4535 |
|
| 4536 |
kern_return_t |
| 4537 |
createSerialIterator(io_iterator_t *serialIterator) |
| 4538 |
{ |
| 4539 |
kern_return_t kernResult; |
| 4540 |
mach_port_t masterPort; |
| 4541 |
CFMutableDictionaryRef classesToMatch; |
| 4542 |
if ((kernResult=IOMasterPort( (int) NULL, &masterPort ) ) != KERN_SUCCESS) |
| 4543 |
{ |
| 4544 |
printf( "IOMasterPort returned %d\n", kernResult); |
| 4545 |
return kernResult; |
| 4546 |
} |
| 4547 |
if ((classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue)) == NULL) |
| 4548 |
{ |
| 4549 |
printf( "IOServiceMatching returned NULL\n" ); |
| 4550 |
return kernResult; |
| 4551 |
} |
| 4552 |
CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); |
| 4553 |
kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, serialIterator); |
| 4554 |
if (kernResult != KERN_SUCCESS) |
| 4555 |
{ |
| 4556 |
printf( "IOServiceGetMatchingServices returned %d\n", kernResult); |
| 4557 |
} |
| 4558 |
return kernResult; |
| 4559 |
} |
| 4560 |
|
| 4561 |
|
| 4562 |
|
| 4563 |
|
| 4564 |
|
| 4565 |
|
| 4566 |
|
| 4567 |
|
| 4568 |
|
| 4569 |
|
| 4570 |
|
| 4571 |
|
| 4572 |
char * |
| 4573 |
getRegistryString(io_object_t sObj, char *propName) |
| 4574 |
{ |
| 4575 |
static char resultStr[256]; |
| 4576 |
CFTypeRef nameCFstring; |
| 4577 |
resultStr[0] = 0; |
| 4578 |
nameCFstring = IORegistryEntryCreateCFProperty(sObj, |
| 4579 |
CFStringCreateWithCString(kCFAllocatorDefault, propName, kCFStringEncodingASCII), |
| 4580 |
kCFAllocatorDefault, 0); |
| 4581 |
if (nameCFstring) |
| 4582 |
{ |
| 4583 |
CFStringGetCString(nameCFstring, resultStr, sizeof(resultStr), kCFStringEncodingASCII); |
| 4584 |
CFRelease(nameCFstring); |
| 4585 |
} |
| 4586 |
return resultStr; |
| 4587 |
} |
| 4588 |
|
| 4589 |
|
| 4590 |
|
| 4591 |
|
| 4592 |
|
| 4593 |
|
| 4594 |
|
| 4595 |
|
| 4596 |
|
| 4597 |
int |
| 4598 |
registerKnownSerialPorts(JNIEnv *env, jobject jobj, jint portType) |
| 4599 |
{ |
| 4600 |
io_iterator_t theSerialIterator; |
| 4601 |
io_object_t theObject; |
| 4602 |
int numPorts = 0; |
| 4603 |
|
| 4604 |
if (( createSerialIterator( &theSerialIterator ) != KERN_SUCCESS) || |
| 4605 |
( ! IOIteratorIsValid( theSerialIterator))) |
| 4606 |
{ |
| 4607 |
|
| 4608 |
report( "createSerialIterator failed\n" ); |
| 4609 |
return(0); |
| 4610 |
} else { |
| 4611 |
jclass cls; |
| 4612 |
jmethodID mid; |
| 4613 |
cls = (*env)->FindClass(env,"gnu/io/CommPortIdentifier" ); |
| 4614 |
if (cls == 0) { |
| 4615 |
report( "can't find class of gnu/io/CommPortIdentifier\n" ); |
| 4616 |
return numPorts; |
| 4617 |
} |
| 4618 |
mid = (*env)->GetStaticMethodID(env, cls, "addPortName", "(Ljava/lang/String;ILgnu/io/CommDriver;)V" ); |
| 4619 |
|
| 4620 |
if (mid == 0) { |
| 4621 |
printf( "getMethodID of CommDriver.addPortName failed\n" ); |
| 4622 |
} else { |
| 4623 |
while (theObject = IOIteratorNext(theSerialIterator)) |
| 4624 |
{ |
| 4625 |
|
| 4626 |
jstring tempJstring; |
| 4627 |
tempJstring = (*env)->NewStringUTF(env,getRegistryString(theObject, kIODialinDeviceKey)); |
| 4628 |
(*env)->CallStaticVoidMethod(env, cls, mid,tempJstring,portType,jobj); |
| 4629 |
(*env)->DeleteLocalRef(env,tempJstring); |
| 4630 |
numPorts++; |
| 4631 |
|
| 4632 |
tempJstring = (*env)->NewStringUTF(env,getRegistryString(theObject, kIOCalloutDeviceKey)); |
| 4633 |
(*env)->CallStaticVoidMethod(env, cls, mid,tempJstring,portType,jobj); |
| 4634 |
(*env)->DeleteLocalRef(env,tempJstring); |
| 4635 |
numPorts++; |
| 4636 |
|
| 4637 |
} |
| 4638 |
} |
| 4639 |
} |
| 4640 |
return numPorts; |
| 4641 |
} |
| 4642 |
#endif |
| 4643 |
|
| 4644 |
|
| 4645 |
|
| 4646 |
|
| 4647 |
|
| 4648 |
|
| 4649 |
|
| 4650 |
|
| 4651 |
|
| 4652 |
|
| 4653 |
JNIEXPORT jboolean JNICALL RXTXCommDriver(registerKnownPorts)(JNIEnv *env, |
| 4654 |
jobject jobj, jint portType) |
| 4655 |
{ |
| 4656 |
enum {PORT_TYPE_SERIAL = 1, |
| 4657 |
PORT_TYPE_PARALLEL, |
| 4658 |
PORT_TYPE_I2C, |
| 4659 |
PORT_TYPE_RS485, |
| 4660 |
PORT_TYPE_RAW}; |
| 4661 |
jboolean result = JNI_FALSE; |
| 4662 |
char message[80]; |
| 4663 |
|
| 4664 |
switch(portType) { |
| 4665 |
case PORT_TYPE_SERIAL: |
| 4666 |
#if defined(__APPLE__) |
| 4667 |
if (registerKnownSerialPorts(env, jobj, |
| 4668 |
PORT_TYPE_SERIAL) > 0) { |
| 4669 |
result = JNI_TRUE; |
| 4670 |
} |
| 4671 |
#endif |
| 4672 |
break; |
| 4673 |
case PORT_TYPE_PARALLEL: break; |
| 4674 |
case PORT_TYPE_I2C: break; |
| 4675 |
case PORT_TYPE_RS485: break; |
| 4676 |
case PORT_TYPE_RAW: break; |
| 4677 |
default: |
| 4678 |
sprintf( message, "unknown portType %d handed to \ |
| 4679 |
native RXTXCommDriver.registerKnownPorts() \ |
| 4680 |
method.\n", |
| 4681 |
(int) portType |
| 4682 |
); |
| 4683 |
report( message ); |
| 4684 |
} |
| 4685 |
return result; |
| 4686 |
} |
| 4687 |
|
| 4688 |
|
| 4689 |
|
| 4690 |
|
| 4691 |
|
| 4692 |
|
| 4693 |
|
| 4694 |
|
| 4695 |
|
| 4696 |
|
| 4697 |
JNIEXPORT jboolean JNICALL RXTXCommDriver(isPortPrefixValid)(JNIEnv *env, |
| 4698 |
jobject jobj, jstring tty_name) |
| 4699 |
{ |
| 4700 |
jboolean result; |
| 4701 |
static struct stat mystat; |
| 4702 |
char teststring[256]; |
| 4703 |
int fd,i; |
| 4704 |
const char *name = (*env)->GetStringUTFChars(env, tty_name, 0); |
| 4705 |
|
| 4706 |
ENTER( "RXTXCommDriver:isPortPrefixValid" ); |
| 4707 |
for(i=0;i<64;i++){ |
| 4708 |
#if defined(__sun__) |
| 4709 |
|
| 4710 |
if( i > 25 ) break; |
| 4711 |
sprintf(teststring,"%s%s%c",DEVICEDIR, name, i + 97 ); |
| 4712 |
fprintf(stderr, "testing: %s\n", teststring); |
| 4713 |
#else |
| 4714 |
#if defined(_GNU_SOURCE) |
| 4715 |
snprintf(teststring, 256, "%s%s%i",DEVICEDIR,name, i); |
| 4716 |
#else |
| 4717 |
sprintf(teststring,"%s%s%i",DEVICEDIR,name, i); |
| 4718 |
#endif |
| 4719 |
stat(teststring,&mystat); |
| 4720 |
#endif |
| 4721 |
|
| •4722 |
#ifndef __FreeBSD_kernel__ |
| 4723 |
if(S_ISCHR(mystat.st_mode)){ |
| 4724 |
fd=OPEN(teststring,O_RDONLY|O_NONBLOCK); |
| 4725 |
if (fd>0){ |
| 4726 |
CLOSE(fd); |
| 4727 |
result=JNI_TRUE; |
| 4728 |
break; |
| 4729 |
} |
| 4730 |
else |
| 4731 |
result=JNI_FALSE; |
| 4732 |
} |
| 4733 |
else |
| 4734 |
result=JNI_FALSE; |
| 4735 |
#else |
| 4736 |
result=JNI_TRUE; |
| 4737 |
#endif |
| 4738 |
} |
| 4739 |
#if defined(_GNU_SOURCE) |
| 4740 |
snprintf(teststring, 256, "%s%s",DEVICEDIR,name); |
| 4741 |
#else |
| 4742 |
sprintf(teststring,"%s%s",DEVICEDIR,name); |
| 4743 |
#endif |
| 4744 |
stat(teststring,&mystat); |
| 4745 |
if(S_ISCHR(mystat.st_mode)){ |
| 4746 |
fd=OPEN(teststring,O_RDONLY|O_NONBLOCK); |
| 4747 |
if (fd>0){ |
| 4748 |
CLOSE(fd); |
| 4749 |
result=JNI_TRUE; |
| 4750 |
} |
| 4751 |
} |
| 4752 |
(*env)->ReleaseStringUTFChars(env, tty_name, name); |
| 4753 |
LEAVE( "RXTXCommDriver:isPortPrefixValid" ); |
| 4754 |
return(result); |
| 4755 |
} |
| 4756 |
|
| 4757 |
|
| 4758 |
|
| 4759 |
|
| 4760 |
|
| 4761 |
|
| 4762 |
|
| 4763 |
|
| 4764 |
|
| 4765 |
|
| 4766 |
|
| 4767 |
|
| 4768 |
JNIEXPORT jstring JNICALL RXTXCommDriver(getDeviceDirectory)(JNIEnv *env, |
| 4769 |
jobject jobj) |
| 4770 |
{ |
| 4771 |
ENTER( "RXTXCommDriver:getDeviceDirectory" ); |
| 4772 |
return (*env)->NewStringUTF(env, DEVICEDIR); |
| 4773 |
LEAVE( "RXTXCommDriver:getDeviceDirectory" ); |
| 4774 |
} |
| 4775 |
|
| 4776 |
|
| 4777 |
|
| 4778 |
|
| 4779 |
|
| 4780 |
|
| 4781 |
|
| 4782 |
|
| 4783 |
|
| 4784 |
|
| 4785 |
JNIEXPORT void JNICALL RXTXPort(setInputBufferSize)(JNIEnv *env, |
| 4786 |
jobject jobj, jint size ) |
| 4787 |
{ |
| 4788 |
report( "setInputBufferSize is not implemented\n" ); |
| 4789 |
} |
| 4790 |
|
| 4791 |
|
| 4792 |
|
| 4793 |
|
| 4794 |
|
| 4795 |
|
| 4796 |
|
| 4797 |
|
| 4798 |
|
| 4799 |
|
| 4800 |
JNIEXPORT jint JNICALL RXTXPort(getInputBufferSize)(JNIEnv *env, |
| 4801 |
jobject jobj) |
| 4802 |
{ |
| 4803 |
report( "getInputBufferSize is not implemented\n" ); |
| 4804 |
return(1); |
| 4805 |
} |
| 4806 |
|
| 4807 |
|
| 4808 |
|
| 4809 |
|
| 4810 |
|
| 4811 |
|
| 4812 |
|
| 4813 |
|
| 4814 |
|
| 4815 |
|
| 4816 |
JNIEXPORT void JNICALL RXTXPort(setOutputBufferSize)(JNIEnv *env, |
| 4817 |
jobject jobj, jint size ) |
| 4818 |
{ |
| 4819 |
report( "setOutputBufferSize is not implemented\n" ); |
| 4820 |
} |
| 4821 |
|
| 4822 |
|
| 4823 |
|
| 4824 |
|
| 4825 |
|
| 4826 |
|
| 4827 |
|
| 4828 |
|
| 4829 |
|
| 4830 |
|
| 4831 |
JNIEXPORT jint JNICALL RXTXPort(getOutputBufferSize)(JNIEnv *env, |
| 4832 |
jobject jobj) |
| 4833 |
{ |
| 4834 |
report( "getOutputBufferSize is not implemented\n" ); |
| 4835 |
return(1); |
| 4836 |
} |
| 4837 |
|
| 4838 |
|
| 4839 |
|
| 4840 |
|
| 4841 |
|
| 4842 |
|
| 4843 |
|
| 4844 |
|
| 4845 |
|
| 4846 |
|
| 4847 |
|
| 4848 |
|
| 4849 |
|
| 4850 |
JNIEXPORT void JNICALL RXTXPort(interruptEventLoop)(JNIEnv *env, |
| 4851 |
jobject jobj) |
| 4852 |
{ |
| 4853 |
struct event_info_struct *index = master_index; |
| 4854 |
int fd = get_java_var( env, jobj, "fd", "I" ); |
| 4855 |
int searching = 1; |
| 4856 |
|
| 4857 |
|
| 4858 |
while( searching ) |
| 4859 |
{ |
| 4860 |
index = master_index; |
| 4861 |
if( index ) |
| 4862 |
{ |
| 4863 |
while( index->fd != fd && |
| 4864 |
index->next ) index = index->next; |
| 4865 |
if ( index->fd == fd ) searching = 0; |
| 4866 |
} |
| 4867 |
else |
| 4868 |
report("x"); |
| 4869 |
if( searching ) |
| 4870 |
{ |
| 4871 |
report("@"); |
| 4872 |
usleep(1000); |
| 4873 |
} |
| 4874 |
} |
| 4875 |
index->eventloop_interrupted = 1; |
| 4876 |
|
| 4877 |
|
| 4878 |
|
| 4879 |
|
| 4880 |
|
| 4881 |
|
| 4882 |
|
| 4883 |
|
| 4884 |
#ifdef TIOCSERGETLSR |
| 4885 |
index->closing=1; |
| 4886 |
#endif |
| 4887 |
#ifdef WIN32 |
| 4888 |
termios_interrupt_event_loop( index->fd, 1 ); |
| 4889 |
#endif |
| 4890 |
#if !defined(TIOCSERGETLSR) && !defined(WIN32) |
| 4891 |
|
| 4892 |
pthread_kill(index->drain_tid, SIGABRT); |
| 4893 |
|
| 4894 |
usleep(50 * 1000); |
| 4895 |
|
| 4896 |
|
| 4897 |
|
| 4898 |
|
| 4899 |
|
| 4900 |
|
| 4901 |
if (index->closing != 1) { |
| 4902 |
|
| 4903 |
report("interruptEventLoop: canceling blocked drain thread\n"); |
| 4904 |
pthread_cancel(index->drain_tid); |
| 4905 |
index->closing = 1; |
| 4906 |
} |
| 4907 |
#endif |
| 4908 |
report("interruptEventLoop: interrupted\n"); |
| 4909 |
} |
| 4910 |
|
| 4911 |
|
| 4912 |
|
| 4913 |
|
| 4914 |
|
| 4915 |
|
| 4916 |
|
| 4917 |
|
| 4918 |
|
| 4919 |
|
| 4920 |
jboolean is_interrupted( struct event_info_struct *eis ) |
| 4921 |
{ |
| 4922 |
int result; |
| 4923 |
JNIEnv *env = eis->env; |
| 4924 |
|
| 4925 |
ENTER( "is_interrupted" ); |
| 4926 |
(*env)->ExceptionClear(env); |
| 4927 |
result = (*env)->CallBooleanMethod( env, *eis->jobj, |
| 4928 |
eis->checkMonitorThread ); |
| 4929 |
#ifdef DEBUG |
| 4930 |
if((*env)->ExceptionOccurred(env)) { |
| 4931 |
report ( "is_interrupted: an error occured calling sendEvent()\n" ); |
| 4932 |
(*env)->ExceptionDescribe(env); |
| 4933 |
(*env)->ExceptionClear(env); |
| 4934 |
} |
| 4935 |
#endif |
| 4936 |
LEAVE( "RXTXCommDriver:is_interrupted" ); |
| 4937 |
return(result); |
| 4938 |
} |
| 4939 |
|
| 4940 |
|
| 4941 |
|
| 4942 |
|
| 4943 |
|
| 4944 |
|
| 4945 |
|
| 4946 |
|
| 4947 |
|
| 4948 |
|
| 4949 |
JNIEXPORT void JNICALL RXTXPort(nativeSetEventFlag)( JNIEnv *env, |
| 4950 |
jobject jobj, |
| 4951 |
jint fd, |
| 4952 |
jint event, |
| 4953 |
jboolean flag ) |
| 4954 |
{ |
| 4955 |
struct event_info_struct *index = master_index; |
| 4956 |
|
| 4957 |
if( !index ) |
| 4958 |
{ |
| 4959 |
report_error("nativeSetEventFlag !index\n"); |
| 4960 |
return; |
| 4961 |
} |
| 4962 |
while( index->fd != fd && index->next ) |
| 4963 |
{ |
| 4964 |
index = index->next; |
| 4965 |
} |
| 4966 |
if( index->fd != fd ) |
| 4967 |
{ |
| 4968 |
report_error("nativeSetEventFlag !fd\n"); |
| 4969 |
return; |
| 4970 |
} |
| 4971 |
index->eventflags[event] = (int) flag; |
| 4972 |
#ifdef WIN32 |
| 4973 |
termios_setflags( fd, index->eventflags ); |
| 4974 |
#endif |
| 4975 |
|
| 4976 |
} |
| 4977 |
|
| 4978 |
|
| 4979 |
|
| 4980 |
|
| 4981 |
|
| 4982 |
|
| 4983 |
|
| 4984 |
|
| 4985 |
|
| 4986 |
|
| 4987 |
int send_event( struct event_info_struct *eis, jint type, int flag ) |
| 4988 |
{ |
| 4989 |
int result; |
| 4990 |
JNIEnv *env; |
| 4991 |
if( eis ) env = eis->env; |
| 4992 |
else return(-1); |
| 4993 |
|
| 4994 |
ENTER( "send_event" ); |
| 4995 |
if( !eis || eis->eventloop_interrupted > 1 ) |
| 4996 |
{ |
| 4997 |
report("event loop interrupted\n"); |
| 4998 |
return JNI_TRUE; |
| 4999 |
} |
| 5000 |
report_verbose("send_event: !eventloop_interupted\n"); |
| 5001 |
if(eis->jclazz == NULL) return JNI_TRUE; |
| 5002 |
report_verbose("send_event: jclazz\n"); |
| 5003 |
|
| 5004 |
(*env)->ExceptionClear(env); |
| 5005 |
|
| 5006 |
report_verbose("send_event: calling\n"); |
| 5007 |
result = (*env)->CallBooleanMethod( env, *eis->jobj, eis->send_event, |
| 5008 |
type, flag > 0 ? JNI_TRUE : JNI_FALSE ); |
| 5009 |
report_verbose("send_event: called\n"); |
| 5010 |
|
| 5011 |
#ifdef asdf |
| 5012 |
if(!eis || (*eis->env)->ExceptionOccurred(eis->env)) { |
| 5013 |
report ( "send_event: an error occured calling sendEvent()\n" ); |
| 5014 |
(*eis->env)->ExceptionDescribe(eis->env); |
| 5015 |
(*eis->env)->ExceptionClear(eis->env); |
| 5016 |
} |
| 5017 |
#endif |
| 5018 |
|
| 5019 |
LEAVE( "send_event" ); |
| 5020 |
return(result); |
| 5021 |
} |
| 5022 |
|
| 5023 |
|
| 5024 |
|
| 5025 |
|
| 5026 |
|
| 5027 |
|
| 5028 |
|
| 5029 |
|
| 5030 |
|
| 5031 |
|
| 5032 |
size_t get_java_var( JNIEnv *env, jobject jobj, char *id, char *type ) |
| 5033 |
{ |
| 5034 |
int result = 0; |
| 5035 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 5036 |
jfieldID jfd = (*env)->GetFieldID( env, jclazz, id, type ); |
| 5037 |
|
| 5038 |
|
| 5039 |
|
| 5040 |
|
| 5041 |
if( !jfd ) { |
| 5042 |
(*env)->ExceptionDescribe( env ); |
| 5043 |
(*env)->ExceptionClear( env ); |
| 5044 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 5045 |
LEAVE( "get_java_var" ); |
| 5046 |
return result; |
| 5047 |
} |
| 5048 |
result = (int)( (*env)->GetIntField( env, jobj, jfd ) ); |
| 5049 |
|
| 5050 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 5051 |
if(!strncmp( "fd",id,2) && result == 0) |
| 5052 |
report_error( "get_java_var: invalid file descriptor\n" ); |
| 5053 |
|
| 5054 |
|
| 5055 |
|
| 5056 |
return result; |
| 5057 |
} |
| 5058 |
|
| 5059 |
|
| 5060 |
|
| 5061 |
|
| 5062 |
|
| 5063 |
|
| 5064 |
|
| 5065 |
|
| 5066 |
|
| 5067 |
|
| 5068 |
|
| 5069 |
|
| 5070 |
|
| 5071 |
void throw_java_exception( JNIEnv *env, char *exc, char *foo, char *msg ) |
| 5072 |
{ |
| 5073 |
char buf[ 60 ]; |
| 5074 |
jclass clazz = (*env)->FindClass( env, exc ); |
| 5075 |
ENTER( "throw_java_exception" ); |
| 5076 |
if( !clazz ) { |
| 5077 |
(*env)->ExceptionDescribe( env ); |
| 5078 |
(*env)->ExceptionClear( env ); |
| 5079 |
LEAVE( "throw_java_exception" ); |
| 5080 |
return; |
| 5081 |
} |
| 5082 |
#if defined(_GNU_SOURCE) |
| 5083 |
snprintf( buf, 60, "%s in %s", msg, foo ); |
| 5084 |
#else |
| 5085 |
sprintf( buf,"%s in %s", msg, foo ); |
| 5086 |
#endif |
| 5087 |
(*env)->ThrowNew( env, clazz, buf ); |
| 5088 |
|
| 5089 |
(*env)->DeleteLocalRef( env, clazz ); |
| 5090 |
LEAVE( "throw_java_exception" ); |
| 5091 |
} |
| 5092 |
|
| 5093 |
|
| 5094 |
|
| 5095 |
|
| 5096 |
|
| 5097 |
|
| 5098 |
|
| 5099 |
|
| 5100 |
|
| 5101 |
|
| 5102 |
void report_warning(const char *msg) |
| 5103 |
{ |
| 5104 |
#ifndef DEBUG_MW |
| 5105 |
fprintf(stderr, "%s", msg); |
| 5106 |
#else |
| 5107 |
mexWarnMsgTxt( (const char *) msg ); |
| 5108 |
#endif |
| 5109 |
} |
| •5110 |
|
| 5111 |
|
| 5112 |
|
| •5113 |
|
| 5114 |
|
| 5115 |
|
| 5116 |
|
| 5117 |
|
| 5118 |
|
| 5119 |
|
| 5120 |
void report_verbose(const char *msg) |
| 5121 |
{ |
| 5122 |
#ifdef DEBUG_VERBOSE |
| 5123 |
#ifdef DEBUG_MW |
| 5124 |
mexErrMsgTxt( msg ); |
| 5125 |
#else |
| 5126 |
fprintf(stderr, "%s", msg); |
| 5127 |
#endif |
| •5128 |
#endif |
| 5129 |
} |
| 5130 |
|
| 5131 |
|
| 5132 |
|
| 5133 |
|
| •5134 |
|
| 5135 |
|
| 5136 |
|
| 5137 |
|
| 5138 |
|
| 5139 |
void report_error(const char *msg) |
| 5140 |
{ |
| 5141 |
#ifndef DEBUG_MW |
| 5142 |
fprintf(stderr, "%s", msg); |
| 5143 |
#else |
| 5144 |
mexWarnMsgTxt( msg ); |
| 5145 |
#endif |
| 5146 |
} |
| •5147 |
|
| 5148 |
|
| 5149 |
|
| •5150 |
|
| 5151 |
|
| 5152 |
|
| 5153 |
|
| 5154 |
|
| 5155 |
|
| 5156 |
|
| 5157 |
void report(const char *msg) |
| 5158 |
{ |
| 5159 |
#ifdef DEBUG |
| 5160 |
# ifndef DEBUG_MW |
| 5161 |
fprintf(stderr, "%s", msg); |
| 5162 |
# else |
| 5163 |
mexPrintf( msg ); |
| 5164 |
# endif |
| •5165 |
#endif |
| 5166 |
} |
| 5167 |
|
| 5168 |
#ifndef WIN32 |
| •5169 |
#ifdef LFS |
| 5170 |
|
| 5171 |
|
| 5172 |
|
| 5173 |
|
| 5174 |
|
| 5175 |
|
| 5176 |
|
| 5177 |
|
| 5178 |
|
| 5179 |
|
| 5180 |
|
| 5181 |
int lfs_lock( const char *filename, int pid ) |
| 5182 |
{ |
| 5183 |
int s; |
| 5184 |
int ret; |
| 5185 |
int size = 1024; |
| 5186 |
char *buffer = malloc(size); |
| 5187 |
struct sockaddr_in addr; |
| 5188 |
|
| 5189 |
if ( !( s = socket( AF_INET, SOCK_STREAM, 0 ) ) > 0 ) |
| 5190 |
return 1; |
| 5191 |
addr.sin_family = AF_INET; |
| 5192 |
addr.sin_port = htons( 50001 ); |
| 5193 |
addr.sin_addr.s_addr = inet_addr( "127.0.0.1" ); |
| 5194 |
|
| 5195 |
if ( !connect( s, ( struct sockaddr * ) &addr, sizeof( addr ) ) == 0 ) |
| 5196 |
return 1; |
| 5197 |
ret=recv( s, buffer, size, 0 ); |
| 5198 |
sprintf( buffer, "lock %s %i\n", filename, pid ); |
| 5199 |
|
| 5200 |
send( s, buffer, strlen(buffer), 0 ); |
| 5201 |
ret=recv( s, buffer, size, 0 ); |
| 5202 |
if ( ret > 0 ) |
| 5203 |
{ |
| 5204 |
buffer[ret] = '\0'; |
| 5205 |
|
| 5206 |
} |
| 5207 |
send( s, "quit\n", strlen( "quit\n" ), 0 ); |
| 5208 |
close(s); |
| 5209 |
|
| 5210 |
if( buffer[0] == '2' ) return 0; |
| 5211 |
return 1; |
| 5212 |
} |
| 5213 |
|
| 5214 |
|
| 5215 |
|
| 5216 |
|
| 5217 |
|
| 5218 |
|
| 5219 |
|
| 5220 |
|
| 5221 |
|
| 5222 |
|
| 5223 |
|
| 5224 |
|
| 5225 |
int lfs_unlock( const char *filename, int pid ) |
| 5226 |
{ |
| 5227 |
int s; |
| 5228 |
int ret; |
| 5229 |
int size = 1024; |
| 5230 |
char *buffer = malloc(size); |
| 5231 |
struct sockaddr_in addr; |
| 5232 |
|
| 5233 |
if ( !( s = socket( AF_INET, SOCK_STREAM, 0 ) ) > 0 ) |
| 5234 |
return 1; |
| 5235 |
addr.sin_family = AF_INET; |
| 5236 |
addr.sin_port = htons( 50001 ); |
| 5237 |
addr.sin_addr.s_addr = inet_addr( "127.0.0.1" ); |
| 5238 |
|
| 5239 |
if ( !connect( s, ( struct sockaddr * ) &addr, sizeof( addr ) ) == 0 ) |
| 5240 |
return 1; |
| 5241 |
sprintf( buffer, "unlock %s %i\n", filename, pid ); |
| 5242 |
|
| 5243 |
send( s, buffer, strlen(buffer), 0 ); |
| 5244 |
ret = recv( s, buffer, size, 0 ); |
| 5245 |
if ( ret > 0 ) |
| 5246 |
{ |
| 5247 |
buffer[ret] = '\0'; |
| 5248 |
|
| 5249 |
} |
| 5250 |
send( s, "quit\n", strlen( "quit\n" ), 0 ); |
| 5251 |
close(s); |
| 5252 |
if( buffer[0] == '2' ) return 0; |
| 5253 |
return 1; |
| 5254 |
} |
| 5255 |
#endif |
| 5256 |
|
| 5257 |
|
| 5258 |
|
| 5259 |
|
| 5260 |
|
| 5261 |
|
| 5262 |
|
| 5263 |
|
| 5264 |
|
| 5265 |
|
| 5266 |
|
| 5267 |
|
| 5268 |
|
| 5269 |
|
| 5270 |
#ifdef LIBLOCKDEV |
| 5271 |
int lib_lock_dev_unlock( const char *filename, int pid ) |
| 5272 |
{ |
| 5273 |
if( dev_unlock( filename, pid ) ) |
| 5274 |
{ |
| 5275 |
report("fhs_unlock: Unable to remove LockFile\n"); |
| 5276 |
return(1); |
| 5277 |
} |
| 5278 |
return(0); |
| 5279 |
} |
| 5280 |
#endif |
| 5281 |
|
| 5282 |
|
| 5283 |
|
| 5284 |
|
| 5285 |
|
| 5286 |
|
| 5287 |
|
| 5288 |
|
| 5289 |
|
| 5290 |
|
| 5291 |
|
| 5292 |
|
| 5293 |
|
| 5294 |
|
| 5295 |
|
| 5296 |
|
| 5297 |
|
| 5298 |
#ifdef LIBLOCKDEV |
| 5299 |
int lib_lock_dev_lock( const char *filename, int pid ) |
| 5300 |
{ |
| 5301 |
char message[80]; |
| 5302 |
printf("LOCKING %s\n", filename); |
| 5303 |
if ( dev_testlock( filename ) ) |
| 5304 |
{ |
| 5305 |
report( "fhs_lock() lockstatus fail\n" ); |
| 5306 |
return 1; |
| 5307 |
} |
| 5308 |
if ( dev_lock( filename ) ) |
| 5309 |
{ |
| 5310 |
snprintf( message, 79, |
| 5311 |
"RXTX fhs_lock() Error: creating lock file for: %s: %s\n", |
| 5312 |
filename, strerror(errno) ); |
| 5313 |
report_error( message ); |
| 5314 |
return 1; |
| 5315 |
} |
| 5316 |
return( 0 ); |
| 5317 |
} |
| •5318 |
#endif |
| 5319 |
|
| 5320 |
|
| 5321 |
|
| 5322 |
|
| 5323 |
|
| 5324 |
|
| 5325 |
|
| 5326 |
|
| 5327 |
|
| 5328 |
|
| 5329 |
|
| 5330 |
|
| 5331 |
|
| 5332 |
|
| 5333 |
|
| 5334 |
|
| 5335 |
int fhs_lock( const char *filename, int pid ) |
| 5336 |
{ |
| 5337 |
|
| 5338 |
|
| 5339 |
|
| 5340 |
|
| 5341 |
|
| 5342 |
|
| 5343 |
|
| 5344 |
int fd,j; |
| 5345 |
char lockinfo[12], message[80]; |
| 5346 |
char file[80], *p; |
| 5347 |
|
| 5348 |
j = strlen( filename ); |
| 5349 |
p = ( char * ) filename + j; |
| 5350 |
|
| 5351 |
|
| 5352 |
|
| 5353 |
|
| 5354 |
while( *( p - 1 ) != '/' && j-- != 1 ) |
| 5355 |
{ |
| 5356 |
#if defined ( __unixware__ ) |
| 5357 |
*p = tolower( *p ); |
| 5358 |
#endif |
| 5359 |
p--; |
| 5360 |
} |
| 5361 |
sprintf( file, "%s/LCK..%s", LOCKDIR, p ); |
| 5362 |
if ( check_lock_status( filename ) ) |
| 5363 |
{ |
| 5364 |
report( "fhs_lock() lockstatus fail\n" ); |
| 5365 |
return 1; |
| 5366 |
} |
| 5367 |
fd = open( file, O_CREAT | O_WRONLY | O_EXCL, 0444 ); |
| 5368 |
if( fd < 0 ) |
| 5369 |
{ |
| 5370 |
snprintf( message, 79, |
| 5371 |
"RXTX fhs_lock() Error: creating lock file: %s: %s\n", |
| 5372 |
file, strerror(errno) ); |
| 5373 |
report_error( message ); |
| 5374 |
return 1; |
| 5375 |
} |
| 5376 |
sprintf( lockinfo, "%10d\n",(int) getpid() ); |
| 5377 |
sprintf( message, "fhs_lock: creating lockfile: %s\n", lockinfo ); |
| •5378 |
report( message ); |
| 5379 |
write( fd, lockinfo, 11 ); |
| 5380 |
close( fd ); |
| 5381 |
return 0; |
| 5382 |
} |
| 5383 |
|
| 5384 |
|
| 5385 |
|
| 5386 |
|
| 5387 |
|
| 5388 |
|
| 5389 |
|
| 5390 |
|
| 5391 |
|
| 5392 |
|
| 5393 |
|
| 5394 |
|
| 5395 |
|
| 5396 |
|
| 5397 |
|
| 5398 |
|
| 5399 |
|
| 5400 |
|
| 5401 |
|
| 5402 |
|
| 5403 |
|
| 5404 |
|
| 5405 |
|
| 5406 |
|
| 5407 |
|
| 5408 |
|
| 5409 |
|
| 5410 |
|
| 5411 |
|
| 5412 |
|
| 5413 |
|
| 5414 |
|
| 5415 |
|
| 5416 |
|
| 5417 |
|
| 5418 |
|
| 5419 |
|
| 5420 |
|
| 5421 |
|
| 5422 |
|
| 5423 |
|
| 5424 |
|
| 5425 |
|
| 5426 |
int uucp_lock( const char *filename, int pid ) |
| 5427 |
{ |
| 5428 |
char lockfilename[80], lockinfo[12], message[80]; |
| 5429 |
char name[80]; |
| 5430 |
int fd; |
| 5431 |
struct stat buf; |
| 5432 |
|
| 5433 |
sprintf( message, "uucp_lock( %s );\n", filename ); |
| 5434 |
report( message ); |
| 5435 |
|
| 5436 |
if ( check_lock_status( filename ) ) |
| 5437 |
{ |
| 5438 |
report( "RXTX uucp check_lock_status true\n" ); |
| 5439 |
return 1; |
| 5440 |
} |
| 5441 |
if ( stat( LOCKDIR, &buf ) != 0 ) |
| 5442 |
{ |
| 5443 |
report( "RXTX uucp_lock() could not find lock directory.\n" ); |
| 5444 |
return 1; |
| 5445 |
} |
| 5446 |
if ( stat( filename, &buf ) != 0 ) |
| 5447 |
{ |
| 5448 |
report( "RXTX uucp_lock() could not find device.\n" ); |
| 5449 |
sprintf( message, "uucp_lock: device was %s\n", name ); |
| 5450 |
report( message ); |
| 5451 |
return 1; |
| 5452 |
} |
| 5453 |
sprintf( lockfilename, "%s/LK.%03d.%03d.%03d", |
| 5454 |
LOCKDIR, |
| 5455 |
(int) major( buf.st_dev ), |
| 5456 |
(int) major( buf.st_rdev ), |
| 5457 |
(int) minor( buf.st_rdev ) |
| 5458 |
); |
| 5459 |
sprintf( lockinfo, "%10d\n", (int) getpid() ); |
| 5460 |
if ( stat( lockfilename, &buf ) == 0 ) |
| 5461 |
{ |
| 5462 |
sprintf( message, "RXTX uucp_lock() %s is there\n", |
| 5463 |
lockfilename ); |
| 5464 |
report( message ); |
| 5465 |
report_error( message ); |
| 5466 |
return 1; |
| 5467 |
} |
| 5468 |
fd = open( lockfilename, O_CREAT | O_WRONLY | O_EXCL, 0444 ); |
| 5469 |
if( fd < 0 ) |
| 5470 |
{ |
| 5471 |
sprintf( message, |
| 5472 |
"RXTX uucp_lock() Error: creating lock file: %s\n", |
| 5473 |
lockfilename ); |
| 5474 |
report_error( message ); |
| 5475 |
return 1; |
| 5476 |
} |
| 5477 |
write( fd, lockinfo,11 ); |
| 5478 |
close( fd ); |
| 5479 |
return 0; |
| 5480 |
} |
| 5481 |
|
| 5482 |
|
| 5483 |
|
| 5484 |
|
| 5485 |
|
| 5486 |
|
| 5487 |
|
| 5488 |
|
| 5489 |
|
| 5490 |
|
| 5491 |
int check_lock_status( const char *filename ) |
| 5492 |
{ |
| 5493 |
struct stat buf; |
| 5494 |
|
| 5495 |
|
| 5496 |
if ( stat( LOCKDIR, &buf ) != 0 ) |
| 5497 |
{ |
| 5498 |
report( "check_lock_status: could not find lock directory.\n" ); |
| 5499 |
return 1; |
| 5500 |
} |
| 5501 |
|
| 5502 |
|
| 5503 |
|
| 5504 |
if ( check_group_uucp() ) |
| 5505 |
{ |
| 5506 |
report_error( "check_lock_status: No permission to create lock file.\nplease see: How can I use Lock Files with rxtx? in INSTALL\n" ); |
| 5507 |
return(1); |
| 5508 |
} |
| 5509 |
|
| 5510 |
|
| 5511 |
|
| 5512 |
if ( is_device_locked( filename ) ) |
| 5513 |
{ |
| 5514 |
report( "check_lock_status: device is locked by another application\n" ); |
| 5515 |
return 1; |
| 5516 |
} |
| 5517 |
return 0; |
| 5518 |
|
| 5519 |
} |
| 5520 |
|
| 5521 |
|
| 5522 |
|
| 5523 |
|
| 5524 |
|
| 5525 |
|
| 5526 |
|
| 5527 |
|
| 5528 |
|
| 5529 |
|
| 5530 |
|
| 5531 |
|
| 5532 |
void fhs_unlock( const char *filename, int openpid ) |
| 5533 |
{ |
| 5534 |
char file[80],*p; |
| 5535 |
int i; |
| 5536 |
|
| 5537 |
i = strlen( filename ); |
| 5538 |
p = ( char * ) filename + i; |
| 5539 |
|
| 5540 |
while( *( p - 1 ) != '/' && i-- != 1 ) p--; |
| 5541 |
sprintf( file, "%s/LCK..%s", LOCKDIR, p ); |
| 5542 |
|
| 5543 |
if( !check_lock_pid( file, openpid ) ) |
| 5544 |
{ |
| 5545 |
unlink(file); |
| 5546 |
report("fhs_unlock: Removing LockFile\n"); |
| 5547 |
} |
| 5548 |
else |
| 5549 |
{ |
| 5550 |
report("fhs_unlock: Unable to remove LockFile\n"); |
| 5551 |
} |
| 5552 |
} |
| 5553 |
|
| 5554 |
|
| 5555 |
|
| 5556 |
|
| 5557 |
|
| 5558 |
|
| 5559 |
|
| 5560 |
|
| 5561 |
|
| 5562 |
|
| 5563 |
void uucp_unlock( const char *filename, int openpid ) |
| 5564 |
{ |
| 5565 |
struct stat buf; |
| 5566 |
char file[80], message[80]; |
| 5567 |
|
| 5568 |
|
| 5569 |
sprintf( message, "uucp_unlock( %s );\n", filename ); |
| 5570 |
report( message ); |
| 5571 |
|
| 5572 |
if ( stat( filename, &buf ) != 0 ) |
| 5573 |
{ |
| 5574 |
|
| 5575 |
report( "uucp_unlock() no such device\n" ); |
| 5576 |
return; |
| 5577 |
} |
| 5578 |
sprintf( file, LOCKDIR"/LK.%03d.%03d.%03d", |
| 5579 |
(int) major( buf.st_dev ), |
| 5580 |
(int) major( buf.st_rdev ), |
| 5581 |
(int) minor( buf.st_rdev ) |
| 5582 |
); |
| 5583 |
if ( stat( file, &buf ) != 0 ) |
| 5584 |
{ |
| 5585 |
|
| 5586 |
report( "uucp_unlock no such lockfile\n" ); |
| 5587 |
return; |
| 5588 |
} |
| 5589 |
if( !check_lock_pid( file, openpid ) ) |
| 5590 |
{ |
| 5591 |
sprintf( message, "uucp_unlock: unlinking %s\n", file ); |
| 5592 |
report( message ); |
| 5593 |
unlink(file); |
| 5594 |
} |
| 5595 |
else |
| 5596 |
{ |
| 5597 |
sprintf( message, "uucp_unlock: unlinking failed %s\n", file ); |
| 5598 |
report( message ); |
| 5599 |
} |
| 5600 |
} |
| 5601 |
|
| 5602 |
|
| 5603 |
|
| 5604 |
|
| 5605 |
|
| 5606 |
|
| 5607 |
|
| 5608 |
|
| 5609 |
|
| 5610 |
|
| 5611 |
int check_lock_pid( const char *file, int openpid ) |
| 5612 |
{ |
| 5613 |
int fd, lockpid; |
| 5614 |
char pid_buffer[12]; |
| 5615 |
char message[80]; |
| 5616 |
|
| 5617 |
fd=open( file, O_RDONLY ); |
| 5618 |
if ( fd < 0 ) |
| 5619 |
{ |
| 5620 |
return( 1 ); |
| 5621 |
} |
| 5622 |
if ( read( fd, pid_buffer, 11 ) < 0 ) |
| 5623 |
{ |
| 5624 |
close( fd ); |
| 5625 |
return( 1 ); |
| 5626 |
} |
| 5627 |
close( fd ); |
| 5628 |
pid_buffer[11] = '\0'; |
| 5629 |
lockpid = atol( pid_buffer ); |
| 5630 |
|
| 5631 |
if ( lockpid != getpid() && lockpid != getppid() && lockpid != openpid ) |
| 5632 |
{ |
| 5633 |
sprintf(message, "check_lock_pid: lock = %s pid = %i gpid=%i openpid=%i\n", |
| 5634 |
pid_buffer, (int) getpid(), (int) getppid(), openpid ); |
| 5635 |
report( message ); |
| 5636 |
return( 1 ); |
| 5637 |
} |
| 5638 |
return( 0 ); |
| 5639 |
} |
| 5640 |
|
| 5641 |
|
| 5642 |
|
| 5643 |
|
| 5644 |
|
| 5645 |
|
| 5646 |
|
| 5647 |
|
| 5648 |
|
| 5649 |
|
| 5650 |
|
| 5651 |
|
| 5652 |
|
| 5653 |
|
| 5654 |
|
| 5655 |
|
| 5656 |
|
| 5657 |
|
| 5658 |
|
| 5659 |
|
| 5660 |
|
| 5661 |
|
| 5662 |
|
| 5663 |
|
| 5664 |
|
| 5665 |
|
| 5666 |
|
| 5667 |
|
| 5668 |
|
| 5669 |
|
| 5670 |
|
| 5671 |
int check_group_uucp() |
| 5672 |
{ |
| 5673 |
#ifndef USER_LOCK_DIRECTORY |
| 5674 |
FILE *testLockFile ; |
| 5675 |
char testLockFileDirName[] = LOCKDIR; |
| 5676 |
char testLockFileName[] = "tmpXXXXXX"; |
| 5677 |
char *testLockAbsFileName; |
| 5678 |
|
| 5679 |
testLockAbsFileName = calloc(strlen(testLockFileDirName) |
| 5680 |
+ strlen(testLockFileName) + 2, sizeof(char)); |
| 5681 |
if ( NULL == testLockAbsFileName ) |
| 5682 |
{ |
| 5683 |
report_error("check_group_uucp(): Insufficient memory"); |
| 5684 |
return 1; |
| 5685 |
} |
| 5686 |
strcat(testLockAbsFileName, testLockFileDirName); |
| 5687 |
strcat(testLockAbsFileName, "/"); |
| 5688 |
strcat(testLockAbsFileName, testLockFileName); |
| 5689 |
if ( NULL == mktemp(testLockAbsFileName) ) |
| 5690 |
{ |
| 5691 |
free(testLockAbsFileName); |
| 5692 |
report_error("check_group_uucp(): mktemp malformed string - \ |
| 5693 |
should not happen"); |
| 5694 |
|
| 5695 |
return 1; |
| 5696 |
} |
| 5697 |
testLockFile = fopen (testLockAbsFileName, "w+"); |
| 5698 |
if (NULL == testLockFile) |
| 5699 |
{ |
| 5700 |
report_error("check_group_uucp(): error testing lock file " |
| 5701 |
"creation Error details:"); |
| 5702 |
report_error(strerror(errno)); |
| 5703 |
free(testLockAbsFileName); |
| 5704 |
return 1; |
| 5705 |
} |
| 5706 |
|
| 5707 |
fclose (testLockFile); |
| 5708 |
unlink (testLockAbsFileName); |
| 5709 |
free(testLockAbsFileName); |
| 5710 |
|
| 5711 |
#endif |
| 5712 |
return 0; |
| 5713 |
|
| 5714 |
#ifdef USE_OLD_CHECK_GROUP_UUCP |
| 5715 |
int check_group_uucp() |
| 5716 |
{ |
| 5717 |
#ifndef USER_LOCK_DIRECTORY |
| 5718 |
int group_count; |
| 5719 |
struct passwd *user = getpwuid( geteuid() ); |
| 5720 |
struct stat buf; |
| 5721 |
char msg[80]; |
| 5722 |
gid_t list[ NGROUPS_MAX ]; |
| 5723 |
|
| 5724 |
if( stat( LOCKDIR, &buf) ) |
| 5725 |
{ |
| 5726 |
sprintf( msg, "check_group_uucp: Can not find Lock Directory: %s\n", LOCKDIR ); |
| 5727 |
report_error( msg ); |
| 5728 |
return( 1 ); |
| 5729 |
} |
| 5730 |
group_count = getgroups( NGROUPS_MAX, list ); |
| 5731 |
list[ group_count ] = geteuid(); |
| 5732 |
|
| 5733 |
|
| 5734 |
if( user == NULL ) |
| 5735 |
{ |
| 5736 |
report_error( "Not able to get user groups.\n" ); |
| 5737 |
return 1; |
| 5738 |
} else |
| 5739 |
|
| 5740 |
|
| 5741 |
|
| 5742 |
if( user->pw_gid ) |
| 5743 |
{ |
| 5744 |
while( group_count >= 0 && buf.st_gid != list[ group_count ] ) |
| 5745 |
{ |
| 5746 |
group_count--; |
| 5747 |
} |
| 5748 |
if( buf.st_gid == list[ group_count ] ) |
| 5749 |
return 0; |
| 5750 |
sprintf( msg, "%i %i\n", buf.st_gid, list[ group_count ] ); |
| 5751 |
report_error( msg ); |
| 5752 |
report_error( UUCP_ERROR ); |
| 5753 |
return 1; |
| 5754 |
} |
| 5755 |
return 0; |
| 5756 |
|
| 5757 |
|
| 5758 |
|
| 5759 |
|
| 5760 |
|
| 5761 |
|
| 5762 |
|
| 5763 |
|
| 5764 |
|
| 5765 |
|
| 5766 |
|
| 5767 |
|
| 5768 |
|
| 5769 |
|
| 5770 |
|
| 5771 |
|
| 5772 |
|
| 5773 |
|
| 5774 |
#endif |
| 5775 |
return 0; |
| 5776 |
#endif |
| 5777 |
} |
| 5778 |
|
| 5779 |
|
| 5780 |
|
| 5781 |
|
| 5782 |
|
| 5783 |
|
| 5784 |
|
| 5785 |
|
| 5786 |
|
| 5787 |
|
| 5788 |
|
| 5789 |
|
| 5790 |
|
| 5791 |
|
| 5792 |
|
| 5793 |
|
| 5794 |
|
| 5795 |
|
| 5796 |
|
| 5797 |
|
| 5798 |
|
| 5799 |
|
| 5800 |
|
| 5801 |
|
| 5802 |
|
| 5803 |
|
| 5804 |
|
| 5805 |
|
| 5806 |
|
| 5807 |
|
| 5808 |
|
| 5809 |
|
| 5810 |
|
| 5811 |
|
| 5812 |
|
| 5813 |
|
| 5814 |
|
| 5815 |
int is_device_locked( const char *port_filename ) |
| 5816 |
{ |
| 5817 |
const char *lockdirs[] = { "/etc/locks", "/usr/spool/kermit", |
| 5818 |
"/usr/spool/locks", "/usr/spool/uucp", "/usr/spool/uucp/", |
| 5819 |
"/usr/spool/uucp/LCK", "/var/lock", "/var/lock/modem", |
| 5820 |
"/var/spool/lock", "/var/spool/locks", "/var/spool/uucp", |
| 5821 |
LOCKDIR, NULL |
| 5822 |
}; |
| 5823 |
const char *lockprefixes[] = { "LCK..", "lk..", "LK.", NULL }; |
| 5824 |
char *p, *file, pid_buffer[20], *message; |
| 5825 |
int i = 0, j, k, fd , pid; |
| 5826 |
struct stat buf, buf2, lockbuf; |
| 5827 |
|
| 5828 |
j = strlen( port_filename ); |
| 5829 |
p = ( char * ) port_filename+j; |
| 5830 |
while( *( p-1 ) != '/' && j-- !=1 ) p--; |
| 5831 |
|
| •5832 |
stat(LOCKDIR, &lockbuf); |
| 5833 |
|
| 5834 |
while( lockdirs[i] ) |
| 5835 |
{ |
| 5836 |
|
| 5837 |
|
| 5838 |
|
| 5839 |
|
| 5840 |
|
| 5841 |
|
| 5842 |
|
| 5843 |
|
| 5844 |
if( !stat( lockdirs[i], &buf2 ) && |
| 5845 |
buf2.st_ino != lockbuf.st_ino && |
| 5846 |
strncmp( lockdirs[i], LOCKDIR, strlen( lockdirs[i] ) ) ) |
| 5847 |
{ |
| 5848 |
j = strlen( port_filename ); |
| 5849 |
p = ( char * ) port_filename + j; |
| 5850 |
|
| 5851 |
|
| 5852 |
|
| 5853 |
|
| 5854 |
while( *( p - 1 ) != '/' && j-- != 1 ) |
| 5855 |
{ |
| 5856 |
#if defined ( __unixware__ ) |
| 5857 |
*p = tolower( *p ); |
| 5858 |
#endif |
| 5859 |
p--; |
| 5860 |
} |
| 5861 |
k=0; |
| 5862 |
while ( lockprefixes[k] ) |
| 5863 |
{ |
| 5864 |
|
| 5865 |
asprintf( &file, "%s/%s%s", lockdirs[i], |
| 5866 |
lockprefixes[k], p ); |
| 5867 |
if( stat( file, &buf ) == 0 ) |
| 5868 |
{ |
| 5869 |
asprintf( &message, UNEXPECTED_LOCK_FILE, |
| 5870 |
file ); |
| 5871 |
report_warning( message ); |
| 5872 |
free( message ); |
| •5873 |
free( file ); |
| 5874 |
return 1; |
| 5875 |
} |
| 5876 |
free( file ); |
| •5877 |
|
| 5878 |
|
| 5879 |
stat(port_filename , &buf ); |
| •5880 |
asprintf( &file, "%s/%s%03d.%03d.%03d", |
| 5881 |
lockdirs[i], |
| 5882 |
lockprefixes[k], |
| 5883 |
(int) major( buf.st_dev ), |
| •5884 |
(int) major( buf.st_rdev ), |
| 5885 |
(int) minor( buf.st_rdev ) |
| 5886 |
); |
| 5887 |
if( stat( file, &buf ) == 0 ) |
| •5888 |
{ |
| 5889 |
asprintf( &message, UNEXPECTED_LOCK_FILE, |
| 5890 |
file ); |
| 5891 |
report_warning( message ); |
| 5892 |
free( message ); |
| 5893 |
free( file ); |
| 5894 |
return 1; |
| 5895 |
} |
| 5896 |
free( file ); |
| •5897 |
k++; |
| 5898 |
} |
| 5899 |
} |
| •5900 |
i++; |
| 5901 |
} |
| 5902 |
|
| 5903 |
|
| •5904 |
|
| 5905 |
|
| 5906 |
|
| 5907 |
|
| 5908 |
|
| 5909 |
#ifdef FHS |
| 5910 |
|
| 5911 |
i = strlen( port_filename ); |
| 5912 |
p = ( char * ) port_filename + i; |
| 5913 |
while( *(p-1) != '/' && i-- != 1) |
| 5914 |
{ |
| 5915 |
#if defined ( __unixware__ ) |
| 5916 |
*p = tolower( *p ); |
| 5917 |
#endif |
| 5918 |
p--; |
| 5919 |
} |
| 5920 |
asprintf( &file, "%s/%s%s", LOCKDIR, LOCKFILEPREFIX, p ); |
| 5921 |
#else |
| 5922 |
|
| 5923 |
if ( stat( port_filename, &buf ) != 0 ) |
| 5924 |
{ |
| 5925 |
report( "RXTX is_device_locked() could not find device.\n" ); |
| 5926 |
return 1; |
| 5927 |
} |
| •5928 |
asprintf( &file, "%s/LK.%03d.%03d.%03d", |
| 5929 |
LOCKDIR, |
| 5930 |
(int) major( buf.st_dev ), |
| 5931 |
(int) major( buf.st_rdev ), |
| 5932 |
(int) minor( buf.st_rdev ) |
| 5933 |
); |
| 5934 |
|
| 5935 |
#endif |
| •5936 |
|
| 5937 |
if( stat( file, &buf ) == 0 ) |
| 5938 |
{ |
| 5939 |
|
| 5940 |
|
| 5941 |
fd=open( file, O_RDONLY ); |
| 5942 |
read( fd, pid_buffer, 11 ); |
| 5943 |
|
| 5944 |
close( fd ); |
| 5945 |
sscanf( pid_buffer, "%d", &pid ); |
| 5946 |
|
| 5947 |
if( kill( (pid_t) pid, 0 ) && errno==ESRCH ) |
| 5948 |
{ |
| 5949 |
asprintf( &message, |
| 5950 |
"RXTX Warning: Removing stale lock file. %s\n", |
| 5951 |
file ); |
| 5952 |
report_warning( message ); |
| 5953 |
free( message ); |
| 5954 |
if( unlink( file ) != 0 ) |
| 5955 |
{ |
| 5956 |
asprintf( &message, "RXTX Error: Unable to \ |
| •5957 |
remove stale lock file: %s\n", |
| 5958 |
file |
| 5959 |
); |
| 5960 |
report_warning( message ); |
| •5961 |
free( message ); |
| 5962 |
free( file ); |
| 5963 |
return 1; |
| •5964 |
} |
| 5965 |
} |
| 5966 |
} |
| 5967 |
free(file); |
| 5968 |
return 0; |
| •5969 |
} |
| 5970 |
#endif |
| 5971 |
|
| 5972 |
|
| 5973 |
|
| 5974 |
|
| •5975 |
|
| 5976 |
|
| 5977 |
|
| 5978 |
|
| 5979 |
|
| 5980 |
|
| 5981 |
int system_does_not_lock( const char * filename, int pid ) |
| 5982 |
{ |
| 5983 |
return 0; |
| 5984 |
} |
| 5985 |
|
| 5986 |
|
| 5987 |
|
| 5988 |
|
| 5989 |
|
| 5990 |
|
| 5991 |
|
| 5992 |
|
| 5993 |
|
| 5994 |
|
| 5995 |
void system_does_not_unlock( const char * filename, int openpid ) |
| 5996 |
{ |
| 5997 |
return; |
| 5998 |
} |
| 5999 |
|
| 6000 |
|
| 6001 |
|
| 6002 |
|
| 6003 |
|
| 6004 |
|
| 6005 |
|
| 6006 |
|
| 6007 |
|
| 6008 |
|
| 6009 |
|
| 6010 |
void dump_termios(char *foo,struct termios *ttyset) |
| 6011 |
{ |
| 6012 |
#ifdef DEBUG |
| 6013 |
int i; |
| 6014 |
|
| 6015 |
fprintf(stderr, "%s c_iflag=%#x\n", foo, ttyset->c_iflag); |
| 6016 |
fprintf(stderr, "%s c_lflag=%#x\n", foo, ttyset->c_lflag); |
| 6017 |
fprintf(stderr, "%s c_oflag=%#x\n", foo, ttyset->c_oflag); |
| 6018 |
fprintf(stderr, "%s c_cflag=%#x\n", foo, ttyset->c_cflag); |
| 6019 |
fprintf(stderr, "%s c_cc[]: ", foo); |
| 6020 |
for(i=0; i<NCCS; i++) |
| 6021 |
{ |
| 6022 |
fprintf(stderr,"%d=%x ", i, ttyset->c_cc[i]); |
| 6023 |
} |
| 6024 |
fprintf(stderr,"\n" ); |
| 6025 |
#endif |
| 6026 |
} |
| 6027 |
|
| 6028 |
|
| 6029 |
|
| 6030 |
|
| 6031 |
|
| 6032 |
|
| 6033 |
|
| 6034 |
|
| 6035 |
|
| 6036 |
|
| 6037 |
|
| 6038 |
JNIEnv *get_java_environment(JavaVM *java_vm, jboolean *was_attached){ |
| 6039 |
void **env = NULL; |
| 6040 |
jint err_get_env; |
| 6041 |
if(java_vm == NULL) return (JNIEnv *) *env; |
| 6042 |
*was_attached = JNI_FALSE; |
| 6043 |
|
| 6044 |
err_get_env = (*java_vm)->GetEnv( |
| 6045 |
java_vm, |
| 6046 |
env, |
| 6047 |
JNI_VERSION_1_2 |
| 6048 |
); |
| 6049 |
if(err_get_env == JNI_ERR) return NULL; |
| 6050 |
if(err_get_env == JNI_EDETACHED){ |
| 6051 |
(*java_vm)->AttachCurrentThread( |
| 6052 |
java_vm, |
| 6053 |
env, |
| 6054 |
(void **) NULL |
| 6055 |
); |
| 6056 |
if(*env != NULL) *was_attached = JNI_TRUE; |
| 6057 |
}else if(err_get_env != JNI_OK) return (JNIEnv *) NULL; |
| 6058 |
return (JNIEnv *) *env; |
| 6059 |
} |
| 6060 |
|
| 6061 |
|
| 6062 |
|
| 6063 |
|
| 6064 |
|
| 6065 |
|
| 6066 |
|
| 6067 |
|
| 6068 |
|
| 6069 |
|
| 6070 |
|
| 6071 |
|
| 6072 |
|
| 6073 |
|
| 6074 |
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *java_vm, void *reserved) |
| 6075 |
{ |
| 6076 |
javaVM = java_vm; |
| 6077 |
report_verbose("JNI_OnLoad called.\n"); |
| 6078 |
return JNI_VERSION_1_2; |
| 6079 |
} |
| 6080 |
|
| 6081 |
|
| 6082 |
|
| 6083 |
|
| 6084 |
|
| 6085 |
|
| 6086 |
|
| 6087 |
|
| 6088 |
|
| 6089 |
|
| 6090 |
|
| 6091 |
|
| 6092 |
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) |
| 6093 |
{ |
| 6094 |
|
| 6095 |
printf("Experimental: JNI_OnUnload called.\n"); |
| 6096 |
} |
| 6097 |
|
| 6098 |
#ifdef asdf |
| 6099 |
|
| 6100 |
|
| 6101 |
|
| 6102 |
|
| 6103 |
|
| 6104 |
|
| 6105 |
|
| 6106 |
|
| 6107 |
int printj(JNIEnv *env, wchar_t *fmt, ...) |
| 6108 |
{ |
| 6109 |
wchar_t buf[1024]; |
| 6110 |
int retval; |
| 6111 |
jstring jsBuf; |
| 6112 |
jclass clsSystem, clsOut; |
| 6113 |
jfieldID jfid; |
| 6114 |
jobject objOut; |
| 6115 |
jmethodID midPrint; |
| 6116 |
|
| 6117 |
va_list ap; |
| 6118 |
va_start(ap, fmt); |
| 6119 |
retval = _vsnwprintf(buf, 1024, fmt, ap); |
| 6120 |
va_end(ap); |
| 6121 |
buf[1023] = '\0'; |
| 6122 |
|
| 6123 |
if((clsSystem = env->FindClass("java/lang/System")) == NULL) |
| 6124 |
{ |
| 6125 |
IF_DEBUG |
| 6126 |
( |
| 6127 |
env->ExceptionDescribe(); |
| 6128 |
) |
| 6129 |
env->ExceptionClear(); |
| 6130 |
return -1; |
| 6131 |
} |
| 6132 |
|
| 6133 |
if( ( jfid = env->GetStaticFieldID(clsSystem, |
| 6134 |
"out", "Ljava/io/PrintStream;" ) ) == NULL ) |
| 6135 |
{ |
| 6136 |
IF_DEBUG |
| 6137 |
( |
| 6138 |
env->ExceptionDescribe(); |
| 6139 |
) |
| 6140 |
env->ExceptionClear(); |
| 6141 |
env->DeleteLocalRef(clsSystem); |
| 6142 |
return -1; |
| 6143 |
} |
| 6144 |
|
| 6145 |
objOut = env->GetStaticObjectField(clsSystem, jfid); |
| 6146 |
clsOut = env->GetObjectClass(objOut); |
| 6147 |
|
| 6148 |
if( ( midPrint = env->GetMethodID(clsOut, "print", |
| 6149 |
"(Ljava/lang/String;)V" ) ) == NULL ) |
| 6150 |
{ |
| 6151 |
IF_DEBUG |
| 6152 |
( |
| 6153 |
env->ExceptionDescribe(); |
| 6154 |
) |
| 6155 |
env->ExceptionClear(); |
| 6156 |
env->DeleteLocalRef(clsOut); |
| 6157 |
env->DeleteLocalRef(clsSystem); |
| 6158 |
return -1; |
| 6159 |
} |
| 6160 |
|
| 6161 |
jsBuf = env->NewString(buf, wcslen(buf)); |
| 6162 |
|
| 6163 |
env->CallVoidMethod(objOut, midPrint, jsBuf); |
| 6164 |
|
| 6165 |
env->DeleteLocalRef(jsBuf); |
| 6166 |
env->DeleteLocalRef(clsOut); |
| 6167 |
env->DeleteLocalRef(clsSystem); |
| 6168 |
|
| 6169 |
return retval; |
| 6170 |
} |
| 6171 |
|
| 6172 |
|
| 6173 |
|
| 6174 |
|
| 6175 |
|
| 6176 |
#endif |
| 6177 |
|