]> Git Repo - binutils.git/blob - gdb/ser-base.c
* ser-base.c: Include "gdb_string.h".
[binutils.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-unix.h"
26 #include "event-loop.h"
27 #ifdef USE_WIN32API
28 #include <winsock2.h>
29 #endif
30
31 #include "gdb_string.h"
32
33 static timer_handler_func push_event;
34 static handler_func fd_event;
35
36 /* Event handling for ASYNC serial code.
37
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.
41
42    ASYNC only stops pestering its client when it is de-async'ed or it
43    is told to go away. */
44
45 /* Value of scb->async_state: */
46 enum {
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. */
51   FD_SCHEDULED = -1,
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. */
58 };
59
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. */
64
65 void
66 reschedule (struct serial *scb)
67 {
68   if (serial_is_async_p (scb))
69     {
70       int next_state;
71       switch (scb->async_state)
72         {
73         case FD_SCHEDULED:
74           if (scb->bufcnt == 0)
75             next_state = FD_SCHEDULED;
76           else
77             {
78               delete_file_handler (scb->fd);
79               next_state = create_timer (0, push_event, scb);
80             }
81           break;
82         case NOTHING_SCHEDULED:
83           if (scb->bufcnt == 0)
84             {
85               add_file_handler (scb->fd, fd_event, scb);
86               next_state = FD_SCHEDULED;
87             }
88           else
89             {
90               next_state = create_timer (0, push_event, scb);
91             }
92           break;
93         default: /* TIMER SCHEDULED */
94           if (scb->bufcnt == 0)
95             {
96               delete_timer (scb->async_state);
97               add_file_handler (scb->fd, fd_event, scb);
98               next_state = FD_SCHEDULED;
99             }
100           else
101             next_state = scb->async_state;
102           break;
103         }
104       if (serial_debug_p (scb))
105         {
106           switch (next_state)
107             {
108             case FD_SCHEDULED:
109               if (scb->async_state != FD_SCHEDULED)
110                 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
111                                     scb->fd);
112               break;
113             default: /* TIMER SCHEDULED */
114               if (scb->async_state == FD_SCHEDULED)
115                 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
116                                     scb->fd);
117               break;
118             }
119         }
120       scb->async_state = next_state;
121     }
122 }
123
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. */
129
130 static void
131 fd_event (int error, void *context)
132 {
133   struct serial *scb = context;
134   if (error != 0)
135     {
136       scb->bufcnt = SERIAL_ERROR;
137     }
138   else if (scb->bufcnt == 0)
139     {
140       /* Prime the input FIFO.  The readchar() function is used to
141          pull characters out of the buffer.  See also
142          generic_readchar(). */
143       int nr;
144       nr = scb->ops->read_prim (scb, BUFSIZ);
145       if (nr == 0)
146         {
147           scb->bufcnt = SERIAL_EOF;
148         }
149       else if (nr > 0)
150         {
151           scb->bufcnt = nr;
152           scb->bufp = scb->buf;
153         }
154       else
155         {
156           scb->bufcnt = SERIAL_ERROR;
157         }
158     }
159   scb->async_handler (scb, scb->async_context);
160   reschedule (scb);
161 }
162
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. */
167
168 static void
169 push_event (void *context)
170 {
171   struct serial *scb = context;
172   scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
173   scb->async_handler (scb, scb->async_context);
174   /* re-schedule */
175   reschedule (scb);
176 }
177
178 /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
179    otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
180
181 static int
182 ser_base_wait_for (struct serial *scb, int timeout)
183 {
184   while (1)
185     {
186       int numfds;
187       struct timeval tv;
188       fd_set readfds, exceptfds;
189
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. */
193
194       tv.tv_sec = timeout;
195       tv.tv_usec = 0;
196
197       FD_ZERO (&readfds);
198       FD_ZERO (&exceptfds);
199       FD_SET (scb->fd, &readfds);
200       FD_SET (scb->fd, &exceptfds);
201
202       if (timeout >= 0)
203         numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
204       else
205         numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
206
207       if (numfds <= 0)
208         {
209           if (numfds == 0)
210             return SERIAL_TIMEOUT;
211           else if (errno == EINTR)
212             continue;
213           else
214             return SERIAL_ERROR;        /* Got an error from select or poll */
215         }
216
217       return 0;
218     }
219 }
220
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). */
225
226 static int
227 do_ser_base_readchar (struct serial *scb, int timeout)
228 {
229   int status;
230   int delta;
231
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.
235
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.  */
238
239   delta = (timeout == 0 ? 0 : 1);
240   while (1)
241     {
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
247          returning 1.  */
248
249       if (deprecated_ui_loop_hook)
250         {
251           if (deprecated_ui_loop_hook (0))
252             return SERIAL_TIMEOUT;
253         }
254
255       status = ser_base_wait_for (scb, delta);
256       if (timeout > 0)
257         timeout -= delta;
258
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)
262         break;
263
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)
267         {
268           status = SERIAL_TIMEOUT;
269           break;
270         }
271     }
272
273   if (status < 0)
274     return status;
275
276   status = scb->ops->read_prim (scb, BUFSIZ);
277
278   if (status <= 0)
279     {
280       if (status == 0)
281         /* 0 chars means timeout.  (We may need to distinguish between EOF
282            & timeouts someday.)  */
283         return SERIAL_TIMEOUT;  
284       else
285         /* Got an error from read.  */
286         return SERIAL_ERROR;    
287     }
288
289   scb->bufcnt = status;
290   scb->bufcnt--;
291   scb->bufp = scb->buf;
292   return *scb->bufp++;
293 }
294
295 /* Perform operations common to both old and new readchar. */
296
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
299    characters.
300
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())
307    will be called. */
308
309 int
310 generic_readchar (struct serial *scb, int timeout,
311                   int (do_readchar) (struct serial *scb, int timeout))
312 {
313   int ch;
314   if (scb->bufcnt > 0)
315     {
316       ch = *scb->bufp;
317       scb->bufcnt--;
318       scb->bufp++;
319     }
320   else if (scb->bufcnt < 0)
321     {
322       /* Some errors/eof are are sticky. */
323       ch = scb->bufcnt;
324     }
325   else
326     {
327       ch = do_readchar (scb, timeout);
328       if (ch < 0)
329         {
330           switch ((enum serial_rc) ch)
331             {
332             case SERIAL_EOF:
333             case SERIAL_ERROR:
334               /* Make the error/eof stick. */
335               scb->bufcnt = ch;
336               break;
337             case SERIAL_TIMEOUT:
338               scb->bufcnt = 0;
339               break;
340             }
341         }
342     }
343   reschedule (scb);
344   return ch;
345 }
346
347 int
348 ser_base_readchar (struct serial *scb, int timeout)
349 {
350   return generic_readchar (scb, timeout, do_ser_base_readchar);
351 }
352
353 int
354 ser_base_write (struct serial *scb, const char *str, int len)
355 {
356   int cc;
357
358   while (len > 0)
359     {
360       cc = scb->ops->write_prim (scb, str, len); 
361
362       if (cc < 0)
363         return 1;
364       len -= cc;
365       str += cc;
366     }
367   return 0;
368 }
369
370 int
371 ser_base_flush_output (struct serial *scb)
372 {
373   return 0;
374 }
375
376 int
377 ser_base_flush_input (struct serial *scb)
378 {
379   if (scb->bufcnt >= 0)
380     {
381       scb->bufcnt = 0;
382       scb->bufp = scb->buf;
383       return 0;
384     }
385   else
386     return SERIAL_ERROR;
387 }
388
389 int
390 ser_base_send_break (struct serial *scb)
391 {
392   return 0;
393 }
394
395 int
396 ser_base_drain_output (struct serial *scb)
397 {
398   return 0;
399 }
400
401 void
402 ser_base_raw (struct serial *scb)
403 {
404   return;                       /* Always in raw mode */
405 }
406
407 serial_ttystate
408 ser_base_get_tty_state (struct serial *scb)
409 {
410   /* allocate a dummy */
411   return (serial_ttystate) XMALLOC (int);
412 }
413
414 int
415 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
416 {
417   return 0;
418 }
419
420 int
421 ser_base_noflush_set_tty_state (struct serial *scb,
422                                 serial_ttystate new_ttystate,
423                                 serial_ttystate old_ttystate)
424 {
425   return 0;
426 }
427
428 void
429 ser_base_print_tty_state (struct serial *scb, 
430                           serial_ttystate ttystate,
431                           struct ui_file *stream)
432 {
433   /* Nothing to print.  */
434   return;
435 }
436
437 int
438 ser_base_setbaudrate (struct serial *scb, int rate)
439 {
440   return 0;                     /* Never fails! */
441 }
442
443 int
444 ser_base_setstopbits (struct serial *scb, int num)
445 {
446   return 0;                     /* Never fails! */
447 }
448
449 /* Put the SERIAL device into/out-of ASYNC mode.  */
450
451 void
452 ser_base_async (struct serial *scb,
453                 int async_p)
454 {
455   if (async_p)
456     {
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",
461                             scb->fd);
462       reschedule (scb);
463     }
464   else
465     {
466       if (serial_debug_p (scb))
467         fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
468                             scb->fd);
469       /* De-schedule whatever tasks are currently scheduled. */
470       switch (scb->async_state)
471         {
472         case FD_SCHEDULED:
473           delete_file_handler (scb->fd);
474           break;
475         case NOTHING_SCHEDULED:
476           break;
477         default: /* TIMER SCHEDULED */
478           delete_timer (scb->async_state);
479           break;
480         }
481     }
482 }
This page took 0.051188 seconds and 4 git commands to generate.