2 * Copyright (c) 1998-2003 Poul-Henning Kamp
4 * Please see src/share/examples/etc/bsd-style-copyright.
17 #include <sys/timepps.h>
19 static int aflag, Aflag, cflag, Cflag, eflag, uflag, vflag;
22 Chew(struct timespec *tsa, struct timespec *tsc, unsigned sa, unsigned sc)
24 printf("%jd .%09ld %u", (intmax_t)tsa->tv_sec, tsa->tv_nsec, sa);
25 printf(" %jd .%09ld %u\n", (intmax_t)tsc->tv_sec, tsc->tv_nsec, sc);
31 main(int argc, char **argv)
44 while ((i = getopt(argc, argv, "aAbBcCeo:uv")) != -1) {
46 case 'a': aflag = 1; break;
47 case 'A': Aflag = 1; break;
48 case 'b': aflag = 1; cflag = 1; break;
49 case 'B': Aflag = 1; Cflag = 1; break;
50 case 'c': cflag = 1; break;
51 case 'C': Cflag = 1; break;
52 case 'e': eflag = 1; break;
53 case 'o': ofn = optarg; break;
54 case 'u': uflag = 1; break;
55 case 'v': vflag = 1; break;
59 "Usage: ppsapitest [-aAcC] device\n");
64 fdo = fopen(ofn, "w");
66 err(1, "Cannot open %s", ofn);
73 fd = open(argv[0], O_RDONLY);
79 i = time_pps_create(fd, &ph);
81 err(1, "time_pps_create");
83 i = time_pps_getcap(ph, &mode);
85 err(1, "time_pps_getcap");
87 fprintf(stderr, "Supported modebits:");
88 if (mode & PPS_CAPTUREASSERT)
89 fprintf(stderr, " CAPTUREASSERT");
90 if (mode & PPS_CAPTURECLEAR)
91 fprintf(stderr, " CAPTURECLEAR");
92 if (mode & PPS_OFFSETASSERT)
93 fprintf(stderr, " OFFSETASSERT");
94 if (mode & PPS_OFFSETCLEAR)
95 fprintf(stderr, " OFFSETCLEAR");
96 if (mode & PPS_ECHOASSERT)
97 fprintf(stderr, " ECHOASSERT");
98 if (mode & PPS_ECHOCLEAR)
99 fprintf(stderr, " ECHOCLEAR");
100 if (mode & PPS_CANWAIT)
101 fprintf(stderr, " CANWAIT");
102 if (mode & PPS_CANPOLL)
103 fprintf(stderr, " CANPOLL");
104 if (mode & PPS_TSFMT_TSPEC)
105 fprintf(stderr, " TSPEC");
106 if (mode & PPS_TSFMT_NTPFP)
107 fprintf(stderr, " NTPFP");
108 fprintf(stderr, "\n");
111 if (!aflag && !cflag) {
112 if (mode & PPS_CAPTUREASSERT)
114 if (mode & PPS_CAPTURECLEAR)
117 if (!Aflag && !Cflag) {
122 if (Cflag && !(mode & PPS_CAPTURECLEAR))
123 errx(1, "-C but cannot capture on clear flank");
125 if (Aflag && !(mode & PPS_CAPTUREASSERT))
126 errx(1, "-A but cannot capture on assert flank");
128 i = time_pps_getparams(ph, &pp);
130 err(1, "time_pps_getparams():");
133 pp.mode |= PPS_CAPTUREASSERT;
135 pp.mode |= PPS_CAPTURECLEAR;
138 pp.mode |= PPS_ECHOASSERT;
141 pp.mode |= PPS_ECHOCLEAR;
143 if (!(pp.mode & PPS_TSFMT_TSPEC))
144 pp.mode |= PPS_TSFMT_TSPEC;
146 i = time_pps_setparams(ph, &pp);
148 err(1, "time_pps_setparams(mode %x):", pp.mode);
152 * Pick up first event outside the loop in order to not
153 * get something ancient into the outfile.
157 i = time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pi, &to);
159 err(1, "time_pps_fetch()");
160 olda = pi.assert_sequence;
161 oldc = pi.clear_sequence;
166 i = time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pi, &to);
168 err(1, "time_pps_fetch()");
169 if (oldc != pi.clear_sequence && Cflag)
171 else if (olda != pi.assert_sequence && Aflag)
178 if (fwrite(&pi, sizeof pi, 1, fdo) != 1)
179 err(1, "Write error on %s", ofn);
183 Chew(&pi.assert_timestamp, &pi.clear_timestamp,
184 pi.assert_sequence, pi.clear_sequence);
185 olda = pi.assert_sequence;
186 oldc = pi.clear_sequence;