#include "clock.h"

int devopen()
{
int	clockdev;	/* file descriptor  to the clock terminal device */
struct	termios	attr;	/* gettting and setting termios attributes   */




/* try to open the device */
/* for read, write and non blocking */

clockdev = open("/dev/cua/a",O_RDWR);


if (clockdev == -1)
	{
	perror("cannot open clock device");
	return(EXIT_FAILURE);
	}

/* get the terminal characteristics into the structure attr */

if (tcgetattr(clockdev,&attr) != 0)
	{
	perror("problems with getting attributes");
	return(EXIT_FAILURE);
	}

/* now we can set all the terminal parameters */

/* c_iflag input processing */
/* BRKINT	ON	generate SIGINT on break */ 
/* ICRNL	OFF	map CR to NL on input */
/* IGNBRK	OFF	ignore BREAK condition */
/* IGNCR	OFF	ignore CR */
/* IGNPAR	OFF	ignore characters with parity errors */
/* INLCR	OFF	map NL to CR on input */
/* INPCK	OFF	enable input parity checking */
/* ISTRIP	ON	strip eighth bit of input characters */
/* IXOFF	OFF	enable start/stop input control */
/* IXON		OFF	enable start/stop output control */
/* PARMRK	OFF	mark parity errors */

/* flags to turn off */
attr.c_iflag &= ~(ICRNL|IGNBRK|IGNCR|IGNPAR|INLCR|INPCK|IXOFF|IXON|PARMRK);

/* flags to turn on */
attr.c_iflag |= (BRKINT|ISTRIP);

/* c_oflag output processing */

/* OPOST	OFF	perform output processing */

/* flags to turn off */
attr.c_oflag &= ~(OPOST);

/* c_cflag control flags */

/* CLOCAL	ON	ignore modem status lines */
/* CREAD	ON	enable receiver */
/* CSIZE	CS8	character size mask */
/* CSTOPB	OFF	send two stop bits */
/* HUPCL	OFF	hangup on last close */
/* PARENB	OFF	parity enable */
/* PARODD	OFF	odd parity, else even */

/* note: POSIX does not define hardware control lines CCTS_OFLOW, CRTS_IFLOW */

/* flags to turn on */
attr.c_cflag |= (CLOCAL|CREAD|CS8);

/* flags to turn off */
attr.c_cflag &= ~(CSTOPB|HUPCL|PARENB|PARODD);

/* c_lflag local processing */

/* ECHO		OFF	enable echo */
/* ECHOE	OFF	visually erase characters */
/* ECHOK	OFF	echo kill */
/* ECHONL	OFF	echo NL */
/* ICANON	OFF	cannonical input */
/* IEXTEN	OFF	enable extended input character processing */
/* ISIG		OFF	enable terminal generated signals */
/* NOFLSH	OFF	disable flush after interrupt or quit */
/* TOSTOP	OFF	send SIGTTOU for background output */

/* flags to turn off */
attr.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|IEXTEN|ISIG|NOFLSH|TOSTOP);

/* set noncannonical mode processing. Return as soon as we get a character */
/* but do not wait more than 2 seconds. We must always get an answer within */
/* one second or else we have an error condition */

attr.c_cc[VMIN] = 0;
attr.c_cc[VTIME] = 20;

/* now set the baud rate in the termios structure */

if (cfsetispeed(&attr,B300)<0)
	{
	perror("error setting input baud rate");
	return(EXIT_FAILURE);
	}

if (cfsetospeed(&attr,B300)<0)
	{
	perror("error setting output baud rate");
	return(EXIT_FAILURE);
	}


/* Think that is all the terminal attributes set so call tcsetattr */
if (tcsetattr(clockdev,TCSANOW,&attr) !=0)
	{
	perror("error setting terminal attributes");
	return(EXIT_FAILURE);
	}

return(clockdev);
}
