-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathppsctl.c
More file actions
332 lines (278 loc) · 7.01 KB
/
ppsctl.c
File metadata and controls
332 lines (278 loc) · 7.01 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* ppsctl.c -- control tool for PPS
*
* Copyright (C) 2009-2011 Alexander Gordeev <alex@gordick.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/timex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include "timepps.h"
/* variables for the command-line operations
* 0 means no operation
* 1 mean enable
* 2 means disable
*/
static int do_bind = 0; /* are we binding or unbinding? */
static int do_setflags = 0; /* should we manipulate kernel NTP PPS flags? */
static int do_echo = 0; /* turn on/off PPS echo function*/
static int opt_edge = PPS_CAPTURECLEAR; /* which edge to use? */
static char *device;
static int find_source(char *path, pps_handle_t *handle, int *avail_mode)
{
int ret;
printf("trying PPS source \"%s\"\n", path);
/* Try to find the source by using the supplied "path" name */
ret = open(path, O_RDWR);
if (ret < 0) {
fprintf(stderr, "unable to open device \"%s\" (%m)\n", path);
return ret;
}
/* Open the PPS source (and check the file descriptor) */
ret = time_pps_create(ret, handle);
if (ret < 0) {
fprintf(stderr, "cannot create a PPS source from device "
"\"%s\" (%m)\n", path);
return -1;
}
printf("found PPS source \"%s\"\n", path);
/* Find out what features are supported */
ret = time_pps_getcap(*handle, avail_mode);
if (ret < 0) {
fprintf(stderr, "cannot get capabilities (%m)\n");
return -1;
}
if ((*avail_mode & PPS_CAPTUREASSERT) == 0) {
fprintf(stderr, "cannot CAPTUREASSERT\n");
return -1;
}
return 0;
}
static inline int bind(pps_handle_t handle, int edge)
{
return time_pps_kcbind(handle, PPS_KC_HARDPPS, edge, PPS_TSFMT_TSPEC);
}
static inline int unbind(pps_handle_t handle)
{
return time_pps_kcbind(handle, PPS_KC_HARDPPS, 0, PPS_TSFMT_TSPEC);
}
static inline int set_flags()
{
struct timex tmx = { .modes = 0 };
if (adjtimex(&tmx) == -1) {
fprintf(stderr, "adjtimex get failed: %s\n", strerror(errno));
return -1;
}
tmx.modes = ADJ_STATUS;
tmx.status |= (STA_PPSFREQ | STA_PPSTIME);
if (adjtimex(&tmx) == -1) {
fprintf(stderr, "adjtimex set failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
static inline int unset_flags()
{
struct timex tmx = { .modes = 0 };
if (adjtimex(&tmx) == -1) {
fprintf(stderr, "adjtimex get failed: %s\n", strerror(errno));
return -1;
}
tmx.modes = ADJ_STATUS;
tmx.status &= ~(STA_PPSFREQ | STA_PPSTIME);
if (adjtimex(&tmx) == -1) {
fprintf(stderr, "adjtimex set failed: %s\n", strerror(errno));
return -1;
}
return 0;
}
static inline int enable_echo(pps_handle_t handle, int edge)
{
pps_params_t params;
int ret;
int avail_mode;
/* Find out what features are supported */
ret = time_pps_getcap(handle, &avail_mode);
if (ret < 0) {
fprintf(stderr, "cannot get capabilities\n");
return ret;
}
if ((avail_mode & edge) == 0) {
fprintf(stderr, "Echo on selected edge unsupported\n");
return -1;
}
ret = time_pps_getparams(handle, ¶ms);
if (ret < 0)
return ret;
params.mode |= (edge == PPS_CAPTUREASSERT) ? PPS_ECHOASSERT : PPS_ECHOCLEAR;
ret = time_pps_setparams(handle, ¶ms);
if (ret < 0)
return ret;
return 0;
}
static inline int disable_echo(pps_handle_t handle)
{
pps_params_t params;
int ret;
ret = time_pps_getparams(handle, ¶ms);
if (ret < 0)
return ret;
params.mode &= ~(PPS_ECHOASSERT | PPS_ECHOCLEAR);
ret = time_pps_setparams(handle, ¶ms);
if (ret < 0)
return ret;
return 0;
}
static inline void usage(char *name)
{
fprintf(stderr, "Usage: %s [-bBfFac] <ppsdev>\n"
"Commands:\n"
" -b bind kernel PPS consumer\n"
" -B unbind kernel PPS consumer\n"
" -f set kernel NTP PPS flags\n"
" -F unset kernel NTP PPS flags\n"
" -e enable PPS echo\n"
" -E disable PPS echo\n"
"Options:\n"
" -a use assert edge\n"
" -c use clear edge (default)\n",
name);
}
static void parse_args(int argc, char **argv)
{
while (1)
{
int c;
/* getopt_long stores the option index here. */
int option_index = 0;
static struct option long_options[] =
{
{"bind", no_argument, 0, 'b'},
{"unbind", no_argument, 0, 'B'},
{"set-flags", no_argument, 0, 'f'},
{"unset-flags", no_argument, 0, 'F'},
{"enable-echo", no_argument, 0, 'e'},
{"disable-echo", no_argument, 0, 'E'},
{"assert", no_argument, 0, 'a'},
{"clear", no_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "bBfFeEach", long_options, &option_index);
/* detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'b': {
do_bind = 1;
break;
}
case 'B': {
do_bind = 2;
break;
}
case 'f': {
do_setflags = 1;
break;
}
case 'F': {
do_setflags = 2;
break;
}
case 'e': {
do_echo = 1;
break;
}
case 'E': {
do_echo = 2;
break;
}
case 'a': {
opt_edge = PPS_CAPTUREASSERT;
break;
}
case 'c': {
opt_edge = PPS_CAPTURECLEAR;
break;
}
case 'h': {
usage(argv[0]);
exit(0);
}
default: {
usage(argv[0]);
exit(1);
}
}
}
if ((do_bind == 0) && (do_setflags == 0) && (do_echo == 0)) {
printf("No command specified!\n");
usage(argv[0]);
exit(1);
}
if (optind == argc - 1) {
device = argv[optind];
} else {
printf("Device name missing!\n");
usage(argv[0]);
exit(1);
}
}
int main(int argc, char *argv[])
{
pps_handle_t handle;
int avail_mode;
/* Check the command line */
parse_args(argc, argv);
if (find_source(device, &handle, &avail_mode) < 0)
exit(EXIT_FAILURE);
if (do_setflags == 2)
if (unset_flags() < 0) {
fprintf(stderr, "Failed to unset flags!\n");
exit(EXIT_FAILURE);
}
if (do_bind == 2)
if (bind(handle, 0) < 0) {
fprintf(stderr, "Unbind failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (do_bind == 1)
if (bind(handle, opt_edge) < 0) {
fprintf(stderr, "Bind failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (do_setflags == 1)
if (set_flags() < 0) {
fprintf(stderr, "Failed to set flags!\n");
exit(EXIT_FAILURE);
}
if (do_echo == 2)
if (disable_echo(handle) < 0) {
fprintf(stderr, "Disable echo failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (do_echo == 1)
if (enable_echo(handle, opt_edge) < 0) {
fprintf(stderr, "Enable echo failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
time_pps_destroy(handle);
return 0;
}