serial.h revision 46283
1296417Sdim/* Remote serial support interface definitions for GDB, the GNU Debugger.
2254721Semaste   Copyright 1992, 1993 Free Software Foundation, Inc.
3254721Semaste
4254721SemasteThis file is part of GDB.
5254721Semaste
6254721SemasteThis program is free software; you can redistribute it and/or modify
7254721Semasteit under the terms of the GNU General Public License as published by
8254721Semastethe Free Software Foundation; either version 2 of the License, or
9254721Semaste(at your option) any later version.
10254721Semaste
11254721SemasteThis program is distributed in the hope that it will be useful,
12254721Semastebut WITHOUT ANY WARRANTY; without even the implied warranty of
13296417SdimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14296417SdimGNU General Public License for more details.
15254721Semaste
16254721SemasteYou should have received a copy of the GNU General Public License
17296417Sdimalong with this program; if not, write to the Free Software
18296417SdimFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19254721Semaste
20254721Semaste#ifndef SERIAL_H
21254721Semaste#define SERIAL_H
22254721Semaste
23254721Semaste/* Terminal state pointer.  This is specific to each type of interface. */
24254721Semaste
25254721Semastetypedef PTR serial_ttystate;
26254721Semaste
27254721Semastestruct _serial_t
28254721Semaste{
29254721Semaste  int fd;			/* File descriptor */
30254721Semaste  struct serial_ops *ops;	/* Function vector */
31254721Semaste  serial_ttystate ttystate;	/* Not used (yet) */
32254721Semaste  int bufcnt;			/* Amount of data in receive buffer */
33254721Semaste  unsigned char *bufp;		/* Current byte */
34254721Semaste  unsigned char buf[BUFSIZ];	/* Da buffer itself */
35254721Semaste  int current_timeout;		/* (termio{s} only), last value of VTIME */
36258054Semaste  /* ser-unix.c termio{,s} only, we still need to wait for this many more
37258054Semaste     seconds.  */
38258054Semaste  int timeout_remaining;
39258054Semaste  char *name;			/* The name of the device or host */
40254721Semaste  struct _serial_t *next;	/* Pointer to the next serial_t */
41254721Semaste  int refcnt;			/* Number of pointers to this block */
42254721Semaste};
43254721Semaste
44254721Semastetypedef struct _serial_t *serial_t;
45254721Semaste
46254721Semastestruct serial_ops {
47276479Sdim  char *name;
48254721Semaste  struct serial_ops *next;
49254721Semaste  int (*open) PARAMS ((serial_t, const char *name));
50254721Semaste  void (*close) PARAMS ((serial_t));
51254721Semaste  int (*readchar) PARAMS ((serial_t, int timeout));
52254721Semaste  int (*write) PARAMS ((serial_t, const char *str, int len));
53254721Semaste  /* Discard pending output */
54254721Semaste  int (*flush_output) PARAMS ((serial_t));
55254721Semaste  /* Discard pending input */
56254721Semaste  int (*flush_input) PARAMS ((serial_t));
57254721Semaste  int (*send_break) PARAMS ((serial_t));
58254721Semaste  void (*go_raw) PARAMS ((serial_t));
59254721Semaste  serial_ttystate (*get_tty_state) PARAMS ((serial_t));
60254721Semaste  int (*set_tty_state) PARAMS ((serial_t, serial_ttystate));
61254721Semaste  void (*print_tty_state) PARAMS ((serial_t, serial_ttystate));
62254721Semaste  int (*noflush_set_tty_state)
63254721Semaste    PARAMS ((serial_t, serial_ttystate, serial_ttystate));
64254721Semaste  int (*setbaudrate) PARAMS ((serial_t, int rate));
65254721Semaste  int (*setstopbits) PARAMS ((serial_t, int num));
66254721Semaste  /* Wait for output to drain */
67254721Semaste  int (*drain_output) PARAMS ((serial_t));
68254721Semaste};
69254721Semaste
70254721Semaste/* Add a new serial interface to the interface list */
71254721Semaste
72254721Semastevoid serial_add_interface PARAMS ((struct serial_ops *optable));
73254721Semaste
74254721Semasteserial_t serial_open PARAMS ((const char *name));
75254721Semaste
76254721Semasteserial_t serial_fdopen PARAMS ((const int fd));
77254721Semaste
78254721Semaste/* For most routines, if a failure is indicated, then errno should be
79254721Semaste   examined.  */
80254721Semaste
81254721Semaste/* Try to open NAME.  Returns a new serial_t on success, NULL on failure.
82254721Semaste */
83254721Semaste
84254721Semaste#define SERIAL_OPEN(NAME) serial_open(NAME)
85254721Semaste
86254721Semaste/* Open a new serial stream using a file handle.  */
87254721Semaste
88254721Semaste#define SERIAL_FDOPEN(FD) serial_fdopen(FD)
89254721Semaste
90254721Semaste/* Allow pending output to drain. */
91254721Semaste
92254721Semaste#define SERIAL_DRAIN_OUTPUT(SERIAL_T) \
93254721Semaste  ((SERIAL_T)->ops->drain_output((SERIAL_T)))
94254721Semaste
95254721Semaste/* Flush (discard) pending output.  Might also flush input (if this system can't flush
96254721Semaste   only output).  */
97254721Semaste
98254721Semaste#define SERIAL_FLUSH_OUTPUT(SERIAL_T) \
99254721Semaste  ((SERIAL_T)->ops->flush_output((SERIAL_T)))
100254721Semaste
101254721Semaste/* Flush pending input.  Might also flush output (if this system can't flush
102254721Semaste   only input).  */
103254721Semaste
104254721Semaste#define SERIAL_FLUSH_INPUT(SERIAL_T)\
105254721Semaste  ((*(SERIAL_T)->ops->flush_input) ((SERIAL_T)))
106254721Semaste
107254721Semaste/* Send a break between 0.25 and 0.5 seconds long.  */
108254721Semaste
109254721Semasteextern int serial_send_break PARAMS ((serial_t scb));
110254721Semaste
111254721Semaste#define SERIAL_SEND_BREAK(SERIAL_T) serial_send_break (SERIAL_T)
112254721Semaste
113254721Semaste/* Turn the port into raw mode. */
114254721Semaste
115254721Semaste#define SERIAL_RAW(SERIAL_T) (SERIAL_T)->ops->go_raw((SERIAL_T))
116254721Semaste
117288943Sdim/* Return a pointer to a newly malloc'd ttystate containing the state
118296417Sdim   of the tty.  */
119254721Semaste#define SERIAL_GET_TTY_STATE(SERIAL_T) (SERIAL_T)->ops->get_tty_state((SERIAL_T))
120254721Semaste
121254721Semaste/* Set the state of the tty to TTYSTATE.  The change is immediate.
122254721Semaste   When changing to or from raw mode, input might be discarded.
123254721Semaste   Returns 0 for success, negative value for error (in which case errno
124254721Semaste   contains the error).  */
125254721Semaste#define SERIAL_SET_TTY_STATE(SERIAL_T, TTYSTATE) (SERIAL_T)->ops->set_tty_state((SERIAL_T), (TTYSTATE))
126296417Sdim
127254721Semaste/* printf_filtered a user-comprehensible description of ttystate.  */
128296417Sdim#define SERIAL_PRINT_TTY_STATE(SERIAL_T, TTYSTATE) \
129254721Semaste  ((*((SERIAL_T)->ops->print_tty_state)) ((SERIAL_T), (TTYSTATE)))
130296417Sdim
131296417Sdim/* Set the tty state to NEW_TTYSTATE, where OLD_TTYSTATE is the
132254721Semaste   current state (generally obtained from a recent call to
133296417Sdim   SERIAL_GET_TTY_STATE), but be careful not to discard any input.
134296417Sdim   This means that we never switch in or out of raw mode, even
135254721Semaste   if NEW_TTYSTATE specifies a switch.  */
136296417Sdim#define SERIAL_NOFLUSH_SET_TTY_STATE(SERIAL_T, NEW_TTYSTATE, OLD_TTYSTATE) \
137296417Sdim  ((*((SERIAL_T)->ops->noflush_set_tty_state)) \
138254721Semaste    ((SERIAL_T), (NEW_TTYSTATE), (OLD_TTYSTATE)))
139296417Sdim
140296417Sdim/* Read one char from the serial device with TIMEOUT seconds to wait
141254721Semaste   or -1 to wait forever.  Use timeout of 0 to effect a poll. Returns
142254721Semaste   char if ok, else one of the following codes.  Note that all error
143254721Semaste   codes are guaranteed to be < 0.  */
144254721Semaste
145254721Semaste#define SERIAL_ERROR -1		/* General error, see errno for details */
146254721Semaste#define SERIAL_TIMEOUT -2
147254721Semaste#define SERIAL_EOF -3
148296417Sdim
149296417Sdimextern int serial_readchar PARAMS ((serial_t scb, int timeout));
150254721Semaste
151254721Semaste#define SERIAL_READCHAR(SERIAL_T, TIMEOUT)  serial_readchar (SERIAL_T, TIMEOUT)
152254721Semaste
153254721Semaste/* Set the baudrate to the decimal value supplied.  Returns 0 for success,
154254721Semaste   -1 for failure.  */
155254721Semaste
156254721Semaste#define SERIAL_SETBAUDRATE(SERIAL_T, RATE) ((SERIAL_T)->ops->setbaudrate((SERIAL_T), RATE))
157296417Sdim
158254721Semaste/* Set the number of stop bits to the value specified.  Returns 0 for success,
159254721Semaste   -1 for failure.  */
160254721Semaste
161254721Semaste#define SERIAL_1_STOPBITS 1
162254721Semaste#define SERIAL_1_AND_A_HALF_STOPBITS 2 /* 1.5 bits, snicker... */
163254721Semaste#define SERIAL_2_STOPBITS 3
164254721Semaste
165254721Semaste#define SERIAL_SETSTOPBITS(SERIAL_T, NUM) ((SERIAL_T)->ops->setstopbits((SERIAL_T), NUM))
166254721Semaste
167254721Semaste/* Write LEN chars from STRING to the port SERIAL_T.  Returns 0 for
168254721Semaste   success, non-zero for failure.  */
169254721Semaste
170254721Semasteextern int serial_write PARAMS ((serial_t scb, const char *str, int len));
171254721Semaste
172254721Semaste#define SERIAL_WRITE(SERIAL_T, STRING,LEN)  serial_write (SERIAL_T, STRING, LEN)
173254721Semaste
174254721Semaste/* Push out all buffers, close the device and destroy SERIAL_T. */
175254721Semaste
176288943Sdimextern void serial_close PARAMS ((serial_t, int));
177254721Semaste
178296417Sdim#define SERIAL_CLOSE(SERIAL_T) serial_close(SERIAL_T, 1)
179254721Semaste
180254721Semaste/* Push out all buffers and destroy SERIAL_T without closing the device.  */
181296417Sdim
182#define SERIAL_UN_FDOPEN(SERIAL_T) serial_close(SERIAL_T, 0)
183
184extern void serial_printf PARAMS ((serial_t desc, const char *, ...))
185     ATTR_FORMAT(printf, 2, 3);
186
187/* File in which to record the remote debugging session */
188
189extern void serial_log_command PARAMS ((const char *));
190
191#endif /* SERIAL_H */
192