summaryrefslogtreecommitdiffstats
path: root/sys_null.c
blob: 3a0d5f6fdd6b7c796e4d44037146aa71f7c23236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2017
 * 
 * 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.
 * 
 **********************************************************************

  =======================================================================

  Null clock driver for operation with no clock control.
  */

#include "config.h"

#include "sysincl.h"

#include "sys_null.h"

#include "local.h"
#include "localp.h"
#include "logging.h"
#include "util.h"

/* Current frequency offset of the system clock (in ppm) */
static double freq;

/* Offset of the system clock at the last update */
static double offset_register;

/* Time of the last update */
static struct timespec last_update;

/* Minimum interval between updates when frequency is constant */
#define MIN_UPDATE_INTERVAL 1000.0

/* ================================================== */

static void
update_offset(void)
{
  struct timespec now;
  double duration;

  LCL_ReadRawTime(&now);
  duration = UTI_DiffTimespecsToDouble(&now, &last_update);
  offset_register += 1.0e-6 * freq * duration;
  last_update = now;

  DEBUG_LOG("System clock offset=%e freq=%f", offset_register, freq);
}

/* ================================================== */

static double
read_frequency(void)
{
  return freq;
}

/* ================================================== */

static double
set_frequency(double freq_ppm)
{
  update_offset();
  freq = freq_ppm;

  return freq;
}

/* ================================================== */

static void
accrue_offset(double offset, double corr_rate)
{
  offset_register += offset;
}

/* ================================================== */

static int
apply_step_offset(double offset)
{
  return 0;
}

/* ================================================== */

static void
offset_convert(struct timespec *raw, double *corr, double *err)
{
  double duration;

  duration = UTI_DiffTimespecsToDouble(raw, &last_update);

  if (duration > MIN_UPDATE_INTERVAL) {
    update_offset();
    duration = 0.0;
  }

  *corr = -1.0e-6 * freq * duration - offset_register;

  if (err)
    *err = 0.0;
}

/* ================================================== */

void
SYS_Null_Initialise(void)
{
  offset_register = 0.0;
  LCL_ReadRawTime(&last_update);

  lcl_RegisterSystemDrivers(read_frequency, set_frequency, accrue_offset,
                            apply_step_offset, offset_convert, NULL, NULL);

  LOG(LOGS_INFO, "Disabled control of system clock");
}

/* ================================================== */

void
SYS_Null_Finalise(void)
{
}