forked from induarun9086/gPTPd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptpcmn.c
293 lines (251 loc) · 9.18 KB
/
gptpcmn.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
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
#include "gptpcmn.h"
#include "log.h"
void gptp_initTxBuf(struct gPTPd* gPTPd)
{
struct ethhdr *eh = (struct ethhdr *)gPTPd->txBuf;
struct gPTPHdr * gh = (struct gPTPHdr *)&gPTPd->txBuf[sizeof(struct ethhdr)];
/* Initialize it */
memset(gPTPd->txBuf, 0, GPTP_TX_BUF_SIZE);
/* Fill in the Ethernet header */
eh->h_dest[0] = 0x01;
eh->h_dest[1] = 0x80;
eh->h_dest[2] = 0xC2;
eh->h_dest[3] = 0x00;
eh->h_dest[4] = 0x00;
eh->h_dest[5] = 0x0E;
eh->h_source[0] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[0];
eh->h_source[1] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[1];
eh->h_source[2] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[2];
eh->h_source[3] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[3];
eh->h_source[4] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[4];
eh->h_source[5] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[5];
/* Fill in Ethertype field */
eh->h_proto = htons(ETH_P_1588);
/* Fill common gPTP header fields */
gh->h.f.b1.tsSpec = GPTP_TRANSPORT_L2;
gh->h.f.b2.ptpVer = GPTP_VERSION_NO;
gh->h.f.srcPortIden[0] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[0];
gh->h.f.srcPortIden[1] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[1];
gh->h.f.srcPortIden[2] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[2];
gh->h.f.srcPortIden[3] = 0xFF;
gh->h.f.srcPortIden[4] = 0xFE;
gh->h.f.srcPortIden[5] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[3];
gh->h.f.srcPortIden[6] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[4];
gh->h.f.srcPortIden[7] = ((u8 *)&gPTPd->if_mac.ifr_hwaddr.sa_data)[5];
gh->h.f.srcPortIden[8] = 0x00;
gh->h.f.srcPortIden[9] = 0x01;
}
void gptp_initRxBuf(struct gPTPd* gPTPd)
{
memset(gPTPd->rxBuf, 0, GPTP_RX_BUF_SIZE);
memset(gPTPd->tsBuf, 0, GPTP_CON_TS_BUF_SIZE);
gPTPd->rxMsgHdr.msg_iov = &gPTPd->rxiov;
gPTPd->rxMsgHdr.msg_iovlen = 1;
gPTPd->rxMsgHdr.msg_control=gPTPd->tsBuf;
gPTPd->rxMsgHdr.msg_controllen=GPTP_CON_TS_BUF_SIZE;
gPTPd->rxMsgHdr.msg_flags=0;
gPTPd->rxMsgHdr.msg_name=&gPTPd->rxSockAddress;
gPTPd->rxMsgHdr.msg_namelen=sizeof(struct sockaddr_ll);
}
u64 gptp_getCurrMilliSecTS(void)
{
u64 currTickTS = 0;
struct timespec ts = {0};
clock_gettime(CLOCK_MONOTONIC, &ts);
currTickTS = ((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
return currTickTS;
}
void gptp_startTimer(struct gPTPd* gPTPd, u32 timerId, u32 timeInterval, u32 timerEvt)
{
gPTPd->timers[timerId].timeInterval = timeInterval;
gPTPd->timers[timerId].timerEvt = timerEvt;
gPTPd->timers[timerId].lastTS = gptp_getCurrMilliSecTS();
}
void gptp_resetTimer(struct gPTPd* gPTPd, u32 timerId)
{
if(gPTPd->timers[timerId].timeInterval != 0)
gPTPd->timers[timerId].lastTS = gptp_getCurrMilliSecTS();
}
void gptp_stopTimer(struct gPTPd* gPTPd, u32 timerId)
{
gPTPd->timers[timerId].timeInterval = 0;
gPTPd->timers[timerId].timerEvt = GPTP_TIMER_INVALID;
gPTPd->timers[timerId].lastTS = 0;
}
int gptp_timespec_absdiff(struct timespec *start, struct timespec *stop, struct timespec *result)
{
int diffsign = 1;
if (stop->tv_sec > start->tv_sec) {
gptp_timespec_diff(start, stop, result);
} else if(stop->tv_sec < start->tv_sec) {
gptp_timespec_diff(stop, start, result);
diffsign = -1;
} else if(stop->tv_nsec > start->tv_nsec) {
gptp_timespec_diff(start, stop, result);
} else {
gptp_timespec_diff(stop, start, result);
diffsign = -1;
}
return diffsign;
}
void gptp_timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result)
{
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
} else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
return;
}
void gptp_timespec_sum(struct timespec *start, struct timespec *stop, struct timespec *result)
{
if ((stop->tv_nsec + start->tv_nsec) >= 1000000000) {
result->tv_sec = stop->tv_sec + start->tv_sec + 1;
result->tv_nsec = (stop->tv_nsec + start->tv_nsec) - 1000000000;
} else {
result->tv_sec = stop->tv_sec + start->tv_sec;
result->tv_nsec = stop->tv_nsec + start->tv_nsec;
}
return;
}
void gptp_copyTSFromBuf(struct timespec *ts, u8 *src)
{
u8 *dest = (u8*)ts;
struct gPTPTS *srcTS = (struct gPTPTS *)src;
gPTP_logMsg(GPTP_LOG_DEBUG, "fb: src %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",
src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
src[8], src[9], src[10], src[11]);
if(sizeof(time_t) == 8) {
ts->tv_sec = (u64)(srcTS->s.lsb + ((u64)srcTS->s.msb << 32));
ts->tv_nsec = srcTS->ns;
gPTP_logMsg(GPTP_LOG_DEBUG, "fb: dest %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",
dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7],
dest[8], dest[9], dest[10], dest[11]);
} else {
ts->tv_sec = srcTS->s.lsb;
ts->tv_nsec = srcTS->ns;
gPTP_logMsg(GPTP_LOG_DEBUG, "fb: dest %02x %02x %02x %02x %02x %02x %02x %02x \n",
dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
}
}
void gptp_copyTSToBuf(struct timespec *ts, u8 *dest)
{
u8 *src = (u8*)ts;
struct gPTPTS *destTS = (struct gPTPTS *)dest;
if(sizeof(time_t) == 8) {
gPTP_logMsg(GPTP_LOG_DEBUG, "tb: src %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",
src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
src[8], src[9], src[10], src[11]);
destTS->s.msb = (u16)((u64)ts->tv_sec >> 32);
destTS->s.lsb = (u32)ts->tv_sec;
destTS->ns = ts->tv_nsec;
} else {
gPTP_logMsg(GPTP_LOG_DEBUG, "tb: src %02x %02x %02x %02x %02x %02x %02x %02x \n",
src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7]);
destTS->s.msb = 0;
destTS->s.lsb = ts->tv_sec;
destTS->ns = ts->tv_nsec;
}
gPTP_logMsg(GPTP_LOG_DEBUG, "tb: dest %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",
dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7],
dest[8], dest[9]);
}
u16 gptp_chgEndianess16(u16 val)
{
return (((val & 0x00ff) << 8) | ((val & 0xff00) >> 8));
}
u8 gptp_calcLogInterval(u32 time)
{
u8 logInt = 0;
u32 linInt = time;
while(linInt > 1) {
linInt = logInt >> 1;
logInt++;
}
return logInt;
}
void getTxTS(struct gPTPd* gPTPd, struct timespec* ts)
{
static short sk_events = POLLPRI;
static short sk_revents = POLLPRI;
int cnt = 0, res = 0, level, type;
struct cmsghdr *cm;
struct timespec *sw, *rts = NULL;
struct pollfd pfd = { gPTPd->sockfd, sk_events, 0 };
do {
res = poll(&pfd, 1, 1000);
if (res < 1) {
gPTP_logMsg(GPTP_LOG_DEBUG, "Poll failed %d\n", res);
break;
} else if (!(pfd.revents & sk_revents)) {
gPTP_logMsg(GPTP_LOG_ERROR, "poll for tx timestamp woke up on non ERR event");
break;
} else {
gPTP_logMsg(GPTP_LOG_DEBUG, "Poll success\n");
gptp_initRxBuf(gPTPd);
cnt = recvmsg(gPTPd->sockfd, &gPTPd->rxMsgHdr, MSG_ERRQUEUE);
if (cnt < 1)
gPTP_logMsg(GPTP_LOG_ERROR, "Recv failed\n");
else
gPTP_logMsg(GPTP_LOG_DEBUG, "TxTs msg len: %d\n", cnt);
for (cm = CMSG_FIRSTHDR(&gPTPd->rxMsgHdr); cm != NULL; cm = CMSG_NXTHDR(&gPTPd->rxMsgHdr, cm)) {
level = cm->cmsg_level;
type = cm->cmsg_type;
gPTP_logMsg(GPTP_LOG_DEBUG, "Lvl:%d Type: %d Size: %d (%d)\n", level, type, cm->cmsg_len, sizeof(struct timespec));
if (SOL_SOCKET == level && SO_TIMESTAMPING == type) {
if (cm->cmsg_len < sizeof(*ts) * 3) {
gPTP_logMsg(GPTP_LOG_DEBUG, "short SO_TIMESTAMPING message");
} else {
rts = (struct timespec *) CMSG_DATA(cm);
for(int i = 0; i < 3; i++)
if((rts[i].tv_sec != 0) || (rts[i].tv_nsec != 0)) {
if(ts != NULL) {
ts->tv_sec = rts[i].tv_sec;
ts->tv_nsec = rts[i].tv_nsec;
}
gPTP_logMsg(GPTP_LOG_INFO, "TxTS: %d: sec: %d nsec: %d \n", i, rts[i].tv_sec, rts[i].tv_nsec);
}
}
}
if (SOL_SOCKET == level && SO_TIMESTAMPNS == type) {
if (cm->cmsg_len < sizeof(*sw)) {
gPTP_logMsg(GPTP_LOG_DEBUG, "short SO_TIMESTAMPNS message");
}
}
}
}
} while(1);
}
void getRxTS(struct gPTPd* gPTPd, struct timespec* ts)
{
int level, type;
struct cmsghdr *cm;
struct timespec *sw, *rts = NULL;
for (cm = CMSG_FIRSTHDR(&gPTPd->rxMsgHdr); cm != NULL; cm = CMSG_NXTHDR(&gPTPd->rxMsgHdr, cm)) {
level = cm->cmsg_level;
type = cm->cmsg_type;
gPTP_logMsg(GPTP_LOG_DEBUG, "Lvl:%d Type: %d Size: %d (%d)\n", level, type, cm->cmsg_len, sizeof(struct timespec));
if (SOL_SOCKET == level && SO_TIMESTAMPING == type) {
if (cm->cmsg_len < sizeof(*ts) * 3) {
gPTP_logMsg(GPTP_LOG_DEBUG, "short SO_TIMESTAMPING message");
} else {
rts = (struct timespec *) CMSG_DATA(cm);
for(int i = 0; i < 3; i++)
if((rts[i].tv_sec != 0) || (rts[i].tv_nsec != 0)) {
if(ts != NULL) {
ts->tv_sec = rts[i].tv_sec;
ts->tv_nsec = rts[i].tv_nsec;
}
gPTP_logMsg(GPTP_LOG_INFO, "RxTS: %d: sec: %d nsec: %d \n", i, rts[i].tv_sec, rts[i].tv_nsec);
}
}
}
if (SOL_SOCKET == level && SO_TIMESTAMPNS == type) {
if (cm->cmsg_len < sizeof(*sw)) {
gPTP_logMsg(GPTP_LOG_DEBUG, "short SO_TIMESTAMPNS message");
}
}
}
}