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"
32 #include "gdb_string.h"
34 static timer_handler_func push_event;
35 static handler_func fd_event;
37 /* Event handling for ASYNC serial code.
39 At any time the SERIAL device either: has an empty FIFO and is
40 waiting on a FD event; or has a non-empty FIFO/error condition and
41 is constantly scheduling timer events.
43 ASYNC only stops pestering its client when it is de-async'ed or it
44 is told to go away. */
46 /* Value of scb->async_state: */
48 /* >= 0 (TIMER_SCHEDULED) */
49 /* The ID of the currently scheduled timer event. This state is
50 rarely encountered. Timer events are one-off so as soon as the
51 event is delivered the state is shanged to NOTHING_SCHEDULED. */
53 /* The fd_event() handler is scheduled. It is called when ever the
54 file descriptor becomes ready. */
55 NOTHING_SCHEDULED = -2
56 /* Either no task is scheduled (just going into ASYNC mode) or a
57 timer event has just gone off and the current state has been
58 forced into nothing scheduled. */
61 /* Identify and schedule the next ASYNC task based on scb->async_state
62 and scb->buf* (the input FIFO). A state machine is used to avoid
63 the need to make redundant calls into the event-loop - the next
64 scheduled task is only changed when needed. */
67 reschedule (struct serial *scb)
69 if (serial_is_async_p (scb))
72 switch (scb->async_state)
76 next_state = FD_SCHEDULED;
79 delete_file_handler (scb->fd);
80 next_state = create_timer (0, push_event, scb);
83 case NOTHING_SCHEDULED:
86 add_file_handler (scb->fd, fd_event, scb);
87 next_state = FD_SCHEDULED;
91 next_state = create_timer (0, push_event, scb);
94 default: /* TIMER SCHEDULED */
97 delete_timer (scb->async_state);
98 add_file_handler (scb->fd, fd_event, scb);
99 next_state = FD_SCHEDULED;
102 next_state = scb->async_state;
105 if (serial_debug_p (scb))
110 if (scb->async_state != FD_SCHEDULED)
111 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
114 default: /* TIMER SCHEDULED */
115 if (scb->async_state == FD_SCHEDULED)
116 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
121 scb->async_state = next_state;
125 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
126 is no pending error). As soon as data arrives, it is read into the
127 input FIFO and the client notified. The client should then drain
128 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
129 push_event() is used to nag the client until it is. */
132 fd_event (int error, void *context)
134 struct serial *scb = context;
137 scb->bufcnt = SERIAL_ERROR;
139 else if (scb->bufcnt == 0)
141 /* Prime the input FIFO. The readchar() function is used to
142 pull characters out of the buffer. See also
143 generic_readchar(). */
145 nr = scb->ops->read_prim (scb, BUFSIZ);
148 scb->bufcnt = SERIAL_EOF;
153 scb->bufp = scb->buf;
157 scb->bufcnt = SERIAL_ERROR;
160 scb->async_handler (scb, scb->async_context);
164 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
165 error). Nag the client until all the data has been read. In the
166 case of errors, the client will need to close or de-async the
167 device before naging stops. */
170 push_event (void *context)
172 struct serial *scb = context;
173 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
174 scb->async_handler (scb, scb->async_context);
179 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
180 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
183 ser_base_wait_for (struct serial *scb, int timeout)
189 fd_set readfds, exceptfds;
191 /* NOTE: Some OS's can scramble the READFDS when the select()
192 call fails (ex the kernel with Red Hat 5.2). Initialize all
193 arguments before each call. */
199 FD_ZERO (&exceptfds);
200 FD_SET (scb->fd, &readfds);
201 FD_SET (scb->fd, &exceptfds);
204 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
206 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
211 return SERIAL_TIMEOUT;
212 else if (errno == EINTR)
215 return SERIAL_ERROR; /* Got an error from select or poll */
222 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
223 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
224 char if successful. Returns -2 if timeout expired, EOF if line dropped
225 dead, or -3 for any other error (see errno in that case). */
228 do_ser_base_readchar (struct serial *scb, int timeout)
233 /* We have to be able to keep the GUI alive here, so we break the
234 original timeout into steps of 1 second, running the "keep the
235 GUI alive" hook each time through the loop.
237 Also, timeout = 0 means to poll, so we just set the delta to 0,
238 so we will only go through the loop once. */
240 delta = (timeout == 0 ? 0 : 1);
243 /* N.B. The UI may destroy our world (for instance by calling
244 remote_stop,) in which case we want to get out of here as
245 quickly as possible. It is not safe to touch scb, since
246 someone else might have freed it. The
247 deprecated_ui_loop_hook signals that we should exit by
250 if (deprecated_ui_loop_hook)
252 if (deprecated_ui_loop_hook (0))
253 return SERIAL_TIMEOUT;
256 status = ser_base_wait_for (scb, delta);
260 /* If we got a character or an error back from wait_for, then we can
261 break from the loop before the timeout is completed. */
262 if (status != SERIAL_TIMEOUT)
265 /* If we have exhausted the original timeout, then generate
266 a SERIAL_TIMEOUT, and pass it out of the loop. */
267 else if (timeout == 0)
269 status = SERIAL_TIMEOUT;
277 status = scb->ops->read_prim (scb, BUFSIZ);
282 /* 0 chars means timeout. (We may need to distinguish between EOF
283 & timeouts someday.) */
284 return SERIAL_TIMEOUT;
286 /* Got an error from read. */
290 scb->bufcnt = status;
292 scb->bufp = scb->buf;
296 /* Perform operations common to both old and new readchar. */
298 /* Return the next character from the input FIFO. If the FIFO is
299 empty, call the SERIAL specific routine to try and read in more
302 Initially data from the input FIFO is returned (fd_event()
303 pre-reads the input into that FIFO. Once that has been emptied,
304 further data is obtained by polling the input FD using the device
305 specific readchar() function. Note: reschedule() is called after
306 every read. This is because there is no guarentee that the lower
307 level fd_event() poll_event() code (which also calls reschedule())
311 generic_readchar (struct serial *scb, int timeout,
312 int (do_readchar) (struct serial *scb, int timeout))
321 else if (scb->bufcnt < 0)
323 /* Some errors/eof are are sticky. */
328 ch = do_readchar (scb, timeout);
331 switch ((enum serial_rc) ch)
335 /* Make the error/eof stick. */
349 ser_base_readchar (struct serial *scb, int timeout)
351 return generic_readchar (scb, timeout, do_ser_base_readchar);
355 ser_base_write (struct serial *scb, const char *str, int len)
361 cc = scb->ops->write_prim (scb, str, len);
372 ser_base_flush_output (struct serial *scb)
378 ser_base_flush_input (struct serial *scb)
380 if (scb->bufcnt >= 0)
383 scb->bufp = scb->buf;
391 ser_base_send_break (struct serial *scb)
397 ser_base_drain_output (struct serial *scb)
403 ser_base_raw (struct serial *scb)
405 return; /* Always in raw mode */
409 ser_base_get_tty_state (struct serial *scb)
411 /* allocate a dummy */
412 return (serial_ttystate) XMALLOC (int);
416 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
422 ser_base_noflush_set_tty_state (struct serial *scb,
423 serial_ttystate new_ttystate,
424 serial_ttystate old_ttystate)
430 ser_base_print_tty_state (struct serial *scb,
431 serial_ttystate ttystate,
432 struct ui_file *stream)
434 /* Nothing to print. */
439 ser_base_setbaudrate (struct serial *scb, int rate)
441 return 0; /* Never fails! */
445 ser_base_setstopbits (struct serial *scb, int num)
447 return 0; /* Never fails! */
450 /* Put the SERIAL device into/out-of ASYNC mode. */
453 ser_base_async (struct serial *scb,
458 /* Force a re-schedule. */
459 scb->async_state = NOTHING_SCHEDULED;
460 if (serial_debug_p (scb))
461 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
467 if (serial_debug_p (scb))
468 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
470 /* De-schedule whatever tasks are currently scheduled. */
471 switch (scb->async_state)
474 delete_file_handler (scb->fd);
476 case NOTHING_SCHEDULED:
478 default: /* TIMER SCHEDULED */
479 delete_timer (scb->async_state);