/* chronyd/chronyc - Programs for keeping computer clocks accurate. ********************************************************************** * Copyright (C) Richard P. Curnow 1997-2003 * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ********************************************************************** ======================================================================= Routines for implementing manual input of real time. The daemon accepts manual time input over the control connection, and adjusts the system time to match. Besides this, though, it can determine the average rate of time loss or gain of the local system and adjust the frequency accordingly. */ #include "config.h" #include "sysincl.h" #include "manual.h" #include "logging.h" #include "local.h" #include "conf.h" #include "util.h" #include "ntp.h" #include "reference.h" #include "regress.h" static int enabled = 0; /* More recent samples at highest indices */ typedef struct { struct timespec when; /* This is our 'cooked' time */ double orig_offset; /*+ Not modified by slew samples */ double offset; /*+ if we are fast of the supplied reference */ double residual; /*+ regression residual (sign convention given by (measured-predicted)) */ } Sample; #define MIN_SAMPLE_SEPARATION 1.0 #define MAX_SAMPLES 16 static Sample samples[16]; static int n_samples; /* ================================================== */ static void slew_samples(struct timespec *raw, struct timespec *cooked, double dfreq, double doffset, LCL_ChangeType change_type, void *not_used); /* ================================================== */ void MNL_Initialise(void) { if (CNF_GetManualEnabled()) { enabled = 1; } else { enabled = 0; } n_samples = 0; LCL_AddParameterChangeHandler(slew_samples, NULL); } /* ================================================== */ void MNL_Finalise(void) { LCL_RemoveParameterChangeHandler(slew_samples, NULL); } /* ================================================== */ static void estimate_and_set_system(struct timespec *now, int offset_provided, double offset, double *reg_offset, double *dfreq_ppm, double *new_afreq_ppm) { double agos[MAX_SAMPLES], offsets[MAX_SAMPLES]; double b0, b1; int n_runs, best_start; /* Unused results from regression analyser */ int i; double freq = 0.0; double skew = 0.099999999; /* All 9's when printed to log file */ int found_freq; double slew_by; b0 = offset_provided ? offset : 0.0; b1 = freq = 0.0; found_freq = 0; if (n_samples > 1) { for (i=0; i max) { *n = max; } else { *n = n_samples; } for (i=0; i= n_samples)) { return 0; } /* Crunch the samples down onto the one being deleted */ for (i=index; i<(n_samples-1); i++) { samples[i] = samples[i+1]; } n_samples -= 1; /* Now re-estimate. NULLs because we don't want the parameters back in this case. */ LCL_ReadCookedTime(&now, NULL); estimate_and_set_system(&now, 0, 0.0, NULL, NULL, NULL); return 1; } /* ================================================== */