]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Serial interface for local (hardwired) serial ports on Un*x like systems |
1e4728e7 AC |
2 | |
3 | Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, | |
4 | 2003, 2004 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
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. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "serial.h" | |
c2c6d25f JM |
25 | #include "ser-unix.h" |
26 | ||
c906108c SS |
27 | #include <fcntl.h> |
28 | #include <sys/types.h> | |
29 | #include "terminal.h" | |
c2c6d25f JM |
30 | #include <sys/socket.h> |
31 | #include <sys/time.h> | |
32 | ||
33 | #include "gdb_string.h" | |
34 | #include "event-loop.h" | |
35 | ||
c906108c SS |
36 | #ifdef HAVE_TERMIOS |
37 | ||
38 | struct hardwire_ttystate | |
c5aa993b JM |
39 | { |
40 | struct termios termios; | |
41 | }; | |
c906108c SS |
42 | #endif /* termios */ |
43 | ||
44 | #ifdef HAVE_TERMIO | |
45 | ||
46 | /* It is believed that all systems which have added job control to SVR3 | |
47 | (e.g. sco) have also added termios. Even if not, trying to figure out | |
48 | all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty | |
49 | bewildering. So we don't attempt it. */ | |
50 | ||
51 | struct hardwire_ttystate | |
c5aa993b JM |
52 | { |
53 | struct termio termio; | |
54 | }; | |
c906108c SS |
55 | #endif /* termio */ |
56 | ||
57 | #ifdef HAVE_SGTTY | |
c906108c | 58 | struct hardwire_ttystate |
c5aa993b JM |
59 | { |
60 | struct sgttyb sgttyb; | |
61 | struct tchars tc; | |
62 | struct ltchars ltc; | |
63 | /* Line discipline flags. */ | |
64 | int lmode; | |
65 | }; | |
c906108c SS |
66 | #endif /* sgtty */ |
67 | ||
819cc324 AC |
68 | static int hardwire_open (struct serial *scb, const char *name); |
69 | static void hardwire_raw (struct serial *scb); | |
70 | static int wait_for (struct serial *scb, int timeout); | |
71 | static int hardwire_readchar (struct serial *scb, int timeout); | |
72 | static int do_hardwire_readchar (struct serial *scb, int timeout); | |
73 | static int generic_readchar (struct serial *scb, int timeout, | |
74 | int (*do_readchar) (struct serial *scb, | |
75 | int timeout)); | |
c2c6d25f | 76 | static int rate_to_code (int rate); |
819cc324 AC |
77 | static int hardwire_setbaudrate (struct serial *scb, int rate); |
78 | static void hardwire_close (struct serial *scb); | |
79 | static int get_tty_state (struct serial *scb, | |
80 | struct hardwire_ttystate * state); | |
81 | static int set_tty_state (struct serial *scb, | |
82 | struct hardwire_ttystate * state); | |
83 | static serial_ttystate hardwire_get_tty_state (struct serial *scb); | |
84 | static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state); | |
85 | static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate, | |
86 | serial_ttystate); | |
87 | static void hardwire_print_tty_state (struct serial *, serial_ttystate, | |
88 | struct ui_file *); | |
89 | static int hardwire_drain_output (struct serial *); | |
90 | static int hardwire_flush_output (struct serial *); | |
91 | static int hardwire_flush_input (struct serial *); | |
92 | static int hardwire_send_break (struct serial *); | |
93 | static int hardwire_setstopbits (struct serial *, int); | |
94 | ||
95 | static int do_unix_readchar (struct serial *scb, int timeout); | |
2acceee2 JM |
96 | static timer_handler_func push_event; |
97 | static handler_func fd_event; | |
819cc324 | 98 | static void reschedule (struct serial *scb); |
2acceee2 | 99 | |
c2c6d25f JM |
100 | void _initialize_ser_hardwire (void); |
101 | ||
c906108c SS |
102 | /* Open up a real live device for serial I/O */ |
103 | ||
104 | static int | |
819cc324 | 105 | hardwire_open (struct serial *scb, const char *name) |
c906108c SS |
106 | { |
107 | scb->fd = open (name, O_RDWR); | |
108 | if (scb->fd < 0) | |
109 | return -1; | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
114 | static int | |
819cc324 | 115 | get_tty_state (struct serial *scb, struct hardwire_ttystate *state) |
c906108c SS |
116 | { |
117 | #ifdef HAVE_TERMIOS | |
c5aa993b | 118 | if (tcgetattr (scb->fd, &state->termios) < 0) |
c906108c SS |
119 | return -1; |
120 | ||
121 | return 0; | |
122 | #endif | |
123 | ||
124 | #ifdef HAVE_TERMIO | |
125 | if (ioctl (scb->fd, TCGETA, &state->termio) < 0) | |
126 | return -1; | |
127 | return 0; | |
128 | #endif | |
129 | ||
130 | #ifdef HAVE_SGTTY | |
131 | if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0) | |
132 | return -1; | |
133 | if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0) | |
134 | return -1; | |
135 | if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0) | |
136 | return -1; | |
137 | if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0) | |
138 | return -1; | |
139 | ||
140 | return 0; | |
141 | #endif | |
142 | } | |
143 | ||
144 | static int | |
819cc324 | 145 | set_tty_state (struct serial *scb, struct hardwire_ttystate *state) |
c906108c SS |
146 | { |
147 | #ifdef HAVE_TERMIOS | |
c5aa993b | 148 | if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0) |
c906108c SS |
149 | return -1; |
150 | ||
151 | return 0; | |
152 | #endif | |
153 | ||
154 | #ifdef HAVE_TERMIO | |
155 | if (ioctl (scb->fd, TCSETA, &state->termio) < 0) | |
156 | return -1; | |
157 | return 0; | |
158 | #endif | |
159 | ||
160 | #ifdef HAVE_SGTTY | |
161 | if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0) | |
162 | return -1; | |
163 | if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0) | |
164 | return -1; | |
165 | if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0) | |
166 | return -1; | |
167 | if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0) | |
168 | return -1; | |
169 | ||
170 | return 0; | |
171 | #endif | |
172 | } | |
173 | ||
174 | static serial_ttystate | |
819cc324 | 175 | hardwire_get_tty_state (struct serial *scb) |
c906108c SS |
176 | { |
177 | struct hardwire_ttystate *state; | |
178 | ||
c5aa993b | 179 | state = (struct hardwire_ttystate *) xmalloc (sizeof *state); |
c906108c | 180 | |
c5aa993b | 181 | if (get_tty_state (scb, state)) |
c906108c SS |
182 | return NULL; |
183 | ||
c5aa993b | 184 | return (serial_ttystate) state; |
c906108c SS |
185 | } |
186 | ||
187 | static int | |
819cc324 | 188 | hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate) |
c906108c SS |
189 | { |
190 | struct hardwire_ttystate *state; | |
191 | ||
c5aa993b | 192 | state = (struct hardwire_ttystate *) ttystate; |
c906108c | 193 | |
c5aa993b | 194 | return set_tty_state (scb, state); |
c906108c SS |
195 | } |
196 | ||
197 | static int | |
819cc324 | 198 | hardwire_noflush_set_tty_state (struct serial *scb, |
c2c6d25f JM |
199 | serial_ttystate new_ttystate, |
200 | serial_ttystate old_ttystate) | |
c906108c SS |
201 | { |
202 | struct hardwire_ttystate new_state; | |
203 | #ifdef HAVE_SGTTY | |
204 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; | |
205 | #endif | |
206 | ||
c5aa993b | 207 | new_state = *(struct hardwire_ttystate *) new_ttystate; |
c906108c SS |
208 | |
209 | /* Don't change in or out of raw mode; we don't want to flush input. | |
210 | termio and termios have no such restriction; for them flushing input | |
211 | is separate from setting the attributes. */ | |
212 | ||
213 | #ifdef HAVE_SGTTY | |
214 | if (state->sgttyb.sg_flags & RAW) | |
215 | new_state.sgttyb.sg_flags |= RAW; | |
216 | else | |
217 | new_state.sgttyb.sg_flags &= ~RAW; | |
218 | ||
219 | /* I'm not sure whether this is necessary; the manpage just mentions | |
220 | RAW not CBREAK. */ | |
221 | if (state->sgttyb.sg_flags & CBREAK) | |
222 | new_state.sgttyb.sg_flags |= CBREAK; | |
223 | else | |
224 | new_state.sgttyb.sg_flags &= ~CBREAK; | |
225 | #endif | |
226 | ||
227 | return set_tty_state (scb, &new_state); | |
228 | } | |
229 | ||
230 | static void | |
819cc324 | 231 | hardwire_print_tty_state (struct serial *scb, |
c2c6d25f | 232 | serial_ttystate ttystate, |
d9fcf2fb | 233 | struct ui_file *stream) |
c906108c SS |
234 | { |
235 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; | |
236 | int i; | |
237 | ||
238 | #ifdef HAVE_TERMIOS | |
c2c6d25f | 239 | fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", |
2acceee2 JM |
240 | (int) state->termios.c_iflag, |
241 | (int) state->termios.c_oflag); | |
c2c6d25f | 242 | fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n", |
2acceee2 JM |
243 | (int) state->termios.c_cflag, |
244 | (int) state->termios.c_lflag); | |
c906108c SS |
245 | #if 0 |
246 | /* This not in POSIX, and is not really documented by those systems | |
247 | which have it (at least not Sun). */ | |
c2c6d25f | 248 | fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line); |
c906108c | 249 | #endif |
c2c6d25f | 250 | fprintf_filtered (stream, "c_cc: "); |
c906108c | 251 | for (i = 0; i < NCCS; i += 1) |
c2c6d25f JM |
252 | fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]); |
253 | fprintf_filtered (stream, "\n"); | |
c906108c SS |
254 | #endif |
255 | ||
256 | #ifdef HAVE_TERMIO | |
c2c6d25f JM |
257 | fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", |
258 | state->termio.c_iflag, state->termio.c_oflag); | |
259 | fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", | |
260 | state->termio.c_cflag, state->termio.c_lflag, | |
261 | state->termio.c_line); | |
262 | fprintf_filtered (stream, "c_cc: "); | |
c906108c | 263 | for (i = 0; i < NCC; i += 1) |
c2c6d25f JM |
264 | fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]); |
265 | fprintf_filtered (stream, "\n"); | |
c906108c SS |
266 | #endif |
267 | ||
268 | #ifdef HAVE_SGTTY | |
c2c6d25f JM |
269 | fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n", |
270 | state->sgttyb.sg_flags); | |
c906108c | 271 | |
c2c6d25f | 272 | fprintf_filtered (stream, "tchars: "); |
c5aa993b | 273 | for (i = 0; i < (int) sizeof (struct tchars); i++) |
c2c6d25f | 274 | fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]); |
64122a8b | 275 | fprintf_filtered (stream, "\n"); |
c906108c | 276 | |
c2c6d25f | 277 | fprintf_filtered (stream, "ltchars: "); |
c5aa993b | 278 | for (i = 0; i < (int) sizeof (struct ltchars); i++) |
c2c6d25f JM |
279 | fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]); |
280 | fprintf_filtered (stream, "\n"); | |
c906108c | 281 | |
c2c6d25f | 282 | fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode); |
c906108c SS |
283 | #endif |
284 | } | |
285 | ||
286 | /* Wait for the output to drain away, as opposed to flushing (discarding) it */ | |
287 | ||
288 | static int | |
819cc324 | 289 | hardwire_drain_output (struct serial *scb) |
c906108c SS |
290 | { |
291 | #ifdef HAVE_TERMIOS | |
292 | return tcdrain (scb->fd); | |
293 | #endif | |
294 | ||
295 | #ifdef HAVE_TERMIO | |
296 | return ioctl (scb->fd, TCSBRK, 1); | |
297 | #endif | |
298 | ||
299 | #ifdef HAVE_SGTTY | |
300 | /* Get the current state and then restore it using TIOCSETP, | |
301 | which should cause the output to drain and pending input | |
302 | to be discarded. */ | |
303 | { | |
304 | struct hardwire_ttystate state; | |
305 | if (get_tty_state (scb, &state)) | |
306 | { | |
307 | return (-1); | |
308 | } | |
309 | else | |
310 | { | |
311 | return (ioctl (scb->fd, TIOCSETP, &state.sgttyb)); | |
312 | } | |
313 | } | |
c5aa993b | 314 | #endif |
c906108c SS |
315 | } |
316 | ||
317 | static int | |
819cc324 | 318 | hardwire_flush_output (struct serial *scb) |
c906108c SS |
319 | { |
320 | #ifdef HAVE_TERMIOS | |
321 | return tcflush (scb->fd, TCOFLUSH); | |
322 | #endif | |
323 | ||
324 | #ifdef HAVE_TERMIO | |
325 | return ioctl (scb->fd, TCFLSH, 1); | |
326 | #endif | |
327 | ||
328 | #ifdef HAVE_SGTTY | |
329 | /* This flushes both input and output, but we can't do better. */ | |
330 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
c5aa993b | 331 | #endif |
c906108c SS |
332 | } |
333 | ||
334 | static int | |
819cc324 | 335 | hardwire_flush_input (struct serial *scb) |
c906108c | 336 | { |
2acceee2 | 337 | ser_unix_flush_input (scb); |
c906108c SS |
338 | |
339 | #ifdef HAVE_TERMIOS | |
340 | return tcflush (scb->fd, TCIFLUSH); | |
341 | #endif | |
342 | ||
343 | #ifdef HAVE_TERMIO | |
344 | return ioctl (scb->fd, TCFLSH, 0); | |
345 | #endif | |
346 | ||
347 | #ifdef HAVE_SGTTY | |
348 | /* This flushes both input and output, but we can't do better. */ | |
349 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
c5aa993b | 350 | #endif |
c906108c SS |
351 | } |
352 | ||
353 | static int | |
819cc324 | 354 | hardwire_send_break (struct serial *scb) |
c906108c SS |
355 | { |
356 | #ifdef HAVE_TERMIOS | |
357 | return tcsendbreak (scb->fd, 0); | |
358 | #endif | |
359 | ||
360 | #ifdef HAVE_TERMIO | |
361 | return ioctl (scb->fd, TCSBRK, 0); | |
362 | #endif | |
363 | ||
364 | #ifdef HAVE_SGTTY | |
365 | { | |
366 | int status; | |
367 | struct timeval timeout; | |
368 | ||
369 | status = ioctl (scb->fd, TIOCSBRK, 0); | |
370 | ||
371 | /* Can't use usleep; it doesn't exist in BSD 4.2. */ | |
372 | /* Note that if this select() is interrupted by a signal it will not wait | |
373 | the full length of time. I think that is OK. */ | |
374 | timeout.tv_sec = 0; | |
375 | timeout.tv_usec = 250000; | |
376 | select (0, 0, 0, 0, &timeout); | |
377 | status = ioctl (scb->fd, TIOCCBRK, 0); | |
378 | return status; | |
379 | } | |
c5aa993b | 380 | #endif |
c906108c SS |
381 | } |
382 | ||
383 | static void | |
819cc324 | 384 | hardwire_raw (struct serial *scb) |
c906108c SS |
385 | { |
386 | struct hardwire_ttystate state; | |
387 | ||
c5aa993b JM |
388 | if (get_tty_state (scb, &state)) |
389 | fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno)); | |
c906108c SS |
390 | |
391 | #ifdef HAVE_TERMIOS | |
392 | state.termios.c_iflag = 0; | |
393 | state.termios.c_oflag = 0; | |
394 | state.termios.c_lflag = 0; | |
c5aa993b | 395 | state.termios.c_cflag &= ~(CSIZE | PARENB); |
c906108c SS |
396 | state.termios.c_cflag |= CLOCAL | CS8; |
397 | state.termios.c_cc[VMIN] = 0; | |
398 | state.termios.c_cc[VTIME] = 0; | |
399 | #endif | |
400 | ||
401 | #ifdef HAVE_TERMIO | |
402 | state.termio.c_iflag = 0; | |
403 | state.termio.c_oflag = 0; | |
404 | state.termio.c_lflag = 0; | |
c5aa993b | 405 | state.termio.c_cflag &= ~(CSIZE | PARENB); |
c906108c SS |
406 | state.termio.c_cflag |= CLOCAL | CS8; |
407 | state.termio.c_cc[VMIN] = 0; | |
408 | state.termio.c_cc[VTIME] = 0; | |
409 | #endif | |
410 | ||
411 | #ifdef HAVE_SGTTY | |
412 | state.sgttyb.sg_flags |= RAW | ANYP; | |
413 | state.sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
414 | #endif | |
415 | ||
416 | scb->current_timeout = 0; | |
417 | ||
418 | if (set_tty_state (scb, &state)) | |
c5aa993b | 419 | fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno)); |
c906108c SS |
420 | } |
421 | ||
422 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, | |
423 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. | |
424 | ||
425 | For termio{s}, we actually just setup VTIME if necessary, and let the | |
426 | timeout occur in the read() in hardwire_read(). | |
427 | */ | |
428 | ||
2acceee2 JM |
429 | /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent |
430 | ser_unix*() until the old TERMIOS/SGTTY/... timer code has been | |
431 | flushed. . */ | |
432 | ||
433 | /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only | |
434 | possible values of the TIMEOUT parameter are ONE and ZERO. | |
435 | Consequently all the code that tries to handle the possability of | |
436 | an overflowed timer is unnecessary. */ | |
c2c6d25f | 437 | |
c906108c | 438 | static int |
819cc324 | 439 | wait_for (struct serial *scb, int timeout) |
c906108c | 440 | { |
c906108c | 441 | #ifdef HAVE_SGTTY |
ab5ba170 AC |
442 | while (1) |
443 | { | |
444 | struct timeval tv; | |
445 | fd_set readfds; | |
446 | int numfds; | |
c906108c | 447 | |
ab5ba170 AC |
448 | /* NOTE: Some OS's can scramble the READFDS when the select() |
449 | call fails (ex the kernel with Red Hat 5.2). Initialize all | |
450 | arguments before each call. */ | |
c906108c | 451 | |
ab5ba170 AC |
452 | tv.tv_sec = timeout; |
453 | tv.tv_usec = 0; | |
c906108c | 454 | |
ab5ba170 AC |
455 | FD_ZERO (&readfds); |
456 | FD_SET (scb->fd, &readfds); | |
c906108c | 457 | |
ab5ba170 AC |
458 | if (timeout >= 0) |
459 | numfds = select (scb->fd + 1, &readfds, 0, 0, &tv); | |
460 | else | |
461 | numfds = select (scb->fd + 1, &readfds, 0, 0, 0); | |
c906108c | 462 | |
ab5ba170 AC |
463 | if (numfds <= 0) |
464 | if (numfds == 0) | |
465 | return SERIAL_TIMEOUT; | |
466 | else if (errno == EINTR) | |
467 | continue; | |
c906108c | 468 | else |
ab5ba170 | 469 | return SERIAL_ERROR; /* Got an error from select or poll */ |
c906108c | 470 | |
ab5ba170 AC |
471 | return 0; |
472 | } | |
c5aa993b | 473 | #endif /* HAVE_SGTTY */ |
c906108c SS |
474 | |
475 | #if defined HAVE_TERMIO || defined HAVE_TERMIOS | |
476 | if (timeout == scb->current_timeout) | |
477 | return 0; | |
478 | ||
479 | scb->current_timeout = timeout; | |
480 | ||
481 | { | |
482 | struct hardwire_ttystate state; | |
483 | ||
c5aa993b JM |
484 | if (get_tty_state (scb, &state)) |
485 | fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno)); | |
c906108c SS |
486 | |
487 | #ifdef HAVE_TERMIOS | |
488 | if (timeout < 0) | |
489 | { | |
490 | /* No timeout. */ | |
491 | state.termios.c_cc[VTIME] = 0; | |
492 | state.termios.c_cc[VMIN] = 1; | |
493 | } | |
494 | else | |
495 | { | |
496 | state.termios.c_cc[VMIN] = 0; | |
497 | state.termios.c_cc[VTIME] = timeout * 10; | |
498 | if (state.termios.c_cc[VTIME] != timeout * 10) | |
499 | { | |
500 | ||
501 | /* If c_cc is an 8-bit signed character, we can't go | |
502 | bigger than this. If it is always unsigned, we could use | |
503 | 25. */ | |
504 | ||
505 | scb->current_timeout = 12; | |
506 | state.termios.c_cc[VTIME] = scb->current_timeout * 10; | |
507 | scb->timeout_remaining = timeout - scb->current_timeout; | |
508 | } | |
509 | } | |
510 | #endif | |
511 | ||
512 | #ifdef HAVE_TERMIO | |
513 | if (timeout < 0) | |
514 | { | |
515 | /* No timeout. */ | |
516 | state.termio.c_cc[VTIME] = 0; | |
517 | state.termio.c_cc[VMIN] = 1; | |
518 | } | |
519 | else | |
520 | { | |
521 | state.termio.c_cc[VMIN] = 0; | |
522 | state.termio.c_cc[VTIME] = timeout * 10; | |
523 | if (state.termio.c_cc[VTIME] != timeout * 10) | |
524 | { | |
525 | /* If c_cc is an 8-bit signed character, we can't go | |
526 | bigger than this. If it is always unsigned, we could use | |
527 | 25. */ | |
528 | ||
529 | scb->current_timeout = 12; | |
530 | state.termio.c_cc[VTIME] = scb->current_timeout * 10; | |
531 | scb->timeout_remaining = timeout - scb->current_timeout; | |
532 | } | |
533 | } | |
534 | #endif | |
535 | ||
536 | if (set_tty_state (scb, &state)) | |
c5aa993b | 537 | fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno)); |
c906108c SS |
538 | |
539 | return 0; | |
540 | } | |
c5aa993b | 541 | #endif /* HAVE_TERMIO || HAVE_TERMIOS */ |
c906108c SS |
542 | } |
543 | ||
544 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
545 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
546 | char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line | |
547 | dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */ | |
c2c6d25f JM |
548 | |
549 | /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent | |
550 | ser_unix*() until the old TERMIOS/SGTTY/... timer code has been | |
551 | flushed. */ | |
552 | ||
553 | /* NOTE: cagney/1999-09-16: This function is not identical to | |
554 | ser_unix_readchar() as part of replacing it with ser_unix*() | |
555 | merging will be required - this code handles the case where read() | |
556 | times out due to no data while ser_unix_readchar() doesn't expect | |
557 | that. */ | |
558 | ||
c906108c | 559 | static int |
819cc324 | 560 | do_hardwire_readchar (struct serial *scb, int timeout) |
c906108c | 561 | { |
7a292a7a SS |
562 | int status, delta; |
563 | int detach = 0; | |
c906108c | 564 | |
c906108c SS |
565 | if (timeout > 0) |
566 | timeout++; | |
c906108c | 567 | |
2c1ab592 MS |
568 | /* We have to be able to keep the GUI alive here, so we break the |
569 | original timeout into steps of 1 second, running the "keep the | |
570 | GUI alive" hook each time through the loop. | |
571 | ||
572 | Also, timeout = 0 means to poll, so we just set the delta to 0, | |
573 | so we will only go through the loop once. */ | |
c5aa993b | 574 | |
7a292a7a | 575 | delta = (timeout == 0 ? 0 : 1); |
c906108c SS |
576 | while (1) |
577 | { | |
c906108c | 578 | |
7a292a7a SS |
579 | /* N.B. The UI may destroy our world (for instance by calling |
580 | remote_stop,) in which case we want to get out of here as | |
581 | quickly as possible. It is not safe to touch scb, since | |
98bbd631 AC |
582 | someone else might have freed it. The |
583 | deprecated_ui_loop_hook signals that we should exit by | |
584 | returning 1. */ | |
7a292a7a | 585 | |
98bbd631 AC |
586 | if (deprecated_ui_loop_hook) |
587 | detach = deprecated_ui_loop_hook (0); | |
7a292a7a SS |
588 | |
589 | if (detach) | |
590 | return SERIAL_TIMEOUT; | |
591 | ||
592 | scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta); | |
593 | status = wait_for (scb, delta); | |
594 | ||
c906108c SS |
595 | if (status < 0) |
596 | return status; | |
597 | ||
2acceee2 | 598 | status = read (scb->fd, scb->buf, BUFSIZ); |
c906108c | 599 | |
2acceee2 | 600 | if (status <= 0) |
c906108c | 601 | { |
2acceee2 | 602 | if (status == 0) |
c906108c SS |
603 | { |
604 | /* Zero characters means timeout (it could also be EOF, but | |
c5aa993b | 605 | we don't (yet at least) distinguish). */ |
c906108c SS |
606 | if (scb->timeout_remaining > 0) |
607 | { | |
608 | timeout = scb->timeout_remaining; | |
609 | continue; | |
610 | } | |
c5aa993b JM |
611 | else if (scb->timeout_remaining < 0) |
612 | continue; | |
c906108c SS |
613 | else |
614 | return SERIAL_TIMEOUT; | |
615 | } | |
616 | else if (errno == EINTR) | |
617 | continue; | |
618 | else | |
619 | return SERIAL_ERROR; /* Got an error from read */ | |
620 | } | |
621 | ||
2acceee2 | 622 | scb->bufcnt = status; |
c906108c SS |
623 | scb->bufcnt--; |
624 | scb->bufp = scb->buf; | |
625 | return *scb->bufp++; | |
626 | } | |
627 | } | |
628 | ||
2acceee2 | 629 | static int |
819cc324 | 630 | hardwire_readchar (struct serial *scb, int timeout) |
2acceee2 JM |
631 | { |
632 | return generic_readchar (scb, timeout, do_hardwire_readchar); | |
633 | } | |
634 | ||
635 | ||
c906108c SS |
636 | #ifndef B19200 |
637 | #define B19200 EXTA | |
638 | #endif | |
639 | ||
640 | #ifndef B38400 | |
641 | #define B38400 EXTB | |
642 | #endif | |
643 | ||
644 | /* Translate baud rates from integers to damn B_codes. Unix should | |
645 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
646 | ||
647 | static struct | |
648 | { | |
649 | int rate; | |
650 | int code; | |
651 | } | |
652 | baudtab[] = | |
653 | { | |
c5aa993b JM |
654 | { |
655 | 50, B50 | |
656 | } | |
657 | , | |
658 | { | |
659 | 75, B75 | |
660 | } | |
661 | , | |
662 | { | |
663 | 110, B110 | |
664 | } | |
665 | , | |
666 | { | |
667 | 134, B134 | |
668 | } | |
669 | , | |
670 | { | |
671 | 150, B150 | |
672 | } | |
673 | , | |
674 | { | |
675 | 200, B200 | |
676 | } | |
677 | , | |
678 | { | |
679 | 300, B300 | |
680 | } | |
681 | , | |
682 | { | |
683 | 600, B600 | |
684 | } | |
685 | , | |
686 | { | |
687 | 1200, B1200 | |
688 | } | |
689 | , | |
690 | { | |
691 | 1800, B1800 | |
692 | } | |
693 | , | |
694 | { | |
695 | 2400, B2400 | |
696 | } | |
697 | , | |
698 | { | |
699 | 4800, B4800 | |
700 | } | |
701 | , | |
702 | { | |
703 | 9600, B9600 | |
704 | } | |
705 | , | |
706 | { | |
707 | 19200, B19200 | |
708 | } | |
709 | , | |
710 | { | |
711 | 38400, B38400 | |
712 | } | |
713 | , | |
c906108c | 714 | #ifdef B57600 |
c5aa993b JM |
715 | { |
716 | 57600, B57600 | |
717 | } | |
718 | , | |
c906108c SS |
719 | #endif |
720 | #ifdef B115200 | |
c5aa993b JM |
721 | { |
722 | 115200, B115200 | |
723 | } | |
724 | , | |
c906108c SS |
725 | #endif |
726 | #ifdef B230400 | |
c5aa993b JM |
727 | { |
728 | 230400, B230400 | |
729 | } | |
730 | , | |
c906108c SS |
731 | #endif |
732 | #ifdef B460800 | |
c5aa993b JM |
733 | { |
734 | 460800, B460800 | |
735 | } | |
736 | , | |
c906108c | 737 | #endif |
c5aa993b JM |
738 | { |
739 | -1, -1 | |
740 | } | |
741 | , | |
c906108c SS |
742 | }; |
743 | ||
c5aa993b | 744 | static int |
c2c6d25f | 745 | rate_to_code (int rate) |
c906108c SS |
746 | { |
747 | int i; | |
748 | ||
749 | for (i = 0; baudtab[i].rate != -1; i++) | |
08b4f080 FN |
750 | { |
751 | /* test for perfect macth. */ | |
752 | if (rate == baudtab[i].rate) | |
753 | return baudtab[i].code; | |
754 | else | |
755 | { | |
756 | /* check if it is in between valid values. */ | |
757 | if (rate < baudtab[i].rate) | |
758 | { | |
759 | if (i) | |
760 | { | |
8a3fe4f8 | 761 | warning (_("Invalid baud rate %d. Closest values are %d and %d."), |
08b4f080 FN |
762 | rate, baudtab[i - 1].rate, baudtab[i].rate); |
763 | } | |
764 | else | |
765 | { | |
8a3fe4f8 | 766 | warning (_("Invalid baud rate %d. Minimum value is %d."), |
08b4f080 FN |
767 | rate, baudtab[0].rate); |
768 | } | |
769 | return -1; | |
770 | } | |
771 | } | |
772 | } | |
773 | ||
774 | /* The requested speed was too large. */ | |
8a3fe4f8 | 775 | warning (_("Invalid baud rate %d. Maximum value is %d."), |
08b4f080 | 776 | rate, baudtab[i - 1].rate); |
c906108c SS |
777 | return -1; |
778 | } | |
779 | ||
780 | static int | |
819cc324 | 781 | hardwire_setbaudrate (struct serial *scb, int rate) |
c906108c SS |
782 | { |
783 | struct hardwire_ttystate state; | |
08b4f080 FN |
784 | int baud_code = rate_to_code (rate); |
785 | ||
786 | if (baud_code < 0) | |
787 | { | |
788 | /* The baud rate was not valid. | |
789 | A warning has already been issued. */ | |
790 | errno = EINVAL; | |
791 | return -1; | |
792 | } | |
c906108c | 793 | |
c5aa993b | 794 | if (get_tty_state (scb, &state)) |
c906108c SS |
795 | return -1; |
796 | ||
797 | #ifdef HAVE_TERMIOS | |
08b4f080 FN |
798 | cfsetospeed (&state.termios, baud_code); |
799 | cfsetispeed (&state.termios, baud_code); | |
c906108c SS |
800 | #endif |
801 | ||
802 | #ifdef HAVE_TERMIO | |
803 | #ifndef CIBAUD | |
804 | #define CIBAUD CBAUD | |
805 | #endif | |
806 | ||
807 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); | |
08b4f080 | 808 | state.termio.c_cflag |= baud_code; |
c906108c SS |
809 | #endif |
810 | ||
811 | #ifdef HAVE_SGTTY | |
08b4f080 FN |
812 | state.sgttyb.sg_ispeed = baud_code; |
813 | state.sgttyb.sg_ospeed = baud_code; | |
c906108c SS |
814 | #endif |
815 | ||
816 | return set_tty_state (scb, &state); | |
817 | } | |
818 | ||
819 | static int | |
819cc324 | 820 | hardwire_setstopbits (struct serial *scb, int num) |
c906108c SS |
821 | { |
822 | struct hardwire_ttystate state; | |
823 | int newbit; | |
824 | ||
c5aa993b | 825 | if (get_tty_state (scb, &state)) |
c906108c SS |
826 | return -1; |
827 | ||
828 | switch (num) | |
829 | { | |
830 | case SERIAL_1_STOPBITS: | |
831 | newbit = 0; | |
832 | break; | |
833 | case SERIAL_1_AND_A_HALF_STOPBITS: | |
834 | case SERIAL_2_STOPBITS: | |
835 | newbit = 1; | |
836 | break; | |
837 | default: | |
838 | return 1; | |
839 | } | |
840 | ||
841 | #ifdef HAVE_TERMIOS | |
842 | if (!newbit) | |
843 | state.termios.c_cflag &= ~CSTOPB; | |
844 | else | |
c5aa993b | 845 | state.termios.c_cflag |= CSTOPB; /* two bits */ |
c906108c SS |
846 | #endif |
847 | ||
848 | #ifdef HAVE_TERMIO | |
849 | if (!newbit) | |
850 | state.termio.c_cflag &= ~CSTOPB; | |
851 | else | |
c5aa993b | 852 | state.termio.c_cflag |= CSTOPB; /* two bits */ |
c906108c SS |
853 | #endif |
854 | ||
855 | #ifdef HAVE_SGTTY | |
856 | return 0; /* sgtty doesn't support this */ | |
857 | #endif | |
858 | ||
859 | return set_tty_state (scb, &state); | |
860 | } | |
861 | ||
c906108c | 862 | static void |
819cc324 | 863 | hardwire_close (struct serial *scb) |
c906108c SS |
864 | { |
865 | if (scb->fd < 0) | |
866 | return; | |
867 | ||
c5aa993b | 868 | close (scb->fd); |
c906108c SS |
869 | scb->fd = -1; |
870 | } | |
871 | ||
c2c6d25f JM |
872 | \f |
873 | /* Generic operations used by all UNIX/FD based serial interfaces. */ | |
874 | ||
875 | serial_ttystate | |
819cc324 | 876 | ser_unix_nop_get_tty_state (struct serial *scb) |
c2c6d25f JM |
877 | { |
878 | /* allocate a dummy */ | |
879 | return (serial_ttystate) XMALLOC (int); | |
880 | } | |
881 | ||
882 | int | |
819cc324 | 883 | ser_unix_nop_set_tty_state (struct serial *scb, serial_ttystate ttystate) |
c2c6d25f JM |
884 | { |
885 | return 0; | |
886 | } | |
887 | ||
888 | void | |
819cc324 | 889 | ser_unix_nop_raw (struct serial *scb) |
c2c6d25f JM |
890 | { |
891 | return; /* Always in raw mode */ | |
892 | } | |
893 | ||
894 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, | |
895 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */ | |
896 | ||
897 | int | |
819cc324 | 898 | ser_unix_wait_for (struct serial *scb, int timeout) |
c2c6d25f | 899 | { |
ab5ba170 AC |
900 | while (1) |
901 | { | |
902 | int numfds; | |
903 | struct timeval tv; | |
904 | fd_set readfds, exceptfds; | |
c2c6d25f | 905 | |
ab5ba170 AC |
906 | /* NOTE: Some OS's can scramble the READFDS when the select() |
907 | call fails (ex the kernel with Red Hat 5.2). Initialize all | |
908 | arguments before each call. */ | |
c2c6d25f | 909 | |
ab5ba170 AC |
910 | tv.tv_sec = timeout; |
911 | tv.tv_usec = 0; | |
c2c6d25f | 912 | |
ab5ba170 AC |
913 | FD_ZERO (&readfds); |
914 | FD_ZERO (&exceptfds); | |
915 | FD_SET (scb->fd, &readfds); | |
916 | FD_SET (scb->fd, &exceptfds); | |
c2c6d25f | 917 | |
c2c6d25f JM |
918 | if (timeout >= 0) |
919 | numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv); | |
920 | else | |
921 | numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0); | |
922 | ||
923 | if (numfds <= 0) | |
924 | { | |
925 | if (numfds == 0) | |
926 | return SERIAL_TIMEOUT; | |
927 | else if (errno == EINTR) | |
928 | continue; | |
929 | else | |
930 | return SERIAL_ERROR; /* Got an error from select or poll */ | |
931 | } | |
932 | ||
933 | return 0; | |
934 | } | |
935 | } | |
936 | ||
937 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
938 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
939 | char if successful. Returns -2 if timeout expired, EOF if line dropped | |
940 | dead, or -3 for any other error (see errno in that case). */ | |
941 | ||
2acceee2 | 942 | static int |
819cc324 | 943 | do_unix_readchar (struct serial *scb, int timeout) |
c2c6d25f JM |
944 | { |
945 | int status; | |
946 | int delta; | |
947 | ||
2c1ab592 MS |
948 | /* We have to be able to keep the GUI alive here, so we break the |
949 | original timeout into steps of 1 second, running the "keep the | |
950 | GUI alive" hook each time through the loop. | |
c2c6d25f | 951 | |
2c1ab592 MS |
952 | Also, timeout = 0 means to poll, so we just set the delta to 0, |
953 | so we will only go through the loop once. */ | |
c2c6d25f | 954 | |
9e294fb8 | 955 | delta = (timeout == 0 ? 0 : 1); |
c2c6d25f JM |
956 | while (1) |
957 | { | |
958 | ||
959 | /* N.B. The UI may destroy our world (for instance by calling | |
960 | remote_stop,) in which case we want to get out of here as | |
961 | quickly as possible. It is not safe to touch scb, since | |
98bbd631 AC |
962 | someone else might have freed it. The |
963 | deprecated_ui_loop_hook signals that we should exit by | |
964 | returning 1. */ | |
c2c6d25f | 965 | |
98bbd631 | 966 | if (deprecated_ui_loop_hook) |
c2c6d25f | 967 | { |
98bbd631 | 968 | if (deprecated_ui_loop_hook (0)) |
c2c6d25f JM |
969 | return SERIAL_TIMEOUT; |
970 | } | |
971 | ||
9e294fb8 | 972 | status = ser_unix_wait_for (scb, delta); |
faa5effd FN |
973 | if (timeout > 0) |
974 | timeout -= delta; | |
c2c6d25f | 975 | |
9e294fb8 AC |
976 | /* If we got a character or an error back from wait_for, then we can |
977 | break from the loop before the timeout is completed. */ | |
c2c6d25f | 978 | |
9e294fb8 AC |
979 | if (status != SERIAL_TIMEOUT) |
980 | { | |
981 | break; | |
982 | } | |
c2c6d25f | 983 | |
9e294fb8 AC |
984 | /* If we have exhausted the original timeout, then generate |
985 | a SERIAL_TIMEOUT, and pass it out of the loop. */ | |
c2c6d25f | 986 | |
9e294fb8 AC |
987 | else if (timeout == 0) |
988 | { | |
989 | status = SERIAL_TIMEOUT; | |
990 | break; | |
c2c6d25f | 991 | } |
9e294fb8 | 992 | } |
c2c6d25f | 993 | |
9e294fb8 AC |
994 | if (status < 0) |
995 | return status; | |
996 | ||
997 | while (1) | |
998 | { | |
999 | status = read (scb->fd, scb->buf, BUFSIZ); | |
1000 | if (status != -1 || errno != EINTR) | |
1001 | break; | |
1002 | } | |
1003 | ||
1004 | if (status <= 0) | |
1005 | { | |
1006 | if (status == 0) | |
1007 | return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to | |
1008 | distinguish between EOF & timeouts | |
1009 | someday] */ | |
1010 | else | |
1011 | return SERIAL_ERROR; /* Got an error from read */ | |
c2c6d25f | 1012 | } |
9e294fb8 AC |
1013 | |
1014 | scb->bufcnt = status; | |
1015 | scb->bufcnt--; | |
1016 | scb->bufp = scb->buf; | |
1017 | return *scb->bufp++; | |
c2c6d25f JM |
1018 | } |
1019 | ||
2acceee2 JM |
1020 | /* Perform operations common to both old and new readchar. */ |
1021 | ||
1022 | /* Return the next character from the input FIFO. If the FIFO is | |
1023 | empty, call the SERIAL specific routine to try and read in more | |
1024 | characters. | |
1025 | ||
1026 | Initially data from the input FIFO is returned (fd_event() | |
1027 | pre-reads the input into that FIFO. Once that has been emptied, | |
1028 | further data is obtained by polling the input FD using the device | |
1029 | specific readchar() function. Note: reschedule() is called after | |
1030 | every read. This is because there is no guarentee that the lower | |
1031 | level fd_event() poll_event() code (which also calls reschedule()) | |
1032 | will be called. */ | |
1033 | ||
1034 | static int | |
819cc324 AC |
1035 | generic_readchar (struct serial *scb, int timeout, |
1036 | int (do_readchar) (struct serial *scb, int timeout)) | |
2acceee2 JM |
1037 | { |
1038 | int ch; | |
1039 | if (scb->bufcnt > 0) | |
1040 | { | |
1041 | ch = *scb->bufp; | |
1042 | scb->bufcnt--; | |
1043 | scb->bufp++; | |
1044 | } | |
1045 | else if (scb->bufcnt < 0) | |
1046 | { | |
1047 | /* Some errors/eof are are sticky. */ | |
1048 | ch = scb->bufcnt; | |
1049 | } | |
1050 | else | |
1051 | { | |
1052 | ch = do_readchar (scb, timeout); | |
1053 | if (ch < 0) | |
1054 | { | |
1055 | switch ((enum serial_rc) ch) | |
1056 | { | |
1057 | case SERIAL_EOF: | |
1058 | case SERIAL_ERROR: | |
1059 | /* Make the error/eof stick. */ | |
1060 | scb->bufcnt = ch; | |
1061 | break; | |
1062 | case SERIAL_TIMEOUT: | |
1063 | scb->bufcnt = 0; | |
1064 | break; | |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | reschedule (scb); | |
1069 | return ch; | |
1070 | } | |
1071 | ||
1072 | int | |
819cc324 | 1073 | ser_unix_readchar (struct serial *scb, int timeout) |
2acceee2 JM |
1074 | { |
1075 | return generic_readchar (scb, timeout, do_unix_readchar); | |
1076 | } | |
1077 | ||
c2c6d25f | 1078 | int |
819cc324 | 1079 | ser_unix_nop_noflush_set_tty_state (struct serial *scb, |
c2c6d25f JM |
1080 | serial_ttystate new_ttystate, |
1081 | serial_ttystate old_ttystate) | |
1082 | { | |
1083 | return 0; | |
1084 | } | |
1085 | ||
1086 | void | |
819cc324 | 1087 | ser_unix_nop_print_tty_state (struct serial *scb, |
c2c6d25f | 1088 | serial_ttystate ttystate, |
d9fcf2fb | 1089 | struct ui_file *stream) |
c2c6d25f JM |
1090 | { |
1091 | /* Nothing to print. */ | |
1092 | return; | |
1093 | } | |
1094 | ||
1095 | int | |
819cc324 | 1096 | ser_unix_nop_setbaudrate (struct serial *scb, int rate) |
c2c6d25f JM |
1097 | { |
1098 | return 0; /* Never fails! */ | |
1099 | } | |
1100 | ||
1101 | int | |
819cc324 | 1102 | ser_unix_nop_setstopbits (struct serial *scb, int num) |
c2c6d25f JM |
1103 | { |
1104 | return 0; /* Never fails! */ | |
1105 | } | |
1106 | ||
1107 | int | |
819cc324 | 1108 | ser_unix_write (struct serial *scb, const char *str, int len) |
c2c6d25f JM |
1109 | { |
1110 | int cc; | |
1111 | ||
1112 | while (len > 0) | |
1113 | { | |
1114 | cc = write (scb->fd, str, len); | |
1115 | ||
1116 | if (cc < 0) | |
1117 | return 1; | |
1118 | len -= cc; | |
1119 | str += cc; | |
1120 | } | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | int | |
819cc324 | 1125 | ser_unix_nop_flush_output (struct serial *scb) |
c2c6d25f JM |
1126 | { |
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | int | |
819cc324 | 1131 | ser_unix_flush_input (struct serial *scb) |
c2c6d25f | 1132 | { |
2acceee2 JM |
1133 | if (scb->bufcnt >= 0) |
1134 | { | |
1135 | scb->bufcnt = 0; | |
1136 | scb->bufp = scb->buf; | |
1137 | return 0; | |
1138 | } | |
1139 | else | |
1140 | return SERIAL_ERROR; | |
c2c6d25f JM |
1141 | } |
1142 | ||
1143 | int | |
819cc324 | 1144 | ser_unix_nop_send_break (struct serial *scb) |
c2c6d25f JM |
1145 | { |
1146 | return 0; | |
1147 | } | |
1148 | ||
1149 | int | |
819cc324 | 1150 | ser_unix_nop_drain_output (struct serial *scb) |
c2c6d25f JM |
1151 | { |
1152 | return 0; | |
1153 | } | |
1154 | ||
2acceee2 JM |
1155 | |
1156 | \f | |
1157 | /* Event handling for ASYNC serial code. | |
1158 | ||
1159 | At any time the SERIAL device either: has an empty FIFO and is | |
1160 | waiting on a FD event; or has a non-empty FIFO/error condition and | |
1161 | is constantly scheduling timer events. | |
1162 | ||
1163 | ASYNC only stops pestering its client when it is de-async'ed or it | |
1164 | is told to go away. */ | |
1165 | ||
1166 | /* Value of scb->async_state: */ | |
1167 | enum { | |
1168 | /* >= 0 (TIMER_SCHEDULED) */ | |
1169 | /* The ID of the currently scheduled timer event. This state is | |
1170 | rarely encountered. Timer events are one-off so as soon as the | |
1171 | event is delivered the state is shanged to NOTHING_SCHEDULED. */ | |
1172 | FD_SCHEDULED = -1, | |
1173 | /* The fd_event() handler is scheduled. It is called when ever the | |
1174 | file descriptor becomes ready. */ | |
1175 | NOTHING_SCHEDULED = -2 | |
1176 | /* Either no task is scheduled (just going into ASYNC mode) or a | |
1177 | timer event has just gone off and the current state has been | |
1178 | forced into nothing scheduled. */ | |
1179 | }; | |
1180 | ||
1181 | /* Identify and schedule the next ASYNC task based on scb->async_state | |
1182 | and scb->buf* (the input FIFO). A state machine is used to avoid | |
1183 | the need to make redundant calls into the event-loop - the next | |
1184 | scheduled task is only changed when needed. */ | |
1185 | ||
c2c6d25f | 1186 | static void |
819cc324 | 1187 | reschedule (struct serial *scb) |
2acceee2 | 1188 | { |
2cd58942 | 1189 | if (serial_is_async_p (scb)) |
2acceee2 JM |
1190 | { |
1191 | int next_state; | |
1192 | switch (scb->async_state) | |
1193 | { | |
1194 | case FD_SCHEDULED: | |
1195 | if (scb->bufcnt == 0) | |
1196 | next_state = FD_SCHEDULED; | |
1197 | else | |
1198 | { | |
1199 | delete_file_handler (scb->fd); | |
1200 | next_state = create_timer (0, push_event, scb); | |
1201 | } | |
1202 | break; | |
1203 | case NOTHING_SCHEDULED: | |
1204 | if (scb->bufcnt == 0) | |
1205 | { | |
1206 | add_file_handler (scb->fd, fd_event, scb); | |
1207 | next_state = FD_SCHEDULED; | |
1208 | } | |
1209 | else | |
1210 | { | |
1211 | next_state = create_timer (0, push_event, scb); | |
1212 | } | |
1213 | break; | |
1214 | default: /* TIMER SCHEDULED */ | |
1215 | if (scb->bufcnt == 0) | |
1216 | { | |
1217 | delete_timer (scb->async_state); | |
1218 | add_file_handler (scb->fd, fd_event, scb); | |
1219 | next_state = FD_SCHEDULED; | |
1220 | } | |
1221 | else | |
1222 | next_state = scb->async_state; | |
1223 | break; | |
1224 | } | |
2cd58942 | 1225 | if (serial_debug_p (scb)) |
2acceee2 JM |
1226 | { |
1227 | switch (next_state) | |
1228 | { | |
1229 | case FD_SCHEDULED: | |
1230 | if (scb->async_state != FD_SCHEDULED) | |
1231 | fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n", | |
1232 | scb->fd); | |
1233 | break; | |
1234 | default: /* TIMER SCHEDULED */ | |
1235 | if (scb->async_state == FD_SCHEDULED) | |
1236 | fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n", | |
1237 | scb->fd); | |
1238 | break; | |
1239 | } | |
1240 | } | |
1241 | scb->async_state = next_state; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | /* FD_EVENT: This is scheduled when the input FIFO is empty (and there | |
1246 | is no pending error). As soon as data arrives, it is read into the | |
1247 | input FIFO and the client notified. The client should then drain | |
1248 | the FIFO using readchar(). If the FIFO isn't immediatly emptied, | |
1249 | push_event() is used to nag the client until it is. */ | |
1250 | ||
1251 | static void | |
1252 | fd_event (int error, void *context) | |
1253 | { | |
819cc324 | 1254 | struct serial *scb = context; |
2acceee2 JM |
1255 | if (error != 0) |
1256 | { | |
1257 | scb->bufcnt = SERIAL_ERROR; | |
1258 | } | |
1259 | else if (scb->bufcnt == 0) | |
1260 | { | |
1261 | /* Prime the input FIFO. The readchar() function is used to | |
1262 | pull characters out of the buffer. See also | |
1263 | generic_readchar(). */ | |
1264 | int nr; | |
1265 | do | |
1266 | { | |
1267 | nr = read (scb->fd, scb->buf, BUFSIZ); | |
1268 | } | |
1269 | while (nr == -1 && errno == EINTR); | |
1270 | if (nr == 0) | |
1271 | { | |
1272 | scb->bufcnt = SERIAL_EOF; | |
1273 | } | |
1274 | else if (nr > 0) | |
1275 | { | |
1276 | scb->bufcnt = nr; | |
1277 | scb->bufp = scb->buf; | |
1278 | } | |
1279 | else | |
1280 | { | |
1281 | scb->bufcnt = SERIAL_ERROR; | |
1282 | } | |
1283 | } | |
1284 | scb->async_handler (scb, scb->async_context); | |
1285 | reschedule (scb); | |
1286 | } | |
1287 | ||
1288 | /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending | |
1289 | error). Nag the client until all the data has been read. In the | |
1290 | case of errors, the client will need to close or de-async the | |
1291 | device before naging stops. */ | |
1292 | ||
1293 | static void | |
1294 | push_event (void *context) | |
c2c6d25f | 1295 | { |
819cc324 | 1296 | struct serial *scb = context; |
2acceee2 JM |
1297 | scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */ |
1298 | scb->async_handler (scb, scb->async_context); | |
1299 | /* re-schedule */ | |
1300 | reschedule (scb); | |
c2c6d25f JM |
1301 | } |
1302 | ||
2acceee2 JM |
1303 | /* Put the SERIAL device into/out-of ASYNC mode. */ |
1304 | ||
c2c6d25f | 1305 | void |
819cc324 | 1306 | ser_unix_async (struct serial *scb, |
c2c6d25f JM |
1307 | int async_p) |
1308 | { | |
1309 | if (async_p) | |
1310 | { | |
2acceee2 JM |
1311 | /* Force a re-schedule. */ |
1312 | scb->async_state = NOTHING_SCHEDULED; | |
2cd58942 | 1313 | if (serial_debug_p (scb)) |
2acceee2 JM |
1314 | fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n", |
1315 | scb->fd); | |
1316 | reschedule (scb); | |
c2c6d25f JM |
1317 | } |
1318 | else | |
1319 | { | |
2cd58942 | 1320 | if (serial_debug_p (scb)) |
2acceee2 JM |
1321 | fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n", |
1322 | scb->fd); | |
8e1a459b | 1323 | /* De-schedule whatever tasks are currently scheduled. */ |
2acceee2 JM |
1324 | switch (scb->async_state) |
1325 | { | |
1326 | case FD_SCHEDULED: | |
1327 | delete_file_handler (scb->fd); | |
1328 | break; | |
54f1137d | 1329 | case NOTHING_SCHEDULED: |
2acceee2 JM |
1330 | break; |
1331 | default: /* TIMER SCHEDULED */ | |
1332 | delete_timer (scb->async_state); | |
1333 | break; | |
1334 | } | |
c2c6d25f JM |
1335 | } |
1336 | } | |
c906108c SS |
1337 | |
1338 | void | |
c2c6d25f | 1339 | _initialize_ser_hardwire (void) |
c906108c | 1340 | { |
c2c6d25f | 1341 | struct serial_ops *ops = XMALLOC (struct serial_ops); |
2fdbdd39 | 1342 | memset (ops, 0, sizeof (struct serial_ops)); |
c2c6d25f JM |
1343 | ops->name = "hardwire"; |
1344 | ops->next = 0; | |
1345 | ops->open = hardwire_open; | |
1346 | ops->close = hardwire_close; | |
1347 | /* FIXME: Don't replace this with the equivalent ser_unix*() until | |
1348 | the old TERMIOS/SGTTY/... timer code has been flushed. cagney | |
1349 | 1999-09-16. */ | |
1350 | ops->readchar = hardwire_readchar; | |
2acceee2 | 1351 | ops->write = ser_unix_write; |
c2c6d25f JM |
1352 | ops->flush_output = hardwire_flush_output; |
1353 | ops->flush_input = hardwire_flush_input; | |
1354 | ops->send_break = hardwire_send_break; | |
1355 | ops->go_raw = hardwire_raw; | |
1356 | ops->get_tty_state = hardwire_get_tty_state; | |
1357 | ops->set_tty_state = hardwire_set_tty_state; | |
1358 | ops->print_tty_state = hardwire_print_tty_state; | |
1359 | ops->noflush_set_tty_state = hardwire_noflush_set_tty_state; | |
1360 | ops->setbaudrate = hardwire_setbaudrate; | |
1361 | ops->setstopbits = hardwire_setstopbits; | |
1362 | ops->drain_output = hardwire_drain_output; | |
1363 | ops->async = ser_unix_async; | |
1364 | serial_add_interface (ops); | |
c906108c | 1365 | } |