1 /* Remote serial interface for OS's with termios, for GDB.
2 Copyright 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 serial_raw(fd, oldstate)
30 struct ttystate *oldstate;
32 struct termios termios;
34 oldstate->flags = fcntl(fd, F_GETFL, 0);
36 fcntl(fd, F_SETFL, oldstate->flags|FNDELAY);
38 if (tcgetattr(fd, &termios))
40 fprintf(stderr, "tcgetattr failed: %s\n", safe_strerror(errno));
43 oldstate->termios = termios;
48 termios.c_cc[VMIN] = 0;
49 termios.c_cc[VTIME] = 0;
51 if (tcsetattr(fd, TCSANOW, &termios))
53 fprintf(stderr, "tcsetattr failed: %s\n", safe_strerror(errno));
58 serial_restore(fd, oldstate)
60 struct ttystate *oldstate;
62 fcntl(fd, F_SETFL, oldstate->flags);
64 tcsetattr(fd, TCSANOW, &oldstate->termios);
67 static struct ttystate oldstate;
69 static fd_set readfds;
75 struct termios termios;
77 desc = open (name, O_RDWR);
79 error("Open of %s failed: %s", name, safe_strerror(errno));
81 serial_raw(desc, &oldstate);
83 /* Setup constant stuff for select */
90 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
91 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
92 char if successful. Returns -2 if timeout expired, EOF if line dropped
93 dead, or -3 for any other error (see errno in that case). */
96 serial_readchar(timeout)
99 static unsigned char buf[BUFSIZ];
100 static unsigned char *bufp;
101 static int bufcnt = 0;
111 FD_SET(desc, &readfds);
114 numfds = select(desc+1, &readfds, 0, 0, &tv);
116 numfds = select(desc+1, &readfds, 0, 0, 0);
120 return -2; /* Timeout */
122 return -3; /* Got an error from select */
124 bufcnt = read(desc, buf, BUFSIZ);
128 return EOF; /* 0 chars means end of file */
130 return -3; /* Got an error from read */
137 /* Translate baud rates from integers to damn B_codes. Unix should
138 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
169 for (i = 0; baudtab[i].rate != -1; i++)
170 if (rate == baudtab[i].rate)
171 return baudtab[i].code;
177 serial_setbaudrate(rate)
180 struct termios termios;
182 if (tcgetattr(desc, &termios))
183 error("tcgetattr failed: %s\n", safe_strerror(errno));
185 cfsetospeed(&termios, rate_to_code(rate));
186 cfsetispeed(&termios, rate_to_code(rate));
188 if (tcsetattr(desc, TCSANOW, &termios))
189 error("tcsetattr failed: %s\n", safe_strerror(errno));
195 serial_write(str, len)
203 cc = write(desc, str, len);
219 serial_restore(desc, &oldstate);