#ifndef __TERMIOS_H__
#define __TERMIOS_H__

/*
 * Edit History
 *
 * Date		Name			Change Description
 * 7/7/04	JZW				Remove dangling commas. These cause compiler warnings using
 *                          the c++ compiler
 */
 
#if __cplusplus
extern "C" {
#endif

/**
 * Bit field type
 *
 * Variables of this type are used as bit fields where individual bits
 * indicate whether a mode or option is selected.
 *
 * @external
 * @category Serial
 * @since 6.0
 */
typedef unsigned long tcflag_t;

/**
 * Used to hold control characters
 *
 * @external
 * @category Serial
 * @since 6.0
 */
typedef unsigned long cc_t;

/**
 * Serial Speed Type
 *
 * @external
 * @category Serial
 * @since 6.0
 */
typedef unsigned long speed_t;

/**
 * A serial port driver is supplied as part of the BSP. This device driver
 * provides support for 2 RS-232 serial ports implemented in the Net+ARM
 * chip. 
 *
 *
 * The Net+ARM chip supports two serial ports, both of which are physically
 * present on the Net+Works development boards. The names of the serial ports
 * are set in the file devices.c in the platform directory.  By default, the 
 * ports are named: /com/0 and /com/1.  The constants BSP_SERIAL_PORT_1 and  
 * BSP_SERIAL_PORT_2 be set to BSP_SERIAL_UART_DRIVER in bsp.h to build the 
 * driver for the two serial ports.
 *
 *
 * Interrupts 12-15 and DMA channels 7-10 are used to support the serial
 * ports.  DMA is automatically used by the driver when high baud rates
 * are selected.
 *
 *
 * Serial ports are accessed using the C Library open, read, write, and close
 * functions.  For example, the following statement would open the first
 * serial port for read/write access:  
 * <example>
 *      handle = open("/com/0", O_RDWR);
 * </example>
 * Applications can use the following constants for the mode filed in open:
 * <list type="unorderd">
 *      <item>O_RDONLY open for reading only</item>
 *      <item>O_WRONLY open for writing only</item>
 *      <item>O_RDWR open for reading and writing</item>
 *      <item>O_NON_BLOCK open for non-blocking</item>
 * </list>
 *
 *
 * The API used to control and configure serial ports based on the standard 
 * termios API used in Linux systems.  Serial port configurations are stored
 * in struct termios structures.  Your application should configure serial
 * ports by doing the following:
 *
 * <list type="ordered">
 *  <item>Call open() to open a port</item>
 *  <item>Call tcgetattr() to read the port's current configuration 
 *        into a termios structure</item>
 *  <item>Update the fields in the termios structure with the desired 
 *        configuration</item>
 *  <item>Call tcsetattr() to set the new configuration</item>
 * </list>
 *
 * The constant BSP_SERIAL_PORT_API in bsp.h must be set to 
 * BSP_SERIAL_API_TERMIOS to use the standard API. 
 *
 * @external
 * @category Serial
 * @since 1.0
 * @overview Serial
 */


/**
 * When this value is used in an entry of the c_cc array, that entry 
 * is disabled
 *
 * @since 6.0
 * @external
 * @category Serial
 */
#define _VDISABLE 0

/**
 * Array subscripts of configuration characters in the c_cc array.
 *
 * @since 6.0
 * @external
 * @category Serial
 *
 * @param VSTART	The value to be used for the XON character
 * @param VSTOP		The value to be used for the XOFF character
 * @param VLNEXT	Escape character that turns off any special character
 *			processing for the next character in the input stream.
 *			Currently unsupported
 * @param VXSTART	Additional XON character.  Currently unsupported.
 * @param VXSTOP	Additional XOFF character. Currently unsupported
 * @param VPREDELAY	The delay to have RTS raised before transmitting data.
 *			In system tics.
 * @param VPOSTDELAY	The delay to have RTS raised after transmission is 
 *			complete.  In system tics
 * @param NCCS		The number of configuration characters
 */
enum ccvals
{
    VSTART,
    VSTOP,
    VLNEXT,
    VXSTART,
    VXSTOP,
    VPREDELAY,
    VPOSTDELAY,
    NCCS
};

/* Arguments for tcsetattr() */
/**
 * These option arguments determine the behavior of tcsetattr().
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcsetattr
 * 
 * @param TCSANOW	Set attributes should occur immediately.
 * @param TCSADRAIN	Set attributes will occur after all output has been
 *			transmitted.
 * @param TCSAFLUSH	Set attributes will occur after all output has been
 *			transmitted.  Also, all data not read at that time will
 *			be discarded.
 */
enum opt_actions
{
    TCSANOW,
    TCSADRAIN,
    TCSAFLUSH
};

/* Arguments for tcflush */
/**
 * These values determine the behavior of a call to tcflush()
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcflush
 *
 * @param TCIFLUSH	The input buffers are flushed
 * @param TCOFLUSH	The output buffers are flushed
 * @param TCIOFLUSH	Both input and output buffers are flushed
 */
enum flush_opts
{
    TCIFLUSH  = 0x1,
    TCOFLUSH  = 0x2,
    TCIOFLUSH = 0x3 /* TCIFLUSH | TCOFLUSH */
};

#if defined(AUTODOC_ONLY)
/**
 * These values determine the behavior of a call to tcflow()
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcflow
 *
 * @param TCOOFF	Output is paused
 * @param TCOON		Output that was paused is resumed
 * @param TCIOFF	The system requests that the remote side of the serial
 *			connection stop transmitting data.
 * @param TCION		The system requests that the remote side of the serial
 *			connection resume transmitting data.
 */
enum flow_opts
{
    TCOOFF,
    TCOON,
    TCIOFF,
    TCION
};
#endif /* AUTODOC_ONLY */

enum flow_opts
{
    TCOOFF,
    TCOON,
    TCIOFF,
    TCION,
/* Following values are currently not supported in the NetOS driver */
    TCOSOFF,
    TCOSON,
    TCISOFF,
    TCISON,
    TCOXOFF,
    TCOXON,
    TCIXOFF,
    TCIXON
};
/* Modem bitmask defines */
/**
 * These values are returned in the mstat member of a call to tcgetstats() 
 * as well as being used to control outgoing signals with tcsetsignals().
 *
 * @since 6.0
 * @external
 * @category Serial
 * @name SignalDefines "Modem signal definitions"
 * @see @link tcgetstats
 * @see @link tcsetsignals
 * 
 * @param MS_DTR	Data Terminal Ready
 * @param MS_RTS	Request to Send
 * @param MS_CTS	Clear to Send
 * @param MS_DSR	Data Set Ready
 * @param MS_RI		Ring Indicator
 * @param MS_DCD	Data Carrier Detect
 * @param MS_MDMOUT	Outgoing modem signals bitmask
 * @param MS_MDMIN	Incoming modem signals bitmask
 */
#define MS_DTR          0x01            //!< Data Terminal Ready
#define MS_RTS          0x02            //!< Request to Send
#define MS_CTS          0x10            //!< Clear to send 
#define MS_DSR          0x20            //!< Data Set Ready
#define MS_RI           0x40            //!< Ring Indicator
#define MS_DCD          0x80            //!< Data Carrier Detect
#define MS_MDMOUT       (MS_DTR | MS_RTS)
#define MS_MDMIN        (MS_CTS | MS_DSR | MS_RI | MS_DCD)

/* not used for autodoc */
#define MF_RTSTOG       0x04            //!< RTS Toggle flow control-do not use
#define MF_RIPOWER		0x08			//!< RI Power output - do not use
#define MS_OPALL        0xf0            //!< CTS,DSR,RI,DCD

/* termios flags */
/**
 * Bitmask values for the c_cflag member of the termios structure
 *
 * @since 6.0
 * @external
 * @category Serial
 * @name cflag_vals "cflag values"
 * @see @link termios
 * 
 * @param CSIZE		Bitmask of bits used for character size selection
 * @param CS5		5 bits/character
 * @param CS6		6 bits/character
 * @param CS7		7 bits/character
 * @param CS8		8 bits/character
 *
 * @param CSTOPB	Use two stop bits instead of one.
 * @param CREAD		Enable receiver.  The receiver is currently always 
 *			enabled making this flag unnecessary.
 * @param PARENB	Enable parity generation.  If PARODD is set, odd parity
 *			will be set, otherwise even parity. When used in 
 *		conjunction with the XPAR flag, PARODD selects Mark
 *		parity, otherwise space parity.
 * @param PARODD	Odd parity.  Mark parity when XPAR is used
 *
 * @param CMFLOW	Bitmask of bits used for hardware flow control and
 *			modem signal line behavior selection.
 * @param CDTR		Use DTR for input flow control - Currently unsupported
 * @param CRTS		Use RTS for input flow control
 * @param CRTSTOGGLE	Encapsulate transmitted data with RTS
 * @param CCTS		Use CTS for output flow control
 * @param CDSR		Use DSR for output flow control - Currently unsupported
 * @param CRI		Use RI for output flow control - Currently unsupported
 * @param CDCD		Use DCD for output flow control - Currently unsupported
 *
 * @param CRTSCTS	Convenience value to set CRTS and CCTS for traditional
 *			hardware flow control
 * @param CCTS_OFLOW	Alias for CCTS
 * @param CRTS_IFLOW	Alias for CRTS
 */
#define CSIZE        0x0030          //!< Character size
#define CS5          0x0000          //!< 5 bits/character
#define CS6          0x0010          //!< 6 bits/character
#define CS7          0x0020          //!< 7 bits/character
#define CS8          0x0030          //!< 8 bits/character
#define CSTOPB       0x0040          //!< 2 stop bits
#define CREAD        0x0080          //!< Enable read (not in XK)
#define PARENB       0x0100          //!< Parity enable
#define PARODD       0x0200          //!< Odd (space) parity
#define CDTR	     (MS_DTR << MFLOW_SHIFT)
#define CRTS	     (MS_RTS << MFLOW_SHIFT)
#define CRTSTOGGLE   (MF_RTSTOG << MFLOW_SHIFT)
#define CCTS	     (MS_CTS << MFLOW_SHIFT)
#define CDSR	     (MS_DSR << MFLOW_SHIFT)
#define CRI          (MS_RI << MFLOW_SHIFT)
#define CDCD         (MS_DCD << MFLOW_SHIFT)
#define CRTSCTS      (CRTS | CCTS)
#define CCTS_OFLOW   (CCTS)
#define CRTS_IFLOW   (CRTS)
#define CMFLOW       0x00ff0000

/* not used in autodoc */
#define HUPCL        0x0400          //!< Hang up on last close(not used)
#define CLOCAL       0x0800          //!< Ignore DCD transitions(not used)
#define CRIPOWER     (MF_RIPOWER << MFLOW_SHIFT)
#define MFLOW_SHIFT  16
#define CMDMOUT	     (CRTS | CCTS)
#define CMDMIN       (CCTS | CDSR | CRI | CDCD)

#if defined(AUTODOC_ONLY)
/**
 * Bitmask values for the c_xflag member of the termios structure
 *
 * @since 6.0
 * @external
 * @category Serial
 * @name xflag_vals "xflag values"
 * @see @link termios
 * 
 * @param XPAR		Enable mark/space parity
 * @param XEDATA	All error data is left unmodified in the data stream 
 *			- currently unsupported
 * @param XTOSS		When used with IXANY flag in iflags, discard the 
 *			character which resumed transmission. Currently 
 *			unsupported.
 * @param XIXON		Enable recognition of VXSTART and VXSTOP characters in
 *			the data stream.  Currently unsupported.
 * @param XFORCEDCD	DCD always reads active
 */
#define XPAR         0x0001          //!< Enable mark/space parity (TODO in XK)
#define XEDATA       0x0008          //!< Error data is unmodified (NI in XK)
#define XTOSS        0x0040          //!< Toss IF_IXANY character (NI in XK)
#define XIXON        0x2000          //!< Enable xtra flow control characters
#define XFORCEDCD    0x0200	     // DCD always reads high
#endif

#define XPAR         0x0001          //!< Enable mark/space parity (TODO in XK)
#define XMODEM       0x0002          //!< Use in-band modem signalling (NI)
#define XCASE        0x0004          //!< Special output char conversion (NI)
#define XEDATA       0x0008          //!< Error data is unmodified (NI in XK)
#define XTOSS        0x0040          //!< Toss IF_IXANY character (NI in XK)

/* Not in XK */
#define XIXON        0x2000          //!< Enable xtra flow control characters
#define XALTPIN      0x0100          // Swap DSR and DCD
#define XFORCEDCD    0x0200	     // DCD always reads high

/**
 * Bitmask values for the c_iflag member of the termios structure
 *
 * @since 6.0
 * @external
 * @category Serial
 * @name iflag_vals "iflag values"
 * @see @link termios
 *
 * @param IGNBRK	Ignore breaks in incoming data - Currently unsupported
 * @param BRKINT	Signal when a break occurs - Currently unsupported
 * @param IGNPAR	Ignore parity errors - Currently unsupported
 * @param PARMRK	Mark parity errors with 0xff 0x00 <character> -
 *			currently unsupported
 * @param INPCK		Enable input parity checking - Currently unsupported. 
 *			This does not mean that parity isn't checked. The error
 *			counters will still increment when a parity error 
 *			occurs.  
 * @param ISTRIP	Strip characters to 7 bits
 * @param IXON		Enable recognition of VSTART/VSTOP flow control chars
 * @param IXANY		Restart output on any character - Currently unsupported
 * @param IXOFF		Enable generation of VSTART/VSTOP flow control chars
 * @param DOSMODE	Provide 16450 error data with PARMRK - Currently 
 *			unsupported
 */
#define IGNBRK       0x0001          //!< Ignore received break (NI in XK)
#define BRKINT       0x0002          //!< Break interrupt (NI in XK)
#define IGNPAR       0x0004          //!< Ignore parity (NI in XK)
#define PARMRK       0x0008          //!< Mark parity (not possible in XK)
#define INPCK        0x0010          //!< Enable input parity checking (TODO)
#define ISTRIP       0x0020          //!< Strip characters to 7 bits (NI in XK)
#define IXON         0x0400          //!< Enable output flow control chars
#define IXANY        0x0800          //!< Restart output on any character (NXK)
#define IXOFF        0x1000          //!< Enable input flow control chars
#define DOSMODE      0x8000          //!< Provide 16450 error data (NI in XK)

/**
 * Bitmask values for the c_oflag member of the termios structure.  These flags
 * control rewriting of the output data stream.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @name oflag_vals "oflag values"
 * @see @link termios
 *
 * @param ONLCR		Newlines in data stream become CR-NL 
 * @param OCRNL		Carriage returns in data stream become CR-NL - 
 *			Currently unsupported
 * @param ONOCR		Strip carriage returns that occur in column 0 -
 *			Currently unsupported
 * @param ONLRET	A newline is also interpreted as a carriage return
 *			by the receiving terminal - Currently unsupported
 * @param TABDLY	Expand tabs to 8-character tab stops - Currently
 *			unsupported
 */
#define ONLCR        0x0004          //!< Newline to CRNL 
#define OCRNL        0x0008          //!< CR to CRNL (NI in XK)
#define ONOCR        0x0010          //!< No CR in column 0 (NI in XK)
#define ONLRET       0x0020          //!< Newline-return (NI in XK)
#define TABDLY       0x1800          //!< Tab handling (NI in XK)

/* not in autodoc */
#define OLCUC        0x0002          //!< Lower case to upper case (NI in XK)
#define TAB0         0x0000          //!< Tabs straight through (NI)
#define TAB3         0x1800          //!< Expand tabs (NI)

/**
 * These definitions define events that the serial driver may report.  The 
 * event status may be checked with the tcgetstats() function call.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @name event_flags "Event Flags"
 * @see @link tcgetstats
 * 
 * @param EV_OPU	Output paused by client
 * @param EV_OPS	Output paused by reqular software flow control
 * @param EV_OPX	Output paused by extra software flow control - 
 *			Currently unsupported
 * @param EV_OPH	Output paused by hardware flow control
 * @param EV_OPT	Output paused for RTS toggle predelay
 *
 * @param EV_IPU	Input paused unconditionally by user
 * @param EV_IPS	Input paused by high/low water marks
 *
 * @param EV_TXB	Transmit break pending - Currently unsupported
 * @param EV_TXI	Transmit immediate pending
 * @param EV_TXF	Transmit flowctrl char pending - Currently unsupported
 * @param EV_RXB	Break received - Currently unsupported
 *
 * @param EV_OPALL	Bitmask of all output pause flags
 * @param EV_IPALL	Bitmask of all input pause flags
 */
#define EV_OPU          0x0001       //!<Output paused by client
#define EV_OPS          0x0002       //!<Output paused by reqular sw flowctrl
#define EV_OPX          0x0004       //!<Output paused by extra sw flowctrl
#define EV_OPH          0x0008       //!<Output paused by hw flowctrl
#define EV_OPT		0x0800	     //!<Output paused for RTS predelay
#define EV_IPU          0x0010       //!<Input paused unconditionally by user
#define EV_IPS          0x0020       //!<Input paused by high/low water marks
#define EV_TXB          0x0040       //!<Transmit break pending
#define EV_TXI          0x0080       //!<Transmit immediate pending
#define EV_TXF          0x0100       //!<Transmit flowctrl char pending
#define EV_RXB          0x0200       //!<Break received
#define EV_OPALL        0x080f       //!<Output pause flags
#define EV_IPALL        0x0030       //!<Input pause flags

//#define EV_IPH		0x0040       //!<Input paused w/ hardware

/**
 * This structure is used as an argument to tcgetattr(), tcsetattr() and the
 * baud rate functions.  It holds all of the configuration information to 
 * govern the behavior of the serial port.
 *
 * @param c_iflag	input modes
 * @param c_oflag	output modes
 * @param c_cflag	control modes
 * @param c_lflag	local modes
 * @param c_xflag	Xtra flags
 * @param c_cc		control chars
 * @param baud		baud rate
 *
 * @since 6.0
 * @external
 * @category Serial
 *
 * @see @link tcgetattr
 * @see @link tcsetattr
 * @see @link cflag_vals
 * @see @link oflag_vals
 * @see @link iflag_vals
 * @see @link xflag_vals
 * 
 */
struct termios
{
    tcflag_t c_iflag;      /* input modes */
    tcflag_t c_oflag;      /* output modes */
    tcflag_t c_cflag;      /* control modes */
    tcflag_t c_lflag;      /* local modes */
    tcflag_t c_xflag;	   /* Xtra flags */
    cc_t c_cc[NCCS];       /* control chars */
    speed_t baud;
};

/* Struct for holding buffer use counts */
/**
 * This structure is used to return information from the tcgetbuffers() 
 * function.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcgetbuffers
 * 
 * @param rxbuf	"Amount of data currently in the receive buffer"
 * @param txbuf "Amount of data currently in the transmit buffer"
 */
struct serial_buffer_t
{
    unsigned long rxbuf;
    unsigned long txbuf;
};

/**
 * This structure is used to return data from the tcgetcounters() function.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcgetcounters
 * 
 * @param norun        number of UART overrun errors
 * @param noflow       number of buffer overflow errors
 * @param nframe       number of framing errors
 * @param nparity      number of parity errors
 * @param nbreak       number of breaks received
 * 
 * @param rbytes       number of received bytes
 * @param tbytes       number of bytes transmitted fully
 */
struct serial_cntr_t
{
    unsigned long norun;
    unsigned long noflow;
    unsigned long nframe;
    unsigned long nparity;
    unsigned long nbreak;

    unsigned long rbytes;
    unsigned long tbytes;
};

/**
 * This structure is used to return the results of a call to the tcgetstats()
 * function.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcgetstats
 * @see @link event_flags
 * @see @link SignalDefines
 * 
 * @param estat		Active events
 * @param  mstat	Active modem signals
 */
struct serial_status_t
{
    unsigned short estat;
    unsigned char  mstat;
};

/* SNMP signal change statistics */
/* SNMP counters for measuring the number of times CTS, DSR, RI, DCD,
 *  RTS, and DTR signal changes from 'on' to 'off' or from 'off' to 'on'
 */
/**
 * This structure is used to return the results of a call to the 
 * tcgetsigchange() function.  The members are counters of the number of times
 * that each signal has transitioned.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link tcgetsigchange
 * 
 * @param cts_counter	CTS transitions
 * @param dsr_counter	DSR transitions
 * @param ri_counter	RI transitions
 * @param dcd_counter	DCD transitions
 * @param rts_counter	RTS transitions
 * @param dtr_counter	DTR transitions
 */
struct sigchange_stats_t
{ 
    unsigned long cts_counter;          
    unsigned long dsr_counter;  
    unsigned long ri_counter;           
    unsigned long dcd_counter;  
    unsigned long rts_counter;  
    unsigned long dtr_counter;  
};

/* Normal termios functions */
/**
 * This function retrieves the current settings of the serial port.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link termios
 * 
 * @param fd		The file descriptor of the port
 * @param termios_p	A pointer to an instance of the termios structure where
 *			the settings will be stored
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcgetattr(int fd, struct termios * termios_p);

/**
 * This function changes the current settings of the serial port
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link termios
 * @see @link opt_actions
 * 
 * @param fd			The file descriptor of the port
 * @param optional_actions	Parameter that determines when the changes
 *				should take effect.  See the optional arguments
 *				link for more information
 * @param termios_p		A pointer to an instance of the termios 
 *				structure where the settings to be set are
 *				stored
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcsetattr(int fd, int optional_actions, struct termios * termios_p);

/**
 * This function causes a break condition to be generated on the serial line.
 * This functionality is currently unsupported
 *
 * @since 6.0
 * @external
 * @category Serial
 * 
 * @param fd			The file descriptor of the port
 * @param duration		The amount of time the break condition should
 *				be maintained
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcsendbreak(int fd, int duration);

/**
 * This function blocks until the serial port pointed to by the fd argument
 * has transmitted all data in its transmit buffer.
 *
 * @since 6.0
 * @external
 * @category Serial
 * 
 * @param fd			The file descriptor of the port
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcdrain(int fd);

/**
 * This function causes the data in the serial buffers to be discarded.  Which 
 * data buffer that is discarded depends on the arguments
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link flush_opts
 * 
 * @param fd			The file descriptor of the port
 * @param queue_selector	An argument from flush_opts that selects the
 *				buffer to be flushed.
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcflush(int fd, int queue_selector);

/**
 * This function allows the application to control the flow of data over the 
 * serial port.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link flow_opts
 * 
 * @param fd			The file descriptor of the port
 * @param action		The flow control action to perform
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcflow(int fd, int action);

/* Additional functions */

/**
 * This function allows the application to send character ahead of buffered 
 * data into the UART.  When called, the argument to this function will be the
 * next character placed in the UART regardless of the state of the transmit
 * buffer.
 *
 * @since 6.0
 * @external
 * @category Serial
 * 
 * @param fd			The file descriptor of the port
 * @param c			The character to transmit 
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcsendnow(int fd, char c);

/**
 * This function returns the internal counters maintained by the serial port.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link serial_cntr_t
 * 
 * @param fd			The file descriptor of the port
 * @param cntrs			A pointer to a structure of type serial_cntr_t
 *				where the results will be stored
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcgetcounters(int fd, struct serial_cntr_t * cntrs);

/**
 * This function returns a structure that represents the current state of
 * events and modem signals for the serial port.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link serial_status_t
 * 
 * @param fd			The file descriptor of the port
 * @param stats			A pointer to a structure of type 
 *				serial_status_t where the results will be
 *				stored
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcgetstats(int fd, struct serial_status_t * stats);

/**
 * This function returns a structure that keeps track of the number of times
 * that signals have changed state.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link sigchange_stats_t
 * 
 * @param fd			The file descriptor of the port
 * @param sigchange		A pointer to a structure of type 
 *				sigchange_stats_t where the results will be
 *				stored
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcgetsigchange(int fd, struct sigchange_stats_t * sigchange);

/**
 * This function allows the application to modify the state of the outgoing
 * signals DTR and RTS.  The function will not modify the state of an outgoing
 * signal if the signal is already in use for a predefined functionality such
 * as hardware flow control or transmit encapsulation.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link SignalDefines
 * 
 * @param fd			The file descriptor of the port
 * @param mout			A bitmask of modem signals that specifies the
 *				desired state of RTS and DTR in the MS_RTS and
 *				MS_DTR bit positions
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcsetsignals(int fd, unsigned char mout);

/**
 * This function allows the application to query the state of the serial ports
 * receive and transmit buffers.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link serial_buffer_t
 * 
 * @param fd			The file descriptor of the port
 * @param bufs			A pointer to a structure of type 
 *				serial_buffer_t where the results will be
 *				stored
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tcgetbuffers(int fd, struct serial_buffer_t * bufs);

/**
 * This function allows the application to determine if any write operation
 * is currently in progress.
 *
 * @since 6.0
 * @external
 * @category Serial
 * 
 * @param fd			The file descriptor of the port
 * @param done			A pointer to an integer, on successful return
 *				from the function call, the integer will be 1
 *				if the serial port is done transmitting, 0 
 *				otherwise
 * @return  0 Success
 * @return -1 Failure, application needs to call getErrno() to get errno
 */
int tctxdone(int fd, int * done);

/* Functions that modify termios structure */

/**
 * Retrieves the output speed specified by the termios_p structure.  The 
 * driver does not allow asymmetrical data speeds so this will return the 
 * current speed for both directions of data transfer.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link termios
 * 
 * @param termios_p		A pointer to an instance of the termios 
 *				structure where the settings to be retrieved from
 * @return  >0  speed of serial link
 */
speed_t cfgetospeed(struct termios * termios_p);

/**
 * Sets the output speed specified by the termios_p structure.  The 
 * driver does not allow asymmetrical data speeds so this will return the 
 * current speed for both directions of data transfer.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link termios
 * 
 * @param termios_p	A pointer to an instance of the termios structure where
 *			the setting will be stored
 * @param speed			The output speed
 * @return  0 Serial link speed is successfully set
 */
int cfsetospeed(struct termios * termios_p, speed_t speed);

/**
 * Retrieves the input speed specified by the termios_p structure.  The 
 * driver does not allow asymmetrical data speeds so this will return the 
 * current speed for both directions of data transfer.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link termios
 * 
 * @param termios_p		A pointer to an instance of the termios 
 *				structure where the settings to be retrieved from
 * @return  >0 Speed of the serial link
 */
speed_t cfgetispeed(struct termios * termios_p);

/**
 * Sets the input speed specified by the termios_p structure.  The 
 * driver does not allow asymmetrical data speeds so this will set the 
 * current speed for both directions of data transfer.
 *
 * @since 6.0
 * @external
 * @category Serial
 * @see @link termios
 * 
 * @param termios_p	A pointer to an instance of the termios structure where
 *			the setting will be stored
 * @param speed			The input speed
 * @return  0 Serial link speed is successfully set
 */
int cfsetispeed(struct termios * termios_p, speed_t speed);

#define SERIAL_MAX 4 /* The most any NS product has is four ports */

#if __cplusplus
}
#endif

#endif /* __TERMIOS_H__ */
