Debian Codegraph @

src/RawImp.c

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