File: refclock_pps.c

package info (click to toggle)
chrony 3.0-4+deb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,496 kB
  • sloc: ansic: 23,531; sh: 2,022; yacc: 866; makefile: 195
file content (169 lines) | stat: -rw-r--r-- 4,159 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

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

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

  PPSAPI refclock driver.

  */

#include "config.h"

#include "refclock.h"

#if FEAT_PPS

#if defined(HAVE_SYS_TIMEPPS_H)
#include <sys/timepps.h>
#elif defined(HAVE_TIMEPPS_H)
#include <timepps.h>
#endif

#include "logging.h"
#include "memory.h"
#include "util.h"

struct pps_instance {
  pps_handle_t handle;
  pps_seq_t last_seq;
  int edge_clear;
};

static int pps_initialise(RCL_Instance instance) {
  pps_handle_t handle;
  pps_params_t params;
  struct pps_instance *pps;
  int fd, edge_clear, mode;
  char *path;

  path = RCL_GetDriverParameter(instance);
  edge_clear = RCL_GetDriverOption(instance, "clear") ? 1 : 0;

  fd = open(path, O_RDWR);
  if (fd < 0) {
    LOG_FATAL(LOGF_Refclock, "open() failed on %s", path);
    return 0;
  }

  UTI_FdSetCloexec(fd);

  if (time_pps_create(fd, &handle) < 0) {
    LOG_FATAL(LOGF_Refclock, "time_pps_create() failed on %s", path);
    return 0;
  }

  if (time_pps_getcap(handle, &mode) < 0) {
    LOG_FATAL(LOGF_Refclock, "time_pps_getcap() failed on %s", path);
    return 0;
  }

  if (time_pps_getparams(handle, &params) < 0) {
    LOG_FATAL(LOGF_Refclock, "time_pps_getparams() failed on %s", path);
    return 0;
  }

  if (!edge_clear) {
    if (!(mode & PPS_CAPTUREASSERT)) {
      LOG_FATAL(LOGF_Refclock, "CAPTUREASSERT not supported on %s", path);
      return 0;
    }
    params.mode |= PPS_CAPTUREASSERT;
    params.mode &= ~PPS_CAPTURECLEAR;
  } else {
    if (!(mode & PPS_CAPTURECLEAR)) {
      LOG_FATAL(LOGF_Refclock, "CAPTURECLEAR not supported on %s", path);
      return 0;
    }
    params.mode |= PPS_CAPTURECLEAR;
    params.mode &= ~PPS_CAPTUREASSERT;
  }

  if (time_pps_setparams(handle, &params) < 0) {
    LOG_FATAL(LOGF_Refclock, "time_pps_setparams() failed on %s", path);
    return 0;
  }


  pps = MallocNew(struct pps_instance);
  pps->handle = handle;
  pps->last_seq = 0;
  pps->edge_clear = edge_clear;

  RCL_SetDriverData(instance, pps);
  return 1;
}

static void pps_finalise(RCL_Instance instance)
{
  struct pps_instance *pps; 

  pps = (struct pps_instance *)RCL_GetDriverData(instance);
  time_pps_destroy(pps->handle);
  Free(pps);
}

static int pps_poll(RCL_Instance instance)
{
  struct pps_instance *pps; 
  struct timespec ts;
  pps_info_t pps_info;
  pps_seq_t seq;

  pps = (struct pps_instance *)RCL_GetDriverData(instance);

  ts.tv_sec = 0;
  ts.tv_nsec = 0;

  if (time_pps_fetch(pps->handle, PPS_TSFMT_TSPEC, &pps_info, &ts) < 0) {
    LOG(LOGS_ERR, LOGF_Refclock, "time_pps_fetch() failed : %s", strerror(errno));
    return 0;
  }

  if (!pps->edge_clear) {
    seq = pps_info.assert_sequence;
    ts = pps_info.assert_timestamp;
  } else {
    seq = pps_info.clear_sequence;
    ts = pps_info.clear_timestamp;
  }

  if (seq == pps->last_seq || UTI_IsZeroTimespec(&ts)) {
    DEBUG_LOG(LOGF_Refclock, "PPS sample ignored seq=%lu ts=%s",
              seq, UTI_TimespecToString(&ts));
    return 0;
  }

  pps->last_seq = seq;

  return RCL_AddPulse(instance, &ts, 1.0e-9 * ts.tv_nsec);
}

RefclockDriver RCL_PPS_driver = {
  pps_initialise,
  pps_finalise,
  pps_poll
};

#else

RefclockDriver RCL_PPS_driver = { NULL, NULL, NULL };

#endif