forked from induarun9086/gPTPd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelaymsr.c
240 lines (201 loc) · 7.31 KB
/
delaymsr.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
#define DELAY_MSR_MODULE
#include "delaymsr.h"
void initDM(struct gPTPd* gPTPd)
{
gPTPd->dm.state = DM_STATE_INIT;
gPTPd->dm.delayReqInterval = GPTP_PDELAY_REQ_INTERVAL;
gPTPd->dm.delayReqTimeOut = GPTP_PDELAY_REQ_TIMEOUT;
}
void unintDM(struct gPTPd* gPTPd)
{
}
void dmHandleEvent(struct gPTPd* gPTPd, int evtId)
{
struct timespec diff[3];
gPTP_logMsg(GPTP_LOG_INFO, "gPTP dmHandleEvent st: %d evt: 0x%x \n", gPTPd->dm.state, evtId);
switch(gPTPd->dm.state) {
case DM_STATE_INIT:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
break;
case GPTP_EVT_DM_ENABLE:
dmHandleStateChange(gPTPd, DM_STATE_IDLE);
break;
case GPTP_EVT_STATE_EXIT:
break;
default:
break;
}
break;
case DM_STATE_IDLE:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
gptp_startTimer(gPTPd, GPTP_TIMER_DELAYREQ_RPT, gPTPd->dm.delayReqInterval, GPTP_EVT_DM_PDELAY_REQ_RPT);
break;
case GPTP_EVT_DM_PDELAY_REQ_RPT:
gptp_stopTimer(gPTPd, GPTP_TIMER_DELAYREQ_RPT);
memset(&gPTPd->ts[0], 0, sizeof(struct timespec) * 4);
sendDelayReq(gPTPd);
getTxTS(gPTPd, &gPTPd->ts[0]);
dmHandleStateChange(gPTPd, DM_STATE_DELAY_RESP_WAIT);
break;
case GPTP_EVT_DM_PDELAY_REQ:
getRxTS(gPTPd, &gPTPd->ts[4]);
sendDelayResp(gPTPd);
getTxTS(gPTPd, &gPTPd->ts[5]);
sendDelayRespFlwUp(gPTPd);
break;
case GPTP_EVT_STATE_EXIT:
break;
default:
break;
}
break;
case DM_STATE_DELAY_RESP_WAIT:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
gptp_startTimer(gPTPd, GPTP_TIMER_DELAYREQ_TO, gPTPd->dm.delayReqTimeOut, GPTP_EVT_DM_PDELAY_REQ_TO);
break;
case GPTP_EVT_DM_PDELAY_REQ:
getRxTS(gPTPd, &gPTPd->ts[4]);
sendDelayResp(gPTPd);
getTxTS(gPTPd, &gPTPd->ts[5]);
sendDelayRespFlwUp(gPTPd);
break;
case GPTP_EVT_DM_PDELAY_REQ_TO:
memset(&gPTPd->ts[0], 0, sizeof(struct timespec) * 4);
sendDelayReq(gPTPd);
getTxTS(gPTPd, &gPTPd->ts[0]);
break;
case GPTP_EVT_DM_PDELAY_RESP:
gptp_stopTimer(gPTPd, GPTP_TIMER_DELAYREQ_TO);
gptp_copyTSFromBuf(&gPTPd->ts[1], &gPTPd->rxBuf[GPTP_BODY_OFFSET]);
getRxTS(gPTPd, &gPTPd->ts[3]);
dmHandleStateChange(gPTPd, DM_STATE_DELAY_RESP_FLWUP_WAIT);
break;
case GPTP_EVT_STATE_EXIT:
break;
default:
break;
}
break;
case DM_STATE_DELAY_RESP_FLWUP_WAIT:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
break;
case GPTP_EVT_DM_PDELAY_REQ:
getRxTS(gPTPd, &gPTPd->ts[4]);
sendDelayResp(gPTPd);
getTxTS(gPTPd, &gPTPd->ts[5]);
sendDelayRespFlwUp(gPTPd);
break;
case GPTP_EVT_DM_PDELAY_RESP_FLWUP:
gptp_copyTSFromBuf(&gPTPd->ts[2], &gPTPd->rxBuf[GPTP_BODY_OFFSET]);
for(int i = 0; i < 4; i++)
gPTP_logMsg(GPTP_LOG_INFO, "@@@ t%d: %lld_%ld\n", (i+1), (s64)gPTPd->ts[i].tv_sec, gPTPd->ts[i].tv_nsec);
gptp_timespec_diff(&gPTPd->ts[0],&gPTPd->ts[3],&diff[0]);
gptp_timespec_diff(&gPTPd->ts[1],&gPTPd->ts[2],&diff[1]);
if(diff[1].tv_nsec > diff[0].tv_nsec) {
gPTP_logMsg(GPTP_LOG_INFO, "Negative delay ignored 0:%lu 1:%lu\n", diff[0].tv_nsec, diff[1].tv_nsec);
} else {
gptp_timespec_diff(&diff[1],&diff[0],&diff[2]);
if(diff[2].tv_nsec > 50000) {
gPTP_logMsg(GPTP_LOG_INFO, "Abnormally large delay ignored %lu\n", (diff[2].tv_nsec / 2));
} else {
gPTPd->msrdDelay = diff[2].tv_nsec/ 2;
gPTP_logMsg(GPTP_LOG_NOTICE, "---> gPTP msrdDelay: %lu\n", gPTPd->msrdDelay);
}
}
dmHandleStateChange(gPTPd, DM_STATE_IDLE);
break;
case GPTP_EVT_STATE_EXIT:
break;
default:
break;
}
break;
default:
break;
}
}
static void dmHandleStateChange(struct gPTPd* gPTPd, int toState)
{
dmHandleEvent(gPTPd, GPTP_EVT_STATE_EXIT);
gPTPd->dm.state = toState;
dmHandleEvent(gPTPd, GPTP_EVT_STATE_ENTRY);
}
static void sendDelayReq(struct gPTPd* gPTPd)
{
int err = 0;
int txLen = sizeof(struct ethhdr);
struct gPTPHdr * gh = (struct gPTPHdr *)&gPTPd->txBuf[sizeof(struct ethhdr)];
/* Fill gPTP header */
gh->h.f.seqNo = gptp_chgEndianess16(gPTPd->dm.txSeqNo);
gh->h.f.b1.msgType = (GPTP_TRANSPORT_L2 | GPTP_MSG_TYPE_PDELAY_REQ);
gh->h.f.flags = gptp_chgEndianess16(GPTP_FLAGS_NONE);
gh->h.f.ctrl = GPTP_CONTROL_DELAY_ANNOUNCE;
gh->h.f.logMsgInt = gptp_calcLogInterval(gPTPd->dm.delayReqInterval / 1000);
/* Add gPTP header size */
txLen += sizeof(struct gPTPHdr);
/* PTP body */
memset(&gPTPd->txBuf[GPTP_BODY_OFFSET], 0, (GPTP_TX_BUF_SIZE - GPTP_BODY_OFFSET));
txLen += (GPTP_TS_LEN + GPTP_PORT_IDEN_LEN);
/* Insert length */
gh->h.f.msgLen = gptp_chgEndianess16(txLen - sizeof(struct ethhdr));
if ((err = sendto(gPTPd->sockfd, gPTPd->txBuf, txLen, 0, (struct sockaddr*)&gPTPd->txSockAddress, sizeof(struct sockaddr_ll))) < 0)
gPTP_logMsg(GPTP_LOG_DEBUG, "PDelayReq Send failed %d %d\n", err, errno);
else
gPTP_logMsg(GPTP_LOG_INFO, ">>> PDelayReq (%d) sent\n", gPTPd->dm.txSeqNo++);
}
static void sendDelayResp(struct gPTPd* gPTPd)
{
int err = 0;
int txLen = sizeof(struct ethhdr);
struct gPTPHdr * gh = (struct gPTPHdr *)&gPTPd->txBuf[sizeof(struct ethhdr)];
/* Fill gPTP header */
gh->h.f.seqNo = gptp_chgEndianess16(gPTPd->dm.rxSeqNo);
gh->h.f.b1.msgType = (GPTP_TRANSPORT_L2 | GPTP_MSG_TYPE_PDELAY_RESP);
gh->h.f.flags = gptp_chgEndianess16(GPTP_FLAGS_TWO_STEP);
gh->h.f.ctrl = GPTP_CONTROL_DELAY_ANNOUNCE;
gh->h.f.logMsgInt = GPTP_LOG_MSG_INT_MAX;
/* Add gPTP header size */
txLen += sizeof(struct gPTPHdr);
/* PTP body */
memset(&gPTPd->txBuf[GPTP_BODY_OFFSET], 0, (GPTP_TX_BUF_SIZE - GPTP_BODY_OFFSET));
gptp_copyTSToBuf(&gPTPd->ts[4], &gPTPd->txBuf[txLen]);
txLen += GPTP_TS_LEN;
memcpy(&gPTPd->txBuf[txLen], &gPTPd->dm.reqPortIden[0], GPTP_PORT_IDEN_LEN);
txLen += GPTP_PORT_IDEN_LEN;
/* Insert length */
gh->h.f.msgLen = gptp_chgEndianess16(txLen - sizeof(struct ethhdr));
if ((err = sendto(gPTPd->sockfd, gPTPd->txBuf, txLen, 0, (struct sockaddr*)&gPTPd->txSockAddress, sizeof(struct sockaddr_ll))) < 0)
gPTP_logMsg(GPTP_LOG_DEBUG, "PDelayResp Send failed %d %d\n", err, errno);
else
gPTP_logMsg(GPTP_LOG_INFO, "=== PDelayResp (%d) sent\n", gPTPd->dm.rxSeqNo);
}
static void sendDelayRespFlwUp(struct gPTPd* gPTPd)
{
int err = 0;
int txLen = sizeof(struct ethhdr);
struct gPTPHdr * gh = (struct gPTPHdr *)&gPTPd->txBuf[sizeof(struct ethhdr)];
/* Fill gPTP header */
gh->h.f.seqNo = gptp_chgEndianess16(gPTPd->dm.rxSeqNo);
gh->h.f.b1.msgType = (GPTP_TRANSPORT_L2 | GPTP_MSG_TYPE_PDELAY_RESP_FLWUP);
gh->h.f.flags = gptp_chgEndianess16(GPTP_FLAGS_NONE);
gh->h.f.ctrl = GPTP_CONTROL_DELAY_ANNOUNCE;
gh->h.f.logMsgInt = GPTP_LOG_MSG_INT_MAX;
/* Add gPTP header size */
txLen += sizeof(struct gPTPHdr);
/* PTP body */
memset(&gPTPd->txBuf[GPTP_BODY_OFFSET], 0, (GPTP_TX_BUF_SIZE - GPTP_BODY_OFFSET));
gptp_copyTSToBuf(&gPTPd->ts[5], &gPTPd->txBuf[txLen]);
txLen += GPTP_TS_LEN;
memcpy(&gPTPd->txBuf[txLen], &gPTPd->dm.reqPortIden[0], GPTP_PORT_IDEN_LEN);
txLen += GPTP_PORT_IDEN_LEN;
/* Insert length */
gh->h.f.msgLen = gptp_chgEndianess16(txLen - sizeof(struct ethhdr));
if ((err = sendto(gPTPd->sockfd, gPTPd->txBuf, txLen, 0, (struct sockaddr*)&gPTPd->txSockAddress, sizeof(struct sockaddr_ll))) < 0)
gPTP_logMsg(GPTP_LOG_DEBUG, "PDelayRespFlwUp Send failed %d %d\n", err, errno);
else
gPTP_logMsg(GPTP_LOG_INFO, "=== PDelayRespFlwUp (%d) sent\n", gPTPd->dm.rxSeqNo);
}