Add inclusive range support for Rust
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2
3 Copyright (C) 1992-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include "ser-base.h"
23 #include "ser-unix.h"
24
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include "terminal.h"
28 #include <sys/socket.h>
29 #include "gdb_sys_time.h"
30
31 #include "gdb_select.h"
32 #include "gdbcmd.h"
33 #include "filestuff.h"
34 #include <termios.h>
35
36 struct hardwire_ttystate
37 {
38 struct termios termios;
39 };
40
41 #ifdef CRTSCTS
42 /* Boolean to explicitly enable or disable h/w flow control. */
43 static int serial_hwflow;
44 static void
45 show_serial_hwflow (struct ui_file *file, int from_tty,
46 struct cmd_list_element *c, const char *value)
47 {
48 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
49 }
50 #endif
51
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,
65 struct ui_file *);
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);
71
72 /* Open up a real live device for serial I/O. */
73
74 static int
75 hardwire_open (struct serial *scb, const char *name)
76 {
77 scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
78 if (scb->fd < 0)
79 return -1;
80
81 return 0;
82 }
83
84 static int
85 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
86 {
87 if (tcgetattr (scb->fd, &state->termios) < 0)
88 return -1;
89
90 return 0;
91 }
92
93 static int
94 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
95 {
96 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
97 return -1;
98
99 return 0;
100 }
101
102 static serial_ttystate
103 hardwire_get_tty_state (struct serial *scb)
104 {
105 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
106
107 if (get_tty_state (scb, state))
108 {
109 xfree (state);
110 return NULL;
111 }
112
113 return (serial_ttystate) state;
114 }
115
116 static serial_ttystate
117 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
118 {
119 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
120
121 *state = *(struct hardwire_ttystate *) ttystate;
122
123 return (serial_ttystate) state;
124 }
125
126 static int
127 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
128 {
129 struct hardwire_ttystate *state;
130
131 state = (struct hardwire_ttystate *) ttystate;
132
133 return set_tty_state (scb, state);
134 }
135
136 static void
137 hardwire_print_tty_state (struct serial *scb,
138 serial_ttystate ttystate,
139 struct ui_file *stream)
140 {
141 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
142 int i;
143
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);
150 #if 0
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);
154 #endif
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");
159 }
160
161 /* Wait for the output to drain away, as opposed to flushing
162 (discarding) it. */
163
164 static int
165 hardwire_drain_output (struct serial *scb)
166 {
167 return tcdrain (scb->fd);
168 }
169
170 static int
171 hardwire_flush_output (struct serial *scb)
172 {
173 return tcflush (scb->fd, TCOFLUSH);
174 }
175
176 static int
177 hardwire_flush_input (struct serial *scb)
178 {
179 ser_base_flush_input (scb);
180
181 return tcflush (scb->fd, TCIFLUSH);
182 }
183
184 static int
185 hardwire_send_break (struct serial *scb)
186 {
187 return tcsendbreak (scb->fd, 0);
188 }
189
190 static void
191 hardwire_raw (struct serial *scb)
192 {
193 struct hardwire_ttystate state;
194
195 if (get_tty_state (scb, &state))
196 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
197 safe_strerror (errno));
198
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;
204 #ifdef CRTSCTS
205 /* h/w flow control. */
206 if (serial_hwflow)
207 state.termios.c_cflag |= CRTSCTS;
208 else
209 state.termios.c_cflag &= ~CRTSCTS;
210 #ifdef CRTS_IFLOW
211 if (serial_hwflow)
212 state.termios.c_cflag |= CRTS_IFLOW;
213 else
214 state.termios.c_cflag &= ~CRTS_IFLOW;
215 #endif
216 #endif
217 state.termios.c_cc[VMIN] = 0;
218 state.termios.c_cc[VTIME] = 0;
219
220 if (set_tty_state (scb, &state))
221 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
222 safe_strerror (errno));
223 }
224
225 #ifndef B19200
226 #define B19200 EXTA
227 #endif
228
229 #ifndef B38400
230 #define B38400 EXTB
231 #endif
232
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. */
235
236 static struct
237 {
238 int rate;
239 int code;
240 }
241 baudtab[] =
242 {
243 {
244 50, B50
245 }
246 ,
247 {
248 75, B75
249 }
250 ,
251 {
252 110, B110
253 }
254 ,
255 {
256 134, B134
257 }
258 ,
259 {
260 150, B150
261 }
262 ,
263 {
264 200, B200
265 }
266 ,
267 {
268 300, B300
269 }
270 ,
271 {
272 600, B600
273 }
274 ,
275 {
276 1200, B1200
277 }
278 ,
279 {
280 1800, B1800
281 }
282 ,
283 {
284 2400, B2400
285 }
286 ,
287 {
288 4800, B4800
289 }
290 ,
291 {
292 9600, B9600
293 }
294 ,
295 {
296 19200, B19200
297 }
298 ,
299 {
300 38400, B38400
301 }
302 ,
303 #ifdef B57600
304 {
305 57600, B57600
306 }
307 ,
308 #endif
309 #ifdef B115200
310 {
311 115200, B115200
312 }
313 ,
314 #endif
315 #ifdef B230400
316 {
317 230400, B230400
318 }
319 ,
320 #endif
321 #ifdef B460800
322 {
323 460800, B460800
324 }
325 ,
326 #endif
327 {
328 -1, -1
329 }
330 ,
331 };
332
333 static int
334 rate_to_code (int rate)
335 {
336 int i;
337
338 for (i = 0; baudtab[i].rate != -1; i++)
339 {
340 /* test for perfect macth. */
341 if (rate == baudtab[i].rate)
342 return baudtab[i].code;
343 else
344 {
345 /* check if it is in between valid values. */
346 if (rate < baudtab[i].rate)
347 {
348 if (i)
349 {
350 warning (_("Invalid baud rate %d. "
351 "Closest values are %d and %d."),
352 rate, baudtab[i - 1].rate, baudtab[i].rate);
353 }
354 else
355 {
356 warning (_("Invalid baud rate %d. Minimum value is %d."),
357 rate, baudtab[0].rate);
358 }
359 return -1;
360 }
361 }
362 }
363
364 /* The requested speed was too large. */
365 warning (_("Invalid baud rate %d. Maximum value is %d."),
366 rate, baudtab[i - 1].rate);
367 return -1;
368 }
369
370 static int
371 hardwire_setbaudrate (struct serial *scb, int rate)
372 {
373 struct hardwire_ttystate state;
374 int baud_code = rate_to_code (rate);
375
376 if (baud_code < 0)
377 {
378 /* The baud rate was not valid.
379 A warning has already been issued. */
380 errno = EINVAL;
381 return -1;
382 }
383
384 if (get_tty_state (scb, &state))
385 return -1;
386
387 cfsetospeed (&state.termios, baud_code);
388 cfsetispeed (&state.termios, baud_code);
389
390 return set_tty_state (scb, &state);
391 }
392
393 static int
394 hardwire_setstopbits (struct serial *scb, int num)
395 {
396 struct hardwire_ttystate state;
397 int newbit;
398
399 if (get_tty_state (scb, &state))
400 return -1;
401
402 switch (num)
403 {
404 case SERIAL_1_STOPBITS:
405 newbit = 0;
406 break;
407 case SERIAL_1_AND_A_HALF_STOPBITS:
408 case SERIAL_2_STOPBITS:
409 newbit = 1;
410 break;
411 default:
412 return 1;
413 }
414
415 if (!newbit)
416 state.termios.c_cflag &= ~CSTOPB;
417 else
418 state.termios.c_cflag |= CSTOPB; /* two bits */
419
420 return set_tty_state (scb, &state);
421 }
422
423 /* Implement the "setparity" serial_ops callback. */
424
425 static int
426 hardwire_setparity (struct serial *scb, int parity)
427 {
428 struct hardwire_ttystate state;
429 int newparity = 0;
430
431 if (get_tty_state (scb, &state))
432 return -1;
433
434 switch (parity)
435 {
436 case GDBPARITY_NONE:
437 newparity = 0;
438 break;
439 case GDBPARITY_ODD:
440 newparity = PARENB | PARODD;
441 break;
442 case GDBPARITY_EVEN:
443 newparity = PARENB;
444 break;
445 default:
446 internal_warning (__FILE__, __LINE__,
447 "Incorrect parity value: %d", parity);
448 return -1;
449 }
450
451 state.termios.c_cflag &= ~(PARENB | PARODD);
452 state.termios.c_cflag |= newparity;
453
454 return set_tty_state (scb, &state);
455 }
456
457
458 static void
459 hardwire_close (struct serial *scb)
460 {
461 if (scb->fd < 0)
462 return;
463
464 close (scb->fd);
465 scb->fd = -1;
466 }
467 \f
468 \f
469
470 /* The hardwire ops. */
471
472 static const struct serial_ops hardwire_ops =
473 {
474 "hardwire",
475 hardwire_open,
476 hardwire_close,
477 NULL,
478 ser_base_readchar,
479 ser_base_write,
480 hardwire_flush_output,
481 hardwire_flush_input,
482 hardwire_send_break,
483 hardwire_raw,
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,
490 hardwire_setparity,
491 hardwire_drain_output,
492 ser_base_async,
493 ser_unix_read_prim,
494 ser_unix_write_prim
495 };
496
497 void
498 _initialize_ser_hardwire (void)
499 {
500 serial_add_interface (&hardwire_ops);
501
502 #ifdef CRTSCTS
503 add_setshow_boolean_cmd ("remoteflow", no_class,
504 &serial_hwflow, _("\
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."),
509 NULL,
510 show_serial_hwflow,
511 &setlist, &showlist);
512 #endif
513 }
514
515 int
516 ser_unix_read_prim (struct serial *scb, size_t count)
517 {
518 return read (scb->fd, scb->buf, count);
519 }
520
521 int
522 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
523 {
524 return write (scb->fd, buf, len);
525 }
This page took 0.040495 seconds and 4 git commands to generate.