Debian Codegraph @

src/gnu/io/RXTXPort.java

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*-------------------------------------------------------------------------
2 | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
3 | RXTX is a native interface to serial ports in java.
4 | Copyright 1997-2008 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 package gnu.io;
59 import java.io.InputStream;
60 import java.io.OutputStream;
61 import java.io.IOException;
62 import java.util.TooManyListenersException;
63 import java.lang.Math;
64
65 /**
66 * An extension of gnu.io.SerialPort
67 * @see gnu.io.SerialPort
68 */
69
70 final public class RXTXPort extends SerialPort
71 {
72 /* I had a report that some JRE's complain when MonitorThread
73 tries to access private variables
74 */
75
76 protected final static boolean debug = false;
77 protected final static boolean debug_read = false;
78 protected final static boolean debug_read_results = false;
79 protected final static boolean debug_write = false;
80 protected final static boolean debug_events = false;
81 protected final static boolean debug_verbose = false;
82
83 private static Zystem z;
84
85 static
86 {
87 try {
88 z = new Zystem();
89 } catch ( Exception e ) {
90 throw new Error(e.toString());
91 }
92
93 if(debug )
94 z.reportln( "RXTXPort {}");
95 System.loadLibrary( "rxtxSerial" );
96 Initialize();
97 }
98
99 /** Initialize the native library */
100 private native static void Initialize();
101 boolean MonitorThreadAlive=false;
102
103 /**
104 * Open the named port
105 * @param name the name of the device to open
106 * @throws PortInUseException
107 * @see gnu.io.SerialPort
108 */
109 public RXTXPort( String name ) throws PortInUseException
110 {
111 if (debug)
112 z.reportln( "RXTXPort:RXTXPort("+name+") called");
113 /*
114 commapi/javadocs/API_users_guide.html specifies that whenever
115 an application tries to open a port in use by another application
116 the PortInUseException will be thrown
117
118 I know some didnt like it this way but I'm not sure how to avoid
119 it. We will just be writing to a bogus fd if we catch the
120 exeption
121
122 Trent
123 */
124 // try {
125 fd = open( name );
126 this.name = name;
127
128 MonitorThreadLock = true;
129 monThread = new MonitorThread();
130 monThread.start();
131 waitForTheNativeCodeSilly();
132 MonitorThreadAlive=true;
133 // } catch ( PortInUseException e ){}
134 timeout = -1; /* default disabled timeout */
135 if (debug)
136 z.reportln( "RXTXPort:RXTXPort("+name+") returns with fd = " +
137 fd);
138 }
139 private native synchronized int open( String name )
140 throws PortInUseException;
141
142
143 /* dont close the file while accessing the fd */
144 int IOLocked = 0;
145 Object IOLockedMutex = new Object();
146
147 /** File descriptor */
148 private int fd = 0;
149
150 /** a pointer to the event info structure used to share information
151 between threads so write threads can send output buffer empty
152 from a pthread if need be.
153
154 long for 64 bit pointers.
155 */
156 long eis = 0;
157 /** pid for lock files */
158 int pid = 0;
159
160 /** DSR flag **/
161 static boolean dsrFlag = false;
162
163 /** Output stream */
164 private final SerialOutputStream out = new SerialOutputStream();
165 /**
166 * get the OutputStream
167 * @return OutputStream
168 */
169 public OutputStream getOutputStream()
170 {
171 if (debug)
172 z.reportln( "RXTXPort:getOutputStream() called and returning");
173 return out;
174 }
175
176 /** Input stream */
177 private final SerialInputStream in = new SerialInputStream();
178 /**
179 * get the InputStream
180 * @return InputStream
181 * @see java.io.InputStream
182 */
183 public InputStream getInputStream()
184 {
185 if (debug)
186 z.reportln( "RXTXPort:getInputStream() called and returning");
187 return in;
188 }
189
190 /**
191 * Set the SerialPort parameters
192 * 1.5 stop bits requires 5 databits
193 * @param b baudrate
194 * @param d databits
195 * @param s stopbits
196 * @param p parity
197 * @throws UnsupportedCommOperationException
198 * @see gnu.io.UnsupportedCommOperationException
199
200 * If speed is not a predifined speed it is assumed to be
201 * the actual speed desired.
202 */
203 private native int nativeGetParity( int fd );
204 private native int nativeGetFlowControlMode( int fd );
205 public synchronized void setSerialPortParams( int b, int d, int s,
206 int p )
207 throws UnsupportedCommOperationException
208 {
209 if (debug)
210 z.reportln( "RXTXPort:setSerialPortParams(" +
211 b + " " + d + " " + s + " " + p + ") called");
212 if ( nativeSetSerialPortParams( b, d, s, p ) )
213 throw new UnsupportedCommOperationException(
214 "Invalid Parameter" );
215 speed = b;
216 if( s== STOPBITS_1_5 ) dataBits = DATABITS_5;
217 else dataBits = d;
218 stopBits = s;
219 parity = p;
220 z.reportln( "RXTXPort:setSerialPortParams(" +
221 b + " " + d + " " + s + " " + p +
222 ") returning");
223 }
224
225 /**
226 * Set the native serial port parameters
227 * If speed is not a predifined speed it is assumed to be
228 * the actual speed desired.
229 */
230 private native boolean nativeSetSerialPortParams( int speed,
231 int dataBits, int stopBits, int parity )
232 throws UnsupportedCommOperationException;
233
234 /** Line speed in bits-per-second */
235 private int speed=9600;
236 /**
237 * @return int representing the baudrate
238 * This will not behave as expected with custom speeds
239 */
240 public int getBaudRate()
241 {
242 if (debug)
243 z.reportln( "RXTXPort:getBaudRate() called and returning " + speed);
244 return speed;
245 }
246
247 /** Data bits port parameter */
248 private int dataBits=DATABITS_8;
249 /**
250 * @return int representing the databits
251 */
252 public int getDataBits()
253 {
254 if (debug)
255 z.reportln( "RXTXPort:getDataBits() called and returning " + dataBits);
256 return dataBits;
257 }
258
259 /** Stop bits port parameter */
260 private int stopBits=SerialPort.STOPBITS_1;
261 /**
262 * @return int representing the stopbits
263 */
264 public int getStopBits()
265 {
266 if (debug)
267 z.reportln( "RXTXPort:getStopBits() called and returning " + stopBits);
268 return stopBits;
269 }
270
271 /** Parity port parameter */
272 private int parity= SerialPort.PARITY_NONE;
273 /**
274 * @return int representing the parity
275 */
276 public int getParity()
277 {
278 if (debug)
279 z.reportln( "RXTXPort:getParity() called and returning " + parity );
280 return parity;
281 }
282
283
284 /** Flow control */
285 private int flowmode = SerialPort.FLOWCONTROL_NONE;
286 /**
287 * @param flowcontrol FLOWCONTROL_NONE is default
288 * @see gnu.io.SerialPort#FLOWCONTROL_NONE
289 */
290 public void setFlowControlMode( int flowcontrol )
291 {
292 if (debug)
293 z.reportln( "RXTXPort:setFlowControlMode( " + flowcontrol + " ) called");
294 if(monThreadisInterrupted)
295 {
296 if( debug_events )
297 z.reportln( "RXTXPort:setFlowControlMode MonThread is Interrupeted returning" );
298 return;
299 }
300 try {
301 setflowcontrol( flowcontrol );
302 }
303 catch( IOException e )
304 {
305 e.printStackTrace();
306 return;
307 }
308 flowmode=flowcontrol;
309 if (debug)
310 z.reportln( "RXTXPort:setFlowControlMode( " + flowcontrol + " ) returning");
311 }
312 /**
313 * @return int representing the flowmode
314 */
315 public int getFlowControlMode()
316 {
317 if (debug)
318 z.reportln( "RXTXPort:getFlowControlMode() returning " + flowmode );
319 return flowmode;
320 }
321 native void setflowcontrol( int flowcontrol ) throws IOException;
322
323
324 /*
325 linux/drivers/char/n_hdlc.c? FIXME
326 [email protected]
327 */
328 /**
329 * Receive framing control
330 * @param f framming
331 * @throws UnsupportedCommOperationException
332 */
333 public void enableReceiveFraming( int f )
334 throws UnsupportedCommOperationException
335 {
336 if (debug)
337 z.reportln( "RXTXPort:enableReceiveFramming() throwing exception");
338 throw new UnsupportedCommOperationException( "Not supported" );
339 }
340 /**
341 */
342 public void disableReceiveFraming()
343 {
344 if (debug)
345 z.reportln( "RXTXPort:disableReceiveFramming() called and returning (noop)");
346 }
347 /**
348 * @return true if framing is enabled
349 */
350 public boolean isReceiveFramingEnabled()
351 {
352 if (debug)
353 z.reportln( "RXTXPort:isReceiveFrammingEnabled() called and returning " + false );
354 return false;
355 }
356 /**
357 * @return int representing the framing byte
358 */
359 public int getReceiveFramingByte()
360 {
361 if (debug)
362 z.reportln( "RXTXPort:getReceiveFrammingByte() called and returning " + 0 );
363 return 0;
364 }
365
366
367 /** Receive timeout control */
368 private int timeout;
369
370 /**
371 * @return int the timeout
372 */
373 public native int NativegetReceiveTimeout();
374 /**
375 * @return bloolean true if recieve timeout is enabled
376 */
377 private native boolean NativeisReceiveTimeoutEnabled();
378 /**
379 * @param time
380 * @param threshold
381 * @param InputBuffer
382 */
383 private native void NativeEnableReceiveTimeoutThreshold(int time,
384 int threshold,int InputBuffer);
385 /**
386 */
387 public void disableReceiveTimeout()
388 {
389 if (debug)
390 z.reportln( "RXTXPort:disableReceiveTimeout() called");
391 timeout = -1;
392 NativeEnableReceiveTimeoutThreshold( timeout , threshold, InputBuffer );
393 if (debug)
394 z.reportln( "RXTXPort:disableReceiveTimeout() returning");
395 }
396 /**
397 * @param time
398 */
399 public void enableReceiveTimeout( int time )
400 {
401 if (debug)
402 z.reportln( "RXTXPort:enableReceiveTimeout() called");
403 if( time >= 0 )
404 {
405 timeout = time;
406 NativeEnableReceiveTimeoutThreshold( time , threshold,
407 InputBuffer );
408 }
409 else
410 {
411 throw new IllegalArgumentException
412 (
413 "Unexpected negative timeout value"
414 );
415 }
416 if (debug)
417 z.reportln( "RXTXPort:enableReceiveTimeout() returning");
418 }
419 /**
420 * @return boolean true if recieve timeout is enabled
421 */
422 public boolean isReceiveTimeoutEnabled()
423 {
424 if (debug)
425 z.reportln( "RXTXPort:isReceiveTimeoutEnabled() called and returning " + NativeisReceiveTimeoutEnabled() );
426 return( NativeisReceiveTimeoutEnabled() );
427 }
428 /**
429 * @return int the timeout
430 */
431 public int getReceiveTimeout()
432 {
433 if (debug)
434 z.reportln( "RXTXPort:getReceiveTimeout() called and returning " + NativegetReceiveTimeout() );
435 return(NativegetReceiveTimeout( ));
436 }
437
438 /** Receive threshold control */
439
440 private int threshold = 0;
441
442 /**
443 * @param thresh threshold
444 */
445 public void enableReceiveThreshold( int thresh )
446 {
447 if (debug)
448 z.reportln( "RXTXPort:enableReceiveThreshold( " + thresh + " ) called");
449 if(thresh >=0)
450 {
451 threshold=thresh;
452 NativeEnableReceiveTimeoutThreshold(timeout, threshold,
453 InputBuffer);
454 }
455 else /* invalid thresh */
456 {
457 throw new IllegalArgumentException
458 (
459 "Unexpected negative threshold value"
460 );
461 }
462 if (debug)
463 z.reportln( "RXTXPort:enableReceiveThreshold( " + thresh + " ) returned");
464 }
465 /**
466 */
467 public void disableReceiveThreshold()
468 {
469 if (debug)
470 z.reportln( "RXTXPort:disableReceiveThreshold() called and returning");
471 enableReceiveThreshold(0);
472 }
473 /**
474 * @return int the recieve threshold
475 */
476 public int getReceiveThreshold()
477 {
478 if (debug)
479 z.reportln( "RXTXPort:getReceiveThreshold() called and returning " + threshold);
480 return threshold;
481 }
482 /**
483 * @return boolean true if receive threshold is enabled
484 */
485 public boolean isReceiveThresholdEnabled()
486 {
487 if (debug)
488 z.reportln( "RXTXPort:isReceiveThresholdEnable() called and returning" + (threshold > 0) );
489 return(threshold>0);
490 }
491
492 /** Input/output buffers */
493 /** FIXME I think this refers to
494 FOPEN(3)/SETBUF(3)/FREAD(3)/FCLOSE(3)
495 [email protected]
496
497 These are native stubs...
498 */
499 private int InputBuffer=0;
500 private int OutputBuffer=0;
501 /**
502 * @param size
503 */
504 public void setInputBufferSize( int size )
505 {
506 if (debug)
507 z.reportln( "RXTXPort:setInputBufferSize( " +
508 size + ") called");
509 if( size < 0 )
510 throw new IllegalArgumentException
511 (
512 "Unexpected negative buffer size value"
513 );
514 else InputBuffer=size;
515 if (debug)
516 z.reportln( "RXTXPort:setInputBufferSize( " +
517 size + ") returning");
518 }
519 /**
520 */
521 public int getInputBufferSize()
522 {
523 if (debug)
524 z.reportln( "RXTXPort:getInputBufferSize() called and returning " + InputBuffer );
525 return(InputBuffer);
526 }
527 /**
528 * @param size
529 */
530 public void setOutputBufferSize( int size )
531 {
532 if (debug)
533 z.reportln( "RXTXPort:setOutputBufferSize( " +
534 size + ") called");
535 if( size < 0 )
536 throw new IllegalArgumentException
537 (
538 "Unexpected negative buffer size value"
539 );
540 else OutputBuffer=size;
541 if (debug)
542 z.reportln( "RXTXPort:setOutputBufferSize( " +
543 size + ") returned");
544
545 }
546 /**
547 * @return in the output buffer size
548 */
549 public int getOutputBufferSize()
550 {
551 if (debug)
552 z.reportln( "RXTXPort:getOutputBufferSize() called and returning " + OutputBuffer );
553 return(OutputBuffer);
554 }
555
556 /* =================== cleaned messages to here */
557
558 /**
559 * Line status methods
560 */
561 /**
562 * @return true if DTR is set
563 */
564 public native boolean isDTR();
565 /**
566 * @param state
567 */
568 public native void setDTR( boolean state );
569 /**
570 * @param state
571 */
572 public native void setRTS( boolean state );
573 private native void setDSR( boolean state );
574 /**
575 * @return boolean true if CTS is set
576 */
577 public native boolean isCTS();
578 /**
579 * @return boolean true if DSR is set
580 */
581 public native boolean isDSR();
582 /**
583 * @return boolean true if CD is set
584 */
585 public native boolean isCD();
586 /**
587 * @return boolean true if RI is set
588 */
589 public native boolean isRI();
590 /**
591 * @return boolean true if RTS is set
592 */
593 public native boolean isRTS();
594
595
596 /**
597 * Write to the port
598 * @param duration
599 */
600 public native void sendBreak( int duration );
601 protected native void writeByte( int b, boolean i ) throws IOException;
602 protected native void writeArray( byte b[], int off, int len, boolean i )
603 throws IOException;
604 protected native boolean nativeDrain( boolean i ) throws IOException;
605
606 /** RXTXPort read methods */
607 protected native int nativeavailable() throws IOException;
608 protected native int readByte() throws IOException;
609 protected native int readArray( byte b[], int off, int len )
610 throws IOException;
611 protected native int readTerminatedArray( byte b[], int off, int len, byte t[] )
612 throws IOException;
613
614
615 /** Serial Port Event listener */
616 private SerialPortEventListener SPEventListener;
617
618 /** Thread to monitor data */
619 private MonitorThread monThread;
620
621 /** Process SerialPortEvents */
622 native void eventLoop();
623
624 /**
625 * @return boolean true if monitor thread is interrupted
626 */
627 boolean monThreadisInterrupted=true;
628 private native void interruptEventLoop( );
629 public boolean checkMonitorThread()
630 {
631 if (debug)
632 z.reportln( "RXTXPort:checkMonitorThread()");
633 if(monThread != null)
634 {
635 if ( debug )
636 z.reportln(
637 "monThreadisInterrupted = " +
638 monThreadisInterrupted );
639 return monThreadisInterrupted;
640 }
641 if ( debug )
642 z.reportln( "monThread is null " );
643 return(true);
644 }
645
646 /**
647 * @param event
648 * @param state
649 * @return boolean true if the port is closing
650 */
651 public boolean sendEvent( int event, boolean state )
652 {
653 if (debug_events)
654 z.report( "RXTXPort:sendEvent(");
655 /* Let the native side know its time to die */
656
657 if ( fd == 0 || SPEventListener == null || monThread == null)
658 {
659 return(true);
660 }
661
662 switch( event )
663 {
664 case SerialPortEvent.DATA_AVAILABLE:
665 if( debug_events )
666 z.reportln( "DATA_AVAILABLE " +
667 monThread.Data + ")" );
668 break;
669 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
670 if( debug_events )
671 z.reportln(
672 "OUTPUT_BUFFER_EMPTY " +
673 monThread.Output + ")" );
674 break;
675 case SerialPortEvent.CTS:
676 if( debug_events )
677 z.reportln( "CTS " +
678 monThread.CTS + ")" );
679 break;
680 case SerialPortEvent.DSR:
681 if( debug_events )
682 z.reportln( "DSR " +
683 monThread.Output + ")" );
684 break;
685 case SerialPortEvent.RI:
686 if( debug_events )
687 z.reportln( "RI " +
688 monThread.RI + ")" );
689 break;
690 case SerialPortEvent.CD:
691 if( debug_events )
692 z.reportln( "CD " +
693 monThread.CD + ")" );
694 break;
695 case SerialPortEvent.OE:
696 if( debug_events )
697 z.reportln( "OE " +
698 monThread.OE + ")" );
699 break;
700 case SerialPortEvent.PE:
701 if( debug_events )
702 z.reportln( "PE " +
703 monThread.PE + ")" );
704 break;
705 case SerialPortEvent.FE:
706 if( debug_events )
707 z.reportln( "FE " +
708 monThread.FE + ")" );
709 break;
710 case SerialPortEvent.BI:
711 if( debug_events )
712 z.reportln( "BI " +
713 monThread.BI + ")" );
714 break;
715 default:
716 if( debug_events )
717 z.reportln( "XXXXXXXXXXXXXX " +
718 event + ")" );
719 break;
720 }
721 if( debug_events && debug_verbose )
722 z.reportln( " checking flags " );
723
724 switch( event )
725 {
726 case SerialPortEvent.DATA_AVAILABLE:
727 if( monThread.Data ) break;
728 return(false);
729 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
730 if( monThread.Output ) break;
731 return(false);
732 case SerialPortEvent.CTS:
733 if( monThread.CTS ) break;
734 return(false);
735 case SerialPortEvent.DSR:
736 if( monThread.DSR ) break;
737 return(false);
738 case SerialPortEvent.RI:
739 if( monThread.RI ) break;
740 return(false);
741 case SerialPortEvent.CD:
742 if( monThread.CD ) break;
743 return(false);
744 case SerialPortEvent.OE:
745 if( monThread.OE ) break;
746 return(false);
747 case SerialPortEvent.PE:
748 if( monThread.PE ) break;
749 return(false);
750 case SerialPortEvent.FE:
751 if( monThread.FE ) break;
752 return(false);
753 case SerialPortEvent.BI:
754 if( monThread.BI ) break;
755 return(false);
756 default:
757 System.err.println( "unknown event: " + event);
758 return(false);
759 }
760 if( debug_events && debug_verbose )
761 z.reportln( " getting event" );
762 SerialPortEvent e = new SerialPortEvent(this, event, !state,
763 state );
764 if( debug_events && debug_verbose )
765 z.reportln( " sending event" );
766 if(monThreadisInterrupted)
767 {
768 if( debug_events )
769 z.reportln( " sendEvent return" );
770 return(true);
771 }
772 if( SPEventListener != null )
773 {
774 SPEventListener.serialEvent( e );
775 }
776
777 if( debug_events && debug_verbose )
778 z.reportln( " sendEvent return" );
779
780 if (fd == 0 || SPEventListener == null || monThread == null)
781 {
782 return(true);
783 }
784 else
785 {
786 return(false);
787 }
788 }
789
790 /**
791 * Add an event listener
792 * @param lsnr SerialPortEventListener
793 * @throws TooManyListenersException
794 */
795
796 boolean MonitorThreadLock = true;
797
798 public void addEventListener(
799 SerialPortEventListener lsnr ) throws TooManyListenersException
800 {
801 /* Don't let and notification requests happen until the
802 Eventloop is ready
803 */
804
805 if (debug)
806 z.reportln( "RXTXPort:addEventListener()");
807 if( SPEventListener != null )
808 {
809 throw new TooManyListenersException();
810 }
811 SPEventListener = lsnr;
812 if( !MonitorThreadAlive )
813 {
814 MonitorThreadLock = true;
815 monThread = new MonitorThread();
816 monThread.start();
817 waitForTheNativeCodeSilly();
818 MonitorThreadAlive=true;
819 }
820 if (debug)
821 z.reportln( "RXTXPort:Interrupt=false");
822 }
823 /**
824 * Remove the serial port event listener
825 */
826 public void removeEventListener()
827 {
828 if (debug)
829 z.reportln( "RXTXPort:removeEventListener() called");
830 waitForTheNativeCodeSilly();
831 //if( monThread != null && monThread.isAlive() )
832 if( monThreadisInterrupted == true )
833 {
834 z.reportln( " RXTXPort:removeEventListener() already interrupted");
835 monThread = null;
836 SPEventListener = null;
837 return;
838 }
839 else if( monThread != null && monThread.isAlive() )
840 {
841 if (debug)
842 z.reportln( " RXTXPort:Interrupt=true");
843 monThreadisInterrupted=true;
844 /*
845 Notify all threads in this PID that something is up
846 They will call back to see if its their thread
847 using isInterrupted().
848 */
849 if (debug)
850 z.reportln( " RXTXPort:calling interruptEventLoop");
851 interruptEventLoop( );
852
853 if (debug)
854 z.reportln( " RXTXPort:calling monThread.join()");
855 try {
856
857 // wait a reasonable moment for the death of the monitor thread
858 monThread.join(3000);
859 } catch (InterruptedException ex) {
860 // somebody called interrupt() on us (ie wants us to abort)
861 // we dont propagate InterruptedExceptions so lets re-set the flag
862 Thread.currentThread().interrupt();
863 return;
864 }
865
866 if ( debug && monThread.isAlive() )
867 {
868 z.reportln( " MonThread is still alive!");
869
870 }
871
872 }
873 monThread = null;
874 SPEventListener = null;
875 MonitorThreadLock = false;
876 MonitorThreadAlive=false;
877 monThreadisInterrupted=true;
878 z.reportln( "RXTXPort:removeEventListener() returning");
879 }
880 /**
881 * Give the native code a chance to start listening to the hardware
882 * or should we say give the native code control of the issue.
883 *
884 * This is important for applications that flicker the Monitor
885 * thread while keeping the port open.
886 * In worst case test cases this loops once or twice every time.
887 */
888
889 protected void waitForTheNativeCodeSilly()
890 {
891 while( MonitorThreadLock )
892 {
893 try {
894 Thread.sleep(5);
895 } catch( Exception e ) {}
896 }
897 }
898 /**
899 * @param enable
900 */
901 private native void nativeSetEventFlag( int fd, int event,
902 boolean flag );
903 public void notifyOnDataAvailable( boolean enable )
904 {
905 if (debug)
906 z.reportln( "RXTXPort:notifyOnDataAvailable( " +
907 enable+" )");
908
909 waitForTheNativeCodeSilly();
910
911 MonitorThreadLock = true;
912 nativeSetEventFlag( fd, SerialPortEvent.DATA_AVAILABLE,
913 enable );
914 monThread.Data = enable;
915 MonitorThreadLock = false;
916 }
917
918 /**
919 * @param enable
920 */
921 public void notifyOnOutputEmpty( boolean enable )
922 {
923 if (debug)
924 z.reportln( "RXTXPort:notifyOnOutputEmpty( " +
925 enable+" )");
926 waitForTheNativeCodeSilly();
927 MonitorThreadLock = true;
928 nativeSetEventFlag( fd, SerialPortEvent.OUTPUT_BUFFER_EMPTY,
929 enable );
930 monThread.Output = enable;
931 MonitorThreadLock = false;
932 }
933
934 /**
935 * @param enable
936 */
937 public void notifyOnCTS( boolean enable )
938 {
939 if (debug)
940 z.reportln( "RXTXPort:notifyOnCTS( " +
941 enable+" )");
942 waitForTheNativeCodeSilly();
943 MonitorThreadLock = true;
944 nativeSetEventFlag( fd, SerialPortEvent.CTS, enable );
945 monThread.CTS = enable;
946 MonitorThreadLock = false;
947 }
948 /**
949 * @param enable
950 */
951 public void notifyOnDSR( boolean enable )
952 {
953 if (debug)
954 z.reportln( "RXTXPort:notifyOnDSR( " +
955 enable+" )");
956 waitForTheNativeCodeSilly();
957 MonitorThreadLock = true;
958 nativeSetEventFlag( fd, SerialPortEvent.DSR, enable );
959 monThread.DSR = enable;
960 MonitorThreadLock = false;
961 }
962 /**
963 * @param enable
964 */
965 public void notifyOnRingIndicator( boolean enable )
966 {
967 if (debug)
968 z.reportln( "RXTXPort:notifyOnRingIndicator( " +
969 enable+" )");
970 waitForTheNativeCodeSilly();
971 MonitorThreadLock = true;
972 nativeSetEventFlag( fd, SerialPortEvent.RI, enable );
973 monThread.RI = enable;
974 MonitorThreadLock = false;
975 }
976 /**
977 * @param enable
978 */
979 public void notifyOnCarrierDetect( boolean enable )
980 {
981 if (debug)
982 z.reportln( "RXTXPort:notifyOnCarrierDetect( " +
983 enable+" )");
984 waitForTheNativeCodeSilly();
985 MonitorThreadLock = true;
986 nativeSetEventFlag( fd, SerialPortEvent.CD, enable );
987 monThread.CD = enable;
988 MonitorThreadLock = false;
989 }
990 /**
991 * @param enable
992 */
993 public void notifyOnOverrunError( boolean enable )
994 {
995 if (debug)
996 z.reportln( "RXTXPort:notifyOnOverrunError( " +
997 enable+" )");
998 waitForTheNativeCodeSilly();
999 MonitorThreadLock = true;
1000 nativeSetEventFlag( fd, SerialPortEvent.OE, enable );
1001 monThread.OE = enable;
1002 MonitorThreadLock = false;
1003 }
1004 /**
1005 * @param enable
1006 */
1007 public void notifyOnParityError( boolean enable )
1008 {
1009 if (debug)
1010 z.reportln( "RXTXPort:notifyOnParityError( " +
1011 enable+" )");
1012 waitForTheNativeCodeSilly();
1013 MonitorThreadLock = true;
1014 nativeSetEventFlag( fd, SerialPortEvent.PE, enable );
1015 monThread.PE = enable;
1016 MonitorThreadLock = false;
1017 }
1018 /**
1019 * @param enable
1020 */
1021 public void notifyOnFramingError( boolean enable )
1022 {
1023 if (debug)
1024 z.reportln( "RXTXPort:notifyOnFramingError( " +
1025 enable+" )");
1026 waitForTheNativeCodeSilly();
1027 MonitorThreadLock = true;
1028 nativeSetEventFlag( fd, SerialPortEvent.FE, enable );
1029 monThread.FE = enable;
1030 MonitorThreadLock = false;
1031 }
1032 /**
1033 * @param enable
1034 */
1035 public void notifyOnBreakInterrupt( boolean enable )
1036 {
1037 if (debug)
1038 z.reportln( "RXTXPort:notifyOnBreakInterrupt( " +
1039 enable+" )");
1040 waitForTheNativeCodeSilly();
1041 MonitorThreadLock = true;
1042 nativeSetEventFlag( fd, SerialPortEvent.BI, enable );
1043 monThread.BI = enable;
1044 MonitorThreadLock = false;
1045 }
1046
1047 /** Close the port */
1048 private native void nativeClose( String name );
1049 /**
1050 */
1051 boolean closeLock = false;
1052 public void close()
1053 {
1054 synchronized (this) {
1055 if (debug)
1056 z.reportln( "RXTXPort:close( " + this.name + " )");
1057
1058 while( IOLocked > 0 )
1059 {
1060 if( debug )
1061 z.reportln("IO is locked " + IOLocked);
1062 try {
1063 this.wait(500);
1064 } catch( InterruptedException ie ) {
1065 // somebody called interrupt() on us
1066 // we obbey and return without without closing the socket
1067 Thread.currentThread().interrupt();
1068 return;
1069 }
1070 }
1071
1072 // we set the closeLock after the above check because we might
1073 // have returned without proceeding
1074 if( closeLock ) return;
1075 closeLock = true;
1076 }
1077
1078 if ( fd <= 0 )
1079 {
1080 z.reportln( "RXTXPort:close detected bad File Descriptor" );
1081 return;
1082 }
1083 setDTR(false);
1084 setDSR(false);
1085 if (debug)
1086 z.reportln( "RXTXPort:close( " + this.name + " ) setting monThreadisInterrupted");
1087 if ( ! monThreadisInterrupted )
1088 {
1089 removeEventListener();
1090 }
1091 if (debug)
1092 z.reportln( "RXTXPort:close( " + this.name + " ) calling nativeClose");
1093 nativeClose( this.name );
1094 if (debug)
1095 z.reportln( "RXTXPort:close( " + this.name + " ) calling super.close");
1096 super.close();
1097 fd = 0;
1098 closeLock = false;
1099 if (debug)
1100 z.reportln( "RXTXPort:close( " + this.name + " ) leaving");
1101 }
1102
1103
1104 /** Finalize the port */
1105 protected void finalize()
1106 {
1107 if (debug)
1108 z.reportln( "RXTXPort:finalize()");
1109 if( fd > 0 )
1110 {
1111 if (debug)
1112 z.reportln( "RXTXPort:calling close()");
1113 close();
1114 }
1115 z.finalize();
1116 }
1117
1118 /** Inner class for SerialOutputStream */
1119 class SerialOutputStream extends OutputStream
1120 {
1121 /**
1122 * @param b
1123 * @throws IOException
1124 */
1125 public void write( int b ) throws IOException
1126 {
1127 if (debug_write)
1128 z.reportln( "RXTXPort:SerialOutputStream:write(int)");
1129 if( speed == 0 ) return;
1130 if ( monThreadisInterrupted == true )
1131 {
1132 return;
1133 }
1134 synchronized (IOLockedMutex) {
1135 IOLocked++;
1136 }
1137 try {
1138 waitForTheNativeCodeSilly();
1139 if ( fd == 0 )
1140 {
1141 throw new IOException();
1142 }
1143 writeByte( b, monThreadisInterrupted );
1144 if (debug_write)
1145 z.reportln( "Leaving RXTXPort:SerialOutputStream:write( int )");
1146 } finally {
1147 synchronized (IOLockedMutex) {
1148 IOLocked--;
1149 }
1150 }
1151 }
1152 /**
1153 * @param b[]
1154 * @throws IOException
1155 */
1156 public void write( byte b[] ) throws IOException
1157 {
1158 if (debug_write)
1159 {
1160 z.reportln( "Entering RXTXPort:SerialOutputStream:write(" + b.length + ") "/* + new String(b)*/ );
1161 }
1162 if( speed == 0 ) return;
1163 if ( monThreadisInterrupted == true )
1164 {
1165 return;
1166 }
1167 if ( fd == 0 ) throw new IOException();
1168 synchronized (IOLockedMutex) {
1169 IOLocked++;
1170 }
1171 try {
1172 waitForTheNativeCodeSilly();
1173 writeArray( b, 0, b.length, monThreadisInterrupted );
1174 if (debug_write)
1175 z.reportln( "Leaving RXTXPort:SerialOutputStream:write(" +b.length +")");
1176 } finally {
1177 synchronized(IOLockedMutex) {
1178 IOLocked--;
1179 }
1180 }
1181
1182 }
1183 /**
1184 * @param b[]
1185 * @param off
1186 * @param len
1187 * @throws IOException
1188 */
1189 public void write( byte b[], int off, int len )
1190 throws IOException
1191 {
1192 if( speed == 0 ) return;
1193 if( off + len > b.length )
1194 {
1195 throw new IndexOutOfBoundsException(
1196 "Invalid offset/length passed to read"
1197 );
1198 }
1199
1200 byte send[] = new byte[len];
1201 System.arraycopy( b, off, send, 0, len );
1202 if (debug_write)
1203 {
1204 z.reportln( "Entering RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send) */ );
1205 }
1206 if ( fd == 0 ) throw new IOException();
1207 if ( monThreadisInterrupted == true )
1208 {
1209 return;
1210 }
1211 synchronized (IOLockedMutex) {
1212 IOLocked++;
1213 }
1214 try
1215 {
1216 waitForTheNativeCodeSilly();
1217 writeArray( send, 0, len, monThreadisInterrupted );
1218 if( debug_write )
1219 z.reportln( "Leaving RXTXPort:SerialOutputStream:write(" + send.length + " " + off + " " + len + " " +") " /*+ new String(send)*/ );
1220 } finally {
1221 synchronized (IOLockedMutex) {
1222 IOLocked--;
1223 }
1224 }
1225 }
1226 /**
1227 */
1228 public void flush() throws IOException
1229 {
1230 if (debug)
1231 z.reportln( "RXTXPort:SerialOutputStream:flush() enter");
1232 if( speed == 0 ) return;
1233 if ( fd == 0 ) throw new IOException();
1234 if ( monThreadisInterrupted == true )
1235 {
1236 if (debug)
1237 z.reportln( "RXTXPort:SerialOutputStream:flush() Leaving Interrupted");
1238 return;
1239 }
1240 synchronized(IOLockedMutex) {
1241 IOLocked++;
1242 }
1243 try
1244 {
1245 waitForTheNativeCodeSilly();
1246 /*
1247 this is probably good on all OS's but for now
1248 just sendEvent from java on Sol
1249 */
1250 if ( nativeDrain( monThreadisInterrupted ) )
1251 sendEvent( SerialPortEvent.OUTPUT_BUFFER_EMPTY, true );
1252 if (debug)
1253 z.reportln( "RXTXPort:SerialOutputStream:flush() leave");
1254 }
1255 finally
1256 {
1257 synchronized (IOLockedMutex) {
1258 IOLocked--;
1259 }
1260 }
1261 }
1262 }
1263
1264 /** Inner class for SerialInputStream */
1265 class SerialInputStream extends InputStream
1266 {
1267 /**
1268 * @return int the int read
1269 * @throws IOException
1270 * @see java.io.InputStream
1271 *
1272 *timeout threshold Behavior
1273 *------------------------------------------------------------------------
1274 *0 0 blocks until 1 byte is available timeout > 0,
1275 * threshold = 0, blocks until timeout occurs, returns -1
1276 * on timeout
1277 *>0 >0 blocks until timeout, returns - 1 on timeout, magnitude
1278 * of threshold doesn't play a role.
1279 *0 >0 Blocks until 1 byte, magnitude of threshold doesn't
1280 * play a role
1281 */
1282 public synchronized int read() throws IOException
1283 {
1284 if (debug_read)
1285 z.reportln( "RXTXPort:SerialInputStream:read() called");
1286 if ( fd == 0 ) throw new IOException();
1287 if ( monThreadisInterrupted )
1288 {
1289 z.reportln( "+++++++++ read() monThreadisInterrupted" );
1290 }
1291 synchronized (IOLockedMutex) {
1292 IOLocked++;
1293 }
1294 try {
1295 if (debug_read_results)
1296 z.reportln( "RXTXPort:SerialInputStream:read() L" );
1297 waitForTheNativeCodeSilly();
1298 if (debug_read_results)
1299 z.reportln( "RXTXPort:SerialInputStream:read() N" );
1300 int result = readByte();
1301 if (debug_read_results)
1302 //z.reportln( "RXTXPort:SerialInputStream:read() returns byte = " + result );
1303 z.reportln( "RXTXPort:SerialInputStream:read() returns" );
1304 return( result );
1305 }
1306 finally
1307 {
1308 synchronized (IOLockedMutex) {
1309 IOLocked--;
1310 }
1311 }
1312 }
1313 /**
1314 * @param b[]
1315 * @return int number of bytes read
1316 * @throws IOException
1317 *
1318 *timeout threshold Behavior
1319 *------------------------------------------------------------------------
1320 *0 0 blocks until 1 byte is available
1321 *>0 0 blocks until timeout occurs, returns 0 on timeout
1322 *>0 >0 blocks until timeout or reads threshold bytes,
1323 returns 0 on timeout
1324 *0 >0 blocks until reads threshold bytes
1325 */
1326 public synchronized int read( byte b[] ) throws IOException
1327 {
1328 int result;
1329 if (debug_read)
1330 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + ") called");
1331 if ( monThreadisInterrupted == true )
1332 {
1333 return(0);
1334 }
1335 synchronized (IOLockedMutex) {
1336 IOLocked++;
1337 }
1338 try
1339 {
1340 waitForTheNativeCodeSilly();
1341 result = read( b, 0, b.length);
1342 if (debug_read_results)
1343 z.reportln( "RXTXPort:SerialInputStream:read() returned " + result + " bytes" );
1344 return( result );
1345 }
1346 finally
1347 {
1348 synchronized (IOLockedMutex) {
1349 IOLocked--;
1350 }
1351 }
1352 }
1353 /*
1354 read(byte b[], int, int)
1355 Documentation is at http://java.sun.com/products/jdk/1.2/docs/api/java/io/InputStream.html#read(byte[], int, int)
1356 */
1357 /**
1358 * @param b[]
1359 * @param off
1360 * @param len
1361 * @return int number of bytes read
1362 * @throws IOException
1363 *
1364 *timeout threshold Behavior
1365 *------------------------------------------------------------------------
1366 *0 0 blocks until 1 byte is available
1367 *>0 0 blocks until timeout occurs, returns 0 on timeout
1368 *>0 >0 blocks until timeout or reads threshold bytes,
1369 returns 0 on timeout
1370 *0 >0 blocks until either threshold # of bytes or len bytes,
1371 whichever was lower.
1372 */
1373 public synchronized int read( byte b[], int off, int len )
1374 throws IOException
1375 {
1376 if (debug_read)
1377 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") called" /*+ new String(b) */ );
1378 int result;
1379 /*
1380 * Some sanity checks
1381 */
1382 if ( fd == 0 )
1383 {
1384 if (debug_read)
1385 z.reportln( "RXTXPort:SerialInputStream:read() fd == 0");
1386 z.reportln("+++++++ IOException()\n");
1387 throw new IOException();
1388 }
1389
1390 if( b==null )
1391 {
1392 z.reportln("+++++++ NullPointerException()\n");
1393 if (debug_read)
1394 z.reportln( "RXTXPort:SerialInputStream:read() b == 0");
1395 throw new NullPointerException();
1396 }
1397
1398 if( (off < 0) || (len < 0) || (off+len > b.length))
1399 {
1400 z.reportln("+++++++ IndexOutOfBoundsException()\n");
1401 if (debug_read)
1402 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1403 throw new IndexOutOfBoundsException();
1404 }
1405
1406 /*
1407 * Return immediately if len==0
1408 */
1409 if( len==0 )
1410 {
1411 if (debug_read)
1412 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1413 return 0;
1414 }
1415 /*
1416 * See how many bytes we should read
1417 */
1418 int Minimum = len;
1419
1420 if( threshold==0 )
1421 {
1422 /*
1423 * If threshold is disabled, read should return as soon
1424 * as data are available (up to the amount of available
1425 * bytes in order to avoid blocking)
1426 * Read may return earlier depending of the receive time
1427 * out.
1428 */
1429 int a = nativeavailable();
1430 if( a == 0 )
1431 Minimum = 1;
1432 else
1433 Minimum = Math.min( Minimum, a );
1434 }
1435 else
1436 {
1437 /*
1438 * Threshold is enabled. Read should return when
1439 * 'threshold' bytes have been received (or when the
1440 * receive timeout expired)
1441 */
1442 Minimum = Math.min(Minimum, threshold);
1443 }
1444 if ( monThreadisInterrupted == true )
1445 {
1446 if (debug_read)
1447 z.reportln( "RXTXPort:SerialInputStream:read() Interrupted");
1448 return(0);
1449 }
1450 synchronized (IOLockedMutex) {
1451 IOLocked++;
1452 }
1453 try
1454 {
1455 waitForTheNativeCodeSilly();
1456 result = readArray( b, off, Minimum);
1457 if (debug_read_results)
1458 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
1459 return( result );
1460 }
1461 finally
1462 {
1463 synchronized (IOLockedMutex) {
1464 IOLocked--;
1465 }
1466 }
1467 }
1468
1469 /**
1470 * @param b[]
1471 * @param off
1472 * @param len
1473 * @param t[]
1474 * @return int number of bytes read
1475 * @throws IOException
1476
1477 We are trying to catch the terminator in the native code
1478 Right now it is assumed that t[] is an array of 2 bytes.
1479
1480 if the read encounters the two bytes, it will return and the
1481 array will contain the terminator. Otherwise read behavior should
1482 be the same as read( b[], off, len ). Timeouts have not been well
1483 tested.
1484 */
1485
1486 public synchronized int read( byte b[], int off, int len, byte t[] )
1487 throws IOException
1488 {
1489 if (debug_read)
1490 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") called" /*+ new String(b) */ );
1491 int result;
1492 /*
1493 * Some sanity checks
1494 */
1495 if ( fd == 0 )
1496 {
1497 if (debug_read)
1498 z.reportln( "RXTXPort:SerialInputStream:read() fd == 0");
1499 z.reportln("+++++++ IOException()\n");
1500 throw new IOException();
1501 }
1502
1503 if( b==null )
1504 {
1505 z.reportln("+++++++ NullPointerException()\n");
1506 if (debug_read)
1507 z.reportln( "RXTXPort:SerialInputStream:read() b == 0");
1508 throw new NullPointerException();
1509 }
1510
1511 if( (off < 0) || (len < 0) || (off+len > b.length))
1512 {
1513 z.reportln("+++++++ IndexOutOfBoundsException()\n");
1514 if (debug_read)
1515 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1516 throw new IndexOutOfBoundsException();
1517 }
1518
1519 /*
1520 * Return immediately if len==0
1521 */
1522 if( len==0 )
1523 {
1524 if (debug_read)
1525 z.reportln( "RXTXPort:SerialInputStream:read() off < 0 ..");
1526 return 0;
1527 }
1528 /*
1529 * See how many bytes we should read
1530 */
1531 int Minimum = len;
1532
1533 if( threshold==0 )
1534 {
1535 /*
1536 * If threshold is disabled, read should return as soon
1537 * as data are available (up to the amount of available
1538 * bytes in order to avoid blocking)
1539 * Read may return earlier depending of the receive time
1540 * out.
1541 */
1542 int a = nativeavailable();
1543 if( a == 0 )
1544 Minimum = 1;
1545 else
1546 Minimum = Math.min( Minimum, a );
1547 }
1548 else
1549 {
1550 /*
1551 * Threshold is enabled. Read should return when
1552 * 'threshold' bytes have been received (or when the
1553 * receive timeout expired)
1554 */
1555 Minimum = Math.min(Minimum, threshold);
1556 }
1557 if ( monThreadisInterrupted == true )
1558 {
1559 if (debug_read)
1560 z.reportln( "RXTXPort:SerialInputStream:read() Interrupted");
1561 return(0);
1562 }
1563 synchronized (IOLockedMutex) {
1564 IOLocked++;
1565 }
1566 try
1567 {
1568 waitForTheNativeCodeSilly();
1569 result = readTerminatedArray( b, off, Minimum, t );
1570 if (debug_read_results)
1571 z.reportln( "RXTXPort:SerialInputStream:read(" + b.length + " " + off + " " + len + ") returned " + result + " bytes" /*+ new String(b) */);
1572 return( result );
1573 }
1574 finally
1575 {
1576 synchronized (IOLockedMutex) {
1577 IOLocked--;
1578 }
1579 }
1580 }
1581 /**
1582 * @return int bytes available
1583 * @throws IOException
1584 */
1585 public synchronized int available() throws IOException
1586 {
1587 if ( monThreadisInterrupted == true )
1588 {
1589 return(0);
1590 }
1591 if ( debug_verbose )
1592 z.reportln( "RXTXPort:available() called" );
1593 synchronized (IOLockedMutex) {
1594 IOLocked++;
1595 }
1596 try
1597 {
1598 int r = nativeavailable();
1599 if ( debug_verbose )
1600 z.reportln( "RXTXPort:available() returning " +
1601 r );
1602 return r;
1603 }
1604 finally
1605 {
1606 synchronized (IOLockedMutex) {
1607 IOLocked--;
1608 }
1609 }
1610 }
1611 }
1612 /**
1613 */
1614 class MonitorThread extends Thread
1615 {
1616 /** Note: these have to be separate boolean flags because the
1617 SerialPortEvent constants are NOT bit-flags, they are just
1618 defined as integers from 1 to 10 -DPL */
1619 private volatile boolean CTS=false;
1620 private volatile boolean DSR=false;
1621 private volatile boolean RI=false;
1622 private volatile boolean CD=false;
1623 private volatile boolean OE=false;
1624 private volatile boolean PE=false;
1625 private volatile boolean FE=false;
1626 private volatile boolean BI=false;
1627 private volatile boolean Data=false;
1628 private volatile boolean Output=false;
1629
1630 MonitorThread()
1631 {
1632 if (debug)
1633 z.reportln( "RXTXPort:MontitorThread:MonitorThread()");
1634 }
1635 /**
1636 * run the thread and call the event loop.
1637 */
1638 public void run()
1639 {
1640 if (debug)
1641 z.reportln( "RXTXPort:MontitorThread:run()");
1642 monThreadisInterrupted=false;
1643 eventLoop();
1644 if (debug)
1645 z.reportln( "eventLoop() returned");
1646 }
1647 protected void finalize() throws Throwable
1648 {
1649 if (debug)
1650 z.reportln( "RXTXPort:MonitorThread exiting");
1651 }
1652 }
1653 /**
1654 * A dummy method added so RXTX compiles on Kaffee
1655 * @deprecated deprecated but used in Kaffe
1656 */
1657 public void setRcvFifoTrigger(int trigger){};
1658
1659 /*------------------------ END OF CommAPI -----------------------------*/
1660
1661 private native static void nativeStaticSetSerialPortParams( String f,
1662 int b, int d, int s, int p )
1663 throws UnsupportedCommOperationException;
1664 private native static boolean nativeStaticSetDSR( String port,
1665 boolean flag )
1666 throws UnsupportedCommOperationException;
1667 private native static boolean nativeStaticSetDTR( String port,
1668 boolean flag )
1669 throws UnsupportedCommOperationException;
1670 private native static boolean nativeStaticSetRTS( String port,
1671 boolean flag )
1672 throws UnsupportedCommOperationException;
1673
1674 private native static boolean nativeStaticIsDSR( String port )
1675 throws UnsupportedCommOperationException;
1676 private native static boolean nativeStaticIsDTR( String port )
1677 throws UnsupportedCommOperationException;
1678 private native static boolean nativeStaticIsRTS( String port )
1679 throws UnsupportedCommOperationException;
1680 private native static boolean nativeStaticIsCTS( String port )
1681 throws UnsupportedCommOperationException;
1682 private native static boolean nativeStaticIsCD( String port )
1683 throws UnsupportedCommOperationException;
1684 private native static boolean nativeStaticIsRI( String port )
1685 throws UnsupportedCommOperationException;
1686
1687 private native static int nativeStaticGetBaudRate( String port )
1688 throws UnsupportedCommOperationException;
1689 private native static int nativeStaticGetDataBits( String port )
1690 throws UnsupportedCommOperationException;
1691 private native static int nativeStaticGetParity( String port )
1692 throws UnsupportedCommOperationException;
1693 private native static int nativeStaticGetStopBits( String port )
1694 throws UnsupportedCommOperationException;
1695
1696
1697 private native byte nativeGetParityErrorChar( )
1698 throws UnsupportedCommOperationException;
1699 private native boolean nativeSetParityErrorChar( byte b )
1700 throws UnsupportedCommOperationException;
1701 private native byte nativeGetEndOfInputChar( )
1702 throws UnsupportedCommOperationException;
1703 private native boolean nativeSetEndOfInputChar( byte b )
1704 throws UnsupportedCommOperationException;
1705 private native boolean nativeSetUartType(String type, boolean test)
1706 throws UnsupportedCommOperationException;
1707 native String nativeGetUartType()
1708 throws UnsupportedCommOperationException;
1709 private native boolean nativeSetBaudBase(int BaudBase)
1710 throws UnsupportedCommOperationException;
1711 private native int nativeGetBaudBase()
1712 throws UnsupportedCommOperationException;
1713 private native boolean nativeSetDivisor(int Divisor)
1714 throws UnsupportedCommOperationException;
1715 private native int nativeGetDivisor()
1716 throws UnsupportedCommOperationException;
1717 private native boolean nativeSetLowLatency()
1718 throws UnsupportedCommOperationException;
1719 private native boolean nativeGetLowLatency()
1720 throws UnsupportedCommOperationException;
1721 private native boolean nativeSetCallOutHangup(boolean NoHup)
1722 throws UnsupportedCommOperationException;
1723 private native boolean nativeGetCallOutHangup()
1724 throws UnsupportedCommOperationException;
1725 private native boolean nativeClearCommInput()
1726 throws UnsupportedCommOperationException;
1727
1728 /**
1729 * Extension to CommAPI
1730 * This is an extension to CommAPI. It may not be supported on
1731 * all operating systems.
1732 *
1733 * This is only accurate up to 38600 baud currently.
1734 *
1735 * @param port the name of the port thats been preopened
1736 * @return BaudRate on success
1737 * @throws UnsupportedCommOperationException;
1738 * This will not behave as expected with custom speeds
1739 *
1740 */
1741 public static int staticGetBaudRate( String port )
1742 throws UnsupportedCommOperationException
1743 {
1744 if ( debug )
1745 z.reportln(
1746 "RXTXPort:staticGetBaudRate( " + port + " )");
1747 return(nativeStaticGetBaudRate( port ));
1748 }
1749 /**
1750 * Extension to CommAPI
1751 * This is an extension to CommAPI. It may not be supported on
1752 * all operating systems.
1753 *
1754 * @param port the name of the port thats been preopened
1755 * @return DataBits on success
1756 * @throws UnsupportedCommOperationException;
1757 *
1758 */
1759 public static int staticGetDataBits( String port )
1760 throws UnsupportedCommOperationException
1761 {
1762 if ( debug )
1763 z.reportln(
1764 "RXTXPort:staticGetDataBits( " + port + " )");
1765 return(nativeStaticGetDataBits( port ) );
1766 }
1767
1768 /**
1769 * Extension to CommAPI
1770 * This is an extension to CommAPI. It may not be supported on
1771 * all operating systems.
1772 *
1773 * @param port the name of the port thats been preopened
1774 * @return Parity on success
1775 * @throws UnsupportedCommOperationException;
1776 *
1777 */
1778 public static int staticGetParity( String port )
1779 throws UnsupportedCommOperationException
1780 {
1781 if ( debug )
1782 z.reportln(
1783 "RXTXPort:staticGetParity( " + port + " )");
1784 return( nativeStaticGetParity( port ) );
1785 }
1786
1787 /**
1788 * Extension to CommAPI
1789 * This is an extension to CommAPI. It may not be supported on
1790 * all operating systems.
1791 *
1792 * @param port the name of the port thats been preopened
1793 * @return StopBits on success
1794 * @throws UnsupportedCommOperationException;
1795 *
1796 */
1797 public static int staticGetStopBits( String port )
1798 throws UnsupportedCommOperationException
1799 {
1800 if ( debug )
1801 z.reportln(
1802 "RXTXPort:staticGetStopBits( " + port + " )");
1803 return(nativeStaticGetStopBits( port ) );
1804 }
1805
1806 /**
1807 * Extension to CommAPI
1808 * This is an extension to CommAPI. It may not be supported on
1809 * all operating systems.
1810 *
1811 * Set the SerialPort parameters
1812 * 1.5 stop bits requires 5 databits
1813 * @param f filename
1814 * @param b baudrate
1815 * @param d databits
1816 * @param s stopbits
1817 * @param p parity
1818 *
1819 * @throws UnsupportedCommOperationException
1820 * @see gnu.io.UnsupportedCommOperationException
1821 */
1822
1823 public static void staticSetSerialPortParams( String f, int b, int d,
1824 int s, int p )
1825 throws UnsupportedCommOperationException
1826 {
1827 if ( debug )
1828 z.reportln(
1829 "RXTXPort:staticSetSerialPortParams( " +
1830 f + " " + b + " " + d + " " + s + " " + p );
1831 nativeStaticSetSerialPortParams( f, b, d, s, p );
1832 }
1833
1834 /**
1835 * Extension to CommAPI
1836 * This is an extension to CommAPI. It may not be supported on
1837 * all operating systems.
1838 *
1839 * Open the port and set DSR. remove lockfile and do not close
1840 * This is so some software can appear to set the DSR before 'opening'
1841 * the port a second time later on.
1842 *
1843 * @return true on success
1844 * @throws UnsupportedCommOperationException;
1845 *
1846 */
1847
1848 public static boolean staticSetDSR( String port, boolean flag )
1849 throws UnsupportedCommOperationException
1850 {
1851 if ( debug )
1852 z.reportln( "RXTXPort:staticSetDSR( " + port +
1853 " " + flag );
1854 return( nativeStaticSetDSR( port, flag ) );
1855 }
1856
1857 /**
1858 * Extension to CommAPI
1859 * This is an extension to CommAPI. It may not be supported on
1860 * all operating systems.
1861 *
1862 * Open the port and set DTR. remove lockfile and do not close
1863 * This is so some software can appear to set the DTR before 'opening'
1864 * the port a second time later on.
1865 *
1866 * @return true on success
1867 * @throws UnsupportedCommOperationException;
1868 *
1869 */
1870
1871 public static boolean staticSetDTR( String port, boolean flag )
1872 throws UnsupportedCommOperationException
1873 {
1874 if ( debug )
1875 z.reportln( "RXTXPort:staticSetDTR( " + port +
1876 " " + flag );
1877 return( nativeStaticSetDTR( port, flag ) );
1878 }
1879
1880 /**
1881 * Extension to CommAPI
1882 * This is an extension to CommAPI. It may not be supported on
1883 * all operating systems.
1884 *
1885 * Open the port and set RTS. remove lockfile and do not close
1886 * This is so some software can appear to set the RTS before 'opening'
1887 * the port a second time later on.
1888 *
1889 * @return none
1890 * @throws UnsupportedCommOperationException;
1891 *
1892 */
1893
1894 public static boolean staticSetRTS( String port, boolean flag )
1895 throws UnsupportedCommOperationException
1896 {
1897 if ( debug )
1898 z.reportln( "RXTXPort:staticSetRTS( " + port +
1899 " " + flag );
1900 return( nativeStaticSetRTS( port, flag ) );
1901 }
1902
1903 /**
1904 * Extension to CommAPI
1905 * This is an extension to CommAPI. It may not be supported on
1906 * all operating systems.
1907 *
1908 * find the fd and return RTS without using a Java open() call
1909 *
1910 * @param port
1911 * @return true if asserted
1912 * @throws UnsupportedCommOperationException;
1913 *
1914 */
1915
1916 public static boolean staticIsRTS( String port )
1917 throws UnsupportedCommOperationException
1918 {
1919 if ( debug )
1920 z.reportln( "RXTXPort:staticIsRTS( " + port + " )" );
1921 return( nativeStaticIsRTS( port ) );
1922 }
1923 /**
1924 * Extension to CommAPI
1925 * This is an extension to CommAPI. It may not be supported on
1926 * all operating systems.
1927 *
1928 * find the fd and return CD without using a Java open() call
1929 *
1930 * @param port
1931 * @return true if asserted
1932 * @throws UnsupportedCommOperationException;
1933 *
1934 */
1935
1936 public static boolean staticIsCD( String port )
1937 throws UnsupportedCommOperationException
1938 {
1939 if ( debug )
1940 z.reportln( "RXTXPort:staticIsCD( " + port + " )" );
1941 return( nativeStaticIsCD( port ) );
1942 }
1943 /**
1944 * Extension to CommAPI
1945 * This is an extension to CommAPI. It may not be supported on
1946 * all operating systems.
1947 *
1948 * find the fd and return CTS without using a Java open() call
1949 *
1950 * @param port
1951 * @return true if asserted
1952 * @throws UnsupportedCommOperationException;
1953 *
1954 */
1955
1956 public static boolean staticIsCTS( String port )
1957 throws UnsupportedCommOperationException
1958 {
1959 if ( debug )
1960 z.reportln( "RXTXPort:staticIsCTS( " + port + " )" );
1961 return( nativeStaticIsCTS( port ) );
1962 }
1963 /**
1964 * Extension to CommAPI
1965 * This is an extension to CommAPI. It may not be supported on
1966 * all operating systems.
1967 *
1968 * find the fd and return DSR without using a Java open() call
1969 *
1970 * @param port
1971 * @return true if asserted
1972 * @throws UnsupportedCommOperationException;
1973 *
1974 */
1975
1976 public static boolean staticIsDSR( String port )
1977 throws UnsupportedCommOperationException
1978 {
1979 if ( debug )
1980 z.reportln( "RXTXPort:staticIsDSR( " + port + " )" );
1981 return( nativeStaticIsDSR( port ) );
1982 }
1983 /**
1984 * Extension to CommAPI
1985 * This is an extension to CommAPI. It may not be supported on
1986 * all operating systems.
1987 *
1988 * find the fd and return DTR without using a Java open() call
1989 *
1990 * @param port
1991 * @return true if asserted
1992 * @throws UnsupportedCommOperationException;
1993 *
1994 */
1995
1996 public static boolean staticIsDTR( String port )
1997 throws UnsupportedCommOperationException
1998 {
1999 if ( debug )
2000 z.reportln( "RXTXPort:staticIsDTR( " + port + " )" );
2001 return( nativeStaticIsDTR( port ) );
2002 }
2003 /**
2004 * Extension to CommAPI
2005 * This is an extension to CommAPI. It may not be supported on
2006 * all operating systems.
2007 *
2008 * find the fd and return RI without using a Java open() call
2009 *
2010 * @param port
2011 * @return true if asserted
2012 * @throws UnsupportedCommOperationException;
2013 *
2014 */
2015
2016 public static boolean staticIsRI( String port )
2017 throws UnsupportedCommOperationException
2018 {
2019 if ( debug )
2020 z.reportln( "RXTXPort:staticIsRI( " + port + " )" );
2021 return( nativeStaticIsRI( port ) );
2022 }
2023
2024
2025 /**
2026 * Extension to CommAPI
2027 * This is an extension to CommAPI. It may not be supported on
2028 * all operating systems.
2029 * @return int the Parity Error Character
2030 * @throws UnsupportedCommOperationException;
2031 *
2032 * Anyone know how to do this in Unix?
2033 */
2034
2035 public byte getParityErrorChar( )
2036 throws UnsupportedCommOperationException
2037 {
2038 byte ret;
2039 if ( debug )
2040 z.reportln( "getParityErrorChar()" );
2041 ret = nativeGetParityErrorChar();
2042 if ( debug )
2043 z.reportln( "getParityErrorChar() returns " +
2044 ret );
2045 return( ret );
2046 }
2047
2048 /**
2049 * Extension to CommAPI
2050 * This is an extension to CommAPI. It may not be supported on
2051 * all operating systems.
2052 * @param b Parity Error Character
2053 * @return boolean true on success
2054 * @throws UnsupportedCommOperationException;
2055 *
2056 * Anyone know how to do this in Unix?
2057 */
2058
2059 public boolean setParityErrorChar( byte b )
2060 throws UnsupportedCommOperationException
2061 {
2062 if ( debug )
2063 z.reportln( "setParityErrorChar(" + b + ")" );
2064 return( nativeSetParityErrorChar( b ) );
2065 }
2066
2067 /**
2068 * Extension to CommAPI
2069 * This is an extension to CommAPI. It may not be supported on
2070 * all operating systems.
2071 * @return int the End of Input Character
2072 * @throws UnsupportedCommOperationException;
2073 *
2074 * Anyone know how to do this in Unix?
2075 */
2076
2077 public byte getEndOfInputChar( )
2078 throws UnsupportedCommOperationException
2079 {
2080 byte ret;
2081 if ( debug )
2082 z.reportln( "getEndOfInputChar()" );
2083 ret = nativeGetEndOfInputChar();
2084 if ( debug )
2085 z.reportln( "getEndOfInputChar() returns " +
2086 ret );
2087 return( ret );
2088 }
2089
2090 /**
2091 * Extension to CommAPI
2092 * This is an extension to CommAPI. It may not be supported on
2093 * all operating systems.
2094 * @param b End Of Input Character
2095 * @return boolean true on success
2096 * @throws UnsupportedCommOperationException;
2097 */
2098
2099 public boolean setEndOfInputChar( byte b )
2100 throws UnsupportedCommOperationException
2101 {
2102 if ( debug )
2103 z.reportln( "setEndOfInputChar(" + b + ")" );
2104 return( nativeSetEndOfInputChar( b ) );
2105 }
2106
2107 /**
2108 * Extension to CommAPI
2109 * This is an extension to CommAPI. It may not be supported on
2110 * all operating systems.
2111 * @param type String representation of the UART type which mayb
2112 * be "none", "8250", "16450", "16550", "16550A", "16650", "16550V2"
2113 * or "16750".
2114 * @param test boolean flag to determin if the UART should be tested.
2115 * @return boolean true on success
2116 * @throws UnsupportedCommOperationException;
2117 */
2118 public boolean setUARTType(String type, boolean test)
2119 throws UnsupportedCommOperationException
2120 {
2121 if ( debug )
2122 z.reportln( "RXTXPort:setUARTType()");
2123 return nativeSetUartType(type, test);
2124 }
2125 /**
2126 * Extension to CommAPI
2127 * This is an extension to CommAPI. It may not be supported on
2128 * all operating systems.
2129 * @return type String representation of the UART type which mayb
2130 * be "none", "8250", "16450", "16550", "16550A", "16650", "16550V2"
2131 * or "16750".
2132 * @throws UnsupportedCommOperationException;
2133 */
2134 public String getUARTType() throws UnsupportedCommOperationException
2135 {
2136 return nativeGetUartType();
2137 }
2138
2139 /**
2140 * Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
2141 * before using.
2142 * @param BaudBase The clock frequency divided by 16. Default
2143 * BaudBase is 115200.
2144 * @return true on success
2145 * @throws UnsupportedCommOperationException, IOException
2146 */
2147
2148 public boolean setBaudBase(int BaudBase)
2149 throws UnsupportedCommOperationException,
2150 IOException
2151 {
2152 if ( debug )
2153 z.reportln( "RXTXPort:setBaudBase()");
2154 return nativeSetBaudBase(BaudBase);
2155 }
2156
2157 /**
2158 * Extension to CommAPI
2159 * @return BaudBase
2160 * @throws UnsupportedCommOperationException, IOException
2161 */
2162
2163 public int getBaudBase() throws UnsupportedCommOperationException,
2164 IOException
2165 {
2166 if ( debug )
2167 z.reportln( "RXTXPort:getBaudBase()");
2168 return nativeGetBaudBase();
2169 }
2170
2171 /**
2172 * Extension to CommAPI. Set Baud Base to 38600 on Linux and W32
2173 * before using.
2174 * @param Divisor
2175 * @throws UnsupportedCommOperationException, IOException
2176 */
2177
2178 public boolean setDivisor(int Divisor)
2179 throws UnsupportedCommOperationException, IOException
2180 {
2181 if ( debug )
2182 z.reportln( "RXTXPort:setDivisor()");
2183 return nativeSetDivisor(Divisor);
2184 }
2185
2186 /**
2187 * Extension to CommAPI
2188 * @return Divisor;
2189 * @throws UnsupportedCommOperationException, IOException
2190 */
2191
2192 public int getDivisor() throws UnsupportedCommOperationException,
2193 IOException
2194 {
2195 if ( debug )
2196 z.reportln( "RXTXPort:getDivisor()");
2197 return nativeGetDivisor();
2198 }
2199
2200 /**
2201 * Extension to CommAPI
2202 * returns boolean true on success
2203 * @throws UnsupportedCommOperationException
2204 */
2205
2206 public boolean setLowLatency() throws UnsupportedCommOperationException
2207 {
2208 if ( debug )
2209 z.reportln( "RXTXPort:setLowLatency()");
2210 return nativeSetLowLatency();
2211 }
2212
2213 /**
2214 * Extension to CommAPI
2215 * returns boolean true on success
2216 * @throws UnsupportedCommOperationException
2217 */
2218
2219 public boolean getLowLatency() throws UnsupportedCommOperationException
2220 {
2221 if ( debug )
2222 z.reportln( "RXTXPort:getLowLatency()");
2223 return nativeGetLowLatency();
2224 }
2225
2226 /**
2227 * Extension to CommAPI
2228 * returns boolean true on success
2229 * @throws UnsupportedCommOperationException
2230 */
2231
2232 public boolean setCallOutHangup(boolean NoHup)
2233 throws UnsupportedCommOperationException
2234 {
2235 if ( debug )
2236 z.reportln( "RXTXPort:setCallOutHangup()");
2237 return nativeSetCallOutHangup(NoHup);
2238 }
2239
2240 /**
2241 * Extension to CommAPI
2242 * returns boolean true on success
2243 * @throws UnsupportedCommOperationException
2244 */
2245
2246 public boolean getCallOutHangup()
2247 throws UnsupportedCommOperationException
2248 {
2249 if ( debug )
2250 z.reportln( "RXTXPort:getCallOutHangup()");
2251 return nativeGetCallOutHangup();
2252 }
2253
2254 /**
2255 * Extension to CommAPI
2256 * returns boolean true on success
2257 * @throws UnsupportedCommOperationException
2258 */
2259
2260 public boolean clearCommInput()
2261 throws UnsupportedCommOperationException
2262 {
2263 if ( debug )
2264 z.reportln( "RXTXPort:clearCommInput()");
2265 return nativeClearCommInput();
2266 }
2267
2268 /*------------------------ END OF CommAPI Extensions -----------------------*/
2269 }