Debian Codegraph @

src/RawImp.c

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