FreeBSD/contrib/ntp/ntpd/refclock_arc.c
/*
 * arc_start - open the devices and initialize data for processing
 */
open the devices and initialize data for processing
open the devices and initialize data for processing
static int
arc_start(
	int unit,
	struct peer *peer
	)
{
	register struct arcunit *up;
	struct refclockproc *pp;
	int temp_fd;
	int fd;
	char device[20];
#ifdef HAVE_TERMIOS
	struct termios arg;
#endif
	msyslog(LOG_NOTICE, "MSF_ARCRON %s: opening unit %d",
		arc_version, unit);
	DPRINTF(1, ("arc: %s: attempt to open unit %d.\n", arc_version,
		unit));
	snprintf(device, sizeof(device), DEVICE, unit);
	temp_fd = refclock_open(device, SPEED, LDISC_CLK);
	if (temp_fd <= 0)
		return 0;
	DPRINTF(1, ("arc: unit %d using tty_open().\n", unit));
	fd = tty_open(device, OPEN_FLAGS, 0777);
	if (fd < 0) {
		msyslog(LOG_ERR, "MSF_ARCRON(%d): failed second open(%s, 0777): %m.",
			unit, device);
		close(temp_fd);
		return 0;
	}
	close(temp_fd);
	temp_fd = -1;		
#ifndef SYS_WINNT
	if (-1 == fcntl(fd, F_SETFL, 0)) 
		msyslog(LOG_ERR, "MSF_ARCRON(%d): fcntl(F_SETFL, 0): %m.",
			unit);
#endif
	DPRINTF(1, ("arc: opened RS232 port with file descriptor %d.\n", fd));
#ifdef HAVE_TERMIOS
	if (tcgetattr(fd, &arg) < 0) {
		msyslog(LOG_ERR, "MSF_ARCRON(%d): tcgetattr(%s): %m.",
			unit, device);
		close(fd);
		return 0;
	}
	arg.c_iflag = IGNBRK | ISTRIP;
	arg.c_oflag = 0;
	arg.c_cflag = B300 | CS8 | CREAD | CLOCAL | CSTOPB;
	arg.c_lflag = 0;
	arg.c_cc[VMIN] = 1;
	arg.c_cc[VTIME] = 0;
	if (tcsetattr(fd, TCSANOW, &arg) < 0) {
		msyslog(LOG_ERR, "MSF_ARCRON(%d): tcsetattr(%s): %m.",
			unit, device);
		close(fd);
		return 0;
	}
#else
	msyslog(LOG_ERR, "ARCRON: termios required by this driver");
	(void)close(fd);
	return 0;
#endif
	up = emalloc_zero(sizeof(*up));
	pp = peer->procptr;
	pp->io.clock_recv = arc_receive;
	pp->io.srcclock = peer;
	pp->io.datalen = 0;
	pp->io.fd = fd;
	if (!io_addclock(&pp->io)) {
		close(fd);
		pp->io.fd = -1;
		free(up); 
		return(0); 
	}
	pp->unitptr = up;
	peer->precision = PRECISION;
	peer->stratum = 2;              
	pp->clockdesc = DESCRIPTION;
	if (peer->MODE > 3) {
		msyslog(LOG_NOTICE, "ARCRON: Invalid mode %d", peer->MODE);
		return 0;
	}
#ifdef DEBUG
	if(debug) { printf("arc: mode = %d.\n", peer->MODE); }
#endif
	switch (peer->MODE) {
	    case 1:
		memcpy((char *)&pp->refid, REFID_MSF, 4);
		break;
	    case 2:
		memcpy((char *)&pp->refid, REFID_DCF77, 4);
		break;
	    case 3:
		memcpy((char *)&pp->refid, REFID_WWVB, 4);
		break;
	    default:
		memcpy((char *)&pp->refid, REFID, 4);
		break;
	}
	up->next_resync = current_time + INITIAL_RESYNC_DELAY + (67*unit)%1009;
#if 0 
	up->resyncing = 0;              
	up->saved_flags = 0;            
	{
		int i;
		for(i = CMDQUEUELEN; i >= 0; --i) { up->cmdqueue[i] = '\0'; }
	}
#endif
#ifdef ARCRON_KEEN
	up->quality = QUALITY_UNKNOWN;  
#else
	up->quality = MIN_CLOCK_QUALITY;
#endif
	peer->procptr->action = arc_event_handler;
	ENQUEUE(up);
	return(1);
}
static int arc_start ( int unit , struct peer * peer ) { register struct arcunit * up ; struct refclockproc * pp ; int temp_fd ; int fd ; char device [ 20 ] ; #ifdef HAVE_TERMIOS struct termios arg ; #endif msyslog ( LOG_NOTICE , " " , arc_version , unit ) ; DPRINTF ( 1 , ( " \n " , arc_version , unit ) ) ; snprintf ( device , sizeof ( device ) , DEVICE , unit ) ; temp_fd = refclock_open ( device , SPEED , LDISC_CLK ) ; if ( temp_fd <= 0 ) return 0 ; DPRINTF ( 1 , ( " \n " , unit ) ) ; fd = tty_open ( device , OPEN_FLAGS , 0777 ) ; if ( fd < 0 ) { msyslog ( LOG_ERR , " " , unit , device ) ; close ( temp_fd ) ; return 0 ; } close ( temp_fd ) ; temp_fd = -1 ; #ifndef SYS_WINNT if ( -1 == fcntl ( fd , F_SETFL , 0 ) ) msyslog ( LOG_ERR , " " , unit ) ; #endif DPRINTF ( 1 , ( " \n " , fd ) ) ; #ifdef HAVE_TERMIOS if ( tcgetattr ( fd , & arg ) < 0 ) { msyslog ( LOG_ERR , " " , unit , device ) ; close ( fd ) ; return 0 ; } arg . c_iflag = IGNBRK | ISTRIP ; arg . c_oflag = 0 ; arg . c_cflag = B300 | CS8 | CREAD | CLOCAL | CSTOPB ; arg . c_lflag = 0 ; arg . c_cc [ VMIN ] = 1 ; arg . c_cc [ VTIME ] = 0 ; if ( tcsetattr ( fd , TCSANOW , & arg ) < 0 ) { msyslog ( LOG_ERR , " " , unit , device ) ; close ( fd ) ; return 0 ; } #else msyslog ( LOG_ERR , " " ) ; ( void ) close ( fd ) ; return 0 ; #endif up = emalloc_zero ( sizeof ( * up ) ) ; pp = peer -> procptr ; pp -> io . clock_recv = arc_receive ; pp -> io . srcclock = peer ; pp -> io . datalen = 0 ; pp -> io . fd = fd ; if ( ! io_addclock ( & pp -> io ) ) { close ( fd ) ; pp -> io . fd = -1 ; free ( up ) ; return ( 0 ) ; } pp -> unitptr = up ; peer -> precision = PRECISION ; peer -> stratum = 2 ; pp -> clockdesc = DESCRIPTION ; if ( peer -> MODE > 3 ) { msyslog ( LOG_NOTICE , " " , peer -> MODE ) ; return 0 ; } #ifdef DEBUG if ( debug ) { printf ( " \n " , peer -> MODE ) ; } #endif switch ( peer -> MODE ) { case 1 : memcpy ( ( char * ) & pp -> refid , REFID_MSF , 4 ) ; break ; case 2 : memcpy ( ( char * ) & pp -> refid , REFID_DCF77 , 4 ) ; break ; case 3 : memcpy ( ( char * ) & pp -> refid , REFID_WWVB , 4 ) ; break ; default : memcpy ( ( char * ) & pp -> refid , REFID , 4 ) ; break ; } up -> next_resync = current_time + INITIAL_RESYNC_DELAY + ( 67 * unit ) % 1009 ; #if 0 up -> resyncing = 0 ; up -> saved_flags = 0 ; { int i ; for ( i = CMDQUEUELEN ; i >= 0 ; -- i ) { up -> cmdqueue [ i ] = ' \0 ' ; } } #endif #ifdef ARCRON_KEEN up -> quality = QUALITY_UNKNOWN ; #else up -> quality = MIN_CLOCK_QUALITY ; #endif peer -> procptr -> action = arc_event_handler ; ENQUEUE ( up ) ; return ( 1 ) ; }
arc_start - open the devices and initialize data for processing
arc_start - open the devices and initialize data for processing
/*
	 * Open serial port. Use CLK line discipline, if available.
	 */ /* not used after this, at *this* time. */ /* clear the descriptor flags */ /* Set structure to all zeros... */ /*
	 * Initialize miscellaneous variables
	 */ /* Default to stratum 2 not 0. */ /* Spread out resyncs so that they should remain separated. */ /* Not needed because of zeroing of arcunit structure... */ /* Not resyncing yet. */ /* Default is all flags off. */ /* Clear send buffer out... */ /* Trust the clock immediately. */ /* Don't trust the clock yet. */