Debian Codegraph @

src/RS485Imp.c

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