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"
28 static timer_handler_func push_event
;
29 static handler_func fd_event
;
31 /* Event handling for ASYNC serial code.
33 At any time the SERIAL device either: has an empty FIFO and is
34 waiting on a FD event; or has a non-empty FIFO/error condition and
35 is constantly scheduling timer events.
37 ASYNC only stops pestering its client when it is de-async'ed or it
38 is told to go away. */
40 /* Value of scb->async_state: */
42 /* >= 0 (TIMER_SCHEDULED) */
43 /* The ID of the currently scheduled timer event. This state is
44 rarely encountered. Timer events are one-off so as soon as the
45 event is delivered the state is shanged to NOTHING_SCHEDULED. */
47 /* The fd_event() handler is scheduled. It is called when ever the
48 file descriptor becomes ready. */
49 NOTHING_SCHEDULED
= -2
50 /* Either no task is scheduled (just going into ASYNC mode) or a
51 timer event has just gone off and the current state has been
52 forced into nothing scheduled. */
55 /* Identify and schedule the next ASYNC task based on scb->async_state
56 and scb->buf* (the input FIFO). A state machine is used to avoid
57 the need to make redundant calls into the event-loop - the next
58 scheduled task is only changed when needed. */
61 reschedule (struct serial
*scb
)
63 if (serial_is_async_p (scb
))
66 switch (scb
->async_state
)
70 next_state
= FD_SCHEDULED
;
73 delete_file_handler (scb
->fd
);
74 next_state
= create_timer (0, push_event
, scb
);
77 case NOTHING_SCHEDULED
:
80 add_file_handler (scb
->fd
, fd_event
, scb
);
81 next_state
= FD_SCHEDULED
;
85 next_state
= create_timer (0, push_event
, scb
);
88 default: /* TIMER SCHEDULED */
91 delete_timer (scb
->async_state
);
92 add_file_handler (scb
->fd
, fd_event
, scb
);
93 next_state
= FD_SCHEDULED
;
96 next_state
= scb
->async_state
;
99 if (serial_debug_p (scb
))
104 if (scb
->async_state
!= FD_SCHEDULED
)
105 fprintf_unfiltered (gdb_stdlog
, "[fd%d->fd-scheduled]\n",
108 default: /* TIMER SCHEDULED */
109 if (scb
->async_state
== FD_SCHEDULED
)
110 fprintf_unfiltered (gdb_stdlog
, "[fd%d->timer-scheduled]\n",
115 scb
->async_state
= next_state
;
119 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
120 is no pending error). As soon as data arrives, it is read into the
121 input FIFO and the client notified. The client should then drain
122 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
123 push_event() is used to nag the client until it is. */
126 fd_event (int error
, void *context
)
128 struct serial
*scb
= context
;
131 scb
->bufcnt
= SERIAL_ERROR
;
133 else if (scb
->bufcnt
== 0)
135 /* Prime the input FIFO. The readchar() function is used to
136 pull characters out of the buffer. See also
137 generic_readchar(). */
141 nr
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
143 while (nr
== -1 && errno
== EINTR
);
146 scb
->bufcnt
= SERIAL_EOF
;
151 scb
->bufp
= scb
->buf
;
155 scb
->bufcnt
= SERIAL_ERROR
;
158 scb
->async_handler (scb
, scb
->async_context
);
162 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
163 error). Nag the client until all the data has been read. In the
164 case of errors, the client will need to close or de-async the
165 device before naging stops. */
168 push_event (void *context
)
170 struct serial
*scb
= context
;
171 scb
->async_state
= NOTHING_SCHEDULED
; /* Timers are one-off */
172 scb
->async_handler (scb
, scb
->async_context
);
178 ser_base_write (struct serial
*scb
, const char *str
, int len
)
184 cc
= write (scb
->fd
, str
, len
);
195 ser_base_flush_output (struct serial
*scb
)
201 ser_base_flush_input (struct serial
*scb
)
203 if (scb
->bufcnt
>= 0)
206 scb
->bufp
= scb
->buf
;
214 ser_base_send_break (struct serial
*scb
)
220 ser_base_drain_output (struct serial
*scb
)
226 ser_base_raw (struct serial
*scb
)
228 return; /* Always in raw mode */
232 ser_base_get_tty_state (struct serial
*scb
)
234 /* allocate a dummy */
235 return (serial_ttystate
) XMALLOC (int);
239 ser_base_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
245 ser_base_noflush_set_tty_state (struct serial
*scb
,
246 serial_ttystate new_ttystate
,
247 serial_ttystate old_ttystate
)
253 ser_base_print_tty_state (struct serial
*scb
,
254 serial_ttystate ttystate
,
255 struct ui_file
*stream
)
257 /* Nothing to print. */
262 ser_base_setbaudrate (struct serial
*scb
, int rate
)
264 return 0; /* Never fails! */
268 ser_base_setstopbits (struct serial
*scb
, int num
)
270 return 0; /* Never fails! */
273 /* Put the SERIAL device into/out-of ASYNC mode. */
276 ser_base_async (struct serial
*scb
,
281 /* Force a re-schedule. */
282 scb
->async_state
= NOTHING_SCHEDULED
;
283 if (serial_debug_p (scb
))
284 fprintf_unfiltered (gdb_stdlog
, "[fd%d->asynchronous]\n",
290 if (serial_debug_p (scb
))
291 fprintf_unfiltered (gdb_stdlog
, "[fd%d->synchronous]\n",
293 /* De-schedule whatever tasks are currently scheduled. */
294 switch (scb
->async_state
)
297 delete_file_handler (scb
->fd
);
299 case NOTHING_SCHEDULED
:
301 default: /* TIMER SCHEDULED */
302 delete_timer (scb
->async_state
);