1 /* Generic serial interface functions.
3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004, 2005 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. */
26 #include "event-loop.h"
31 #include "gdb_string.h"
33 static timer_handler_func push_event;
34 static handler_func fd_event;
36 /* Event handling for ASYNC serial code.
38 At any time the SERIAL device either: has an empty FIFO and is
39 waiting on a FD event; or has a non-empty FIFO/error condition and
40 is constantly scheduling timer events.
42 ASYNC only stops pestering its client when it is de-async'ed or it
43 is told to go away. */
45 /* Value of scb->async_state: */
47 /* >= 0 (TIMER_SCHEDULED) */
48 /* The ID of the currently scheduled timer event. This state is
49 rarely encountered. Timer events are one-off so as soon as the
50 event is delivered the state is shanged to NOTHING_SCHEDULED. */
52 /* The fd_event() handler is scheduled. It is called when ever the
53 file descriptor becomes ready. */
54 NOTHING_SCHEDULED = -2
55 /* Either no task is scheduled (just going into ASYNC mode) or a
56 timer event has just gone off and the current state has been
57 forced into nothing scheduled. */
60 /* Identify and schedule the next ASYNC task based on scb->async_state
61 and scb->buf* (the input FIFO). A state machine is used to avoid
62 the need to make redundant calls into the event-loop - the next
63 scheduled task is only changed when needed. */
66 reschedule (struct serial *scb)
68 if (serial_is_async_p (scb))
71 switch (scb->async_state)
75 next_state = FD_SCHEDULED;
78 delete_file_handler (scb->fd);
79 next_state = create_timer (0, push_event, scb);
82 case NOTHING_SCHEDULED:
85 add_file_handler (scb->fd, fd_event, scb);
86 next_state = FD_SCHEDULED;
90 next_state = create_timer (0, push_event, scb);
93 default: /* TIMER SCHEDULED */
96 delete_timer (scb->async_state);
97 add_file_handler (scb->fd, fd_event, scb);
98 next_state = FD_SCHEDULED;
101 next_state = scb->async_state;
104 if (serial_debug_p (scb))
109 if (scb->async_state != FD_SCHEDULED)
110 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
113 default: /* TIMER SCHEDULED */
114 if (scb->async_state == FD_SCHEDULED)
115 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
120 scb->async_state = next_state;
124 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
125 is no pending error). As soon as data arrives, it is read into the
126 input FIFO and the client notified. The client should then drain
127 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
128 push_event() is used to nag the client until it is. */
131 fd_event (int error, void *context)
133 struct serial *scb = context;
136 scb->bufcnt = SERIAL_ERROR;
138 else if (scb->bufcnt == 0)
140 /* Prime the input FIFO. The readchar() function is used to
141 pull characters out of the buffer. See also
142 generic_readchar(). */
144 nr = scb->ops->read_prim (scb, BUFSIZ);
147 scb->bufcnt = SERIAL_EOF;
152 scb->bufp = scb->buf;
156 scb->bufcnt = SERIAL_ERROR;
159 scb->async_handler (scb, scb->async_context);
163 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
164 error). Nag the client until all the data has been read. In the
165 case of errors, the client will need to close or de-async the
166 device before naging stops. */
169 push_event (void *context)
171 struct serial *scb = context;
172 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
173 scb->async_handler (scb, scb->async_context);
178 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
179 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
182 ser_base_wait_for (struct serial *scb, int timeout)
188 fd_set readfds, exceptfds;
190 /* NOTE: Some OS's can scramble the READFDS when the select()
191 call fails (ex the kernel with Red Hat 5.2). Initialize all
192 arguments before each call. */
198 FD_ZERO (&exceptfds);
199 FD_SET (scb->fd, &readfds);
200 FD_SET (scb->fd, &exceptfds);
203 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
205 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
210 return SERIAL_TIMEOUT;
211 else if (errno == EINTR)
214 return SERIAL_ERROR; /* Got an error from select or poll */
221 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
222 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
223 char if successful. Returns -2 if timeout expired, EOF if line dropped
224 dead, or -3 for any other error (see errno in that case). */
227 do_ser_base_readchar (struct serial *scb, int timeout)
232 /* We have to be able to keep the GUI alive here, so we break the
233 original timeout into steps of 1 second, running the "keep the
234 GUI alive" hook each time through the loop.
236 Also, timeout = 0 means to poll, so we just set the delta to 0,
237 so we will only go through the loop once. */
239 delta = (timeout == 0 ? 0 : 1);
242 /* N.B. The UI may destroy our world (for instance by calling
243 remote_stop,) in which case we want to get out of here as
244 quickly as possible. It is not safe to touch scb, since
245 someone else might have freed it. The
246 deprecated_ui_loop_hook signals that we should exit by
249 if (deprecated_ui_loop_hook)
251 if (deprecated_ui_loop_hook (0))
252 return SERIAL_TIMEOUT;
255 status = ser_base_wait_for (scb, delta);
259 /* If we got a character or an error back from wait_for, then we can
260 break from the loop before the timeout is completed. */
261 if (status != SERIAL_TIMEOUT)
264 /* If we have exhausted the original timeout, then generate
265 a SERIAL_TIMEOUT, and pass it out of the loop. */
266 else if (timeout == 0)
268 status = SERIAL_TIMEOUT;
276 status = scb->ops->read_prim (scb, BUFSIZ);
281 /* 0 chars means timeout. (We may need to distinguish between EOF
282 & timeouts someday.) */
283 return SERIAL_TIMEOUT;
285 /* Got an error from read. */
289 scb->bufcnt = status;
291 scb->bufp = scb->buf;
295 /* Perform operations common to both old and new readchar. */
297 /* Return the next character from the input FIFO. If the FIFO is
298 empty, call the SERIAL specific routine to try and read in more
301 Initially data from the input FIFO is returned (fd_event()
302 pre-reads the input into that FIFO. Once that has been emptied,
303 further data is obtained by polling the input FD using the device
304 specific readchar() function. Note: reschedule() is called after
305 every read. This is because there is no guarentee that the lower
306 level fd_event() poll_event() code (which also calls reschedule())
310 generic_readchar (struct serial *scb, int timeout,
311 int (do_readchar) (struct serial *scb, int timeout))
320 else if (scb->bufcnt < 0)
322 /* Some errors/eof are are sticky. */
327 ch = do_readchar (scb, timeout);
330 switch ((enum serial_rc) ch)
334 /* Make the error/eof stick. */
348 ser_base_readchar (struct serial *scb, int timeout)
350 return generic_readchar (scb, timeout, do_ser_base_readchar);
354 ser_base_write (struct serial *scb, const char *str, int len)
360 cc = scb->ops->write_prim (scb, str, len);
371 ser_base_flush_output (struct serial *scb)
377 ser_base_flush_input (struct serial *scb)
379 if (scb->bufcnt >= 0)
382 scb->bufp = scb->buf;
390 ser_base_send_break (struct serial *scb)
396 ser_base_drain_output (struct serial *scb)
402 ser_base_raw (struct serial *scb)
404 return; /* Always in raw mode */
408 ser_base_get_tty_state (struct serial *scb)
410 /* allocate a dummy */
411 return (serial_ttystate) XMALLOC (int);
415 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
421 ser_base_noflush_set_tty_state (struct serial *scb,
422 serial_ttystate new_ttystate,
423 serial_ttystate old_ttystate)
429 ser_base_print_tty_state (struct serial *scb,
430 serial_ttystate ttystate,
431 struct ui_file *stream)
433 /* Nothing to print. */
438 ser_base_setbaudrate (struct serial *scb, int rate)
440 return 0; /* Never fails! */
444 ser_base_setstopbits (struct serial *scb, int num)
446 return 0; /* Never fails! */
449 /* Put the SERIAL device into/out-of ASYNC mode. */
452 ser_base_async (struct serial *scb,
457 /* Force a re-schedule. */
458 scb->async_state = NOTHING_SCHEDULED;
459 if (serial_debug_p (scb))
460 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
466 if (serial_debug_p (scb))
467 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
469 /* De-schedule whatever tasks are currently scheduled. */
470 switch (scb->async_state)
473 delete_file_handler (scb->fd);
475 case NOTHING_SCHEDULED:
477 default: /* TIMER SCHEDULED */
478 delete_timer (scb->async_state);