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