Debian Codegraph @

src/SerialImp.cpp

raw symbols ◀ ▶ SCIP
3 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 #include "config.h"
59 #include "gnu_io_RXTXPort.h"
60 #include <time.h>
61 #include <unistd.h>
62 #include <stdio.h>
63 //#include <OS.h> // !!!
64 //#include <iostream.h> // !!!
65 #include <SerialPort.h> // !!!
66 //#include <Joystick.h>
67 //#include <Application.h> // !!!
68 #include <string.h>
69 #include <stdlib.h>
70 #include <errno.h>
71 #include <limits.h>
72 #ifndef WIN32
73 #include <sys/ioctl.h>
74 #include <sys/param.h>
75 #include <sys/utsname.h>
76 #ifdef HAVE_TERMIOS_H
77 # include <termios.h>
78 #endif
79 #ifdef HAVE_SYS_SIGNAL_H
80 # include <sys/signal.h>
81 #endif
82 #else
83 # include <win32termios.h>
84 #endif
85 #include <sys/types.h>
86 #include <sys/stat.h>
87 #include <sys/time.h>
88 #ifdef HAVE_SYS_FCNTL_H
89 # include <sys/fcntl.h>
90 #endif
91 #ifdef HAVE_SYS_FILE_H
92 # include <sys/file.h>
93 #endif
94
95 extern int errno;
96 #include "SerialImp.h"
97 /* #define DEBUG */
98
99 /* this is so diff will not generate noise when merging 1.4 and 1.5 changes
100 * It will eventually be removed.
101 * */
102 #define RXTXPort(foo) Java_gnu_io_RXTXPort_ ## foo
103 #define RXTXCommDriver(foo) Java_gnu_io_RXTXCommDriver_ ## foo
104 //#define MAX_PORT_NUM 65;
105 BSerialPort *PortArray[65]; // !!!
106 BSerialPort *port; // !!!
107 FILE *fp; // !!!
108 /*----------------------------------------------------------
109 RXTXPort.Initialize
110
111 accept: none
112 perform: Initialize the native library
113 return: none
114 exceptions: none
115 ----------------------------------------------------------*/
116 JNIEXPORT void JNICALL RXTXPort(Initialize)(
117 JNIEnv *env,
118 jclass jclazz
119 )
120 {
121 ////////////////// Open Log File /////////////////////
122 fp = fopen("RXTXOut.log", "a");
123 fprintf(fp, "Brian is inside RXTXPort.Initialize\n"); // !!!
124 fclose(fp);
125 ////////////////// Close Log File ////////////////////
126
127 #ifndef WIN32
128 #ifndef __BEOS__
129 #ifdef DEBUG
130 struct utsname name;
131 #endif
132 /* This bit of code checks to see if there is a signal handler installed
133 for SIGIO, and installs SIG_IGN if there is not. This is necessary
134 for the native threads jdk, but we don't want to do it with green
135 threads, because it slows things down. Go figure. */
136
137 /* POSIX signal handling functions */
•138 #if !defined(__FreeBSD_kernel___)
139 struct sigaction handler;
140 sigaction( SIGIO, NULL, &handler );
141 if( !handler.sa_handler ) signal( SIGIO, SIG_IGN );
•142 #endif /* !__FreeBSD_kernel__ */
143 #ifdef DEBUG
144 /* Lets let people who upgraded kernels know they may have problems */
•145 /*
146 if (uname (&name) == -1)
147 {
148 report("RXTX WARNING: cannot get system name\n");
149 return;
150 }
151 if(!strcmp(name.release,UTS_RELEASE))
152 {
153 fprintf(stderr, LINUX_KERNEL_VERSION_ERROR, UTS_RELEASE,
154 name.release);
155 getchar();
156 }
•157 */
158 #endif /* DEBUG */
159 #endif /* __BEOS__ */
160 #endif /* WIN32 */
161 }
162
163
164 /*----------------------------------------------------------
165 RXTXPort.open
166
167 accept: The device to open. ie "/dev/ttyS0"
168 perform: open the device, set the termios struct to sane settings and
169 return the filedescriptor
170 return: fd
171 exceptions: IOException
172 comments: Very often people complain about not being able to get past
173 this function and it turns out to be permissions on the
174 device file or bios has the device disabled.
175 ----------------------------------------------------------*/
176 JNIEXPORT jint JNICALL RXTXPort(open)(
177 JNIEnv *env,
178 jobject jobj,
179 jstring jstr
180 )
181 {
182 int i = 1;
183 int fd = -1;
184 for (i = 1; i < 65; i++) // we start at 1 because fd = 0 is invalid to RXTXPort.java
185 {
186 if (!PortArray[i]) // Here, we are finding the first non-null index into PortArray.
187 {
188 fd = i; // fd is no longer a "file descriptor" but an index
189 // into an array of pointers to BSerialPort Objects.
190 break; // got a good one.
191 }
192 }
193 if (fd < 1) // if no good fd found, throw exception.
194 {
195 throw_java_exception( env, PORT_IN_USE_EXCEPTION, "open",
196 strerror( errno ) );
197 return -1;
198 }
199
200 // retrieve the serial port filename, strip off extra goo to leave just serial1, serial2, etc.
201 const char *filename = env->GetStringUTFChars(jstr, 0); // retrieve filename from JAVAland.
202 char devName[B_OS_NAME_LENGTH];
203 const char *newDevName = strstr(filename, "serial");
204 strncpy(&devName[0], &newDevName[0], (strlen(newDevName) + 1));
205
206 // Instantiate the BSerialPort associated with devName (serial1, serial2, etc).
207 PortArray[fd] = new BSerialPort();
208 // Actually open serial port here:
209 //do
210 //{
211 status_t results = PortArray[fd]->Open(&devName[0]);
212 //} while (errno==EINTR);
213 if (results < 1) // throw exception if can't open port.
214 {
215 throw_java_exception( env, PORT_IN_USE_EXCEPTION, "open",
216 strerror( errno ) );
217 return -1;
218 }
219
220 // Set initial port parameters
221 PortArray[fd]->SetFlowControl(0);
222 PortArray[fd]->SetBlocking(false);
223 if (PortArray[fd]->SetDataRate(B_9600_BPS) != B_OK)
224 {
225 throw_java_exception( env, PORT_IN_USE_EXCEPTION, "open",
226 strerror( errno ) );
227 return -1;
228 }
229
230 env->ReleaseStringUTFChars(jstr, NULL); // releasing filename for JAVAland.
231
232
233 // Open Debug Error log file
234 fp = fopen("RXTXOut.log", "a");
235 fprintf(fp, "inside RXTXPort.Open fd for BSerialPort is %i.\n",fd);
236 fprintf(fp, "inside RXTXPort.Open devName for BSerialPort is %s.\n", devName);
237 fprintf(fp, "inside RXTXPort.Open results of open is %i.\n", (int)results);
238 fclose(fp);
239 // Close Log File
240
241 return (jint)fd; // returning the "fd" (the index into array of BSerialPort pointers).
242 }
243
244 /*----------------------------------------------------------
245 RXTXPort.nativeClose
246
247 accept: none
248 perform: get the fd from the java end and close it
249 return: none
250 exceptions: none
251 ----------------------------------------------------------*/
252 JNIEXPORT void JNICALL RXTXPort(nativeClose)( JNIEnv *env,
253 jobject jobj )
254 {
255 int fd = get_java_var( env, jobj,"fd","I" );
256
257 ////////////////// Open Log File /////////////////////
258 fp = fopen("RXTXOut.log", "a");
259 fprintf(fp, "Brian is inside nativeClose\n"); // !!!
260 fclose(fp);
261 ////////////////// Close Log File ////////////////////
262
263 delete PortArray[fd];
264 return;
265 }
266
267 /*----------------------------------------------------------
268 RXTXPort.nativeSetSerialPortParams
269
270 accept: speed, data bits, stop bits, parity
271 perform: set the serial port parameters
272 return: void
273 exceptions: UnsupportedCommOperationException
274 ----------------------------------------------------------*/
275 JNIEXPORT void JNICALL RXTXPort(nativeSetSerialPortParams)(
276 JNIEnv *env, jobject jobj, jint speed, jint dataBits, jint stopBits,
277 jint parity )
278 {
279 int fd = get_java_var( env, jobj,"fd","I" );
280 data_rate cspeed = translate_speed( env, speed );
281 data_bits numofdatabits;
282 stop_bits numofstopbits;
283 parity_mode whichparitymode;
284
285 if( !cspeed ) return;
286 if( !translate_data_bits( env, &(numofdatabits), dataBits ) ) return;
287 if( !translate_stop_bits( env, &(numofstopbits), stopBits ) ) return;
288 if( !translate_parity( env, &(whichparitymode), parity ) ) return;
289
290 PortArray[fd]->SetDataBits(numofdatabits);
291 PortArray[fd]->SetStopBits(numofstopbits);
292 PortArray[fd]->SetParityMode(whichparitymode);
293 if (PortArray[fd]->SetDataRate(cspeed) != B_OK)
294 {
295 throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
296 "nativeSetSerialPortParams", strerror( errno ) );
297 }
298
299 ////////////////// Open Log File /////////////////////
300 fp = fopen("RXTXOut.log", "a");
301 fprintf(fp, "Brian is inside RXTXPort.nativeSetSerialPortParams\n");
302 fprintf(fp, "speed = %i\n", speed);
303 fprintf(fp, "fd = %i\n", fd);
304 fprintf(fp, "RXTXPort:nativeSetSerialPortParams right before return\n");
305 fclose(fp);
306 ////////////////// Close Log File /////////////////////
307
308 return;
309 }
310
311 /*----------------------------------------------------------
312 translate_speed
313
314 accept: speed in bits-per-second
315 perform: convert bits-per-second to a speed_t constant
316 return: speed_t constant
317 exceptions: UnsupportedCommOperationException
318 comments: Only the lowest level code should know about
319 the magic constants.
320 ----------------------------------------------------------*/
321 data_rate translate_speed( JNIEnv *env, jint speed )
322 {
323
324 ////////////////// Open Log File /////////////////////
325 fp = fopen("RXTXOut.log", "a");
326 fprintf(fp, "Brian is inside translate_speed\n");
327 fclose(fp);
328 ////////////////// Close Log File ////////////////////
329
330
331 switch( speed ) {
332 case 0: return B_0_BPS;
333 case 50: return B_50_BPS;
334 case 75: return B_75_BPS;
335 case 110: return B_110_BPS;
336 case 134: return B_134_BPS;
337 case 150: return B_150_BPS;
338 case 200: return B_200_BPS;
339 case 300: return B_300_BPS;
340 case 600: return B_600_BPS;
341 case 1200: return B_1200_BPS;
342 case 1800: return B_1800_BPS;
343 case 2400: return B_2400_BPS;
344 case 4800: return B_4800_BPS;
345 case 9600: return B_9600_BPS;
346 case 19200: return B_19200_BPS;
347 case 38400: return B_38400_BPS;
348 case 57600: return B_57600_BPS;
349 case 115200: return B_115200_BPS;
350 case 230400: return B_230400_BPS;
351 case 31250: return B_31250_BPS; // this one was in BeOS's SerialPort.h, but I am
352 // skeptical that this setting will work...
353 }
354
355 throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
356 "translate_speed", "speed" );
357 return B_0_BPS;
358 }
359
360 /*----------------------------------------------------------
361 translate_data_bits
362
363 accept: gnu.io.SerialPort.DATABITS_* constant
364 perform: convert to BeOS eqivalent bits
365 return: 1 if successful
366 0 if an exception is thrown
367 exceptions: UnsupportedCommOperationException
368 ----------------------------------------------------------*/
369 int translate_data_bits( JNIEnv *env, data_bits *dbits, jint dataBits )
370 {
371 ////////////////// Open Log File /////////////////////
372 fp = fopen("RXTXOut.log", "a");
373 fprintf(fp, "Brian is inside translate_data_bits\n"); // !!!
374 fclose(fp);
375 ////////////////// Close Log File ////////////////////
376
377 switch( dataBits )
378 {
379 case DATABITS_7:
380 (*dbits) = B_DATA_BITS_7;
381 return 1;
382 case DATABITS_8:
383 (*dbits) = B_DATA_BITS_8;
384 return 1;
385 }
386
387 throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
388 "translate_data_bits", "data bits" );
389 return 0;
390 }
391
392 /*----------------------------------------------------------
393 translate_stop_bits
394
395 accept: gnu.io.SerialPort.STOPBITS_* constant
396 perform: convert to BeOS eqivalent bits
397 return: 1 if successful
398 0 if an exception is thrown
399 exceptions: UnsupportedCommOperationException
400 comments: You now can't specify anything but 1 or 2 stop bits.
401 ----------------------------------------------------------*/
402 int translate_stop_bits( JNIEnv *env, stop_bits *sbits, jint stopBits )
403 {
404
405 ////////////////// Open Log File /////////////////////
406 fp = fopen("RXTXOut.log", "a");
407 fprintf(fp, "Brian is inside translate_stop_bits\n"); // !!!
408 fclose(fp);
409 ////////////////// Close Log File ////////////////////
410
411 switch( stopBits )
412 {
413 case STOPBITS_1:
414 (*sbits) = B_STOP_BITS_1;
415 return 1;
416 case STOPBITS_2:
417 (*sbits) = B_STOP_BITS_2;
418 return 1;
419 }
420
421 throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
422 "translate_stop_bits", "stop bits" );
423 return 0;
424 }
425
426 /*----------------------------------------------------------
427 translate_parity
428
429 accept: gnu.io.SerialPort.PARITY_* constant
430 perform: convert parity parameters to BeOS specific ones.
431 return: 1 if successful
432 0 if an exception is thrown
433 exceptions: UnsupportedCommOperationException
434 comments: The CMSPAR bit not there???
435 ----------------------------------------------------------*/
436 int translate_parity( JNIEnv *env, parity_mode *parmode, jint parity )
437 {
438 ////////////////// Open Log File /////////////////////
439 fp = fopen("RXTXOut.log", "a");
440 fprintf(fp, "Brian is inside translate_parity\n");
441 fclose(fp);
442 ////////////////// Close Log File ////////////////////
443
444 switch( parity )
445 {
446 case PARITY_NONE:
447 (*parmode) = B_NO_PARITY;
448 return 1;
449 case PARITY_EVEN:
450 (*parmode) = B_EVEN_PARITY;
451 return 1;
452 case PARITY_ODD:
453 (*parmode) = B_ODD_PARITY;
454 return 1;
455 }
456
457 throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
458 "translate_parity", "parity" );
459 return 0;
460 }
461
462
463 /*----------------------------------------------------------
464 RXTXPort.writeByte
465
466 accept: byte to write (passed as int)
467 perform: write a single byte to the port
468 return: none
469 exceptions: IOException
470 ----------------------------------------------------------*/
471 JNIEXPORT void JNICALL RXTXPort(writeByte)( JNIEnv *env,
472 jobject jobj, jint ji )
473 {
474
475 ////////////////// Open Log File /////////////////////
476 fp = fopen("RXTXOut.log", "a");
477 fprintf(fp, "Brian is inside RXTXPort.writeByte\n"); // !!!
478 fclose(fp);
479 ////////////////// Close Log File ////////////////////
480
481 unsigned char byte = (unsigned char)ji;
482 int fd = get_java_var( env, jobj,"fd","I" );
483 //fprintf(fp, "RXTXPort.writeByte fd = %i\n", fd);
484 ssize_t result;
485
486 do
487 {
488 result = PortArray[fd]->Write(&byte, sizeof(unsigned char));
489 } while ((result < (ssize_t) 0) && (errno==EINTR));
490 //fclose(fp);
491 //return;
492
493 //do {
494 // result=write (14, &byte, sizeof(unsigned char));
495 //} while (result < 0 && errno==EINTR);
496
497 if(result >= (ssize_t)0)
498 return;
499 throw_java_exception( env, IO_EXCEPTION, "writeByte",
500 strerror( errno ) );
501 }
502
503
504 /*----------------------------------------------------------
505 RXTXPort.writeArray
506
507 accept: jbarray: bytes used for writing
508 offset: offset in array to start writing
509 count: Number of bytes to write
510 perform: write length bytes of jbarray
511 return: none
512 exceptions: IOException
513 ----------------------------------------------------------*/
514 JNIEXPORT void JNICALL RXTXPort(writeArray)( JNIEnv *env,
515 jobject jobj, jbyteArray jbarray, jint offset, jint count )
516 {
517
518 ////////////////// Open Log File /////////////////////
519 fp = fopen("RXTXOut.log", "a");
520 fprintf(fp, "Brian is inside RXTXPort.writeArray\n"); // !!!
521 fclose(fp);
522 ////////////////// Close Log File ////////////////////
523
524
525 int fd = get_java_var( env, jobj,"fd","I" );
526 int total=0,i;
527 size_t result = 0;
528
529 unsigned char *bytes = (unsigned char *)malloc( count );
530
531 jbyte *body = env->GetByteArrayElements(jbarray, 0);
532 for( i = 0; i < count; i++ ) bytes[ i ] = body[ i + offset ];
533 env->ReleaseByteArrayElements(jbarray, body, 0);
534 do {
535 //result=write (fd, bytes + total, count - total);
536 result = PortArray[fd]->Write(bytes + total, count - total);
537 if(result >0){
538 total += result;
539 }
540 } while ((total<count)||(result < 0 && errno==EINTR));
541 free( bytes );
542 if( result < 0 ) throw_java_exception( env, IO_EXCEPTION,
543 "writeArray", strerror( errno ) );
544 }
545
546
547 /*----------------------------------------------------------
548 RXTXPort.drain
549
550 accept: none
551 perform: wait until all data is transmitted
552 return: none
553 exceptions: IOException
554 comments: java.io.OutputStream.flush() is equivalent to tcdrain,
555 not tcflush, which throws away unsent bytes
556
557 count logic added to avoid infinite loops when EINTR is
558 true... Thread.yeild() was suggested.
559 ----------------------------------------------------------*/
560 JNIEXPORT void JNICALL RXTXPort(drain)( JNIEnv *env,
561 jobject jobj )
562 {
563
564 ////////////////// Open Log File /////////////////////
565 fp = fopen("RXTXOut.log", "a");
566 fprintf(fp, "Brian is inside RXTXPort.drain\n"); // !!!
567 fclose(fp);
568 ////////////////// Close Log File ////////////////////
569
570
571 int fd = get_java_var( env, jobj,"fd","I" );
572 int count=0;
573
574 do
575 {
576 PortArray[fd]->ClearOutput();
577 count++;
578 } while (errno==EINTR && count <5);
579
580 //if( result ) throw_java_exception( env, IO_EXCEPTION, "drain",
581 // strerror( errno ) );
582 }
583
584 /*----------------------------------------------------------
585 RXTXPort.sendBreak
586
587 accept: duration in milliseconds.
588 perform: send break for actual time. not less than 0.25 seconds.
589 exceptions: none
590 comments: not very precise
591 ----------------------------------------------------------*/
592 JNIEXPORT void JNICALL RXTXPort(sendBreak)( JNIEnv *env,
593 jobject jobj, jint duration )
594 {
595
596 ////////////////// Open Log File /////////////////////
597 fp = fopen("RXTXOut.log", "a");
598 fprintf(fp, "Brian is inside RXTXPort.sendBreak\n"); // !!!
599 fclose(fp);
600 ////////////////// Close Log File ////////////////////
601
602
603 int fd = get_java_var( env, jobj,"fd","I" );
604 tcsendbreak( fd, (int)( duration / 250 ) );
605 }
606
607
608 /*----------------------------------------------------------
609 RXTXPort.NativegetReceiveTimeout
610
611 accept: none
612 perform: get termios.c_cc[VTIME]
613 return: VTIME
614 comments: see NativeEnableReceiveTimeoutThreshold
615 ----------------------------------------------------------*/
616 JNIEXPORT jint JNICALL RXTXPort(NativegetReceiveTimeout)(
617 JNIEnv *env,
618 jobject jobj
619 )
620 {
621
622 ////////////////// Open Log File /////////////////////
623 fp = fopen("RXTXOut.log", "a");
624 fprintf(fp, "Brian is inside RXTXPort.NativeReceiveTimeout\n"); // !!!
625 fclose(fp);
626 ////////////////// Close Log File ////////////////////
627
628
629 int fd = get_java_var( env, jobj,"fd","I" );
630 struct termios ttyset;
631
632 if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
633 return(ttyset.c_cc[ VTIME ] * 100);
634 fail:
635 throw_java_exception( env, IO_EXCEPTION, "getReceiveTimeout",
636 strerror( errno ) );
637 return -1;
638 }
639
640 /*----------------------------------------------------------
641 RXTXPort.NativeisReceiveTimeoutEnabled
642
643 accept: none
644 perform: determine if VTIME is none 0
645 return: JNI_TRUE if VTIME > 0 else JNI_FALSE
646 comments: see NativeEnableReceiveTimeoutThreshold
647 ----------------------------------------------------------*/
648 JNIEXPORT jboolean JNICALL RXTXPort(NativeisReceiveTimeoutEnabled)(
649 JNIEnv *env,
650 jobject jobj
651 )
652 {
653
654 ////////////////// Open Log File /////////////////////
655 fp = fopen("RXTXOut.log", "a");
656 fprintf(fp, "Brian is inside RXTXPort.NativeisReceiveTimeoutEnabled\n"); // !!!
657 fclose(fp);
658 ////////////////// Close Log File ////////////////////
659
660
661 int fd = get_java_var( env, jobj,"fd","I" );
662 struct termios ttyset;
663
664 if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
665 return(ttyset.c_cc[ VTIME ] > 0 ? JNI_TRUE:JNI_FALSE);
666 fail:
667 throw_java_exception( env, IO_EXCEPTION, "isReceiveTimeoutEnabled",
668 strerror( errno ) );
669 return JNI_FALSE;
670 }
671
672
673 /*----------------------------------------------------------
674 RXTXPort.isDSR
675
676 accept: none
677 perform: check status of DSR
678 return: true if TIOCM_DSR is set
679 false if TIOCM_DSR is not set
680 exceptions: none
681 comments: DSR stands for Data Set Ready
682 ----------------------------------------------------------*/
683 JNIEXPORT jboolean JNICALL RXTXPort(isDSR)( JNIEnv *env,
684 jobject jobj )
685 {
686
687 ////////////////// Open Log File /////////////////////
688 fp = fopen("RXTXOut.log", "a");
689 fprintf(fp, "Brian is inside RXTXPort.isDSR\n"); // !!!
690 fclose(fp);
691 ////////////////// Close Log File ////////////////////
692
693
694 int fd = get_java_var( env, jobj,"fd","I" );
695
696 if (PortArray[fd]->IsDSR())
697 {
698 return JNI_TRUE;
699 }
700 else
701 {
702 return JNI_FALSE;
703 }
704 }
705
706 /*----------------------------------------------------------
707 RXTXPort.isCD
708
709 accept: none
710 perform: check status of CD
711 return: true if TIOCM_CD is set
712 false if TIOCM_CD is not set
713 exceptions: none
714 comments: CD stands for Carrier Detect
715 The following comment has been made...
716 "well, it works, there might ofcourse be a bug, but making DCD
717 permanently on fixed it for me so I don't care"
718
719 ----------------------------------------------------------*/
720 JNIEXPORT jboolean JNICALL RXTXPort(isCD)( JNIEnv *env,
721 jobject jobj )
722 {
723
724 ////////////////// Open Log File /////////////////////
725 fp = fopen("RXTXOut.log", "a");
726 fprintf(fp, "Brian is inside RXTXPort.isCD\n"); // !!!
727 fclose(fp);
728 ////////////////// Close Log File ////////////////////
729
730
731 int fd = get_java_var( env, jobj,"fd","I" );
732
733 if (PortArray[fd]->IsDCD())
734 {
735 return JNI_TRUE;
736 }
737 else
738 {
739 return JNI_FALSE;
740 }
741 }
742
743 /*----------------------------------------------------------
744 RXTXPort.isCTS
745
746 accept: none
747 perform: check status of CTS
748 return: true if TIOCM_CTS is set
749 false if TIOCM_CTS is not set
750 exceptions: none
751 comments: CTS stands for Clear To Send.
752 ----------------------------------------------------------*/
753 JNIEXPORT jboolean JNICALL RXTXPort(isCTS)( JNIEnv *env,
754 jobject jobj )
755 {
756
757 ////////////////// Open Log File /////////////////////
758 fp = fopen("RXTXOut.log", "a");
759 fprintf(fp, "Brian is inside RXTXPort.isCTS\n"); // !!!
760 fclose(fp);
761 ////////////////// Close Log File ////////////////////
762
763
764 int fd = get_java_var( env, jobj,"fd","I" );
765
766 if (PortArray[fd]->IsCTS())
767 {
768 return JNI_TRUE;
769 }
770 else
771 {
772 return JNI_FALSE;
773 }
774 }
775
776 /*----------------------------------------------------------
777 RXTXPort.isRI
778
779 accept: none
780 perform: check status of RI
781 return: true if TIOCM_RI is set
782 false if TIOCM_RI is not set
783 exceptions: none
784 comments: RI stands for Ring Indicator
785 ----------------------------------------------------------*/
786 JNIEXPORT jboolean JNICALL RXTXPort(isRI)( JNIEnv *env,
787 jobject jobj )
788 {
789
790 ////////////////// Open Log File /////////////////////
791 fp = fopen("RXTXOut.log", "a");
792 fprintf(fp, "Brian is inside RXTXPort.isRI\n"); // !!!
793 fclose(fp);
794 ////////////////// Close Log File ////////////////////
795
796
797 int fd = get_java_var( env, jobj,"fd","I" );
798
799 if (PortArray[fd]->IsRI())
800 {
801 return JNI_TRUE;
802 }
803 else
804 {
805 return JNI_FALSE;
806 }
807 }
808
809 #ifndef __BEOS__
810 /*----------------------------------------------------------
811 RXTXPort.isRTS
812
813 accept: none
814 perform: check status of RTS
815 return: true if TIOCM_RTS is set
816 false if TIOCM_RTS is not set
817 exceptions: none
818 comments: tcgetattr with c_cflag CRTS_IFLOW
819 ----------------------------------------------------------*/
820 JNIEXPORT jboolean JNICALL RXTXPort(isRTS)( JNIEnv *env,
821 jobject jobj )
822 {
823
824 ////////////////// Open Log File /////////////////////
825 fp = fopen("RXTXOut.log", "a");
826 fprintf(fp, "Brian is inside RXTXPort.isRTS\n"); // !!!
827 fclose(fp);
828 ////////////////// Close Log File ////////////////////
829
830
831 int fd = get_java_var( env, jobj,"fd","I" );
832
833 if (PortArray[fd]->IsRTS())
834 {
835 return JNI_TRUE;
836 }
837 else
838 {
839 return JNI_FALSE;
840 }
841 }
842 #endif /* __BEOS__ */
843
844 /*----------------------------------------------------------
845 RXTXPort.setRTS
846
847 accept: state flag to set/unset.
848 perform: depends on the state flag
849 if true TIOCM_RTS is set
850 if false TIOCM_RTS is unset
851 return: none
852 exceptions: none
853 comments: tcsetattr with c_cflag CRTS_IFLOW
854 ----------------------------------------------------------*/
855 JNIEXPORT void JNICALL RXTXPort(setRTS)( JNIEnv *env,
856 jobject jobj, jboolean state )
857 {
858
859 ////////////////// Open Log File /////////////////////
860 fp = fopen("RXTXOut.log", "a");
861 fprintf(fp, "Brian is inside RXTXPort.setRTS\n"); // !!!
862 fclose(fp);
863 ////////////////// Close Log File ////////////////////
864
865
866 int fd = get_java_var( env, jobj,"fd","I" );
867
868 if (state == JNI_TRUE)
869 {
870 PortArray[fd]->SetRTS(true);
871 }
872 else
873 {
874 PortArray[fd]->SetRTS(false);
875 }
876 return;
877 }
878
879 #ifndef __BEOS__
880 /*----------------------------------------------------------
881 RXTXPort.setDSR
882
883 accept: state flag to set/unset.
884 perform: depends on the state flag
885 if true TIOCM_DSR is set
886 if false TIOCM_DSR is unset
887 return: none
888 exceptions: none
889 comments: tcsetattr with c_cflag CRTS_IFLOW
890 ----------------------------------------------------------*/
891 JNIEXPORT void JNICALL RXTXPort(setDSR)( JNIEnv *env,
892 jobject jobj, jboolean state )
893 {
894
895 ////////////////// Open Log File /////////////////////
896 fp = fopen("RXTXOut.log", "a");
897 fprintf(fp, "Brian is inside RXTXPort.setDSR\n"); // !!!
898 fclose(fp);
899 ////////////////// Close Log File ////////////////////
900
901
902 int fd = get_java_var( env, jobj,"fd","I" );
903
904 if (state == JNI_TRUE)
905 {
906 PortArray[fd]->SetDSR(true);
907 }
908 else
909 {
910 PortArray[fd]->SetDSR(false);
911 }
912 return;
913 }
914
915 /*----------------------------------------------------------
916 RXTXPort.isDTR
917
918 accept: none
919 perform: check status of DTR
920 return: true if TIOCM_DTR is set
921 false if TIOCM_DTR is not set
922 exceptions: none
923 comments: DTR stands for Data Terminal Ready
924 ----------------------------------------------------------*/
925 JNIEXPORT jboolean JNICALL RXTXPort(isDTR)( JNIEnv *env,
926 jobject jobj )
927 {
928
929 ////////////////// Open Log File /////////////////////
930 fp = fopen("RXTXOut.log", "a");
931 fprintf(fp, "Brian is inside RXTXPort.isDTR\n"); // !!!
932 fclose(fp);
933 ////////////////// Close Log File ////////////////////
934
935
936 int fd = get_java_var( env, jobj,"fd","I" );
937
938 if (PortArray[fd]->IsDTR())
939 {
940 return JNI_TRUE;
941 }
942 else
943 {
944 return JNI_FALSE;
945 }
946 }
947 #endif /* __BEOS__ */
948
949 /*----------------------------------------------------------
950 RXTXPort.setDTR
951
952 accept: new DTR state
953 perform: if state is true, TIOCM_DTR is set
954 if state is false, TIOCM_DTR is unset
955 return: none
956 exceptions: none
957 comments: DTR stands for Data Terminal Ready
958 ----------------------------------------------------------*/
959 JNIEXPORT void JNICALL RXTXPort(setDTR)( JNIEnv *env,
960 jobject jobj, jboolean state )
961 {
962
963 ////////////////// Open Log File /////////////////////
964 fp = fopen("RXTXOut.log", "a");
965 fprintf(fp, "Brian is inside RXTXPort.setDTR\n"); // !!!
966 fclose(fp);
967 ////////////////// Close Log File ////////////////////
968
969
970 int fd = get_java_var( env, jobj,"fd","I" );
971
972 if (state == JNI_TRUE)
973 {
974 PortArray[fd]->SetDTR(true);
975 }
976 else
977 {
978 PortArray[fd]->SetDTR(false);
979 }
980 return;
981 }
982
983 /*----------------------------------------------------------
984 read_byte_array
985
986 accept: int fd file descriptor to read from
987 unsigned char *buffer buffer to read data into
988 int length number of bytes to read
989 int timeout milliseconds to wait before returning
990 perform: read bytes from the port into a buffer
991 return: status of read
992 -1 fail (IOException)
993 0 timeout
994 >0 number of bytes read
995 comments: According to the Communications API spec, a receive threshold
996 of 1 is the same as having the threshold disabled.
997
998 The nuts and bolts are documented in
999 NativeEnableReceiveTimeoutThreshold()
1000 ----------------------------------------------------------*/
1001 int read_byte_array( int fd, unsigned char *buffer, int length, int timeout )
1002 {
1003
1004 ////////////////// Open Log File /////////////////////
1005 fp = fopen("RXTXOut.log", "a");
1006 fprintf(fp, "Brian is inside read_byte_array\n"); // !!!
1007 fclose(fp);
1008 ////////////////// Close Log File ////////////////////
1009
1010
1011 int ret, left, bytes = 0;
1012 struct timeval sleep;
1013 struct timeval *psleep=&sleep;
1014 #ifndef __BEOS__
1015 fd_set rfds;
1016
1017 FD_ZERO( &rfds );
1018 FD_SET( fd, &rfds );
1019 if( timeout != 0 )
1020 {
1021 sleep.tv_sec = timeout / 1000;
1022 sleep.tv_usec = 1000 * ( timeout % 1000 );
1023 }
1024 #endif __BEOS__
1025 left = length;
1026 while( bytes < length )
1027 {
1028 #ifndef __BEOS__
1029 /* FIXME: In Linux, select updates the timeout automatically, so
1030 other OSes will need to update it manually if they want to have
1031 the same behavior. For those OSes, timeouts will occur after no
1032 data AT ALL is received for the timeout duration. No big deal. */
1033 do {
1034 if( timeout == 0 ) psleep = NULL;
1035 ret=select( fd + 1, &rfds, NULL, NULL, psleep );
1036 } while (ret < 0 && errno==EINTR);
1037 if( ret == 0 ) break;
1038 if( ret < 0 ) return -1;
1039 #endif __BEOS__
1040 ret = read( fd, buffer + bytes, left );
1041 if( ret == 0 ) break;
1042 if( ret < 0 ) return -1;
1043 bytes += ret;
1044 left -= ret;
1045 }
1046 return bytes;
1047 }
1048
1049 /*----------------------------------------------------------
1050 NativeEnableReceiveTimeoutThreshold
1051 accept: int threshold, int vtime,int buffer
1052 perform: Set c_cc->VMIN to threshold and c_cc=>VTIME to vtime
1053 return: void
1054 exceptions: IOException
1055 comments: This is actually all handled in read with select in
1056 canonical input mode.
1057 ----------------------------------------------------------*/
1058 JNIEXPORT void JNICALL RXTXPort(NativeEnableReceiveTimeoutThreshold)(
1059 JNIEnv *env, jobject jobj, jint vtime, jint threshold, jint buffer)
1060 {
1061
1062 ////////////////// Open Log File /////////////////////
1063 fp = fopen("RXTXOut.log", "a");
1064 fprintf(fp, "Brian is inside NativeEnableReceiveTimeoutThreshold\n"); // !!!
1065 fclose(fp);
1066 ////////////////// Close Log File ////////////////////
1067
1068
1069 int fd = get_java_var( env, jobj,"fd","I" );
1070 struct termios ttyset;
1071
1072 if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
1073 ttyset.c_cc[ VMIN ] = threshold;
1074 ttyset.c_cc[ VTIME ] = vtime/100;
1075 if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail;
1076
1077 return;
1078 fail:
1079 throw_java_exception( env, IO_EXCEPTION, "TimeoutThreshold",
1080 strerror( errno ) );
1081 return;
1082 }
1083
1084 /*----------------------------------------------------------
1085 RXTXPort.readByte
1086
1087 accept: none
1088 perform: Read a single byte from the port
1089 return: The byte read
1090 exceptions: IOException
1091 ----------------------------------------------------------*/
1092 JNIEXPORT jint JNICALL RXTXPort(readByte)( JNIEnv *env,
1093 jobject jobj )
1094 {
1095
1096 ////////////////// Open Log File /////////////////////
1097 fp = fopen("RXTXOut.log", "a");
1098 fprintf(fp, "Brian is inside RXTXPort.readByte\n"); // !!!
1099 fclose(fp);
1100 ////////////////// Close Log File ////////////////////
1101
1102 //status_t SetTimeout(bigtime_t timeout)
1103 //int bytes;
1104 unsigned char buffer[ 1 ];
1105 int fd = get_java_var( env, jobj,"fd","I" );
1106 int timeout = get_java_var( env, jobj, "timeout", "I" );
1107
1108 if (PortArray[fd]->SetTimeout((bigtime_t) timeout) != B_OK)
1109 {
1110 throw_java_exception( env, IO_EXCEPTION, "readByte",
1111 strerror( errno ) );
1112 return -1;
1113 }
1114 if (PortArray[fd]->Read(&buffer[0], sizeof(unsigned char)) != 1)
1115 {
1116 throw_java_exception( env, IO_EXCEPTION, "readByte",
1117 strerror( errno ) );
1118 return -1;
1119 }
1120
1121 return (jint)buffer[0];
1122 /*
1123 bytes = read_byte_array( fd, buffer, 1, timeout );
1124 if( bytes < 0 ) {
1125 throw_java_exception( env, IO_EXCEPTION, "readByte",
1126 strerror( errno ) );
1127 return -1;
1128 }
1129 return (bytes ? (jint)buffer[ 0 ] : -1);
1130 */
1131 }
1132
1133 /*----------------------------------------------------------
1134 RXTXPort.readArray
1135
1136 accept: offset (offset to start storing data in the jbarray) and
1137 Length (bytes to read)
1138 perform: read bytes from the port into a byte array
1139 return: bytes read on success
1140 0 on read timeout
1141 exceptions: IOException
1142 comments: throws ArrayIndexOutOfBoundsException if asked to
1143 read more than SSIZE_MAX bytes
1144 ----------------------------------------------------------*/
1145 JNIEXPORT jint JNICALL RXTXPort(readArray)( JNIEnv *env,
1146 jobject jobj, jbyteArray jbarray, jint offset, jint length )
1147 {
1148
1149 ////////////////// Open Log File /////////////////////
1150 fp = fopen("RXTXOut.log", "a");
1151 fprintf(fp, "Brian is inside RXTXPort.readArray\n"); // !!!
1152 fclose(fp);
1153 ////////////////// Close Log File ////////////////////
1154
1155
1156 int bytes;
1157 jbyte *body;
1158 unsigned char *buffer;
1159 int fd = get_java_var( env, jobj, "fd", "I" );
1160 int timeout = get_java_var( env, jobj, "timeout", "I" );
1161
1162 if( length > SSIZE_MAX || length < 0 ) {
1163 throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS,
1164 "readArray", "Invalid length" );
1165 return -1;
1166 }
1167
1168 buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length );
1169 if( buffer == 0 ) {
1170 throw_java_exception( env, OUT_OF_MEMORY, "readArray",
1171 "Unable to allocate buffer" );
1172 return -1;
1173 }
1174
1175 bytes = read_byte_array( fd, buffer, length, timeout );
1176 if( bytes < 0 ) {
1177 free( buffer );
1178 throw_java_exception( env, IO_EXCEPTION, "readArray",
1179 strerror( errno ) );
1180 return -1;
1181 }
1182 //body = (*env)->GetByteArrayElements( env, jbarray, 0 );
1183 body = env->GetByteArrayElements( jbarray, 0 ); // !!!
1184 memcpy(body + offset, buffer, bytes);
1185 //(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 );
1186 env->ReleaseByteArrayElements( jbarray, body, 0 ); // !!!
1187 free( buffer );
1188 return (bytes ? bytes : -1);
1189 }
1190
1191 /*----------------------------------------------------------
1192 RXTXPort.nativeavailable
1193
1194 accept: none
1195 perform: find out the number of bytes available for reading
1196 return: available bytes
1197 -1 on error
1198 exceptions: none
1199 ----------------------------------------------------------*/
1200 JNIEXPORT jint JNICALL RXTXPort(nativeavailable)( JNIEnv *env,
1201 jobject jobj )
1202 {
1203
1204 ////////////////// Open Log File /////////////////////
1205 fp = fopen("RXTXOut.log", "a");
1206 fprintf(fp, "Brian is inside RXTXPort.nativeavailable\n"); // !!!
1207 fclose(fp);
1208 ////////////////// Close Log File ////////////////////
1209
1210
1211 //#ifndef __BEOS__
1212 int fd = get_java_var( env, jobj,"fd","I" );
1213 //int result;
1214 ssize_t result = 0;
1215 result = PortArray[fd]->WaitForInput(); // WaitForInput() not the best choice, but all I could find.
1216 return (jint)result;
1217 /*
1218 if( ioctl( fd, FIONREAD, &result ) )
1219 {
1220 throw_java_exception( env, IO_EXCEPTION, "nativeavailable",
1221 strerror( errno ) );
1222 return -1;
1223 }
1224 else return (jint)result;
1225 */
1226 //#endif
1227 //return (-1);
1228 }
1229
1230 /*----------------------------------------------------------
1231 RXTXPort.setflowcontrol
1232
1233 accept: flowmode
1234 FLOWCONTROL_NONE none
1235 FLOWCONTROL_RTSCTS_IN hardware flow control
1236 FLOWCONTROL_RTSCTS_OUT ""
1237 FLOWCONTROL_XONXOFF_IN input software flow control
1238 FLOWCONTROL_XONXOFF_OUT output software flow control
1239 perform: set flow control to flowmode
1240 return: none
1241 exceptions: IOException
1242 comments: there is no differentiation between input and output hardware
1243 flow control
1244 ----------------------------------------------------------*/
1245 JNIEXPORT void JNICALL RXTXPort(setflowcontrol)( JNIEnv *env,
1246 jobject jobj, jint flowmode )
1247 {
1248
1249 ////////////////// Open Log File /////////////////////
1250 fp = fopen("RXTXOut.log", "a");
1251 fprintf(fp, "Brian is inside RXTXPort.setflowcontrol\n"); // !!!
1252 fclose(fp);
1253 ////////////////// Close Log File ////////////////////
1254
1255 int fd = get_java_var( env, jobj,"fd","I" );
1256 switch( flowmode )
1257 {
1258 case FLOWCONTROL_NONE:
1259 PortArray[fd]->SetFlowControl(B_NOFLOW_CONTROL);
1260 if (PortArray[fd]->FlowControl() != B_NOFLOW_CONTROL)
1261 {
1262 throw_java_exception( env, IO_EXCEPTION, "setHWFC",
1263 strerror( errno ) );
1264 return;
1265 }
1266 break;
1267 case FLOWCONTROL_RTSCTS_IN:
1268 PortArray[fd]->SetFlowControl(B_HARDWARE_CONTROL);
1269 if (PortArray[fd]->FlowControl() != B_HARDWARE_CONTROL)
1270 {
1271 throw_java_exception( env, IO_EXCEPTION, "setHWFC",
1272 strerror( errno ) );
1273 return;
1274 }
1275 break;
1276 case FLOWCONTROL_RTSCTS_OUT:
1277 PortArray[fd]->SetFlowControl(B_HARDWARE_CONTROL);
1278 if (PortArray[fd]->FlowControl() != B_HARDWARE_CONTROL)
1279 {
1280 throw_java_exception( env, IO_EXCEPTION, "setHWFC",
1281 strerror( errno ) );
1282 return;
1283 }
1284 break;
1285 case FLOWCONTROL_XONXOFF_IN:
1286 PortArray[fd]->SetFlowControl(B_SOFTWARE_CONTROL);
1287 if (PortArray[fd]->FlowControl() != B_SOFTWARE_CONTROL)
1288 {
1289 throw_java_exception( env, IO_EXCEPTION, "setHWFC",
1290 strerror( errno ) );
1291 return;
1292 }
1293 break;
1294 case FLOWCONTROL_XONXOFF_OUT:
1295 PortArray[fd]->SetFlowControl(B_SOFTWARE_CONTROL);
1296 if (PortArray[fd]->FlowControl() != B_SOFTWARE_CONTROL)
1297 {
1298 throw_java_exception( env, IO_EXCEPTION, "setHWFC",
1299 strerror( errno ) );
1300 return;
1301 }
1302 break;
1303 }
1304 return;
1305
1306
1307
1308 /*
1309 if( tcgetattr( fd, &ttyset ) ) goto fail;
1310
1311 if ( flowmode & ( FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT ) )
1312 ttyset.c_cflag |= HARDWARE_FLOW_CONTROL;
1313 else ttyset.c_cflag &= ~HARDWARE_FLOW_CONTROL;
1314
1315 ttyset.c_iflag &= ~IXANY;
1316
1317 if ( flowmode & FLOWCONTROL_XONXOFF_IN )
1318 ttyset.c_iflag |= IXOFF;
1319 else ttyset.c_iflag &= ~IXOFF;
1320
1321 if ( flowmode & FLOWCONTROL_XONXOFF_OUT )
1322 ttyset.c_iflag |= IXON;
1323 else ttyset.c_iflag &= ~IXON;
1324
1325 if( tcsetattr( fd, TCSANOW, &ttyset ) ) goto fail;
1326 return;
1327 fail:
1328 throw_java_exception( env, IO_EXCEPTION, "setHWFC",
1329 strerror( errno ) );
1330 return;
1331 */
1332 }
1333
1334 #ifdef __BEOS__
1335 /*----------------------------------------------------------
1336 RXTXPort.eventLoop
1337
1338 accept: none
1339 perform: periodically check for SerialPortEvents
1340 return: none
1341 exceptions: none
1342 comments:
1343 ----------------------------------------------------------*/
1344 JNIEXPORT void JNICALL RXTXPort(eventLoop)( JNIEnv *env, jobject jobj )
1345 {
1346 printf("BeOS eventLoop not Implemented\n");
1347 #else /* __BEOS__ */
1348
1349 ////////////////// Open Log File /////////////////////
1350 fp = fopen("RXTXOut.log", "a");
1351 fprintf(fp, "Brian is inside RXTXPort.eventLoop\n"); // !!!
1352 fclose(fp);
1353 ////////////////// Close Log File ////////////////////
1354
1355
1356 int fd, ret, change;
1357 fd_set rfds;
1358 struct timeval tv_sleep;
1359 unsigned int mflags, omflags;
1360 jboolean interrupted = 0;
1361 #if defined TIOCSERGETLSR
1362 struct stat fstatbuf;
1363 #endif /* TIOCSERGETLSR */
1364
1365 #if defined(TIOCGICOUNT)
1366 struct serial_icounter_struct sis, osis;
1367 /* JK00: flag if this can be used on this port */
1368 int has_tiocgicount = 1;
1369 #endif /* TIOCGICOUNT */
1370
1371 #if defined(TIOCSERGETLSR)
1372 int has_tiocsergetlsr = 1;
1373 #endif /* TIOCSERGETLSR */
1374
1375 fd = get_java_var(env, jobj, "fd", "I");
1376
1377 #if defined(TIOCGICOUNT)
1378 /* Some multiport serial cards do not implement TIOCGICOUNT ... */
1379 /* So use the 'dumb' mode to enable using them after all! JK00 */
1380 if( ioctl( fd, TIOCGICOUNT, &osis ) < 0 ) {
1381 report("Port does not support TIOCGICOUNT events\n" );
1382 has_tiocgicount = 0;
1383 }
1384 #endif /* TIOCGICOUNT */
1385
1386 #if defined(TIOCSERGETLSR)
1387 /* JK00: work around for multiport cards without TIOCSERGETLSR */
1388 /* Cyclades is one of those :-( */
1389 if( ioctl( fd, TIOCSERGETLSR, &change ) ) {
1390 report("Port does not support TIOCSERGETLSR\n" );
1391 has_tiocsergetlsr = 0;
1392 }
1393 #endif /* TIOCSERGETLSR */
1394
1395 if( ioctl( fd, TIOCMGET, &omflags) <0 ) {
1396 report("Port does not support events\n" );
1397 return;
1398 }
1399
1400 FD_ZERO( &rfds );
1401 while( !interrupted ) {
1402 FD_SET( fd, &rfds );
1403 tv_sleep.tv_sec = 0;
1404 tv_sleep.tv_usec = 100000;
1405 do {
1406 ret=select( fd + 1, &rfds, NULL, NULL, &tv_sleep );
1407 } while (ret < 0 && errno==EINTR);
1408 if( ret < 0 ) break;
1409
1410 interrupted = is_interrupted(env, jobj);
1411 if(interrupted) return;
1412
1413 #if defined TIOCSERGETLSR
1414 /* JK00: work around for Multi IO cards without TIOCSERGETLSR */
1415 if( has_tiocsergetlsr ) {
1416 if (fstat(fd, &fstatbuf)) break;
1417 if( ioctl( fd, TIOCSERGETLSR, &change ) ) break;
1418 else if( change )
1419 send_event( env, jobj, SPE_OUTPUT_BUFFER_EMPTY,
1420 1 );
1421 }
1422 #endif /* TIOCSERGETLSR */
1423 #if defined(TIOCGICOUNT)
1424 /* wait for RNG, DSR, CD or CTS but not DataAvailable
1425 * The drawback here is it never times out so if someone
1426 * reads there will be no chance to try again.
1427 * This may make sense if the program does not want to
1428 * be notified of data available or errors.
1429 * ret=ioctl(fd,TIOCMIWAIT);
1430 */
1431 /* JK00: only use it if supported by this port */
1432 if (has_tiocgicount) {
1433 if( ioctl( fd, TIOCGICOUNT, &sis ) ) break;
1434 while( sis.frame != osis.frame ) {
1435 send_event( env, jobj, SPE_FE, 1);
1436 osis.frame++;
1437 }
1438 while( sis.overrun != osis.overrun ) {
1439 send_event( env, jobj, SPE_OE, 1);
1440 osis.overrun++;
1441 }
1442 while( sis.parity != osis.parity ) {
1443 send_event( env, jobj, SPE_PE, 1);
1444 osis.parity++;
1445 }
1446 while( sis.brk != osis.brk ) {
1447 send_event( env, jobj, SPE_BI, 1);
1448 osis.brk++;
1449 }
1450 osis = sis;
1451 }
1452 #endif /* TIOCGICOUNT */
1453 /* A Portable implementation */
1454
1455 if( ioctl( fd, TIOCMGET, &mflags ) ) break;
1456
1457 change = (mflags&TIOCM_CTS) - (omflags&TIOCM_CTS);
1458 if( change ) send_event( env, jobj, SPE_CTS, change );
1459
1460 change = (mflags&TIOCM_DSR) - (omflags&TIOCM_DSR);
1461 if( change ) send_event( env, jobj, SPE_DSR, change );
1462
1463 change = (mflags&TIOCM_RNG) - (omflags&TIOCM_RNG);
1464 if( change ) send_event( env, jobj, SPE_RI, change );
1465
1466 change = (mflags&TIOCM_CD) - (omflags&TIOCM_CD);
1467 if( change ) send_event( env, jobj, SPE_CD, change );
1468
1469 omflags = mflags;
1470
1471 ioctl( fd, FIONREAD, &change );
1472 if( change ) {
1473 if(!send_event( env, jobj, SPE_DATA_AVAILABLE, 1 ))
1474 usleep(100000); /* select wont block */
1475 }
1476 }
1477 return;
1478 #endif /* __BEOS__ */
1479 }
1480
1481 /*----------------------------------------------------------
1482 isDeviceGood
1483
1484 accept: a port name
1485 perform: see if the port is valid on this OS.
1486 return: JNI_TRUE if it exhists otherwise JNI_FALSE
1487 exceptions: none
1488 comments:
1489 ----------------------------------------------------------*/
1490 JNIEXPORT jboolean JNICALL RXTXCommDriver(isDeviceGood)(JNIEnv *env,
1491 jobject jobj, jstring tty_name)
1492 {
1493
1494 ////////////////// Open Log File /////////////////////
1495 fp = fopen("RXTXOut.log", "a");
1496 fprintf(fp, "Brian is inside isDeviceGood\n"); // !!!
1497 fclose(fp);
1498 ////////////////// Close Log File ////////////////////
1499
1500
1501 jboolean result;
1502 static struct stat mystat;
1503 char teststring[256];
1504 int fd, i;
1505 const char *name = env->GetStringUTFChars(tty_name, 0); // !!!
1506 printf("inside RXTXCommDriver:isDeviceGood() deviceName is %s. ",name);
1507 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1508 //Let's try to enumerate the ports
1509 /*
1510 int32 portcount = 0;
1511 int32 n = 0;
1512 port = new BSerialPort();
1513 char devName[B_OS_NAME_LENGTH];
1514 portcount = port->CountDevices();
1515 for (n = portcount - 1; n >= 0; n--)
1516 {
1517 port->GetDeviceName(n, devName);
1518 printf("inside RXTXCommDriver:isDeviceGood() deviceName is %s. ",devName);
1519 }
1520 */
1521 for(i=0;i<64;i++){
1522 #if defined(_GNU_SOURCE)
1523 snprintf(teststring, 256, "%s%s%i",DEVICEDIR,name, i);
1524 #else
1525 sprintf(teststring,"%s%s%i",DEVICEDIR,name, i);
1526 #endif /* _GNU_SOURCE */
1527 stat(teststring,&mystat);
1528 if(S_ISCHR(mystat.st_mode)){
1529 printf("inside RXTXCommDriver:isDeviceGood() trying to open %s. ",teststring);
1530 fd=open(teststring,O_RDONLY|O_NONBLOCK);
1531 printf("got %i\n",fd);
1532 if (fd>0){
1533 printf("inside RXTXCommDriver:isDeviceGood() %s is valid on this system.\n",teststring);
1534 close(fd);
1535 result=JNI_TRUE;
1536 break;
1537 }
1538 }
1539 result=JNI_FALSE;
1540 }
1541 #if defined(_GNU_SOURCE)
1542 snprintf(teststring, 256, "%s%s",DEVICEDIR,name);
1543 #else
1544 sprintf(teststring,"%s%s",DEVICEDIR,name);
1545 #endif /* _GNU_SOURCE */
1546 stat(teststring,&mystat);
1547 if(S_ISCHR(mystat.st_mode)){
1548 fd=open(teststring,O_RDONLY|O_NONBLOCK);
1549 if (fd>0){
1550 close(fd);
1551 result=JNI_TRUE;
1552 }
1553 }
1554 printf("result is %i for %s\n",result, name);
1555 env->ReleaseStringUTFChars(tty_name, name); // !!!
1556 return(result);
1557 }
1558
1559 /*----------------------------------------------------------
1560 getDeviceDirectory
1561
1562 accept:
1563 perform:
1564 return: the directory containing the device files
1565 exceptions:
1566 comments: use this to avoid hard coded "/dev/"
1567 values are in SerialImp.h
1568 ----------------------------------------------------------*/
1569 /*
1570 JNIEXPORT jstring JNICALL Java_gnu_io_RXTXCommDriver_getDeviceDirectory(JNIEnv*, jobject);
1571
1572 */
1573 JNIEXPORT jstring JNICALL RXTXCommDriver(getDeviceDirectory)(JNIEnv *env,
1574 jobject jobj, int test)
1575 {
1576 return env->NewStringUTF(DEVICEDIR);
1577 }
1578 /*----------------------------------------------------------
1579 setInputBufferSize
1580
1581 accept:
1582 perform:
1583 return: none
1584 exceptions: none
1585 comments: see fopen/fclose/fwrite/fread man pages.
1586 ----------------------------------------------------------*/
1587 JNIEXPORT void JNICALL RXTXPort(setInputBufferSize)(JNIEnv *env,
1588 jobject jobj, jint size )
1589 {
1590 report("setInputBufferSize is not implemented\n");
1591 }
1592 /*----------------------------------------------------------
1593 getIputBufferSize
1594
1595 accept:
1596 perform:
1597 return: none
1598 exceptions: none
1599 comments: see fopen/fclose/fwrite/fread man pages.
1600 ----------------------------------------------------------*/
1601 JNIEXPORT jint JNICALL RXTXPort(getInputBufferSize)(JNIEnv *env,
1602 jobject jobj)
1603 {
1604 report("getInputBufferSize is not implemented\n");
1605 return(1);
1606 }
1607 /*----------------------------------------------------------
1608 setOutputBufferSize
1609
1610 accept:
1611 perform:
1612 return: none
1613 exceptions: none
1614 comments: see fopen/fclose/fwrite/fread man pages.
1615 ----------------------------------------------------------*/
1616 JNIEXPORT void JNICALL RXTXPort(setOutputBufferSize)(JNIEnv *env,
1617 jobject jobj, jint size )
1618 {
1619 report("setOutputBufferSize is not implemented\n");
1620 }
1621 /*----------------------------------------------------------
1622 getOutputBufferSize
1623
1624 accept:
1625 perform:
1626 return: none
1627 exceptions: none
1628 comments: see fopen/fclose/fwrite/fread man pages.
1629 ----------------------------------------------------------*/
1630 JNIEXPORT jint JNICALL RXTXPort(getOutputBufferSize)(JNIEnv *env,
1631 jobject jobj)
1632 {
1633 report("getOutputBufferSize is not implemented\n");
1634 return(1);
1635 }
1636
1637 /*----------------------------------------------------------
1638 is_interrupted
1639
1640 accept:
1641 perform: see if the port is being closed.
1642 return: a positive value if the port is being closed.
1643 exceptions: none
1644 comments:
1645 ----------------------------------------------------------*/
1646 jboolean is_interrupted(JNIEnv *env, jobject jobj)
1647 {
1648
1649 ////////////////// Open Log File /////////////////////
1650 fp = fopen("RXTXOut.log", "a");
1651 fprintf(fp, "Brian is is_interrupted\n"); // !!!
1652 fclose(fp);
1653 ////////////////// Close Log File ////////////////////
1654
1655
1656 jmethodID foo;
1657 jclass jclazz;
1658 int result;
1659
1660 //(*env)->ExceptionClear(env);
1661 env->ExceptionClear(); // !!!
1662
1663 //jclazz = (*env)->GetObjectClass( env, jobj );
1664 jclazz = env->GetObjectClass( jobj ); // !!!
1665 if(jclazz == NULL) return JNI_TRUE;
1666
1667 //foo = (*env)->GetMethodID( env, jclazz, "checkMonitorThread", "()Z");
1668 foo = env->GetMethodID( jclazz, "checkMonitorThread", "()Z"); // !!!
1669 if(foo == NULL) return JNI_TRUE;
1670
1671 //result = (*env)->CallBooleanMethod( env, jobj, foo );
1672 result = env->CallBooleanMethod( jobj, foo ); // !!!
1673
1674 #ifdef DEBUG
1675 //if((*env)->ExceptionOccurred(env)) {
1676 if(env->ExceptionOccurred()) { // !!!
1677 report ("an error occured calling sendEvent()\n");
1678 //(*env)->ExceptionDescribe(env);
1679 env->ExceptionDescribe(); // !!!
1680 //(*env)->ExceptionClear(env);
1681 env->ExceptionClear(); // !!!
1682 }
1683 #endif /* DEBUG */
1684 //(*env)->DeleteLocalRef( env, jclazz );
1685 env->DeleteLocalRef( jclazz ); // !!!
1686 return(result);
1687 }
1688
1689 /*----------------------------------------------------------
1690 send_event
1691
1692 accept: The event type and the event state
1693 perform: if state is > 0 send a JNI_TRUE event otherwise send JNI_FALSE
1694 return: a positive value if the port is being closed.
1695 exceptions: none
1696 comments:
1697 ----------------------------------------------------------*/
1698 int send_event(JNIEnv *env, jobject jobj, jint type, int flag)
1699 {
1700
1701 ////////////////// Open Log File /////////////////////
1702 fp = fopen("RXTXOut.log", "a");
1703 fprintf(fp, "Brian is inside send_event\n"); // !!!
1704 fclose(fp);
1705 ////////////////// Close Log File ////////////////////
1706
1707
1708 int result;
1709 jmethodID foo;
1710 //jclass jclazz = (*env)->GetObjectClass( env, jobj );
1711 jclass jclazz = env->GetObjectClass( jobj ); // !!!
1712
1713 if(jclazz == NULL) return JNI_TRUE;
1714 //foo = (*env)->GetMethodID( env, jclazz, "sendEvent", "(IZ)Z" );
1715 foo = env->GetMethodID( jclazz, "sendEvent", "(IZ)Z" ); // !!!
1716
1717 //(*env)->ExceptionClear(env);
1718 env->ExceptionClear(); // !!!
1719
1720 //result = (*env)->CallBooleanMethod( env, jobj, foo, type,
1721 result = env->CallBooleanMethod( jobj, foo, type, // !!!
1722 flag > 0 ? JNI_TRUE : JNI_FALSE );
1723
1724 #ifdef DEBUG
1725 //if((*env)->ExceptionOccurred(env)) {
1726 if(env->ExceptionOccurred()) { // !!!
1727 report ("an error occured calling sendEvent()\n");
1728 //(*env)->ExceptionDescribe(env);
1729 env->ExceptionDescribe(); // !!!
1730 //(*env)->ExceptionClear(env);
1731 env->ExceptionClear(); // !!!
1732 }
1733 #endif /* DEBUG */
1734 //(*env)->DeleteLocalRef( env, jclazz );
1735 env->DeleteLocalRef( jclazz ); // !!!
1736 return(result);
1737 }
1738 /*----------------------------------------------------------
1739 get_java_var
1740
1741 accept: env (keyhole to java)
1742 jobj (java RXTXPort object)
1743 return: the fd field from the java object
1744 exceptions: none
1745 comments:
1746 ----------------------------------------------------------*/
1747 int get_java_var( JNIEnv *env, jobject jobj, char *id, char *type )
1748 {
1749
1750
1751 ////////////////// Open Log File /////////////////////
1752 fp = fopen("RXTXOut.log", "a");
1753 fprintf(fp, "Brian is inside get_java_var\n"); // !!!
1754 fclose(fp);
1755 ////////////////// Close Log File ////////////////////
1756
1757 int result = 0;
1758 //jclass jclazz = (*env)->GetObjectClass( env, jobj );
1759 jclass jclazz = env->GetObjectClass( jobj ); // !!!
1760 //jfieldID jfd = (*env)->GetFieldID( env, jclazz, id, type );
1761 jfieldID jfd = env->GetFieldID( jclazz, id, type ); // !!!
1762
1763 if( !jfd ) {
1764 //(*env)->ExceptionDescribe( env );
1765 env->ExceptionDescribe(); // !!!
1766 //(*env)->ExceptionClear( env );
1767 env->ExceptionClear(); // !!!
1768 return result;
1769 }
1770 //result = (int)( (*env)->GetIntField( env, jobj, jfd ) );
1771 result = (int)( env->GetIntField( jobj, jfd ) ); // !!!
1772 /* ct7 & gel * Added DeleteLocalRef */
1773 //(*env)->DeleteLocalRef( env, jclazz );
1774 env->DeleteLocalRef( jclazz ); // !!!
1775 #ifdef DEBUG
1776 if(!strncmp("fd",id,2) && result == 0)
1777 report("invalid file descriptor\n");
1778 #endif
1779 return result;
1780 }
1781
1782 /*----------------------------------------------------------
1783 throw_java_exception
1784
1785 accept: env (keyhole to java)
1786 *exc (exception class name)
1787 *foo (function name)
1788 *msg (error message)
1789 perform: Throw a new java exception
1790 return: none
1791 exceptions: haha!
1792 comments:
1793 ----------------------------------------------------------*/
1794 void throw_java_exception( JNIEnv *env, char *exc, char *foo, char *msg )
1795 {
1796
1797 ////////////////// Open Log File /////////////////////
1798 fp = fopen("RXTXOut.log", "a");
1799 fprintf(fp, "Brian is inside throw_java_exception\n\n"); // !!!
1800 fprintf(fp, "%s in %s\n", msg, foo ); // !!!
1801 fclose(fp);
1802 ////////////////// Close Log File ////////////////////
1803
1804
1805 char buf[ 60 ];
1806 //jclass clazz = (*env)->FindClass( env, exc );
1807 jclass clazz = env->FindClass( exc ); // !!!
1808 if( !clazz ) {
1809 //(*env)->ExceptionDescribe( env );
1810 env->ExceptionDescribe(); // !!!
1811 //(*env)->ExceptionClear( env );
1812 env->ExceptionClear(); // !!!
1813 return;
1814 }
1815 #if defined(_GNU_SOURCE)
1816 snprintf( buf, 60, "%s in %s", msg, foo );
1817 #else
1818 sprintf( buf,"%s in %s", msg, foo );
1819 #endif /* _GNU_SOURCE */
1820 //(*env)->ThrowNew( env, clazz, buf );
1821 env->ThrowNew( clazz, buf ); // !!!
1822 /* ct7 * Added DeleteLocalRef */
1823 //(*env)->DeleteLocalRef( env, clazz );
1824 env->DeleteLocalRef( clazz ); // !!!
1825 }
1826
1827 /*----------------------------------------------------------
1828 report
1829
1830 accept: string to send to stderr
1831 perform: if DEBUG is defined send the string to stderr.
1832 return: none
1833 exceptions: none
1834 comments:
1835 ----------------------------------------------------------*/
1836 void report(char *msg)
1837 {
1838
1839 ////////////////// Open Log File /////////////////////
1840 fp = fopen("RXTXOut.log", "a");
1841 fprintf(fp, "Brian is inside report\n"); // !!!
1842 fclose(fp);
1843 ////////////////// Close Log File ////////////////////
1844
1845
1846 #ifdef DEBUG
•1847 fprintf(stderr, "%s", msg);
1848 #endif
1849 }
1850
1851 /*----------------------------------------------------------
1852 dump_termios
1853
1854 accept: string to indicate where this was called.
1855 termios struct
1856 perform: print the termios struct to stderr.
1857 return: none
1858 exceptions: none
1859 comments: used to debug the termios struct.
1860 ----------------------------------------------------------*/
1861 void dump_termios(char *foo,struct termios *ttyset)
1862 {
1863
1864 ////////////////// Open Log File /////////////////////
1865 fp = fopen("RXTXOut.log", "a");
1866 fprintf(fp, "Brian is inside dump_termios\n"); // !!!
1867 fclose(fp);
1868 ////////////////// Close Log File ////////////////////
1869
1870
1871 int i;
1872
1873 //fprintf(stderr,"%s %o\n",foo,ttyset->c_iflag);
1874 //fprintf(stderr,"%s %o\n",foo,ttyset->c_lflag);
1875 //fprintf(stderr,"%s %o\n",foo,ttyset->c_oflag);
1876 //fprintf(stderr,"%s %o\n",foo,ttyset->c_cflag);
1877 for(i=0;i<NCCS;i++)
1878 {
1879 fprintf(stderr,"%s %o ",foo,ttyset->c_cc[i]);
1880 }
1881 fprintf(stderr,"\n");
1882 }