-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathset_ptpclock.c
161 lines (150 loc) · 3.75 KB
/
set_ptpclock.c
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
/*
* Excelfore gptp - Implementation of gPTP(IEEE 802.1AS)
* Copyright (C) 2019 Excelfore Corporation (https://excelfore.com)
*
* This file is part of Excelfore-gptp.
*
* Excelfore-gptp 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.
*
* Excelfore-gptp 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.
*
* You should have received a copy of the GNU General Public License
* along with Excelfore-gptp. If not, see
* <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
*/
#include <fcntl.h>
#include <stdio.h>
#include <getopt.h>
#include <time.h>
#include "ll_gptpsupport.h"
#define PTPDEV_CLOCKFD 3
#define FD_TO_CLOCKID(ptpfd) ((~(clockid_t) (ptpfd) << 3) | PTPDEV_CLOCKFD)
static int print_usage(char *pname)
{
char *s;
if((s=strrchr(pname,'/'))==NULL)
s=pname;
else
s++;
printf("%s [options] [+/-]yyyy:mm:dd-HH:MM:SS\n", s);
printf("-h|--help: this help\n");
printf("-d|--ptpdev ptp_device (default:/dev/ptp0)\n");
printf("\ne.g.\n");
printf("# set to 2021 Jan. 1st 00:00:00\n");
printf(" %s -d /dev/ptp1 2021:01:01-00:00:00\n", s);
printf("# set 5 seconds ahead from the current, clock=/dev/ptp0 by default\n");
printf(" %s +5\n", s);
printf("# set 1 min 5 seconds behind from the current, clock=/dev/ptp0 by default\n");
printf(" %s -- -01:10\n", s);
return -1;
}
static int set_options(int argc, char *argv[], char **ptpdev)
{
int oc;
struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"ptpdev", required_argument, 0, 'd'},
{NULL, 0, 0, 0},
};
while((oc=getopt_long(argc, argv, "hd:", long_options, NULL))!=-1){
switch(oc){
case 'd':
*ptpdev=optarg;
break;
case 'h':
print_usage(argv[0]);
return 1;
default:
return print_usage(argv[0]);
}
}
return 0;
}
static void print_tm(int ptpfd, struct timespec *nts)
{
struct timespec ts;
struct tm *ltm;
char stime[64];
if(!nts){
clock_gettime(FD_TO_CLOCKID(ptpfd), &ts);
nts=&ts;
}
ltm=localtime(&nts->tv_sec);
strftime(stime, sizeof(stime), "%Y:%m:%d-%H:%M:%S", ltm);
printf("%s\n", stime);
return;
}
static int get_newts(int ptpfd, char *stv, struct timespec *nts)
{
//struct timespec ts;
int rv=0;
int vv[6];
int i,v,dn;
char *nstv;
int rt;
struct tm *ltm;
if(*stv=='+') rv=1;
if(*stv=='-') rv=-1;
if(rv) stv++;
for(i=0;i<6;i++){
v=strtol(stv, &nstv, 10);
if(stv==nstv) break;
vv[i]=v;
stv=nstv+1;
}
dn=i;
if(dn==0){
printf("invalid string: %s\n", stv);
return -1;
}
if(!rv){
// set absolute time
clock_gettime(FD_TO_CLOCKID(ptpfd), nts);
ltm=localtime(&nts->tv_sec);
if(dn>5) ltm->tm_year=vv[dn-6]-1900;
if(dn>4) ltm->tm_mon=vv[dn-5]-1;
if(dn>3) ltm->tm_mday=vv[dn-4];
if(dn>2) ltm->tm_hour=vv[dn-3];
if(dn>1) ltm->tm_min=vv[dn-2];
if(dn>0) ltm->tm_sec=vv[dn-1];
nts->tv_sec=mktime(ltm);
}else{
if(dn>3){
printf("only HH:MM:SS is valide to set diff: %s\n", stv);
return -1;
}
clock_gettime(FD_TO_CLOCKID(ptpfd), nts);
rt=1;
for(i=dn-1;i>=0;i--){
nts->tv_sec+=rv*vv[i]*rt;
rt*=60;
}
}
return 0;
}
int main(int argc, char* argv[])
{
char *ptpdev="/dev/ptp0";
int ptpfd;
if(set_options(argc, argv, &ptpdev)) return -1;
ptpfd = open(ptpdev, O_RDWR);
if(ptpfd<0){
printf("can't open ptpdev=%s\n", ptpdev);
return -1;
}
print_tm(ptpfd, NULL);
if(argc>optind){
struct timespec nts;
get_newts(ptpfd, argv[optind], &nts);
print_tm(ptpfd, &nts);
clock_settime(FD_TO_CLOCKID(ptpfd), &nts);
}
close(ptpfd);
return 0;
}