/*
 * serial header file.
 *
 * Copyright 2000 by Gray Watson
 *
 * This file is part of the serial-util package.
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose and without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies, and that the name of Gray Watson not be used in advertising
 * or publicity pertaining to distribution of the document or software
 * without specific, written prior permission.
 *
 * Gray Watson makes no representations about the suitability of the
 * software described herein for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * The author may be contacted at http://256.com/gray/
 *
 * $Id: serial.h,v 1.23 2000/03/09 03:49:06 gray Exp $
 */

#include <stdio.h>
#include <sys/time.h>			/* for timeval */

#ifndef __SERIAL_H__
#define __SERIAL_H__

/* log routines file-pointer types */
#define SERIAL_LOG_INPUT	1	/* log input file pointer */
#define SERIAL_LOG_OUTPUT	2	/* log output file pointer */
#define SERIAL_LOG_DEBUG	3	/* log debug file pointer */

/*
 * used to replace %? in scripts.  If ? matches sa_token then replace
 * with sa_value.
 */
typedef struct {
  char		sa_token;		/* character to match with %? */
  char		*sa_value;		/* value to replace with */
} serial_arg_t;

/*
 * file types for serial_write argument
 */
#define SERIAL_WRITE_NORMAL	0	/* normal output file */
#define SERIAL_WRITE_BINARY	1	/* output in binary format */
#define SERIAL_WRITE_LOCKED	2	/* output a locked binary file */

/*
 * serial error codes
 */
#define SERIAL_ERROR_NONE	1	/* no error */
#define SERIAL_ERROR_ARG	2	/* improper arg to function */
#define SERIAL_ERROR_ALLOC	3	/* problem allocating memory */
#define SERIAL_ERROR_LOCKED	4	/* input file is locked */
#define SERIAL_ERROR_DEVREAD	5	/* could not read from device */
#define SERIAL_ERROR_DEVWRITE	6	/* could not write to device */
#define SERIAL_ERROR_SCRREAD	7	/* could not read from script */
#define SERIAL_ERROR_SCRWRITE	8	/* could not write to script */
#define SERIAL_ERROR_TIMEOUT	9	/* waiting for input timed out */
#define SERIAL_ERROR_STTY	10	/* improper stty token */
#define SERIAL_ERROR_MODES	11	/* could not set new stty modes */
#define SERIAL_ERROR_FLUSH	12	/* could not flush device */
#define SERIAL_ERROR_LOGSLOT	13	/* no log file slots open */
#define SERIAL_ERROR_REMOVE	14	/* tried to remove unknown log */
#define SERIAL_ERROR_BINARY	15	/* bad token from binary script */
#define SERIAL_ERROR_SCRIPT	16	/* problem with plain script */
#define SERIAL_ERROR_REGCOMP	16	/* could not compie regex */
#define SERIAL_ERROR_LABEL	17	/* tried to jump to unknown label */
#define SERIAL_ERROR_LOCK	18	/* could not lock port */
#define SERIAL_ERROR_UNLOCK	19	/* could not unlock port */

#ifndef SERIAL_MAIN

typedef void	serial_t;

#else

#include "serial_loc.h"

#endif

/*<<<<<<<<<<  The below prototypes are auto-generated by fillproto */

/*
 * Start the transactions with a serial port opened for input with
 * IN_FD and output with OUT_FD, attached to PATH (which can be null).
 * Returns pointer to new struct or NULL on error.  Passes back serial
 * error codes in ERROR_P.
 */
extern
serial_t	*serial_start(const int in_fd, const int out_fd,
			      const char *path, int *error_p);

/*
 * End transactions with a serial device in SERIAL_P.  Returns serial
 * error codes.
 */
extern
int	serial_end(serial_t *serial_p);

/*
 * Set with stty-style COMMAND for serial line opened with IN_FD. if
 * EXCL then clear all other flags before setting this command.
 * Returns serial error codes.
 */
extern
int	serial_stty(serial_t *serial_p, const char *command, const char excl);

/*
 * Load a script from INFILE into SERIAL_P buffer in preparation for a
 * serial_run.  ARGS are used to insert strings into the
 * script. Returns serial error codes.
 */
extern
int	serial_load(serial_t *serial_p, FILE *infile,
		    const serial_arg_t *args);

/*
 * Write a script from SERIAL_P to OUTFILE possibly in BINARY format
 * and possible LOCKED so it cannot be re-written.  Returns serial
 * error codes.
 */
extern
int	serial_write(serial_t *serial_p, FILE *outfile, const int type);

/*
 * Run a script on IN_FD from file opened to FILE_P starting at LABEL
 * (if NULL then the top) this passes back an error CODE_P and static
 * string message in STR_P (if either not NULL).  Returns serial error
 * codes.
 */
extern
int	serial_script(serial_t *serial_p, const char *label,
		      int *code_p, char **str_p);

/*
 * Lock via uucp protocols the path associated with SERIAL_P.  Returns
 * serial error codes.
 */
extern
int	serial_lock(serial_t *serial_p);

/*
 * Unlock via uucp protocols the path associated with SERIAL_P.
 * Returns serial error codes.
 */
extern
int	serial_unlock(serial_t *serial_p);

/*
 * Lock via uucp protocols the PORT.  PORT should contain no special
 * characters, and not overlap with the name of any device that may be
 * locked in the uucp lock area.  Returns serial error codes.
 */
extern
int	serial_lock_port(const char *port);

/*
 * Unlock via uucp protocols the PORT.  PORT should contain no special
 * characters, and not overlap with the name of any device that may be
 * locked in the uucp lock area.  Returns serial error codes.
 */
extern
int	serial_unlock_port(const char *port);

/*
 * Set the timeout value for SERIAL_P.  Set tv_sec to -1 to disable
 * the timeout.  Returns serial error codes.
 */
extern
int	serial_timeout(serial_t *serial_p, struct timeval *timeout_p);

/*
 * Add a file-pointer FILE_P of WHICH type (see serial.h) to the serial
 * connection in SERIAL_P.  Returns serial error codes.
 */
extern
int	serial_log_add(const serial_t *serial_p, const int which, FILE *file_p);

/*
 * Remove a file-pointer FILE_P of WHICH type (see serial.h) from the
 * serial connection in SERIAL_P.  Returns serial error codes.
 */
extern
int	serial_log_remove(const serial_t *serial_p, const int which,
			  FILE *file_p);

/*
 * Log a printf like message to SERIAL_P's log entries of WHICH type
 * (see serial.h) using FORMAT and ...  Returns serial error codes.
 *
 * NOTE: this depends on the log_open/close status from the script._
 */
extern
int	serial_log_printf(const serial_t *serial_p, const int which,
			  const char *format, ...);

/*
 * Log a printf like message to SERIAL_P's log entries of WHICH type
 * (see serial.h) using FORMAT and ...  Returns serial error codes.
 *
 * NOTE: this depends on the log_open/close status from the script._
 */
extern
int	serial_log_fwrite(const serial_t *serial_p, const int which,
			  const char *buf, const int size, const int count);

/*
 * Return the string equivalent of serial ERROR.
 */
extern
char	*serial_strerror(const int error);

/*<<<<<<<<<<   This is end of the auto-generated output from fillproto. */

#endif /* ! __SERIAL_H__ */

