1 /* Generic serial interface functions.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
26 #include "event-loop.h"
28 #include "gdb_select.h"
29 #include "gdb_string.h"
36 static timer_handler_func push_event
;
37 static handler_func fd_event
;
39 /* Event handling for ASYNC serial code.
41 At any time the SERIAL device either: has an empty FIFO and is
42 waiting on a FD event; or has a non-empty FIFO/error condition and
43 is constantly scheduling timer events.
45 ASYNC only stops pestering its client when it is de-async'ed or it
46 is told to go away. */
48 /* Value of scb->async_state: */
50 /* >= 0 (TIMER_SCHEDULED) */
51 /* The ID of the currently scheduled timer event. This state is
52 rarely encountered. Timer events are one-off so as soon as the
53 event is delivered the state is shanged to NOTHING_SCHEDULED. */
55 /* The fd_event() handler is scheduled. It is called when ever the
56 file descriptor becomes ready. */
57 NOTHING_SCHEDULED
= -2
58 /* Either no task is scheduled (just going into ASYNC mode) or a
59 timer event has just gone off and the current state has been
60 forced into nothing scheduled. */
63 /* Identify and schedule the next ASYNC task based on scb->async_state
64 and scb->buf* (the input FIFO). A state machine is used to avoid
65 the need to make redundant calls into the event-loop - the next
66 scheduled task is only changed when needed. */
69 reschedule (struct serial
*scb
)
71 if (serial_is_async_p (scb
))
74 switch (scb
->async_state
)
78 next_state
= FD_SCHEDULED
;
81 delete_file_handler (scb
->fd
);
82 next_state
= create_timer (0, push_event
, scb
);
85 case NOTHING_SCHEDULED
:
88 add_file_handler (scb
->fd
, fd_event
, scb
);
89 next_state
= FD_SCHEDULED
;
93 next_state
= create_timer (0, push_event
, scb
);
96 default: /* TIMER SCHEDULED */
99 delete_timer (scb
->async_state
);
100 add_file_handler (scb
->fd
, fd_event
, scb
);
101 next_state
= FD_SCHEDULED
;
104 next_state
= scb
->async_state
;
107 if (serial_debug_p (scb
))
112 if (scb
->async_state
!= FD_SCHEDULED
)
113 fprintf_unfiltered (gdb_stdlog
, "[fd%d->fd-scheduled]\n",
116 default: /* TIMER SCHEDULED */
117 if (scb
->async_state
== FD_SCHEDULED
)
118 fprintf_unfiltered (gdb_stdlog
, "[fd%d->timer-scheduled]\n",
123 scb
->async_state
= next_state
;
127 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
128 is no pending error). As soon as data arrives, it is read into the
129 input FIFO and the client notified. The client should then drain
130 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
131 push_event() is used to nag the client until it is. */
134 fd_event (int error
, void *context
)
136 struct serial
*scb
= context
;
139 scb
->bufcnt
= SERIAL_ERROR
;
141 else if (scb
->bufcnt
== 0)
143 /* Prime the input FIFO. The readchar() function is used to
144 pull characters out of the buffer. See also
145 generic_readchar(). */
147 nr
= scb
->ops
->read_prim (scb
, BUFSIZ
);
150 scb
->bufcnt
= SERIAL_EOF
;
155 scb
->bufp
= scb
->buf
;
159 scb
->bufcnt
= SERIAL_ERROR
;
162 scb
->async_handler (scb
, scb
->async_context
);
166 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
167 error). Nag the client until all the data has been read. In the
168 case of errors, the client will need to close or de-async the
169 device before naging stops. */
172 push_event (void *context
)
174 struct serial
*scb
= context
;
175 scb
->async_state
= NOTHING_SCHEDULED
; /* Timers are one-off */
176 scb
->async_handler (scb
, scb
->async_context
);
181 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
182 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
185 ser_base_wait_for (struct serial
*scb
, int timeout
)
191 fd_set readfds
, exceptfds
;
193 /* NOTE: Some OS's can scramble the READFDS when the select()
194 call fails (ex the kernel with Red Hat 5.2). Initialize all
195 arguments before each call. */
201 FD_ZERO (&exceptfds
);
202 FD_SET (scb
->fd
, &readfds
);
203 FD_SET (scb
->fd
, &exceptfds
);
206 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, &tv
);
208 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, 0);
213 return SERIAL_TIMEOUT
;
214 else if (errno
== EINTR
)
217 return SERIAL_ERROR
; /* Got an error from select or poll */
224 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
225 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
226 char if successful. Returns -2 if timeout expired, EOF if line dropped
227 dead, or -3 for any other error (see errno in that case). */
230 do_ser_base_readchar (struct serial
*scb
, int timeout
)
235 /* We have to be able to keep the GUI alive here, so we break the
236 original timeout into steps of 1 second, running the "keep the
237 GUI alive" hook each time through the loop.
239 Also, timeout = 0 means to poll, so we just set the delta to 0,
240 so we will only go through the loop once. */
242 delta
= (timeout
== 0 ? 0 : 1);
245 /* N.B. The UI may destroy our world (for instance by calling
246 remote_stop,) in which case we want to get out of here as
247 quickly as possible. It is not safe to touch scb, since
248 someone else might have freed it. The
249 deprecated_ui_loop_hook signals that we should exit by
252 if (deprecated_ui_loop_hook
)
254 if (deprecated_ui_loop_hook (0))
255 return SERIAL_TIMEOUT
;
258 status
= ser_base_wait_for (scb
, delta
);
262 /* If we got a character or an error back from wait_for, then we can
263 break from the loop before the timeout is completed. */
264 if (status
!= SERIAL_TIMEOUT
)
267 /* If we have exhausted the original timeout, then generate
268 a SERIAL_TIMEOUT, and pass it out of the loop. */
269 else if (timeout
== 0)
271 status
= SERIAL_TIMEOUT
;
279 status
= scb
->ops
->read_prim (scb
, BUFSIZ
);
284 /* 0 chars means timeout. (We may need to distinguish between EOF
285 & timeouts someday.) */
286 return SERIAL_TIMEOUT
;
288 /* Got an error from read. */
292 scb
->bufcnt
= status
;
294 scb
->bufp
= scb
->buf
;
298 /* Perform operations common to both old and new readchar. */
300 /* Return the next character from the input FIFO. If the FIFO is
301 empty, call the SERIAL specific routine to try and read in more
304 Initially data from the input FIFO is returned (fd_event()
305 pre-reads the input into that FIFO. Once that has been emptied,
306 further data is obtained by polling the input FD using the device
307 specific readchar() function. Note: reschedule() is called after
308 every read. This is because there is no guarentee that the lower
309 level fd_event() poll_event() code (which also calls reschedule())
313 generic_readchar (struct serial
*scb
, int timeout
,
314 int (do_readchar
) (struct serial
*scb
, int timeout
))
323 else if (scb
->bufcnt
< 0)
325 /* Some errors/eof are are sticky. */
330 ch
= do_readchar (scb
, timeout
);
333 switch ((enum serial_rc
) ch
)
337 /* Make the error/eof stick. */
351 ser_base_readchar (struct serial
*scb
, int timeout
)
353 return generic_readchar (scb
, timeout
, do_ser_base_readchar
);
357 ser_base_write (struct serial
*scb
, const char *str
, int len
)
363 cc
= scb
->ops
->write_prim (scb
, str
, len
);
374 ser_base_flush_output (struct serial
*scb
)
380 ser_base_flush_input (struct serial
*scb
)
382 if (scb
->bufcnt
>= 0)
385 scb
->bufp
= scb
->buf
;
393 ser_base_send_break (struct serial
*scb
)
399 ser_base_drain_output (struct serial
*scb
)
405 ser_base_raw (struct serial
*scb
)
407 return; /* Always in raw mode */
411 ser_base_get_tty_state (struct serial
*scb
)
413 /* allocate a dummy */
414 return (serial_ttystate
) XMALLOC (int);
418 ser_base_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
424 ser_base_noflush_set_tty_state (struct serial
*scb
,
425 serial_ttystate new_ttystate
,
426 serial_ttystate old_ttystate
)
432 ser_base_print_tty_state (struct serial
*scb
,
433 serial_ttystate ttystate
,
434 struct ui_file
*stream
)
436 /* Nothing to print. */
441 ser_base_setbaudrate (struct serial
*scb
, int rate
)
443 return 0; /* Never fails! */
447 ser_base_setstopbits (struct serial
*scb
, int num
)
449 return 0; /* Never fails! */
452 /* Put the SERIAL device into/out-of ASYNC mode. */
455 ser_base_async (struct serial
*scb
,
460 /* Force a re-schedule. */
461 scb
->async_state
= NOTHING_SCHEDULED
;
462 if (serial_debug_p (scb
))
463 fprintf_unfiltered (gdb_stdlog
, "[fd%d->asynchronous]\n",
469 if (serial_debug_p (scb
))
470 fprintf_unfiltered (gdb_stdlog
, "[fd%d->synchronous]\n",
472 /* De-schedule whatever tasks are currently scheduled. */
473 switch (scb
->async_state
)
476 delete_file_handler (scb
->fd
);
478 case NOTHING_SCHEDULED
:
480 default: /* TIMER SCHEDULED */
481 delete_timer (scb
->async_state
);