1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include "gdbsupport/gdb_sys_time.h"
31 #include "gdbsupport/gdb_select.h"
33 #include "gdbsupport/filestuff.h"
35 #include "gdbsupport/scoped_ignore_sigttou.h"
37 struct hardwire_ttystate
39 struct termios termios
;
43 /* Boolean to explicitly enable or disable h/w flow control. */
44 static bool serial_hwflow
;
46 show_serial_hwflow (struct ui_file
*file
, int from_tty
,
47 struct cmd_list_element
*c
, const char *value
)
49 gdb_printf (file
, _("Hardware flow control is %s.\n"), value
);
53 static int hardwire_open (struct serial
*scb
, const char *name
);
54 static void hardwire_raw (struct serial
*scb
);
55 static int rate_to_code (int rate
);
56 static int hardwire_setbaudrate (struct serial
*scb
, int rate
);
57 static int hardwire_setparity (struct serial
*scb
, int parity
);
58 static void hardwire_close (struct serial
*scb
);
59 static int get_tty_state (struct serial
*scb
,
60 struct hardwire_ttystate
* state
);
61 static int set_tty_state (struct serial
*scb
,
62 struct hardwire_ttystate
* state
);
63 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
64 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
65 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
67 static int hardwire_drain_output (struct serial
*);
68 static int hardwire_flush_output (struct serial
*);
69 static int hardwire_flush_input (struct serial
*);
70 static int hardwire_send_break (struct serial
*);
71 static int hardwire_setstopbits (struct serial
*, int);
73 /* Open up a real live device for serial I/O. */
76 hardwire_open (struct serial
*scb
, const char *name
)
78 scb
->fd
= gdb_open_cloexec (name
, O_RDWR
, 0).release ();
86 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
88 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
95 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
97 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
103 static serial_ttystate
104 hardwire_get_tty_state (struct serial
*scb
)
106 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
108 if (get_tty_state (scb
, state
))
114 return (serial_ttystate
) state
;
117 static serial_ttystate
118 hardwire_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
120 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
122 *state
= *(struct hardwire_ttystate
*) ttystate
;
124 return (serial_ttystate
) state
;
128 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
130 struct hardwire_ttystate
*state
;
132 state
= (struct hardwire_ttystate
*) ttystate
;
134 return set_tty_state (scb
, state
);
138 hardwire_print_tty_state (struct serial
*scb
,
139 serial_ttystate ttystate
,
140 struct ui_file
*stream
)
142 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
145 gdb_printf (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
146 (int) state
->termios
.c_iflag
,
147 (int) state
->termios
.c_oflag
);
148 gdb_printf (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
149 (int) state
->termios
.c_cflag
,
150 (int) state
->termios
.c_lflag
);
152 /* This not in POSIX, and is not really documented by those systems
153 which have it (at least not Sun). */
154 gdb_printf (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
156 gdb_printf (stream
, "c_cc: ");
157 for (i
= 0; i
< NCCS
; i
+= 1)
158 gdb_printf (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
159 gdb_printf (stream
, "\n");
162 /* Wait for the output to drain away, as opposed to flushing
166 hardwire_drain_output (struct serial
*scb
)
168 /* Ignore SIGTTOU which may occur during the drain. */
169 scoped_ignore_sigttou ignore_sigttou
;
171 return tcdrain (scb
->fd
);
175 hardwire_flush_output (struct serial
*scb
)
177 return tcflush (scb
->fd
, TCOFLUSH
);
181 hardwire_flush_input (struct serial
*scb
)
183 ser_base_flush_input (scb
);
185 return tcflush (scb
->fd
, TCIFLUSH
);
189 hardwire_send_break (struct serial
*scb
)
191 return tcsendbreak (scb
->fd
, 0);
195 hardwire_raw (struct serial
*scb
)
197 struct hardwire_ttystate state
;
199 if (get_tty_state (scb
, &state
))
200 gdb_printf (gdb_stderr
, "get_tty_state failed: %s\n",
201 safe_strerror (errno
));
203 state
.termios
.c_iflag
= 0;
204 state
.termios
.c_oflag
= 0;
205 state
.termios
.c_lflag
= 0;
206 state
.termios
.c_cflag
&= ~CSIZE
;
207 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
209 /* h/w flow control. */
211 state
.termios
.c_cflag
|= CRTSCTS
;
213 state
.termios
.c_cflag
&= ~CRTSCTS
;
216 state
.termios
.c_cflag
|= CRTS_IFLOW
;
218 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
221 state
.termios
.c_cc
[VMIN
] = 0;
222 state
.termios
.c_cc
[VTIME
] = 0;
224 if (set_tty_state (scb
, &state
))
225 gdb_printf (gdb_stderr
, "set_tty_state failed: %s\n",
226 safe_strerror (errno
));
237 /* Translate baud rates from integers to damn B_codes. Unix should
238 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
338 rate_to_code (int rate
)
342 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
344 /* test for perfect macth. */
345 if (rate
== baudtab
[i
].rate
)
346 return baudtab
[i
].code
;
349 /* check if it is in between valid values. */
350 if (rate
< baudtab
[i
].rate
)
354 warning (_("Invalid baud rate %d. "
355 "Closest values are %d and %d."),
356 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
360 warning (_("Invalid baud rate %d. Minimum value is %d."),
361 rate
, baudtab
[0].rate
);
368 /* The requested speed was too large. */
369 warning (_("Invalid baud rate %d. Maximum value is %d."),
370 rate
, baudtab
[i
- 1].rate
);
375 hardwire_setbaudrate (struct serial
*scb
, int rate
)
377 struct hardwire_ttystate state
;
378 int baud_code
= rate_to_code (rate
);
382 /* The baud rate was not valid.
383 A warning has already been issued. */
388 if (get_tty_state (scb
, &state
))
391 cfsetospeed (&state
.termios
, baud_code
);
392 cfsetispeed (&state
.termios
, baud_code
);
394 return set_tty_state (scb
, &state
);
398 hardwire_setstopbits (struct serial
*scb
, int num
)
400 struct hardwire_ttystate state
;
403 if (get_tty_state (scb
, &state
))
408 case SERIAL_1_STOPBITS
:
411 case SERIAL_1_AND_A_HALF_STOPBITS
:
412 case SERIAL_2_STOPBITS
:
420 state
.termios
.c_cflag
&= ~CSTOPB
;
422 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
424 return set_tty_state (scb
, &state
);
427 /* Implement the "setparity" serial_ops callback. */
430 hardwire_setparity (struct serial
*scb
, int parity
)
432 struct hardwire_ttystate state
;
435 if (get_tty_state (scb
, &state
))
444 newparity
= PARENB
| PARODD
;
450 internal_warning ("Incorrect parity value: %d", parity
);
454 state
.termios
.c_cflag
&= ~(PARENB
| PARODD
);
455 state
.termios
.c_cflag
|= newparity
;
457 return set_tty_state (scb
, &state
);
462 hardwire_close (struct serial
*scb
)
473 /* The hardwire ops. */
475 static const struct serial_ops hardwire_ops
=
483 hardwire_flush_output
,
484 hardwire_flush_input
,
487 hardwire_get_tty_state
,
488 hardwire_copy_tty_state
,
489 hardwire_set_tty_state
,
490 hardwire_print_tty_state
,
491 hardwire_setbaudrate
,
492 hardwire_setstopbits
,
494 hardwire_drain_output
,
500 void _initialize_ser_hardwire ();
502 _initialize_ser_hardwire ()
504 serial_add_interface (&hardwire_ops
);
507 add_setshow_boolean_cmd ("remoteflow", no_class
,
509 Set use of hardware flow control for remote serial I/O."), _("\
510 Show use of hardware flow control for remote serial I/O."), _("\
511 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
512 when debugging using remote targets."),
515 &setlist
, &showlist
);
520 ser_unix_read_prim (struct serial
*scb
, size_t count
)
522 return read (scb
->fd
, scb
->buf
, count
);
526 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
528 return write (scb
->fd
, buf
, len
);