Debian Codegraph @

src/termios.c

raw symbols SCIP
1 #ifdef TRENT_IS_HERE
2 #define TRACE
3 #define DEBUG
4 #define DEBUG_MW
5 #ifdef DEBUG_MW
6 extern void mexWarMsgTxt( const char * );
7 extern void mexPrintf( const char *, ... );
8 #endif /* DEBUG_MW */
9 #endif /* TRENT_IS_HERE */
10 extern void report( char * );
11 extern void report_warning( char * );
12 extern void report_error( char * );
13 /*-------------------------------------------------------------------------
14 | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
15 | RXTX is a native interface to serial ports in java.
16 | Copyright 1998-2002 by Wayne roberts [email protected]
17 | Copyright 1997-2008 by Trent Jarvi [email protected] and others who
18 | actually wrote it. See individual source files for more information.
19 |
20 | A copy of the LGPL v 2.1 may be found at
21 | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is
22 | here for your convenience.
23 |
24 | This library is free software; you can redistribute it and/or
25 | modify it under the terms of the GNU Lesser General Public
26 | License as published by the Free Software Foundation; either
27 | version 2.1 of the License, or (at your option) any later version.
28 |
29 | This library is distributed in the hope that it will be useful,
30 | but WITHOUT ANY WARRANTY; without even the implied warranty of
31 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 | Lesser General Public License for more details.
33 |
34 | An executable that contains no derivative of any portion of RXTX, but
35 | is designed to work with RXTX by being dynamically linked with it,
36 | is considered a "work that uses the Library" subject to the terms and
37 | conditions of the GNU Lesser General Public License.
38 |
39 | The following has been added to the RXTX License to remove
40 | any confusion about linking to RXTX. We want to allow in part what
41 | section 5, paragraph 2 of the LGPL does not permit in the special
42 | case of linking over a controlled interface. The intent is to add a
43 | Java Specification Request or standards body defined interface in the
44 | future as another exception but one is not currently available.
45 |
46 | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
47 |
48 | As a special exception, the copyright holders of RXTX give you
49 | permission to link RXTX with independent modules that communicate with
50 | RXTX solely through the Sun Microsytems CommAPI interface version 2,
51 | regardless of the license terms of these independent modules, and to copy
52 | and distribute the resulting combined work under terms of your choice,
53 | provided that every copy of the combined work is accompanied by a complete
54 | copy of the source code of RXTX (the version of RXTX used to produce the
55 | combined work), being distributed under the terms of the GNU Lesser General
56 | Public License plus this exception. An independent module is a
57 | module which is not derived from or based on RXTX.
58 |
59 | Note that people who make modified versions of RXTX are not obligated
60 | to grant this special exception for their modified versions; it is
61 | their choice whether to do so. The GNU Lesser General Public License
62 | gives permission to release a modified version without this exception; this
63 | exception also makes it possible to release a modified version which
64 | carries forward this exception.
65 |
66 | You should have received a copy of the GNU Lesser General Public
67 | License along with this library; if not, write to the Free
68 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
69 | All trademarks belong to their respective owners.
70 --------------------------------------------------------------------------*/
71 #include <windows.h>
72 #include <stdio.h>
73 #include <errno.h>
74 #include <time.h>
75 #include "win32termios.h"
76
77 /*
78 * odd malloc.h error with lcc compiler
79 * winsock has FIONREAD with lcc
80 */
81
82 #ifdef __LCC__
83 # include <winsock.h>
84 #else
85 # include <malloc.h>
86 #endif /* __LCC__ */
87
88 #define SIGIO 0
89
90 int my_errno;
91 extern int errno;
92 struct termios_list
93 {
94 char filename[80];
95 int my_errno;
96 int interrupt;
97 int event_flag;
98 int tx_happened;
99 unsigned long *hComm;
100 struct termios *ttyset;
101 struct serial_struct *sstruct;
102 /* for DTR DSR */
103 unsigned char MSR;
104 struct async_struct *astruct;
105 struct serial_icounter_struct *sis;
106 int open_flags;
107 OVERLAPPED rol;
108 OVERLAPPED wol;
109 OVERLAPPED sol;
110 int fd;
111 struct termios_list *next;
112 struct termios_list *prev;
113 };
114 struct termios_list *first_tl = NULL;
115
116 /*----------------------------------------------------------
117 serial_test
118
119 accept: filename to test
120 perform:
121 return: 1 on success 0 on failure
122 exceptions:
123 win32api: CreateFile CloseHandle
124 comments: if the file opens it should be ok.
125 ----------------------------------------------------------*/
126 int serial_test( char * filename )
127 {
128 unsigned long *hcomm;
129 int ret;
130 hcomm = CreateFile( filename, GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
131 if ( hcomm == INVALID_HANDLE_VALUE )
132 {
133 if (GetLastError() == ERROR_ACCESS_DENIED)
134 {
135 ret = 1;
136 }
137 else
138 {
139 ret = 0;
140 }
141 }
142 else
143 {
144 ret = 1;
145 }
146 CloseHandle( hcomm );
147 return(ret);
148 }
149
150 void termios_setflags( int fd, int termios_flags[] )
151 {
152 struct termios_list *index = find_port( fd );
153 int i, result;
154 int windows_flags[11] = { 0, EV_RXCHAR, EV_TXEMPTY, EV_CTS, EV_DSR,
155 EV_RING|0x2000, EV_RLSD, EV_ERR,
156 EV_ERR, EV_ERR, EV_BREAK
157 };
158 if( !index )
159 {
160 LEAVE( "termios_setflags" );
161 return;
162 }
163 index->event_flag = 0;
164 for(i=0;i<11;i++)
165 if( termios_flags[i] )
166 index->event_flag |= windows_flags[i];
167 result = SetCommMask( index->hComm, index->event_flag );
168 /*
169 This is rank. 0x2000 was used to detect the trailing edge of ring.
170 The leading edge is detedted by EV_RING.
171
172 The trailing edge is reliable. The leading edge is not.
173 Softie no longer allows the trailing edge to be detected in NTsp2
174 and beyond.
175
176 So... Try the reliable option above and if it fails, use the less
177 reliable means.
178
179 The screams for a giveio solution that bypasses the kernel.
180 */
181 if( index->event_flag & 0x2000 && result == 0 )
182 {
183 index->event_flag &= ~0x2000;
184 SetCommMask( index->hComm, index->event_flag );
185 }
186 }
187
188 /*----------------------------------------------------------
189 get_fd()
190
191 accept: filename
192 perform: find the file descriptor associated with the filename
193 return: fd
194 exceptions:
195 win32api: None
196 comments: This is not currently called by anything
197 ----------------------------------------------------------*/
198
199 int get_fd( char *filename )
200 {
201 struct termios_list *index = first_tl;
202
203 ENTER( "get_fd" );
204 if( !index )
205 {
206 return -1;
207 }
208
209 while( strcmp( index->filename, filename ) )
210 {
211 index = index->next;
212 if( !index->next )
213 return( -1 );
214 }
215 LEAVE( "get_fd" );
216 return( index->fd );
217 }
218
219 /*----------------------------------------------------------
220 get_filename()
221
222 accept: file descriptor
223 perform: find the filename associated with the file descriptor
224 return: the filename associated with the fd
225 exceptions: None
226 win32api: None
227 comments: This is not currently called by anything
228 ----------------------------------------------------------*/
229
230 char *get_filename( int fd )
231 {
232 struct termios_list *index = first_tl;
233
234 ENTER( "get_filename" );
235 if( !index )
236 return( "bad" );
237 while( index->fd != fd )
238 {
239 if( index->next == NULL )
240 return( "bad" );
241 index = index->next;
242 }
243 LEAVE( "get_filename" );
244 return( index->filename );
245 }
246
247 /*----------------------------------------------------------
248 dump_termios_list()
249
250 accept: string to print out.
251 perform:
252 return:
253 exceptions:
254 win32api: None
255 comments: used only for debugging eg serial_close()
256 ----------------------------------------------------------*/
257
258 void dump_termios_list( char *foo )
259 {
260 #ifdef DEBUG
261 struct termios_list *index = first_tl;
262 printf( "============== %s start ===============\n", foo );
263 if ( index )
264 {
265 printf( "%i filename | %s\n", index->fd, index->filename );
266 }
267 /*
268 if ( index->next )
269 {
270 printf( "%i filename | %s\n", index->fd, index->filename );
271 }
272 */
273 printf( "============== %s end ===============\n", foo );
274 #endif
275 }
276
277 /*----------------------------------------------------------
278 set_errno()
279
280 accept:
281 perform:
282 return:
283 exceptions:
284 win32api: None
285 comments: FIXME
286 ----------------------------------------------------------*/
287
288 void set_errno( int error )
289 {
290 my_errno = error;
291 }
292
293 /*----------------------------------------------------------
294 usleep()
295
296 accept:
297 perform:
298 return:
299 exceptions:
300 win32api: Sleep()
301 comments:
302 ----------------------------------------------------------*/
303
304 void usleep( unsigned long usec )
305 {
306 Sleep( usec/1000 );
307 }
308
309 /*----------------------------------------------------------
310 CBR_toB()
311
312 accept:
313 perform:
314 return:
315 exceptions:
316 win32api: None
317 comments:
318 ----------------------------------------------------------*/
319
320 int CBR_to_B( int Baud )
321 {
322 ENTER( "CBR_to_B" );
323 switch ( Baud )
324 {
325
326 case 0: return( B0 );
327 case 50: return( B50 );
328 case 75: return( B75 );
329 case CBR_110: return( B110 );
330 case 134: return( B134 );
331 case 150: return( B150 );
332 case 200: return( B200 );
333 case CBR_300: return( B300 );
334 case CBR_600: return( B600 );
335 case CBR_1200: return( B1200 );
336 case 1800: return( B1800 );
337 case CBR_2400: return( B2400 );
338 case CBR_4800: return( B4800 );
339 case CBR_9600: return( B9600 );
340 case CBR_14400: return( B14400 );
341 case CBR_19200: return( B19200 );
342 case CBR_28800: return( B28800 );
343 case CBR_38400: return( B38400 );
344 case CBR_57600: return( B57600 );
345 case CBR_115200: return( B115200 );
346 case CBR_128000: return( B128000 );
347 case CBR_230400: return( B230400 );
348 case CBR_256000: return( B256000 );
349 case CBR_460800: return( B460800 );
350 case CBR_500000: return( B500000 );
351 case CBR_576000: return( B576000 );
352 case CBR_921600: return( B921600 );
353 case CBR_1000000: return( B1000000 );
354 case CBR_1152000: return( B1152000 );
355 case CBR_1500000: return( B1500000 );
356 case CBR_2000000: return( B2000000 );
357 case CBR_2500000: return( B2500000 );
358 case CBR_3000000: return( B3000000 );
359 case CBR_3500000: return( B3500000 );
360 case CBR_4000000: return( B4000000 );
361 default:
362 /* assume custom baudrate */
363 return( Baud );
364 }
365 }
366
367 /*----------------------------------------------------------
368 B_to_CBR()
369
370 accept:
371 perform:
372 return:
373 exceptions:
374 win32api:
375 comments: None
376 ----------------------------------------------------------*/
377
378 int B_to_CBR( int Baud )
379 {
380 int ret;
381 ENTER( "B_to_CBR" );
382 switch ( Baud )
383 {
384 case 0: ret = 0; break;
385 case B50: ret = 50; break;
386 case B75: ret = 75; break;
387 case B110: ret = CBR_110; break;
388 case B134: ret = 134; break;
389 case B150: ret = 150; break;
390 case B200: ret = 200; break;
391 case B300: ret = CBR_300; break;
392 case B600: ret = CBR_600; break;
393 case B1200: ret = CBR_1200; break;
394 case B1800: ret = 1800; break;
395 case B2400: ret = CBR_2400; break;
396 case B4800: ret = CBR_4800; break;
397 case B9600: ret = CBR_9600; break;
398 case B14400: ret = CBR_14400; break;
399 case B19200: ret = CBR_19200; break;
400 case B28800: ret = CBR_28800; break;
401 case B38400: ret = CBR_38400; break;
402 case B57600: ret = CBR_57600; break;
403 case B115200: ret = CBR_115200; break;
404 case B128000: ret = CBR_128000; break;
405 case B230400: ret = CBR_230400; break;
406 case B256000: ret = CBR_256000; break;
407 case B460800: ret = CBR_460800; break;
408 case B500000: ret = CBR_500000; break;
409 case B576000: ret = CBR_576000; break;
410 case B921600: ret = CBR_921600; break;
411 case B1000000: ret = CBR_1000000; break;
412 case B1152000: ret = CBR_1152000; break;
413 case B1500000: ret = CBR_1500000; break;
414 case B2000000: ret = CBR_2000000; break;
415 case B2500000: ret = CBR_2500000; break;
416 case B3000000: ret = CBR_3000000; break;
417 case B3500000: ret = CBR_3500000; break;
418 case B4000000: ret = CBR_4000000; break;
419
420 default:
421 /* assume custom baudrate */
422 return Baud;
423 }
424 LEAVE( "B_to_CBR" );
425 return ret;
426 }
427
428 /*----------------------------------------------------------
429 bytesize_to_termios()
430
431 accept:
432 perform:
433 return:
434 exceptions:
435 win32api: None
436 comments:
437 ----------------------------------------------------------*/
438
439 int bytesize_to_termios( int ByteSize )
440 {
441 ENTER( "bytesize_to_termios" );
442 switch ( ByteSize )
443 {
444 case 5: return( CS5 );
445 case 6: return( CS6 );
446 case 7: return( CS7 );
447 case 8:
448 default: return( CS8 );
449 }
450 }
451
452 /*----------------------------------------------------------
453 termios_to_bytesize()
454
455 accept:
456 perform:
457 return:
458 exceptions:
459 win32api: None
460 comments:
461 ----------------------------------------------------------*/
462
463 int termios_to_bytesize( int cflag )
464 {
465 ENTER( "termios_to_bytesize" );
466 switch ( cflag & CSIZE )
467 {
468 case CS5: return( 5 );
469 case CS6: return( 6 );
470 case CS7: return( 7 );
471 case CS8:
472 default: return( 8 );
473 }
474 }
475
476 /*----------------------------------------------------------
477 get_dos_port()
478
479 accept:
480 perform:
481 return:
482 exceptions:
483 win32api: None
484 comments:
485 ----------------------------------------------------------*/
486
487 const char *get_dos_port( char const *name )
488 {
489 ENTER( "get_dos_port" );
490 if ( !strcmp( name, "/dev/cua0" ) ) return( "COM1" );
491 if ( !strcmp( name, "/dev/cua1" ) ) return( "COM2" );
492 if ( !strcmp( name, "/dev/cua2" ) ) return( "COM3" );
493 if ( !strcmp( name, "/dev/cua3" ) ) return( "COM4" );
494 LEAVE( "get_dos_port" );
495 return( ( const char * ) name );
496 }
497
498 /*----------------------------------------------------------
499 ClearErrors()
500
501 accept:
502 perform: keep track of errors for the eventLoop() (SerialImp.c)
503 return: the return value of ClearCommError()
504 exceptions:
505 win32api: ClearCommError()
506 comments:
507 ----------------------------------------------------------*/
508
509 int ClearErrors( struct termios_list *index, COMSTAT *Stat )
510 {
511 unsigned long ErrCode;
512 int ret;
513
514 ret = ClearCommError( index->hComm, &ErrCode, Stat );
515 if ( ret == 0 )
516 {
517 YACK();
518 return( ret );
519 }
520 #ifdef DEBUG_ERRORS
521 if ( ErrCode )
522 {
523 printf("%i frame %i %i overrun %i %i parity %u %i brk %i %i\n",
524 (int) ErrCode,
525 (int) ErrCode & CE_FRAME,
526 index->sis->frame,
527 (int) (ErrCode & CE_OVERRUN) | ( ErrCode & CE_RXOVER ),
528 index->sis->overrun,
529 (int) ErrCode & CE_RXPARITY,
530 index->sis->parity,
531 (int) ErrCode & CE_BREAK,
532 index->sis->brk
533 );
534 }
535 #endif /* DEBUG_ERRORS */
536 if( ErrCode & CE_FRAME )
537 {
538 index->sis->frame++;
539 ErrCode &= ~CE_FRAME;
540 }
541 #ifdef LIFE_IS_GOOD
542 FIXME OVERRUN is spewing
543 if( ErrCode & CE_OVERRUN )
544 {
545 index->sis->overrun++;
546 ErrCode &= ~CE_OVERRUN;
547 }
548 /* should this be here? */
549 else if( ErrCode & CE_RXOVER )
550 {
551 index->sis->overrun++;
552 ErrCode &= ~CE_OVERRUN;
553 }
554 #endif /* LIFE_IS_GOOD */
555 if( ErrCode & CE_RXPARITY )
556 {
557 index->sis->parity++;
558 ErrCode &= ~CE_RXPARITY;
559 }
560 if( ErrCode & CE_BREAK )
561 {
562 index->sis->brk++;
563 ErrCode &= ~CE_BREAK;
564 }
565 return( ret );
566 }
567
568 /*----------------------------------------------------------
569 FillDCB()
570
571 accept:
572 perform:
573 return:
574 exceptions:
575 win32api: GetCommState(), SetCommState(), SetCommTimeouts()
576 comments:
577 ----------------------------------------------------------*/
578
579 BOOL FillDCB( DCB *dcb, unsigned long *hCommPort, COMMTIMEOUTS Timeout )
580 {
581
582 ENTER( "FillDCB" );
583 dcb->DCBlength = sizeof( dcb );
584 if ( !GetCommState( hCommPort, dcb ) )
585 {
586 report( "GetCommState\n" );
587 return( -1 );
588 }
589 dcb->BaudRate = CBR_9600 ;
590 dcb->ByteSize = 8;
591 dcb->Parity = NOPARITY;
592 dcb->StopBits = ONESTOPBIT;
593 dcb->fDtrControl = DTR_CONTROL_ENABLE;
594 dcb->fRtsControl = RTS_CONTROL_ENABLE;
595 dcb->fOutxCtsFlow = FALSE;
596 dcb->fOutxDsrFlow = FALSE;
597 dcb->fDsrSensitivity = FALSE;
598 dcb->fOutX = FALSE;
599 dcb->fInX = FALSE;
600 dcb->fTXContinueOnXoff = FALSE;
601 dcb->XonChar = 0x11;
602 dcb->XoffChar = 0x13;
603 dcb->XonLim = 0;
604 dcb->XoffLim = 0;
605 dcb->fParity = TRUE;
606 if ( EV_BREAK|EV_CTS|EV_DSR|EV_ERR|EV_RING|( EV_RLSD & EV_RXFLAG ) )
607 dcb->EvtChar = '\n';
608 else dcb->EvtChar = '\0';
609 if ( !SetCommState( hCommPort, dcb ) )
610 {
611 report( "SetCommState\n" );
612 YACK();
613 return( -1 );
614 }
615 if ( !SetCommTimeouts( hCommPort, &Timeout ) )
616 {
617 YACK();
618 report( "SetCommTimeouts\n" );
619 return( -1 );
620 }
621 LEAVE( "FillDCB" );
622 return ( TRUE ) ;
623 }
624
625 /*----------------------------------------------------------
626 serial_close()
627
628 accept:
629 perform:
630 return:
631 exceptions:
632 win32api: SetCommMask(), CloseHandle()
633 comments:
634 ----------------------------------------------------------*/
635
636 int serial_close( int fd )
637 {
638 struct termios_list *index;
639 /* char message[80]; */
640
641 ENTER( "serial_close" );
642 if( !first_tl || !first_tl->hComm )
643 {
644 report( "gotit!" );
645 return( 0 );
646 }
647 index = find_port( fd );
648 if ( !index )
649 {
650 LEAVE( "serial_close" );
651 return -1;
652 }
653
654 /* WaitForSingleObject( index->wol.hEvent, INFINITE ); */
655 /*
656 if ( index->hComm != INVALID_HANDLE_VALUE )
657 {
658 if ( !SetCommMask( index->hComm, EV_RXCHAR ) )
659 {
660 YACK();
661 report( "eventLoop hung\n" );
662 }
663 CloseHandle( index->hComm );
664 }
665 else
666 {
667 sprintf( message, "serial_ close(): Invalid Port Reference for %s\n",
668 index->filename );
669 report( message );
670 }
671 */
672 if ( index->next && index->prev )
673 {
674 index->next->prev = index->prev;
675 index->prev->next = index->next;
676 }
677 else if ( index->prev )
678 {
679 index->prev->next = NULL;
680 }
681 else if ( index->next )
682 {
683 index->next->prev = NULL;
684 first_tl = index->next;
685 }
686 else
687 first_tl = NULL;
688 if ( index )
689 {
690 if ( index->rol.hEvent ) CloseHandle( index->rol.hEvent );
691 if ( index->wol.hEvent ) CloseHandle( index->wol.hEvent );
692 if ( index->sol.hEvent ) CloseHandle( index->sol.hEvent );
693 if ( index->hComm ) CloseHandle( index->hComm );
694 if ( index->ttyset ) free( index->ttyset );
695 if ( index->astruct ) free( index->astruct );
696 if ( index->sstruct ) free( index->sstruct );
697 if ( index->sis ) free( index->sis );
698 /* had problems with strdup
699 if ( index->filename ) free( index->filename );
700 */
701 free( index );
702 }
703 LEAVE( "serial_close" );
704 return 0;
705 }
706
707 /*----------------------------------------------------------
708 cfmakeraw()
709
710 accept:
711 perform:
712 return:
713 exceptions:
714 win32api: None
715 comments:
716 ----------------------------------------------------------*/
717
718 void cfmakeraw( struct termios *s_termios )
719 {
720 ENTER( "cfmakeraw" );
721 s_termios->c_iflag &= ~( IGNBRK|BRKINT|PARMRK|ISTRIP
722 |INLCR|IGNCR|ICRNL|IXON );
723 s_termios->c_oflag &= ~OPOST;
724 s_termios->c_lflag &= ~( ECHO|ECHONL|ICANON|ISIG|IEXTEN );
725 s_termios->c_cflag &= ~( CSIZE|PARENB );
726 s_termios->c_cflag |= CS8;
727 LEAVE( "cfmakeraw" );
728 }
729
730 /*----------------------------------------------------------
731 init_termios()
732
733 accept:
734 perform:
735 return:
736 exceptions:
737 win32api:
738 comments:
739 ----------------------------------------------------------*/
740
741 BOOL init_serial_struct( struct serial_struct *sstruct )
742 {
743 ENTER( "init_serial_struct" );
744
745 /*
746 use of custom_divisor and baud_base requires access to
747 kernel space. The kernel does try its best if you just
748 toss a baud rate at it though.
749 */
750
751 sstruct->custom_divisor = 0;
752 sstruct->baud_base = 115200;
753
754 /* not currently used check values before using */
755
756 /* unsigned short */
757
758 sstruct->close_delay = 0;
759 sstruct->closing_wait = 0;
760 sstruct->iomem_reg_shift = 0;
761
762 /* int */
763
764 sstruct->type = 0;
765 sstruct->line = 0;
766 sstruct->irq = 0;
767 sstruct->flags = 0;
768 sstruct->xmit_fifo_size = 0;
769 sstruct->hub6 = 0;
770
771 /* unsigned int */
772
773 sstruct->port = 0;
774 sstruct->port_high = 0;
775
776 /* char */
777
778 sstruct->io_type = 0;
779
780 /* unsigned char * */
781
782 sstruct->iomem_base = NULL;
783
784 LEAVE( "init_serial_struct" );
785 return TRUE;
786
787 }
788 /*----------------------------------------------------------
789 init_termios()
790
791 accept:
792 perform:
793 return:
794 exceptions:
795 win32api:
796 comments:
797 ----------------------------------------------------------*/
798
799 BOOL init_termios(struct termios *ttyset )
800 {
801 ENTER( "init_termios" );
802 if ( !ttyset )
803 return FALSE;
804 memset( ttyset, 0, sizeof( struct termios ) );
805 cfsetospeed( ttyset, B9600 );
806 cfmakeraw( ttyset );
807 ttyset->c_cc[VINTR] = 0x03; /* 0: C-c */
808 ttyset->c_cc[VQUIT] = 0x1c; /* 1: C-\ */
809 ttyset->c_cc[VERASE] = 0x7f; /* 2: <del> */
810 ttyset->c_cc[VKILL] = 0x15; /* 3: C-u */
811 ttyset->c_cc[VEOF] = 0x04; /* 4: C-d */
812 ttyset->c_cc[VTIME] = 0; /* 5: read timeout */
813 ttyset->c_cc[VMIN] = 1; /* 6: read returns after this
814 many bytes */
815 ttyset->c_cc[VSUSP] = 0x1a; /* 10: C-z */
816 ttyset->c_cc[VEOL] = '\r'; /* 11: */
817 ttyset->c_cc[VREPRINT] = 0x12; /* 12: C-r */
818 /*
819 ttyset->c_cc[VDISCARD] = 0x; 13: IEXTEN only
820 */
821 ttyset->c_cc[VWERASE] = 0x17; /* 14: C-w */
822 ttyset->c_cc[VLNEXT] = 0x16; /* 15: C-w */
823 ttyset->c_cc[VEOL2] = '\n'; /* 16: */
824 LEAVE( "init_termios" );
825 return TRUE;
826 /* default VTIME = 0, VMIN = 1: read blocks forever until one byte */
827 }
828
829 /*----------------------------------------------------------
830 port_opened()
831
832 accept:
833 perform:
834 return:
835 exceptions:
836 win32api: None
837 comments:
838 ----------------------------------------------------------*/
839
840 int port_opened( const char *filename )
841 {
842 struct termios_list *index = first_tl;
843
844 ENTER( "port_opened" );
845 if ( ! index )
846 return 0;
847 if( !strcmp( index->filename, filename ) )
848 return index->fd;
849 while ( index->next )
850 {
851 index = index->next;
852 if( !strcmp( index->filename, filename ) )
853 return index->fd;
854 }
855 LEAVE( "port_opened" );
856 return 0;
857 }
858
859 /*----------------------------------------------------------
860 open_port()
861
862 accept:
863 perform:
864 return:
865 exceptions:
866 win32api: CreateFile(), SetupComm(), CreateEvent()
867 comments:
868 FILE_FLAG_OVERLAPPED allows one to break out the select()
869 so RXTXPort.close() does not hang.
870
871 The setDTR() and setDSR() are the functions that noticed
872 to be blocked in the java close. Basically ioctl(TIOCM[GS]ET)
873 are where it hangs.
874
875 FILE_FLAG_OVERLAPPED also means we need to create valid OVERLAPPED
876 structure in Serial_select.
877 ----------------------------------------------------------*/
878
879 int open_port( struct termios_list *port )
880 {
881 ENTER( "open_port" );
882 port->hComm = CreateFile( port->filename,
883 GENERIC_READ | GENERIC_WRITE,
884 0,
885 0,
886 OPEN_EXISTING,
887 FILE_FLAG_OVERLAPPED,
888 0
889 );
890 if ( port->hComm == INVALID_HANDLE_VALUE )
891 {
892 YACK();
893 set_errno( EINVAL );
894 /*
895 printf( "serial_open failed %s\n", port->filename );
896 */
897 return -1;
898 }
899 if( !SetupComm( port->hComm, 2048, 1024 ) )
900 {
901 YACK();
902 return -1;
903 }
904
905 memset( &port->rol, 0, sizeof( OVERLAPPED ) );
906 memset( &port->wol, 0, sizeof( OVERLAPPED ) );
907 memset( &port->sol, 0, sizeof( OVERLAPPED ) );
908
909 port->rol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
910
911 if ( !port->rol.hEvent )
912 {
913 YACK();
914 report( "Could not create read overlapped\n" );
915 goto fail;
916 }
917
918 port->sol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
919
920 if ( !port->sol.hEvent )
921 {
922 YACK();
923 report( "Could not create select overlapped\n" );
924 goto fail;
925 }
926 port->wol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
927
928 if ( !port->wol.hEvent )
929 {
930 YACK();
931 report( "Could not create write overlapped\n" );
932 goto fail;
933 }
934 LEAVE( "open_port" );
935 return( 0 );
936 fail:
937 return( -1 );
938 }
939
940 /*----------------------------------------------------------
941 termios_list()
942
943 accept: fd which is a fake # for the port assigned when the port
944 is opened
945 perform: walk through a double linked list to see if the given
946 fd is in any of the termios_list members.
947 return: the termios_list if it is found.
948 NULL if no matches are found.
949 exceptions: None
950 win32api: None
951 comments:
952 ----------------------------------------------------------*/
953
954 struct termios_list *find_port( int fd )
955 {
956
957 char message[80];
958 struct termios_list *index = first_tl;
959
960 ENTER( "find_port" );
961 if ( fd <= 0 || !first_tl ) goto fail;
962
963 while( index->fd )
964 {
965 if ( index->fd == fd )
966 {
967 LEAVE( "find_port" );
968 return index;
969 }
970 if ( !index->next )
971 break;
972 index = index->next;
973 }
974 fail:
975 sprintf( message, "No info known about the port. %i\n", fd );
976 report( message );
977 set_errno( EBADF );
978 LEAVE( "find_port" );
979 return NULL;
980 }
981
982 /*----------------------------------------------------------
983 get_free_fd()
984
985 accept:
986 perform:
987 return:
988 exceptions:
989 win32api: None
990 comments:
991 ----------------------------------------------------------*/
992
993 int get_free_fd()
994 {
995 int next, last;
996 struct termios_list *index = first_tl;
997
998 ENTER( "get_free_fd" );
999 if ( !index )
1000 {
1001 return( 1 );
1002 }
1003 if ( !index->fd )
1004 {
1005 report( "!index->fd\n" );
1006 return( 1 );
1007 }
1008 if ( index->fd > 1)
1009 {
1010 first_tl = index;
1011 return ( 1 );
1012 }
1013
1014 last = index->fd;
1015
1016 while( index->next )
1017 {
1018 next = index->next->fd;
1019 if ( next != last + 1 )
1020 {
1021 return( last + 1 );
1022
1023 }
1024 index = index->next;
1025 last = next;
1026 }
1027 LEAVE( "get_free_fd" );
1028 return( index->fd + 1 );
1029 }
1030
1031 /*----------------------------------------------------------
1032 add_port()
1033
1034 accept:
1035 perform:
1036 return:
1037 exceptions:
1038 win32api: None
1039 comments:
1040 ----------------------------------------------------------*/
1041
1042 struct termios_list *add_port( const char *filename )
1043 {
1044 struct termios_list *index = first_tl;
1045 struct termios_list *port;
1046
1047 ENTER( "add_port" );
1048
1049 port = malloc( sizeof( struct termios_list ) );
1050 if( !port )
1051 goto fail;
1052 memset( port, 0, sizeof( struct termios_list ) );
1053
1054 port->ttyset = malloc( sizeof( struct termios ) );
1055 if( ! port->ttyset )
1056 goto fail;
1057 memset( port->ttyset, 0, sizeof( struct termios ) );
1058
1059 port->sstruct = malloc( sizeof( struct serial_struct ) );
1060 if( ! port->sstruct )
1061 goto fail;
1062 memset( port->sstruct, 0, sizeof( struct serial_struct ) );
1063 port->sis = malloc( sizeof( struct serial_icounter_struct ) );
1064 if( ! port->sis )
1065 goto fail;
1066 memset( port->sis, 0, sizeof( struct serial_icounter_struct ) );
1067
1068 /* FIXME the async_struct is being defined by mingw32 headers?
1069 port->astruct = malloc( sizeof( struct async_struct ) );
1070 if( ! port->astruct )
1071 goto fail;
1072 memset( port->astruct, 0, sizeof( struct async_struct ) );
1073 */
1074 port->MSR = 0;
1075
1076 strcpy(port->filename, filename );
1077 /* didnt free well? strdup( filename ); */
1078 if( ! port->filename )
1079 goto fail;
1080
1081 port->fd = get_free_fd();
1082
1083
1084 if ( !first_tl )
1085 {
1086 port->prev = NULL;
1087 port->next = NULL;
1088 first_tl = port;
1089 }
1090 else
1091 {
1092 while ( ( index->fd < port->fd ) && index->next )
1093 index = index->next;
1094 if ( index->fd > port->fd )
1095 {
1096 /* inserting previously closed fd */
1097 port->prev = index->prev;
1098 port->next=index;
1099 index->prev->next = port;
1100 index->prev = port;
1101 }
1102 else
1103 {
1104 /* adding to end of list */
1105 port->prev = index;
1106 port->next = NULL;
1107 index->next = port;
1108 }
1109 }
1110 LEAVE( "add_port" );
1111 return port;
1112
1113 fail:
1114 report( "add_port: Out Of Memory\n");
1115 if ( port->ttyset ) free( port->ttyset );
1116 if ( port->astruct ) free( port->astruct );
1117 if ( port->sstruct ) free( port->sstruct );
1118 if ( port->sis ) free( port->sis );
1119 /* had problems with strdup
1120 if ( port->filename ) free( port->filename );
1121 */
1122 if ( port ) free( port );
1123 return port;
1124 }
1125
1126 /*----------------------------------------------------------
1127 check_port_capabilities()
1128
1129 accept:
1130 perform:
1131 return:
1132 exceptions:
1133 win32api: GetCommProperties(), GetCommState()
1134 comments:
1135 ----------------------------------------------------------*/
1136
1137 int check_port_capabilities( struct termios_list *index )
1138 {
1139 COMMPROP cp;
1140 DCB dcb;
1141 char message[80];
1142
1143 ENTER( "check_port_capabilities" );
1144 /* check for capabilities */
1145 GetCommProperties( index->hComm, &cp );
1146 if ( !( cp.dwProvCapabilities & PCF_DTRDSR ) )
1147 {
1148 sprintf( message,
1149 "%s: no DTR & DSR support\n", index->filename );
1150 report( message );
1151 }
1152 if ( !( cp.dwProvCapabilities & PCF_RLSD ) )
1153 {
1154 sprintf( message, "%s: no carrier detect (RLSD) support\n",
1155 index->filename );
1156 report( message );
1157 }
1158 if ( !( cp.dwProvCapabilities & PCF_RTSCTS ) )
1159 {
1160 sprintf( message,
1161 "%s: no RTS & CTS support\n", index->filename );
1162 report( message );
1163 }
1164 if ( !( cp.dwProvCapabilities & PCF_TOTALTIMEOUTS ) )
1165 {
1166 sprintf( message, "%s: no timeout support\n", index->filename );
1167 report( message );
1168 }
1169 if ( !GetCommState( index->hComm, &dcb ) )
1170 {
1171 YACK();
1172 report( "GetCommState\n" );
1173 return -1;
1174 }
1175 LEAVE( "check_port_capabilities" );
1176 return 0;
1177
1178 }
1179
1180 /*----------------------------------------------------------
1181 serial_open()
1182
1183 accept:
1184 perform:
1185 return:
1186 exceptions:
1187 win32api: None
1188 comments:
1189 ----------------------------------------------------------*/
1190
1191 int serial_open( const char *filename, int flags, ... )
1192 {
1193 struct termios_list *index;
1194 char message[80];
1195
1196 ENTER( "serial_open" );
1197 if ( port_opened( filename ) )
1198 {
1199 report( "Port is already opened\n" );
1200 return( -1 );
1201 }
1202 index = add_port( filename );
1203 if( !index )
1204 {
1205 report( "serial_open !index\n" );
1206 return( -1 );
1207 }
1208
1209 index->interrupt = 0;
1210 index->tx_happened = 0;
1211 if ( open_port( index ) )
1212 {
1213 sprintf( message, "serial_open(): Invalid Port Reference for %s\n",
1214 filename );
1215 report( message );
1216 serial_close( index->fd );
1217 return -1;
1218 }
1219
1220 if( check_port_capabilities( index ) )
1221 {
1222 report( "check_port_capabilites!" );
1223 serial_close( index->fd );
1224 return -1;
1225 }
1226
1227 init_termios( index->ttyset );
1228 init_serial_struct( index->sstruct );
1229
1230 /* set default condition */
1231 tcsetattr( index->fd, 0, index->ttyset );
1232
1233 /* if opened with non-blocking, then operating non-blocking */
1234 if ( flags & O_NONBLOCK )
1235 index->open_flags = O_NONBLOCK;
1236 else
1237 index->open_flags = 0;
1238
1239
1240 if( !first_tl->hComm )
1241 {
1242 sprintf( message, "open(): Invalid Port Reference for %s\n",
1243 index->filename );
1244 report( message );
1245 }
1246 if ( first_tl->hComm == INVALID_HANDLE_VALUE )
1247 report( "serial_open: test\n" );
1248 LEAVE( "serial_open" );
1249 return( index->fd );
1250 }
1251
1252
1253 /*----------------------------------------------------------
1254 serial_write()
1255
1256 accept:
1257 perform:
1258 return:
1259 exceptions:
1260 win32api: WriteFile(), GetLastError(),
1261 WaitForSingleObject(), GetOverlappedResult(),
1262 FlushFileBuffers(), Sleep()
1263 comments:
1264 ----------------------------------------------------------*/
1265
1266 int serial_write( int fd, char *Str, int length )
1267 {
1268 unsigned long nBytes;
1269 struct termios_list *index;
1270 /* COMSTAT Stat; */
1271 int old_flag;
1272
1273 ENTER( "serial_write" );
1274
1275 if ( fd <= 0 )
1276 {
1277 return 0;
1278 }
1279 index = find_port( fd );
1280 if ( !index )
1281 {
1282 LEAVE( "serial_write");
1283 return -1;
1284 }
1285 old_flag = index->event_flag;
1286 /*
1287 index->event_flag &= ~EV_TXEMPTY;
1288 SetCommMask( index->hComm, index->event_flag );
1289 index->tx_happened = 1;
1290 */
1291 index->wol.Offset = index->wol.OffsetHigh = 0;
1292 ResetEvent( index->wol.hEvent );
1293 if ( !WriteFile( index->hComm, Str, length, &nBytes, &index->wol ) )
1294 {
1295 WaitForSingleObject( index->wol.hEvent,100 );
1296 if ( GetLastError() != ERROR_IO_PENDING )
1297 {
1298 /* ClearErrors( index, &Stat ); */
1299 report( "serial_write error\n" );
1300 /* report("Condition 1 Detected in write()\n"); */
1301 YACK();
1302 errno = EIO;
1303 nBytes=-1;
1304 goto end;
1305 }
1306 /* This is breaking on Win2K, WinXP for some reason */
1307 else while( !GetOverlappedResult( index->hComm, &index->wol,
1308 &nBytes, TRUE ) )
1309 {
1310 if ( GetLastError() != ERROR_IO_INCOMPLETE )
1311 {
1312 /* report("Condition 2 Detected in write()\n"); */
1313 YACK();
1314 errno = EIO;
1315 nBytes = -1;
1316 goto end;
1317 /* ClearErrors( index, &Stat ); */
1318 }
1319 }
1320 }
1321 else
1322 {
1323 /* Write finished synchronously. That is ok!
1324 * I have seen this with USB to Serial
1325 * devices like TI's.
1326 */
1327 }
1328 end:
1329 /* FlushFileBuffers( index->hComm ); */
1330 index->event_flag |= EV_TXEMPTY;
1331 /* ClearErrors( index, &Stat ); */
1332 SetCommMask( index->hComm, index->event_flag );
1333 /* ClearErrors( index, &Stat ); */
1334 index->event_flag = old_flag;
1335 index->tx_happened = 1;
1336 LEAVE( "serial_write" );
1337 return nBytes;
1338 }
1339
1340 /*----------------------------------------------------------
1341 serial_read()
1342
1343 accept:
1344 perform:
1345 return:
1346 exceptions:
1347 win32api: ReadFile(), GetLastError(), WaitForSingleObject()
1348 GetOverLappedResult()
1349 comments: If setting errno make sure not to use EWOULDBLOCK
1350 In that case use EAGAIN. See SerialImp.c:testRead()
1351 ----------------------------------------------------------*/
1352
1353 int serial_read( int fd, void *vb, int size )
1354 {
1355 long start, now;
1356 unsigned long nBytes = 0, total = 0, error;
1357 /* unsigned long waiting = 0; */
1358 int err, vmin;
1359 struct termios_list *index;
1360 char message[80];
1361 COMSTAT stat;
1362 clock_t c;
1363 unsigned char *dest = vb;
1364
1365 start = GetTickCount();
1366 ENTER( "serial_read" );
1367
1368 if ( fd <= 0 )
1369 {
1370 return 0;
1371 }
1372 index = find_port( fd );
1373 if ( !index )
1374 {
1375 LEAVE( "serial_read" );
1376 return -1;
1377 }
1378
1379 /* FIXME: CREAD: without this, data cannot be read
1380 FIXME: PARMRK: mark framing & parity errors
1381 FIXME: IGNCR: ignore \r
1382 FIXME: ICRNL: convert \r to \n
1383 FIXME: INLCR: convert \n to \r
1384 */
1385
1386 if ( index->open_flags & O_NONBLOCK )
1387 {
1388 int ret;
1389 vmin = 0;
1390 /* pull mucho-cpu here? */
1391 do {
1392 #ifdef DEBUG_VERBOSE
1393 report( "vmin=0\n" );
1394 #endif /* DEBUG_VERBOSE */
1395 ret = ClearErrors( index, &stat);
1396 /* we should use -1 instead of 0 for disabled timeout */
1397 now = GetTickCount();
1398 if ( index->ttyset->c_cc[VTIME] &&
1399 now-start >= (index->ttyset->c_cc[VTIME]*100)) {
1400 /*
1401 sprintf( message, "now = %i start = %i time = %i total =%i\n", now, start, index->ttyset->c_cc[VTIME]*100, total);
1402 report( message );
1403 */
1404 return total; /* read timeout */
1405 }
1406 } while( stat.cbInQue < size && size > 1 );
1407 }
1408 else
1409 {
1410 /* VTIME is in units of 0.1 seconds */
1411
1412 #ifdef DEBUG_VERBOSE
1413 report( "vmin!=0\n" );
1414 #endif /* DEBUG_VERBOSE */
1415 vmin = index->ttyset->c_cc[VMIN];
1416
1417 c = clock() + index->ttyset->c_cc[VTIME] * CLOCKS_PER_SEC / 10;
1418 do {
1419 error = ClearErrors( index, &stat);
1420 usleep(1000);
1421 } while ( c > clock() );
1422
1423 }
1424
1425 total = 0;
1426 while ( size > 0 )
1427 {
1428 nBytes = 0;
1429 /* ret = ClearErrors( index, &stat); */
1430
1431 index->rol.Offset = index->rol.OffsetHigh = 0;
1432 ResetEvent( index->rol.hEvent );
1433
1434 err = ReadFile( index->hComm, dest + total, size, &nBytes, &index->rol );
1435 #ifdef DEBUG_VERBOSE
1436 /* warning Roy Rogers! */
1437 sprintf(message, " ========== ReadFile = %i %s\n",
1438 ( int ) nBytes, (char *) dest + total );
1439 report( message );
1440 #endif /* DEBUG_VERBOSE */
1441 size -= nBytes;
1442 total += nBytes;
1443
1444 if ( !err )
1445 {
1446 switch ( GetLastError() )
1447 {
1448 case ERROR_BROKEN_PIPE:
1449 report( "ERROR_BROKEN_PIPE\n ");
1450 nBytes = 0;
1451 break;
1452 case ERROR_MORE_DATA:
1453 report( "ERROR_MORE_DATA\n" );
1454 break;
1455 case ERROR_IO_PENDING:
1456 while( ! GetOverlappedResult(
1457 index->hComm,
1458 &index->rol,
1459 &nBytes,
1460 TRUE ) )
1461 {
1462 if( GetLastError() !=
1463 ERROR_IO_INCOMPLETE )
1464 {
1465 ClearErrors(
1466 index,
1467 &stat);
1468 return( total );
1469 }
1470 }
1471 size -= nBytes;
1472 total += nBytes;
1473 if (size > 0) {
1474 now = GetTickCount();
1475 sprintf(message, "size > 0: spent=%ld have=%d\n", now-start, index->ttyset->c_cc[VTIME]*100);
1476 report( message );
1477 /* we should use -1 for disabled
1478 timouts */
1479 if ( index->ttyset->c_cc[VTIME] && now-start >= (index->ttyset->c_cc[VTIME]*100)) {
1480 report( "TO " );
1481 /* read timeout */
1482 return total;
1483 }
1484 }
1485 sprintf(message, "end nBytes=%ld] ", nBytes);
1486 report( message );
1487 report( "ERROR_IO_PENDING\n" );
1488 break;
1489 default:
1490 YACK();
1491 return -1;
1492 }
1493 }
1494 else
1495 {
1496 ClearErrors( index, &stat);
1497 return( total );
1498 }
1499 }
1500 LEAVE( "serial_read" );
1501 return total;
1502 }
1503
1504 #ifdef asdf
1505 int serial_read( int fd, void *vb, int size )
1506 {
1507 long start, now;
1508 unsigned long nBytes = 0, total = 0, error;
1509 /* unsigned long waiting = 0; */
1510 int err, vmin;
1511 struct termios_list *index;
1512 char message[80];
1513 COMSTAT Stat;
1514 clock_t c;
1515 unsigned char *dest = vb;
1516
1517 start = GetTickCount();
1518 ENTER( "serial_read" );
1519
1520 if ( fd <= 0 )
1521 {
1522 printf("1\n");
1523 return 0;
1524 }
1525 index = find_port( fd );
1526 if ( !index )
1527 {
1528 LEAVE( "serial_read 7" );
1529 errno = EIO;
1530 printf("2\n");
1531 return -1;
1532 }
1533
1534 /* FIXME: CREAD: without this, data cannot be read
1535 FIXME: PARMRK: mark framing & parity errors
1536 FIXME: IGNCR: ignore \r
1537 FIXME: ICRNL: convert \r to \n
1538 FIXME: INLCR: convert \n to \r
1539 */
1540
1541 ClearErrors( index, &Stat );
1542 if ( index->open_flags & O_NONBLOCK )
1543 {
1544 int ret;
1545 vmin = 0;
1546 /* pull mucho-cpu here? */
1547 do {
1548 #ifdef DEBUG_VERBOSE
1549 report( "vmin=0\n" );
1550 #endif /* DEBUG_VERBOSE */
1551 ret = ClearErrors( index, &Stat);
1552 /* we should use -1 instead of 0 for disabled timeout */
1553 now = GetTickCount();
1554 if ( index->ttyset->c_cc[VTIME] &&
1555 now-start >= (index->ttyset->c_cc[VTIME]*100)) {
1556 /*
1557 sprintf( message, "now = %i start = %i time = %i total =%i\n", now, start, index->ttyset->c_cc[VTIME]*100, total);
1558 report( message );
1559 */
1560 errno = EAGAIN;
1561 printf("3\n");
1562 return -1; /* read timeout */
1563 }
1564 } while( Stat.cbInQue < size && size > 1 );
1565 }
1566 else
1567 {
1568 /* VTIME is in units of 0.1 seconds */
1569
1570 #ifdef DEBUG_VERBOSE
1571 report( "vmin!=0\n" );
1572 #endif /* DEBUG_VERBOSE */
1573 vmin = index->ttyset->c_cc[VMIN];
1574
1575 c = clock() + index->ttyset->c_cc[VTIME] * CLOCKS_PER_SEC / 10;
1576 do {
1577 error = ClearErrors( index, &Stat);
1578 usleep(1000);
1579 } while ( c > clock() );
1580
1581 }
1582
1583 total = 0;
1584 while ( size > 0 )
1585 {
1586 nBytes = 0;
1587 /* ret = ClearErrors( index, &Stat); */
1588
1589 index->rol.Offset = index->rol.OffsetHigh = 0;
1590 ResetEvent( index->rol.hEvent );
1591
1592 err = ReadFile( index->hComm, dest + total, size, &nBytes, &index->rol );
1593 #ifdef DEBUG_VERBOSE
1594 /* warning Roy Rogers! */
1595 sprintf(message, " ========== ReadFile = %i %s\n",
1596 ( int ) nBytes, (char *) dest + total );
1597 report( message );
1598 #endif /* DEBUG_VERBOSE */
1599 size -= nBytes;
1600 total += nBytes;
1601
1602 if ( !err )
1603 {
1604 switch ( GetLastError() )
1605 {
1606 case ERROR_BROKEN_PIPE:
1607 report( "ERROR_BROKEN_PIPE\n ");
1608 nBytes = 0;
1609 break;
1610 case ERROR_MORE_DATA:
1611 report( "ERROR_MORE_DATA\n" );
1612 break;
1613 case ERROR_IO_PENDING:
1614 while( ! GetOverlappedResult(
1615 index->hComm,
1616 &index->rol,
1617 &nBytes,
1618 TRUE ) )
1619 {
1620 if( GetLastError() !=
1621 ERROR_IO_INCOMPLETE )
1622 {
1623 ClearErrors(
1624 index,
1625 &Stat);
1626 printf("4\n");
1627 return( total );
1628 }
1629 }
1630 size -= nBytes;
1631 total += nBytes;
1632 if (size > 0) {
1633 now = GetTickCount();
1634 sprintf(message, "size > 0: spent=%ld have=%d\n", now-start, index->ttyset->c_cc[VTIME]*100);
1635 report( message );
1636 /* we should use -1 for disabled
1637 timouts */
1638 if ( index->ttyset->c_cc[VTIME] && now-start >= (index->ttyset->c_cc[VTIME]*100)) {
1639 report( "TO " );
1640 /* read timeout */
1641 printf("5\n");
1642 return total;
1643 }
1644 }
1645 sprintf(message, "end nBytes=%ld] ", nBytes);
1646 report( message );
1647 report( "ERROR_IO_PENDING\n" );
1648 break;
1649 default:
1650 YACK();
1651 errno = EIO;
1652 printf("6\n");
1653 return -1;
1654 }
1655 }
1656 else
1657 {
1658 ClearErrors( index, &Stat);
1659 printf("7\n");
1660 return( total );
1661 }
1662 }
1663 LEAVE( "serial_read" );
1664 ClearErrors( index, &Stat);
1665 return total;
1666 }
1667 #endif /* asdf */
1668
1669 /*----------------------------------------------------------
1670 cfsetospeed()
1671
1672 accept:
1673 perform:
1674 return:
1675 exceptions:
1676 win32api: None
1677 comments:
1678 ----------------------------------------------------------*/
1679
1680 int cfsetospeed( struct termios *s_termios, speed_t speed )
1681 {
1682 char message[80];
1683 ENTER( "cfsetospeed" );
1684 /* clear baudrate */
1685 s_termios->c_cflag &= ~CBAUD;
1686 if ( speed & ~CBAUD )
1687 {
1688 sprintf( message, "cfsetospeed: not speed: %#o\n", speed );
1689 report( message );
1690 /* continue assuming its a custom baudrate */
1691 s_termios->c_cflag |= B38400; /* use 38400 during custom */
1692 s_termios->c_cflag |= CBAUDEX; /* use CBAUDEX for custom */
1693 }
1694 else if( speed )
1695 {
1696 s_termios->c_cflag |= speed;
1697 }
1698 else
1699 {
1700 /* PC blows up with speed 0 handled in Java */
1701 s_termios->c_cflag |= B9600;
1702 }
1703 s_termios->c_ispeed = s_termios->c_ospeed = speed;
1704 LEAVE( "cfsetospeed" );
1705 return 1;
1706 }
1707
1708 /*----------------------------------------------------------
1709 cfsetispeed()
1710
1711 accept:
1712 perform:
1713 return:
1714 exceptions:
1715 win32api: None
1716 comments:
1717 ----------------------------------------------------------*/
1718
1719 int cfsetispeed( struct termios *s_termios, speed_t speed )
1720 {
1721 return cfsetospeed( s_termios, speed );
1722 }
1723
1724 /*----------------------------------------------------------
1725 cfsetspeed()
1726
1727 accept:
1728 perform:
1729 return:
1730 exceptions:
1731 win32api: None
1732 comments:
1733 ----------------------------------------------------------*/
1734
1735 int cfsetspeed( struct termios *s_termios, speed_t speed )
1736 {
1737 return cfsetospeed( s_termios, speed );
1738 }
1739
1740 /*----------------------------------------------------------
1741 cfgetospeed()
1742
1743 accept:
1744 perform:
1745 return:
1746 exceptions:
1747 win32api: None
1748 comments:
1749 ----------------------------------------------------------*/
1750
1751 speed_t cfgetospeed( struct termios *s_termios )
1752 {
1753 ENTER( "cfgetospeed" );
1754 return s_termios->c_ospeed;
1755 }
1756
1757 /*----------------------------------------------------------
1758 cfgetispeed()
1759
1760 accept:
1761 perform:
1762 return:
1763 exceptions:
1764 win32api: None
1765 comments:
1766 ----------------------------------------------------------*/
1767
1768 speed_t cfgetispeed( struct termios *s_termios )
1769 {
1770 ENTER( "cfgetospeed" );
1771 return s_termios->c_ispeed;
1772 }
1773
1774 /*----------------------------------------------------------
1775 termios_to_DCB()
1776
1777 accept:
1778 perform:
1779 return:
1780 exceptions:
1781 win32api: None
1782 comments:
1783 ----------------------------------------------------------*/
1784 int termios_to_DCB( struct termios *s_termios, DCB *dcb )
1785 {
1786 ENTER( "termios_to_DCB" );
1787 if ( !(s_termios->c_cflag & CBAUDEX) )
1788 s_termios->c_ispeed = s_termios->c_ospeed = s_termios->c_cflag & CBAUD;
1789 dcb->BaudRate = B_to_CBR( s_termios->c_ispeed );
1790 dcb->ByteSize = termios_to_bytesize( s_termios->c_cflag );
1791
1792 if ( s_termios->c_cflag & PARENB )
1793 {
1794 if ( s_termios->c_cflag & PARODD
1795 && s_termios->c_cflag & CMSPAR )
1796 {
1797 dcb->Parity = MARKPARITY;
1798 }
1799 else if ( s_termios->c_cflag & PARODD )
1800 {
1801 dcb->Parity = ODDPARITY;
1802 }
1803 else if ( s_termios->c_cflag & CMSPAR )
1804 {
1805 dcb->Parity = SPACEPARITY;
1806 }
1807 else
1808 {
1809 dcb->Parity = EVENPARITY;
1810 }
1811 }
1812 else
1813 {
1814 dcb->Parity = NOPARITY;
1815 }
1816
1817 if ( s_termios->c_cflag & CSTOPB )
1818 {
1819 if (dcb->ByteSize == 5)
1820 {
1821 dcb->StopBits = ONE5STOPBITS;
1822 } else dcb->StopBits = TWOSTOPBITS;
1823 }
1824 else
1825 {
1826 dcb->StopBits = ONESTOPBIT;
1827 }
1828
1829 if ( s_termios->c_cflag & HARDWARE_FLOW_CONTROL )
1830 {
1831 dcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
1832 dcb->fOutxCtsFlow = TRUE;
1833 }
1834 else
1835 {
1836 dcb->fRtsControl = RTS_CONTROL_DISABLE;
1837 dcb->fOutxCtsFlow = FALSE;
1838 }
1839
1840 LEAVE( "termios_to_DCB" );
1841 return 0;
1842 }
1843
1844 /*----------------------------------------------------------
1845 DCB_to_serial_struct()
1846
1847 accept:
1848 perform:
1849 return:
1850 exceptions:
1851 win32api: None
1852 comments:
1853 ----------------------------------------------------------*/
1854 int DCB_to_serial_struct( DCB *dcb, struct serial_struct *sstruct )
1855 {
1856 return( 0 );
1857 }
1858 /*----------------------------------------------------------
1859 DCB_to_termios()
1860
1861 accept:
1862 perform:
1863 return:
1864 exceptions:
1865 win32api: None
1866 comments:
1867 ----------------------------------------------------------*/
1868 void DCB_to_termios( DCB *dcb, struct termios *s_termios )
1869 {
1870 ENTER( "DCB_to_termios" );
1871 s_termios->c_ispeed = CBR_to_B( dcb->BaudRate );
1872 s_termios->c_ospeed = s_termios->c_ispeed;
1873 s_termios->c_cflag |= s_termios->c_ispeed & CBAUD;
1874 LEAVE( "DCB_to_termios" );
1875 }
1876
1877 /*----------------------------------------------------------
1878 show_DCB()
1879
1880 accept:
1881 perform:
1882 return:
1883 exceptions:
1884 win32api: None
1885 comments:
1886 ----------------------------------------------------------*/
1887 void show_DCB( myDCB )
1888 {
1889
1890 #ifdef DEBUG_HOSED
1891 char message[80];
1892
1893 sprintf( message, "DCBlength: %ld\n", myDCB.DCBlength );
1894 report( message );
1895 sprintf( "BaudRate: %ld\n", myDCB.BaudRate );
1896 report( message );
1897 if ( myDCB.fBinary )
1898 report( "fBinary\n" );
1899 if ( myDCB.fParity )
1900 {
1901 report( "fParity: " );
1902 if ( myDCB.fErrorChar )
1903 {
1904 sprintf( message, "fErrorChar: %#x\n", myDCB.ErrorChar );
1905 report( message );
1906 }
1907 else
1908 {
1909 report( "fErrorChar == false\n" );
1910 }
1911 }
1912 if ( myDCB.fOutxCtsFlow )
1913 report( "fOutxCtsFlow\n" );
1914 if ( myDCB.fOutxDsrFlow )
1915 report( "fOutxDsrFlow\n" );
1916 if ( myDCB.fDtrControl & DTR_CONTROL_HANDSHAKE );
1917 report( "DTR_CONTROL_HANDSHAKE\n" );
1918 if ( myDCB.fDtrControl & DTR_CONTROL_ENABLE );
1919 report( "DTR_CONTROL_ENABLE\n" );
1920 if ( myDCB.fDtrControl & DTR_CONTROL_DISABLE );
1921 report( "DTR_CONTROL_DISABLE\n" );
1922 if ( myDCB.fDsrSensitivity )
1923 report( "fDsrSensitivity\n" );
1924 if ( myDCB.fTXContinueOnXoff )
1925 report( "fTXContinueOnXoff\n" );
1926 if ( myDCB.fOutX )
1927 report( "fOutX\n" );
1928 if ( myDCB.fInX )
1929 report( "fInX\n" );
1930 if ( myDCB.fNull )
1931 report( "fNull\n" );
1932 if ( myDCB.fRtsControl & RTS_CONTROL_TOGGLE )
1933 report( "RTS_CONTROL_TOGGLE\n" );
1934 if ( myDCB.fRtsControl == 0 )
1935 report( "RTS_CONTROL_HANDSHAKE ( fRtsControl==0 )\n" );
1936 if ( myDCB.fRtsControl & RTS_CONTROL_HANDSHAKE )
1937 report( "RTS_CONTROL_HANDSHAKE\n" );
1938 if ( myDCB.fRtsControl & RTS_CONTROL_ENABLE )
1939 report( "RTS_CONTROL_ENABLE\n" );
1940 if ( myDCB.fRtsControl & RTS_CONTROL_DISABLE )
1941 report( "RTS_CONTROL_DISABLE\n" );
1942 if ( myDCB.fAbortOnError )
1943 report( "fAbortOnError\n" );
1944 sprintf( message, "XonLim: %d\n", myDCB.XonLim );
1945 report( message );
1946 sprintf( message, "XoffLim: %d\n", myDCB.XoffLim );
1947 report( message );
1948 sprintf( message, "ByteSize: %d\n", myDCB.ByteSize );
1949 report( message );
1950 switch ( myDCB.Parity )
1951 {
1952 case EVENPARITY:
1953 report( "EVENPARITY" );
1954 break;
1955 case MARKPARITY:
1956 report( "MARKPARITY" );
1957 break;
1958 case NOPARITY:
1959 report( "NOPARITY" );
1960 break;
1961 case ODDPARITY:
1962 report( "ODDPARITY" );
1963 break;
1964 default:
1965 sprintf( message,
1966 "unknown Parity (%#x ):", myDCB.Parity );
1967 report( message );
1968 break;
1969 }
1970 report( "\n" );
1971 switch( myDCB.StopBits )
1972 {
1973 case ONESTOPBIT:
1974 report( "ONESTOPBIT" );
1975 break;
1976 case ONE5STOPBITS:
1977 report( "ONE5STOPBITS" );
1978 break;
1979 case TWOSTOPBITS:
1980 report( "TWOSTOPBITS" );
1981 break;
1982 default:
1983 report( "unknown StopBits (%#x ):", myDCB.StopBits );
1984 break;
1985 }
1986 report( "\n" );
1987 sprintf( message, "XonChar: %#x\n", myDCB.XonChar );
1988 report( message );
1989 sprintf( message, "XoffChar: %#x\n", myDCB.XoffChar );
1990 report( message );
1991 sprintf( message, "EofChar: %#x\n", myDCB.EofChar );
1992 report( message );
1993 sprintf( message, "EvtChar: %#x\n", myDCB.EvtChar );
1994 report( message );
1995 report( "\n" );
1996 #endif /* DEBUG_HOSED */
1997 }
1998
1999 /*----------------------------------------------------------
2000 tcgetattr()
2001
2002 accept:
2003 perform:
2004 return:
2005 exceptions:
2006 win32api: GetCommState(), GetCommTimeouts()
2007 comments:
2008 ----------------------------------------------------------*/
2009
2010 int tcgetattr( int fd, struct termios *s_termios )
2011 {
2012 DCB myDCB;
2013 COMMTIMEOUTS timeouts;
2014 struct termios_list *index;
2015 char message[80];
2016
2017 ENTER( "tcgetattr" );
2018 if ( fd <= 0 )
2019 return 0;
2020 index = find_port( fd );
2021 if ( !index )
2022 {
2023 LEAVE( "tcgetattr" );
2024 return -1;
2025 }
2026 if ( !GetCommState( index->hComm, &myDCB ) )
2027 {
2028 sprintf( message, "GetCommState failed\n" );
2029 report( message );
2030 return -1;
2031 }
2032 memcpy( s_termios, index->ttyset, sizeof( struct termios ) );
2033
2034 show_DCB( myDCB );
2035
2036 /***** input mode flags (c_iflag ) ****/
2037 /* parity check enable */
2038 if ( myDCB.fParity )
2039 {
2040 s_termios->c_iflag |= INPCK;
2041 s_termios->c_iflag &= ~IGNPAR;
2042 } else {
2043 s_termios->c_iflag &= ~INPCK;
2044 s_termios->c_iflag |= IGNPAR;
2045 }
2046 /* FIXME: IGNBRK: ignore break */
2047 /* FIXME: BRKINT: interrupt on break */
2048
2049 if ( myDCB.fOutX )
2050 {
2051 s_termios->c_iflag |= IXON;
2052 }
2053 else
2054 {
2055 /* IXON: output start/stop control */
2056 s_termios->c_iflag &= ~IXON;
2057 }
2058 if ( myDCB.fInX )
2059 {
2060 s_termios->c_iflag |= IXOFF;
2061 }
2062 else
2063 {
2064 /* IXOFF: input start/stop control */
2065 s_termios->c_iflag &= ~IXOFF;
2066 }
2067
2068 if ( myDCB.fTXContinueOnXoff )
2069 {
2070 s_termios->c_iflag |= IXANY;
2071 }
2072 else
2073 {
2074 /* IXANY: any char restarts output */
2075 s_termios->c_iflag &= ~IXANY;
2076 }
2077 /* FIXME: IMAXBEL: if input buffer full, send bell */
2078
2079 /***** control mode flags (c_cflag ) *****/
2080 /* FIXME: CLOCAL: DONT send SIGHUP on modem disconnect */
2081 /* FIXME: HUPCL: generate modem disconnect when all has closed or
2082 exited */
2083 /* CSTOPB two stop bits ( otherwise one) */
2084 if ( myDCB.StopBits == TWOSTOPBITS )
2085 {
2086 s_termios->c_cflag |= CSTOPB;
2087 } else if ( myDCB.StopBits == ONE5STOPBITS )
2088 {
2089 s_termios->c_cflag |= CSTOPB;
2090 s_termios->c_cflag |= CS5;
2091 } else if ( myDCB.StopBits == ONESTOPBIT )
2092 {
2093 s_termios->c_cflag &= ~CSTOPB;
2094 }
2095
2096 /* PARENB enable parity bit */
2097 s_termios->c_cflag &= ~( PARENB | PARODD | CMSPAR );
2098 myDCB.fParity = 1;
2099 if( myDCB.fParity )
2100 {
2101 report( "tcgetattr getting parity\n" );
2102 s_termios->c_cflag |= PARENB;
2103 if ( myDCB.Parity == MARKPARITY )
2104 {
2105 s_termios->c_cflag |= ( PARODD | CMSPAR );
2106 }
2107 else if ( myDCB.Parity == SPACEPARITY )
2108 {
2109 s_termios->c_cflag |= CMSPAR;
2110 }
2111 else if ( myDCB.Parity == ODDPARITY )
2112 {
2113 report( "ODDPARITY\n" );
2114 s_termios->c_cflag |= PARODD;
2115 }
2116 else if ( myDCB.Parity == EVENPARITY )
2117 {
2118 report( "EVENPARITY\n" );
2119 s_termios->c_cflag &= ~PARODD;
2120 }
2121 else if ( myDCB.Parity == NOPARITY )
2122 {
2123 s_termios->c_cflag &= ~(PARODD | CMSPAR | PARENB);
2124 }
2125 } else
2126 {
2127 s_termios->c_cflag &= ~PARENB;
2128 }
2129 /* CSIZE */
2130 s_termios->c_cflag |= bytesize_to_termios( myDCB.ByteSize );
2131 /* HARDWARE_FLOW_CONTROL: hardware flow control */
2132 if (( myDCB.fOutxCtsFlow == TRUE ) ||
2133 ( myDCB.fRtsControl == RTS_CONTROL_HANDSHAKE))
2134 {
2135 s_termios->c_cflag |= HARDWARE_FLOW_CONTROL;
2136 }
2137 else
2138 {
2139 s_termios->c_cflag &= ~HARDWARE_FLOW_CONTROL;
2140 }
2141 /* MDMBUF: carrier based flow control of output */
2142 /* CIGNORE: tcsetattr will ignore control modes & baudrate */
2143
2144 /***** NOT SUPPORTED: local mode flags (c_lflag) *****/
2145 /* ICANON: canonical (not raw) mode */
2146 /* ECHO: echo back to terminal */
2147 /* ECHOE: echo erase */
2148 /* ECHOPRT: hardcopy echo erase */
2149 /* ECHOK: show KILL char */
2150 /* ECHOKE: BSD ECHOK */
2151 /* ECHONL: ICANON only: echo newline even with no ECHO */
2152 /* ECHOCTL: if ECHO, then control-A are printed as '^A' */
2153 /* ISIG: recognize INTR, QUIT & SUSP */
2154 /* IEXTEN: implmentation defined */
2155 /* NOFLSH: dont clear i/o queues on INTR, QUIT or SUSP */
2156 /* TOSTOP: background process generate SIGTTOU */
2157 /* ALTWERASE: alt-w erase distance */
2158 /* FLUSHO: user DISCARD char */
2159 /* NOKERNINFO: disable STATUS char */
2160 /* PENDIN: input line needsd reprinting, set by REPRINT char */
2161 /***** END - NOT SUPPORTED *****/
2162
2163 /***** control characters (c_cc[NCCS] ) *****/
2164
2165 if ( !GetCommTimeouts( index->hComm, &timeouts ) )
2166 {
2167 YACK();
2168 report( "GetCommTimeouts\n" );
2169 return -1;
2170 }
2171
2172 s_termios->c_cc[VTIME] = timeouts.ReadTotalTimeoutConstant/100;
2173 /*
2174 handled in SerialImp.c?
2175 s_termios->c_cc[VMIN] = ?
2176 */
2177
2178 s_termios->c_cc[VSTART] = myDCB.XonChar;
2179 s_termios->c_cc[VSTOP] = myDCB.XoffChar;
2180 s_termios->c_cc[VEOF] = myDCB.EofChar;
2181
2182 #ifdef DEBUG_VERBOSE
2183 sprintf( message,
2184 "tcgetattr: VTIME:%d, VMIN:%d\n", s_termios->c_cc[VTIME],
2185 s_termios->c_cc[VMIN] );
2186 report( message );
2187 #endif /* DEBUG_VERBOSE */
2188
2189 /***** line discipline ( c_line ) ( == c_cc[33] ) *****/
2190
2191 DCB_to_termios( &myDCB, s_termios ); /* baudrate */
2192 LEAVE( "tcgetattr" );
2193 return 0;
2194 }
2195
2196 /*
2197 `TCSANOW'
2198 Make the change immediately.
2199
2200 `TCSADRAIN'
2201 Make the change after waiting until all queued output has
2202 been written. You should usually use this option when
2203 changing parameters that affect output.
2204
2205 `TCSAFLUSH'
2206 This is like `TCSADRAIN', but also discards any queued input.
2207
2208 `TCSASOFT'
2209 This is a flag bit that you can add to any of the above
2210 alternatives. Its meaning is to inhibit alteration of the
2211 state of the terminal hardware. It is a BSD extension; it is
2212 only supported on BSD systems and the GNU system.
2213
2214 Using `TCSASOFT' is exactly the same as setting the `CIGNORE'
2215 bit in the `c_cflag' member of the structure TERMIOS-P points
2216 to. *Note Control Modes::, for a description of `CIGNORE'.
2217 */
2218
2219 /*----------------------------------------------------------
2220 tcsetattr()
2221
2222 accept:
2223 perform:
2224 return:
2225 exceptions:
2226 win32api: GetCommState(), GetCommTimeouts(), SetCommState(),
2227 SetCommTimeouts()
2228 comments:
2229 ----------------------------------------------------------*/
2230 int tcsetattr( int fd, int when, struct termios *s_termios )
2231 {
2232 int vtime;
2233 DCB dcb;
2234 COMMTIMEOUTS timeouts;
2235 struct termios_list *index;
2236
2237 ENTER( "tcsetattr" );
2238 if ( fd <= 0 )
2239 return 0;
2240 index = find_port( fd );
2241 if ( !index )
2242 {
2243 LEAVE( "tcsetattr" );
2244 return -1;
2245 }
2246 fflush( stdout );
2247 if ( s_termios->c_lflag & ICANON )
2248 {
2249 report( "tcsetattr: no canonical mode support\n" );
2250 /* and all other c_lflags too */
2251 return -1;
2252 }
2253 if ( !GetCommState( index->hComm, &dcb ) )
2254 {
2255 YACK();
2256 report( "tcsetattr:GetCommState\n" );
2257 return -1;
2258 }
2259 if ( !GetCommTimeouts( index->hComm, &timeouts ) )
2260 {
2261 YACK();
2262 report( "tcsetattr:GetCommTimeouts\n" );
2263 return -1;
2264 }
2265
2266 /*** control flags, c_cflag **/
2267 if ( !( s_termios->c_cflag & CIGNORE ) )
2268 {
2269 dcb.fParity=1;
2270 /* CIGNORE: ignore control modes and baudrate */
2271 /* baudrate */
2272 if ( termios_to_DCB( s_termios, &dcb ) < 0 ) return -1;
2273 }
2274 else
2275 {
2276 }
2277
2278 /*** input flags, c_iflag **/
2279 /* This is wrong. It disables Parity FIXME
2280 if( ( s_termios->c_iflag & INPCK ) && !( s_termios->c_iflag & IGNPAR ) )
2281 {
2282 dcb.fParity = TRUE;
2283 } else
2284 {
2285 dcb.fParity = FALSE;
2286 }
2287 */
2288 /* not in win95?
2289 Some years later...
2290 eww.. FIXME This is used for changing the Parity
2291 error character
2292
2293 I think this code is hosed. See VEOF below
2294
2295 Trent
2296 */
2297
2298 if ( s_termios->c_iflag & ISTRIP ) dcb.fBinary = FALSE;
2299 /* ISTRIP: strip to seven bits */
2300 else dcb.fBinary = TRUE;
2301
2302 /* FIXME: IGNBRK: ignore break */
2303 /* FIXME: BRKINT: interrupt on break */
2304 if ( s_termios->c_iflag & IXON )
2305 {
2306 dcb.fOutX = TRUE;
2307 }
2308 else
2309 {
2310 dcb.fOutX = FALSE;
2311 }
2312 if ( s_termios->c_iflag & IXOFF )
2313 {
2314 dcb.fInX = TRUE;
2315 }
2316 else
2317 {
2318 dcb.fInX = FALSE;
2319 }
2320 dcb.fTXContinueOnXoff = ( s_termios->c_iflag & IXANY ) ? TRUE : FALSE;
2321 /* FIXME: IMAXBEL: if input buffer full, send bell */
2322
2323 /* no DTR control in termios? */
2324 dcb.fDtrControl = DTR_CONTROL_ENABLE;
2325 /* no DSR control in termios? */
2326 dcb.fOutxDsrFlow = FALSE;
2327 /* DONT ignore rx bytes when DSR is OFF */
2328 dcb.fDsrSensitivity = FALSE;
2329 dcb.XonChar = s_termios->c_cc[VSTART];
2330 dcb.XoffChar = s_termios->c_cc[VSTOP];
2331 dcb.XonLim = 0; /* ? */
2332 dcb.XoffLim = 0; /* ? */
2333 dcb.EofChar = s_termios->c_cc[VEOF];
2334 if( dcb.EofChar != '\0' )
2335 {
2336 dcb.fBinary = 0;
2337 }
2338 else
2339 {
2340 dcb.fBinary = 1;
2341 }
2342 if ( EV_BREAK|EV_CTS|EV_DSR|EV_ERR|EV_RING | ( EV_RLSD & EV_RXFLAG ) )
2343 dcb.EvtChar = '\n';
2344 else
2345 dcb.EvtChar = '\0';
2346
2347 if ( !SetCommState( index->hComm, &dcb ) )
2348 {
2349 report( "SetCommState error\n" );
2350 YACK();
2351 return -1;
2352 }
2353
2354 #ifdef DEBUG_VERBOSE
2355 sprintf( message, "VTIME:%d, VMIN:%d\n", s_termios->c_cc[VTIME],
2356 s_termios->c_cc[VMIN] );
2357 report( message );
2358 #endif /* DEBUG_VERBOSE */
2359 vtime = s_termios->c_cc[VTIME] * 100;
2360 timeouts.ReadTotalTimeoutConstant = vtime;
2361 timeouts.ReadIntervalTimeout = 0;
2362 timeouts.ReadTotalTimeoutMultiplier = 0;
2363
2364 timeouts.WriteTotalTimeoutConstant = vtime;
2365 timeouts.WriteTotalTimeoutMultiplier = 0;
2366 /* max between bytes */
2367 if ( s_termios->c_cc[VMIN] > 0 && vtime > 0 )
2368 {
2369 /* read blocks forever on VMIN chars */
2370 } else if ( s_termios->c_cc[VMIN] == 0 && vtime == 0 )
2371 {
2372 /* read returns immediately */
2373 timeouts.ReadIntervalTimeout = MAXDWORD;
2374 timeouts.ReadTotalTimeoutConstant = 0;
2375 timeouts.ReadTotalTimeoutMultiplier = 0;
2376 }
2377 #ifdef DEBUG_VERBOSE
2378 sprintf( message, "ReadIntervalTimeout=%ld\n",
2379 timeouts.ReadIntervalTimeout );
2380 report( message );
2381 sprintf( message, "c_cc[VTIME] = %d, c_cc[VMIN] = %d\n",
2382 s_termios->c_cc[VTIME], s_termios->c_cc[VMIN] );
2383 report( message );
2384 sprintf( message, "ReadTotalTimeoutConstant: %ld\n",
2385 timeouts.ReadTotalTimeoutConstant );
2386 report( message );
2387 sprintf( message, "ReadIntervalTimeout : %ld\n",
2388 timeouts.ReadIntervalTimeout );
2389 report( message );
2390 sprintf( message, "ReadTotalTimeoutMultiplier: %ld\n",
2391 timeouts.ReadTotalTimeoutMultiplier );
2392 report( message );
2393 #endif /* DEBUG_VERBOSE */
2394 if ( !SetCommTimeouts( index->hComm, &timeouts ) )
2395 {
2396 YACK();
2397 report( "SetCommTimeouts\n" );
2398 return -1;
2399 }
2400 memcpy( index->ttyset, s_termios, sizeof( struct termios ) );
2401 LEAVE( "tcsetattr" );
2402 return 0;
2403 }
2404
2405 /*----------------------------------------------------------
2406 tcsendbreak()
2407
2408 accept:
2409 perform:
2410 return:
2411 exceptions:
2412 win32api: None
2413 comments:
2414 break for duration*0.25 seconds or
2415 0.25 seconds if duration = 0.
2416 ----------------------------------------------------------*/
2417
2418 int tcsendbreak( int fd, int duration )
2419 {
2420 struct termios_list *index;
2421 COMSTAT Stat;
2422
2423 ENTER( "tcsendbreak" );
2424
2425 index = find_port( fd );
2426
2427 if ( !index )
2428 {
2429 LEAVE( "tcdrain" );
2430 return -1;
2431 }
2432
2433 if ( duration <= 0 ) duration = 1;
2434
2435 if( !SetCommBreak( index->hComm ) )
2436 ClearErrors( index, &Stat );
2437 /* 0.25 seconds == 250000 usec */
2438 usleep( duration * 250000 );
2439 if( !ClearCommBreak( index->hComm ) )
2440 ClearErrors( index, &Stat );
2441 LEAVE( "tcsendbreak" );
2442 return 1;
2443 }
2444
2445 /*----------------------------------------------------------
2446 tcdrain()
2447
2448 accept: file descriptor
2449 perform: wait for ouput to be written.
2450 return: 0 on success, -1 otherwise
2451 exceptions: None
2452 win32api: FlushFileBuffers
2453 comments:
2454 ----------------------------------------------------------*/
2455
2456 int tcdrain ( int fd )
2457 {
2458 struct termios_list *index;
2459 char message[80];
2460 int old_flag;
2461
2462 ENTER( "tcdrain" );
2463 index = find_port( fd );
2464
2465 if ( !index )
2466 {
2467 LEAVE( "tcdrain" );
2468 return -1;
2469 }
2470 old_flag = index->event_flag;
2471 /*
2472 index->event_flag &= ~EV_TXEMPTY;
2473 SetCommMask( index->hComm, index->event_flag );
2474 index->tx_happened = 1;
2475 */
2476 if ( !FlushFileBuffers( index->hComm ) )
2477 {
2478 /* FIXME Need to figure out what the various errors are in
2479 windows. YACK() should report them and we can
2480 handle them as we find them
2481
2482
2483 Something funky is happening on NT. GetLastError =
2484 0.
2485 */
2486 sprintf( message, "FlushFileBuffers() %i\n",
2487 (int) GetLastError() );
2488 report( message );
2489 if( GetLastError() == 0 )
2490 {
2491 set_errno( 0 );
2492 return(0);
2493 }
2494 set_errno( EAGAIN );
2495 YACK();
2496 LEAVE( "tcdrain" );
2497 return -1;
2498 }
2499 /*
2500 sprintf( message, "FlushFileBuffers() %i\n",
2501 (int) GetLastError() );
2502 report( message );
2503 */
2504 LEAVE( "tcdrain success" );
2505 index->event_flag |= EV_TXEMPTY;
2506 SetCommMask( index->hComm, index->event_flag );
2507 index->event_flag = old_flag;
2508 /*
2509 index->tx_happened = 1;
2510 */
2511 return 0;
2512 }
2513
2514 /*----------------------------------------------------------
2515 tcflush()
2516
2517 accept: file descriptor, queue_selector
2518 perform: discard data not transmitted or read
2519 TCIFLUSH: flush data not read
2520 TCOFLUSH: flush data not transmitted
2521 TCIOFLUSH: flush both
2522 return: 0 on success, -1 on error
2523 exceptions: none
2524 win32api: PurgeComm
2525 comments:
2526 ----------------------------------------------------------*/
2527
2528 int tcflush( int fd, int queue_selector )
2529 {
2530 struct termios_list *index;
2531 int old_flag;
2532
2533 index = find_port( fd );
2534 if( !index)
2535 {
2536 LEAVE( "tclflush" );
2537 return(-1);
2538 }
2539
2540 old_flag = index->event_flag;
2541 /*
2542 index->event_flag &= ~EV_TXEMPTY;
2543 SetCommMask( index->hComm, index->event_flag );
2544 index->tx_happened = 1;
2545 */
2546 ENTER( "tcflush" );
2547 if ( !index )
2548 {
2549 LEAVE( "tcflush" );
2550 return -1;
2551 }
2552
2553 index->tx_happened = 1;
2554 switch( queue_selector )
2555 {
2556 case TCIFLUSH:
2557 if ( !PurgeComm( index->hComm, PURGE_RXABORT ) )
2558 {
2559 goto fail;
2560 }
2561 break;
2562 case TCOFLUSH:
2563 if ( !PurgeComm( index->hComm, PURGE_TXABORT ) )
2564 {
2565 goto fail;
2566 }
2567 break;
2568 case TCIOFLUSH:
2569 if ( !PurgeComm( index->hComm, PURGE_TXABORT ) )
2570 {
2571 goto fail;
2572 }
2573 if ( !PurgeComm( index->hComm, PURGE_RXABORT ) )
2574 {
2575 goto fail;
2576 }
2577 break;
2578 default:
2579 /*
2580 set_errno( ENOTSUP );
2581 */
2582 report( "tcflush: Unknown queue_selector\n" );
2583 LEAVE( "tcflush" );
2584 return -1;
2585 }
2586 index->event_flag |= EV_TXEMPTY;
2587 SetCommMask( index->hComm, index->event_flag );
2588 index->event_flag = old_flag;
2589 index->tx_happened = 1;
2590 LEAVE( "tcflush" );
2591 return( 0 );
2592
2593 /* FIXME Need to figure out what the various errors are in
2594 windows. YACK() should report them and we can
2595 handle them as we find them
2596 */
2597
2598 fail:
2599 LEAVE( "tcflush" );
2600 set_errno( EAGAIN );
2601 YACK();
2602 return -1;
2603
2604 }
2605
2606 /*----------------------------------------------------------
2607 tcflow()
2608
2609 accept:
2610 perform:
2611 return:
2612 exceptions:
2613 win32api: None
2614 comments: FIXME
2615 ----------------------------------------------------------*/
2616
2617 int tcflow( int fd, int action )
2618 {
2619 ENTER( "tcflow" );
2620 switch ( action )
2621 {
2622 /* Suspend transmission of output */
2623 case TCOOFF: break;
2624 /* Restart transmission of output */
2625 case TCOON: break;
2626 /* Transmit a STOP character */
2627 case TCIOFF: break;
2628 /* Transmit a START character */
2629 case TCION: break;
2630 default: return -1;
2631 }
2632 LEAVE( "tcflow" );
2633 return 1;
2634 }
2635 /*----------------------------------------------------------
2636 fstat()
2637
2638 accept:
2639 perform:
2640 return:
2641 exceptions:
2642 win32api:
2643 comments: this is just to keep the eventLoop happy.
2644 ----------------------------------------------------------*/
2645
2646 int fstat( int fd, ... )
2647 {
2648 return( 0 );
2649 }
2650 /*----------------------------------------------------------
2651 ioctl()
2652
2653 accept:
2654 perform:
2655 return:
2656 exceptions:
2657 win32api: GetCommError(), GetCommModemStatus, EscapeCommFunction()
2658 comments: FIXME
2659 the DCB struct is:
2660
2661 typedef struct _DCB
2662 {
2663 unsigned long DCBlength, BaudRate, fBinary:1, fParity:1;
2664 unsigned long fOutxCtsFlow:1, fOutxDsrFlow:1, fDtrControl:2;
2665 unsigned long fDsrSensitivity:1, fTXContinueOnXoff:1;
2666 unsigned long fOutX:1, fInX:1, fErrorChar:1, fNull:1;
2667 unsigned long fRtsControl:2, fAbortOnError:1, fDummy2:17;
2668 WORD wReserved, XonLim, XoffLim;
2669 BYTE ByteSize, Parity, StopBits;
2670 char XonChar, XoffChar, ErrorChar, EofChar, EvtChar;
2671 WORD wReserved1;
2672 } DCB;
2673
2674 ----------------------------------------------------------*/
2675
2676 int ioctl( int fd, int request, ... )
2677 {
2678 unsigned long dwStatus = 0;
2679 va_list ap;
2680 int *arg, ret, result, old_flag;
2681 char message[80];
2682
2683 #ifdef TIOCGSERIAL
2684 DCB *dcb;
2685 struct serial_struct *sstruct;
2686 #endif /* TIOCGSERIAL */
2687 COMSTAT Stat;
2688
2689 struct termios_list *index;
2690 struct async_struct *astruct;
2691 struct serial_multiport_struct *mstruct;
2692 #ifdef TIOCGICOUNT
2693 struct serial_icounter_struct *sistruct;
2694 #endif /* TIOCGICOUNT */
2695
2696 ENTER( "ioctl" );
2697 if ( fd <= 0 )
2698 return 0;
2699 index = find_port( fd );
2700 if ( !index )
2701 {
2702 LEAVE( "ioctl" );
2703 return -1;
2704 }
2705
2706 va_start( ap, request );
2707
2708 ret = ClearErrors( index, &Stat );
2709 if (ret == 0)
2710 {
2711 set_errno( EBADFD );
2712 YACK();
2713 report( "ClearError Failed! ernno EBADFD" );
2714 arg = va_arg( ap, int * );
2715 va_end( ap );
2716 return -1;
2717 }
2718 switch( request )
2719 {
2720 case TCSBRK:
2721 arg = va_arg( ap, int * );
2722 va_end( ap );
2723 return -ENOIOCTLCMD;
2724 case TCSBRKP:
2725 arg = va_arg( ap, int * );
2726 va_end( ap );
2727 return -ENOIOCTLCMD;
2728 case TIOCGSOFTCAR:
2729 arg = va_arg( ap, int * );
2730 va_end( ap );
2731 return -ENOIOCTLCMD;
2732 case TIOCSSOFTCAR:
2733 arg = va_arg( ap, int * );
2734 va_end( ap );
2735 return -ENOIOCTLCMD;
2736
2737 case TIOCMGET:
2738 arg = va_arg( ap, int * );
2739 /* DORITOS */
2740 if ( !GetCommModemStatus( index->hComm, &dwStatus ) )
2741 report_error( "GetCommMOdemStatus failed!\n" );
2742 if ( dwStatus & MS_RLSD_ON ) *arg |= TIOCM_CAR;
2743 else *arg &= ~TIOCM_CAR;
2744 if ( dwStatus & MS_RING_ON ) *arg |= TIOCM_RNG;
2745 else *arg &= ~TIOCM_RNG;
2746 if ( dwStatus & MS_DSR_ON ) *arg |= TIOCM_DSR;
2747 else *arg &= ~TIOCM_DSR;
2748 if ( dwStatus & MS_CTS_ON ) *arg |= TIOCM_CTS;
2749 else *arg &= ~TIOCM_CTS;
2750 /* I'm not seeing a way to read the MSR directly
2751 we store the state using TIOCM_*
2752
2753 Trent
2754 */
2755 if ( index->MSR & TIOCM_DTR )
2756 *arg |= TIOCM_DTR;
2757 else *arg &= ~TIOCM_DTR;
2758 if ( index->MSR & TIOCM_RTS )
2759 *arg |= TIOCM_RTS;
2760 else *arg &= ~TIOCM_RTS;
2761
2762 /*
2763
2764 TIOCM_LE
2765 TIOCM_ST
2766 TIOCM_SR
2767 */
2768 va_end( ap );
2769 return( 0 );
2770 /* TIOCMIS, TIOCMBIC and TIOCMSET all do the same thing... */
2771 case TIOCMBIS:
2772 arg = va_arg( ap, int * );
2773 va_end( ap );
2774 return -ENOIOCTLCMD;
2775 case TIOCMBIC:
2776 arg = va_arg( ap, int * );
2777 va_end( ap );
2778 return -ENOIOCTLCMD;
2779 case TIOCMSET:
2780 arg = va_arg( ap, int * );
2781 if (( *arg & TIOCM_DTR) == (index->MSR & TIOCM_DTR) )
2782 {
2783 report( "DTR is unchanged\n" );
2784 }
2785 sprintf(message, "DTR %i %i\n", *arg&TIOCM_DTR, index->MSR & TIOCM_DTR );
2786 report( message );
2787 if ( *arg & TIOCM_DTR )
2788 {
2789 index->MSR |= TIOCM_DTR;
2790 }
2791 else
2792 {
2793 index->MSR &= ~TIOCM_DTR;
2794 }
2795 if ( EscapeCommFunction( index->hComm,
2796 ( *arg & TIOCM_DTR ) ? SETDTR :
2797 CLRDTR ) )
2798 report( "EscapeCommFunction: True\n" );
2799 else
2800 report( "EscapeCommFunction: False\n" );
2801 if ( (*arg & TIOCM_RTS) == ( index->MSR & TIOCM_RTS) )
2802 {
2803 report( "RTS is unchanged\n" );
2804 }
2805 sprintf( message, "RTS %i %i\n", *arg&TIOCM_RTS, index->MSR & TIOCM_RTS );
2806 report( message );
2807 if ( *arg & TIOCM_RTS )
2808 {
2809 index->MSR |= TIOCM_RTS;
2810 result &= SETRTS;
2811 }
2812 else
2813 {
2814 index->MSR &= ~TIOCM_RTS;
2815 result &= CLRRTS;
2816 }
2817 if( EscapeCommFunction( index->hComm,
2818 ( *arg & TIOCM_RTS ) ? SETRTS : CLRRTS ) )
2819 report( "EscapeCommFunction: True\n" );
2820 else
2821 report( "EscapeCommFunction: False\n" );
2822 break;
2823
2824 #ifdef TIOCGSERIAL
2825 case TIOCGSERIAL:
2826 report( "TIOCGSERIAL\n" );
2827
2828 dcb = malloc( sizeof( DCB ) );
2829 if( !dcb )
2830 {
2831 va_end( ap );
2832 return -1;
2833 }
2834 memset( dcb, 0, sizeof( DCB ) );
2835 GetCommState( index->hComm, dcb );
2836
2837 sstruct = va_arg( ap, struct serial_struct * );
2838 if ( DCB_to_serial_struct( dcb, sstruct ) < 0 )
2839 {
2840 va_end( ap );
2841 return -1;
2842 }
2843 index->sstruct = sstruct;
2844
2845 report( "TIOCGSERIAL\n" );
2846 free(dcb);
2847 break;
2848
2849 #endif /* TIOCGSERIAL */
2850 #ifdef TIOCSSERIAL
2851 case TIOCSSERIAL:
2852 report( "TIOCSSERIAL\n" );
2853
2854 dcb = malloc( sizeof( DCB ) );
2855 if( !dcb )
2856 {
2857 va_end( ap );
2858 return -1;
2859 }
2860 memset( dcb, 0, sizeof( DCB ) );
2861 GetCommState( index->hComm, dcb );
2862
2863 index->sstruct = va_arg( ap, struct serial_struct * );
2864
2865 report( "TIOCSSERIAL\n" );
2866 free(dcb);
2867 break;
2868
2869 #endif /* TIOCSSERIAL */
2870 case TIOCSERCONFIG:
2871 case TIOCSERGETLSR:
2872 arg = va_arg( ap, int * );
2873 /*
2874 do {
2875 wait = WaitForSingleObject( index->sol.hEvent, 5000 );
2876 } while ( wait == WAIT_TIMEOUT );
2877 */
2878 ret = ClearErrors( index, &Stat );
2879 if ( ret == 0 )
2880 {
2881 /* FIXME ? */
2882 set_errno( EBADFD );
2883 YACK();
2884 report( "TIOCSERGETLSR EBADFD" );
2885 va_end( ap );
2886 return -1;
2887 }
2888 if ( (int ) Stat.cbOutQue == 0 )
2889 {
2890 /* output is empty */
2891 if( index->tx_happened == 1 )
2892 {
2893 old_flag = index->event_flag;
2894 index->event_flag &= ~EV_TXEMPTY;
2895 SetCommMask( index->hComm,
2896 index->event_flag );
2897 index->event_flag = old_flag;
2898 *arg = 1;
2899 index->tx_happened = 0;
2900 report( "ioctl: ouput empty\n" );
2901 }
2902 else
2903 {
2904 *arg = 0;
2905 }
2906 ret = 0;
2907 }
2908 else
2909 {
2910 /* still data out there */
2911 *arg = 0;
2912 ret = 0;
2913 }
2914 va_end( ap );
2915 return(0);
2916 break;
2917 case TIOCSERGSTRUCT:
2918 astruct = va_arg( ap, struct async_struct * );
2919 va_end( ap );
2920 return -ENOIOCTLCMD;
2921 case TIOCSERGETMULTI:
2922 mstruct = va_arg( ap, struct serial_multiport_struct * );
2923 va_end( ap );
2924 return -ENOIOCTLCMD;
2925 case TIOCSERSETMULTI:
2926 mstruct = va_arg( ap, struct serial_multiport_struct * );
2927 va_end( ap );
2928 return -ENOIOCTLCMD;
2929 case TIOCMIWAIT:
2930 arg = va_arg( ap, int * );
2931 va_end( ap );
2932 return -ENOIOCTLCMD;
2933 /*
2934 On linux this fills a struct with all the line info
2935 (data available, bytes sent, ...
2936 */
2937 #ifdef TIOCGICOUNT
2938 case TIOCGICOUNT:
2939 sistruct= va_arg( ap, struct serial_icounter_struct * );
2940 ret = ClearErrors( index, &Stat );
2941 if ( ret == 0 )
2942 {
2943 /* FIXME ? */
2944 report( "TIOCGICOUNT failed\n" );
2945 set_errno( EBADFD );
2946 va_end( ap );
2947 return -1;
2948 }
2949 if( sistruct->frame != index->sis->frame )
2950 {
2951 sistruct->frame = index->sis->frame;
2952 /*
2953 printf( "---------------frame = %i\n", sistruct->frame++ );
2954 */
2955 }
2956 if( sistruct->overrun != index->sis->overrun )
2957 {
2958 /*
2959 printf( "---------------overrun\n" );
2960 */
2961 sistruct->overrun = index->sis->overrun;
2962 /* ErrCode &= ~CE_OVERRUN; */
2963 }
2964 if( sistruct->parity != index->sis->parity )
2965 {
2966 /*
2967 printf( "---------------parity\n" );
2968 */
2969 sistruct->parity = index->sis->parity;
2970 }
2971 if( sistruct->brk != index->sis->brk )
2972 {
2973 /*
2974 printf( "---------------brk\n" );
2975 */
2976 sistruct->brk = index->sis->brk;
2977 }
2978 va_end( ap );
2979 return 0;
2980 /* abolete ioctls */
2981 #endif /* TIOCGICOUNT */
2982 case TIOCSERGWILD:
2983 case TIOCSERSWILD:
2984 report( "TIOCSER[GS]WILD absolete\n" );
2985 va_end( ap );
2986 return 0;
2987 /* number of bytes available for reading */
2988 case FIONREAD:
2989 arg = va_arg( ap, int * );
2990 ret = ClearErrors( index, &Stat );
2991 if ( ret == 0 )
2992 {
2993 /* FIXME ? */
2994 report( "FIONREAD failed\n" );
2995 set_errno( EBADFD );
2996 va_end( ap );
2997 return -1;
2998 }
2999 *arg = ( int ) Stat.cbInQue;
3000 #ifdef DEBUG_VERBOSE
3001 sprintf( message, "FIONREAD: %i bytes available\n",
3002 (int) Stat.cbInQue );
3003 report( message );
3004 if( *arg )
3005 {
3006 sprintf( message, "FIONREAD: %i\n", *arg );
3007 report( message );
3008 }
3009 #endif /* DEBUG_VERBOSE */
3010 ret = 0;
3011 break;
3012
3013 /* pending bytes to be sent */
3014 case TIOCOUTQ:
3015 arg = va_arg( ap, int * );
3016 va_end( ap );
3017 return -ENOIOCTLCMD;
3018 default:
3019 sprintf( message,
3020 "FIXME: ioctl: unknown request: %#x\n",
3021 request );
3022 report( message );
3023 va_end( ap );
3024 return -ENOIOCTLCMD;
3025 }
3026 va_end( ap );
3027 LEAVE( "ioctl" );
3028 return 0;
3029 }
3030
3031 /*----------------------------------------------------------
3032 fcntl()
3033
3034 accept:
3035 perform:
3036 return:
3037 exceptions:
3038 win32api: None
3039 comments: FIXME
3040 ----------------------------------------------------------*/
3041
3042 int fcntl( int fd, int command, ... )
3043 {
3044 int arg, ret = 0;
3045 va_list ap;
3046 struct termios_list *index;
3047 char message[80];
3048
3049 ENTER( "fcntl" );
3050 if ( fd <= 0 )
3051 return 0;
3052 index = find_port( fd );
3053 if ( !index )
3054 {
3055 LEAVE( "fcntl" );
3056 return -1;
3057 }
3058
3059 va_start( ap, command );
3060
3061 arg = va_arg( ap, int );
3062 switch ( command )
3063 {
3064 case F_SETOWN: /* set ownership of fd */
3065 break;
3066 case F_SETFL: /* set operating flags */
3067 #ifdef DEBUG
3068 sprintf( message, "F_SETFL fd=%d flags=%d\n", fd, arg );
3069 report( message );
3070 #endif
3071 index->open_flags = arg;
3072 break;
3073 case F_GETFL: /* get operating flags */
3074 ret = index->open_flags;
3075 break;
3076 default:
3077 sprintf( message, "unknown fcntl command %#x\n", command );
3078 report( message );
3079 break;
3080 }
3081
3082 va_end( ap );
3083 LEAVE( "fcntl" );
3084 return ret;
3085 }
3086
3087 /*----------------------------------------------------------
3088 termios_interrupt_event_loop()
3089
3090 accept:
3091 perform:
3092 return: let Serial_select break out so the thread can die
3093 exceptions:
3094 win32api:
3095 comments:
3096 ----------------------------------------------------------*/
3097 void termios_interrupt_event_loop( int fd, int flag )
3098 {
3099 struct termios_list * index = find_port( fd );
3100 if ( !index )
3101 {
3102 LEAVE( "termios_interrupt_event_loop" );
3103 return;
3104 }
3105 /*
3106 index->event_flag = 0;
3107 TRENT SetCommMask( index->hComm, index->event_flag );
3108 usleep(2000);
3109 tcdrain( index->fd );
3110 SetEvent( index->sol.hEvent );
3111 */
3112 index->interrupt = flag;
3113 return;
3114 }
3115
3116 /*----------------------------------------------------------
3117 Serial_select()
3118
3119 accept:
3120 perform:
3121 return: number of fd's changed on success or -1 on error.
3122 exceptions:
3123 win32api: SetCommMask(), GetCommEvent(), WaitSingleObject()
3124 comments:
3125 ----------------------------------------------------------*/
3126 #ifndef __LCC__
3127 int serial_select( int n, fd_set *readfds, fd_set *writefds,
3128 fd_set *exceptfds, struct timeval *timeout )
3129 {
3130
3131 unsigned long dwCommEvent, wait = WAIT_TIMEOUT;
3132 int fd = n-1;
3133 struct termios_list *index;
3134 char message[80];
3135 COMSTAT Stat;
3136 int ret;
3137
3138 ENTER( "serial_select" );
3139 if ( fd <= 0 )
3140 {
3141 /* Baby did a bad baad thing */
3142 goto fail;
3143 }
3144
3145 index = find_port( fd );
3146 if ( !index || !index->event_flag )
3147 {
3148 /* still setting up the port? hold off for a Sec so
3149 things can fire up
3150
3151 this does happen. loops ~twice on a 350 Mzh with
3152 usleep(1000000)
3153 */
3154 usleep(10000);
3155 return(0);
3156 }
3157
3158 ResetEvent( index->wol.hEvent );
3159 ResetEvent( index->sol.hEvent );
3160 ResetEvent( index->rol.hEvent );
3161 ret = ClearErrors( index, &Stat );
3162 if (ret == 0) goto fail;
3163 while ( wait == WAIT_TIMEOUT && index->sol.hEvent )
3164 {
3165 if( index->interrupt == 1 )
3166 {
3167 goto fail;
3168 }
3169 SetCommMask( index->hComm, index->event_flag );
3170 ClearErrors( index, &Stat );
3171 if ( !WaitCommEvent( index->hComm, &dwCommEvent,
3172 &index->sol ) )
3173 {
3174 /* WaitCommEvent failed probably overlapped though */
3175 if ( GetLastError() != ERROR_IO_PENDING )
3176 {
3177 ClearErrors( index, &Stat );
3178 goto fail;
3179 }
3180 /* thought so... */
3181 }
3182 /* could use the select timeout here but it should not
3183 be needed
3184 */
3185 ClearErrors( index, &Stat );
3186 wait = WaitForSingleObject( index->sol.hEvent, 100 );
3187 switch ( wait )
3188 {
3189 case WAIT_OBJECT_0:
3190 goto end;
3191 case WAIT_TIMEOUT:
3192 goto timeout;
3193 case WAIT_ABANDONED:
3194 default:
3195 goto fail;
3196
3197 }
3198 }
3199 end:
3200 /* You may want to chop this out for lower latency */
3201 usleep(1000);
3202 LEAVE( "serial_select" );
3203 return( 1 );
3204 timeout:
3205 LEAVE( "serial_select" );
3206 return( 0 );
3207 fail:
3208 YACK();
3209 sprintf( message, "< select called error %i\n", n );
3210 report( message );
3211 errno = EBADFD;
3212 LEAVE( "serial_select" );
3213 return( 1 );
3214 }
3215 #ifdef asdf
3216 int serial_select( int n, fd_set *readfds, fd_set *writefds,
3217 fd_set *exceptfds, struct timeval *timeout )
3218 {
3219
3220 unsigned long nBytes, dwCommEvent, wait = WAIT_TIMEOUT;
3221 int fd = n-1;
3222 struct termios_list *index;
3223 char message[80];
3224
3225 ENTER( "serial_select" );
3226 if ( fd <= 0 )
3227 {
3228 usleep(1000);
3229 return 1;
3230 }
3231 index = find_port( fd );
3232 if ( !index )
3233 {
3234 LEAVE( "serial_select" );
3235 return -1;
3236 }
3237 if( index->interrupt == 1 )
3238 {
3239 goto end;
3240 }
3241 while(!index->event_flag )
3242 {
3243 usleep(1000);
3244 return -1;
3245 }
3246
3247 while ( wait == WAIT_TIMEOUT && index->sol.hEvent )
3248 {
3249 if( index->interrupt == 1 )
3250 {
3251 goto end;
3252 }
3253 if( !index->sol.hEvent )
3254 {
3255 return 1;
3256 }
3257 if ( !WaitCommEvent( index->hComm, &dwCommEvent,
3258 &index->sol ) )
3259 {
3260 /* WaitCommEvent failed */
3261 if( index->interrupt == 1 )
3262 {
3263 goto end;
3264 }
3265 if ( GetLastError() != ERROR_IO_PENDING )
3266 {
3267 sprintf( message, "WaitCommEvent filename = %s\n", index->filename);
3268 report( message );
3269 return(1);
3270 /*
3271 goto fail;
3272 */
3273 }
3274 return(1);
3275 }
3276 if( index->interrupt == 1 )
3277 {
3278 goto end;
3279 }
3280 wait = WaitForSingleObject( index->sol.hEvent, 1000 );
3281 switch ( wait )
3282 {
3283 case WAIT_OBJECT_0:
3284 if( index->interrupt == 1 )
3285 {
3286 goto end;
3287 }
3288 if( !index->sol.hEvent ) return(1);
3289 if (!GetOverlappedResult( index->hComm,
3290 &index->sol, &nBytes, TRUE ))
3291 {
3292 goto end;
3293 }
3294 else if( index->tx_happened == 1 )
3295 {
3296 goto end;
3297 }
3298 else
3299 goto end;
3300 break;
3301 case WAIT_TIMEOUT:
3302 default:
3303 return(1); /* WaitFor error */
3304
3305 }
3306 }
3307 end:
3308 LEAVE( "serial_select" );
3309 return( 1 );
3310 #ifdef asdf
3311 /* FIXME this needs to be cleaned up... */
3312 fail:
3313 sprintf( message, "< select called error %i\n", n );
3314 YACK();
3315 report( message );
3316 set_errno( EBADFD );
3317 LEAVE( "serial_select" );
3318 return( 1 );
3319 #endif /* asdf */
3320
3321 }
3322 #endif /* asdf */
3323 #endif /* __LCC__ */
3324
3325 /*----------------------------------------------------------
3326 termiosSetParityError()
3327
3328 accept: fd The device opened
3329 perform: Get the Parity Error Char
3330 return: the Parity Error Char
3331 exceptions: none
3332 win32api: GetCommState()
3333 comments: No idea how to do this in Unix (handle in read?)
3334 ----------------------------------------------------------*/
3335
3336 int termiosGetParityErrorChar( int fd )
3337 {
3338 struct termios_list *index;
3339 DCB dcb;
3340
3341 ENTER( "termiosGetParityErrorChar" );
3342 index = find_port( fd );
3343 if( !index )
3344 {
3345 LEAVE( "termiosGetParityErrorChar" );
3346 return(-1);
3347 }
3348 GetCommState( index->hComm, &dcb );
3349 LEAVE( "termiosGetParityErrorChar" );
3350 return( dcb.ErrorChar );
3351 }
3352
3353 /*----------------------------------------------------------
3354 termiosSetParityError()
3355
3356 accept: fd The device opened, value the new Parity Error Char
3357 perform: Set the Parity Error Char
3358 return: void
3359 exceptions: none
3360 win32api: GetCommState(), SetCommState()
3361 comments: No idea how to do this in Unix (handle in read?)
3362 ----------------------------------------------------------*/
3363
3364 void termiosSetParityError( int fd, char value )
3365 {
3366 DCB dcb;
3367 struct termios_list *index;
3368
3369 ENTER( "termiosGetParityErrorChar" );
3370 index = find_port( fd );
3371 if ( !index )
3372 {
3373 LEAVE( "termiosSetParityError" );
3374 return;
3375 }
3376 GetCommState( index->hComm, &dcb );
3377 dcb.ErrorChar = value;
3378 SetCommState( index->hComm, &dcb );
3379 LEAVE( "termiosGetParityErrorChar" );
3380 }
3381 /*----------------------- END OF LIBRARY -----------------*/
3382 #ifdef PLAYING_AROUND
3383 static inline int inportb( int index )
3384 {
3385 unsigned char value;
3386 __asm__ volatile ( "inb %1,%0"
3387 : "=a" (value)
3388 : "d" ((unsigned short)index) );
3389 return value;
3390 }
3391
3392 static inline void outportb(unsigned char val, unsigned short int index)
3393 {
3394 __asm__ volatile (
3395 "outb %0,%1\n"
3396 :
3397 : "a" (val), "d" (index)
3398 );
3399 }
3400 #endif /* PLAYING_AROUND */