FreeBSD/contrib/ntp/ntpd/refclock_leitch.c
/*
 * leitch_start - open the LEITCH devices and initialize data for processing
 */
open the LEITCH devices and initialize data for processing
open the LEITCH devices and initialize data for processing
static int
leitch_start(
	int unit,
	struct peer *peer
	)
{
	struct leitchunit *leitch;
	int fd232;
	char leitchdev[20];
	if (unit >= MAXUNITS) {
		msyslog(LOG_ERR, "leitch_start: unit %d invalid", unit);
		return (0);
	}
	if (unitinuse[unit]) {
		msyslog(LOG_ERR, "leitch_start: unit %d in use", unit);
		return (0);
	}
	snprintf(leitchdev, sizeof(leitchdev), LEITCH232, unit);
	fd232 = open(leitchdev, O_RDWR, 0777);
	if (fd232 == -1) {
		msyslog(LOG_ERR,
			"leitch_start: open of %s: %m", leitchdev);
		return (0);
	}
	leitch = &leitchunits[unit];
	memset(leitch, 0, sizeof(*leitch));
#if defined(HAVE_SYSV_TTYS)
	{	struct termio ttyb;
	if (ioctl(fd232, TCGETA, &ttyb) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: ioctl(%s, TCGETA): %m", leitchdev);
		goto screwed;
	}
	ttyb.c_iflag = IGNBRK|IGNPAR|ICRNL;
	ttyb.c_oflag = 0;
	ttyb.c_cflag = SPEED232|CS8|CLOCAL|CREAD;
	ttyb.c_lflag = ICANON;
	ttyb.c_cc[VERASE] = ttyb.c_cc[VKILL] = '\0';
	if (ioctl(fd232, TCSETA, &ttyb) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: ioctl(%s, TCSETA): %m", leitchdev);
		goto screwed;
	}
	}
#endif 
#if defined(HAVE_TERMIOS)
	{	struct termios ttyb, *ttyp;
	ttyp = &ttyb;
	if (tcgetattr(fd232, ttyp) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: tcgetattr(%s): %m", leitchdev);
		goto screwed;
	}
	ttyp->c_iflag = IGNBRK|IGNPAR|ICRNL;
	ttyp->c_oflag = 0;
	ttyp->c_cflag = SPEED232|CS8|CLOCAL|CREAD;
	ttyp->c_lflag = ICANON;
	ttyp->c_cc[VERASE] = ttyp->c_cc[VKILL] = '\0';
	if (tcsetattr(fd232, TCSANOW, ttyp) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: tcsetattr(%s): %m", leitchdev);
		goto screwed;
	}
	if (tcflush(fd232, TCIOFLUSH) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: tcflush(%s): %m", leitchdev);
		goto screwed;
	}
	}
#endif 
#if defined(HAVE_BSD_TTYS)
	{
		struct sgttyb ttyb;
	if (ioctl(fd232, TIOCGETP, &ttyb) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: ioctl(%s, TIOCGETP): %m", leitchdev);
		goto screwed;
	}
	ttyb.sg_ispeed = ttyb.sg_ospeed = SPEED232;
	ttyb.sg_erase = ttyb.sg_kill = '\0';
	ttyb.sg_flags = EVENP|ODDP|CRMOD;
	if (ioctl(fd232, TIOCSETP, &ttyb) < 0) {
		msyslog(LOG_ERR,
			"leitch_start: ioctl(%s, TIOCSETP): %m", leitchdev);
		goto screwed;
	}
	}
#endif 
	leitch->peer = peer;
	leitch->unit = unit;
	leitch->state = STATE_IDLE;
	leitch->fudge1 = 15;	
	leitch->leitchio.clock_recv = leitch_receive;
	leitch->leitchio.srcclock = peer;
	leitch->leitchio.datalen = 0;
	leitch->leitchio.fd = fd232;
	if (!io_addclock(&leitch->leitchio)) {
		leitch->leitchio.fd = -1;
		goto screwed;
	}
	peer->precision = PRECISION;
	peer->stratum = stratumtouse[unit];
	peer->refid = refid[unit];
	unitinuse[unit] = 1;
	return(1);
    screwed:
	close(fd232);
	return(0);
}
static int leitch_start ( int unit , struct peer * peer ) { struct leitchunit * leitch ; int fd232 ; char leitchdev [ 20 ] ; if ( unit >= MAXUNITS ) { msyslog ( LOG_ERR , " " , unit ) ; return ( 0 ) ; } if ( unitinuse [ unit ] ) { msyslog ( LOG_ERR , " " , unit ) ; return ( 0 ) ; } snprintf ( leitchdev , sizeof ( leitchdev ) , LEITCH232 , unit ) ; fd232 = open ( leitchdev , O_RDWR , 0777 ) ; if ( fd232 == -1 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; return ( 0 ) ; } leitch = & leitchunits [ unit ] ; memset ( leitch , 0 , sizeof ( * leitch ) ) ; #if defined ( HAVE_SYSV_TTYS ) { struct termio ttyb ; if ( ioctl ( fd232 , TCGETA , & ttyb ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } ttyb . c_iflag = IGNBRK | IGNPAR | ICRNL ; ttyb . c_oflag = 0 ; ttyb . c_cflag = SPEED232 | CS8 | CLOCAL | CREAD ; ttyb . c_lflag = ICANON ; ttyb . c_cc [ VERASE ] = ttyb . c_cc [ VKILL ] = ' \0 ' ; if ( ioctl ( fd232 , TCSETA , & ttyb ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } } #endif #if defined ( HAVE_TERMIOS ) { struct termios ttyb , * ttyp ; ttyp = & ttyb ; if ( tcgetattr ( fd232 , ttyp ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } ttyp -> c_iflag = IGNBRK | IGNPAR | ICRNL ; ttyp -> c_oflag = 0 ; ttyp -> c_cflag = SPEED232 | CS8 | CLOCAL | CREAD ; ttyp -> c_lflag = ICANON ; ttyp -> c_cc [ VERASE ] = ttyp -> c_cc [ VKILL ] = ' \0 ' ; if ( tcsetattr ( fd232 , TCSANOW , ttyp ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } if ( tcflush ( fd232 , TCIOFLUSH ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } } #endif #if defined ( HAVE_BSD_TTYS ) { struct sgttyb ttyb ; if ( ioctl ( fd232 , TIOCGETP , & ttyb ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } ttyb . sg_ispeed = ttyb . sg_ospeed = SPEED232 ; ttyb . sg_erase = ttyb . sg_kill = ' \0 ' ; ttyb . sg_flags = EVENP | ODDP | CRMOD ; if ( ioctl ( fd232 , TIOCSETP , & ttyb ) < 0 ) { msyslog ( LOG_ERR , " " , leitchdev ) ; goto screwed ; } } #endif leitch -> peer = peer ; leitch -> unit = unit ; leitch -> state = STATE_IDLE ; leitch -> fudge1 = 15 ; leitch -> leitchio . clock_recv = leitch_receive ; leitch -> leitchio . srcclock = peer ; leitch -> leitchio . datalen = 0 ; leitch -> leitchio . fd = fd232 ; if ( ! io_addclock ( & leitch -> leitchio ) ) { leitch -> leitchio . fd = -1 ; goto screwed ; } peer -> precision = PRECISION ; peer -> stratum = stratumtouse [ unit ] ; peer -> refid = refid [ unit ] ; unitinuse [ unit ] = 1 ; return ( 1 ) ; screwed : close ( fd232 ) ; return ( 0 ) ; }
leitch_start - open the LEITCH devices and initialize data for processing
leitch_start - open the LEITCH devices and initialize data for processing
/*
	 * Check configuration info.
	 */ /*
	 * Open serial port.
	 */ /*
	 * System V serial line parameters (termio interface)
	 *
	 */ /* HAVE_SYSV_TTYS */ /*
	 * POSIX serial line parameters (termios interface)
	 */ /* HAVE_TERMIOS */ /*
	 * 4.3bsd serial line parameters (sgttyb interface)
	 */ /* HAVE_BSD_TTYS */ /*
	 * Set up the structures
	 */ /* 15ms */ /*
	 * All done.  Initialize a few random peer variables, then
	 * return success.
	 */ /*
	 * Something broke; abandon ship.
	 */