* remote.c: Add arg names to prototypes, in a modest effort at
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
4e772f44
SG
1/* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "serial.h"
22#include <fcntl.h>
23#include <sys/types.h>
24#include <sys/time.h>
25
26#if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
27#define HAVE_SGTTY
28#endif
29
30#ifdef HAVE_TERMIOS
31#include <termios.h>
32#include <unistd.h>
38dc5e12
SG
33
34struct hardwire_ttystate
35{
36 struct termios termios;
37};
4e772f44 38#endif
38dc5e12 39
4e772f44 40#ifdef HAVE_TERMIO
ebd99d54 41#include <termio.h>
38dc5e12
SG
42
43struct hardwire_ttystate
44{
45 struct termio termio;
46};
4e772f44 47#endif
38dc5e12 48
4e772f44
SG
49#ifdef HAVE_SGTTY
50#include <sgtty.h>
38dc5e12
SG
51
52struct hardwire_ttystate
53{
54 struct sgttyb sgttyb;
55};
4e772f44
SG
56#endif
57
9775789d
SG
58static int hardwire_open PARAMS ((serial_t scb, const char *name));
59static void hardwire_raw PARAMS ((serial_t scb));
60static int wait_for PARAMS ((serial_t scb, int timeout));
61static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
62static int rate_to_code PARAMS ((int rate));
63static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
64static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
65static void hardwire_restore PARAMS ((serial_t scb));
66static void hardwire_close PARAMS ((serial_t scb));
38dc5e12
SG
67static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
68static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
69static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
70static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
9775789d 71
4e772f44
SG
72/* Open up a real live device for serial I/O */
73
74static int
75hardwire_open(scb, name)
76 serial_t scb;
77 const char *name;
78{
79 scb->fd = open (name, O_RDWR);
80 if (scb->fd < 0)
4febd102 81 return -1;
4e772f44
SG
82
83 return 0;
84}
85
38dc5e12
SG
86static int
87get_tty_state(scb, state)
4e772f44 88 serial_t scb;
38dc5e12 89 struct hardwire_ttystate *state;
4e772f44
SG
90{
91#ifdef HAVE_TERMIOS
38dc5e12
SG
92 return tcgetattr(scb->fd, &state->termios);
93#endif
4e772f44 94
38dc5e12
SG
95#ifdef HAVE_TERMIO
96 return ioctl (scb->fd, TCGETA, &state->termio);
97#endif
4e772f44 98
38dc5e12
SG
99#ifdef HAVE_SGTTY
100 return ioctl (scb->fd, TIOCGETP, &state->sgttyb);
101#endif
102}
4e772f44 103
38dc5e12
SG
104static int
105set_tty_state(scb, state)
106 serial_t scb;
107 struct hardwire_ttystate *state;
108{
109 int err;
110
111#ifdef HAVE_TERMIOS
112 return tcsetattr(scb->fd, TCSANOW, &state->termios);
4e772f44
SG
113#endif
114
115#ifdef HAVE_TERMIO
38dc5e12
SG
116 return ioctl (scb->fd, TCSETA, &state->termio);
117#endif
4e772f44 118
38dc5e12
SG
119#ifdef HAVE_SGTTY
120 return ioctl (scb->fd, TIOCSETP, &state->sgttyb);
121#endif
122}
4e772f44 123
38dc5e12
SG
124static serial_ttystate
125hardwire_get_tty_state(scb)
126 serial_t scb;
127{
128 struct hardwire_ttystate *state;
4e772f44 129
38dc5e12 130 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
4e772f44 131
38dc5e12
SG
132 if (get_tty_state(scb, state))
133 return NULL;
4e772f44 134
38dc5e12
SG
135 return (serial_ttystate)state;
136}
4e772f44 137
38dc5e12
SG
138static int
139hardwire_set_tty_state(scb, ttystate)
140 serial_t scb;
141 serial_ttystate ttystate;
142{
143 struct hardwire_ttystate *state;
4e772f44 144
38dc5e12
SG
145 state = (struct hardwire_ttystate *)ttystate;
146
147 return set_tty_state(scb, state);
148}
149
150static void
151hardwire_raw(scb)
152 serial_t scb;
153{
154 struct hardwire_ttystate state;
155
156 if (get_tty_state(scb, &state))
157 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
158
159#ifdef HAVE_TERMIOS
160 state.termios.c_iflag = 0;
161 state.termios.c_oflag = 0;
162 state.termios.c_lflag = 0;
163 state.termios.c_cflag &= ~(CSIZE|PARENB);
164 state.termios.c_cflag |= CS8;
165 state.termios.c_cc[VMIN] = 0;
166 state.termios.c_cc[VTIME] = 0;
167#endif
168
169#ifdef HAVE_TERMIO
170 state.termio.c_iflag = 0;
171 state.termio.c_oflag = 0;
172 state.termio.c_lflag = 0;
173 state.termio.c_cflag &= ~(CSIZE|PARENB);
174 state.termio.c_cflag |= CS8;
175 state.termio.c_cc[VMIN] = 0;
176 state.termio.c_cc[VTIME] = 0;
177#endif
178
179#ifdef HAVE_SGTTY
180 state.sgttyb.sg_flags |= RAW | ANYP;
181 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
4e772f44 182#endif
9e15da4a
SG
183
184 scb->current_timeout = 0;
38dc5e12
SG
185
186 if (set_tty_state (scb, &state))
187 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
4e772f44
SG
188}
189
9e15da4a
SG
190/* Wait for input on scb, with timeout seconds. Returns 0 on success,
191 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
192
193 For termio{s}, we actually just setup VTIME if necessary, and let the
194 timeout occur in the read() in hardwire_read().
195 */
4e772f44
SG
196
197static int
9775789d 198wait_for(scb, timeout)
4e772f44
SG
199 serial_t scb;
200 int timeout;
201{
9775789d 202 int numfds;
4e772f44 203
9775789d
SG
204#ifdef HAVE_SGTTY
205 struct timeval tv;
206 fd_set readfds;
eca29634 207
9775789d 208 FD_ZERO (&readfds);
eca29634 209
9775789d
SG
210 tv.tv_sec = timeout;
211 tv.tv_usec = 0;
eca29634 212
9775789d 213 FD_SET(scb->fd, &readfds);
eca29634 214
a037b21e
SG
215 while (1)
216 {
217 if (timeout >= 0)
218 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
219 else
220 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
221
222 if (numfds <= 0)
223 if (numfds == 0)
224 return SERIAL_TIMEOUT;
225 else if (errno == EINTR)
226 continue;
227 else
228 return SERIAL_ERROR; /* Got an error from select or poll */
229
230 return 0;
231 }
9e15da4a 232
9775789d 233#endif /* HAVE_SGTTY */
4e772f44 234
9775789d 235#if defined HAVE_TERMIO || defined HAVE_TERMIOS
9e15da4a
SG
236 if (timeout == scb->current_timeout)
237 return 0;
4e772f44 238
9e15da4a 239 {
38dc5e12 240 struct hardwire_ttystate state;
eca29634 241
38dc5e12
SG
242 if (get_tty_state(scb, &state))
243 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
eca29634 244
38dc5e12
SG
245#ifdef HAVE_TERMIOS
246 state.termios.c_cc[VTIME] = timeout * 10;
247#endif
9775789d 248
9e15da4a 249#ifdef HAVE_TERMIO
38dc5e12
SG
250 state.termio.c_cc[VTIME] = timeout * 10;
251#endif
9e15da4a 252
38dc5e12 253 scb->current_timeout = timeout;
9e15da4a 254
38dc5e12
SG
255 if (set_tty_state (scb, &state))
256 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
9e15da4a 257
9e15da4a
SG
258 return 0;
259 }
260#endif /* HAVE_TERMIO || HAVE_TERMIOS */
9775789d
SG
261}
262
263/* Read a character with user-specified timeout. TIMEOUT is number of seconds
264 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
265 char if successful. Returns -2 if timeout expired, EOF if line dropped
266 dead, or -3 for any other error (see errno in that case). */
267
268static int
269hardwire_readchar(scb, timeout)
270 serial_t scb;
271 int timeout;
272{
273 int status;
274
275 if (scb->bufcnt-- > 0)
276 return *scb->bufp++;
277
278 status = wait_for(scb, timeout);
279
280 if (status < 0)
281 return status;
4e772f44 282
9775789d 283 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
4e772f44
SG
284
285 if (scb->bufcnt <= 0)
286 if (scb->bufcnt == 0)
9e15da4a
SG
287 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
288 distinguish between EOF & timeouts
289 someday] */
4e772f44 290 else
4febd102 291 return SERIAL_ERROR; /* Got an error from read */
4e772f44
SG
292
293 scb->bufcnt--;
294 scb->bufp = scb->buf;
295 return *scb->bufp++;
296}
297
298#ifndef B19200
299#define B19200 EXTA
300#endif
301
302#ifndef B38400
303#define B38400 EXTB
304#endif
305
306/* Translate baud rates from integers to damn B_codes. Unix should
307 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
308
309static struct
310{
311 int rate;
312 int code;
313}
314baudtab[] =
315{
316 {50, B50},
317 {75, B75},
318 {110, B110},
319 {134, B134},
320 {150, B150},
321 {200, B200},
322 {300, B300},
323 {600, B600},
324 {1200, B1200},
325 {1800, B1800},
326 {2400, B2400},
327 {4800, B4800},
328 {9600, B9600},
329 {19200, B19200},
330 {38400, B38400},
331 {-1, -1},
332};
333
334static int
335rate_to_code(rate)
336 int rate;
337{
338 int i;
339
340 for (i = 0; baudtab[i].rate != -1; i++)
341 if (rate == baudtab[i].rate)
342 return baudtab[i].code;
343
344 return -1;
345}
346
347static int
348hardwire_setbaudrate(scb, rate)
349 serial_t scb;
350 int rate;
351{
38dc5e12 352 struct hardwire_ttystate state;
4e772f44 353
38dc5e12 354 if (get_tty_state(scb, &state))
4febd102 355 return -1;
4e772f44 356
38dc5e12
SG
357#ifdef HAVE_TERMIOS
358 cfsetospeed (&state.termios, rate_to_code (rate));
359 cfsetispeed (&state.termios, rate_to_code (rate));
4e772f44
SG
360#endif
361
362#ifdef HAVE_TERMIO
4e772f44
SG
363#ifndef CIBAUD
364#define CIBAUD CBAUD
365#endif
366
38dc5e12
SG
367 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
368 state.termio.c_cflag |= rate_to_code (rate);
4e772f44
SG
369#endif
370
371#ifdef HAVE_SGTTY
38dc5e12
SG
372 state.sgttyb.sg_ispeed = rate_to_code (rate);
373 state.sgttyb.sg_ospeed = rate_to_code (rate);
4e772f44 374#endif
38dc5e12
SG
375
376 return set_tty_state (scb, &state);
4e772f44
SG
377}
378
379static int
380hardwire_write(scb, str, len)
381 serial_t scb;
382 const char *str;
383 int len;
384{
385 int cc;
386
387 while (len > 0)
388 {
389 cc = write(scb->fd, str, len);
390
391 if (cc < 0)
392 return 1;
393 len -= cc;
394 str += cc;
395 }
396 return 0;
397}
398
4e772f44
SG
399static void
400hardwire_close(scb)
401 serial_t scb;
402{
403 if (scb->fd < 0)
404 return;
405
4e772f44
SG
406 close(scb->fd);
407 scb->fd = -1;
408}
409
410static struct serial_ops hardwire_ops =
411{
412 "hardwire",
413 0,
414 hardwire_open,
415 hardwire_close,
416 hardwire_readchar,
417 hardwire_write,
418 hardwire_raw,
38dc5e12
SG
419 hardwire_get_tty_state,
420 hardwire_set_tty_state,
421 hardwire_setbaudrate,
4e772f44
SG
422};
423
9775789d 424void
4e772f44
SG
425_initialize_ser_hardwire ()
426{
427 serial_add_interface (&hardwire_ops);
428}
This page took 0.0613359999999999 seconds and 4 git commands to generate.