Debian Codegraph @

src/SerialImp.cpp

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