1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994, 1998-1999 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 #ifdef HAVE_SYS_WAIT_H
31 #include <sys/socket.h>
34 #include "gdb_string.h"
35 #include "event-loop.h"
40 struct hardwire_ttystate
42 struct termios termios;
48 /* It is believed that all systems which have added job control to SVR3
49 (e.g. sco) have also added termios. Even if not, trying to figure out
50 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
51 bewildering. So we don't attempt it. */
53 struct hardwire_ttystate
60 struct hardwire_ttystate
65 /* Line discipline flags. */
70 static int hardwire_open (serial_t scb, const char *name);
71 static void hardwire_raw (serial_t scb);
72 static int wait_for (serial_t scb, int timeout);
73 static int hardwire_readchar (serial_t scb, int timeout);
74 static int rate_to_code (int rate);
75 static int hardwire_setbaudrate (serial_t scb, int rate);
76 static int hardwire_write (serial_t scb, const char *str, int len);
77 static void hardwire_close (serial_t scb);
78 static int get_tty_state (serial_t scb, struct hardwire_ttystate * state);
79 static int set_tty_state (serial_t scb, struct hardwire_ttystate * state);
80 static serial_ttystate hardwire_get_tty_state (serial_t scb);
81 static int hardwire_set_tty_state (serial_t scb, serial_ttystate state);
82 static int hardwire_noflush_set_tty_state (serial_t, serial_ttystate,
84 static void hardwire_print_tty_state (serial_t, serial_ttystate, struct gdb_file *);
85 static int hardwire_drain_output (serial_t);
86 static int hardwire_flush_output (serial_t);
87 static int hardwire_flush_input (serial_t);
88 static int hardwire_send_break (serial_t);
89 static int hardwire_setstopbits (serial_t, int);
91 void _initialize_ser_hardwire (void);
93 extern int (*ui_loop_hook) (int);
95 /* Open up a real live device for serial I/O */
98 hardwire_open (serial_t scb, const char *name)
100 scb->fd = open (name, O_RDWR);
108 get_tty_state (serial_t scb, struct hardwire_ttystate *state)
111 if (tcgetattr (scb->fd, &state->termios) < 0)
118 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
124 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
126 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
128 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
130 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
138 set_tty_state (serial_t scb, struct hardwire_ttystate *state)
141 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
148 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
154 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
156 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
158 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
160 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
167 static serial_ttystate
168 hardwire_get_tty_state (serial_t scb)
170 struct hardwire_ttystate *state;
172 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
174 if (get_tty_state (scb, state))
177 return (serial_ttystate) state;
181 hardwire_set_tty_state (serial_t scb, serial_ttystate ttystate)
183 struct hardwire_ttystate *state;
185 state = (struct hardwire_ttystate *) ttystate;
187 return set_tty_state (scb, state);
191 hardwire_noflush_set_tty_state (serial_t scb,
192 serial_ttystate new_ttystate,
193 serial_ttystate old_ttystate)
195 struct hardwire_ttystate new_state;
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
200 new_state = *(struct hardwire_ttystate *) new_ttystate;
202 /* Don't change in or out of raw mode; we don't want to flush input.
203 termio and termios have no such restriction; for them flushing input
204 is separate from setting the attributes. */
207 if (state->sgttyb.sg_flags & RAW)
208 new_state.sgttyb.sg_flags |= RAW;
210 new_state.sgttyb.sg_flags &= ~RAW;
212 /* I'm not sure whether this is necessary; the manpage just mentions
214 if (state->sgttyb.sg_flags & CBREAK)
215 new_state.sgttyb.sg_flags |= CBREAK;
217 new_state.sgttyb.sg_flags &= ~CBREAK;
220 return set_tty_state (scb, &new_state);
224 hardwire_print_tty_state (serial_t scb,
225 serial_ttystate ttystate,
226 struct gdb_file *stream)
228 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
232 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
233 state->termios.c_iflag, state->termios.c_oflag);
234 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
235 state->termios.c_cflag, state->termios.c_lflag);
237 /* This not in POSIX, and is not really documented by those systems
238 which have it (at least not Sun). */
239 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
241 fprintf_filtered (stream, "c_cc: ");
242 for (i = 0; i < NCCS; i += 1)
243 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
244 fprintf_filtered (stream, "\n");
248 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
249 state->termio.c_iflag, state->termio.c_oflag);
250 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
251 state->termio.c_cflag, state->termio.c_lflag,
252 state->termio.c_line);
253 fprintf_filtered (stream, "c_cc: ");
254 for (i = 0; i < NCC; i += 1)
255 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
256 fprintf_filtered (stream, "\n");
260 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
261 state->sgttyb.sg_flags);
263 fprintf_filtered (stream, "tchars: ");
264 for (i = 0; i < (int) sizeof (struct tchars); i++)
265 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
266 fprintf_filtered ("\n");
268 fprintf_filtered (stream, "ltchars: ");
269 for (i = 0; i < (int) sizeof (struct ltchars); i++)
270 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
271 fprintf_filtered (stream, "\n");
273 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
277 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
280 hardwire_drain_output (serial_t scb)
283 return tcdrain (scb->fd);
287 return ioctl (scb->fd, TCSBRK, 1);
291 /* Get the current state and then restore it using TIOCSETP,
292 which should cause the output to drain and pending input
295 struct hardwire_ttystate state;
296 if (get_tty_state (scb, &state))
302 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
309 hardwire_flush_output (serial_t scb)
312 return tcflush (scb->fd, TCOFLUSH);
316 return ioctl (scb->fd, TCFLSH, 1);
320 /* This flushes both input and output, but we can't do better. */
321 return ioctl (scb->fd, TIOCFLUSH, 0);
326 hardwire_flush_input (serial_t scb)
329 scb->bufp = scb->buf;
332 return tcflush (scb->fd, TCIFLUSH);
336 return ioctl (scb->fd, TCFLSH, 0);
340 /* This flushes both input and output, but we can't do better. */
341 return ioctl (scb->fd, TIOCFLUSH, 0);
346 hardwire_send_break (serial_t scb)
349 return tcsendbreak (scb->fd, 0);
353 return ioctl (scb->fd, TCSBRK, 0);
359 struct timeval timeout;
361 status = ioctl (scb->fd, TIOCSBRK, 0);
363 /* Can't use usleep; it doesn't exist in BSD 4.2. */
364 /* Note that if this select() is interrupted by a signal it will not wait
365 the full length of time. I think that is OK. */
367 timeout.tv_usec = 250000;
368 select (0, 0, 0, 0, &timeout);
369 status = ioctl (scb->fd, TIOCCBRK, 0);
376 hardwire_raw (serial_t scb)
378 struct hardwire_ttystate state;
380 if (get_tty_state (scb, &state))
381 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
384 state.termios.c_iflag = 0;
385 state.termios.c_oflag = 0;
386 state.termios.c_lflag = 0;
387 state.termios.c_cflag &= ~(CSIZE | PARENB);
388 state.termios.c_cflag |= CLOCAL | CS8;
389 state.termios.c_cc[VMIN] = 0;
390 state.termios.c_cc[VTIME] = 0;
394 state.termio.c_iflag = 0;
395 state.termio.c_oflag = 0;
396 state.termio.c_lflag = 0;
397 state.termio.c_cflag &= ~(CSIZE | PARENB);
398 state.termio.c_cflag |= CLOCAL | CS8;
399 state.termio.c_cc[VMIN] = 0;
400 state.termio.c_cc[VTIME] = 0;
404 state.sgttyb.sg_flags |= RAW | ANYP;
405 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
408 scb->current_timeout = 0;
410 if (set_tty_state (scb, &state))
411 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
414 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
415 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
417 For termio{s}, we actually just setup VTIME if necessary, and let the
418 timeout occur in the read() in hardwire_read().
421 /* FIXME: Don't replace this with the equivalent ser_unix*() until the
422 old TERMIOS/SGTTY/... timer code has been flushed. cagney
426 wait_for (serial_t scb, int timeout)
438 FD_SET (scb->fd, &readfds);
445 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
447 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
451 return SERIAL_TIMEOUT;
452 else if (errno == EINTR)
455 return SERIAL_ERROR; /* Got an error from select or poll */
460 #endif /* HAVE_SGTTY */
462 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
463 if (timeout == scb->current_timeout)
466 scb->current_timeout = timeout;
469 struct hardwire_ttystate state;
471 if (get_tty_state (scb, &state))
472 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
478 state.termios.c_cc[VTIME] = 0;
479 state.termios.c_cc[VMIN] = 1;
483 state.termios.c_cc[VMIN] = 0;
484 state.termios.c_cc[VTIME] = timeout * 10;
485 if (state.termios.c_cc[VTIME] != timeout * 10)
488 /* If c_cc is an 8-bit signed character, we can't go
489 bigger than this. If it is always unsigned, we could use
492 scb->current_timeout = 12;
493 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
494 scb->timeout_remaining = timeout - scb->current_timeout;
503 state.termio.c_cc[VTIME] = 0;
504 state.termio.c_cc[VMIN] = 1;
508 state.termio.c_cc[VMIN] = 0;
509 state.termio.c_cc[VTIME] = timeout * 10;
510 if (state.termio.c_cc[VTIME] != timeout * 10)
512 /* If c_cc is an 8-bit signed character, we can't go
513 bigger than this. If it is always unsigned, we could use
516 scb->current_timeout = 12;
517 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
518 scb->timeout_remaining = timeout - scb->current_timeout;
523 if (set_tty_state (scb, &state))
524 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
528 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
531 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
532 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
533 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
534 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
536 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
537 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
540 /* NOTE: cagney/1999-09-16: This function is not identical to
541 ser_unix_readchar() as part of replacing it with ser_unix*()
542 merging will be required - this code handles the case where read()
543 times out due to no data while ser_unix_readchar() doesn't expect
547 hardwire_readchar (serial_t scb, int timeout)
552 if (scb->bufcnt-- > 0)
558 /* We have to be able to keep the GUI alive here, so we break the original
559 timeout into steps of 1 second, running the "keep the GUI alive" hook
560 each time through the loop.
561 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
562 will only go through the loop once. */
564 delta = (timeout == 0 ? 0 : 1);
568 /* N.B. The UI may destroy our world (for instance by calling
569 remote_stop,) in which case we want to get out of here as
570 quickly as possible. It is not safe to touch scb, since
571 someone else might have freed it. The ui_loop_hook signals that
572 we should exit by returning 1. */
575 detach = ui_loop_hook (0);
578 return SERIAL_TIMEOUT;
580 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
581 status = wait_for (scb, delta);
586 /* NOTE: cagney/1999-09-17: See ser_unix_readchar() for reason
587 why ASYNC reads are character by character. */
589 scb->bufcnt = read (scb->fd, scb->buf,
590 (SERIAL_IS_ASYNC_P (scb) ? 1 : BUFSIZ));
592 if (scb->bufcnt <= 0)
594 if (scb->bufcnt == 0)
596 /* Zero characters means timeout (it could also be EOF, but
597 we don't (yet at least) distinguish). */
598 if (scb->timeout_remaining > 0)
600 timeout = scb->timeout_remaining;
603 else if (scb->timeout_remaining < 0)
606 return SERIAL_TIMEOUT;
608 else if (errno == EINTR)
611 return SERIAL_ERROR; /* Got an error from read */
615 scb->bufp = scb->buf;
628 /* Translate baud rates from integers to damn B_codes. Unix should
629 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
729 rate_to_code (int rate)
733 for (i = 0; baudtab[i].rate != -1; i++)
734 if (rate == baudtab[i].rate)
735 return baudtab[i].code;
741 hardwire_setbaudrate (serial_t scb, int rate)
743 struct hardwire_ttystate state;
745 if (get_tty_state (scb, &state))
749 cfsetospeed (&state.termios, rate_to_code (rate));
750 cfsetispeed (&state.termios, rate_to_code (rate));
758 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
759 state.termio.c_cflag |= rate_to_code (rate);
763 state.sgttyb.sg_ispeed = rate_to_code (rate);
764 state.sgttyb.sg_ospeed = rate_to_code (rate);
767 return set_tty_state (scb, &state);
771 hardwire_setstopbits (scb, num)
775 struct hardwire_ttystate state;
778 if (get_tty_state (scb, &state))
783 case SERIAL_1_STOPBITS:
786 case SERIAL_1_AND_A_HALF_STOPBITS:
787 case SERIAL_2_STOPBITS:
796 state.termios.c_cflag &= ~CSTOPB;
798 state.termios.c_cflag |= CSTOPB; /* two bits */
803 state.termio.c_cflag &= ~CSTOPB;
805 state.termio.c_cflag |= CSTOPB; /* two bits */
809 return 0; /* sgtty doesn't support this */
812 return set_tty_state (scb, &state);
815 /* FIXME: Don't replace this with the equivalent ser_unix*() until the
816 old TERMIOS/SGTTY/... timer code has been flushed. cagney
820 hardwire_write (serial_t scb, const char *str, int len)
826 cc = write (scb->fd, str, len);
838 hardwire_close (serial_t scb)
848 /* Generic operations used by all UNIX/FD based serial interfaces. */
851 ser_unix_nop_get_tty_state (serial_t scb)
853 /* allocate a dummy */
854 return (serial_ttystate) XMALLOC (int);
858 ser_unix_nop_set_tty_state (serial_t scb, serial_ttystate ttystate)
864 ser_unix_nop_raw (serial_t scb)
866 return; /* Always in raw mode */
869 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
870 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
873 ser_unix_wait_for (serial_t scb, int timeout)
877 fd_set readfds, exceptfds;
880 FD_ZERO (&exceptfds);
885 FD_SET (scb->fd, &readfds);
886 FD_SET (scb->fd, &exceptfds);
891 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
893 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
898 return SERIAL_TIMEOUT;
899 else if (errno == EINTR)
902 return SERIAL_ERROR; /* Got an error from select or poll */
909 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
910 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
911 char if successful. Returns -2 if timeout expired, EOF if line dropped
912 dead, or -3 for any other error (see errno in that case). */
915 ser_unix_readchar (serial_t scb, int timeout)
920 if (scb->bufcnt-- > 0)
923 /* We have to be able to keep the GUI alive here, so we break the original
924 timeout into steps of 1 second, running the "keep the GUI alive" hook
925 each time through the loop.
927 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
928 will only go through the loop once. */
930 delta = (timeout == 0 ? 0 : 1);
934 /* N.B. The UI may destroy our world (for instance by calling
935 remote_stop,) in which case we want to get out of here as
936 quickly as possible. It is not safe to touch scb, since
937 someone else might have freed it. The ui_loop_hook signals that
938 we should exit by returning 1. */
942 if (ui_loop_hook (0))
943 return SERIAL_TIMEOUT;
946 status = ser_unix_wait_for (scb, delta);
949 /* If we got a character or an error back from wait_for, then we can
950 break from the loop before the timeout is completed. */
952 if (status != SERIAL_TIMEOUT)
957 /* If we have exhausted the original timeout, then generate
958 a SERIAL_TIMEOUT, and pass it out of the loop. */
960 else if (timeout == 0)
962 status = SERIAL_TIMEOUT;
972 /* FIXME: cagney/1999-09-17: ASYNC: The ASYNC serial code needs
973 to be modified so that it agressivly tries to drain its local
974 input buffer. Until this is done, the read() below can only
975 take in single characters. This is to ensure that
976 unprocessed data doesn't end up sitting in the input fifo. */
977 scb->bufcnt = read (scb->fd, scb->buf,
978 (SERIAL_IS_ASYNC_P (scb) ? 1 : BUFSIZ));
979 if (scb->bufcnt != -1 || errno != EINTR)
983 if (scb->bufcnt <= 0)
985 if (scb->bufcnt == 0)
986 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
987 distinguish between EOF & timeouts
990 return SERIAL_ERROR; /* Got an error from read */
994 scb->bufp = scb->buf;
999 ser_unix_nop_noflush_set_tty_state (serial_t scb,
1000 serial_ttystate new_ttystate,
1001 serial_ttystate old_ttystate)
1007 ser_unix_nop_print_tty_state (serial_t scb,
1008 serial_ttystate ttystate,
1009 struct gdb_file *stream)
1011 /* Nothing to print. */
1016 ser_unix_nop_setbaudrate (serial_t scb, int rate)
1018 return 0; /* Never fails! */
1022 ser_unix_nop_setstopbits (serial_t scb, int num)
1024 return 0; /* Never fails! */
1028 ser_unix_write (serial_t scb, const char *str, int len)
1034 cc = write (scb->fd, str, len);
1045 ser_unix_nop_flush_output (serial_t scb)
1051 ser_unix_nop_flush_input (serial_t scb)
1057 ser_unix_nop_send_break (serial_t scb)
1063 ser_unix_nop_drain_output (serial_t scb)
1069 ser_unix_event (int error, int fd, gdb_client_data context)
1071 serial_t scb = context;
1072 scb->async_handler (error, scb->async_context, fd);
1076 ser_unix_async (serial_t scb,
1081 add_file_handler (scb->fd, ser_unix_event, scb);
1085 delete_file_handler (scb->fd);
1090 _initialize_ser_hardwire (void)
1092 struct serial_ops *ops = XMALLOC (struct serial_ops);
1093 memset (ops, sizeof (struct serial_ops), 0);
1094 ops->name = "hardwire";
1096 ops->open = hardwire_open;
1097 ops->close = hardwire_close;
1098 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1099 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1101 ops->readchar = hardwire_readchar;
1102 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1103 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1105 ops->write = hardwire_write;
1106 ops->flush_output = hardwire_flush_output;
1107 ops->flush_input = hardwire_flush_input;
1108 ops->send_break = hardwire_send_break;
1109 ops->go_raw = hardwire_raw;
1110 ops->get_tty_state = hardwire_get_tty_state;
1111 ops->set_tty_state = hardwire_set_tty_state;
1112 ops->print_tty_state = hardwire_print_tty_state;
1113 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1114 ops->setbaudrate = hardwire_setbaudrate;
1115 ops->setstopbits = hardwire_setstopbits;
1116 ops->drain_output = hardwire_drain_output;
1117 ops->async = ser_unix_async;
1118 serial_add_interface (ops);