* Makefile.in: Add new file ser-tcp.c.
[external/binutils.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2    Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 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>
33
34 struct hardwire_ttystate
35 {
36   struct termios termios;
37 };
38 #endif
39
40 #ifdef HAVE_TERMIO
41 #include <termio.h>
42
43 struct hardwire_ttystate
44 {
45   struct termio termio;
46 };
47 #endif
48
49 #ifdef HAVE_SGTTY
50 #include <sgtty.h>
51
52 struct hardwire_ttystate
53 {
54   struct sgttyb sgttyb;
55 };
56 #endif
57
58 static int hardwire_open PARAMS ((serial_t scb, const char *name));
59 static void hardwire_raw PARAMS ((serial_t scb));
60 static int wait_for PARAMS ((serial_t scb, int timeout));
61 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
62 static int rate_to_code PARAMS ((int rate));
63 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
64 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
65 static void hardwire_restore PARAMS ((serial_t scb));
66 static void hardwire_close PARAMS ((serial_t scb));
67 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
68 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
69 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
70 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
71
72 /* Open up a real live device for serial I/O */
73
74 static int
75 hardwire_open(scb, name)
76      serial_t scb;
77      const char *name;
78 {
79   scb->fd = open (name, O_RDWR);
80   if (scb->fd < 0)
81     return -1;
82
83   return 0;
84 }
85
86 static int
87 get_tty_state(scb, state)
88      serial_t scb;
89      struct hardwire_ttystate *state;
90 {
91 #ifdef HAVE_TERMIOS
92   return tcgetattr(scb->fd, &state->termios);
93 #endif
94
95 #ifdef HAVE_TERMIO
96   return ioctl (scb->fd, TCGETA, &state->termio);
97 #endif
98
99 #ifdef HAVE_SGTTY
100   return ioctl (scb->fd, TIOCGETP, &state->sgttyb);
101 #endif
102 }
103
104 static int
105 set_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);
113 #endif
114
115 #ifdef HAVE_TERMIO
116   return ioctl (scb->fd, TCSETA, &state->termio);
117 #endif
118
119 #ifdef HAVE_SGTTY
120   return ioctl (scb->fd, TIOCSETP, &state->sgttyb);
121 #endif
122 }
123
124 static serial_ttystate
125 hardwire_get_tty_state(scb)
126      serial_t scb;
127 {
128   struct hardwire_ttystate *state;
129
130   state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
131
132   if (get_tty_state(scb, state))
133     return NULL;
134
135   return (serial_ttystate)state;
136 }
137
138 static int
139 hardwire_set_tty_state(scb, ttystate)
140      serial_t scb;
141      serial_ttystate ttystate;
142 {
143   struct hardwire_ttystate *state;
144
145   state = (struct hardwire_ttystate *)ttystate;
146
147   return set_tty_state(scb, state);
148 }
149
150 static void
151 hardwire_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);
182 #endif
183
184   scb->current_timeout = 0;
185
186   if (set_tty_state (scb, &state))
187     fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
188 }
189
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  */
196
197 static int
198 wait_for(scb, timeout)
199      serial_t scb;
200      int timeout;
201 {
202   int numfds;
203
204 #ifdef HAVE_SGTTY
205   struct timeval tv;
206   fd_set readfds;
207
208   FD_ZERO (&readfds);
209
210   tv.tv_sec = timeout;
211   tv.tv_usec = 0;
212
213   FD_SET(scb->fd, &readfds);
214
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);
219
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
228 #endif  /* HAVE_SGTTY */
229
230 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
231   if (timeout == scb->current_timeout)
232     return 0;
233
234   {
235     struct hardwire_ttystate state;
236
237     if (get_tty_state(scb, &state))
238       fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
239
240 #ifdef HAVE_TERMIOS
241     state.termios.c_cc[VTIME] = timeout * 10;
242 #endif
243
244 #ifdef HAVE_TERMIO
245     state.termio.c_cc[VTIME] = timeout * 10;
246 #endif
247
248     scb->current_timeout = timeout;
249
250     if (set_tty_state (scb, &state))
251       fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
252
253     return 0;
254   }
255 #endif  /* HAVE_TERMIO || HAVE_TERMIOS */
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
263 static int
264 hardwire_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;
277
278   scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
279
280   if (scb->bufcnt <= 0)
281     if (scb->bufcnt == 0)
282       return SERIAL_TIMEOUT;    /* 0 chars means timeout [may need to
283                                    distinguish between EOF & timeouts
284                                    someday] */
285     else
286       return SERIAL_ERROR;      /* Got an error from read */
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
304 static struct
305 {
306   int rate;
307   int code;
308 }
309 baudtab[] =
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
329 static int 
330 rate_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
342 static int
343 hardwire_setbaudrate(scb, rate)
344      serial_t scb;
345      int rate;
346 {
347   struct hardwire_ttystate state;
348
349   if (get_tty_state(scb, &state))
350     return -1;
351
352 #ifdef HAVE_TERMIOS
353   cfsetospeed (&state.termios, rate_to_code (rate));
354   cfsetispeed (&state.termios, rate_to_code (rate));
355 #endif
356
357 #ifdef HAVE_TERMIO
358 #ifndef CIBAUD
359 #define CIBAUD CBAUD
360 #endif
361
362   state.termio.c_cflag &= ~(CBAUD | CIBAUD);
363   state.termio.c_cflag |= rate_to_code (rate);
364 #endif
365
366 #ifdef HAVE_SGTTY
367   state.sgttyb.sg_ispeed = rate_to_code (rate);
368   state.sgttyb.sg_ospeed = rate_to_code (rate);
369 #endif
370
371   return set_tty_state (scb, &state);
372 }
373
374 static int
375 hardwire_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
394 static void
395 hardwire_close(scb)
396      serial_t scb;
397 {
398   if (scb->fd < 0)
399     return;
400
401   close(scb->fd);
402   scb->fd = -1;
403 }
404
405 static 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,
414   hardwire_get_tty_state,
415   hardwire_set_tty_state,
416   hardwire_setbaudrate,
417 };
418
419 void
420 _initialize_ser_hardwire ()
421 {
422   serial_add_interface (&hardwire_ops);
423 }