summaryrefslogtreecommitdiffstats
path: root/tools/test/ppsapi/ppsapitest.c
blob: 78e73825e16c70ad97e4d1488a2868dbb42dfe69 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*-
 * Copyright (c) 1998-2003 Poul-Henning Kamp
 *
 * Please see src/share/examples/etc/bsd-style-copyright.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <sys/timepps.h>

static int aflag, Aflag, cflag, Cflag, eflag, uflag, vflag;

static void
Chew(struct timespec *tsa, struct timespec *tsc, unsigned sa, unsigned sc)
{
	printf("%jd .%09ld %u", (intmax_t)tsa->tv_sec, tsa->tv_nsec, sa);
	printf(" %jd .%09ld %u\n", (intmax_t)tsc->tv_sec, tsc->tv_nsec, sc);
	if (uflag)
		fflush(stdout);
}

int
main(int argc, char **argv)
{
	int fd;
	FILE *fdo;
	pps_info_t pi;
	pps_params_t pp;
	pps_handle_t ph;
	int i, mode;
	u_int olda, oldc;
	struct timespec to;
	char const *ofn;

	ofn = NULL;
	while ((i = getopt(argc, argv, "aAbBcCeo:uv")) != -1) {
		switch (i) {
		case 'a': aflag = 1; break;
		case 'A': Aflag = 1; break;
		case 'b': aflag = 1; cflag = 1; break;
		case 'B': Aflag = 1; Cflag = 1; break;
		case 'c': cflag = 1; break;
		case 'C': Cflag = 1; break;
		case 'e': eflag = 1; break;
		case 'o': ofn = optarg; break;
		case 'u': uflag = 1; break;
		case 'v': vflag = 1; break;
		case '?':
		default:
			fprintf(stderr,
			    "Usage: ppsapitest [-aAcC] device\n");
			exit (1);
		}
	}
	if (ofn != NULL) {
		fdo = fopen(ofn, "w");
		if (fdo == NULL)
			err(1, "Cannot open %s", ofn);
	} else {
		fdo = NULL;
	}
	argc -= optind;
	argv += optind;
	if (argc > 0) {
		fd = open(argv[0], O_RDONLY);
		if (fd < 0) 
			err(1, argv[0]);
	} else {
		fd = 0;
	}
	i = time_pps_create(fd, &ph);
	if (i < 0)
		err(1, "time_pps_create");

	i = time_pps_getcap(ph, &mode);
	if (i < 0)
		err(1, "time_pps_getcap");
	if (vflag) {
		fprintf(stderr, "Supported modebits:");
		if (mode & PPS_CAPTUREASSERT)
			fprintf(stderr, " CAPTUREASSERT");
		if (mode & PPS_CAPTURECLEAR)
			fprintf(stderr, " CAPTURECLEAR");
		if (mode & PPS_OFFSETASSERT)
			fprintf(stderr, " OFFSETASSERT");
		if (mode & PPS_OFFSETCLEAR)
			fprintf(stderr, " OFFSETCLEAR");
		if (mode & PPS_ECHOASSERT)
			fprintf(stderr, " ECHOASSERT");
		if (mode & PPS_ECHOCLEAR)
			fprintf(stderr, " ECHOCLEAR");
		if (mode & PPS_CANWAIT)
			fprintf(stderr, " CANWAIT");
		if (mode & PPS_CANPOLL)
			fprintf(stderr, " CANPOLL");
		if (mode & PPS_TSFMT_TSPEC)
			fprintf(stderr, " TSPEC");
		if (mode & PPS_TSFMT_NTPFP)
			fprintf(stderr, " NTPFP");
		fprintf(stderr, "\n");
	}

	if (!aflag && !cflag) {
		if (mode & PPS_CAPTUREASSERT)
			aflag = 1;
		if (mode & PPS_CAPTURECLEAR)
			cflag = 1;
	}
	if (!Aflag && !Cflag) {
		Aflag = aflag;
		Cflag = cflag;
	}

	if (Cflag && !(mode & PPS_CAPTURECLEAR))
		errx(1, "-C but cannot capture on clear flank");

	if (Aflag && !(mode & PPS_CAPTUREASSERT))
		errx(1, "-A but cannot capture on assert flank");

	i = time_pps_getparams(ph, &pp);
	if (i < 0)
		err(1, "time_pps_getparams():");

	if (aflag)
		pp.mode |= PPS_CAPTUREASSERT;
	if (cflag)
		pp.mode |= PPS_CAPTURECLEAR;

	if (eflag & aflag)
		pp.mode |= PPS_ECHOASSERT;

	if (eflag & cflag)
		pp.mode |= PPS_ECHOCLEAR;

	if (!(pp.mode & PPS_TSFMT_TSPEC))
		pp.mode |= PPS_TSFMT_TSPEC;
	
	i = time_pps_setparams(ph, &pp);
	if (i < 0) {
		err(1, "time_pps_setparams(mode %x):", pp.mode);
	}

	/*
	 * Pick up first event outside the loop in order to not
	 * get something ancient into the outfile.
	 */
	to.tv_nsec = 0;
	to.tv_sec = 0;
	i = time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pi, &to);
	if (i < 0)
		err(1, "time_pps_fetch()");
	olda = pi.assert_sequence;
	oldc = pi.clear_sequence;

	while (1) {
		to.tv_nsec = 0;
		to.tv_sec = 0;
		i = time_pps_fetch(ph, PPS_TSFMT_TSPEC, &pi, &to);
		if (i < 0)
			err(1, "time_pps_fetch()");
		if (oldc != pi.clear_sequence && Cflag)
			;
		else if (olda != pi.assert_sequence && Aflag)
			;
		else {
			usleep(10000);
			continue;
		}
		if (fdo != NULL) {
			if (fwrite(&pi, sizeof pi, 1, fdo) != 1)
				err(1, "Write error on %s", ofn);
			if (uflag)
				fflush(fdo);
		}
		Chew(&pi.assert_timestamp, &pi.clear_timestamp,
			pi.assert_sequence, pi.clear_sequence);
		olda = pi.assert_sequence;
		oldc = pi.clear_sequence;
	}
	return(0);
}
OpenPOWER on IntegriCloud