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