1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993 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. */
23 #include <sys/types.h>
25 /* Note: HAVE_SELECT is not yet defined on all the systems which could
26 define it. The USE_ALARM_TIMEOUT code seems to work OK, though, so
27 it doesn't really matter. */
28 #if !defined (HAVE_SELECT)
29 #if defined (HAVE_TERMIO)
30 #define USE_TERMIO_TIMEOUT 1
32 #define USE_ALARM_TIMEOUT 1
36 #if defined (HAVE_SELECT)
39 #ifdef USE_ALARM_TIMEOUT
43 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
52 #include <sys/termio.h>
58 /* Open up a real live device for serial I/O */
61 hardwire_open(scb
, name
)
65 scb
->fd
= open (name
, O_RDWR
);
77 struct termios termios
;
79 if (tcgetattr(scb
->fd
, &termios
))
81 fprintf(stderr
, "tcgetattr failed: %s\n", safe_strerror(errno
));
87 termios
.c_cflag
&= ~(CSIZE
|PARENB
);
88 termios
.c_cflag
|= CS8
;
89 termios
.c_cc
[VMIN
] = 0;
90 termios
.c_cc
[VTIME
] = 0;
92 if (tcsetattr(scb
->fd
, TCSANOW
, &termios
))
94 fprintf(stderr
, "tcsetattr failed: %s\n", safe_strerror(errno
));
101 if (ioctl (scb
->fd
, TCGETA
, &termio
))
103 fprintf(stderr
, "TCGETA failed: %s\n", safe_strerror(errno
));
109 termio
.c_cflag
&= ~(CSIZE
|PARENB
);
110 termio
.c_cflag
|= CS8
;
111 termio
.c_cc
[VMIN
] = 0;
112 termio
.c_cc
[VTIME
] = 0;
114 if (ioctl (scb
->fd
, TCSETA
, &termio
))
116 fprintf(stderr
, "TCSETA failed: %s\n", safe_strerror(errno
));
121 struct sgttyb sgttyb
;
123 if (ioctl (scb
->fd
, TIOCGETP
, &sgttyb
))
124 fprintf(stderr
, "TIOCGETP failed: %s\n", safe_strerror(errno
));
126 sgttyb
.sg_flags
|= RAW
| ANYP
;
127 sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
129 if (ioctl (scb
->fd
, TIOCSETP
, &sgttyb
))
130 fprintf(stderr
, "TIOCSETP failed: %s\n", safe_strerror(errno
));
134 #ifdef USE_ALARM_TIMEOUT
135 /* Called when SIGALRM sent. */
142 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
143 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
144 char if successful. Returns -2 if timeout expired, EOF if line dropped
145 dead, or -3 for any other error (see errno in that case). */
148 hardwire_readchar(scb
, timeout
)
152 if (scb
->bufcnt
-- > 0)
166 FD_SET(scb
->fd
, &readfds
);
169 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, &tv
);
171 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, 0);
175 return SERIAL_TIMEOUT
;
177 return SERIAL_ERROR
; /* Got an error from select */
179 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
181 #endif /* HAVE_SELECT. */
183 #ifdef USE_TERMIO_TIMEOUT
185 struct termio termio
;
187 if (ioctl (scb
->fd
, TCGETA
, &termio
))
189 fprintf(stderr
, "TCGETA failed: %s\n", safe_strerror(errno
));
192 termio
.c_cc
[VTIME
] = timeout
* 10;
194 if (ioctl (scb
->fd
, TCSETA
, &termio
))
196 fprintf(stderr
, "TCSETA failed: %s\n", safe_strerror(errno
));
199 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
200 if (scb
->bufcnt
== 0)
201 /* Can this also mean end of file? Does "end of file" have any
202 meaning with ICANON clear? */
203 return SERIAL_TIMEOUT
;
205 #endif /* USE_TERMIO_TIMEOUT. */
207 #ifdef USE_ALARM_TIMEOUT
209 void (*old_sigalrm_handler
) ();
211 #ifndef NO_SIGINTERRUPT
212 /* Cause SIGARLM to make read fail with EINTR. */
213 if (siginterrupt (SIGALRM
, 1) != 0)
214 fprintf (stderr
, "siginterrupt failed: %s\n", safe_strerror (errno
));
217 old_sigalrm_handler
= (void (*) ()) signal (SIGALRM
, remote_timer
);
218 if (old_sigalrm_handler
== (void (*) ()) -1)
219 fprintf (stderr
, "signal failed: %s\n", safe_strerror (errno
));
222 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
225 signal (SIGALRM
, old_sigalrm_handler
);
226 if (scb
->bufcnt
< 0 && errno
== EINTR
)
228 return SERIAL_TIMEOUT
;
231 #endif /* USE_ALARM_TIMEOUT */
233 if (scb
->bufcnt
<= 0)
234 if (scb
->bufcnt
== 0)
235 return SERIAL_EOF
; /* 0 chars means end of file */
237 return SERIAL_ERROR
; /* Got an error from read */
240 scb
->bufp
= scb
->buf
;
252 /* Translate baud rates from integers to damn B_codes. Unix should
253 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
286 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
287 if (rate
== baudtab
[i
].rate
)
288 return baudtab
[i
].code
;
294 hardwire_setbaudrate(scb
, rate
)
299 struct termios termios
;
301 if (tcgetattr (scb
->fd
, &termios
))
304 cfsetospeed (&termios
, rate_to_code (rate
));
305 cfsetispeed (&termios
, rate_to_code (rate
));
307 if (tcsetattr (scb
->fd
, TCSANOW
, &termios
))
312 struct termio termio
;
314 if (ioctl (scb
->fd
, TCGETA
, &termio
))
321 termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
322 termio
.c_cflag
|= rate_to_code (rate
);
324 if (ioctl (scb
->fd
, TCSETA
, &termio
))
329 struct sgttyb sgttyb
;
331 if (ioctl (scb
->fd
, TIOCGETP
, &sgttyb
))
334 sgttyb
.sg_ispeed
= rate_to_code (rate
);
335 sgttyb
.sg_ospeed
= rate_to_code (rate
);
337 if (ioctl (scb
->fd
, TIOCSETP
, &sgttyb
))
344 hardwire_write(scb
, str
, len
)
353 cc
= write(scb
->fd
, str
, len
);
364 hardwire_restore(scb
)
380 static struct serial_ops hardwire_ops
=
393 _initialize_ser_hardwire ()
395 serial_add_interface (&hardwire_ops
);
This page took 0.039331 seconds and 5 git commands to generate.