1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include "gdb_string.h"
34 #include "event-loop.h"
38 struct hardwire_ttystate
40 struct termios termios
;
46 /* It is believed that all systems which have added job control to SVR3
47 (e.g. sco) have also added termios. Even if not, trying to figure out
48 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
49 bewildering. So we don't attempt it. */
51 struct hardwire_ttystate
58 struct hardwire_ttystate
63 /* Line discipline flags. */
68 static int hardwire_open (struct serial
*scb
, const char *name
);
69 static void hardwire_raw (struct serial
*scb
);
70 static int wait_for (struct serial
*scb
, int timeout
);
71 static int hardwire_readchar (struct serial
*scb
, int timeout
);
72 static int do_hardwire_readchar (struct serial
*scb
, int timeout
);
73 static int generic_readchar (struct serial
*scb
, int timeout
,
74 int (*do_readchar
) (struct serial
*scb
,
76 static int rate_to_code (int rate
);
77 static int hardwire_setbaudrate (struct serial
*scb
, int rate
);
78 static void hardwire_close (struct serial
*scb
);
79 static int get_tty_state (struct serial
*scb
,
80 struct hardwire_ttystate
* state
);
81 static int set_tty_state (struct serial
*scb
,
82 struct hardwire_ttystate
* state
);
83 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
84 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
85 static int hardwire_noflush_set_tty_state (struct serial
*, serial_ttystate
,
87 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
89 static int hardwire_drain_output (struct serial
*);
90 static int hardwire_flush_output (struct serial
*);
91 static int hardwire_flush_input (struct serial
*);
92 static int hardwire_send_break (struct serial
*);
93 static int hardwire_setstopbits (struct serial
*, int);
95 static int do_unix_readchar (struct serial
*scb
, int timeout
);
96 static timer_handler_func push_event
;
97 static handler_func fd_event
;
98 static void reschedule (struct serial
*scb
);
100 void _initialize_ser_hardwire (void);
102 /* Open up a real live device for serial I/O */
105 hardwire_open (struct serial
*scb
, const char *name
)
107 scb
->fd
= open (name
, O_RDWR
);
115 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
118 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
125 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
131 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
133 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
135 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
137 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
145 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
148 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
155 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
161 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
163 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
165 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
167 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
174 static serial_ttystate
175 hardwire_get_tty_state (struct serial
*scb
)
177 struct hardwire_ttystate
*state
;
179 state
= (struct hardwire_ttystate
*) xmalloc (sizeof *state
);
181 if (get_tty_state (scb
, state
))
184 return (serial_ttystate
) state
;
188 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
190 struct hardwire_ttystate
*state
;
192 state
= (struct hardwire_ttystate
*) ttystate
;
194 return set_tty_state (scb
, state
);
198 hardwire_noflush_set_tty_state (struct serial
*scb
,
199 serial_ttystate new_ttystate
,
200 serial_ttystate old_ttystate
)
202 struct hardwire_ttystate new_state
;
204 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
207 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
209 /* Don't change in or out of raw mode; we don't want to flush input.
210 termio and termios have no such restriction; for them flushing input
211 is separate from setting the attributes. */
214 if (state
->sgttyb
.sg_flags
& RAW
)
215 new_state
.sgttyb
.sg_flags
|= RAW
;
217 new_state
.sgttyb
.sg_flags
&= ~RAW
;
219 /* I'm not sure whether this is necessary; the manpage just mentions
221 if (state
->sgttyb
.sg_flags
& CBREAK
)
222 new_state
.sgttyb
.sg_flags
|= CBREAK
;
224 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
227 return set_tty_state (scb
, &new_state
);
231 hardwire_print_tty_state (struct serial
*scb
,
232 serial_ttystate ttystate
,
233 struct ui_file
*stream
)
235 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
239 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
240 (int) state
->termios
.c_iflag
,
241 (int) state
->termios
.c_oflag
);
242 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
243 (int) state
->termios
.c_cflag
,
244 (int) state
->termios
.c_lflag
);
246 /* This not in POSIX, and is not really documented by those systems
247 which have it (at least not Sun). */
248 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
250 fprintf_filtered (stream
, "c_cc: ");
251 for (i
= 0; i
< NCCS
; i
+= 1)
252 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
253 fprintf_filtered (stream
, "\n");
257 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
258 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
259 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
260 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
261 state
->termio
.c_line
);
262 fprintf_filtered (stream
, "c_cc: ");
263 for (i
= 0; i
< NCC
; i
+= 1)
264 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
265 fprintf_filtered (stream
, "\n");
269 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
270 state
->sgttyb
.sg_flags
);
272 fprintf_filtered (stream
, "tchars: ");
273 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
274 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
275 fprintf_filtered (stream
, "\n");
277 fprintf_filtered (stream
, "ltchars: ");
278 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
279 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
280 fprintf_filtered (stream
, "\n");
282 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
286 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
289 hardwire_drain_output (struct serial
*scb
)
292 return tcdrain (scb
->fd
);
296 return ioctl (scb
->fd
, TCSBRK
, 1);
300 /* Get the current state and then restore it using TIOCSETP,
301 which should cause the output to drain and pending input
304 struct hardwire_ttystate state
;
305 if (get_tty_state (scb
, &state
))
311 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
318 hardwire_flush_output (struct serial
*scb
)
321 return tcflush (scb
->fd
, TCOFLUSH
);
325 return ioctl (scb
->fd
, TCFLSH
, 1);
329 /* This flushes both input and output, but we can't do better. */
330 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
335 hardwire_flush_input (struct serial
*scb
)
337 ser_unix_flush_input (scb
);
340 return tcflush (scb
->fd
, TCIFLUSH
);
344 return ioctl (scb
->fd
, TCFLSH
, 0);
348 /* This flushes both input and output, but we can't do better. */
349 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
354 hardwire_send_break (struct serial
*scb
)
357 return tcsendbreak (scb
->fd
, 0);
361 return ioctl (scb
->fd
, TCSBRK
, 0);
367 struct timeval timeout
;
369 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
371 /* Can't use usleep; it doesn't exist in BSD 4.2. */
372 /* Note that if this select() is interrupted by a signal it will not wait
373 the full length of time. I think that is OK. */
375 timeout
.tv_usec
= 250000;
376 select (0, 0, 0, 0, &timeout
);
377 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
384 hardwire_raw (struct serial
*scb
)
386 struct hardwire_ttystate state
;
388 if (get_tty_state (scb
, &state
))
389 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
392 state
.termios
.c_iflag
= 0;
393 state
.termios
.c_oflag
= 0;
394 state
.termios
.c_lflag
= 0;
395 state
.termios
.c_cflag
&= ~(CSIZE
| PARENB
);
396 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
397 state
.termios
.c_cc
[VMIN
] = 0;
398 state
.termios
.c_cc
[VTIME
] = 0;
402 state
.termio
.c_iflag
= 0;
403 state
.termio
.c_oflag
= 0;
404 state
.termio
.c_lflag
= 0;
405 state
.termio
.c_cflag
&= ~(CSIZE
| PARENB
);
406 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
407 state
.termio
.c_cc
[VMIN
] = 0;
408 state
.termio
.c_cc
[VTIME
] = 0;
412 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
413 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
416 scb
->current_timeout
= 0;
418 if (set_tty_state (scb
, &state
))
419 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
422 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
423 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
425 For termio{s}, we actually just setup VTIME if necessary, and let the
426 timeout occur in the read() in hardwire_read().
429 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
430 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
433 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
434 possible values of the TIMEOUT parameter are ONE and ZERO.
435 Consequently all the code that tries to handle the possability of
436 an overflowed timer is unnecessary. */
439 wait_for (struct serial
*scb
, int timeout
)
448 /* NOTE: Some OS's can scramble the READFDS when the select()
449 call fails (ex the kernel with Red Hat 5.2). Initialize all
450 arguments before each call. */
456 FD_SET (scb
->fd
, &readfds
);
459 numfds
= select (scb
->fd
+ 1, &readfds
, 0, 0, &tv
);
461 numfds
= select (scb
->fd
+ 1, &readfds
, 0, 0, 0);
465 return SERIAL_TIMEOUT
;
466 else if (errno
== EINTR
)
469 return SERIAL_ERROR
; /* Got an error from select or poll */
473 #endif /* HAVE_SGTTY */
475 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
476 if (timeout
== scb
->current_timeout
)
479 scb
->current_timeout
= timeout
;
482 struct hardwire_ttystate state
;
484 if (get_tty_state (scb
, &state
))
485 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
491 state
.termios
.c_cc
[VTIME
] = 0;
492 state
.termios
.c_cc
[VMIN
] = 1;
496 state
.termios
.c_cc
[VMIN
] = 0;
497 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
498 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
501 /* If c_cc is an 8-bit signed character, we can't go
502 bigger than this. If it is always unsigned, we could use
505 scb
->current_timeout
= 12;
506 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
507 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
516 state
.termio
.c_cc
[VTIME
] = 0;
517 state
.termio
.c_cc
[VMIN
] = 1;
521 state
.termio
.c_cc
[VMIN
] = 0;
522 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
523 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
525 /* If c_cc is an 8-bit signed character, we can't go
526 bigger than this. If it is always unsigned, we could use
529 scb
->current_timeout
= 12;
530 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
531 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
536 if (set_tty_state (scb
, &state
))
537 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
541 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
544 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
545 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
546 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
547 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
549 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
550 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
553 /* NOTE: cagney/1999-09-16: This function is not identical to
554 ser_unix_readchar() as part of replacing it with ser_unix*()
555 merging will be required - this code handles the case where read()
556 times out due to no data while ser_unix_readchar() doesn't expect
560 do_hardwire_readchar (struct serial
*scb
, int timeout
)
568 /* We have to be able to keep the GUI alive here, so we break the original
569 timeout into steps of 1 second, running the "keep the GUI alive" hook
570 each time through the loop.
571 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
572 will only go through the loop once. */
574 delta
= (timeout
== 0 ? 0 : 1);
578 /* N.B. The UI may destroy our world (for instance by calling
579 remote_stop,) in which case we want to get out of here as
580 quickly as possible. It is not safe to touch scb, since
581 someone else might have freed it. The ui_loop_hook signals that
582 we should exit by returning 1. */
585 detach
= ui_loop_hook (0);
588 return SERIAL_TIMEOUT
;
590 scb
->timeout_remaining
= (timeout
< 0 ? timeout
: timeout
- delta
);
591 status
= wait_for (scb
, delta
);
596 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
602 /* Zero characters means timeout (it could also be EOF, but
603 we don't (yet at least) distinguish). */
604 if (scb
->timeout_remaining
> 0)
606 timeout
= scb
->timeout_remaining
;
609 else if (scb
->timeout_remaining
< 0)
612 return SERIAL_TIMEOUT
;
614 else if (errno
== EINTR
)
617 return SERIAL_ERROR
; /* Got an error from read */
620 scb
->bufcnt
= status
;
622 scb
->bufp
= scb
->buf
;
628 hardwire_readchar (struct serial
*scb
, int timeout
)
630 return generic_readchar (scb
, timeout
, do_hardwire_readchar
);
642 /* Translate baud rates from integers to damn B_codes. Unix should
643 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
743 rate_to_code (int rate
)
747 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
749 /* test for perfect macth. */
750 if (rate
== baudtab
[i
].rate
)
751 return baudtab
[i
].code
;
754 /* check if it is in between valid values. */
755 if (rate
< baudtab
[i
].rate
)
759 warning ("Invalid baud rate %d. Closest values are %d and %d.",
760 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
764 warning ("Invalid baud rate %d. Minimum value is %d.",
765 rate
, baudtab
[0].rate
);
772 /* The requested speed was too large. */
773 warning ("Invalid baud rate %d. Maximum value is %d.",
774 rate
, baudtab
[i
- 1].rate
);
779 hardwire_setbaudrate (struct serial
*scb
, int rate
)
781 struct hardwire_ttystate state
;
782 int baud_code
= rate_to_code (rate
);
786 /* The baud rate was not valid.
787 A warning has already been issued. */
792 if (get_tty_state (scb
, &state
))
796 cfsetospeed (&state
.termios
, baud_code
);
797 cfsetispeed (&state
.termios
, baud_code
);
805 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
806 state
.termio
.c_cflag
|= baud_code
;
810 state
.sgttyb
.sg_ispeed
= baud_code
;
811 state
.sgttyb
.sg_ospeed
= baud_code
;
814 return set_tty_state (scb
, &state
);
818 hardwire_setstopbits (struct serial
*scb
, int num
)
820 struct hardwire_ttystate state
;
823 if (get_tty_state (scb
, &state
))
828 case SERIAL_1_STOPBITS
:
831 case SERIAL_1_AND_A_HALF_STOPBITS
:
832 case SERIAL_2_STOPBITS
:
841 state
.termios
.c_cflag
&= ~CSTOPB
;
843 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
848 state
.termio
.c_cflag
&= ~CSTOPB
;
850 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
854 return 0; /* sgtty doesn't support this */
857 return set_tty_state (scb
, &state
);
861 hardwire_close (struct serial
*scb
)
871 /* Generic operations used by all UNIX/FD based serial interfaces. */
874 ser_unix_nop_get_tty_state (struct serial
*scb
)
876 /* allocate a dummy */
877 return (serial_ttystate
) XMALLOC (int);
881 ser_unix_nop_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
887 ser_unix_nop_raw (struct serial
*scb
)
889 return; /* Always in raw mode */
892 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
893 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
896 ser_unix_wait_for (struct serial
*scb
, int timeout
)
902 fd_set readfds
, exceptfds
;
904 /* NOTE: Some OS's can scramble the READFDS when the select()
905 call fails (ex the kernel with Red Hat 5.2). Initialize all
906 arguments before each call. */
912 FD_ZERO (&exceptfds
);
913 FD_SET (scb
->fd
, &readfds
);
914 FD_SET (scb
->fd
, &exceptfds
);
917 numfds
= select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, &tv
);
919 numfds
= select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, 0);
924 return SERIAL_TIMEOUT
;
925 else if (errno
== EINTR
)
928 return SERIAL_ERROR
; /* Got an error from select or poll */
935 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
936 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
937 char if successful. Returns -2 if timeout expired, EOF if line dropped
938 dead, or -3 for any other error (see errno in that case). */
941 do_unix_readchar (struct serial
*scb
, int timeout
)
946 /* We have to be able to keep the GUI alive here, so we break the original
947 timeout into steps of 1 second, running the "keep the GUI alive" hook
948 each time through the loop.
950 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
951 will only go through the loop once. */
953 delta
= (timeout
== 0 ? 0 : 1);
957 /* N.B. The UI may destroy our world (for instance by calling
958 remote_stop,) in which case we want to get out of here as
959 quickly as possible. It is not safe to touch scb, since
960 someone else might have freed it. The ui_loop_hook signals that
961 we should exit by returning 1. */
965 if (ui_loop_hook (0))
966 return SERIAL_TIMEOUT
;
969 status
= ser_unix_wait_for (scb
, delta
);
973 /* If we got a character or an error back from wait_for, then we can
974 break from the loop before the timeout is completed. */
976 if (status
!= SERIAL_TIMEOUT
)
981 /* If we have exhausted the original timeout, then generate
982 a SERIAL_TIMEOUT, and pass it out of the loop. */
984 else if (timeout
== 0)
986 status
= SERIAL_TIMEOUT
;
996 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
997 if (status
!= -1 || errno
!= EINTR
)
1004 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
1005 distinguish between EOF & timeouts
1008 return SERIAL_ERROR
; /* Got an error from read */
1011 scb
->bufcnt
= status
;
1013 scb
->bufp
= scb
->buf
;
1014 return *scb
->bufp
++;
1017 /* Perform operations common to both old and new readchar. */
1019 /* Return the next character from the input FIFO. If the FIFO is
1020 empty, call the SERIAL specific routine to try and read in more
1023 Initially data from the input FIFO is returned (fd_event()
1024 pre-reads the input into that FIFO. Once that has been emptied,
1025 further data is obtained by polling the input FD using the device
1026 specific readchar() function. Note: reschedule() is called after
1027 every read. This is because there is no guarentee that the lower
1028 level fd_event() poll_event() code (which also calls reschedule())
1032 generic_readchar (struct serial
*scb
, int timeout
,
1033 int (do_readchar
) (struct serial
*scb
, int timeout
))
1036 if (scb
->bufcnt
> 0)
1042 else if (scb
->bufcnt
< 0)
1044 /* Some errors/eof are are sticky. */
1049 ch
= do_readchar (scb
, timeout
);
1052 switch ((enum serial_rc
) ch
)
1056 /* Make the error/eof stick. */
1059 case SERIAL_TIMEOUT
:
1070 ser_unix_readchar (struct serial
*scb
, int timeout
)
1072 return generic_readchar (scb
, timeout
, do_unix_readchar
);
1076 ser_unix_nop_noflush_set_tty_state (struct serial
*scb
,
1077 serial_ttystate new_ttystate
,
1078 serial_ttystate old_ttystate
)
1084 ser_unix_nop_print_tty_state (struct serial
*scb
,
1085 serial_ttystate ttystate
,
1086 struct ui_file
*stream
)
1088 /* Nothing to print. */
1093 ser_unix_nop_setbaudrate (struct serial
*scb
, int rate
)
1095 return 0; /* Never fails! */
1099 ser_unix_nop_setstopbits (struct serial
*scb
, int num
)
1101 return 0; /* Never fails! */
1105 ser_unix_write (struct serial
*scb
, const char *str
, int len
)
1111 cc
= write (scb
->fd
, str
, len
);
1122 ser_unix_nop_flush_output (struct serial
*scb
)
1128 ser_unix_flush_input (struct serial
*scb
)
1130 if (scb
->bufcnt
>= 0)
1133 scb
->bufp
= scb
->buf
;
1137 return SERIAL_ERROR
;
1141 ser_unix_nop_send_break (struct serial
*scb
)
1147 ser_unix_nop_drain_output (struct serial
*scb
)
1154 /* Event handling for ASYNC serial code.
1156 At any time the SERIAL device either: has an empty FIFO and is
1157 waiting on a FD event; or has a non-empty FIFO/error condition and
1158 is constantly scheduling timer events.
1160 ASYNC only stops pestering its client when it is de-async'ed or it
1161 is told to go away. */
1163 /* Value of scb->async_state: */
1165 /* >= 0 (TIMER_SCHEDULED) */
1166 /* The ID of the currently scheduled timer event. This state is
1167 rarely encountered. Timer events are one-off so as soon as the
1168 event is delivered the state is shanged to NOTHING_SCHEDULED. */
1170 /* The fd_event() handler is scheduled. It is called when ever the
1171 file descriptor becomes ready. */
1172 NOTHING_SCHEDULED
= -2
1173 /* Either no task is scheduled (just going into ASYNC mode) or a
1174 timer event has just gone off and the current state has been
1175 forced into nothing scheduled. */
1178 /* Identify and schedule the next ASYNC task based on scb->async_state
1179 and scb->buf* (the input FIFO). A state machine is used to avoid
1180 the need to make redundant calls into the event-loop - the next
1181 scheduled task is only changed when needed. */
1184 reschedule (struct serial
*scb
)
1186 if (serial_is_async_p (scb
))
1189 switch (scb
->async_state
)
1192 if (scb
->bufcnt
== 0)
1193 next_state
= FD_SCHEDULED
;
1196 delete_file_handler (scb
->fd
);
1197 next_state
= create_timer (0, push_event
, scb
);
1200 case NOTHING_SCHEDULED
:
1201 if (scb
->bufcnt
== 0)
1203 add_file_handler (scb
->fd
, fd_event
, scb
);
1204 next_state
= FD_SCHEDULED
;
1208 next_state
= create_timer (0, push_event
, scb
);
1211 default: /* TIMER SCHEDULED */
1212 if (scb
->bufcnt
== 0)
1214 delete_timer (scb
->async_state
);
1215 add_file_handler (scb
->fd
, fd_event
, scb
);
1216 next_state
= FD_SCHEDULED
;
1219 next_state
= scb
->async_state
;
1222 if (serial_debug_p (scb
))
1227 if (scb
->async_state
!= FD_SCHEDULED
)
1228 fprintf_unfiltered (gdb_stdlog
, "[fd%d->fd-scheduled]\n",
1231 default: /* TIMER SCHEDULED */
1232 if (scb
->async_state
== FD_SCHEDULED
)
1233 fprintf_unfiltered (gdb_stdlog
, "[fd%d->timer-scheduled]\n",
1238 scb
->async_state
= next_state
;
1242 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1243 is no pending error). As soon as data arrives, it is read into the
1244 input FIFO and the client notified. The client should then drain
1245 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
1246 push_event() is used to nag the client until it is. */
1249 fd_event (int error
, void *context
)
1251 struct serial
*scb
= context
;
1254 scb
->bufcnt
= SERIAL_ERROR
;
1256 else if (scb
->bufcnt
== 0)
1258 /* Prime the input FIFO. The readchar() function is used to
1259 pull characters out of the buffer. See also
1260 generic_readchar(). */
1264 nr
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
1266 while (nr
== -1 && errno
== EINTR
);
1269 scb
->bufcnt
= SERIAL_EOF
;
1274 scb
->bufp
= scb
->buf
;
1278 scb
->bufcnt
= SERIAL_ERROR
;
1281 scb
->async_handler (scb
, scb
->async_context
);
1285 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1286 error). Nag the client until all the data has been read. In the
1287 case of errors, the client will need to close or de-async the
1288 device before naging stops. */
1291 push_event (void *context
)
1293 struct serial
*scb
= context
;
1294 scb
->async_state
= NOTHING_SCHEDULED
; /* Timers are one-off */
1295 scb
->async_handler (scb
, scb
->async_context
);
1300 /* Put the SERIAL device into/out-of ASYNC mode. */
1303 ser_unix_async (struct serial
*scb
,
1308 /* Force a re-schedule. */
1309 scb
->async_state
= NOTHING_SCHEDULED
;
1310 if (serial_debug_p (scb
))
1311 fprintf_unfiltered (gdb_stdlog
, "[fd%d->asynchronous]\n",
1317 if (serial_debug_p (scb
))
1318 fprintf_unfiltered (gdb_stdlog
, "[fd%d->synchronous]\n",
1320 /* De-schedule whatever tasks are currently scheduled. */
1321 switch (scb
->async_state
)
1324 delete_file_handler (scb
->fd
);
1326 case NOTHING_SCHEDULED
:
1328 default: /* TIMER SCHEDULED */
1329 delete_timer (scb
->async_state
);
1336 _initialize_ser_hardwire (void)
1338 struct serial_ops
*ops
= XMALLOC (struct serial_ops
);
1339 memset (ops
, 0, sizeof (struct serial_ops
));
1340 ops
->name
= "hardwire";
1342 ops
->open
= hardwire_open
;
1343 ops
->close
= hardwire_close
;
1344 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1345 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1347 ops
->readchar
= hardwire_readchar
;
1348 ops
->write
= ser_unix_write
;
1349 ops
->flush_output
= hardwire_flush_output
;
1350 ops
->flush_input
= hardwire_flush_input
;
1351 ops
->send_break
= hardwire_send_break
;
1352 ops
->go_raw
= hardwire_raw
;
1353 ops
->get_tty_state
= hardwire_get_tty_state
;
1354 ops
->set_tty_state
= hardwire_set_tty_state
;
1355 ops
->print_tty_state
= hardwire_print_tty_state
;
1356 ops
->noflush_set_tty_state
= hardwire_noflush_set_tty_state
;
1357 ops
->setbaudrate
= hardwire_setbaudrate
;
1358 ops
->setstopbits
= hardwire_setstopbits
;
1359 ops
->drain_output
= hardwire_drain_output
;
1360 ops
->async
= ser_unix_async
;
1361 serial_add_interface (ops
);