2005-04-28 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / ser-base.c
1 /* Generic serial interface functions.
2
3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "serial.h"
25 #include "ser-base.h"
26 #include "event-loop.h"
27
28 #include "gdb_string.h"
29 #include <sys/time.h>
30 #ifdef USE_WIN32API
31 #include <winsock2.h>
32 #endif
33
34
35 static timer_handler_func push_event;
36 static handler_func fd_event;
37
38 /* Event handling for ASYNC serial code.
39
40 At any time the SERIAL device either: has an empty FIFO and is
41 waiting on a FD event; or has a non-empty FIFO/error condition and
42 is constantly scheduling timer events.
43
44 ASYNC only stops pestering its client when it is de-async'ed or it
45 is told to go away. */
46
47 /* Value of scb->async_state: */
48 enum {
49 /* >= 0 (TIMER_SCHEDULED) */
50 /* The ID of the currently scheduled timer event. This state is
51 rarely encountered. Timer events are one-off so as soon as the
52 event is delivered the state is shanged to NOTHING_SCHEDULED. */
53 FD_SCHEDULED = -1,
54 /* The fd_event() handler is scheduled. It is called when ever the
55 file descriptor becomes ready. */
56 NOTHING_SCHEDULED = -2
57 /* Either no task is scheduled (just going into ASYNC mode) or a
58 timer event has just gone off and the current state has been
59 forced into nothing scheduled. */
60 };
61
62 /* Identify and schedule the next ASYNC task based on scb->async_state
63 and scb->buf* (the input FIFO). A state machine is used to avoid
64 the need to make redundant calls into the event-loop - the next
65 scheduled task is only changed when needed. */
66
67 void
68 reschedule (struct serial *scb)
69 {
70 if (serial_is_async_p (scb))
71 {
72 int next_state;
73 switch (scb->async_state)
74 {
75 case FD_SCHEDULED:
76 if (scb->bufcnt == 0)
77 next_state = FD_SCHEDULED;
78 else
79 {
80 delete_file_handler (scb->fd);
81 next_state = create_timer (0, push_event, scb);
82 }
83 break;
84 case NOTHING_SCHEDULED:
85 if (scb->bufcnt == 0)
86 {
87 add_file_handler (scb->fd, fd_event, scb);
88 next_state = FD_SCHEDULED;
89 }
90 else
91 {
92 next_state = create_timer (0, push_event, scb);
93 }
94 break;
95 default: /* TIMER SCHEDULED */
96 if (scb->bufcnt == 0)
97 {
98 delete_timer (scb->async_state);
99 add_file_handler (scb->fd, fd_event, scb);
100 next_state = FD_SCHEDULED;
101 }
102 else
103 next_state = scb->async_state;
104 break;
105 }
106 if (serial_debug_p (scb))
107 {
108 switch (next_state)
109 {
110 case FD_SCHEDULED:
111 if (scb->async_state != FD_SCHEDULED)
112 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
113 scb->fd);
114 break;
115 default: /* TIMER SCHEDULED */
116 if (scb->async_state == FD_SCHEDULED)
117 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
118 scb->fd);
119 break;
120 }
121 }
122 scb->async_state = next_state;
123 }
124 }
125
126 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
127 is no pending error). As soon as data arrives, it is read into the
128 input FIFO and the client notified. The client should then drain
129 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
130 push_event() is used to nag the client until it is. */
131
132 static void
133 fd_event (int error, void *context)
134 {
135 struct serial *scb = context;
136 if (error != 0)
137 {
138 scb->bufcnt = SERIAL_ERROR;
139 }
140 else if (scb->bufcnt == 0)
141 {
142 /* Prime the input FIFO. The readchar() function is used to
143 pull characters out of the buffer. See also
144 generic_readchar(). */
145 int nr;
146 nr = scb->ops->read_prim (scb, BUFSIZ);
147 if (nr == 0)
148 {
149 scb->bufcnt = SERIAL_EOF;
150 }
151 else if (nr > 0)
152 {
153 scb->bufcnt = nr;
154 scb->bufp = scb->buf;
155 }
156 else
157 {
158 scb->bufcnt = SERIAL_ERROR;
159 }
160 }
161 scb->async_handler (scb, scb->async_context);
162 reschedule (scb);
163 }
164
165 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
166 error). Nag the client until all the data has been read. In the
167 case of errors, the client will need to close or de-async the
168 device before naging stops. */
169
170 static void
171 push_event (void *context)
172 {
173 struct serial *scb = context;
174 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
175 scb->async_handler (scb, scb->async_context);
176 /* re-schedule */
177 reschedule (scb);
178 }
179
180 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
181 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
182
183 static int
184 ser_base_wait_for (struct serial *scb, int timeout)
185 {
186 while (1)
187 {
188 int numfds;
189 struct timeval tv;
190 fd_set readfds, exceptfds;
191
192 /* NOTE: Some OS's can scramble the READFDS when the select()
193 call fails (ex the kernel with Red Hat 5.2). Initialize all
194 arguments before each call. */
195
196 tv.tv_sec = timeout;
197 tv.tv_usec = 0;
198
199 FD_ZERO (&readfds);
200 FD_ZERO (&exceptfds);
201 FD_SET (scb->fd, &readfds);
202 FD_SET (scb->fd, &exceptfds);
203
204 if (timeout >= 0)
205 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
206 else
207 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
208
209 if (numfds <= 0)
210 {
211 if (numfds == 0)
212 return SERIAL_TIMEOUT;
213 else if (errno == EINTR)
214 continue;
215 else
216 return SERIAL_ERROR; /* Got an error from select or poll */
217 }
218
219 return 0;
220 }
221 }
222
223 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
224 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
225 char if successful. Returns -2 if timeout expired, EOF if line dropped
226 dead, or -3 for any other error (see errno in that case). */
227
228 static int
229 do_ser_base_readchar (struct serial *scb, int timeout)
230 {
231 int status;
232 int delta;
233
234 /* We have to be able to keep the GUI alive here, so we break the
235 original timeout into steps of 1 second, running the "keep the
236 GUI alive" hook each time through the loop.
237
238 Also, timeout = 0 means to poll, so we just set the delta to 0,
239 so we will only go through the loop once. */
240
241 delta = (timeout == 0 ? 0 : 1);
242 while (1)
243 {
244 /* N.B. The UI may destroy our world (for instance by calling
245 remote_stop,) in which case we want to get out of here as
246 quickly as possible. It is not safe to touch scb, since
247 someone else might have freed it. The
248 deprecated_ui_loop_hook signals that we should exit by
249 returning 1. */
250
251 if (deprecated_ui_loop_hook)
252 {
253 if (deprecated_ui_loop_hook (0))
254 return SERIAL_TIMEOUT;
255 }
256
257 status = ser_base_wait_for (scb, delta);
258 if (timeout > 0)
259 timeout -= delta;
260
261 /* If we got a character or an error back from wait_for, then we can
262 break from the loop before the timeout is completed. */
263 if (status != SERIAL_TIMEOUT)
264 break;
265
266 /* If we have exhausted the original timeout, then generate
267 a SERIAL_TIMEOUT, and pass it out of the loop. */
268 else if (timeout == 0)
269 {
270 status = SERIAL_TIMEOUT;
271 break;
272 }
273 }
274
275 if (status < 0)
276 return status;
277
278 status = scb->ops->read_prim (scb, BUFSIZ);
279
280 if (status <= 0)
281 {
282 if (status == 0)
283 /* 0 chars means timeout. (We may need to distinguish between EOF
284 & timeouts someday.) */
285 return SERIAL_TIMEOUT;
286 else
287 /* Got an error from read. */
288 return SERIAL_ERROR;
289 }
290
291 scb->bufcnt = status;
292 scb->bufcnt--;
293 scb->bufp = scb->buf;
294 return *scb->bufp++;
295 }
296
297 /* Perform operations common to both old and new readchar. */
298
299 /* Return the next character from the input FIFO. If the FIFO is
300 empty, call the SERIAL specific routine to try and read in more
301 characters.
302
303 Initially data from the input FIFO is returned (fd_event()
304 pre-reads the input into that FIFO. Once that has been emptied,
305 further data is obtained by polling the input FD using the device
306 specific readchar() function. Note: reschedule() is called after
307 every read. This is because there is no guarentee that the lower
308 level fd_event() poll_event() code (which also calls reschedule())
309 will be called. */
310
311 int
312 generic_readchar (struct serial *scb, int timeout,
313 int (do_readchar) (struct serial *scb, int timeout))
314 {
315 int ch;
316 if (scb->bufcnt > 0)
317 {
318 ch = *scb->bufp;
319 scb->bufcnt--;
320 scb->bufp++;
321 }
322 else if (scb->bufcnt < 0)
323 {
324 /* Some errors/eof are are sticky. */
325 ch = scb->bufcnt;
326 }
327 else
328 {
329 ch = do_readchar (scb, timeout);
330 if (ch < 0)
331 {
332 switch ((enum serial_rc) ch)
333 {
334 case SERIAL_EOF:
335 case SERIAL_ERROR:
336 /* Make the error/eof stick. */
337 scb->bufcnt = ch;
338 break;
339 case SERIAL_TIMEOUT:
340 scb->bufcnt = 0;
341 break;
342 }
343 }
344 }
345 reschedule (scb);
346 return ch;
347 }
348
349 int
350 ser_base_readchar (struct serial *scb, int timeout)
351 {
352 return generic_readchar (scb, timeout, do_ser_base_readchar);
353 }
354
355 int
356 ser_base_write (struct serial *scb, const char *str, int len)
357 {
358 int cc;
359
360 while (len > 0)
361 {
362 cc = scb->ops->write_prim (scb, str, len);
363
364 if (cc < 0)
365 return 1;
366 len -= cc;
367 str += cc;
368 }
369 return 0;
370 }
371
372 int
373 ser_base_flush_output (struct serial *scb)
374 {
375 return 0;
376 }
377
378 int
379 ser_base_flush_input (struct serial *scb)
380 {
381 if (scb->bufcnt >= 0)
382 {
383 scb->bufcnt = 0;
384 scb->bufp = scb->buf;
385 return 0;
386 }
387 else
388 return SERIAL_ERROR;
389 }
390
391 int
392 ser_base_send_break (struct serial *scb)
393 {
394 return 0;
395 }
396
397 int
398 ser_base_drain_output (struct serial *scb)
399 {
400 return 0;
401 }
402
403 void
404 ser_base_raw (struct serial *scb)
405 {
406 return; /* Always in raw mode */
407 }
408
409 serial_ttystate
410 ser_base_get_tty_state (struct serial *scb)
411 {
412 /* allocate a dummy */
413 return (serial_ttystate) XMALLOC (int);
414 }
415
416 int
417 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
418 {
419 return 0;
420 }
421
422 int
423 ser_base_noflush_set_tty_state (struct serial *scb,
424 serial_ttystate new_ttystate,
425 serial_ttystate old_ttystate)
426 {
427 return 0;
428 }
429
430 void
431 ser_base_print_tty_state (struct serial *scb,
432 serial_ttystate ttystate,
433 struct ui_file *stream)
434 {
435 /* Nothing to print. */
436 return;
437 }
438
439 int
440 ser_base_setbaudrate (struct serial *scb, int rate)
441 {
442 return 0; /* Never fails! */
443 }
444
445 int
446 ser_base_setstopbits (struct serial *scb, int num)
447 {
448 return 0; /* Never fails! */
449 }
450
451 /* Put the SERIAL device into/out-of ASYNC mode. */
452
453 void
454 ser_base_async (struct serial *scb,
455 int async_p)
456 {
457 if (async_p)
458 {
459 /* Force a re-schedule. */
460 scb->async_state = NOTHING_SCHEDULED;
461 if (serial_debug_p (scb))
462 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
463 scb->fd);
464 reschedule (scb);
465 }
466 else
467 {
468 if (serial_debug_p (scb))
469 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
470 scb->fd);
471 /* De-schedule whatever tasks are currently scheduled. */
472 switch (scb->async_state)
473 {
474 case FD_SCHEDULED:
475 delete_file_handler (scb->fd);
476 break;
477 case NOTHING_SCHEDULED:
478 break;
479 default: /* TIMER SCHEDULED */
480 delete_timer (scb->async_state);
481 break;
482 }
483 }
484 }
This page took 0.039339 seconds and 4 git commands to generate.