* Rename remote-es1800.c to remote-es.c
[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
9775789d
SG
215 if (timeout >= 0)
216 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
217 else
218 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
4e772f44 219
9e15da4a
SG
220 if (numfds <= 0)
221 if (numfds == 0)
222 return SERIAL_TIMEOUT;
223 else
224 return SERIAL_ERROR; /* Got an error from select or poll */
225
226 return 0;
227
9775789d 228#endif /* HAVE_SGTTY */
4e772f44 229
9775789d 230#if defined HAVE_TERMIO || defined HAVE_TERMIOS
9e15da4a
SG
231 if (timeout == scb->current_timeout)
232 return 0;
4e772f44 233
9e15da4a 234 {
38dc5e12 235 struct hardwire_ttystate state;
eca29634 236
38dc5e12
SG
237 if (get_tty_state(scb, &state))
238 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
eca29634 239
38dc5e12
SG
240#ifdef HAVE_TERMIOS
241 state.termios.c_cc[VTIME] = timeout * 10;
242#endif
9775789d 243
9e15da4a 244#ifdef HAVE_TERMIO
38dc5e12
SG
245 state.termio.c_cc[VTIME] = timeout * 10;
246#endif
9e15da4a 247
38dc5e12 248 scb->current_timeout = timeout;
9e15da4a 249
38dc5e12
SG
250 if (set_tty_state (scb, &state))
251 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
9e15da4a 252
9e15da4a
SG
253 return 0;
254 }
255#endif /* HAVE_TERMIO || HAVE_TERMIOS */
9775789d
SG
256}
257
258/* Read a character with user-specified timeout. TIMEOUT is number of seconds
259 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
260 char if successful. Returns -2 if timeout expired, EOF if line dropped
261 dead, or -3 for any other error (see errno in that case). */
262
263static int
264hardwire_readchar(scb, timeout)
265 serial_t scb;
266 int timeout;
267{
268 int status;
269
270 if (scb->bufcnt-- > 0)
271 return *scb->bufp++;
272
273 status = wait_for(scb, timeout);
274
275 if (status < 0)
276 return status;
4e772f44 277
9775789d 278 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
4e772f44
SG
279
280 if (scb->bufcnt <= 0)
281 if (scb->bufcnt == 0)
9e15da4a
SG
282 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
283 distinguish between EOF & timeouts
284 someday] */
4e772f44 285 else
4febd102 286 return SERIAL_ERROR; /* Got an error from read */
4e772f44
SG
287
288 scb->bufcnt--;
289 scb->bufp = scb->buf;
290 return *scb->bufp++;
291}
292
293#ifndef B19200
294#define B19200 EXTA
295#endif
296
297#ifndef B38400
298#define B38400 EXTB
299#endif
300
301/* Translate baud rates from integers to damn B_codes. Unix should
302 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
303
304static struct
305{
306 int rate;
307 int code;
308}
309baudtab[] =
310{
311 {50, B50},
312 {75, B75},
313 {110, B110},
314 {134, B134},
315 {150, B150},
316 {200, B200},
317 {300, B300},
318 {600, B600},
319 {1200, B1200},
320 {1800, B1800},
321 {2400, B2400},
322 {4800, B4800},
323 {9600, B9600},
324 {19200, B19200},
325 {38400, B38400},
326 {-1, -1},
327};
328
329static int
330rate_to_code(rate)
331 int rate;
332{
333 int i;
334
335 for (i = 0; baudtab[i].rate != -1; i++)
336 if (rate == baudtab[i].rate)
337 return baudtab[i].code;
338
339 return -1;
340}
341
342static int
343hardwire_setbaudrate(scb, rate)
344 serial_t scb;
345 int rate;
346{
38dc5e12 347 struct hardwire_ttystate state;
4e772f44 348
38dc5e12 349 if (get_tty_state(scb, &state))
4febd102 350 return -1;
4e772f44 351
38dc5e12
SG
352#ifdef HAVE_TERMIOS
353 cfsetospeed (&state.termios, rate_to_code (rate));
354 cfsetispeed (&state.termios, rate_to_code (rate));
4e772f44
SG
355#endif
356
357#ifdef HAVE_TERMIO
4e772f44
SG
358#ifndef CIBAUD
359#define CIBAUD CBAUD
360#endif
361
38dc5e12
SG
362 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
363 state.termio.c_cflag |= rate_to_code (rate);
4e772f44
SG
364#endif
365
366#ifdef HAVE_SGTTY
38dc5e12
SG
367 state.sgttyb.sg_ispeed = rate_to_code (rate);
368 state.sgttyb.sg_ospeed = rate_to_code (rate);
4e772f44 369#endif
38dc5e12
SG
370
371 return set_tty_state (scb, &state);
4e772f44
SG
372}
373
374static int
375hardwire_write(scb, str, len)
376 serial_t scb;
377 const char *str;
378 int len;
379{
380 int cc;
381
382 while (len > 0)
383 {
384 cc = write(scb->fd, str, len);
385
386 if (cc < 0)
387 return 1;
388 len -= cc;
389 str += cc;
390 }
391 return 0;
392}
393
4e772f44
SG
394static void
395hardwire_close(scb)
396 serial_t scb;
397{
398 if (scb->fd < 0)
399 return;
400
4e772f44
SG
401 close(scb->fd);
402 scb->fd = -1;
403}
404
405static struct serial_ops hardwire_ops =
406{
407 "hardwire",
408 0,
409 hardwire_open,
410 hardwire_close,
411 hardwire_readchar,
412 hardwire_write,
413 hardwire_raw,
38dc5e12
SG
414 hardwire_get_tty_state,
415 hardwire_set_tty_state,
416 hardwire_setbaudrate,
4e772f44
SG
417};
418
9775789d 419void
4e772f44
SG
420_initialize_ser_hardwire ()
421{
422 serial_add_interface (&hardwire_ops);
423}
This page took 0.052673 seconds and 4 git commands to generate.