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 (struct serial *scb, const char *name);
68 static void hardwire_raw (struct serial *scb);
69 static int wait_for (struct serial *scb, int timeout);
70 static int hardwire_readchar (struct serial *scb, int timeout);
71 static int do_hardwire_readchar (struct serial *scb, int timeout);
72 static int generic_readchar (struct serial *scb, int timeout,
73 int (*do_readchar) (struct serial *scb,
75 static int rate_to_code (int rate);
76 static int hardwire_setbaudrate (struct serial *scb, int rate);
77 static void hardwire_close (struct serial *scb);
78 static int get_tty_state (struct serial *scb,
79 struct hardwire_ttystate * state);
80 static int set_tty_state (struct serial *scb,
81 struct hardwire_ttystate * state);
82 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
83 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
84 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
86 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
88 static int hardwire_drain_output (struct serial *);
89 static int hardwire_flush_output (struct serial *);
90 static int hardwire_flush_input (struct serial *);
91 static int hardwire_send_break (struct serial *);
92 static int hardwire_setstopbits (struct serial *, int);
94 static int do_unix_readchar (struct serial *scb, int timeout);
95 static timer_handler_func push_event;
96 static handler_func fd_event;
97 static void reschedule (struct serial *scb);
99 void _initialize_ser_hardwire (void);
101 extern int (*ui_loop_hook) (int);
103 /* Open up a real live device for serial I/O */
106 hardwire_open (struct serial *scb, const char *name)
108 scb->fd = open (name, O_RDWR);
116 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
119 if (tcgetattr (scb->fd, &state->termios) < 0)
126 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
132 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
134 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
136 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
138 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
146 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
149 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
156 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
162 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
164 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
166 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
168 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
175 static serial_ttystate
176 hardwire_get_tty_state (struct serial *scb)
178 struct hardwire_ttystate *state;
180 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
182 if (get_tty_state (scb, state))
185 return (serial_ttystate) state;
189 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
191 struct hardwire_ttystate *state;
193 state = (struct hardwire_ttystate *) ttystate;
195 return set_tty_state (scb, state);
199 hardwire_noflush_set_tty_state (struct serial *scb,
200 serial_ttystate new_ttystate,
201 serial_ttystate old_ttystate)
203 struct hardwire_ttystate new_state;
205 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
208 new_state = *(struct hardwire_ttystate *) new_ttystate;
210 /* Don't change in or out of raw mode; we don't want to flush input.
211 termio and termios have no such restriction; for them flushing input
212 is separate from setting the attributes. */
215 if (state->sgttyb.sg_flags & RAW)
216 new_state.sgttyb.sg_flags |= RAW;
218 new_state.sgttyb.sg_flags &= ~RAW;
220 /* I'm not sure whether this is necessary; the manpage just mentions
222 if (state->sgttyb.sg_flags & CBREAK)
223 new_state.sgttyb.sg_flags |= CBREAK;
225 new_state.sgttyb.sg_flags &= ~CBREAK;
228 return set_tty_state (scb, &new_state);
232 hardwire_print_tty_state (struct serial *scb,
233 serial_ttystate ttystate,
234 struct ui_file *stream)
236 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
240 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
241 (int) state->termios.c_iflag,
242 (int) state->termios.c_oflag);
243 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
244 (int) state->termios.c_cflag,
245 (int) state->termios.c_lflag);
247 /* This not in POSIX, and is not really documented by those systems
248 which have it (at least not Sun). */
249 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
251 fprintf_filtered (stream, "c_cc: ");
252 for (i = 0; i < NCCS; i += 1)
253 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
254 fprintf_filtered (stream, "\n");
258 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
259 state->termio.c_iflag, state->termio.c_oflag);
260 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
261 state->termio.c_cflag, state->termio.c_lflag,
262 state->termio.c_line);
263 fprintf_filtered (stream, "c_cc: ");
264 for (i = 0; i < NCC; i += 1)
265 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
266 fprintf_filtered (stream, "\n");
270 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
271 state->sgttyb.sg_flags);
273 fprintf_filtered (stream, "tchars: ");
274 for (i = 0; i < (int) sizeof (struct tchars); i++)
275 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
276 fprintf_filtered (stream, "\n");
278 fprintf_filtered (stream, "ltchars: ");
279 for (i = 0; i < (int) sizeof (struct ltchars); i++)
280 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
281 fprintf_filtered (stream, "\n");
283 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
287 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
290 hardwire_drain_output (struct serial *scb)
293 return tcdrain (scb->fd);
297 return ioctl (scb->fd, TCSBRK, 1);
301 /* Get the current state and then restore it using TIOCSETP,
302 which should cause the output to drain and pending input
305 struct hardwire_ttystate state;
306 if (get_tty_state (scb, &state))
312 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
319 hardwire_flush_output (struct serial *scb)
322 return tcflush (scb->fd, TCOFLUSH);
326 return ioctl (scb->fd, TCFLSH, 1);
330 /* This flushes both input and output, but we can't do better. */
331 return ioctl (scb->fd, TIOCFLUSH, 0);
336 hardwire_flush_input (struct serial *scb)
338 ser_unix_flush_input (scb);
341 return tcflush (scb->fd, TCIFLUSH);
345 return ioctl (scb->fd, TCFLSH, 0);
349 /* This flushes both input and output, but we can't do better. */
350 return ioctl (scb->fd, TIOCFLUSH, 0);
355 hardwire_send_break (struct serial *scb)
358 return tcsendbreak (scb->fd, 0);
362 return ioctl (scb->fd, TCSBRK, 0);
368 struct timeval timeout;
370 status = ioctl (scb->fd, TIOCSBRK, 0);
372 /* Can't use usleep; it doesn't exist in BSD 4.2. */
373 /* Note that if this select() is interrupted by a signal it will not wait
374 the full length of time. I think that is OK. */
376 timeout.tv_usec = 250000;
377 select (0, 0, 0, 0, &timeout);
378 status = ioctl (scb->fd, TIOCCBRK, 0);
385 hardwire_raw (struct serial *scb)
387 struct hardwire_ttystate state;
389 if (get_tty_state (scb, &state))
390 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
393 state.termios.c_iflag = 0;
394 state.termios.c_oflag = 0;
395 state.termios.c_lflag = 0;
396 state.termios.c_cflag &= ~(CSIZE | PARENB);
397 state.termios.c_cflag |= CLOCAL | CS8;
398 state.termios.c_cc[VMIN] = 0;
399 state.termios.c_cc[VTIME] = 0;
403 state.termio.c_iflag = 0;
404 state.termio.c_oflag = 0;
405 state.termio.c_lflag = 0;
406 state.termio.c_cflag &= ~(CSIZE | PARENB);
407 state.termio.c_cflag |= CLOCAL | CS8;
408 state.termio.c_cc[VMIN] = 0;
409 state.termio.c_cc[VTIME] = 0;
413 state.sgttyb.sg_flags |= RAW | ANYP;
414 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
417 scb->current_timeout = 0;
419 if (set_tty_state (scb, &state))
420 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
423 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
424 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
426 For termio{s}, we actually just setup VTIME if necessary, and let the
427 timeout occur in the read() in hardwire_read().
430 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
431 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
434 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
435 possible values of the TIMEOUT parameter are ONE and ZERO.
436 Consequently all the code that tries to handle the possability of
437 an overflowed timer is unnecessary. */
440 wait_for (struct serial *scb, int timeout)
449 /* NOTE: Some OS's can scramble the READFDS when the select()
450 call fails (ex the kernel with Red Hat 5.2). Initialize all
451 arguments before each call. */
457 FD_SET (scb->fd, &readfds);
460 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
462 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
466 return SERIAL_TIMEOUT;
467 else if (errno == EINTR)
470 return SERIAL_ERROR; /* Got an error from select or poll */
474 #endif /* HAVE_SGTTY */
476 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
477 if (timeout == scb->current_timeout)
480 scb->current_timeout = timeout;
483 struct hardwire_ttystate state;
485 if (get_tty_state (scb, &state))
486 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
492 state.termios.c_cc[VTIME] = 0;
493 state.termios.c_cc[VMIN] = 1;
497 state.termios.c_cc[VMIN] = 0;
498 state.termios.c_cc[VTIME] = timeout * 10;
499 if (state.termios.c_cc[VTIME] != timeout * 10)
502 /* If c_cc is an 8-bit signed character, we can't go
503 bigger than this. If it is always unsigned, we could use
506 scb->current_timeout = 12;
507 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
508 scb->timeout_remaining = timeout - scb->current_timeout;
517 state.termio.c_cc[VTIME] = 0;
518 state.termio.c_cc[VMIN] = 1;
522 state.termio.c_cc[VMIN] = 0;
523 state.termio.c_cc[VTIME] = timeout * 10;
524 if (state.termio.c_cc[VTIME] != timeout * 10)
526 /* If c_cc is an 8-bit signed character, we can't go
527 bigger than this. If it is always unsigned, we could use
530 scb->current_timeout = 12;
531 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
532 scb->timeout_remaining = timeout - scb->current_timeout;
537 if (set_tty_state (scb, &state))
538 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
542 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
545 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
546 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
547 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
548 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
550 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
551 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
554 /* NOTE: cagney/1999-09-16: This function is not identical to
555 ser_unix_readchar() as part of replacing it with ser_unix*()
556 merging will be required - this code handles the case where read()
557 times out due to no data while ser_unix_readchar() doesn't expect
561 do_hardwire_readchar (struct serial *scb, int timeout)
569 /* We have to be able to keep the GUI alive here, so we break the original
570 timeout into steps of 1 second, running the "keep the GUI alive" hook
571 each time through the loop.
572 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
573 will only go through the loop once. */
575 delta = (timeout == 0 ? 0 : 1);
579 /* N.B. The UI may destroy our world (for instance by calling
580 remote_stop,) in which case we want to get out of here as
581 quickly as possible. It is not safe to touch scb, since
582 someone else might have freed it. The ui_loop_hook signals that
583 we should exit by returning 1. */
586 detach = ui_loop_hook (0);
589 return SERIAL_TIMEOUT;
591 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
592 status = wait_for (scb, delta);
597 status = read (scb->fd, scb->buf, BUFSIZ);
603 /* Zero characters means timeout (it could also be EOF, but
604 we don't (yet at least) distinguish). */
605 if (scb->timeout_remaining > 0)
607 timeout = scb->timeout_remaining;
610 else if (scb->timeout_remaining < 0)
613 return SERIAL_TIMEOUT;
615 else if (errno == EINTR)
618 return SERIAL_ERROR; /* Got an error from read */
621 scb->bufcnt = status;
623 scb->bufp = scb->buf;
629 hardwire_readchar (struct serial *scb, int timeout)
631 return generic_readchar (scb, timeout, do_hardwire_readchar);
643 /* Translate baud rates from integers to damn B_codes. Unix should
644 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
744 rate_to_code (int rate)
748 for (i = 0; baudtab[i].rate != -1; i++)
750 /* test for perfect macth. */
751 if (rate == baudtab[i].rate)
752 return baudtab[i].code;
755 /* check if it is in between valid values. */
756 if (rate < baudtab[i].rate)
760 warning ("Invalid baud rate %d. Closest values are %d and %d.",
761 rate, baudtab[i - 1].rate, baudtab[i].rate);
765 warning ("Invalid baud rate %d. Minimum value is %d.",
766 rate, baudtab[0].rate);
773 /* The requested speed was too large. */
774 warning ("Invalid baud rate %d. Maximum value is %d.",
775 rate, baudtab[i - 1].rate);
780 hardwire_setbaudrate (struct serial *scb, int rate)
782 struct hardwire_ttystate state;
783 int baud_code = rate_to_code (rate);
787 /* The baud rate was not valid.
788 A warning has already been issued. */
793 if (get_tty_state (scb, &state))
797 cfsetospeed (&state.termios, baud_code);
798 cfsetispeed (&state.termios, baud_code);
806 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
807 state.termio.c_cflag |= baud_code;
811 state.sgttyb.sg_ispeed = baud_code;
812 state.sgttyb.sg_ospeed = baud_code;
815 return set_tty_state (scb, &state);
819 hardwire_setstopbits (struct serial *scb, int num)
821 struct hardwire_ttystate state;
824 if (get_tty_state (scb, &state))
829 case SERIAL_1_STOPBITS:
832 case SERIAL_1_AND_A_HALF_STOPBITS:
833 case SERIAL_2_STOPBITS:
842 state.termios.c_cflag &= ~CSTOPB;
844 state.termios.c_cflag |= CSTOPB; /* two bits */
849 state.termio.c_cflag &= ~CSTOPB;
851 state.termio.c_cflag |= CSTOPB; /* two bits */
855 return 0; /* sgtty doesn't support this */
858 return set_tty_state (scb, &state);
862 hardwire_close (struct serial *scb)
872 /* Generic operations used by all UNIX/FD based serial interfaces. */
875 ser_unix_nop_get_tty_state (struct serial *scb)
877 /* allocate a dummy */
878 return (serial_ttystate) XMALLOC (int);
882 ser_unix_nop_set_tty_state (struct serial *scb, serial_ttystate ttystate)
888 ser_unix_nop_raw (struct serial *scb)
890 return; /* Always in raw mode */
893 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
894 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
897 ser_unix_wait_for (struct serial *scb, int timeout)
903 fd_set readfds, exceptfds;
905 /* NOTE: Some OS's can scramble the READFDS when the select()
906 call fails (ex the kernel with Red Hat 5.2). Initialize all
907 arguments before each call. */
913 FD_ZERO (&exceptfds);
914 FD_SET (scb->fd, &readfds);
915 FD_SET (scb->fd, &exceptfds);
918 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
920 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
925 return SERIAL_TIMEOUT;
926 else if (errno == EINTR)
929 return SERIAL_ERROR; /* Got an error from select or poll */
936 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
937 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
938 char if successful. Returns -2 if timeout expired, EOF if line dropped
939 dead, or -3 for any other error (see errno in that case). */
942 do_unix_readchar (struct serial *scb, int timeout)
947 /* We have to be able to keep the GUI alive here, so we break the original
948 timeout into steps of 1 second, running the "keep the GUI alive" hook
949 each time through the loop.
951 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
952 will only go through the loop once. */
954 delta = (timeout == 0 ? 0 : 1);
958 /* N.B. The UI may destroy our world (for instance by calling
959 remote_stop,) in which case we want to get out of here as
960 quickly as possible. It is not safe to touch scb, since
961 someone else might have freed it. The ui_loop_hook signals that
962 we should exit by returning 1. */
966 if (ui_loop_hook (0))
967 return SERIAL_TIMEOUT;
970 status = ser_unix_wait_for (scb, delta);
974 /* If we got a character or an error back from wait_for, then we can
975 break from the loop before the timeout is completed. */
977 if (status != SERIAL_TIMEOUT)
982 /* If we have exhausted the original timeout, then generate
983 a SERIAL_TIMEOUT, and pass it out of the loop. */
985 else if (timeout == 0)
987 status = SERIAL_TIMEOUT;
997 status = read (scb->fd, scb->buf, BUFSIZ);
998 if (status != -1 || errno != EINTR)
1005 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
1006 distinguish between EOF & timeouts
1009 return SERIAL_ERROR; /* Got an error from read */
1012 scb->bufcnt = status;
1014 scb->bufp = scb->buf;
1015 return *scb->bufp++;
1018 /* Perform operations common to both old and new readchar. */
1020 /* Return the next character from the input FIFO. If the FIFO is
1021 empty, call the SERIAL specific routine to try and read in more
1024 Initially data from the input FIFO is returned (fd_event()
1025 pre-reads the input into that FIFO. Once that has been emptied,
1026 further data is obtained by polling the input FD using the device
1027 specific readchar() function. Note: reschedule() is called after
1028 every read. This is because there is no guarentee that the lower
1029 level fd_event() poll_event() code (which also calls reschedule())
1033 generic_readchar (struct serial *scb, int timeout,
1034 int (do_readchar) (struct serial *scb, int timeout))
1037 if (scb->bufcnt > 0)
1043 else if (scb->bufcnt < 0)
1045 /* Some errors/eof are are sticky. */
1050 ch = do_readchar (scb, timeout);
1053 switch ((enum serial_rc) ch)
1057 /* Make the error/eof stick. */
1060 case SERIAL_TIMEOUT:
1071 ser_unix_readchar (struct serial *scb, int timeout)
1073 return generic_readchar (scb, timeout, do_unix_readchar);
1077 ser_unix_nop_noflush_set_tty_state (struct serial *scb,
1078 serial_ttystate new_ttystate,
1079 serial_ttystate old_ttystate)
1085 ser_unix_nop_print_tty_state (struct serial *scb,
1086 serial_ttystate ttystate,
1087 struct ui_file *stream)
1089 /* Nothing to print. */
1094 ser_unix_nop_setbaudrate (struct serial *scb, int rate)
1096 return 0; /* Never fails! */
1100 ser_unix_nop_setstopbits (struct serial *scb, int num)
1102 return 0; /* Never fails! */
1106 ser_unix_write (struct serial *scb, const char *str, int len)
1112 cc = write (scb->fd, str, len);
1123 ser_unix_nop_flush_output (struct serial *scb)
1129 ser_unix_flush_input (struct serial *scb)
1131 if (scb->bufcnt >= 0)
1134 scb->bufp = scb->buf;
1138 return SERIAL_ERROR;
1142 ser_unix_nop_send_break (struct serial *scb)
1148 ser_unix_nop_drain_output (struct serial *scb)
1155 /* Event handling for ASYNC serial code.
1157 At any time the SERIAL device either: has an empty FIFO and is
1158 waiting on a FD event; or has a non-empty FIFO/error condition and
1159 is constantly scheduling timer events.
1161 ASYNC only stops pestering its client when it is de-async'ed or it
1162 is told to go away. */
1164 /* Value of scb->async_state: */
1166 /* >= 0 (TIMER_SCHEDULED) */
1167 /* The ID of the currently scheduled timer event. This state is
1168 rarely encountered. Timer events are one-off so as soon as the
1169 event is delivered the state is shanged to NOTHING_SCHEDULED. */
1171 /* The fd_event() handler is scheduled. It is called when ever the
1172 file descriptor becomes ready. */
1173 NOTHING_SCHEDULED = -2
1174 /* Either no task is scheduled (just going into ASYNC mode) or a
1175 timer event has just gone off and the current state has been
1176 forced into nothing scheduled. */
1179 /* Identify and schedule the next ASYNC task based on scb->async_state
1180 and scb->buf* (the input FIFO). A state machine is used to avoid
1181 the need to make redundant calls into the event-loop - the next
1182 scheduled task is only changed when needed. */
1185 reschedule (struct serial *scb)
1187 if (serial_is_async_p (scb))
1190 switch (scb->async_state)
1193 if (scb->bufcnt == 0)
1194 next_state = FD_SCHEDULED;
1197 delete_file_handler (scb->fd);
1198 next_state = create_timer (0, push_event, scb);
1201 case NOTHING_SCHEDULED:
1202 if (scb->bufcnt == 0)
1204 add_file_handler (scb->fd, fd_event, scb);
1205 next_state = FD_SCHEDULED;
1209 next_state = create_timer (0, push_event, scb);
1212 default: /* TIMER SCHEDULED */
1213 if (scb->bufcnt == 0)
1215 delete_timer (scb->async_state);
1216 add_file_handler (scb->fd, fd_event, scb);
1217 next_state = FD_SCHEDULED;
1220 next_state = scb->async_state;
1223 if (serial_debug_p (scb))
1228 if (scb->async_state != FD_SCHEDULED)
1229 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
1232 default: /* TIMER SCHEDULED */
1233 if (scb->async_state == FD_SCHEDULED)
1234 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
1239 scb->async_state = next_state;
1243 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1244 is no pending error). As soon as data arrives, it is read into the
1245 input FIFO and the client notified. The client should then drain
1246 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
1247 push_event() is used to nag the client until it is. */
1250 fd_event (int error, void *context)
1252 struct serial *scb = context;
1255 scb->bufcnt = SERIAL_ERROR;
1257 else if (scb->bufcnt == 0)
1259 /* Prime the input FIFO. The readchar() function is used to
1260 pull characters out of the buffer. See also
1261 generic_readchar(). */
1265 nr = read (scb->fd, scb->buf, BUFSIZ);
1267 while (nr == -1 && errno == EINTR);
1270 scb->bufcnt = SERIAL_EOF;
1275 scb->bufp = scb->buf;
1279 scb->bufcnt = SERIAL_ERROR;
1282 scb->async_handler (scb, scb->async_context);
1286 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1287 error). Nag the client until all the data has been read. In the
1288 case of errors, the client will need to close or de-async the
1289 device before naging stops. */
1292 push_event (void *context)
1294 struct serial *scb = context;
1295 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
1296 scb->async_handler (scb, scb->async_context);
1301 /* Put the SERIAL device into/out-of ASYNC mode. */
1304 ser_unix_async (struct serial *scb,
1309 /* Force a re-schedule. */
1310 scb->async_state = NOTHING_SCHEDULED;
1311 if (serial_debug_p (scb))
1312 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
1318 if (serial_debug_p (scb))
1319 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
1321 /* De-schedule whatever tasks are currently scheduled. */
1322 switch (scb->async_state)
1325 delete_file_handler (scb->fd);
1327 case NOTHING_SCHEDULED:
1329 default: /* TIMER SCHEDULED */
1330 delete_timer (scb->async_state);
1337 _initialize_ser_hardwire (void)
1339 struct serial_ops *ops = XMALLOC (struct serial_ops);
1340 memset (ops, 0, sizeof (struct serial_ops));
1341 ops->name = "hardwire";
1343 ops->open = hardwire_open;
1344 ops->close = hardwire_close;
1345 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1346 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1348 ops->readchar = hardwire_readchar;
1349 ops->write = ser_unix_write;
1350 ops->flush_output = hardwire_flush_output;
1351 ops->flush_input = hardwire_flush_input;
1352 ops->send_break = hardwire_send_break;
1353 ops->go_raw = hardwire_raw;
1354 ops->get_tty_state = hardwire_get_tty_state;
1355 ops->set_tty_state = hardwire_set_tty_state;
1356 ops->print_tty_state = hardwire_print_tty_state;
1357 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1358 ops->setbaudrate = hardwire_setbaudrate;
1359 ops->setstopbits = hardwire_setstopbits;
1360 ops->drain_output = hardwire_drain_output;
1361 ops->async = ser_unix_async;
1362 serial_add_interface (ops);