/* Serial interface for local (hardwired) serial ports on Un*x like systems
- Copyright 1992, 1993 Free Software Foundation, Inc.
+ Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
This file is part of GDB.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include "defs.h"
#include "serial.h"
#include <fcntl.h>
#include <sys/types.h>
-
-#if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
-#define HAVE_SGTTY
+#include "terminal.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
#endif
#ifdef HAVE_TERMIOS
-#include <termios.h>
-#include <unistd.h>
struct hardwire_ttystate
{
#endif /* termios */
#ifdef HAVE_TERMIO
-#include <termio.h>
/* It is believed that all systems which have added job control to SVR3
(e.g. sco) have also added termios. Even if not, trying to figure out
too if it existed on all systems. */
#include <sys/time.h>
-#include <sgtty.h>
-
struct hardwire_ttystate
{
struct sgttyb sgttyb;
static int rate_to_code PARAMS ((int rate));
static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
-/* FIXME: static void hardwire_restore PARAMS ((serial_t scb)); */
static void hardwire_close PARAMS ((serial_t scb));
static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
+static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
+ serial_ttystate));
+static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
+static int hardwire_flush_output PARAMS ((serial_t));
+static int hardwire_flush_input PARAMS ((serial_t));
+static int hardwire_send_break PARAMS ((serial_t));
+static int hardwire_setstopbits PARAMS ((serial_t, int));
/* Open up a real live device for serial I/O */
serial_ttystate old_ttystate;
{
struct hardwire_ttystate new_state;
+#ifdef HAVE_SGTTY
struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
+#endif
new_state = *(struct hardwire_ttystate *)new_ttystate;
hardwire_flush_input (scb)
serial_t scb;
{
+ scb->bufcnt = 0;
+ scb->bufp = scb->buf;
+
#ifdef HAVE_TERMIOS
return tcflush (scb->fd, TCIFLUSH);
#endif
state.termios.c_oflag = 0;
state.termios.c_lflag = 0;
state.termios.c_cflag &= ~(CSIZE|PARENB);
- state.termios.c_cflag |= CS8;
+ state.termios.c_cflag |= CLOCAL | CS8;
state.termios.c_cc[VMIN] = 0;
state.termios.c_cc[VTIME] = 0;
#endif
state.termio.c_oflag = 0;
state.termio.c_lflag = 0;
state.termio.c_cflag &= ~(CSIZE|PARENB);
- state.termio.c_cflag |= CS8;
+ state.termio.c_cflag |= CLOCAL | CS8;
state.termio.c_cc[VMIN] = 0;
state.termio.c_cc[VTIME] = 0;
#endif
serial_t scb;
int timeout;
{
+ scb->timeout_remaining = 0;
+
#ifdef HAVE_SGTTY
- struct timeval tv;
- fd_set readfds;
+ {
+ struct timeval tv;
+ fd_set readfds;
- FD_ZERO (&readfds);
+ FD_ZERO (&readfds);
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
- FD_SET(scb->fd, &readfds);
+ FD_SET(scb->fd, &readfds);
- while (1)
- {
- int numfds;
-
- if (timeout >= 0)
- numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
- else
- numfds = select(scb->fd+1, &readfds, 0, 0, 0);
-
- if (numfds <= 0)
- if (numfds == 0)
- return SERIAL_TIMEOUT;
- else if (errno == EINTR)
- continue;
+ while (1)
+ {
+ int numfds;
+
+ if (timeout >= 0)
+ numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
else
- return SERIAL_ERROR; /* Got an error from select or poll */
+ numfds = select(scb->fd+1, &readfds, 0, 0, 0);
- return 0;
- }
+ if (numfds <= 0)
+ if (numfds == 0)
+ return SERIAL_TIMEOUT;
+ else if (errno == EINTR)
+ continue;
+ else
+ return SERIAL_ERROR; /* Got an error from select or poll */
+ return 0;
+ }
+ }
#endif /* HAVE_SGTTY */
#if defined HAVE_TERMIO || defined HAVE_TERMIOS
if (timeout == scb->current_timeout)
return 0;
+ scb->current_timeout = timeout;
+
{
struct hardwire_ttystate state;
state.termios.c_cc[VTIME] = timeout * 10;
if (state.termios.c_cc[VTIME] != timeout * 10)
{
- warning ("Timeout value %d too large, using %d", timeout,
- state.termios.c_cc[VTIME] / 10);
+
+ /* If c_cc is an 8-bit signed character, we can't go
+ bigger than this. If it is always unsigned, we could use
+ 25. */
+
+ scb->current_timeout = 12;
+ state.termios.c_cc[VTIME] = scb->current_timeout * 10;
+ scb->timeout_remaining = timeout - scb->current_timeout;
}
}
#endif
state.termio.c_cc[VTIME] = timeout * 10;
if (state.termio.c_cc[VTIME] != timeout * 10)
{
- warning ("Timeout value %d too large, using %d", timeout,
- state.termio.c_cc[VTIME] / 10);
+ /* If c_cc is an 8-bit signed character, we can't go
+ bigger than this. If it is always unsigned, we could use
+ 25. */
+
+ scb->current_timeout = 12;
+ state.termio.c_cc[VTIME] = scb->current_timeout * 10;
+ scb->timeout_remaining = timeout - scb->current_timeout;
}
}
#endif
- scb->current_timeout = timeout;
-
if (set_tty_state (scb, &state))
fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
if (scb->bufcnt-- > 0)
return *scb->bufp++;
- status = wait_for(scb, timeout);
-
- if (status < 0)
- return status;
-
- scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
-
- if (scb->bufcnt <= 0)
- if (scb->bufcnt == 0)
- return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
- distinguish between EOF & timeouts
- someday] */
- else
- return SERIAL_ERROR; /* Got an error from read */
-
- scb->bufcnt--;
- scb->bufp = scb->buf;
- return *scb->bufp++;
+ while (1)
+ {
+ status = wait_for (scb, timeout);
+
+ if (status < 0)
+ return status;
+
+ scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
+
+ if (scb->bufcnt <= 0)
+ {
+ if (scb->bufcnt == 0)
+ {
+ /* Zero characters means timeout (it could also be EOF, but
+ we don't (yet at least) distinguish). */
+ if (scb->timeout_remaining > 0)
+ {
+ timeout = scb->timeout_remaining;
+ continue;
+ }
+ else
+ return SERIAL_TIMEOUT;
+ }
+ else if (errno == EINTR)
+ continue;
+ else
+ return SERIAL_ERROR; /* Got an error from read */
+ }
+
+ scb->bufcnt--;
+ scb->bufp = scb->buf;
+ return *scb->bufp++;
+ }
}
#ifndef B19200
return set_tty_state (scb, &state);
}
+static int
+hardwire_setstopbits(scb, num)
+ serial_t scb;
+ int num;
+{
+ struct hardwire_ttystate state;
+ int newbit;
+
+ if (get_tty_state(scb, &state))
+ return -1;
+
+ switch (num)
+ {
+ case SERIAL_1_STOPBITS:
+ newbit = 0;
+ break;
+ case SERIAL_1_AND_A_HALF_STOPBITS:
+ case SERIAL_2_STOPBITS:
+ newbit = 1;
+ break;
+ default:
+ return 1;
+ }
+
+#ifdef HAVE_TERMIOS
+ if (!newbit)
+ state.termios.c_cflag &= ~CSTOPB;
+ else
+ state.termios.c_cflag |= CSTOPB; /* two bits */
+#endif
+
+#ifdef HAVE_TERMIO
+ if (!newbit)
+ state.termio.c_cflag &= ~CSTOPB;
+ else
+ state.termio.c_cflag |= CSTOPB; /* two bits */
+#endif
+
+#ifdef HAVE_SGTTY
+ return 0; /* sgtty doesn't support this */
+#endif
+
+ return set_tty_state (scb, &state);
+}
+
static int
hardwire_write(scb, str, len)
serial_t scb;
hardwire_print_tty_state,
hardwire_noflush_set_tty_state,
hardwire_setbaudrate,
+ hardwire_setstopbits,
};
void