| 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 | |
| 28 | static timer_handler_func push_event; |
| 29 | static handler_func fd_event; |
| 30 | |
| 31 | /* Event handling for ASYNC serial code. |
| 32 | |
| 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. |
| 36 | |
| 37 | ASYNC only stops pestering its client when it is de-async'ed or it |
| 38 | is told to go away. */ |
| 39 | |
| 40 | /* Value of scb->async_state: */ |
| 41 | enum { |
| 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. */ |
| 46 | FD_SCHEDULED = -1, |
| 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. */ |
| 53 | }; |
| 54 | |
| 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. */ |
| 59 | |
| 60 | void |
| 61 | reschedule (struct serial *scb) |
| 62 | { |
| 63 | if (serial_is_async_p (scb)) |
| 64 | { |
| 65 | int next_state; |
| 66 | switch (scb->async_state) |
| 67 | { |
| 68 | case FD_SCHEDULED: |
| 69 | if (scb->bufcnt == 0) |
| 70 | next_state = FD_SCHEDULED; |
| 71 | else |
| 72 | { |
| 73 | delete_file_handler (scb->fd); |
| 74 | next_state = create_timer (0, push_event, scb); |
| 75 | } |
| 76 | break; |
| 77 | case NOTHING_SCHEDULED: |
| 78 | if (scb->bufcnt == 0) |
| 79 | { |
| 80 | add_file_handler (scb->fd, fd_event, scb); |
| 81 | next_state = FD_SCHEDULED; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | next_state = create_timer (0, push_event, scb); |
| 86 | } |
| 87 | break; |
| 88 | default: /* TIMER SCHEDULED */ |
| 89 | if (scb->bufcnt == 0) |
| 90 | { |
| 91 | delete_timer (scb->async_state); |
| 92 | add_file_handler (scb->fd, fd_event, scb); |
| 93 | next_state = FD_SCHEDULED; |
| 94 | } |
| 95 | else |
| 96 | next_state = scb->async_state; |
| 97 | break; |
| 98 | } |
| 99 | if (serial_debug_p (scb)) |
| 100 | { |
| 101 | switch (next_state) |
| 102 | { |
| 103 | case FD_SCHEDULED: |
| 104 | if (scb->async_state != FD_SCHEDULED) |
| 105 | fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n", |
| 106 | scb->fd); |
| 107 | break; |
| 108 | default: /* TIMER SCHEDULED */ |
| 109 | if (scb->async_state == FD_SCHEDULED) |
| 110 | fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n", |
| 111 | scb->fd); |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | scb->async_state = next_state; |
| 116 | } |
| 117 | } |
| 118 | |
| 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. */ |
| 124 | |
| 125 | static void |
| 126 | fd_event (int error, void *context) |
| 127 | { |
| 128 | struct serial *scb = context; |
| 129 | if (error != 0) |
| 130 | { |
| 131 | scb->bufcnt = SERIAL_ERROR; |
| 132 | } |
| 133 | else if (scb->bufcnt == 0) |
| 134 | { |
| 135 | /* Prime the input FIFO. The readchar() function is used to |
| 136 | pull characters out of the buffer. See also |
| 137 | generic_readchar(). */ |
| 138 | int nr; |
| 139 | do |
| 140 | { |
| 141 | nr = read (scb->fd, scb->buf, BUFSIZ); |
| 142 | } |
| 143 | while (nr == -1 && errno == EINTR); |
| 144 | if (nr == 0) |
| 145 | { |
| 146 | scb->bufcnt = SERIAL_EOF; |
| 147 | } |
| 148 | else if (nr > 0) |
| 149 | { |
| 150 | scb->bufcnt = nr; |
| 151 | scb->bufp = scb->buf; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | scb->bufcnt = SERIAL_ERROR; |
| 156 | } |
| 157 | } |
| 158 | scb->async_handler (scb, scb->async_context); |
| 159 | reschedule (scb); |
| 160 | } |
| 161 | |
| 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. */ |
| 166 | |
| 167 | static void |
| 168 | push_event (void *context) |
| 169 | { |
| 170 | struct serial *scb = context; |
| 171 | scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */ |
| 172 | scb->async_handler (scb, scb->async_context); |
| 173 | /* re-schedule */ |
| 174 | reschedule (scb); |
| 175 | } |
| 176 | |
| 177 | int |
| 178 | ser_base_write (struct serial *scb, const char *str, int len) |
| 179 | { |
| 180 | int cc; |
| 181 | |
| 182 | while (len > 0) |
| 183 | { |
| 184 | cc = write (scb->fd, str, len); |
| 185 | |
| 186 | if (cc < 0) |
| 187 | return 1; |
| 188 | len -= cc; |
| 189 | str += cc; |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | int |
| 195 | ser_base_flush_output (struct serial *scb) |
| 196 | { |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int |
| 201 | ser_base_flush_input (struct serial *scb) |
| 202 | { |
| 203 | if (scb->bufcnt >= 0) |
| 204 | { |
| 205 | scb->bufcnt = 0; |
| 206 | scb->bufp = scb->buf; |
| 207 | return 0; |
| 208 | } |
| 209 | else |
| 210 | return SERIAL_ERROR; |
| 211 | } |
| 212 | |
| 213 | int |
| 214 | ser_base_send_break (struct serial *scb) |
| 215 | { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | int |
| 220 | ser_base_drain_output (struct serial *scb) |
| 221 | { |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | ser_base_raw (struct serial *scb) |
| 227 | { |
| 228 | return; /* Always in raw mode */ |
| 229 | } |
| 230 | |
| 231 | serial_ttystate |
| 232 | ser_base_get_tty_state (struct serial *scb) |
| 233 | { |
| 234 | /* allocate a dummy */ |
| 235 | return (serial_ttystate) XMALLOC (int); |
| 236 | } |
| 237 | |
| 238 | int |
| 239 | ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate) |
| 240 | { |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int |
| 245 | ser_base_noflush_set_tty_state (struct serial *scb, |
| 246 | serial_ttystate new_ttystate, |
| 247 | serial_ttystate old_ttystate) |
| 248 | { |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | ser_base_print_tty_state (struct serial *scb, |
| 254 | serial_ttystate ttystate, |
| 255 | struct ui_file *stream) |
| 256 | { |
| 257 | /* Nothing to print. */ |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | int |
| 262 | ser_base_setbaudrate (struct serial *scb, int rate) |
| 263 | { |
| 264 | return 0; /* Never fails! */ |
| 265 | } |
| 266 | |
| 267 | int |
| 268 | ser_base_setstopbits (struct serial *scb, int num) |
| 269 | { |
| 270 | return 0; /* Never fails! */ |
| 271 | } |
| 272 | |
| 273 | /* Put the SERIAL device into/out-of ASYNC mode. */ |
| 274 | |
| 275 | void |
| 276 | ser_base_async (struct serial *scb, |
| 277 | int async_p) |
| 278 | { |
| 279 | if (async_p) |
| 280 | { |
| 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", |
| 285 | scb->fd); |
| 286 | reschedule (scb); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | if (serial_debug_p (scb)) |
| 291 | fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n", |
| 292 | scb->fd); |
| 293 | /* De-schedule whatever tasks are currently scheduled. */ |
| 294 | switch (scb->async_state) |
| 295 | { |
| 296 | case FD_SCHEDULED: |
| 297 | delete_file_handler (scb->fd); |
| 298 | break; |
| 299 | case NOTHING_SCHEDULED: |
| 300 | break; |
| 301 | default: /* TIMER SCHEDULED */ |
| 302 | delete_timer (scb->async_state); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |