/*
** (c) 1992 Uwe C. Schroeder, Anwendungssysteme , Augsburg, Germany
**
** 
** Permission to use, copy, and modify this software and its
** documentation for non comercial  purpose is hereby granted 
** without fee, provided that
** the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation, and that the name of Uwe C. Schroeder not be used in
** advertising or publicity pertaining to distribution of the software without
** specific, written prior permission.  Uwe Schroeder makes no representations
** about the suitability of this software for any purpose.  It is provided
** "as is" without express or implied warranty.
** This software may not be sold or distributed with commercial products
** ( this includes distribution "for users convenience" ) without prior
** written permission by the author.
**
** UWE SCHROEDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
** INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
** EVENT SHALL UWE SCHROEDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
** DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
** TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
** PERFORMANCE OF THIS SOFTWARE.
**
**
** This is a alpha release, which means, that there are still several
** known bugs.
**
** Please report bugs to:
**
**                     usys@phoenix.abg.sub.org
**
*/

/* Routines for opening the serial port tty  in various raw modes 
   routines using sgtty struct are in tty.c 
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <signal.h>
#include <fcntl.h>
#include <termio.h>
#include "vfax.h"

extern void exit();
extern int local;
struct termio tty_mode;
extern int fd;

#ifndef Linux
int
tcdrain()
{
  if(ioctl(fd,TCSBRK,1) <0)
    return -1;
  return 0;
}
#endif

int
tcflushin()
{
  if(ioctl(fd,TCFLSH,0) <0)
    return -1;
  return 0;
}

int
tcflushout()
{
  if(ioctl(fd,TCFLSH,1) <0)
    return -1;
  return 0;
}

#ifndef Linux
int
tcflush()
{
  if(ioctl(fd,TCFLSH,2) <0)
    return -1;
  return 0;
}
#endif

int
tcdroprts()
{
  if(ioctl(fd,TCRTS,0) <0)
    return -1;
  return 0;
}


int
tcriserts()
{
  if(ioctl(fd,TCRTS,1) <0)
    return -1;
  return 0;
}

int
tty_set_reenable()
{
  struct termio t;

  if(ioctl(fd,TCGETA,&t) <0)
    {
      perror("TCGETA: reenable"); 
      return -1;
    }
  t.c_cflag |= REENABLE;
  if(ioctl(fd,TCSETA,&t) <0)
    {
      perror("TCSETA: reenable"); 
      return -1;
    }
  return 0;
}

int
reset_tty()
{
  struct termio t;

  if(ioctl(fd,TCGETA,&t) <0)
    {
      return -1;
    }
  t.c_cflag |= CLOCAL;

  if(ioctl(fd,TCSETA,&t) <0)
    {
      return -1;
    }

  t.c_cflag &= ~CLOCAL;

  if(ioctl(fd,TCSETA,&t) <0)
    {
      return -1;
    }

  logto(4,"Reset TTY succeded\n");
  return 0;
}

int
drop_rts()
{
  struct termio t;

  if(ioctl(fd,TCGETA,&t) <0)
    {
      return -1;
    }
  t.c_cflag |= CLOCAL;

  if(ioctl(fd,TCSETAW,&t) <0)
    {
      return -1;
    }

  if(tcdroprts()!=0)
    logto(0,"drop rts failed\n");

  logto(4,"drop rts succeded\n");
  return 0;
}

rise_rts()
{
  struct termio t;

  if(ioctl(fd,TCGETA,&t) <0)
    {
      return -1;
    }
  t.c_cflag &= ~CLOCAL;

  if(ioctl(fd,TCSETA,&t) <0)
    {
      return -1;
    }

  if(tcriserts()!=0)
    logto(0,"rise rts failed\n");

  logto(4,"rise rts succeded\n");
  return 0;
}

