* ser-unix.c: Move #include of <sys/time.h> to HAVE_SGTTY section.
[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
25 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
26 #define HAVE_SGTTY
27 #endif
28
29 #ifdef HAVE_TERMIOS
30 #include <termios.h>
31 #include <unistd.h>
32
33 struct hardwire_ttystate
34 {
35 struct termios termios;
36 };
37 #endif
38
39 #ifdef HAVE_TERMIO
40 #include <termio.h>
41
42 struct hardwire_ttystate
43 {
44 struct termio termio;
45 };
46 #endif
47
48 #ifdef HAVE_SGTTY
49 /* Needed for the code which uses select(). We would include <sys/select.h>
50 too if it existed on all systems. */
51 #include <sys/time.h>
52
53 #include <sgtty.h>
54
55 struct hardwire_ttystate
56 {
57 struct sgttyb sgttyb;
58 };
59 #endif
60
61 static int hardwire_open PARAMS ((serial_t scb, const char *name));
62 static void hardwire_raw PARAMS ((serial_t scb));
63 static int wait_for PARAMS ((serial_t scb, int timeout));
64 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
65 static int rate_to_code PARAMS ((int rate));
66 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
67 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
68 static void hardwire_restore PARAMS ((serial_t scb));
69 static void hardwire_close PARAMS ((serial_t scb));
70 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
71 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
72 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
73 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
74
75 /* Open up a real live device for serial I/O */
76
77 static int
78 hardwire_open(scb, name)
79 serial_t scb;
80 const char *name;
81 {
82 scb->fd = open (name, O_RDWR);
83 if (scb->fd < 0)
84 return -1;
85
86 return 0;
87 }
88
89 static int
90 get_tty_state(scb, state)
91 serial_t scb;
92 struct hardwire_ttystate *state;
93 {
94 #ifdef HAVE_TERMIOS
95 return tcgetattr(scb->fd, &state->termios);
96 #endif
97
98 #ifdef HAVE_TERMIO
99 return ioctl (scb->fd, TCGETA, &state->termio);
100 #endif
101
102 #ifdef HAVE_SGTTY
103 return ioctl (scb->fd, TIOCGETP, &state->sgttyb);
104 #endif
105 }
106
107 static int
108 set_tty_state(scb, state)
109 serial_t scb;
110 struct hardwire_ttystate *state;
111 {
112 int err;
113
114 #ifdef HAVE_TERMIOS
115 return tcsetattr(scb->fd, TCSANOW, &state->termios);
116 #endif
117
118 #ifdef HAVE_TERMIO
119 return ioctl (scb->fd, TCSETA, &state->termio);
120 #endif
121
122 #ifdef HAVE_SGTTY
123 return ioctl (scb->fd, TIOCSETP, &state->sgttyb);
124 #endif
125 }
126
127 static serial_ttystate
128 hardwire_get_tty_state(scb)
129 serial_t scb;
130 {
131 struct hardwire_ttystate *state;
132
133 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
134
135 if (get_tty_state(scb, state))
136 return NULL;
137
138 return (serial_ttystate)state;
139 }
140
141 static int
142 hardwire_set_tty_state(scb, ttystate)
143 serial_t scb;
144 serial_ttystate ttystate;
145 {
146 struct hardwire_ttystate *state;
147
148 state = (struct hardwire_ttystate *)ttystate;
149
150 return set_tty_state(scb, state);
151 }
152
153 static void
154 hardwire_raw(scb)
155 serial_t scb;
156 {
157 struct hardwire_ttystate state;
158
159 if (get_tty_state(scb, &state))
160 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
161
162 #ifdef HAVE_TERMIOS
163 state.termios.c_iflag = 0;
164 state.termios.c_oflag = 0;
165 state.termios.c_lflag = 0;
166 state.termios.c_cflag &= ~(CSIZE|PARENB);
167 state.termios.c_cflag |= CS8;
168 state.termios.c_cc[VMIN] = 0;
169 state.termios.c_cc[VTIME] = 0;
170 #endif
171
172 #ifdef HAVE_TERMIO
173 state.termio.c_iflag = 0;
174 state.termio.c_oflag = 0;
175 state.termio.c_lflag = 0;
176 state.termio.c_cflag &= ~(CSIZE|PARENB);
177 state.termio.c_cflag |= CS8;
178 state.termio.c_cc[VMIN] = 0;
179 state.termio.c_cc[VTIME] = 0;
180 #endif
181
182 #ifdef HAVE_SGTTY
183 state.sgttyb.sg_flags |= RAW | ANYP;
184 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
185 #endif
186
187 scb->current_timeout = 0;
188
189 if (set_tty_state (scb, &state))
190 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
191 }
192
193 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
194 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
195
196 For termio{s}, we actually just setup VTIME if necessary, and let the
197 timeout occur in the read() in hardwire_read().
198 */
199
200 static int
201 wait_for(scb, timeout)
202 serial_t scb;
203 int timeout;
204 {
205 int numfds;
206
207 #ifdef HAVE_SGTTY
208 struct timeval tv;
209 fd_set readfds;
210
211 FD_ZERO (&readfds);
212
213 tv.tv_sec = timeout;
214 tv.tv_usec = 0;
215
216 FD_SET(scb->fd, &readfds);
217
218 while (1)
219 {
220 if (timeout >= 0)
221 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
222 else
223 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
224
225 if (numfds <= 0)
226 if (numfds == 0)
227 return SERIAL_TIMEOUT;
228 else if (errno == EINTR)
229 continue;
230 else
231 return SERIAL_ERROR; /* Got an error from select or poll */
232
233 return 0;
234 }
235
236 #endif /* HAVE_SGTTY */
237
238 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
239 if (timeout == scb->current_timeout)
240 return 0;
241
242 {
243 struct hardwire_ttystate state;
244
245 if (get_tty_state(scb, &state))
246 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
247
248 #ifdef HAVE_TERMIOS
249 state.termios.c_cc[VTIME] = timeout * 10;
250 #endif
251
252 #ifdef HAVE_TERMIO
253 state.termio.c_cc[VTIME] = timeout * 10;
254 #endif
255
256 scb->current_timeout = timeout;
257
258 if (set_tty_state (scb, &state))
259 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
260
261 return 0;
262 }
263 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
264 }
265
266 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
267 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
268 char if successful. Returns -2 if timeout expired, EOF if line dropped
269 dead, or -3 for any other error (see errno in that case). */
270
271 static int
272 hardwire_readchar(scb, timeout)
273 serial_t scb;
274 int timeout;
275 {
276 int status;
277
278 if (scb->bufcnt-- > 0)
279 return *scb->bufp++;
280
281 status = wait_for(scb, timeout);
282
283 if (status < 0)
284 return status;
285
286 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
287
288 if (scb->bufcnt <= 0)
289 if (scb->bufcnt == 0)
290 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
291 distinguish between EOF & timeouts
292 someday] */
293 else
294 return SERIAL_ERROR; /* Got an error from read */
295
296 scb->bufcnt--;
297 scb->bufp = scb->buf;
298 return *scb->bufp++;
299 }
300
301 #ifndef B19200
302 #define B19200 EXTA
303 #endif
304
305 #ifndef B38400
306 #define B38400 EXTB
307 #endif
308
309 /* Translate baud rates from integers to damn B_codes. Unix should
310 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
311
312 static struct
313 {
314 int rate;
315 int code;
316 }
317 baudtab[] =
318 {
319 {50, B50},
320 {75, B75},
321 {110, B110},
322 {134, B134},
323 {150, B150},
324 {200, B200},
325 {300, B300},
326 {600, B600},
327 {1200, B1200},
328 {1800, B1800},
329 {2400, B2400},
330 {4800, B4800},
331 {9600, B9600},
332 {19200, B19200},
333 {38400, B38400},
334 {-1, -1},
335 };
336
337 static int
338 rate_to_code(rate)
339 int rate;
340 {
341 int i;
342
343 for (i = 0; baudtab[i].rate != -1; i++)
344 if (rate == baudtab[i].rate)
345 return baudtab[i].code;
346
347 return -1;
348 }
349
350 static int
351 hardwire_setbaudrate(scb, rate)
352 serial_t scb;
353 int rate;
354 {
355 struct hardwire_ttystate state;
356
357 if (get_tty_state(scb, &state))
358 return -1;
359
360 #ifdef HAVE_TERMIOS
361 cfsetospeed (&state.termios, rate_to_code (rate));
362 cfsetispeed (&state.termios, rate_to_code (rate));
363 #endif
364
365 #ifdef HAVE_TERMIO
366 #ifndef CIBAUD
367 #define CIBAUD CBAUD
368 #endif
369
370 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
371 state.termio.c_cflag |= rate_to_code (rate);
372 #endif
373
374 #ifdef HAVE_SGTTY
375 state.sgttyb.sg_ispeed = rate_to_code (rate);
376 state.sgttyb.sg_ospeed = rate_to_code (rate);
377 #endif
378
379 return set_tty_state (scb, &state);
380 }
381
382 static int
383 hardwire_write(scb, str, len)
384 serial_t scb;
385 const char *str;
386 int len;
387 {
388 int cc;
389
390 while (len > 0)
391 {
392 cc = write(scb->fd, str, len);
393
394 if (cc < 0)
395 return 1;
396 len -= cc;
397 str += cc;
398 }
399 return 0;
400 }
401
402 static void
403 hardwire_close(scb)
404 serial_t scb;
405 {
406 if (scb->fd < 0)
407 return;
408
409 close(scb->fd);
410 scb->fd = -1;
411 }
412
413 static struct serial_ops hardwire_ops =
414 {
415 "hardwire",
416 0,
417 hardwire_open,
418 hardwire_close,
419 hardwire_readchar,
420 hardwire_write,
421 hardwire_raw,
422 hardwire_get_tty_state,
423 hardwire_set_tty_state,
424 hardwire_setbaudrate,
425 };
426
427 void
428 _initialize_ser_hardwire ()
429 {
430 serial_add_interface (&hardwire_ops);
431 }
This page took 0.039508 seconds and 5 git commands to generate.