import gdb-1999-10-04 snapshot
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
c906108c 1/* Serial interface for local (hardwired) serial ports on Un*x like systems
c2c6d25f 2 Copyright 1992, 1993, 1994, 1998-1999 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21#include "defs.h"
22#include "serial.h"
c2c6d25f
JM
23#include "ser-unix.h"
24
c906108c
SS
25#include <fcntl.h>
26#include <sys/types.h>
27#include "terminal.h"
c2c6d25f
JM
28#ifdef HAVE_SYS_WAIT_H
29#include <sys/wait.h>
30#endif
31#include <sys/socket.h>
32#include <sys/time.h>
33
34#include "gdb_string.h"
35#include "event-loop.h"
36
c906108c
SS
37
38#ifdef HAVE_TERMIOS
39
40struct hardwire_ttystate
c5aa993b
JM
41 {
42 struct termios termios;
43 };
c906108c
SS
44#endif /* termios */
45
46#ifdef HAVE_TERMIO
47
48/* It is believed that all systems which have added job control to SVR3
49 (e.g. sco) have also added termios. Even if not, trying to figure out
50 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
51 bewildering. So we don't attempt it. */
52
53struct hardwire_ttystate
c5aa993b
JM
54 {
55 struct termio termio;
56 };
c906108c
SS
57#endif /* termio */
58
59#ifdef HAVE_SGTTY
c906108c 60struct hardwire_ttystate
c5aa993b
JM
61 {
62 struct sgttyb sgttyb;
63 struct tchars tc;
64 struct ltchars ltc;
65 /* Line discipline flags. */
66 int lmode;
67 };
c906108c
SS
68#endif /* sgtty */
69
c2c6d25f
JM
70static int hardwire_open (serial_t scb, const char *name);
71static void hardwire_raw (serial_t scb);
72static int wait_for (serial_t scb, int timeout);
73static int hardwire_readchar (serial_t scb, int timeout);
74static int rate_to_code (int rate);
75static int hardwire_setbaudrate (serial_t scb, int rate);
76static int hardwire_write (serial_t scb, const char *str, int len);
77static void hardwire_close (serial_t scb);
78static int get_tty_state (serial_t scb, struct hardwire_ttystate * state);
79static int set_tty_state (serial_t scb, struct hardwire_ttystate * state);
80static serial_ttystate hardwire_get_tty_state (serial_t scb);
81static int hardwire_set_tty_state (serial_t scb, serial_ttystate state);
82static int hardwire_noflush_set_tty_state (serial_t, serial_ttystate,
83 serial_ttystate);
84static void hardwire_print_tty_state (serial_t, serial_ttystate, struct gdb_file *);
85static int hardwire_drain_output (serial_t);
86static int hardwire_flush_output (serial_t);
87static int hardwire_flush_input (serial_t);
88static int hardwire_send_break (serial_t);
89static int hardwire_setstopbits (serial_t, int);
90
91void _initialize_ser_hardwire (void);
92
93extern int (*ui_loop_hook) (int);
c906108c
SS
94
95/* Open up a real live device for serial I/O */
96
97static int
c2c6d25f 98hardwire_open (serial_t scb, const char *name)
c906108c
SS
99{
100 scb->fd = open (name, O_RDWR);
101 if (scb->fd < 0)
102 return -1;
103
104 return 0;
105}
106
107static int
c2c6d25f 108get_tty_state (serial_t scb, struct hardwire_ttystate *state)
c906108c
SS
109{
110#ifdef HAVE_TERMIOS
c5aa993b 111 if (tcgetattr (scb->fd, &state->termios) < 0)
c906108c
SS
112 return -1;
113
114 return 0;
115#endif
116
117#ifdef HAVE_TERMIO
118 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
119 return -1;
120 return 0;
121#endif
122
123#ifdef HAVE_SGTTY
124 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
125 return -1;
126 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
127 return -1;
128 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
129 return -1;
130 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
131 return -1;
132
133 return 0;
134#endif
135}
136
137static int
c2c6d25f 138set_tty_state (serial_t scb, struct hardwire_ttystate *state)
c906108c
SS
139{
140#ifdef HAVE_TERMIOS
c5aa993b 141 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
c906108c
SS
142 return -1;
143
144 return 0;
145#endif
146
147#ifdef HAVE_TERMIO
148 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
149 return -1;
150 return 0;
151#endif
152
153#ifdef HAVE_SGTTY
154 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
155 return -1;
156 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
157 return -1;
158 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
159 return -1;
160 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
161 return -1;
162
163 return 0;
164#endif
165}
166
167static serial_ttystate
c2c6d25f 168hardwire_get_tty_state (serial_t scb)
c906108c
SS
169{
170 struct hardwire_ttystate *state;
171
c5aa993b 172 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
c906108c 173
c5aa993b 174 if (get_tty_state (scb, state))
c906108c
SS
175 return NULL;
176
c5aa993b 177 return (serial_ttystate) state;
c906108c
SS
178}
179
180static int
c2c6d25f 181hardwire_set_tty_state (serial_t scb, serial_ttystate ttystate)
c906108c
SS
182{
183 struct hardwire_ttystate *state;
184
c5aa993b 185 state = (struct hardwire_ttystate *) ttystate;
c906108c 186
c5aa993b 187 return set_tty_state (scb, state);
c906108c
SS
188}
189
190static int
c2c6d25f
JM
191hardwire_noflush_set_tty_state (serial_t scb,
192 serial_ttystate new_ttystate,
193 serial_ttystate old_ttystate)
c906108c
SS
194{
195 struct hardwire_ttystate new_state;
196#ifdef HAVE_SGTTY
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
198#endif
199
c5aa993b 200 new_state = *(struct hardwire_ttystate *) new_ttystate;
c906108c
SS
201
202 /* Don't change in or out of raw mode; we don't want to flush input.
203 termio and termios have no such restriction; for them flushing input
204 is separate from setting the attributes. */
205
206#ifdef HAVE_SGTTY
207 if (state->sgttyb.sg_flags & RAW)
208 new_state.sgttyb.sg_flags |= RAW;
209 else
210 new_state.sgttyb.sg_flags &= ~RAW;
211
212 /* I'm not sure whether this is necessary; the manpage just mentions
213 RAW not CBREAK. */
214 if (state->sgttyb.sg_flags & CBREAK)
215 new_state.sgttyb.sg_flags |= CBREAK;
216 else
217 new_state.sgttyb.sg_flags &= ~CBREAK;
218#endif
219
220 return set_tty_state (scb, &new_state);
221}
222
223static void
c2c6d25f
JM
224hardwire_print_tty_state (serial_t scb,
225 serial_ttystate ttystate,
226 struct gdb_file *stream)
c906108c
SS
227{
228 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
229 int i;
230
231#ifdef HAVE_TERMIOS
c2c6d25f
JM
232 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
233 state->termios.c_iflag, state->termios.c_oflag);
234 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
235 state->termios.c_cflag, state->termios.c_lflag);
c906108c
SS
236#if 0
237 /* This not in POSIX, and is not really documented by those systems
238 which have it (at least not Sun). */
c2c6d25f 239 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
c906108c 240#endif
c2c6d25f 241 fprintf_filtered (stream, "c_cc: ");
c906108c 242 for (i = 0; i < NCCS; i += 1)
c2c6d25f
JM
243 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
244 fprintf_filtered (stream, "\n");
c906108c
SS
245#endif
246
247#ifdef HAVE_TERMIO
c2c6d25f
JM
248 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
249 state->termio.c_iflag, state->termio.c_oflag);
250 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
251 state->termio.c_cflag, state->termio.c_lflag,
252 state->termio.c_line);
253 fprintf_filtered (stream, "c_cc: ");
c906108c 254 for (i = 0; i < NCC; i += 1)
c2c6d25f
JM
255 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
256 fprintf_filtered (stream, "\n");
c906108c
SS
257#endif
258
259#ifdef HAVE_SGTTY
c2c6d25f
JM
260 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
261 state->sgttyb.sg_flags);
c906108c 262
c2c6d25f 263 fprintf_filtered (stream, "tchars: ");
c5aa993b 264 for (i = 0; i < (int) sizeof (struct tchars); i++)
c2c6d25f
JM
265 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
266 fprintf_filtered ("\n");
c906108c 267
c2c6d25f 268 fprintf_filtered (stream, "ltchars: ");
c5aa993b 269 for (i = 0; i < (int) sizeof (struct ltchars); i++)
c2c6d25f
JM
270 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
271 fprintf_filtered (stream, "\n");
c906108c 272
c2c6d25f 273 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
c906108c
SS
274#endif
275}
276
277/* Wait for the output to drain away, as opposed to flushing (discarding) it */
278
279static int
c2c6d25f 280hardwire_drain_output (serial_t scb)
c906108c
SS
281{
282#ifdef HAVE_TERMIOS
283 return tcdrain (scb->fd);
284#endif
285
286#ifdef HAVE_TERMIO
287 return ioctl (scb->fd, TCSBRK, 1);
288#endif
289
290#ifdef HAVE_SGTTY
291 /* Get the current state and then restore it using TIOCSETP,
292 which should cause the output to drain and pending input
293 to be discarded. */
294 {
295 struct hardwire_ttystate state;
296 if (get_tty_state (scb, &state))
297 {
298 return (-1);
299 }
300 else
301 {
302 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
303 }
304 }
c5aa993b 305#endif
c906108c
SS
306}
307
308static int
c2c6d25f 309hardwire_flush_output (serial_t scb)
c906108c
SS
310{
311#ifdef HAVE_TERMIOS
312 return tcflush (scb->fd, TCOFLUSH);
313#endif
314
315#ifdef HAVE_TERMIO
316 return ioctl (scb->fd, TCFLSH, 1);
317#endif
318
319#ifdef HAVE_SGTTY
320 /* This flushes both input and output, but we can't do better. */
321 return ioctl (scb->fd, TIOCFLUSH, 0);
c5aa993b 322#endif
c906108c
SS
323}
324
325static int
c2c6d25f 326hardwire_flush_input (serial_t scb)
c906108c
SS
327{
328 scb->bufcnt = 0;
329 scb->bufp = scb->buf;
330
331#ifdef HAVE_TERMIOS
332 return tcflush (scb->fd, TCIFLUSH);
333#endif
334
335#ifdef HAVE_TERMIO
336 return ioctl (scb->fd, TCFLSH, 0);
337#endif
338
339#ifdef HAVE_SGTTY
340 /* This flushes both input and output, but we can't do better. */
341 return ioctl (scb->fd, TIOCFLUSH, 0);
c5aa993b 342#endif
c906108c
SS
343}
344
345static int
c2c6d25f 346hardwire_send_break (serial_t scb)
c906108c
SS
347{
348#ifdef HAVE_TERMIOS
349 return tcsendbreak (scb->fd, 0);
350#endif
351
352#ifdef HAVE_TERMIO
353 return ioctl (scb->fd, TCSBRK, 0);
354#endif
355
356#ifdef HAVE_SGTTY
357 {
358 int status;
359 struct timeval timeout;
360
361 status = ioctl (scb->fd, TIOCSBRK, 0);
362
363 /* Can't use usleep; it doesn't exist in BSD 4.2. */
364 /* Note that if this select() is interrupted by a signal it will not wait
365 the full length of time. I think that is OK. */
366 timeout.tv_sec = 0;
367 timeout.tv_usec = 250000;
368 select (0, 0, 0, 0, &timeout);
369 status = ioctl (scb->fd, TIOCCBRK, 0);
370 return status;
371 }
c5aa993b 372#endif
c906108c
SS
373}
374
375static void
c2c6d25f 376hardwire_raw (serial_t scb)
c906108c
SS
377{
378 struct hardwire_ttystate state;
379
c5aa993b
JM
380 if (get_tty_state (scb, &state))
381 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
382
383#ifdef HAVE_TERMIOS
384 state.termios.c_iflag = 0;
385 state.termios.c_oflag = 0;
386 state.termios.c_lflag = 0;
c5aa993b 387 state.termios.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
388 state.termios.c_cflag |= CLOCAL | CS8;
389 state.termios.c_cc[VMIN] = 0;
390 state.termios.c_cc[VTIME] = 0;
391#endif
392
393#ifdef HAVE_TERMIO
394 state.termio.c_iflag = 0;
395 state.termio.c_oflag = 0;
396 state.termio.c_lflag = 0;
c5aa993b 397 state.termio.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
398 state.termio.c_cflag |= CLOCAL | CS8;
399 state.termio.c_cc[VMIN] = 0;
400 state.termio.c_cc[VTIME] = 0;
401#endif
402
403#ifdef HAVE_SGTTY
404 state.sgttyb.sg_flags |= RAW | ANYP;
405 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
406#endif
407
408 scb->current_timeout = 0;
409
410 if (set_tty_state (scb, &state))
c5aa993b 411 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
412}
413
414/* Wait for input on scb, with timeout seconds. Returns 0 on success,
415 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
416
417 For termio{s}, we actually just setup VTIME if necessary, and let the
418 timeout occur in the read() in hardwire_read().
419 */
420
c2c6d25f
JM
421/* FIXME: Don't replace this with the equivalent ser_unix*() until the
422 old TERMIOS/SGTTY/... timer code has been flushed. cagney
423 1999-09-16. */
424
c906108c 425static int
c2c6d25f 426wait_for (serial_t scb, int timeout)
c906108c 427{
c906108c
SS
428#ifdef HAVE_SGTTY
429 {
430 struct timeval tv;
431 fd_set readfds;
432
433 FD_ZERO (&readfds);
434
435 tv.tv_sec = timeout;
436 tv.tv_usec = 0;
437
c5aa993b 438 FD_SET (scb->fd, &readfds);
c906108c
SS
439
440 while (1)
441 {
442 int numfds;
443
444 if (timeout >= 0)
c5aa993b 445 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
c906108c 446 else
c5aa993b 447 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
c906108c
SS
448
449 if (numfds <= 0)
450 if (numfds == 0)
451 return SERIAL_TIMEOUT;
452 else if (errno == EINTR)
453 continue;
454 else
455 return SERIAL_ERROR; /* Got an error from select or poll */
456
457 return 0;
458 }
459 }
c5aa993b 460#endif /* HAVE_SGTTY */
c906108c
SS
461
462#if defined HAVE_TERMIO || defined HAVE_TERMIOS
463 if (timeout == scb->current_timeout)
464 return 0;
465
466 scb->current_timeout = timeout;
467
468 {
469 struct hardwire_ttystate state;
470
c5aa993b
JM
471 if (get_tty_state (scb, &state))
472 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
473
474#ifdef HAVE_TERMIOS
475 if (timeout < 0)
476 {
477 /* No timeout. */
478 state.termios.c_cc[VTIME] = 0;
479 state.termios.c_cc[VMIN] = 1;
480 }
481 else
482 {
483 state.termios.c_cc[VMIN] = 0;
484 state.termios.c_cc[VTIME] = timeout * 10;
485 if (state.termios.c_cc[VTIME] != timeout * 10)
486 {
487
488 /* If c_cc is an 8-bit signed character, we can't go
489 bigger than this. If it is always unsigned, we could use
490 25. */
491
492 scb->current_timeout = 12;
493 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
494 scb->timeout_remaining = timeout - scb->current_timeout;
495 }
496 }
497#endif
498
499#ifdef HAVE_TERMIO
500 if (timeout < 0)
501 {
502 /* No timeout. */
503 state.termio.c_cc[VTIME] = 0;
504 state.termio.c_cc[VMIN] = 1;
505 }
506 else
507 {
508 state.termio.c_cc[VMIN] = 0;
509 state.termio.c_cc[VTIME] = timeout * 10;
510 if (state.termio.c_cc[VTIME] != timeout * 10)
511 {
512 /* If c_cc is an 8-bit signed character, we can't go
513 bigger than this. If it is always unsigned, we could use
514 25. */
515
516 scb->current_timeout = 12;
517 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
518 scb->timeout_remaining = timeout - scb->current_timeout;
519 }
520 }
521#endif
522
523 if (set_tty_state (scb, &state))
c5aa993b 524 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
525
526 return 0;
527 }
c5aa993b 528#endif /* HAVE_TERMIO || HAVE_TERMIOS */
c906108c
SS
529}
530
531/* Read a character with user-specified timeout. TIMEOUT is number of seconds
532 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
533 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
534 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
c2c6d25f
JM
535
536/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
537 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
538 flushed. */
539
540/* NOTE: cagney/1999-09-16: This function is not identical to
541 ser_unix_readchar() as part of replacing it with ser_unix*()
542 merging will be required - this code handles the case where read()
543 times out due to no data while ser_unix_readchar() doesn't expect
544 that. */
545
c906108c 546static int
c2c6d25f 547hardwire_readchar (serial_t scb, int timeout)
c906108c 548{
7a292a7a
SS
549 int status, delta;
550 int detach = 0;
c906108c
SS
551
552 if (scb->bufcnt-- > 0)
553 return *scb->bufp++;
554
c906108c
SS
555 if (timeout > 0)
556 timeout++;
c906108c 557
7a292a7a
SS
558 /* We have to be able to keep the GUI alive here, so we break the original
559 timeout into steps of 1 second, running the "keep the GUI alive" hook
560 each time through the loop.
561 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
562 will only go through the loop once. */
c5aa993b 563
7a292a7a 564 delta = (timeout == 0 ? 0 : 1);
c906108c
SS
565 while (1)
566 {
c906108c 567
7a292a7a
SS
568 /* N.B. The UI may destroy our world (for instance by calling
569 remote_stop,) in which case we want to get out of here as
570 quickly as possible. It is not safe to touch scb, since
571 someone else might have freed it. The ui_loop_hook signals that
572 we should exit by returning 1. */
573
c906108c 574 if (ui_loop_hook)
c5aa993b 575 detach = ui_loop_hook (0);
7a292a7a
SS
576
577 if (detach)
578 return SERIAL_TIMEOUT;
579
580 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
581 status = wait_for (scb, delta);
582
c906108c
SS
583 if (status < 0)
584 return status;
585
c2c6d25f
JM
586 /* NOTE: cagney/1999-09-17: See ser_unix_readchar() for reason
587 why ASYNC reads are character by character. */
588
589 scb->bufcnt = read (scb->fd, scb->buf,
590 (SERIAL_IS_ASYNC_P (scb) ? 1 : BUFSIZ));
c906108c
SS
591
592 if (scb->bufcnt <= 0)
593 {
594 if (scb->bufcnt == 0)
595 {
596 /* Zero characters means timeout (it could also be EOF, but
c5aa993b 597 we don't (yet at least) distinguish). */
c906108c
SS
598 if (scb->timeout_remaining > 0)
599 {
600 timeout = scb->timeout_remaining;
601 continue;
602 }
c5aa993b
JM
603 else if (scb->timeout_remaining < 0)
604 continue;
c906108c
SS
605 else
606 return SERIAL_TIMEOUT;
607 }
608 else if (errno == EINTR)
609 continue;
610 else
611 return SERIAL_ERROR; /* Got an error from read */
612 }
613
614 scb->bufcnt--;
615 scb->bufp = scb->buf;
616 return *scb->bufp++;
617 }
618}
619
620#ifndef B19200
621#define B19200 EXTA
622#endif
623
624#ifndef B38400
625#define B38400 EXTB
626#endif
627
628/* Translate baud rates from integers to damn B_codes. Unix should
629 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
630
631static struct
632{
633 int rate;
634 int code;
635}
636baudtab[] =
637{
c5aa993b
JM
638 {
639 50, B50
640 }
641 ,
642 {
643 75, B75
644 }
645 ,
646 {
647 110, B110
648 }
649 ,
650 {
651 134, B134
652 }
653 ,
654 {
655 150, B150
656 }
657 ,
658 {
659 200, B200
660 }
661 ,
662 {
663 300, B300
664 }
665 ,
666 {
667 600, B600
668 }
669 ,
670 {
671 1200, B1200
672 }
673 ,
674 {
675 1800, B1800
676 }
677 ,
678 {
679 2400, B2400
680 }
681 ,
682 {
683 4800, B4800
684 }
685 ,
686 {
687 9600, B9600
688 }
689 ,
690 {
691 19200, B19200
692 }
693 ,
694 {
695 38400, B38400
696 }
697 ,
c906108c 698#ifdef B57600
c5aa993b
JM
699 {
700 57600, B57600
701 }
702 ,
c906108c
SS
703#endif
704#ifdef B115200
c5aa993b
JM
705 {
706 115200, B115200
707 }
708 ,
c906108c
SS
709#endif
710#ifdef B230400
c5aa993b
JM
711 {
712 230400, B230400
713 }
714 ,
c906108c
SS
715#endif
716#ifdef B460800
c5aa993b
JM
717 {
718 460800, B460800
719 }
720 ,
c906108c 721#endif
c5aa993b
JM
722 {
723 -1, -1
724 }
725 ,
c906108c
SS
726};
727
c5aa993b 728static int
c2c6d25f 729rate_to_code (int rate)
c906108c
SS
730{
731 int i;
732
733 for (i = 0; baudtab[i].rate != -1; i++)
c5aa993b 734 if (rate == baudtab[i].rate)
c906108c
SS
735 return baudtab[i].code;
736
737 return -1;
738}
739
740static int
c2c6d25f 741hardwire_setbaudrate (serial_t scb, int rate)
c906108c
SS
742{
743 struct hardwire_ttystate state;
744
c5aa993b 745 if (get_tty_state (scb, &state))
c906108c
SS
746 return -1;
747
748#ifdef HAVE_TERMIOS
749 cfsetospeed (&state.termios, rate_to_code (rate));
750 cfsetispeed (&state.termios, rate_to_code (rate));
751#endif
752
753#ifdef HAVE_TERMIO
754#ifndef CIBAUD
755#define CIBAUD CBAUD
756#endif
757
758 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
759 state.termio.c_cflag |= rate_to_code (rate);
760#endif
761
762#ifdef HAVE_SGTTY
763 state.sgttyb.sg_ispeed = rate_to_code (rate);
764 state.sgttyb.sg_ospeed = rate_to_code (rate);
765#endif
766
767 return set_tty_state (scb, &state);
768}
769
770static int
c5aa993b 771hardwire_setstopbits (scb, num)
c906108c
SS
772 serial_t scb;
773 int num;
774{
775 struct hardwire_ttystate state;
776 int newbit;
777
c5aa993b 778 if (get_tty_state (scb, &state))
c906108c
SS
779 return -1;
780
781 switch (num)
782 {
783 case SERIAL_1_STOPBITS:
784 newbit = 0;
785 break;
786 case SERIAL_1_AND_A_HALF_STOPBITS:
787 case SERIAL_2_STOPBITS:
788 newbit = 1;
789 break;
790 default:
791 return 1;
792 }
793
794#ifdef HAVE_TERMIOS
795 if (!newbit)
796 state.termios.c_cflag &= ~CSTOPB;
797 else
c5aa993b 798 state.termios.c_cflag |= CSTOPB; /* two bits */
c906108c
SS
799#endif
800
801#ifdef HAVE_TERMIO
802 if (!newbit)
803 state.termio.c_cflag &= ~CSTOPB;
804 else
c5aa993b 805 state.termio.c_cflag |= CSTOPB; /* two bits */
c906108c
SS
806#endif
807
808#ifdef HAVE_SGTTY
809 return 0; /* sgtty doesn't support this */
810#endif
811
812 return set_tty_state (scb, &state);
813}
814
c2c6d25f
JM
815/* FIXME: Don't replace this with the equivalent ser_unix*() until the
816 old TERMIOS/SGTTY/... timer code has been flushed. cagney
817 1999-09-16. */
818
c906108c 819static int
c2c6d25f 820hardwire_write (serial_t scb, const char *str, int len)
c906108c
SS
821{
822 int cc;
823
824 while (len > 0)
825 {
c5aa993b 826 cc = write (scb->fd, str, len);
c906108c
SS
827
828 if (cc < 0)
c2c6d25f 829 return 1;
c906108c
SS
830 len -= cc;
831 str += cc;
832 }
833 return 0;
834}
835
c2c6d25f 836
c906108c 837static void
c2c6d25f 838hardwire_close (serial_t scb)
c906108c
SS
839{
840 if (scb->fd < 0)
841 return;
842
c5aa993b 843 close (scb->fd);
c906108c
SS
844 scb->fd = -1;
845}
846
c2c6d25f
JM
847\f
848/* Generic operations used by all UNIX/FD based serial interfaces. */
849
850serial_ttystate
851ser_unix_nop_get_tty_state (serial_t scb)
852{
853 /* allocate a dummy */
854 return (serial_ttystate) XMALLOC (int);
855}
856
857int
858ser_unix_nop_set_tty_state (serial_t scb, serial_ttystate ttystate)
859{
860 return 0;
861}
862
863void
864ser_unix_nop_raw (serial_t scb)
865{
866 return; /* Always in raw mode */
867}
868
869/* Wait for input on scb, with timeout seconds. Returns 0 on success,
870 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
871
872int
873ser_unix_wait_for (serial_t scb, int timeout)
874{
875 int numfds;
876 struct timeval tv;
877 fd_set readfds, exceptfds;
878
879 FD_ZERO (&readfds);
880 FD_ZERO (&exceptfds);
881
882 tv.tv_sec = timeout;
883 tv.tv_usec = 0;
884
885 FD_SET (scb->fd, &readfds);
886 FD_SET (scb->fd, &exceptfds);
887
888 while (1)
889 {
890 if (timeout >= 0)
891 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
892 else
893 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
894
895 if (numfds <= 0)
896 {
897 if (numfds == 0)
898 return SERIAL_TIMEOUT;
899 else if (errno == EINTR)
900 continue;
901 else
902 return SERIAL_ERROR; /* Got an error from select or poll */
903 }
904
905 return 0;
906 }
907}
908
909/* Read a character with user-specified timeout. TIMEOUT is number of seconds
910 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
911 char if successful. Returns -2 if timeout expired, EOF if line dropped
912 dead, or -3 for any other error (see errno in that case). */
913
914int
915ser_unix_readchar (serial_t scb, int timeout)
916{
917 int status;
918 int delta;
919
920 if (scb->bufcnt-- > 0)
921 return *scb->bufp++;
922
923 /* We have to be able to keep the GUI alive here, so we break the original
924 timeout into steps of 1 second, running the "keep the GUI alive" hook
925 each time through the loop.
926
927 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
928 will only go through the loop once. */
929
930 delta = (timeout == 0 ? 0 : 1);
931 while (1)
932 {
933
934 /* N.B. The UI may destroy our world (for instance by calling
935 remote_stop,) in which case we want to get out of here as
936 quickly as possible. It is not safe to touch scb, since
937 someone else might have freed it. The ui_loop_hook signals that
938 we should exit by returning 1. */
939
940 if (ui_loop_hook)
941 {
942 if (ui_loop_hook (0))
943 return SERIAL_TIMEOUT;
944 }
945
946 status = ser_unix_wait_for (scb, delta);
947 timeout -= delta;
948
949 /* If we got a character or an error back from wait_for, then we can
950 break from the loop before the timeout is completed. */
951
952 if (status != SERIAL_TIMEOUT)
953 {
954 break;
955 }
956
957 /* If we have exhausted the original timeout, then generate
958 a SERIAL_TIMEOUT, and pass it out of the loop. */
959
960 else if (timeout == 0)
961 {
962 status = SERIAL_TIMEOUT;
963 break;
964 }
965 }
966
967 if (status < 0)
968 return status;
969
970 while (1)
971 {
972 /* FIXME: cagney/1999-09-17: ASYNC: The ASYNC serial code needs
973 to be modified so that it agressivly tries to drain its local
974 input buffer. Until this is done, the read() below can only
975 take in single characters. This is to ensure that
976 unprocessed data doesn't end up sitting in the input fifo. */
977 scb->bufcnt = read (scb->fd, scb->buf,
978 (SERIAL_IS_ASYNC_P (scb) ? 1 : BUFSIZ));
979 if (scb->bufcnt != -1 || errno != EINTR)
980 break;
981 }
982
983 if (scb->bufcnt <= 0)
984 {
985 if (scb->bufcnt == 0)
986 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
987 distinguish between EOF & timeouts
988 someday] */
989 else
990 return SERIAL_ERROR; /* Got an error from read */
991 }
992
993 scb->bufcnt--;
994 scb->bufp = scb->buf;
995 return *scb->bufp++;
996}
997
998int
999ser_unix_nop_noflush_set_tty_state (serial_t scb,
1000 serial_ttystate new_ttystate,
1001 serial_ttystate old_ttystate)
1002{
1003 return 0;
1004}
1005
1006void
1007ser_unix_nop_print_tty_state (serial_t scb,
1008 serial_ttystate ttystate,
1009 struct gdb_file *stream)
1010{
1011 /* Nothing to print. */
1012 return;
1013}
1014
1015int
1016ser_unix_nop_setbaudrate (serial_t scb, int rate)
1017{
1018 return 0; /* Never fails! */
1019}
1020
1021int
1022ser_unix_nop_setstopbits (serial_t scb, int num)
1023{
1024 return 0; /* Never fails! */
1025}
1026
1027int
1028ser_unix_write (serial_t scb, const char *str, int len)
1029{
1030 int cc;
1031
1032 while (len > 0)
1033 {
1034 cc = write (scb->fd, str, len);
1035
1036 if (cc < 0)
1037 return 1;
1038 len -= cc;
1039 str += cc;
1040 }
1041 return 0;
1042}
1043
1044int
1045ser_unix_nop_flush_output (serial_t scb)
1046{
1047 return 0;
1048}
1049
1050int
1051ser_unix_nop_flush_input (serial_t scb)
1052{
1053 return 0;
1054}
1055
1056int
1057ser_unix_nop_send_break (serial_t scb)
1058{
1059 return 0;
1060}
1061
1062int
1063ser_unix_nop_drain_output (serial_t scb)
1064{
1065 return 0;
1066}
1067
1068static void
1069ser_unix_event (int error, int fd, gdb_client_data context)
1070{
1071 serial_t scb = context;
1072 scb->async_handler (error, scb->async_context, fd);
1073}
1074
1075void
1076ser_unix_async (serial_t scb,
1077 int async_p)
1078{
1079 if (async_p)
1080 {
1081 add_file_handler (scb->fd, ser_unix_event, scb);
1082 }
1083 else
1084 {
1085 delete_file_handler (scb->fd);
1086 }
1087}
c906108c
SS
1088
1089void
c2c6d25f 1090_initialize_ser_hardwire (void)
c906108c 1091{
c2c6d25f
JM
1092 struct serial_ops *ops = XMALLOC (struct serial_ops);
1093 memset (ops, sizeof (struct serial_ops), 0);
1094 ops->name = "hardwire";
1095 ops->next = 0;
1096 ops->open = hardwire_open;
1097 ops->close = hardwire_close;
1098 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1099 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1100 1999-09-16. */
1101 ops->readchar = hardwire_readchar;
1102 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1103 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1104 1999-09-16. */
1105 ops->write = hardwire_write;
1106 ops->flush_output = hardwire_flush_output;
1107 ops->flush_input = hardwire_flush_input;
1108 ops->send_break = hardwire_send_break;
1109 ops->go_raw = hardwire_raw;
1110 ops->get_tty_state = hardwire_get_tty_state;
1111 ops->set_tty_state = hardwire_set_tty_state;
1112 ops->print_tty_state = hardwire_print_tty_state;
1113 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1114 ops->setbaudrate = hardwire_setbaudrate;
1115 ops->setstopbits = hardwire_setstopbits;
1116 ops->drain_output = hardwire_drain_output;
1117 ops->async = ser_unix_async;
1118 serial_add_interface (ops);
c906108c 1119}
This page took 0.082949 seconds and 4 git commands to generate.