xref: /netbsd-src/external/bsd/ntp/dist/util/pps-api.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: pps-api.c,v 1.5 2020/05/25 20:47:37 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel 
5abb0f93cSkardel Try to run this program to see what the PPS-API finds. You give it the
6abb0f93cSkardel device as argument and you may have to modify the pp.mode = BLA assignment.
7abb0f93cSkardel 
8abb0f93cSkardel Poul-Henning
9abb0f93cSkardel 
10abb0f93cSkardel */
11abb0f93cSkardel 
12abb0f93cSkardel #include <stdio.h>
13abb0f93cSkardel #include <errno.h>
14abb0f93cSkardel #include <fcntl.h>
15abb0f93cSkardel #include <err.h>
16abb0f93cSkardel #include <sys/types.h>
17abb0f93cSkardel #include <time.h>
18abb0f93cSkardel #include <sys/timepps.h>
19abb0f93cSkardel #include <sys/termios.h>
20abb0f93cSkardel 
21abb0f93cSkardel #define timespecsub(vvp, uvp)                                           \
22abb0f93cSkardel         do {                                                            \
23abb0f93cSkardel                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
24abb0f93cSkardel                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
25abb0f93cSkardel                 if ((vvp)->tv_nsec < 0) {                               \
26abb0f93cSkardel                         (vvp)->tv_sec--;                                \
27abb0f93cSkardel                         (vvp)->tv_nsec += 1000000000;                   \
28abb0f93cSkardel                 }                                                       \
29abb0f93cSkardel         } while (0)
30abb0f93cSkardel 
31abb0f93cSkardel 
32abb0f93cSkardel void
Chew(struct timespec * tsa,struct timespec * tsc,unsigned sa,unsigned sc)33abb0f93cSkardel Chew(struct timespec *tsa, struct timespec *tsc, unsigned sa, unsigned sc)
34abb0f93cSkardel {
35abb0f93cSkardel 	static int idx;
36abb0f93cSkardel 	struct timespec ts;
37abb0f93cSkardel 
38abb0f93cSkardel 	printf("%d.%09d ", tsa->tv_sec, tsa->tv_nsec);
39abb0f93cSkardel 	printf("%d.%09d ", tsc->tv_sec, tsc->tv_nsec);
40abb0f93cSkardel 	printf("%u %u ", sa, sc);
41abb0f93cSkardel 
42abb0f93cSkardel 	ts = *tsc;
43abb0f93cSkardel 	timespecsub(&ts,tsa);
44abb0f93cSkardel 	printf("%.9f ", ts.tv_sec + ts.tv_nsec / 1e9);
45abb0f93cSkardel 	printf("\n");
46abb0f93cSkardel 	fflush(stdout);
47abb0f93cSkardel }
48abb0f93cSkardel 
49abb0f93cSkardel int
main(int argc,char ** argv)50abb0f93cSkardel main(int argc, char **argv)
51abb0f93cSkardel {
52abb0f93cSkardel 	int fd;
53abb0f93cSkardel 	pps_info_t pi;
54abb0f93cSkardel 	pps_params_t pp;
55abb0f93cSkardel 	pps_handle_t ph;
56abb0f93cSkardel 	int i, mode;
57abb0f93cSkardel 	u_int olda, oldc;
58abb0f93cSkardel 	double d = 0;
59abb0f93cSkardel 	struct timespec to;
60abb0f93cSkardel 
61abb0f93cSkardel 	if (argc < 2)
62abb0f93cSkardel 		argv[1] = "/dev/cuaa1";
63abb0f93cSkardel 	setbuf(stdout, 0);
64abb0f93cSkardel 	fd = open(argv[1], O_RDONLY);
65abb0f93cSkardel 	if (fd < 0)
66abb0f93cSkardel 		err(1, argv[1]);
67abb0f93cSkardel 	i = time_pps_create(fd, &ph);
68abb0f93cSkardel 	if (i < 0)
69abb0f93cSkardel 		err(1, "time_pps_create");
70abb0f93cSkardel 
71abb0f93cSkardel 	i = time_pps_getcap(ph, &mode);
72abb0f93cSkardel 	if (i < 0)
73abb0f93cSkardel 		err(1, "time_pps_getcap");
74abb0f93cSkardel 
75abb0f93cSkardel 	pp.mode = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
76abb0f93cSkardel 	pp.mode = PPS_CAPTUREBOTH;
77abb0f93cSkardel 	/* pp.mode = PPS_CAPTUREASSERT; */
78abb0f93cSkardel 
79abb0f93cSkardel 	i = time_pps_setparams(ph, &pp);
80abb0f93cSkardel 	if (i < 0)
81abb0f93cSkardel 		err(1, "time_pps_setparams");
82abb0f93cSkardel 
83abb0f93cSkardel 	while (1) {
84abb0f93cSkardel 		to.tv_nsec = 0;
85abb0f93cSkardel 		to.tv_sec = 0;
86abb0f93cSkardel 		i = time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pi, &to);
87abb0f93cSkardel 		if (i < 0)
88abb0f93cSkardel 			err(1, "time_pps_fetch");
89abb0f93cSkardel 		if (olda == pi.assert_sequence &&
90abb0f93cSkardel 		    oldc == pi.clear_sequence) {
91abb0f93cSkardel 			usleep(10000);
92abb0f93cSkardel 			continue;
93abb0f93cSkardel 		}
94abb0f93cSkardel 
95abb0f93cSkardel 		Chew(&pi.assert_timestamp, &pi.clear_timestamp,
96abb0f93cSkardel 			pi.assert_sequence, pi.clear_sequence);
97abb0f93cSkardel 		olda = pi.assert_sequence;
98abb0f93cSkardel 		oldc = pi.clear_sequence;
99abb0f93cSkardel 	}
100abb0f93cSkardel 
101abb0f93cSkardel 	return(0);
102abb0f93cSkardel }
103