1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2018 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 "gdb_sys_time.h"
31 #include "gdb_select.h"
33 #include "filestuff.h"
36 struct hardwire_ttystate
38 struct termios termios
;
42 /* Boolean to explicitly enable or disable h/w flow control. */
43 static int serial_hwflow
;
45 show_serial_hwflow (struct ui_file
*file
, int from_tty
,
46 struct cmd_list_element
*c
, const char *value
)
48 fprintf_filtered (file
, _("Hardware flow control is %s.\n"), value
);
52 static int hardwire_open (struct serial
*scb
, const char *name
);
53 static void hardwire_raw (struct serial
*scb
);
54 static int rate_to_code (int rate
);
55 static int hardwire_setbaudrate (struct serial
*scb
, int rate
);
56 static int hardwire_setparity (struct serial
*scb
, int parity
);
57 static void hardwire_close (struct serial
*scb
);
58 static int get_tty_state (struct serial
*scb
,
59 struct hardwire_ttystate
* state
);
60 static int set_tty_state (struct serial
*scb
,
61 struct hardwire_ttystate
* state
);
62 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
63 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
64 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
66 static int hardwire_drain_output (struct serial
*);
67 static int hardwire_flush_output (struct serial
*);
68 static int hardwire_flush_input (struct serial
*);
69 static int hardwire_send_break (struct serial
*);
70 static int hardwire_setstopbits (struct serial
*, int);
72 /* Open up a real live device for serial I/O. */
75 hardwire_open (struct serial
*scb
, const char *name
)
77 scb
->fd
= gdb_open_cloexec (name
, O_RDWR
, 0);
85 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
87 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
94 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
96 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
102 static serial_ttystate
103 hardwire_get_tty_state (struct serial
*scb
)
105 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
107 if (get_tty_state (scb
, state
))
113 return (serial_ttystate
) state
;
116 static serial_ttystate
117 hardwire_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
119 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
121 *state
= *(struct hardwire_ttystate
*) ttystate
;
123 return (serial_ttystate
) state
;
127 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
129 struct hardwire_ttystate
*state
;
131 state
= (struct hardwire_ttystate
*) ttystate
;
133 return set_tty_state (scb
, state
);
137 hardwire_print_tty_state (struct serial
*scb
,
138 serial_ttystate ttystate
,
139 struct ui_file
*stream
)
141 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
144 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
145 (int) state
->termios
.c_iflag
,
146 (int) state
->termios
.c_oflag
);
147 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
148 (int) state
->termios
.c_cflag
,
149 (int) state
->termios
.c_lflag
);
151 /* This not in POSIX, and is not really documented by those systems
152 which have it (at least not Sun). */
153 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
155 fprintf_filtered (stream
, "c_cc: ");
156 for (i
= 0; i
< NCCS
; i
+= 1)
157 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
158 fprintf_filtered (stream
, "\n");
161 /* Wait for the output to drain away, as opposed to flushing
165 hardwire_drain_output (struct serial
*scb
)
167 return tcdrain (scb
->fd
);
171 hardwire_flush_output (struct serial
*scb
)
173 return tcflush (scb
->fd
, TCOFLUSH
);
177 hardwire_flush_input (struct serial
*scb
)
179 ser_base_flush_input (scb
);
181 return tcflush (scb
->fd
, TCIFLUSH
);
185 hardwire_send_break (struct serial
*scb
)
187 return tcsendbreak (scb
->fd
, 0);
191 hardwire_raw (struct serial
*scb
)
193 struct hardwire_ttystate state
;
195 if (get_tty_state (scb
, &state
))
196 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n",
197 safe_strerror (errno
));
199 state
.termios
.c_iflag
= 0;
200 state
.termios
.c_oflag
= 0;
201 state
.termios
.c_lflag
= 0;
202 state
.termios
.c_cflag
&= ~CSIZE
;
203 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
205 /* h/w flow control. */
207 state
.termios
.c_cflag
|= CRTSCTS
;
209 state
.termios
.c_cflag
&= ~CRTSCTS
;
212 state
.termios
.c_cflag
|= CRTS_IFLOW
;
214 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
217 state
.termios
.c_cc
[VMIN
] = 0;
218 state
.termios
.c_cc
[VTIME
] = 0;
220 if (set_tty_state (scb
, &state
))
221 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n",
222 safe_strerror (errno
));
233 /* Translate baud rates from integers to damn B_codes. Unix should
234 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
334 rate_to_code (int rate
)
338 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
340 /* test for perfect macth. */
341 if (rate
== baudtab
[i
].rate
)
342 return baudtab
[i
].code
;
345 /* check if it is in between valid values. */
346 if (rate
< baudtab
[i
].rate
)
350 warning (_("Invalid baud rate %d. "
351 "Closest values are %d and %d."),
352 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
356 warning (_("Invalid baud rate %d. Minimum value is %d."),
357 rate
, baudtab
[0].rate
);
364 /* The requested speed was too large. */
365 warning (_("Invalid baud rate %d. Maximum value is %d."),
366 rate
, baudtab
[i
- 1].rate
);
371 hardwire_setbaudrate (struct serial
*scb
, int rate
)
373 struct hardwire_ttystate state
;
374 int baud_code
= rate_to_code (rate
);
378 /* The baud rate was not valid.
379 A warning has already been issued. */
384 if (get_tty_state (scb
, &state
))
387 cfsetospeed (&state
.termios
, baud_code
);
388 cfsetispeed (&state
.termios
, baud_code
);
390 return set_tty_state (scb
, &state
);
394 hardwire_setstopbits (struct serial
*scb
, int num
)
396 struct hardwire_ttystate state
;
399 if (get_tty_state (scb
, &state
))
404 case SERIAL_1_STOPBITS
:
407 case SERIAL_1_AND_A_HALF_STOPBITS
:
408 case SERIAL_2_STOPBITS
:
416 state
.termios
.c_cflag
&= ~CSTOPB
;
418 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
420 return set_tty_state (scb
, &state
);
423 /* Implement the "setparity" serial_ops callback. */
426 hardwire_setparity (struct serial
*scb
, int parity
)
428 struct hardwire_ttystate state
;
431 if (get_tty_state (scb
, &state
))
440 newparity
= PARENB
| PARODD
;
446 internal_warning (__FILE__
, __LINE__
,
447 "Incorrect parity value: %d", parity
);
451 state
.termios
.c_cflag
&= ~(PARENB
| PARODD
);
452 state
.termios
.c_cflag
|= newparity
;
454 return set_tty_state (scb
, &state
);
459 hardwire_close (struct serial
*scb
)
470 /* The hardwire ops. */
472 static const struct serial_ops hardwire_ops
=
480 hardwire_flush_output
,
481 hardwire_flush_input
,
484 hardwire_get_tty_state
,
485 hardwire_copy_tty_state
,
486 hardwire_set_tty_state
,
487 hardwire_print_tty_state
,
488 hardwire_setbaudrate
,
489 hardwire_setstopbits
,
491 hardwire_drain_output
,
498 _initialize_ser_hardwire (void)
500 serial_add_interface (&hardwire_ops
);
503 add_setshow_boolean_cmd ("remoteflow", no_class
,
505 Set use of hardware flow control for remote serial I/O."), _("\
506 Show use of hardware flow control for remote serial I/O."), _("\
507 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
508 when debugging using remote targets."),
511 &setlist
, &showlist
);
516 ser_unix_read_prim (struct serial
*scb
, size_t count
)
518 return read (scb
->fd
, scb
->buf
, count
);
522 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
524 return write (scb
->fd
, buf
, len
);