Lines Matching +full:noise +full:- +full:sensitive

2  * ntp_refclock - processing support for reference clocks
53 * In addition, there may be a driver-specific unit structure used for
57 * which is used for all peer-specific processing and contains a
82 #if MAXSTAGE & (MAXSTAGE - 1)
89 pp->coderecv = (pp->coderecv + 1) % MAXSTAGE;
90 if (pp->coderecv == pp->codeproc)
91 pp->codeproc = (pp->codeproc + 1) % MAXSTAGE;
92 pp->filter[pp->coderecv] = sv;
99 if (pp->coderecv == pp->codeproc)
101 pp->codeproc = (pp->codeproc + 1) % MAXSTAGE;
102 return pp->filter[pp->codeproc];
109 u_int retv = pp->coderecv - pp->codeproc;
122 pp->coderecv = (pp->coderecv + 1) & (MAXSTAGE - 1);
123 if (pp->coderecv == pp->codeproc)
124 pp->codeproc = (pp->codeproc + 1) & (MAXSTAGE - 1);
125 pp->filter[pp->coderecv] = sv;
132 if (pp->coderecv == pp->codeproc)
134 pp->codeproc = (pp->codeproc + 1) & (MAXSTAGE - 1);
135 return pp->filter[pp->codeproc];
142 return (pp->coderecv - pp->codeproc) & (MAXSTAGE - 1);
148 * refclock_report - note the occurance of an event
162 pp = peer->procptr;
169 pp->noreply++;
173 pp->badformat++;
181 pp->baddata++;
188 if ((code != CEVNT_NOMINAL) && (pp->lastevent < 15))
189 pp->lastevent++;
190 if (pp->currentstatus != code) {
191 pp->currentstatus = (u_char)code;
198 * init_refclock - initialize the reference clock drivers
210 if (refclock_conf[i]->clock_init != noentry)
211 (refclock_conf[i]->clock_init)();
216 * refclock_newpeer - initialize and start a reference clock
220 * driver-specific support routine completes the initialization, if
240 if (!ISREFCLOCKADR(&peer->srcadr)) {
243 stoa(&peer->srcadr));
246 clktype = (u_char)REFCLOCKTYPE(&peer->srcadr);
247 unit = REFCLOCKUNIT(&peer->srcadr);
249 refclock_conf[clktype]->clock_start == noentry) {
260 peer->procptr = pp;
265 peer->refclktype = clktype;
266 peer->refclkunit = (u_char)unit;
267 peer->flags |= FLAG_REFCLOCK;
268 peer->leap = LEAP_NOTINSYNC;
269 peer->stratum = STRATUM_REFCLOCK;
270 peer->ppoll = peer->maxpoll;
271 pp->type = clktype;
272 pp->conf = refclock_conf[clktype];
273 pp->timestarted = current_time;
274 pp->io.fd = -1;
279 switch (peer->hmode) {
281 peer->pmode = MODE_PASSIVE;
285 peer->pmode = MODE_SERVER;
293 if (!((refclock_conf[clktype]->clock_start)(unit, peer))) {
297 peer->refid = pp->refid;
303 * refclock_unpeer - shut down a clock
317 if (NULL == peer->procptr)
320 clktype = peer->refclktype;
321 unit = peer->refclkunit;
322 if (refclock_conf[clktype]->clock_shutdown != noentry)
323 (refclock_conf[clktype]->clock_shutdown)(unit, peer);
324 free(peer->procptr);
325 peer->procptr = NULL;
330 * refclock_timer - called once per second for housekeeping.
340 unit = p->refclkunit;
341 pp = p->procptr;
342 if (pp->conf->clock_timer != noentry)
343 (*pp->conf->clock_timer)(unit, p);
344 if (pp->action != NULL && pp->nextaction <= current_time)
345 (*pp->action)(p);
350 * refclock_transmit - simulate the transmit procedure
365 clktype = peer->refclktype;
366 unit = peer->refclkunit;
367 peer->sent++;
368 get_systime(&peer->xmt);
373 * protocol here and call the driver-specific transmit routine.
375 if (peer->burst == 0) {
380 current_time, stoa(&(peer->srcadr)));
387 oreach = peer->reach & 0xfe;
388 peer->reach <<= 1;
389 if (!(peer->reach & 0x0f))
391 peer->outdate = current_time;
392 if (!peer->reach) {
395 peer->timereachable = current_time;
398 if (peer->flags & FLAG_BURST)
399 peer->burst = NSTAGE;
402 peer->burst--;
404 peer->procptr->inpoll = TRUE;
405 if (refclock_conf[clktype]->clock_poll != noentry)
406 (refclock_conf[clktype]->clock_poll)(unit, peer);
407 poll_update(peer, peer->hpoll, 0);
412 * Compare two doubles - used with qsort()
424 return -1;
440 # if MAXSTAGE & (MAXSTAGE - 1)
442 na = pp->coderecv - pp->codeproc;
448 na = (pp->coderecv - pp->codeproc) & (MAXSTAGE - 1);
470 # if MAXSTAGE & (MAXSTAGE - 1)
472 na = pp->coderecv - pp->codeproc;
477 pp->codeproc = (pp->codeproc + nd) % MAXSTAGE;
481 na = (pp->coderecv - pp->codeproc) & (MAXSTAGE - 1);
484 pp->codeproc = (pp->codeproc + nd) & (MAXSTAGE - 1);
491 * refclock_process_offset - update median filter
508 pp->lastrec = lastrec;
513 refclock_checkburst(pp->io.srcclock, pp);
518 * refclock_process - process a sample from the clock
519 * refclock_process_f - refclock_process with other than time1 fudge
527 * Important for PPS users: Normally, the pp->lastrec is set to the
528 * system time when the on-time character is received and the pp->year,
529 * ..., pp->second decoded and the seconds fraction pp->nsec in
530 * nanoseconds). When a PPS offset is available, pp->nsec is forced to
531 * zero and the fraction for pp->lastrec is set to the PPS offset.
549 if (!clocktime(pp->day, pp->hour, pp->minute, pp->second, GMT,
550 pp->lastrec.l_ui, &pp->yearstart, &offset.l_ui))
554 DTOLFP(pp->nsec / 1e9, &ltemp);
556 refclock_process_offset(pp, offset, pp->lastrec, fudge);
566 return refclock_process_f(pp, pp->fudgetime1);
571 * refclock_sample - process a pile of samples from the clock
593 while (pp->codeproc != pp->coderecv)
610 m = n - (n * 4) / 10;
611 while ((k = j - i) > m) {
612 k = (k - 1) >> 1;
613 if ((off[j - 1] - off[j - k - 1]) < (off[i + k] - off[i]))
616 j--; /* reject high end */
622 pp->offset = off[i];
623 pp->jitter = 0;
625 pp->offset += off[k];
626 pp->jitter += SQUARE(off[k] - off[k - 1]);
628 pp->offset /= m;
629 m -= (m > 1); /* only (m-1) terms attribute to jitter! */
630 pp->jitter = max(SQRT(pp->jitter / m), LOGTOD(sys_precision));
643 pp->jitter = max(pp->jitter, pp->fudgeminjitter);
649 (int)n, pp->offset, pp->disp, pp->jitter);
656 * refclock_receive - simulate the receive and packet procedures
674 current_time, stoa(&peer->srcadr));
682 pp = peer->procptr;
683 pp->inpoll = FALSE;
684 peer->leap = pp->leap;
685 if (peer->leap == LEAP_NOTINSYNC)
688 peer->received++;
689 peer->timereceived = current_time;
690 if (!peer->reach) {
692 peer->timereachable = current_time;
694 peer->reach = (peer->reach << (peer->reach & 1)) | 1;
695 peer->reftime = pp->lastref;
696 peer->aorg = pp->lastrec;
697 peer->rootdisp = pp->disp;
698 get_systime(&peer->dst);
702 clock_filter(peer, pp->offset, 0., pp->jitter);
705 if (sys_peer->refclktype == REFCLK_ATOM_PPS &&
706 peer->refclktype != REFCLK_ATOM_PPS)
707 pp->fudgetime1 -= pp->offset * FUDGEFAC;
713 * refclock_gtlin - groom next input line and extract timestamp
737 dpend = dp + bmax - 1; /* leave room for NUL pad */
738 sp = (const char *)rbufp->recv_buffer;
739 spend = sp + rbufp->recv_length;
752 dlen = dp - lineptr;
755 *tsptr = rbufp->recv_time;
757 rbufp->fd, ulfptoa(&rbufp->recv_time, 6), dlen,
766 * refclock_gtraw - get next line/chunk of data
790 bmax -= 1; /* leave room for trailing NUL */
791 if (bmax > rbufp->recv_length)
792 bmax = rbufp->recv_length;
793 memcpy(lineptr, rbufp->recv_buffer, bmax);
796 *tsptr = rbufp->recv_time;
798 rbufp->fd, ulfptoa(&rbufp->recv_time, 6), bmax,
836 refnumtoa(&peer->srcadr), what,
843 refnumtoa(&peer->srcadr), what,
859 if ( ! (peer && peer->procptr)) {
863 refnumtoa(&peer->srcadr), what);
867 return refclock_fdwrite(peer, peer->procptr->io.fd,
889 if (rio->io_input != NULL && (*rio->io_input)(rb) == 0) {
891 * data was consumed - nothing to pass up
920 rio = &rb->recv_peer->procptr->io;
926 if (rio->io_input == NULL || (*rio->io_input)(rb) != 0) {
927 rio->recvcount++;
930 (*rio->clock_recv)(rb);
943 * refclock_open - open serial port for reference clock
947 * returns -1.
990 return -1;
994 return -1;
998 return -1;
1006 * buffer. Since we have non-blocking IO available, this is a
1018 * refclock_setup - initialize terminal interface structure
1032 * (line-oriented) mode at specified line speed, 8 bits and no
1060 ttyp->c_iflag = IGNBRK | IGNPAR | ICRNL;
1061 ttyp->c_oflag = 0;
1062 ttyp->c_cflag = CS8 | CLOCAL | CREAD;
1064 /* HP Z3801A needs 7-bit, odd parity */
1065 ttyp->c_cflag = CS7 | PARENB | PARODD | CLOCAL | CREAD;
1070 ttyp->c_cc[i] = '\0';
1088 ttyp->c_cflag &= ~CLOCAL;
1093 * Set raw and echo modes. These can be changed on-fly.
1095 ttyp->c_lflag = ICANON;
1097 ttyp->c_lflag = 0;
1098 ttyp->c_iflag = 0;
1099 ttyp->c_cc[VMIN] = 1;
1102 ttyp->c_lflag |= ECHO;
1144 ttyp->c_iflag = IGNBRK | IGNPAR | ICRNL;
1145 ttyp->c_oflag = 0;
1146 ttyp->c_cflag = speed | CS8 | CLOCAL | CREAD;
1148 ttyp->c_cc[i] = '\0';
1166 ttyp->c_cflag &= ~CLOCAL;
1171 * Set raw and echo modes. These can be changed on-fly.
1173 ttyp->c_lflag = ICANON;
1175 ttyp->c_lflag = 0;
1176 ttyp->c_iflag = 0;
1177 ttyp->c_cc[VMIN] = 1;
1202 ttyp->sg_ispeed = ttyp->sg_ospeed = speed;
1203 ttyp->sg_flags = EVENP | ODDP | CRMOD;
1217 * refclock_ioctl - set serial port control functions
1219 * This routine attempts to hide the internal, system-specific details
1242 * refclock_control - set and/or return clock values
1271 peer = findexistingpeer(srcadr, NULL, NULL, -1, 0, NULL);
1276 INSIST(peer->procptr != NULL);
1277 pp = peer->procptr;
1283 if (in->haveflags & CLK_HAVETIME1)
1284 pp->fudgetime1 = in->fudgetime1;
1285 if (in->haveflags & CLK_HAVETIME2)
1286 pp->fudgetime2 = in->fudgetime2;
1287 if (in->haveflags & CLK_HAVEVAL1)
1288 peer->stratum = pp->stratum = (u_char)in->fudgeval1;
1289 if (in->haveflags & CLK_HAVEVAL2)
1290 peer->refid = pp->refid = in->fudgeval2;
1291 if (in->haveflags & CLK_HAVEFLAG1) {
1292 pp->sloppyclockflag &= ~CLK_FLAG1;
1293 pp->sloppyclockflag |= in->flags & CLK_FLAG1;
1295 if (in->haveflags & CLK_HAVEFLAG2) {
1296 pp->sloppyclockflag &= ~CLK_FLAG2;
1297 pp->sloppyclockflag |= in->flags & CLK_FLAG2;
1299 if (in->haveflags & CLK_HAVEFLAG3) {
1300 pp->sloppyclockflag &= ~CLK_FLAG3;
1301 pp->sloppyclockflag |= in->flags & CLK_FLAG3;
1303 if (in->haveflags & CLK_HAVEFLAG4) {
1304 pp->sloppyclockflag &= ~CLK_FLAG4;
1305 pp->sloppyclockflag |= in->flags & CLK_FLAG4;
1307 if (in->haveflags & CLK_HAVEMINJIT)
1308 pp->fudgeminjitter = in->fudgeminjitter;
1315 out->fudgeval1 = pp->stratum;
1316 out->fudgeval2 = pp->refid;
1317 out->haveflags = CLK_HAVEVAL1 | CLK_HAVEVAL2;
1318 out->fudgetime1 = pp->fudgetime1;
1319 if (0.0 != out->fudgetime1)
1320 out->haveflags |= CLK_HAVETIME1;
1321 out->fudgetime2 = pp->fudgetime2;
1322 if (0.0 != out->fudgetime2)
1323 out->haveflags |= CLK_HAVETIME2;
1324 out->flags = (u_char) pp->sloppyclockflag;
1325 if (CLK_FLAG1 & out->flags)
1326 out->haveflags |= CLK_HAVEFLAG1;
1327 if (CLK_FLAG2 & out->flags)
1328 out->haveflags |= CLK_HAVEFLAG2;
1329 if (CLK_FLAG3 & out->flags)
1330 out->haveflags |= CLK_HAVEFLAG3;
1331 if (CLK_FLAG4 & out->flags)
1332 out->haveflags |= CLK_HAVEFLAG4;
1333 out->fudgeminjitter = pp->fudgeminjitter;
1334 if (0.0 != out->fudgeminjitter)
1335 out->haveflags |= CLK_HAVEMINJIT;
1337 out->timereset = current_time - pp->timestarted;
1338 out->polls = pp->polls;
1339 out->noresponse = pp->noreply;
1340 out->badformat = pp->badformat;
1341 out->baddata = pp->baddata;
1343 out->lastevent = pp->lastevent;
1344 out->currentstatus = pp->currentstatus;
1345 out->type = pp->type;
1346 out->clockdesc = pp->clockdesc;
1347 out->lencode = (u_short)pp->lencode;
1348 out->p_lastcode = pp->a_lastcode;
1354 if (refclock_conf[clktype]->clock_control != noentry)
1355 (refclock_conf[clktype]->clock_control)(unit, in, out, peer);
1360 * refclock_buginfo - return debugging info
1387 peer = findexistingpeer(srcadr, NULL, NULL, -1, 0, NULL);
1389 if (NULL == peer || NULL == peer->procptr)
1392 pp = peer->procptr;
1397 bug->nvalues = 8;
1398 bug->svalues = 0x0000003f;
1399 bug->values[0] = pp->year;
1400 bug->values[1] = pp->day;
1401 bug->values[2] = pp->hour;
1402 bug->values[3] = pp->minute;
1403 bug->values[4] = pp->second;
1404 bug->values[5] = pp->nsec;
1405 bug->values[6] = pp->yearstart;
1406 bug->values[7] = pp->coderecv;
1407 bug->stimes = 0xfffffffc;
1408 bug->times[0] = pp->lastref;
1409 bug->times[1] = pp->lastrec;
1410 for (u = 2; u < bug->ntimes; u++)
1411 DTOLFP(pp->filter[u - 2], &bug->times[u]);
1416 if (refclock_conf[clktype]->clock_buginfo != noentry)
1417 (refclock_conf[clktype]->clock_buginfo)(unit, bug, peer);
1423 * refclock_ppsapi - initialize/update ppsapi
1434 if (ap->handle == 0) {
1435 if (time_pps_create(fddev, &ap->handle) < 0) {
1440 ZERO(ap->ts); /* [Bug 2689] defined INIT state */
1447 * refclock_params - set ppsapi parameters
1458 ZERO(ap->pps_params);
1459 ap->pps_params.api_version = PPS_API_VERS_1;
1468 ap->pps_params.mode = PPS_TSFMT_TSPEC | PPS_CAPTURECLEAR;
1470 ap->pps_params.mode = PPS_TSFMT_TSPEC | PPS_CAPTUREASSERT;
1471 if (time_pps_setparams(ap->handle, &ap->pps_params) < 0) {
1485 if (time_pps_kcbind(ap->handle, PPS_KC_HARDPPS,
1486 ap->pps_params.mode & ~PPS_TSFMT_TSPEC,
1503 * refclock_pps - called once per second
1506 * timestamp from the kernel and saves the sign-extended fraction in
1526 pp = peer->procptr;
1527 if (ap->handle == 0)
1530 if (ap->pps_params.mode == 0 && sys_leap != LEAP_NOTINSYNC) {
1531 if (refclock_params(pp->sloppyclockflag, ap) < 1)
1536 if (time_pps_fetch(ap->handle, PPS_TSFMT_TSPEC, &pps_info,
1541 timeout = ap->ts; /* save old timestamp for check */
1542 if (ap->pps_params.mode & PPS_CAPTUREASSERT)
1543 ap->ts = pps_info.assert_timestamp;
1544 else if (ap->pps_params.mode & PPS_CAPTURECLEAR)
1545 ap->ts = pps_info.clear_timestamp;
1549 /* [Bug 2689] Discard the first sample we read -- if the PPS
1552 * is all-zero, we consider this sample unrealiable and drop it.
1558 * Note 2: If we ever again get an all-zero PPS sample, the next
1569 if (0 == memcmp(&timeout, &ap->ts, sizeof(timeout)))
1574 * fold the correction into the [-0.5s,0.5s] range. Handle
1577 dtemp = ap->ts.tv_nsec / 1e9;
1578 dcorr = modf((pp->fudgetime1 - dtemp), &trash);
1580 dcorr -= 1.0;
1581 else if (dcorr < -0.5)
1584 /* phase gate check: avoid wobbling by +/-1s when too close to
1585 * the switch-over point. We allow +/-400ms max phase deviation.
1586 * The trade-off is clear: The smaller the limit, the less
1587 * sensitive to sampling noise the clock becomes. OTOH the
1597 pp->lastrec.l_ui = (u_int32)ap->ts.tv_sec + JAN_1970;
1598 pp->lastrec.l_uf = (u_int32)(dtemp * FRAC);
1605 dcorr, pp->fudgetime1);
1613 * -------------------------------------------------------------------
1614 * refclock_ppsaugment(...) -- correlate with PPS edge
1629 * +/-100ms precision should be enough. But since the fudge value will
1641 * - no PPS/ATOM unit given
1642 * - PPS stamp is stale (that is, the difference between the PPS stamp
1644 * - The phase difference is too close to 0.5, and the decision wether
1645 * to move up or down is too sensitive to noise.
1649 * -------------------------------------------------------------------
1683 if (time_pps_fetch(ap->handle, PPS_TSFMT_TSPEC,
1688 if (ap->pps_params.mode & PPS_CAPTUREASSERT)
1690 else if (ap->pps_params.mode & PPS_CAPTURECLEAR)
1700 /* Get difference between PPS stamp (--> yield) and receive time
1701 * (--> base)
1709 phase = delta->l_ui;
1710 if (phase >= 2 && phase < (uint32_t)-2)
1714 * move up or down is becoming noise sensitive. That is, we
1715 * might amplify usec noise between samples into seconds with a
1721 phase = delta->l_uf;
1723 return FALSE; /* we're in the noise lock gap */
1725 /* sign-extend fraction into seconds */
1726 delta->l_ui = UINT32_C(0) - ((phase >> 31) & 1);
1745 * -------------------------------------------------------------------
1763 if (pp->inpoll || (peer->procptr != pp) ||
1764 ((peer->reach == 0xFF) && (peer->disp <= MAXDISTANCE)))
1769 if (peer->nextdate <= limit)
1775 needs = peer->reach;
1776 needs -= (needs >> 1) & 0x55;
1788 * re-adjusted, but maybe some time the hint will work...)
1790 peer->hpoll = peer->minpoll;
1791 peer->nextdate = limit;
1795 * -------------------------------------------------------------------
1806 if (len == (size_t)-1)
1807 len = strnlen(tc, sizeof(pp->a_lastcode) - 1);
1808 else if (len >= sizeof(pp->a_lastcode))
1809 len = sizeof(pp->a_lastcode) - 1;
1811 pp->lencode = (u_short)len;
1812 memcpy(pp->a_lastcode, tc, len);
1813 pp->a_lastcode[len] = '\0';
1826 len = vsnprintf(pp->a_lastcode, sizeof(pp->a_lastcode), fmt, va);
1829 } else if (len >= sizeof(pp->a_lastcode)) {
1830 len = sizeof(pp->a_lastcode) - 1;
1833 pp->lencode = (u_short)len;
1834 pp->a_lastcode[len] = '\0';