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