1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include "gdb_string.h"
33 #include "event-loop.h"
37 struct hardwire_ttystate
39 struct termios termios
;
45 /* It is believed that all systems which have added job control to SVR3
46 (e.g. sco) have also added termios. Even if not, trying to figure out
47 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
48 bewildering. So we don't attempt it. */
50 struct hardwire_ttystate
57 struct hardwire_ttystate
62 /* Line discipline flags. */
67 static int hardwire_open (serial_t scb
, const char *name
);
68 static void hardwire_raw (serial_t scb
);
69 static int wait_for (serial_t scb
, int timeout
);
70 static int hardwire_readchar (serial_t scb
, int timeout
);
71 static int do_hardwire_readchar (serial_t scb
, int timeout
);
72 static int generic_readchar (serial_t scb
, int timeout
, int (*do_readchar
) (serial_t scb
, int timeout
));
73 static int rate_to_code (int rate
);
74 static int hardwire_setbaudrate (serial_t scb
, int rate
);
75 static void hardwire_close (serial_t scb
);
76 static int get_tty_state (serial_t scb
, struct hardwire_ttystate
* state
);
77 static int set_tty_state (serial_t scb
, struct hardwire_ttystate
* state
);
78 static serial_ttystate
hardwire_get_tty_state (serial_t scb
);
79 static int hardwire_set_tty_state (serial_t scb
, serial_ttystate state
);
80 static int hardwire_noflush_set_tty_state (serial_t
, serial_ttystate
,
82 static void hardwire_print_tty_state (serial_t
, serial_ttystate
, struct ui_file
*);
83 static int hardwire_drain_output (serial_t
);
84 static int hardwire_flush_output (serial_t
);
85 static int hardwire_flush_input (serial_t
);
86 static int hardwire_send_break (serial_t
);
87 static int hardwire_setstopbits (serial_t
, int);
89 static int do_unix_readchar (serial_t scb
, int timeout
);
90 static timer_handler_func push_event
;
91 static handler_func fd_event
;
92 static void reschedule (serial_t scb
);
94 void _initialize_ser_hardwire (void);
96 extern int (*ui_loop_hook
) (int);
98 /* Open up a real live device for serial I/O */
101 hardwire_open (serial_t scb
, const char *name
)
103 scb
->fd
= open (name
, O_RDWR
);
111 get_tty_state (serial_t scb
, struct hardwire_ttystate
*state
)
114 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
121 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
127 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
129 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
131 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
133 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
141 set_tty_state (serial_t scb
, struct hardwire_ttystate
*state
)
144 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
151 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
157 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
159 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
161 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
163 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
170 static serial_ttystate
171 hardwire_get_tty_state (serial_t scb
)
173 struct hardwire_ttystate
*state
;
175 state
= (struct hardwire_ttystate
*) xmalloc (sizeof *state
);
177 if (get_tty_state (scb
, state
))
180 return (serial_ttystate
) state
;
184 hardwire_set_tty_state (serial_t scb
, serial_ttystate ttystate
)
186 struct hardwire_ttystate
*state
;
188 state
= (struct hardwire_ttystate
*) ttystate
;
190 return set_tty_state (scb
, state
);
194 hardwire_noflush_set_tty_state (serial_t scb
,
195 serial_ttystate new_ttystate
,
196 serial_ttystate old_ttystate
)
198 struct hardwire_ttystate new_state
;
200 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
203 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
205 /* Don't change in or out of raw mode; we don't want to flush input.
206 termio and termios have no such restriction; for them flushing input
207 is separate from setting the attributes. */
210 if (state
->sgttyb
.sg_flags
& RAW
)
211 new_state
.sgttyb
.sg_flags
|= RAW
;
213 new_state
.sgttyb
.sg_flags
&= ~RAW
;
215 /* I'm not sure whether this is necessary; the manpage just mentions
217 if (state
->sgttyb
.sg_flags
& CBREAK
)
218 new_state
.sgttyb
.sg_flags
|= CBREAK
;
220 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
223 return set_tty_state (scb
, &new_state
);
227 hardwire_print_tty_state (serial_t scb
,
228 serial_ttystate ttystate
,
229 struct ui_file
*stream
)
231 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
235 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
236 (int) state
->termios
.c_iflag
,
237 (int) state
->termios
.c_oflag
);
238 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
239 (int) state
->termios
.c_cflag
,
240 (int) state
->termios
.c_lflag
);
242 /* This not in POSIX, and is not really documented by those systems
243 which have it (at least not Sun). */
244 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
246 fprintf_filtered (stream
, "c_cc: ");
247 for (i
= 0; i
< NCCS
; i
+= 1)
248 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
249 fprintf_filtered (stream
, "\n");
253 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
254 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
255 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
256 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
257 state
->termio
.c_line
);
258 fprintf_filtered (stream
, "c_cc: ");
259 for (i
= 0; i
< NCC
; i
+= 1)
260 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
261 fprintf_filtered (stream
, "\n");
265 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
266 state
->sgttyb
.sg_flags
);
268 fprintf_filtered (stream
, "tchars: ");
269 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
270 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
271 fprintf_filtered (stream
, "\n");
273 fprintf_filtered (stream
, "ltchars: ");
274 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
275 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
276 fprintf_filtered (stream
, "\n");
278 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
282 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
285 hardwire_drain_output (serial_t scb
)
288 return tcdrain (scb
->fd
);
292 return ioctl (scb
->fd
, TCSBRK
, 1);
296 /* Get the current state and then restore it using TIOCSETP,
297 which should cause the output to drain and pending input
300 struct hardwire_ttystate state
;
301 if (get_tty_state (scb
, &state
))
307 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
314 hardwire_flush_output (serial_t scb
)
317 return tcflush (scb
->fd
, TCOFLUSH
);
321 return ioctl (scb
->fd
, TCFLSH
, 1);
325 /* This flushes both input and output, but we can't do better. */
326 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
331 hardwire_flush_input (serial_t scb
)
333 ser_unix_flush_input (scb
);
336 return tcflush (scb
->fd
, TCIFLUSH
);
340 return ioctl (scb
->fd
, TCFLSH
, 0);
344 /* This flushes both input and output, but we can't do better. */
345 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
350 hardwire_send_break (serial_t scb
)
353 return tcsendbreak (scb
->fd
, 0);
357 return ioctl (scb
->fd
, TCSBRK
, 0);
363 struct timeval timeout
;
365 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
367 /* Can't use usleep; it doesn't exist in BSD 4.2. */
368 /* Note that if this select() is interrupted by a signal it will not wait
369 the full length of time. I think that is OK. */
371 timeout
.tv_usec
= 250000;
372 select (0, 0, 0, 0, &timeout
);
373 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
380 hardwire_raw (serial_t scb
)
382 struct hardwire_ttystate state
;
384 if (get_tty_state (scb
, &state
))
385 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
388 state
.termios
.c_iflag
= 0;
389 state
.termios
.c_oflag
= 0;
390 state
.termios
.c_lflag
= 0;
391 state
.termios
.c_cflag
&= ~(CSIZE
| PARENB
);
392 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
393 state
.termios
.c_cc
[VMIN
] = 0;
394 state
.termios
.c_cc
[VTIME
] = 0;
398 state
.termio
.c_iflag
= 0;
399 state
.termio
.c_oflag
= 0;
400 state
.termio
.c_lflag
= 0;
401 state
.termio
.c_cflag
&= ~(CSIZE
| PARENB
);
402 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
403 state
.termio
.c_cc
[VMIN
] = 0;
404 state
.termio
.c_cc
[VTIME
] = 0;
408 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
409 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
412 scb
->current_timeout
= 0;
414 if (set_tty_state (scb
, &state
))
415 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
418 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
419 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
421 For termio{s}, we actually just setup VTIME if necessary, and let the
422 timeout occur in the read() in hardwire_read().
425 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
426 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
429 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
430 possible values of the TIMEOUT parameter are ONE and ZERO.
431 Consequently all the code that tries to handle the possability of
432 an overflowed timer is unnecessary. */
435 wait_for (serial_t scb
, int timeout
)
444 /* NOTE: Some OS's can scramble the READFDS when the select()
445 call fails (ex the kernel with Red Hat 5.2). Initialize all
446 arguments before each call. */
452 FD_SET (scb
->fd
, &readfds
);
455 numfds
= select (scb
->fd
+ 1, &readfds
, 0, 0, &tv
);
457 numfds
= select (scb
->fd
+ 1, &readfds
, 0, 0, 0);
461 return SERIAL_TIMEOUT
;
462 else if (errno
== EINTR
)
465 return SERIAL_ERROR
; /* Got an error from select or poll */
469 #endif /* HAVE_SGTTY */
471 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
472 if (timeout
== scb
->current_timeout
)
475 scb
->current_timeout
= timeout
;
478 struct hardwire_ttystate state
;
480 if (get_tty_state (scb
, &state
))
481 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
487 state
.termios
.c_cc
[VTIME
] = 0;
488 state
.termios
.c_cc
[VMIN
] = 1;
492 state
.termios
.c_cc
[VMIN
] = 0;
493 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
494 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
497 /* If c_cc is an 8-bit signed character, we can't go
498 bigger than this. If it is always unsigned, we could use
501 scb
->current_timeout
= 12;
502 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
503 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
512 state
.termio
.c_cc
[VTIME
] = 0;
513 state
.termio
.c_cc
[VMIN
] = 1;
517 state
.termio
.c_cc
[VMIN
] = 0;
518 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
519 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
521 /* If c_cc is an 8-bit signed character, we can't go
522 bigger than this. If it is always unsigned, we could use
525 scb
->current_timeout
= 12;
526 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
527 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
532 if (set_tty_state (scb
, &state
))
533 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
537 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
540 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
541 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
542 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
543 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
545 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
546 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
549 /* NOTE: cagney/1999-09-16: This function is not identical to
550 ser_unix_readchar() as part of replacing it with ser_unix*()
551 merging will be required - this code handles the case where read()
552 times out due to no data while ser_unix_readchar() doesn't expect
556 do_hardwire_readchar (serial_t scb
, int timeout
)
564 /* We have to be able to keep the GUI alive here, so we break the original
565 timeout into steps of 1 second, running the "keep the GUI alive" hook
566 each time through the loop.
567 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
568 will only go through the loop once. */
570 delta
= (timeout
== 0 ? 0 : 1);
574 /* N.B. The UI may destroy our world (for instance by calling
575 remote_stop,) in which case we want to get out of here as
576 quickly as possible. It is not safe to touch scb, since
577 someone else might have freed it. The ui_loop_hook signals that
578 we should exit by returning 1. */
581 detach
= ui_loop_hook (0);
584 return SERIAL_TIMEOUT
;
586 scb
->timeout_remaining
= (timeout
< 0 ? timeout
: timeout
- delta
);
587 status
= wait_for (scb
, delta
);
592 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
598 /* Zero characters means timeout (it could also be EOF, but
599 we don't (yet at least) distinguish). */
600 if (scb
->timeout_remaining
> 0)
602 timeout
= scb
->timeout_remaining
;
605 else if (scb
->timeout_remaining
< 0)
608 return SERIAL_TIMEOUT
;
610 else if (errno
== EINTR
)
613 return SERIAL_ERROR
; /* Got an error from read */
616 scb
->bufcnt
= status
;
618 scb
->bufp
= scb
->buf
;
624 hardwire_readchar (serial_t scb
, int timeout
)
626 return generic_readchar (scb
, timeout
, do_hardwire_readchar
);
638 /* Translate baud rates from integers to damn B_codes. Unix should
639 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
739 rate_to_code (int rate
)
743 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
745 /* test for perfect macth. */
746 if (rate
== baudtab
[i
].rate
)
747 return baudtab
[i
].code
;
750 /* check if it is in between valid values. */
751 if (rate
< baudtab
[i
].rate
)
755 warning ("Invalid baud rate %d. Closest values are %d and %d.",
756 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
760 warning ("Invalid baud rate %d. Minimum value is %d.",
761 rate
, baudtab
[0].rate
);
768 /* The requested speed was too large. */
769 warning ("Invalid baud rate %d. Maximum value is %d.",
770 rate
, baudtab
[i
- 1].rate
);
775 hardwire_setbaudrate (serial_t scb
, int rate
)
777 struct hardwire_ttystate state
;
778 int baud_code
= rate_to_code (rate
);
782 /* The baud rate was not valid.
783 A warning has already been issued. */
788 if (get_tty_state (scb
, &state
))
792 cfsetospeed (&state
.termios
, baud_code
);
793 cfsetispeed (&state
.termios
, baud_code
);
801 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
802 state
.termio
.c_cflag
|= baud_code
;
806 state
.sgttyb
.sg_ispeed
= baud_code
;
807 state
.sgttyb
.sg_ospeed
= baud_code
;
810 return set_tty_state (scb
, &state
);
814 hardwire_setstopbits (serial_t scb
, int num
)
816 struct hardwire_ttystate state
;
819 if (get_tty_state (scb
, &state
))
824 case SERIAL_1_STOPBITS
:
827 case SERIAL_1_AND_A_HALF_STOPBITS
:
828 case SERIAL_2_STOPBITS
:
837 state
.termios
.c_cflag
&= ~CSTOPB
;
839 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
844 state
.termio
.c_cflag
&= ~CSTOPB
;
846 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
850 return 0; /* sgtty doesn't support this */
853 return set_tty_state (scb
, &state
);
857 hardwire_close (serial_t scb
)
867 /* Generic operations used by all UNIX/FD based serial interfaces. */
870 ser_unix_nop_get_tty_state (serial_t scb
)
872 /* allocate a dummy */
873 return (serial_ttystate
) XMALLOC (int);
877 ser_unix_nop_set_tty_state (serial_t scb
, serial_ttystate ttystate
)
883 ser_unix_nop_raw (serial_t scb
)
885 return; /* Always in raw mode */
888 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
889 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
892 ser_unix_wait_for (serial_t scb
, int timeout
)
898 fd_set readfds
, exceptfds
;
900 /* NOTE: Some OS's can scramble the READFDS when the select()
901 call fails (ex the kernel with Red Hat 5.2). Initialize all
902 arguments before each call. */
908 FD_ZERO (&exceptfds
);
909 FD_SET (scb
->fd
, &readfds
);
910 FD_SET (scb
->fd
, &exceptfds
);
913 numfds
= select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, &tv
);
915 numfds
= select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, 0);
920 return SERIAL_TIMEOUT
;
921 else if (errno
== EINTR
)
924 return SERIAL_ERROR
; /* Got an error from select or poll */
931 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
932 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
933 char if successful. Returns -2 if timeout expired, EOF if line dropped
934 dead, or -3 for any other error (see errno in that case). */
937 do_unix_readchar (serial_t scb
, int timeout
)
942 /* We have to be able to keep the GUI alive here, so we break the original
943 timeout into steps of 1 second, running the "keep the GUI alive" hook
944 each time through the loop.
946 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
947 will only go through the loop once. */
949 delta
= (timeout
== 0 ? 0 : 1);
953 /* N.B. The UI may destroy our world (for instance by calling
954 remote_stop,) in which case we want to get out of here as
955 quickly as possible. It is not safe to touch scb, since
956 someone else might have freed it. The ui_loop_hook signals that
957 we should exit by returning 1. */
961 if (ui_loop_hook (0))
962 return SERIAL_TIMEOUT
;
965 status
= ser_unix_wait_for (scb
, delta
);
969 /* If we got a character or an error back from wait_for, then we can
970 break from the loop before the timeout is completed. */
972 if (status
!= SERIAL_TIMEOUT
)
977 /* If we have exhausted the original timeout, then generate
978 a SERIAL_TIMEOUT, and pass it out of the loop. */
980 else if (timeout
== 0)
982 status
= SERIAL_TIMEOUT
;
992 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
993 if (status
!= -1 || errno
!= EINTR
)
1000 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
1001 distinguish between EOF & timeouts
1004 return SERIAL_ERROR
; /* Got an error from read */
1007 scb
->bufcnt
= status
;
1009 scb
->bufp
= scb
->buf
;
1010 return *scb
->bufp
++;
1013 /* Perform operations common to both old and new readchar. */
1015 /* Return the next character from the input FIFO. If the FIFO is
1016 empty, call the SERIAL specific routine to try and read in more
1019 Initially data from the input FIFO is returned (fd_event()
1020 pre-reads the input into that FIFO. Once that has been emptied,
1021 further data is obtained by polling the input FD using the device
1022 specific readchar() function. Note: reschedule() is called after
1023 every read. This is because there is no guarentee that the lower
1024 level fd_event() poll_event() code (which also calls reschedule())
1028 generic_readchar (serial_t scb
, int timeout
,
1029 int (do_readchar
) (serial_t scb
, int timeout
))
1032 if (scb
->bufcnt
> 0)
1038 else if (scb
->bufcnt
< 0)
1040 /* Some errors/eof are are sticky. */
1045 ch
= do_readchar (scb
, timeout
);
1048 switch ((enum serial_rc
) ch
)
1052 /* Make the error/eof stick. */
1055 case SERIAL_TIMEOUT
:
1066 ser_unix_readchar (serial_t scb
, int timeout
)
1068 return generic_readchar (scb
, timeout
, do_unix_readchar
);
1072 ser_unix_nop_noflush_set_tty_state (serial_t scb
,
1073 serial_ttystate new_ttystate
,
1074 serial_ttystate old_ttystate
)
1080 ser_unix_nop_print_tty_state (serial_t scb
,
1081 serial_ttystate ttystate
,
1082 struct ui_file
*stream
)
1084 /* Nothing to print. */
1089 ser_unix_nop_setbaudrate (serial_t scb
, int rate
)
1091 return 0; /* Never fails! */
1095 ser_unix_nop_setstopbits (serial_t scb
, int num
)
1097 return 0; /* Never fails! */
1101 ser_unix_write (serial_t scb
, const char *str
, int len
)
1107 cc
= write (scb
->fd
, str
, len
);
1118 ser_unix_nop_flush_output (serial_t scb
)
1124 ser_unix_flush_input (serial_t scb
)
1126 if (scb
->bufcnt
>= 0)
1129 scb
->bufp
= scb
->buf
;
1133 return SERIAL_ERROR
;
1137 ser_unix_nop_send_break (serial_t scb
)
1143 ser_unix_nop_drain_output (serial_t scb
)
1150 /* Event handling for ASYNC serial code.
1152 At any time the SERIAL device either: has an empty FIFO and is
1153 waiting on a FD event; or has a non-empty FIFO/error condition and
1154 is constantly scheduling timer events.
1156 ASYNC only stops pestering its client when it is de-async'ed or it
1157 is told to go away. */
1159 /* Value of scb->async_state: */
1161 /* >= 0 (TIMER_SCHEDULED) */
1162 /* The ID of the currently scheduled timer event. This state is
1163 rarely encountered. Timer events are one-off so as soon as the
1164 event is delivered the state is shanged to NOTHING_SCHEDULED. */
1166 /* The fd_event() handler is scheduled. It is called when ever the
1167 file descriptor becomes ready. */
1168 NOTHING_SCHEDULED
= -2
1169 /* Either no task is scheduled (just going into ASYNC mode) or a
1170 timer event has just gone off and the current state has been
1171 forced into nothing scheduled. */
1174 /* Identify and schedule the next ASYNC task based on scb->async_state
1175 and scb->buf* (the input FIFO). A state machine is used to avoid
1176 the need to make redundant calls into the event-loop - the next
1177 scheduled task is only changed when needed. */
1180 reschedule (serial_t scb
)
1182 if (SERIAL_IS_ASYNC_P (scb
))
1185 switch (scb
->async_state
)
1188 if (scb
->bufcnt
== 0)
1189 next_state
= FD_SCHEDULED
;
1192 delete_file_handler (scb
->fd
);
1193 next_state
= create_timer (0, push_event
, scb
);
1196 case NOTHING_SCHEDULED
:
1197 if (scb
->bufcnt
== 0)
1199 add_file_handler (scb
->fd
, fd_event
, scb
);
1200 next_state
= FD_SCHEDULED
;
1204 next_state
= create_timer (0, push_event
, scb
);
1207 default: /* TIMER SCHEDULED */
1208 if (scb
->bufcnt
== 0)
1210 delete_timer (scb
->async_state
);
1211 add_file_handler (scb
->fd
, fd_event
, scb
);
1212 next_state
= FD_SCHEDULED
;
1215 next_state
= scb
->async_state
;
1218 if (SERIAL_DEBUG_P (scb
))
1223 if (scb
->async_state
!= FD_SCHEDULED
)
1224 fprintf_unfiltered (gdb_stdlog
, "[fd%d->fd-scheduled]\n",
1227 default: /* TIMER SCHEDULED */
1228 if (scb
->async_state
== FD_SCHEDULED
)
1229 fprintf_unfiltered (gdb_stdlog
, "[fd%d->timer-scheduled]\n",
1234 scb
->async_state
= next_state
;
1238 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1239 is no pending error). As soon as data arrives, it is read into the
1240 input FIFO and the client notified. The client should then drain
1241 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
1242 push_event() is used to nag the client until it is. */
1245 fd_event (int error
, void *context
)
1247 serial_t scb
= context
;
1250 scb
->bufcnt
= SERIAL_ERROR
;
1252 else if (scb
->bufcnt
== 0)
1254 /* Prime the input FIFO. The readchar() function is used to
1255 pull characters out of the buffer. See also
1256 generic_readchar(). */
1260 nr
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
1262 while (nr
== -1 && errno
== EINTR
);
1265 scb
->bufcnt
= SERIAL_EOF
;
1270 scb
->bufp
= scb
->buf
;
1274 scb
->bufcnt
= SERIAL_ERROR
;
1277 scb
->async_handler (scb
, scb
->async_context
);
1281 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1282 error). Nag the client until all the data has been read. In the
1283 case of errors, the client will need to close or de-async the
1284 device before naging stops. */
1287 push_event (void *context
)
1289 serial_t scb
= context
;
1290 scb
->async_state
= NOTHING_SCHEDULED
; /* Timers are one-off */
1291 scb
->async_handler (scb
, scb
->async_context
);
1296 /* Put the SERIAL device into/out-of ASYNC mode. */
1299 ser_unix_async (serial_t scb
,
1304 /* Force a re-schedule. */
1305 scb
->async_state
= NOTHING_SCHEDULED
;
1306 if (SERIAL_DEBUG_P (scb
))
1307 fprintf_unfiltered (gdb_stdlog
, "[fd%d->asynchronous]\n",
1313 if (SERIAL_DEBUG_P (scb
))
1314 fprintf_unfiltered (gdb_stdlog
, "[fd%d->synchronous]\n",
1316 /* De-schedule whatever tasks are currently scheduled. */
1317 switch (scb
->async_state
)
1320 delete_file_handler (scb
->fd
);
1324 default: /* TIMER SCHEDULED */
1325 delete_timer (scb
->async_state
);
1332 _initialize_ser_hardwire (void)
1334 struct serial_ops
*ops
= XMALLOC (struct serial_ops
);
1335 memset (ops
, sizeof (struct serial_ops
), 0);
1336 ops
->name
= "hardwire";
1338 ops
->open
= hardwire_open
;
1339 ops
->close
= hardwire_close
;
1340 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1341 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1343 ops
->readchar
= hardwire_readchar
;
1344 ops
->write
= ser_unix_write
;
1345 ops
->flush_output
= hardwire_flush_output
;
1346 ops
->flush_input
= hardwire_flush_input
;
1347 ops
->send_break
= hardwire_send_break
;
1348 ops
->go_raw
= hardwire_raw
;
1349 ops
->get_tty_state
= hardwire_get_tty_state
;
1350 ops
->set_tty_state
= hardwire_set_tty_state
;
1351 ops
->print_tty_state
= hardwire_print_tty_state
;
1352 ops
->noflush_set_tty_state
= hardwire_noflush_set_tty_state
;
1353 ops
->setbaudrate
= hardwire_setbaudrate
;
1354 ops
->setstopbits
= hardwire_setstopbits
;
1355 ops
->drain_output
= hardwire_drain_output
;
1356 ops
->async
= ser_unix_async
;
1357 serial_add_interface (ops
);