PPS-Client  2.0.0
Client for synchronizing the system clock to a GPS PPS source
timepps.h
Go to the documentation of this file.
1 /*
2  * timepps.h -- PPS API main header
3  *
4  * Copyright (C) 2005-2007 Rodolfo Giometti <giometti@linux.it>
5  * Copyright (C) 2009-2011 Alexander Gordeev <alex@gordick.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  */
17 
18 #ifndef _SYS_TIMEPPS_H_
19 #define _SYS_TIMEPPS_H_
20 
21 #include <errno.h>
22 #include <unistd.h>
23 #include <sys/time.h>
24 #include <sys/ioctl.h>
25 #include <linux/types.h>
26 #include <linux/pps.h>
27 
28 #define LINUXPPS 1 /* signal we are using LinuxPPS */
29 
30 /*
31  * New data structures
32  */
33 
34 struct ntp_fp {
35  unsigned int integral;
36  unsigned int fractional;
37 };
38 
39 union pps_timeu {
40  struct timespec tspec;
41  struct ntp_fp ntpfp;
42  unsigned long longpad[3];
43 };
44 
45 struct pps_info {
46  unsigned long assert_sequence; /* seq. num. of assert event */
47  unsigned long clear_sequence; /* seq. num. of clear event */
48  union pps_timeu assert_tu; /* time of assert event */
49  union pps_timeu clear_tu; /* time of clear event */
50  int current_mode; /* current mode bits */
51 };
52 
53 struct pps_params {
54  int api_version; /* API version # */
55  int mode; /* mode bits */
56  union pps_timeu assert_off_tu; /* offset compensation for assert */
57  union pps_timeu clear_off_tu; /* offset compensation for clear */
58 };
59 
60 typedef int pps_handle_t; /* represents a PPS source */
61 typedef unsigned long pps_seq_t; /* sequence number */
62 typedef struct ntp_fp ntp_fp_t; /* NTP-compatible time stamp */
63 typedef union pps_timeu pps_timeu_t; /* generic data type for time stamps */
64 typedef struct pps_info pps_info_t;
65 typedef struct pps_params pps_params_t;
66 
67 #define assert_timestamp assert_tu.tspec
68 #define clear_timestamp clear_tu.tspec
69 
70 #define assert_timestamp_ntpfp assert_tu.ntpfp
71 #define clear_timestamp_ntpfp clear_tu.ntpfp
72 
73 #define assert_offset assert_off_tu.tspec
74 #define clear_offset clear_off_tu.tspec
75 
76 #define assert_offset_ntpfp assert_off_tu.ntpfp
77 #define clear_offset_ntpfp clear_off_tu.ntpfp
78 
79 /*
80  * The PPS API
81  */
82 
83 static __inline int time_pps_create(int source, pps_handle_t *handle)
84 {
85  int ret;
86  struct pps_kparams dummy;
87 
88  if (!handle) {
89  errno = EINVAL;
90  return -1;
91  }
92 
93  /* First we check if current device is a valid PPS one by
94  * doing a dummy PPS_GETPARAMS...
95  */
96  ret = ioctl(source, PPS_GETPARAMS, &dummy);
97  if (ret) {
98  errno = EOPNOTSUPP;
99  return -1;
100  }
101 
102  /* ... then since in LinuxPPS there are no differences between a
103  * "PPS source" and a "PPS handle", we simply return the same value.
104  */
105  *handle = source;
106 
107  return 0;
108 }
109 
110 static __inline int time_pps_destroy(pps_handle_t handle)
111 {
112  return close(handle);
113 }
114 
115 static __inline int time_pps_getparams(pps_handle_t handle,
116  pps_params_t *ppsparams)
117 {
118  int ret;
119  struct pps_kparams __ppsparams;
120 
121  ret = ioctl(handle, PPS_GETPARAMS, &__ppsparams);
122 
123  ppsparams->api_version = __ppsparams.api_version;
124  ppsparams->mode = __ppsparams.mode;
125  ppsparams->assert_off_tu.tspec.tv_sec = __ppsparams.assert_off_tu.sec;
126  ppsparams->assert_off_tu.tspec.tv_nsec = __ppsparams.assert_off_tu.nsec;
127  ppsparams->clear_off_tu.tspec.tv_sec = __ppsparams.clear_off_tu.sec;
128  ppsparams->clear_off_tu.tspec.tv_nsec = __ppsparams.clear_off_tu.nsec;
129 
130  return ret;
131 }
132 
133 static __inline int time_pps_setparams(pps_handle_t handle,
134  const pps_params_t *ppsparams)
135 {
136  struct pps_kparams __ppsparams;
137 
138  __ppsparams.api_version = ppsparams->api_version;
139  __ppsparams.mode = ppsparams->mode;
140  __ppsparams.assert_off_tu.sec = ppsparams->assert_off_tu.tspec.tv_sec;
141  __ppsparams.assert_off_tu.nsec = ppsparams->assert_off_tu.tspec.tv_nsec;
142  __ppsparams.clear_off_tu.sec = ppsparams->clear_off_tu.tspec.tv_sec;
143  __ppsparams.clear_off_tu.nsec = ppsparams->clear_off_tu.tspec.tv_nsec;
144 
145  return ioctl(handle, PPS_SETPARAMS, &__ppsparams);
146 }
147 
148 /* Get capabilities for handle */
149 static __inline int time_pps_getcap(pps_handle_t handle, int *mode)
150 {
151  return ioctl(handle, PPS_GETCAP, mode);
152 }
153 
154 static __inline int time_pps_fetch(pps_handle_t handle, const int tsformat,
155  pps_info_t *ppsinfobuf,
156  const struct timespec *timeout)
157 {
158  struct pps_fdata __fdata;
159  int ret;
160 
161  /* Sanity checks */
162  if (tsformat != PPS_TSFMT_TSPEC) {
163  errno = EINVAL;
164  return -1;
165  }
166 
167  if (timeout) {
168  __fdata.timeout.sec = timeout->tv_sec;
169  __fdata.timeout.nsec = timeout->tv_nsec;
170  __fdata.timeout.flags = ~PPS_TIME_INVALID;
171  } else
172  __fdata.timeout.flags = PPS_TIME_INVALID;
173 
174  ret = ioctl(handle, PPS_FETCH, &__fdata);
175 
176  ppsinfobuf->assert_sequence = __fdata.info.assert_sequence;
177  ppsinfobuf->clear_sequence = __fdata.info.clear_sequence;
178  ppsinfobuf->assert_tu.tspec.tv_sec = __fdata.info.assert_tu.sec;
179  ppsinfobuf->assert_tu.tspec.tv_nsec = __fdata.info.assert_tu.nsec;
180  ppsinfobuf->clear_tu.tspec.tv_sec = __fdata.info.clear_tu.sec;
181  ppsinfobuf->clear_tu.tspec.tv_nsec = __fdata.info.clear_tu.nsec;
182  ppsinfobuf->current_mode = __fdata.info.current_mode;
183 
184  return ret;
185 }
186 
187 #ifdef PPS_KC_BIND
188 
189 static __inline int time_pps_kcbind(pps_handle_t handle,
190  const int kernel_consumer,
191  const int edge, const int tsformat)
192 {
193  struct pps_bind_args __bind_args;
194 
195  __bind_args.tsformat = tsformat;
196  __bind_args.edge = edge;
197  __bind_args.consumer = kernel_consumer;
198 
199  return ioctl(handle, PPS_KC_BIND, &__bind_args);
200 }
201 
202 #else /* !PPS_KC_BIND */
203 
204 static __inline int time_pps_kcbind(pps_handle_t handle,
205  const int kernel_consumer,
206  const int edge, const int tsformat)
207 {
208  /* LinuxPPS doesn't implement kernel consumer feature */
209  errno = EOPNOTSUPP;
210  return -1;
211 }
212 
213 #endif /* PPS_KC_BIND */
214 
215 #endif /* _SYS_TIMEPPS_H_ */
pps_seq_t
unsigned long pps_seq_t
Definition: timepps.h:61
pps_params
Definition: timepps.h:53
pps_timeu::ntpfp
struct ntp_fp ntpfp
Definition: timepps.h:41
pps_info::current_mode
int current_mode
Definition: timepps.h:50
pps_timeu
Definition: timepps.h:39
pps_info
Definition: timepps.h:45
ntp_fp::fractional
unsigned int fractional
Definition: timepps.h:36
pps_info::clear_tu
union pps_timeu clear_tu
Definition: timepps.h:49
pps_params::api_version
int api_version
Definition: timepps.h:54
pps_info::assert_tu
union pps_timeu assert_tu
Definition: timepps.h:48
pps_info::assert_sequence
unsigned long assert_sequence
Definition: timepps.h:46
pps_handle_t
int pps_handle_t
Definition: timepps.h:60
pps_params::assert_off_tu
union pps_timeu assert_off_tu
Definition: timepps.h:56
pps_timeu::tspec
struct timespec tspec
Definition: timepps.h:40
pps_params::clear_off_tu
union pps_timeu clear_off_tu
Definition: timepps.h:57
ntp_fp
Definition: timepps.h:34
pps_info::clear_sequence
unsigned long clear_sequence
Definition: timepps.h:47
pps_params::mode
int mode
Definition: timepps.h:55
pps_timeu::longpad
unsigned long longpad[3]
Definition: timepps.h:42
ntp_fp::integral
unsigned int integral
Definition: timepps.h:35