119370Spst/* Remote serial support interface definitions for GDB, the GNU Debugger.
298944Sobrien   Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000
398944Sobrien   Free Software Foundation, Inc.
419370Spst
598944Sobrien   This file is part of GDB.
619370Spst
798944Sobrien   This program is free software; you can redistribute it and/or modify
898944Sobrien   it under the terms of the GNU General Public License as published by
998944Sobrien   the Free Software Foundation; either version 2 of the License, or
1098944Sobrien   (at your option) any later version.
1119370Spst
1298944Sobrien   This program is distributed in the hope that it will be useful,
1398944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1498944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1598944Sobrien   GNU General Public License for more details.
1619370Spst
1798944Sobrien   You should have received a copy of the GNU General Public License
1898944Sobrien   along with this program; if not, write to the Free Software
1998944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2098944Sobrien   Boston, MA 02111-1307, USA.  */
2119370Spst
2219370Spst#ifndef SERIAL_H
2319370Spst#define SERIAL_H
2419370Spst
25130803Smarcelstruct ui_file;
26130803Smarcel
2798944Sobrien/* For most routines, if a failure is indicated, then errno should be
2898944Sobrien   examined.  */
2919370Spst
3098944Sobrien/* Terminal state pointer.  This is specific to each type of
3198944Sobrien   interface. */
3219370Spst
3398944Sobrientypedef void *serial_ttystate;
3498944Sobrienstruct serial;
3519370Spst
3698944Sobrien/* Try to open NAME.  Returns a new `struct serial *' on success, NULL
3798944Sobrien   on failure. Note that some open calls can block and, if possible,
3898944Sobrien   should be  written to be non-blocking, with calls to ui_look_hook
3998944Sobrien   so they can be cancelled. An async interface for open could be
4098944Sobrien   added to GDB if necessary. */
4119370Spst
4298944Sobrienextern struct serial *serial_open (const char *name);
4319370Spst
4498944Sobrien/* Open a new serial stream using a file handle.  */
4519370Spst
4698944Sobrienextern struct serial *serial_fdopen (const int fd);
4719370Spst
4898944Sobrien/* Push out all buffers, close the device and destroy SCB. */
4919370Spst
5098944Sobrienextern void serial_close (struct serial *scb);
5119370Spst
5298944Sobrien/* Push out all buffers and destroy SCB without closing the device.  */
5319370Spst
5498944Sobrienextern void serial_un_fdopen (struct serial *scb);
5519370Spst
5698944Sobrien/* Read one char from the serial device with TIMEOUT seconds to wait
5798944Sobrien   or -1 to wait forever.  Use timeout of 0 to effect a poll.
5898944Sobrien   Infinite waits are not permitted. Returns unsigned char if ok, else
5998944Sobrien   one of the following codes.  Note that all error return-codes are
6098944Sobrien   guaranteed to be < 0. */
6119370Spst
6298944Sobrienenum serial_rc {
6398944Sobrien  SERIAL_ERROR = -1,	/* General error. */
6498944Sobrien  SERIAL_TIMEOUT = -2,	/* Timeout or data-not-ready during read.
6598944Sobrien			   Unfortunately, through ui_loop_hook(), this
6698944Sobrien			   can also be a QUIT indication.  */
6798944Sobrien  SERIAL_EOF = -3	/* General end-of-file or remote target
6898944Sobrien			   connection closed, indication.  Includes
6998944Sobrien			   things like the line dropping dead. */
7098944Sobrien};
7119370Spst
7298944Sobrienextern int serial_readchar (struct serial *scb, int timeout);
7319370Spst
7498944Sobrien/* Write LEN chars from STRING to the port SCB.  Returns 0 for
7598944Sobrien   success, non-zero for failure.  */
7698944Sobrien
7798944Sobrienextern int serial_write (struct serial *scb, const char *str, int len);
7898944Sobrien
7998944Sobrien/* Write a printf style string onto the serial port. */
8098944Sobrien
8198944Sobrienextern void serial_printf (struct serial *desc, const char *,...) ATTR_FORMAT (printf, 2, 3);
8298944Sobrien
8346283Sdfr/* Allow pending output to drain. */
8446283Sdfr
8598944Sobrienextern int serial_drain_output (struct serial *);
8619370Spst
8798944Sobrien/* Flush (discard) pending output.  Might also flush input (if this
8898944Sobrien   system can't flush only output).  */
8919370Spst
9098944Sobrienextern int serial_flush_output (struct serial *);
9119370Spst
9298944Sobrien/* Flush pending input.  Might also flush output (if this system can't
9398944Sobrien   flush only input).  */
9419370Spst
9598944Sobrienextern int serial_flush_input (struct serial *);
9698944Sobrien
9719370Spst/* Send a break between 0.25 and 0.5 seconds long.  */
9819370Spst
9998944Sobrienextern int serial_send_break (struct serial *scb);
10019370Spst
10119370Spst/* Turn the port into raw mode. */
10219370Spst
10398944Sobrienextern void serial_raw (struct serial *scb);
10419370Spst
10519370Spst/* Return a pointer to a newly malloc'd ttystate containing the state
10619370Spst   of the tty.  */
10719370Spst
10898944Sobrienextern serial_ttystate serial_get_tty_state (struct serial *scb);
10998944Sobrien
11019370Spst/* Set the state of the tty to TTYSTATE.  The change is immediate.
11119370Spst   When changing to or from raw mode, input might be discarded.
11298944Sobrien   Returns 0 for success, negative value for error (in which case
11398944Sobrien   errno contains the error).  */
11419370Spst
11598944Sobrienextern int serial_set_tty_state (struct serial *scb, serial_ttystate ttystate);
11619370Spst
11798944Sobrien/* printf_filtered a user-comprehensible description of ttystate on
11898944Sobrien   the specified STREAM. FIXME: At present this sends output to the
11998944Sobrien   default stream - GDB_STDOUT. */
12098944Sobrien
12198944Sobrienextern void serial_print_tty_state (struct serial *scb, serial_ttystate ttystate, struct ui_file *);
12298944Sobrien
12319370Spst/* Set the tty state to NEW_TTYSTATE, where OLD_TTYSTATE is the
12419370Spst   current state (generally obtained from a recent call to
12598944Sobrien   serial_get_tty_state()), but be careful not to discard any input.
12698944Sobrien   This means that we never switch in or out of raw mode, even if
12798944Sobrien   NEW_TTYSTATE specifies a switch.  */
12819370Spst
12998944Sobrienextern int serial_noflush_set_tty_state (struct serial *scb, serial_ttystate new_ttystate, serial_ttystate old_ttystate);
13019370Spst
13198944Sobrien/* Set the baudrate to the decimal value supplied.  Returns 0 for
13298944Sobrien   success, -1 for failure.  */
13319370Spst
13498944Sobrienextern int serial_setbaudrate (struct serial *scb, int rate);
13519370Spst
13698944Sobrien/* Set the number of stop bits to the value specified.  Returns 0 for
13798944Sobrien   success, -1 for failure.  */
13819370Spst
13998944Sobrien#define SERIAL_1_STOPBITS 1
14098944Sobrien#define SERIAL_1_AND_A_HALF_STOPBITS 2	/* 1.5 bits, snicker... */
14198944Sobrien#define SERIAL_2_STOPBITS 3
14219370Spst
14398944Sobrienextern int serial_setstopbits (struct serial *scb, int num);
14419370Spst
14598944Sobrien/* Asynchronous serial interface: */
14619370Spst
14798944Sobrien/* Can the serial device support asynchronous mode? */
14819370Spst
14998944Sobrienextern int serial_can_async_p (struct serial *scb);
15019370Spst
15198944Sobrien/* Has the serial device been put in asynchronous mode? */
15219370Spst
15398944Sobrienextern int serial_is_async_p (struct serial *scb);
15419370Spst
15598944Sobrien/* For ASYNC enabled devices, register a callback and enable
15698944Sobrien   asynchronous mode.  To disable asynchronous mode, register a NULL
15798944Sobrien   callback. */
15819370Spst
15998944Sobrientypedef void (serial_event_ftype) (struct serial *scb, void *context);
16098944Sobrienextern void serial_async (struct serial *scb, serial_event_ftype *handler, void *context);
16119370Spst
16298944Sobrien/* Provide direct access to the underlying FD (if any) used to
16398944Sobrien   implement the serial device.  This interface is clearly
16498944Sobrien   deprecated. Will call internal_error() if the operation isn't
16598944Sobrien   applicable to the current serial device. */
16619370Spst
16798944Sobrienextern int deprecated_serial_fd (struct serial *scb);
16819370Spst
16998944Sobrien/* Trace/debug mechanism.
17019370Spst
17198944Sobrien   serial_debug() enables/disables internal debugging.
17298944Sobrien   serial_debug_p() indicates the current debug state. */
17319370Spst
17498944Sobrienextern void serial_debug (struct serial *scb, int debug_p);
17519370Spst
17698944Sobrienextern int serial_debug_p (struct serial *scb);
17798944Sobrien
17898944Sobrien
17998944Sobrien/* Details of an instance of a serial object */
18098944Sobrien
18198944Sobrienstruct serial
18298944Sobrien  {
18398944Sobrien    int fd;			/* File descriptor */
18498944Sobrien    struct serial_ops *ops;	/* Function vector */
18598944Sobrien    void *state;       		/* Local context info for open FD */
18698944Sobrien    serial_ttystate ttystate;	/* Not used (yet) */
18798944Sobrien    int bufcnt;			/* Amount of data remaining in receive
18898944Sobrien				   buffer.  -ve for sticky errors. */
18998944Sobrien    unsigned char *bufp;	/* Current byte */
19098944Sobrien    unsigned char buf[BUFSIZ];	/* Da buffer itself */
19198944Sobrien    int current_timeout;	/* (ser-unix.c termio{,s} only), last
19298944Sobrien				   value of VTIME */
19398944Sobrien    int timeout_remaining;	/* (ser-unix.c termio{,s} only), we
19498944Sobrien				   still need to wait for this many
19598944Sobrien				   more seconds.  */
19698944Sobrien    char *name;			/* The name of the device or host */
19798944Sobrien    struct serial *next;	/* Pointer to the next `struct serial *' */
19898944Sobrien    int refcnt;			/* Number of pointers to this block */
19998944Sobrien    int debug_p;		/* Trace this serial devices operation. */
20098944Sobrien    int async_state;		/* Async internal state. */
20198944Sobrien    void *async_context;	/* Async event thread's context */
20298944Sobrien    serial_event_ftype *async_handler;/* Async event handler */
20398944Sobrien  };
20498944Sobrien
20598944Sobrienstruct serial_ops
20698944Sobrien  {
20798944Sobrien    char *name;
20898944Sobrien    struct serial_ops *next;
20998944Sobrien    int (*open) (struct serial *, const char *name);
21098944Sobrien    void (*close) (struct serial *);
21198944Sobrien    int (*readchar) (struct serial *, int timeout);
21298944Sobrien    int (*write) (struct serial *, const char *str, int len);
21398944Sobrien    /* Discard pending output */
21498944Sobrien    int (*flush_output) (struct serial *);
21598944Sobrien    /* Discard pending input */
21698944Sobrien    int (*flush_input) (struct serial *);
21798944Sobrien    int (*send_break) (struct serial *);
21898944Sobrien    void (*go_raw) (struct serial *);
21998944Sobrien    serial_ttystate (*get_tty_state) (struct serial *);
22098944Sobrien    int (*set_tty_state) (struct serial *, serial_ttystate);
22198944Sobrien    void (*print_tty_state) (struct serial *, serial_ttystate,
22298944Sobrien			     struct ui_file *);
22398944Sobrien    int (*noflush_set_tty_state) (struct serial *, serial_ttystate,
22498944Sobrien				  serial_ttystate);
22598944Sobrien    int (*setbaudrate) (struct serial *, int rate);
22698944Sobrien    int (*setstopbits) (struct serial *, int num);
22798944Sobrien    /* Wait for output to drain */
22898944Sobrien    int (*drain_output) (struct serial *);
22998944Sobrien    /* Change the serial device into/out of asynchronous mode, call
23098944Sobrien       the specified function when ever there is something
23198944Sobrien       interesting. */
23298944Sobrien    void (*async) (struct serial *scb, int async_p);
23398944Sobrien  };
23498944Sobrien
23598944Sobrien/* Add a new serial interface to the interface list */
23698944Sobrien
23798944Sobrienextern void serial_add_interface (struct serial_ops * optable);
23898944Sobrien
23919370Spst/* File in which to record the remote debugging session */
24019370Spst
24198944Sobrienextern void serial_log_command (const char *);
24219370Spst
24319370Spst#endif /* SERIAL_H */
244