FreeBSD/contrib/ntp/ntpd/refclock_ripencc.c
/*
 * ripencc_receive()
 *
 * called when a packet is received on the serial port
 * takes care of further processing
 *
 */
ripencc_receive()
called when a packet is received on the serial port
takes care of further processing
ripencc_receive () called when a packet is received on the serial port takes care of further processing
static void
ripencc_receive(struct recvbuf *rbufp)
{
	register struct ripencc_unit *up;
	register struct refclockproc *pp;	
	struct peer *peer;
	static TSIPPKT rpt;	 
	TSIPPKT spt;		
	int ns_since_pps;			
	int i;
	char *cp;
	char    rd_lastcode[BMAX];
	l_fp    rd_tmp;
	u_short rd_lencode;
	peer = rbufp->recv_peer;
	pp = peer->procptr;
	up = pp->unitptr;
	rd_lencode = refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
#ifdef DEBUG_RAW
	if (debug)
		fprintf(stderr, "ripencc_receive(%d)\n", up->unit);
#endif 
#ifdef DEBUG_RAW
	if (debug) {		
		int i;
		unsigned char *cp;
		printf("ripencc_receive: len %d\n", rbufp->recv_length);
		for (i=1, cp=(char*)&rbufp->recv_space;
		     i <= rbufp->recv_length;
		     i++, cp++) {
			printf(" %02X", *cp);
			if (i%10 == 0) 
				printf("\n");
		}
		printf("\n");
	}
#endif 
	cp = (char*) &rbufp->recv_space;
	i=rbufp->recv_length;
	while (i--) {		
		tsip_input_proc(&rpt, (unsigned char) *cp++);
		if (rpt.status != TSIP_PARSED_FULL)
			continue;
		switch (rpt.code) {
		    case 0x8F:	
			switch (rpt.buf[0]) {
			    case 0xAD:	
				ns_since_pps = 200;
				if (up->polled && ns_since_pps < 150) {
					msyslog(LOG_INFO, "%s(): up->polled",
						__FUNCTION__);
					ripencc_poll(up->unit, peer);
					break;
				}
				if (parse0x8FAD(&rpt, peer) < 0) {
					msyslog(LOG_INFO, "%s(): parse0x8FAD < 0",__FUNCTION__);
					refclock_report(peer, CEVNT_BADREPLY);
					break;
				}
				if (ripencc_get_pps_ts(up, &rd_tmp) == 1) {
					pp->lastrec = up->tstamp = rd_tmp;
					pp->nsec = 0;
				}
				else
					msyslog(LOG_INFO, "%s(): ripencc_get_pps_ts returns failure",__FUNCTION__);
				if (!up->polled) { 
					msyslog(LOG_INFO, "%s(): unrequested packet",__FUNCTION__);
					break;
				}
				up->polled = 0;
				up->pollcnt = 2;
				cmd_0x8E0Bq(&spt);
				ripencc_send(peer,spt);
				if (ns_since_pps < 0) { 
					msyslog(LOG_INFO, "%s(): ns_since_pps < 0",__FUNCTION__);
					refclock_report(peer, CEVNT_BADTIME);
					break;
				}
				if (!refclock_process(pp)) {
					msyslog(LOG_INFO, "%s(): !refclock_process",__FUNCTION__);
					refclock_report(peer, CEVNT_BADTIME);
					break;
				}
				refclock_receive(peer);
				break;
			    case 0x0B: 
				parse0x8F0B(&rpt, peer);
				break;
			    default: 
#ifdef DEBUG_NCC
				msyslog(LOG_INFO, "%s(): calling parseany",
					__FUNCTION__);
#endif 
#ifdef TRIMBLE_OUTPUT_FUNC
				parseany(&rpt, peer);
#endif 
				break;
			}
			break;
		    case 0x4F:	
			parse0x4F(&rpt, peer);
			break;
		    case 0x5C:	
			parse0x5C(&rpt, peer);
			break;
		    default:	
#ifdef TRIMBLE_OUTPUT_FUNC
			parseany(&rpt, peer);
#endif 
			break;
		}
   		rpt.status = TSIP_PARSED_EMPTY;
	}
}
static void ripencc_receive ( struct recvbuf * rbufp ) { register struct ripencc_unit * up ; register struct refclockproc * pp ; struct peer * peer ; static TSIPPKT rpt ; TSIPPKT spt ; int ns_since_pps ; int i ; char * cp ; char rd_lastcode [ BMAX ] ; l_fp rd_tmp ; u_short rd_lencode ; peer = rbufp -> recv_peer ; pp = peer -> procptr ; up = pp -> unitptr ; rd_lencode = refclock_gtlin ( rbufp , rd_lastcode , BMAX , & rd_tmp ) ; #ifdef DEBUG_RAW if ( debug ) fprintf ( stderr , " \n " , up -> unit ) ; #endif #ifdef DEBUG_RAW if ( debug ) { int i ; unsigned char * cp ; printf ( " \n " , rbufp -> recv_length ) ; for ( i = 1 , cp = ( char * ) & rbufp -> recv_space ; i <= rbufp -> recv_length ; i ++ , cp ++ ) { printf ( " " , * cp ) ; if ( i % 10 == 0 ) printf ( " \n " ) ; } printf ( " \n " ) ; } #endif cp = ( char * ) & rbufp -> recv_space ; i = rbufp -> recv_length ; while ( i -- ) { tsip_input_proc ( & rpt , ( unsigned char ) * cp ++ ) ; if ( rpt . status != TSIP_PARSED_FULL ) continue ; switch ( rpt . code ) { case 0x8F : switch ( rpt . buf [ 0 ] ) { case 0xAD : ns_since_pps = 200 ; if ( up -> polled && ns_since_pps < 150 ) { msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; ripencc_poll ( up -> unit , peer ) ; break ; } if ( parse0x8FAD ( & rpt , peer ) < 0 ) { msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; refclock_report ( peer , CEVNT_BADREPLY ) ; break ; } if ( ripencc_get_pps_ts ( up , & rd_tmp ) == 1 ) { pp -> lastrec = up -> tstamp = rd_tmp ; pp -> nsec = 0 ; } else msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; if ( ! up -> polled ) { msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; break ; } up -> polled = 0 ; up -> pollcnt = 2 ; cmd_0x8E0Bq ( & spt ) ; ripencc_send ( peer , spt ) ; if ( ns_since_pps < 0 ) { msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; refclock_report ( peer , CEVNT_BADTIME ) ; break ; } if ( ! refclock_process ( pp ) ) { msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; refclock_report ( peer , CEVNT_BADTIME ) ; break ; } refclock_receive ( peer ) ; break ; case 0x0B : parse0x8F0B ( & rpt , peer ) ; break ; default : #ifdef DEBUG_NCC msyslog ( LOG_INFO , " " , __FUNCTION__ ) ; #endif #ifdef TRIMBLE_OUTPUT_FUNC parseany ( & rpt , peer ) ; #endif break ; } break ; case 0x4F : parse0x4F ( & rpt , peer ) ; break ; case 0x5C : parse0x5C ( & rpt , peer ) ; break ; default : #ifdef TRIMBLE_OUTPUT_FUNC parseany ( & rpt , peer ) ; #endif break ; } rpt . status = TSIP_PARSED_EMPTY ; } }
ripencc_receive()
 called when a packet is received on the serial port
 takes care of further processing
ripencc_receive () called when a packet is received on the serial port takes care of further processing
/* for current incoming TSIP report */ /* send packet */ /* these variables hold data until we decide it's worth keeping */ /* msyslog(LOG_INFO, "%s",__FUNCTION__); */ /*
	 * Initialize pointers and read the timecode and timestamp
	 */ /* DEBUG_RAW */ /* print raw packet */ /* DEBUG_RAW */ /* loop over received chars */ /* superpacket */ /* UTC Time */ /*
				** When polling on port B the timecode is
				** the time of the previous PPS.  If we
				** completed receiving the packet less than
				** 150ms after the turn of the second, it
				** may have the code of the previous second.
				** We do not trust that and simply poll
				** again without even parsing it.
				**
				** More elegant would be to re-schedule the
				** poll, but I do not know (yet) how to do
				** that cleanly.
				**
				*/ /* BLA ns_since_pps = ncc_tstmp(rbufp, &trtmp); */ /*   if (up->polled && ns_since_pps > -1 && ns_since_pps < 150) { */ /*
 				 * Parse primary utc time packet
				 * and fill refclock structure 
				 * from results. 
				 */ /*
				 * If the PPSAPI is working, rather use its 
				 * timestamps.
				 * assume that the PPS occurs on the second 
				 * so blow any msec
				 */ /* unrequested packet */ /* we have been polled ! */ /* poll for next packet */ /* no PPS */ /*
				** Process the new sample in the median
				** filter and determine the reference clock
				** offset and dispersion.
				*/ /* comprehensive time packet */ /* other superpackets */ /* DEBUG_NCC */ /* TRIMBLE_OUTPUT_FUNC */ /* UTC parameters, for leap info */ /* sat tracking data */ /* other packets */ /* TRIMBLE_OUTPUT_FUNC */