📄 refclock_datum.c
字号:
int error_code; struct datum_pts_unit *datum_pts;#ifdef DEBUG_DATUM_PTC if (debug) printf("Poll Datum PTS\n");#endif /* ** Find the right unit and send out a time request once it is found. */ unit_index = -1; for (i=0; i<nunits; i++) { if (datum_pts_unit[i]->unit == unit) { unit_index = i; datum_pts = datum_pts_unit[i]; error_code = write(datum_pts->PTS_fd, TIME_REQUEST, 6); if (error_code != 6) perror("TIME_REQUEST"); datum_pts->nbytes = 0; break; } } /* ** Print out an error message if we could not find the right unit. */ if (unit_index == -1) {#ifdef DEBUG_DATUM_PTC if (debug) printf("Error, could not poll unit %d\n",unit);#endif msyslog(LOG_ERR, "Datum_PTS: Could not poll unit %d",unit); return; }}/*......................................................................*//* datum_pts_control - not used *//*......................................................................*/static voiddatum_pts_control( int unit, struct refclockstat *in, struct refclockstat *out, struct peer *peer ){#ifdef DEBUG_DATUM_PTC if (debug) printf("Control Datum PTS\n");#endif}/*......................................................................*//* datum_pts_init - initializes things for all possible Datum *//* time code generators that might be used. In practice, this is *//* only called once at the beginning before anything else is *//* called. *//*......................................................................*/static voiddatum_pts_init(void){ /* */ /*...... open up the log file if we are debugging ......................*/ /* */ /* ** Open up the log file if we are debugging. For now, send data out to the ** screen (stdout). */#ifdef DEBUG_DATUM_PTC if (debug) printf("Init Datum PTS\n");#endif /* ** Initialize the time request command string. This is the only message ** that we ever have to send to the Datum PTS (although others are defined). */ memcpy(TIME_REQUEST, "//k/mn",6); /* ** Initialize the number of units to 0 and set the dynamic array of units to ** NULL since there are no units defined yet. */ datum_pts_unit = NULL; nunits = 0;}/*......................................................................*//* datum_pts_buginfo - not used *//*......................................................................*/static voiddatum_pts_buginfo( int unit, register struct refclockbug *bug, register struct peer *peer ){#ifdef DEBUG_DATUM_PTC if (debug) printf("Buginfo Datum PTS\n");#endif}/*......................................................................*//* datum_pts_receive - receive the time buffer that was read in *//* by the ntpd io handling routines. When 7 bytes have been *//* received (it may take several tries before all 7 bytes are *//* received), then the time code must be unpacked and sent to *//* the ntpd clock_receive() routine which causes the systems *//* clock to be updated (several layers down). *//*......................................................................*/static voiddatum_pts_receive( struct recvbuf *rbufp ){ int i; l_fp tstmp; struct datum_pts_unit *datum_pts; char *dpt; int dpend; int tzoff; int timerr; double ftimerr, abserr;#ifdef DEBUG_DATUM_PTC double dispersion;#endif int goodtime; /*double doffset;*/ /* ** Get the time code (maybe partial) message out of the rbufp buffer. */ datum_pts = (struct datum_pts_unit *)rbufp->recv_srcclock; dpt = (char *)&rbufp->recv_space; dpend = rbufp->recv_length;#ifdef DEBUG_DATUM_PTC if (debug) printf("Receive Datum PTS: %d bytes\n", dpend);#endif /* */ /*...... save the ntp system time when the first byte is received ......*/ /* */ /* ** Save the ntp system time when the first byte is received. Note that ** because it may take several calls to this routine before all seven ** bytes of our return message are finally received by the io handlers in ** ntpd, we really do want to use the time tag when the first byte is ** received to reduce the jitter. */ if (datum_pts->nbytes == 0) { datum_pts->lastrec = rbufp->recv_time; } /* ** Increment our count to the number of bytes received so far. Return if we ** haven't gotten all seven bytes yet. */ for (i=0; i<dpend; i++) { datum_pts->retbuf[datum_pts->nbytes+i] = dpt[i]; } datum_pts->nbytes += dpend; if (datum_pts->nbytes != 7) { return; } /* ** Convert the seven bytes received in our time buffer to day, hour, minute, ** second, and msecond values. The usec value is not used for anything ** currently. It is just the fractional part of the time stored in units ** of microseconds. */ datum_pts->day = 100*(datum_pts->retbuf[0] & 0x0f) + 10*((datum_pts->retbuf[1] & 0xf0)>>4) + (datum_pts->retbuf[1] & 0x0f); datum_pts->hour = 10*((datum_pts->retbuf[2] & 0x30)>>4) + (datum_pts->retbuf[2] & 0x0f); datum_pts->minute = 10*((datum_pts->retbuf[3] & 0x70)>>4) + (datum_pts->retbuf[3] & 0x0f); datum_pts->second = 10*((datum_pts->retbuf[4] & 0x70)>>4) + (datum_pts->retbuf[4] & 0x0f); datum_pts->msec = 100*((datum_pts->retbuf[5] & 0xf0) >> 4) + 10*(datum_pts->retbuf[5] & 0x0f) + ((datum_pts->retbuf[6] & 0xf0)>>4); datum_pts->usec = 1000*datum_pts->msec;#ifdef DEBUG_DATUM_PTC if (debug) printf("day %d, hour %d, minute %d, second %d, msec %d\n", datum_pts->day, datum_pts->hour, datum_pts->minute, datum_pts->second, datum_pts->msec);#endif /* ** Get the GMT time zone offset. Note that GMT should be zero if the Datum ** reference time is using GMT as its time base. Otherwise we have to ** determine the offset if the Datum PTS is using time of day as its time ** base. */ goodtime = 0; /* We are not sure about the time and offset yet */#ifdef GMT /* ** This is the case where the Datum PTS is using GMT so there is no time ** zone offset. */ tzoff = 0; /* set time zone offset to 0 */#else /* ** This is the case where the Datum PTS is using regular time of day for its ** time so we must compute the time zone offset. The way we do it is kind of ** funny but it works. We loop through different time zones (0 to 24) and ** pick the one that gives the smallest error (+- one half hour). The time ** zone offset is stored in the datum_pts structure for future use. Normally, ** the clocktime() routine is only called once (unless the time zone offset ** changes due to daylight savings) since the goodtime flag is set when a ** good time is found (with a good offset). Note that even if the Datum ** PTS is using GMT, this mechanism will still work since it should come up ** with a value for tzoff = 0 (assuming that your system clock is within ** a half hour of the Datum time (even with time zone differences). */ for (tzoff=0; tzoff<24; tzoff++) { if (clocktime( datum_pts->day, datum_pts->hour, datum_pts->minute, datum_pts->second, (tzoff + datum_pts->tzoff) % 24, datum_pts->lastrec.l_ui, &datum_pts->yearstart, &datum_pts->lastref.l_ui) ) { datum_pts->lastref.l_uf = 0; error = datum_pts->lastref.l_ui - datum_pts->lastrec.l_ui;#ifdef DEBUG_DATUM_PTC printf("Time Zone (clocktime method) = %d, error = %d\n", tzoff, error);#endif if ((error < 1799) && (error > -1799)) { tzoff = (tzoff + datum_pts->tzoff) % 24; datum_pts->tzoff = tzoff; goodtime = 1;#ifdef DEBUG_DATUM_PTC printf("Time Zone found (clocktime method) = %d\n",tzoff);#endif break; } } }#endif /* ** Make sure that we have a good time from the Datum PTS. Clocktime() also ** sets yearstart and lastref.l_ui. We will have to set astref.l_uf (i.e., ** the fraction of a second) stuff later. */ if (!goodtime) { if (!clocktime( datum_pts->day, datum_pts->hour, datum_pts->minute, datum_pts->second, tzoff, datum_pts->lastrec.l_ui, &datum_pts->yearstart, &datum_pts->lastref.l_ui) ) {#ifdef DEBUG_DATUM_PTC if (debug) { printf("Error: bad clocktime\n"); printf("GMT %d, lastrec %d, yearstart %d, lastref %d\n", tzoff, datum_pts->lastrec.l_ui, datum_pts->yearstart, datum_pts->lastref.l_ui); }#endif msyslog(LOG_ERR, "Datum_PTS: Bad clocktime"); return; }else{#ifdef DEBUG_DATUM_PTC if (debug) printf("Good clocktime\n");#endif } } /* ** We have datum_pts->lastref.l_ui set (which is the integer part of the ** time. Now set the microseconds field. */ TVUTOTSF(datum_pts->usec, datum_pts->lastref.l_uf); /* ** Compute the time correction as the difference between the reference ** time (i.e., the Datum time) minus the receive time (system time). */ tstmp = datum_pts->lastref; /* tstmp is the datum ntp time */ L_SUB(&tstmp, &datum_pts->lastrec); /* tstmp is now the correction */ datum_pts->coderecv++; /* increment a counter */#ifdef DEBUG_DATUM_PTC dispersion = DATUM_DISPERSION; /* set the dispersion to 0 */ ftimerr = dispersion; ftimerr /= (1024.0 * 64.0); if (debug) printf("dispersion = %d, %f\n", dispersion, ftimerr);#endif /* ** Pass the new time to ntpd through the refclock_receive function. Note ** that we are not trying to make any corrections due to the time it takes ** for the Datum PTS to send the message back. I am (erroneously) assuming ** that the time for the Datum PTS to send the time back to us is negligable. ** I suspect that this time delay may be as much as 15 ms or so (but probably ** less). For our needs at JPL, this kind of error is ok so it is not ** necessary to use fudge factors in the ntp.conf file. Maybe later we will. */ /*LFPTOD(&tstmp, doffset);*/ datum_pts->lastref = datum_pts->lastrec; refclock_receive(datum_pts->peer); /* ** Compute sigma squared (not used currently). Maybe later, this could be ** used for the dispersion estimate. The problem is that ntpd does not link ** in the math library so sqrt() is not available. Anyway, this is useful ** for debugging. Maybe later I will just use absolute values for the time ** error to come up with my dispersion estimate. Anyway, for now my dispersion ** is set to 0. */ timerr = tstmp.l_ui<<20; timerr |= (tstmp.l_uf>>12) & 0x000fffff; ftimerr = timerr; ftimerr /= 1024*1024; abserr = ftimerr; if (ftimerr < 0.0) abserr = -ftimerr; if (datum_pts->sigma2 == 0.0) { if (abserr < DATUM_MAX_ERROR) { datum_pts->sigma2 = abserr*abserr; }else{ datum_pts->sigma2 = DATUM_MAX_ERROR2; } }else{ if (abserr < DATUM_MAX_ERROR) { datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*abserr*abserr; }else{ datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*DATUM_MAX_ERROR2; } }#ifdef DEBUG_DATUM_PTC if (debug) printf("Time error = %f seconds\n", ftimerr);#endif#if defined(DEBUG_DATUM_PTC) || defined(LOG_TIME_ERRORS) if (debug) printf("PTS: day %d, hour %d, minute %d, second %d, msec %d, Time Error %f\n", datum_pts->day, datum_pts->hour, datum_pts->minute, datum_pts->second, datum_pts->msec, ftimerr);#endif}#elseint refclock_datum_bs;#endif /* REFCLOCK */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -