1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992-1994, 1998-2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include "gdb_string.h"
32 #include "event-loop.h"
36 struct hardwire_ttystate
38 struct termios termios;
44 /* It is believed that all systems which have added job control to SVR3
45 (e.g. sco) have also added termios. Even if not, trying to figure out
46 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
47 bewildering. So we don't attempt it. */
49 struct hardwire_ttystate
56 struct hardwire_ttystate
61 /* Line discipline flags. */
66 static int hardwire_open (serial_t scb, const char *name);
67 static void hardwire_raw (serial_t scb);
68 static int wait_for (serial_t scb, int timeout);
69 static int hardwire_readchar (serial_t scb, int timeout);
70 static int do_hardwire_readchar (serial_t scb, int timeout);
71 static int generic_readchar (serial_t scb, int timeout, int (*do_readchar) (serial_t scb, int timeout));
72 static int rate_to_code (int rate);
73 static int hardwire_setbaudrate (serial_t scb, int rate);
74 static void hardwire_close (serial_t scb);
75 static int get_tty_state (serial_t scb, struct hardwire_ttystate * state);
76 static int set_tty_state (serial_t scb, struct hardwire_ttystate * state);
77 static serial_ttystate hardwire_get_tty_state (serial_t scb);
78 static int hardwire_set_tty_state (serial_t scb, serial_ttystate state);
79 static int hardwire_noflush_set_tty_state (serial_t, serial_ttystate,
81 static void hardwire_print_tty_state (serial_t, serial_ttystate, struct ui_file *);
82 static int hardwire_drain_output (serial_t);
83 static int hardwire_flush_output (serial_t);
84 static int hardwire_flush_input (serial_t);
85 static int hardwire_send_break (serial_t);
86 static int hardwire_setstopbits (serial_t, int);
88 static int do_unix_readchar (serial_t scb, int timeout);
89 static timer_handler_func push_event;
90 static handler_func fd_event;
91 static void reschedule (serial_t scb);
93 void _initialize_ser_hardwire (void);
95 extern int (*ui_loop_hook) (int);
97 /* Open up a real live device for serial I/O */
100 hardwire_open (serial_t scb, const char *name)
102 scb->fd = open (name, O_RDWR);
110 get_tty_state (serial_t scb, struct hardwire_ttystate *state)
113 if (tcgetattr (scb->fd, &state->termios) < 0)
120 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
126 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
128 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
130 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
132 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
140 set_tty_state (serial_t scb, struct hardwire_ttystate *state)
143 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
150 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
156 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
158 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
160 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
162 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
169 static serial_ttystate
170 hardwire_get_tty_state (serial_t scb)
172 struct hardwire_ttystate *state;
174 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
176 if (get_tty_state (scb, state))
179 return (serial_ttystate) state;
183 hardwire_set_tty_state (serial_t scb, serial_ttystate ttystate)
185 struct hardwire_ttystate *state;
187 state = (struct hardwire_ttystate *) ttystate;
189 return set_tty_state (scb, state);
193 hardwire_noflush_set_tty_state (serial_t scb,
194 serial_ttystate new_ttystate,
195 serial_ttystate old_ttystate)
197 struct hardwire_ttystate new_state;
199 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
202 new_state = *(struct hardwire_ttystate *) new_ttystate;
204 /* Don't change in or out of raw mode; we don't want to flush input.
205 termio and termios have no such restriction; for them flushing input
206 is separate from setting the attributes. */
209 if (state->sgttyb.sg_flags & RAW)
210 new_state.sgttyb.sg_flags |= RAW;
212 new_state.sgttyb.sg_flags &= ~RAW;
214 /* I'm not sure whether this is necessary; the manpage just mentions
216 if (state->sgttyb.sg_flags & CBREAK)
217 new_state.sgttyb.sg_flags |= CBREAK;
219 new_state.sgttyb.sg_flags &= ~CBREAK;
222 return set_tty_state (scb, &new_state);
226 hardwire_print_tty_state (serial_t scb,
227 serial_ttystate ttystate,
228 struct ui_file *stream)
230 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
234 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
235 (int) state->termios.c_iflag,
236 (int) state->termios.c_oflag);
237 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
238 (int) state->termios.c_cflag,
239 (int) state->termios.c_lflag);
241 /* This not in POSIX, and is not really documented by those systems
242 which have it (at least not Sun). */
243 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
245 fprintf_filtered (stream, "c_cc: ");
246 for (i = 0; i < NCCS; i += 1)
247 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
248 fprintf_filtered (stream, "\n");
252 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
253 state->termio.c_iflag, state->termio.c_oflag);
254 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
255 state->termio.c_cflag, state->termio.c_lflag,
256 state->termio.c_line);
257 fprintf_filtered (stream, "c_cc: ");
258 for (i = 0; i < NCC; i += 1)
259 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
260 fprintf_filtered (stream, "\n");
264 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
265 state->sgttyb.sg_flags);
267 fprintf_filtered (stream, "tchars: ");
268 for (i = 0; i < (int) sizeof (struct tchars); i++)
269 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
270 fprintf_filtered (stream, "\n");
272 fprintf_filtered (stream, "ltchars: ");
273 for (i = 0; i < (int) sizeof (struct ltchars); i++)
274 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
275 fprintf_filtered (stream, "\n");
277 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
281 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
284 hardwire_drain_output (serial_t scb)
287 return tcdrain (scb->fd);
291 return ioctl (scb->fd, TCSBRK, 1);
295 /* Get the current state and then restore it using TIOCSETP,
296 which should cause the output to drain and pending input
299 struct hardwire_ttystate state;
300 if (get_tty_state (scb, &state))
306 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
313 hardwire_flush_output (serial_t scb)
316 return tcflush (scb->fd, TCOFLUSH);
320 return ioctl (scb->fd, TCFLSH, 1);
324 /* This flushes both input and output, but we can't do better. */
325 return ioctl (scb->fd, TIOCFLUSH, 0);
330 hardwire_flush_input (serial_t scb)
332 ser_unix_flush_input (scb);
335 return tcflush (scb->fd, TCIFLUSH);
339 return ioctl (scb->fd, TCFLSH, 0);
343 /* This flushes both input and output, but we can't do better. */
344 return ioctl (scb->fd, TIOCFLUSH, 0);
349 hardwire_send_break (serial_t scb)
352 return tcsendbreak (scb->fd, 0);
356 return ioctl (scb->fd, TCSBRK, 0);
362 struct timeval timeout;
364 status = ioctl (scb->fd, TIOCSBRK, 0);
366 /* Can't use usleep; it doesn't exist in BSD 4.2. */
367 /* Note that if this select() is interrupted by a signal it will not wait
368 the full length of time. I think that is OK. */
370 timeout.tv_usec = 250000;
371 select (0, 0, 0, 0, &timeout);
372 status = ioctl (scb->fd, TIOCCBRK, 0);
379 hardwire_raw (serial_t scb)
381 struct hardwire_ttystate state;
383 if (get_tty_state (scb, &state))
384 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
387 state.termios.c_iflag = 0;
388 state.termios.c_oflag = 0;
389 state.termios.c_lflag = 0;
390 state.termios.c_cflag &= ~(CSIZE | PARENB);
391 state.termios.c_cflag |= CLOCAL | CS8;
392 state.termios.c_cc[VMIN] = 0;
393 state.termios.c_cc[VTIME] = 0;
397 state.termio.c_iflag = 0;
398 state.termio.c_oflag = 0;
399 state.termio.c_lflag = 0;
400 state.termio.c_cflag &= ~(CSIZE | PARENB);
401 state.termio.c_cflag |= CLOCAL | CS8;
402 state.termio.c_cc[VMIN] = 0;
403 state.termio.c_cc[VTIME] = 0;
407 state.sgttyb.sg_flags |= RAW | ANYP;
408 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
411 scb->current_timeout = 0;
413 if (set_tty_state (scb, &state))
414 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
417 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
418 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
420 For termio{s}, we actually just setup VTIME if necessary, and let the
421 timeout occur in the read() in hardwire_read().
424 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
425 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
428 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
429 possible values of the TIMEOUT parameter are ONE and ZERO.
430 Consequently all the code that tries to handle the possability of
431 an overflowed timer is unnecessary. */
434 wait_for (serial_t scb, int timeout)
443 /* NOTE: Some OS's can scramble the READFDS when the select()
444 call fails (ex the kernel with Red Hat 5.2). Initialize all
445 arguments before each call. */
451 FD_SET (scb->fd, &readfds);
454 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
456 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
460 return SERIAL_TIMEOUT;
461 else if (errno == EINTR)
464 return SERIAL_ERROR; /* Got an error from select or poll */
468 #endif /* HAVE_SGTTY */
470 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
471 if (timeout == scb->current_timeout)
474 scb->current_timeout = timeout;
477 struct hardwire_ttystate state;
479 if (get_tty_state (scb, &state))
480 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
486 state.termios.c_cc[VTIME] = 0;
487 state.termios.c_cc[VMIN] = 1;
491 state.termios.c_cc[VMIN] = 0;
492 state.termios.c_cc[VTIME] = timeout * 10;
493 if (state.termios.c_cc[VTIME] != timeout * 10)
496 /* If c_cc is an 8-bit signed character, we can't go
497 bigger than this. If it is always unsigned, we could use
500 scb->current_timeout = 12;
501 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
502 scb->timeout_remaining = timeout - scb->current_timeout;
511 state.termio.c_cc[VTIME] = 0;
512 state.termio.c_cc[VMIN] = 1;
516 state.termio.c_cc[VMIN] = 0;
517 state.termio.c_cc[VTIME] = timeout * 10;
518 if (state.termio.c_cc[VTIME] != timeout * 10)
520 /* If c_cc is an 8-bit signed character, we can't go
521 bigger than this. If it is always unsigned, we could use
524 scb->current_timeout = 12;
525 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
526 scb->timeout_remaining = timeout - scb->current_timeout;
531 if (set_tty_state (scb, &state))
532 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
536 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
539 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
540 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
541 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
542 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
544 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
545 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
548 /* NOTE: cagney/1999-09-16: This function is not identical to
549 ser_unix_readchar() as part of replacing it with ser_unix*()
550 merging will be required - this code handles the case where read()
551 times out due to no data while ser_unix_readchar() doesn't expect
555 do_hardwire_readchar (serial_t scb, int timeout)
563 /* We have to be able to keep the GUI alive here, so we break the original
564 timeout into steps of 1 second, running the "keep the GUI alive" hook
565 each time through the loop.
566 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
567 will only go through the loop once. */
569 delta = (timeout == 0 ? 0 : 1);
573 /* N.B. The UI may destroy our world (for instance by calling
574 remote_stop,) in which case we want to get out of here as
575 quickly as possible. It is not safe to touch scb, since
576 someone else might have freed it. The ui_loop_hook signals that
577 we should exit by returning 1. */
580 detach = ui_loop_hook (0);
583 return SERIAL_TIMEOUT;
585 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
586 status = wait_for (scb, delta);
591 status = read (scb->fd, scb->buf, BUFSIZ);
597 /* Zero characters means timeout (it could also be EOF, but
598 we don't (yet at least) distinguish). */
599 if (scb->timeout_remaining > 0)
601 timeout = scb->timeout_remaining;
604 else if (scb->timeout_remaining < 0)
607 return SERIAL_TIMEOUT;
609 else if (errno == EINTR)
612 return SERIAL_ERROR; /* Got an error from read */
615 scb->bufcnt = status;
617 scb->bufp = scb->buf;
623 hardwire_readchar (serial_t scb, int timeout)
625 return generic_readchar (scb, timeout, do_hardwire_readchar);
637 /* Translate baud rates from integers to damn B_codes. Unix should
638 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
738 rate_to_code (int rate)
742 for (i = 0; baudtab[i].rate != -1; i++)
743 if (rate == baudtab[i].rate)
744 return baudtab[i].code;
750 hardwire_setbaudrate (serial_t scb, int rate)
752 struct hardwire_ttystate state;
754 if (get_tty_state (scb, &state))
758 cfsetospeed (&state.termios, rate_to_code (rate));
759 cfsetispeed (&state.termios, rate_to_code (rate));
767 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
768 state.termio.c_cflag |= rate_to_code (rate);
772 state.sgttyb.sg_ispeed = rate_to_code (rate);
773 state.sgttyb.sg_ospeed = rate_to_code (rate);
776 return set_tty_state (scb, &state);
780 hardwire_setstopbits (serial_t scb, int num)
782 struct hardwire_ttystate state;
785 if (get_tty_state (scb, &state))
790 case SERIAL_1_STOPBITS:
793 case SERIAL_1_AND_A_HALF_STOPBITS:
794 case SERIAL_2_STOPBITS:
803 state.termios.c_cflag &= ~CSTOPB;
805 state.termios.c_cflag |= CSTOPB; /* two bits */
810 state.termio.c_cflag &= ~CSTOPB;
812 state.termio.c_cflag |= CSTOPB; /* two bits */
816 return 0; /* sgtty doesn't support this */
819 return set_tty_state (scb, &state);
823 hardwire_close (serial_t scb)
833 /* Generic operations used by all UNIX/FD based serial interfaces. */
836 ser_unix_nop_get_tty_state (serial_t scb)
838 /* allocate a dummy */
839 return (serial_ttystate) XMALLOC (int);
843 ser_unix_nop_set_tty_state (serial_t scb, serial_ttystate ttystate)
849 ser_unix_nop_raw (serial_t scb)
851 return; /* Always in raw mode */
854 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
855 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
858 ser_unix_wait_for (serial_t scb, int timeout)
864 fd_set readfds, exceptfds;
866 /* NOTE: Some OS's can scramble the READFDS when the select()
867 call fails (ex the kernel with Red Hat 5.2). Initialize all
868 arguments before each call. */
874 FD_ZERO (&exceptfds);
875 FD_SET (scb->fd, &readfds);
876 FD_SET (scb->fd, &exceptfds);
879 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
881 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
886 return SERIAL_TIMEOUT;
887 else if (errno == EINTR)
890 return SERIAL_ERROR; /* Got an error from select or poll */
897 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
898 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
899 char if successful. Returns -2 if timeout expired, EOF if line dropped
900 dead, or -3 for any other error (see errno in that case). */
903 do_unix_readchar (serial_t scb, int timeout)
908 /* We have to be able to keep the GUI alive here, so we break the original
909 timeout into steps of 1 second, running the "keep the GUI alive" hook
910 each time through the loop.
912 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
913 will only go through the loop once. */
915 delta = (timeout == 0 ? 0 : 1);
919 /* N.B. The UI may destroy our world (for instance by calling
920 remote_stop,) in which case we want to get out of here as
921 quickly as possible. It is not safe to touch scb, since
922 someone else might have freed it. The ui_loop_hook signals that
923 we should exit by returning 1. */
927 if (ui_loop_hook (0))
928 return SERIAL_TIMEOUT;
931 status = ser_unix_wait_for (scb, delta);
935 /* If we got a character or an error back from wait_for, then we can
936 break from the loop before the timeout is completed. */
938 if (status != SERIAL_TIMEOUT)
943 /* If we have exhausted the original timeout, then generate
944 a SERIAL_TIMEOUT, and pass it out of the loop. */
946 else if (timeout == 0)
948 status = SERIAL_TIMEOUT;
958 status = read (scb->fd, scb->buf, BUFSIZ);
959 if (status != -1 || errno != EINTR)
966 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
967 distinguish between EOF & timeouts
970 return SERIAL_ERROR; /* Got an error from read */
973 scb->bufcnt = status;
975 scb->bufp = scb->buf;
979 /* Perform operations common to both old and new readchar. */
981 /* Return the next character from the input FIFO. If the FIFO is
982 empty, call the SERIAL specific routine to try and read in more
985 Initially data from the input FIFO is returned (fd_event()
986 pre-reads the input into that FIFO. Once that has been emptied,
987 further data is obtained by polling the input FD using the device
988 specific readchar() function. Note: reschedule() is called after
989 every read. This is because there is no guarentee that the lower
990 level fd_event() poll_event() code (which also calls reschedule())
994 generic_readchar (serial_t scb, int timeout,
995 int (do_readchar) (serial_t scb, int timeout))
1004 else if (scb->bufcnt < 0)
1006 /* Some errors/eof are are sticky. */
1011 ch = do_readchar (scb, timeout);
1014 switch ((enum serial_rc) ch)
1018 /* Make the error/eof stick. */
1021 case SERIAL_TIMEOUT:
1032 ser_unix_readchar (serial_t scb, int timeout)
1034 return generic_readchar (scb, timeout, do_unix_readchar);
1038 ser_unix_nop_noflush_set_tty_state (serial_t scb,
1039 serial_ttystate new_ttystate,
1040 serial_ttystate old_ttystate)
1046 ser_unix_nop_print_tty_state (serial_t scb,
1047 serial_ttystate ttystate,
1048 struct ui_file *stream)
1050 /* Nothing to print. */
1055 ser_unix_nop_setbaudrate (serial_t scb, int rate)
1057 return 0; /* Never fails! */
1061 ser_unix_nop_setstopbits (serial_t scb, int num)
1063 return 0; /* Never fails! */
1067 ser_unix_write (serial_t scb, const char *str, int len)
1073 cc = write (scb->fd, str, len);
1084 ser_unix_nop_flush_output (serial_t scb)
1090 ser_unix_flush_input (serial_t scb)
1092 if (scb->bufcnt >= 0)
1095 scb->bufp = scb->buf;
1099 return SERIAL_ERROR;
1103 ser_unix_nop_send_break (serial_t scb)
1109 ser_unix_nop_drain_output (serial_t scb)
1116 /* Event handling for ASYNC serial code.
1118 At any time the SERIAL device either: has an empty FIFO and is
1119 waiting on a FD event; or has a non-empty FIFO/error condition and
1120 is constantly scheduling timer events.
1122 ASYNC only stops pestering its client when it is de-async'ed or it
1123 is told to go away. */
1125 /* Value of scb->async_state: */
1127 /* >= 0 (TIMER_SCHEDULED) */
1128 /* The ID of the currently scheduled timer event. This state is
1129 rarely encountered. Timer events are one-off so as soon as the
1130 event is delivered the state is shanged to NOTHING_SCHEDULED. */
1132 /* The fd_event() handler is scheduled. It is called when ever the
1133 file descriptor becomes ready. */
1134 NOTHING_SCHEDULED = -2
1135 /* Either no task is scheduled (just going into ASYNC mode) or a
1136 timer event has just gone off and the current state has been
1137 forced into nothing scheduled. */
1140 /* Identify and schedule the next ASYNC task based on scb->async_state
1141 and scb->buf* (the input FIFO). A state machine is used to avoid
1142 the need to make redundant calls into the event-loop - the next
1143 scheduled task is only changed when needed. */
1146 reschedule (serial_t scb)
1148 if (SERIAL_IS_ASYNC_P (scb))
1151 switch (scb->async_state)
1154 if (scb->bufcnt == 0)
1155 next_state = FD_SCHEDULED;
1158 delete_file_handler (scb->fd);
1159 next_state = create_timer (0, push_event, scb);
1162 case NOTHING_SCHEDULED:
1163 if (scb->bufcnt == 0)
1165 add_file_handler (scb->fd, fd_event, scb);
1166 next_state = FD_SCHEDULED;
1170 next_state = create_timer (0, push_event, scb);
1173 default: /* TIMER SCHEDULED */
1174 if (scb->bufcnt == 0)
1176 delete_timer (scb->async_state);
1177 add_file_handler (scb->fd, fd_event, scb);
1178 next_state = FD_SCHEDULED;
1181 next_state = scb->async_state;
1184 if (SERIAL_DEBUG_P (scb))
1189 if (scb->async_state != FD_SCHEDULED)
1190 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
1193 default: /* TIMER SCHEDULED */
1194 if (scb->async_state == FD_SCHEDULED)
1195 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
1200 scb->async_state = next_state;
1204 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1205 is no pending error). As soon as data arrives, it is read into the
1206 input FIFO and the client notified. The client should then drain
1207 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
1208 push_event() is used to nag the client until it is. */
1211 fd_event (int error, void *context)
1213 serial_t scb = context;
1216 scb->bufcnt = SERIAL_ERROR;
1218 else if (scb->bufcnt == 0)
1220 /* Prime the input FIFO. The readchar() function is used to
1221 pull characters out of the buffer. See also
1222 generic_readchar(). */
1226 nr = read (scb->fd, scb->buf, BUFSIZ);
1228 while (nr == -1 && errno == EINTR);
1231 scb->bufcnt = SERIAL_EOF;
1236 scb->bufp = scb->buf;
1240 scb->bufcnt = SERIAL_ERROR;
1243 scb->async_handler (scb, scb->async_context);
1247 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1248 error). Nag the client until all the data has been read. In the
1249 case of errors, the client will need to close or de-async the
1250 device before naging stops. */
1253 push_event (void *context)
1255 serial_t scb = context;
1256 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
1257 scb->async_handler (scb, scb->async_context);
1262 /* Put the SERIAL device into/out-of ASYNC mode. */
1265 ser_unix_async (serial_t scb,
1270 /* Force a re-schedule. */
1271 scb->async_state = NOTHING_SCHEDULED;
1272 if (SERIAL_DEBUG_P (scb))
1273 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
1279 if (SERIAL_DEBUG_P (scb))
1280 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
1282 /* De-schedule whatever tasks are currently scheduled. */
1283 switch (scb->async_state)
1286 delete_file_handler (scb->fd);
1290 default: /* TIMER SCHEDULED */
1291 delete_timer (scb->async_state);
1298 _initialize_ser_hardwire (void)
1300 struct serial_ops *ops = XMALLOC (struct serial_ops);
1301 memset (ops, sizeof (struct serial_ops), 0);
1302 ops->name = "hardwire";
1304 ops->open = hardwire_open;
1305 ops->close = hardwire_close;
1306 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1307 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1309 ops->readchar = hardwire_readchar;
1310 ops->write = ser_unix_write;
1311 ops->flush_output = hardwire_flush_output;
1312 ops->flush_input = hardwire_flush_input;
1313 ops->send_break = hardwire_send_break;
1314 ops->go_raw = hardwire_raw;
1315 ops->get_tty_state = hardwire_get_tty_state;
1316 ops->set_tty_state = hardwire_set_tty_state;
1317 ops->print_tty_state = hardwire_print_tty_state;
1318 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1319 ops->setbaudrate = hardwire_setbaudrate;
1320 ops->setstopbits = hardwire_setstopbits;
1321 ops->drain_output = hardwire_drain_output;
1322 ops->async = ser_unix_async;
1323 serial_add_interface (ops);