* ser-unix.c (wait_for): New routine to handle read timeouts,
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/time.h>
25
26 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
27 #define HAVE_SGTTY
28 #endif
29
30 #ifdef HAVE_TERMIOS
31 #include <termios.h>
32 #include <unistd.h>
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_TERMIO
36 #include <termio.h>
37 #include <poll.h>
38 #endif
39 #ifdef HAVE_SGTTY
40 #include <sgtty.h>
41 #endif
42
43 static int hardwire_open PARAMS ((serial_t scb, const char *name));
44 static void hardwire_raw PARAMS ((serial_t scb));
45 static int wait_for PARAMS ((serial_t scb, int timeout));
46 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
47 static int rate_to_code PARAMS ((int rate));
48 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
49 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
50 static void hardwire_restore PARAMS ((serial_t scb));
51 static void hardwire_close PARAMS ((serial_t scb));
52
53 /* Open up a real live device for serial I/O */
54
55 static int
56 hardwire_open(scb, name)
57 serial_t scb;
58 const char *name;
59 {
60 scb->fd = open (name, O_RDWR);
61 if (scb->fd < 0)
62 return -1;
63
64 return 0;
65 }
66
67 static void
68 hardwire_raw(scb)
69 serial_t scb;
70 {
71 #ifdef HAVE_TERMIOS
72 struct termios termios;
73
74 if (tcgetattr(scb->fd, &termios))
75 {
76 fprintf(stderr, "tcgetattr failed: %s\n", safe_strerror(errno));
77 }
78
79 termios.c_iflag = 0;
80 termios.c_oflag = 0;
81 termios.c_lflag = 0;
82 termios.c_cflag &= ~(CSIZE|PARENB);
83 termios.c_cflag |= CS8;
84 termios.c_cc[VMIN] = 0;
85 termios.c_cc[VTIME] = 0;
86
87 if (tcsetattr(scb->fd, TCSANOW, &termios))
88 {
89 fprintf(stderr, "tcsetattr failed: %s\n", safe_strerror(errno));
90 }
91 #endif
92
93 #ifdef HAVE_TERMIO
94 struct termio termio;
95
96 if (ioctl (scb->fd, TCGETA, &termio))
97 {
98 fprintf(stderr, "TCGETA failed: %s\n", safe_strerror(errno));
99 }
100
101 termio.c_iflag = 0;
102 termio.c_oflag = 0;
103 termio.c_lflag = 0;
104 termio.c_cflag &= ~(CSIZE|PARENB);
105 termio.c_cflag |= CS8;
106 termio.c_cc[VMIN] = 0;
107 termio.c_cc[VTIME] = 0;
108
109 if (ioctl (scb->fd, TCSETA, &termio))
110 {
111 fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno));
112 }
113 #endif
114
115 #ifdef HAVE_SGTTY
116 struct sgttyb sgttyb;
117
118 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
119 fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno));
120
121 sgttyb.sg_flags |= RAW | ANYP;
122 sgttyb.sg_flags &= ~(CBREAK | ECHO);
123
124 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
125 fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno));
126 #endif
127 }
128
129 /* Wait for input on scb, with timeout seconds */
130
131 static int
132 wait_for(scb, timeout)
133 serial_t scb;
134 int timeout;
135 {
136 int numfds;
137
138 #ifdef HAVE_SGTTY
139 struct timeval tv;
140 fd_set readfds;
141
142 FD_ZERO (&readfds);
143
144 tv.tv_sec = timeout;
145 tv.tv_usec = 0;
146
147 FD_SET(scb->fd, &readfds);
148
149 if (timeout >= 0)
150 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
151 else
152 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
153
154 #endif /* HAVE_SGTTY */
155
156 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
157 struct pollfd pollfd;
158
159 pollfd.fd = scb->fd;
160 pollfd.events = POLLIN | POLLPRI;
161
162 if (timeout > 0)
163 timeout *= 1000;
164
165 numfds = poll(&pollfd, 1, timeout);
166
167 #endif /* defined HAVE_TERMIO || defined HAVE_TERMIOS */
168
169 if (numfds <= 0)
170 if (numfds == 0)
171 return SERIAL_TIMEOUT;
172 else
173 return SERIAL_ERROR; /* Got an error from select or poll */
174 }
175
176 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
177 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
178 char if successful. Returns -2 if timeout expired, EOF if line dropped
179 dead, or -3 for any other error (see errno in that case). */
180
181 static int
182 hardwire_readchar(scb, timeout)
183 serial_t scb;
184 int timeout;
185 {
186 int status;
187
188 if (scb->bufcnt-- > 0)
189 return *scb->bufp++;
190
191 status = wait_for(scb, timeout);
192
193 if (status < 0)
194 return status;
195
196 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
197
198 if (scb->bufcnt <= 0)
199 if (scb->bufcnt == 0)
200 return SERIAL_EOF; /* 0 chars means end of file */
201 else
202 return SERIAL_ERROR; /* Got an error from read */
203
204 scb->bufcnt--;
205 scb->bufp = scb->buf;
206 return *scb->bufp++;
207 }
208
209 #ifndef B19200
210 #define B19200 EXTA
211 #endif
212
213 #ifndef B38400
214 #define B38400 EXTB
215 #endif
216
217 /* Translate baud rates from integers to damn B_codes. Unix should
218 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
219
220 static struct
221 {
222 int rate;
223 int code;
224 }
225 baudtab[] =
226 {
227 {50, B50},
228 {75, B75},
229 {110, B110},
230 {134, B134},
231 {150, B150},
232 {200, B200},
233 {300, B300},
234 {600, B600},
235 {1200, B1200},
236 {1800, B1800},
237 {2400, B2400},
238 {4800, B4800},
239 {9600, B9600},
240 {19200, B19200},
241 {38400, B38400},
242 {-1, -1},
243 };
244
245 static int
246 rate_to_code(rate)
247 int rate;
248 {
249 int i;
250
251 for (i = 0; baudtab[i].rate != -1; i++)
252 if (rate == baudtab[i].rate)
253 return baudtab[i].code;
254
255 return -1;
256 }
257
258 static int
259 hardwire_setbaudrate(scb, rate)
260 serial_t scb;
261 int rate;
262 {
263 #ifdef HAVE_TERMIOS
264 struct termios termios;
265
266 if (tcgetattr (scb->fd, &termios))
267 return -1;
268
269 cfsetospeed (&termios, rate_to_code (rate));
270 cfsetispeed (&termios, rate_to_code (rate));
271
272 if (tcsetattr (scb->fd, TCSANOW, &termios))
273 return -1;
274 #endif
275
276 #ifdef HAVE_TERMIO
277 struct termio termio;
278
279 if (ioctl (scb->fd, TCGETA, &termio))
280 return -1;
281
282 #ifndef CIBAUD
283 #define CIBAUD CBAUD
284 #endif
285
286 termio.c_cflag &= ~(CBAUD | CIBAUD);
287 termio.c_cflag |= rate_to_code (rate);
288
289 if (ioctl (scb->fd, TCSETA, &termio))
290 return -1;
291 #endif
292
293 #ifdef HAVE_SGTTY
294 struct sgttyb sgttyb;
295
296 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
297 return -1;
298
299 sgttyb.sg_ispeed = rate_to_code (rate);
300 sgttyb.sg_ospeed = rate_to_code (rate);
301
302 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
303 return -1;
304 #endif
305 return 0;
306 }
307
308 static int
309 hardwire_write(scb, str, len)
310 serial_t scb;
311 const char *str;
312 int len;
313 {
314 int cc;
315
316 while (len > 0)
317 {
318 cc = write(scb->fd, str, len);
319
320 if (cc < 0)
321 return 1;
322 len -= cc;
323 str += cc;
324 }
325 return 0;
326 }
327
328 static void
329 hardwire_restore(scb)
330 serial_t scb;
331 {
332 }
333
334 static void
335 hardwire_close(scb)
336 serial_t scb;
337 {
338 if (scb->fd < 0)
339 return;
340
341 close(scb->fd);
342 scb->fd = -1;
343 }
344
345 static struct serial_ops hardwire_ops =
346 {
347 "hardwire",
348 0,
349 hardwire_open,
350 hardwire_close,
351 hardwire_readchar,
352 hardwire_write,
353 hardwire_raw,
354 hardwire_restore,
355 hardwire_setbaudrate
356 };
357
358 void
359 _initialize_ser_hardwire ()
360 {
361 serial_add_interface (&hardwire_ops);
362 }
This page took 0.060346 seconds and 5 git commands to generate.