forked from induarun9086/gPTPd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmc.c
226 lines (195 loc) · 7.84 KB
/
bmc.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
#define BEST_MASTER_CLOCK_SELECTION
#include "bmc.h"
#include "sync.h"
void initBMC(struct gPTPd* gPTPd)
{
gPTPd->bmc.state = BMC_STATE_INIT;
gPTPd->bmc.announceInterval = GPTP_ANNOUNCE_INTERVAL;
gPTPd->bmc.announceTimeout = GPTP_ANNOUNCE_TIMEOUT;
}
void unintBMC(struct gPTPd* gPTPd)
{
}
void bmcHandleEvent(struct gPTPd* gPTPd, int evtId)
{
gPTP_logMsg(GPTP_LOG_INFO, "gPTP bmcHandleEvent st: %d evt: 0x%x \n", gPTPd->bmc.state, evtId);
switch(gPTPd->bmc.state) {
case BMC_STATE_INIT:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
break;
case GPTP_EVT_BMC_ENABLE:
bmcHandleStateChange(gPTPd, BMC_STATE_GRAND_MASTER);
updatePrioVectors(gPTPd);
break;
case GPTP_EVT_STATE_EXIT:
break;
default:
break;
}
break;
case BMC_STATE_GRAND_MASTER:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
gptp_startTimer(gPTPd, GPTP_TIMER_ANNOUNCE_RPT, gPTPd->bmc.announceInterval, GPTP_EVT_BMC_ANNOUNCE_RPT);
csSetState(gPTPd, TRUE);
break;
case GPTP_EVT_BMC_ANNOUNCE_MSG:
if(updateAnnounceInfo(gPTPd) == TRUE)
bmcHandleStateChange(gPTPd, BMC_STATE_SLAVE);
break;
case GPTP_EVT_BMC_ANNOUNCE_RPT:
sendAnnounce(gPTPd);
break;
case GPTP_EVT_STATE_EXIT:
gptp_stopTimer(gPTPd, GPTP_TIMER_ANNOUNCE_RPT);
break;
default:
break;
}
break;
case BMC_STATE_SLAVE:
switch (evtId) {
case GPTP_EVT_STATE_ENTRY:
gptp_startTimer(gPTPd, GPTP_TIMER_ANNOUNCE_TO, gPTPd->bmc.announceTimeout, GPTP_EVT_BMC_ANNOUNCE_TO);
csSetState(gPTPd, FALSE);
break;
case GPTP_EVT_BMC_ANNOUNCE_MSG:
if(updateAnnounceInfo(gPTPd) == TRUE)
gptp_resetTimer(gPTPd, GPTP_TIMER_ANNOUNCE_TO);
break;
case GPTP_EVT_BMC_ANNOUNCE_TO:
bmcHandleStateChange(gPTPd, BMC_STATE_GRAND_MASTER);
break;
case GPTP_EVT_STATE_EXIT:
gptp_stopTimer(gPTPd, GPTP_TIMER_ANNOUNCE_TO);
break;
default:
break;
}
break;
default:
break;
}
}
static void bmcHandleStateChange(struct gPTPd* gPTPd, int toState)
{
bmcHandleEvent(gPTPd, GPTP_EVT_STATE_EXIT);
gPTPd->bmc.state = toState;
bmcHandleEvent(gPTPd, GPTP_EVT_STATE_ENTRY);
}
static void sendAnnounce(struct gPTPd* gPTPd)
{
int err = 0;
int txLen = sizeof(struct ethhdr);
struct gPTPHdr *gh = (struct gPTPHdr *)&gPTPd->txBuf[sizeof(struct ethhdr)];
struct gPTPPrioVec *prio = (struct gPTPPrioVec*)&gPTPd->txBuf[GPTP_BODY_OFFSET];
struct gPTPtlv *tlv;
/* Fill gPTP header */
gh->h.f.seqNo = gptp_chgEndianess16(gPTPd->bmc.annoSeqNo);
gh->h.f.b1.msgType = (GPTP_TRANSPORT_L2 | GPTP_MSG_TYPE_ANNOUNCE);
gh->h.f.flags = gptp_chgEndianess16(GPTP_FLAGS_NONE);
gh->h.f.ctrl = GPTP_CONTROL_DELAY_ANNOUNCE;
gh->h.f.logMsgInt = gptp_calcLogInterval(gPTPd->bmc.announceInterval / 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));
prio->currUTCOff = gPTPd->bmc.portPrio.currUTCOff;
prio->prio1 = gPTPd->bmc.portPrio.prio1;
prio->clockQual.clockClass = gPTPd->bmc.portPrio.clockQual.clockClass;
prio->clockQual.clockAccuracy = gPTPd->bmc.portPrio.clockQual.clockAccuracy;
prio->clockQual.offsetScaledLogVariance = gptp_chgEndianess16(gPTPd->bmc.portPrio.clockQual.offsetScaledLogVariance);
prio->prio2 = gPTPd->bmc.portPrio.prio2;
memcpy(&prio->iden[0], &gPTPd->bmc.portPrio.iden[0], GPTP_PORT_IDEN_LEN);
prio->stepsRem = gPTPd->bmc.portPrio.stepsRem;
prio->clockSrc = gPTPd->bmc.portPrio.clockSrc;
txLen += sizeof(struct gPTPPrioVec);
/* Path trace TLV */
tlv = (struct gPTPtlv *)&gPTPd->txBuf[txLen];
tlv->type = gptp_chgEndianess16(GPTP_TLV_TYPE_PATH_TRACE);
tlv->len = gptp_chgEndianess16(GPTP_CLOCK_IDEN_LEN);
txLen += sizeof(struct gPTPtlv);
memcpy(&gPTPd->txBuf[txLen], &gh->h.f.srcPortIden, GPTP_CLOCK_IDEN_LEN);
txLen += GPTP_CLOCK_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, "Announce Send failed %d %d\n", err, errno);
else
gPTP_logMsg(GPTP_LOG_INFO, ">>> Announce (%d) sent\n", gPTPd->bmc.annoSeqNo++);
}
static bool updateAnnounceInfo(struct gPTPd* gPTPd)
{
bool gmFound = FALSE;
struct gPTPPrioVec *gnPrio = (struct gPTPPrioVec *)&gPTPd->rxBuf[GPTP_BODY_OFFSET];
gPTP_logMsg(GPTP_LOG_DEBUG, "Gnprio: %x:%x:%x:%x:%x:%x:%x \n",
gnPrio->prio1, gnPrio->clockQual.clockClass, gnPrio->clockQual.clockAccuracy,
gptp_chgEndianess16(gnPrio->clockQual.offsetScaledLogVariance), gnPrio->prio2,
gnPrio->stepsRem, gnPrio->clockSrc);
gPTP_logMsg(GPTP_LOG_DEBUG, "Gnprio: %x:%x:%x:%x:%x:%x:%x:%x \n",
gnPrio->iden[0], gnPrio->iden[1], gnPrio->iden[2], gnPrio->iden[3],
gnPrio->iden[4], gnPrio->iden[5], gnPrio->iden[6], gnPrio->iden[7]);
gPTP_logMsg(GPTP_LOG_DEBUG, "Portprio: %x:%x:%x:%x:%x:%x:%x \n",
gPTPd->bmc.portPrio.prio1, gPTPd->bmc.portPrio.clockQual.clockClass,
gPTPd->bmc.portPrio.clockQual.clockAccuracy,
gPTPd->bmc.portPrio.clockQual.offsetScaledLogVariance,
gPTPd->bmc.portPrio.prio2,
gPTPd->bmc.portPrio.stepsRem,
gPTPd->bmc.portPrio.clockSrc);
gPTP_logMsg(GPTP_LOG_DEBUG, "Portprio: %x:%x:%x:%x:%x:%x:%x:%x \n",
gPTPd->bmc.portPrio.iden[0], gPTPd->bmc.portPrio.iden[1],
gPTPd->bmc.portPrio.iden[2], gPTPd->bmc.portPrio.iden[3],
gPTPd->bmc.portPrio.iden[4], gPTPd->bmc.portPrio.iden[5],
gPTPd->bmc.portPrio.iden[6], gPTPd->bmc.portPrio.iden[7]);
if(gnPrio->prio1 < gPTPd->bmc.portPrio.prio1)
gmFound = TRUE;
else if(gnPrio->clockQual.clockClass < gPTPd->bmc.portPrio.clockQual.clockClass)
gmFound = TRUE;
else if(gnPrio->clockQual.clockAccuracy < gPTPd->bmc.portPrio.clockQual.clockAccuracy)
gmFound = TRUE;
else if(gptp_chgEndianess16(gnPrio->clockQual.offsetScaledLogVariance) < gPTPd->bmc.portPrio.clockQual.offsetScaledLogVariance)
gmFound = TRUE;
else if(gnPrio->prio2 < gPTPd->bmc.portPrio.prio2)
gmFound = TRUE;
else if(gnPrio->stepsRem < gPTPd->bmc.portPrio.stepsRem)
gmFound = TRUE;
else if(gnPrio->clockSrc < gPTPd->bmc.portPrio.clockSrc)
gmFound = TRUE;
else {
for(int i = 0; ((i < GPTP_PORT_IDEN_LEN) && (gmFound == FALSE)); i++) {
if(gnPrio->iden[i] < gPTPd->bmc.portPrio.iden[i])
gmFound = TRUE;
else if(gnPrio->iden[i] > gPTPd->bmc.portPrio.iden[i])
break;
else
continue;
}
}
if(gmFound == TRUE) {
gPTP_logMsg(GPTP_LOG_INFO, "High prio announce from: %x:%x:%x:%x:%x:%x:%x:%x \n",
gnPrio->iden[0], gnPrio->iden[1], gnPrio->iden[2], gnPrio->iden[3],
gnPrio->iden[4], gnPrio->iden[5], gnPrio->iden[6], gnPrio->iden[7]);
memcpy(&gPTPd->bmc.gmPrio, gnPrio, sizeof(struct gPTPPrioVec));
} else {
gPTP_logMsg(GPTP_LOG_INFO, "Low prio announce from %x:%x:%x:%x:%x:%x:%x:%x \n",
gnPrio->iden[0], gnPrio->iden[1], gnPrio->iden[2], gnPrio->iden[3],
gnPrio->iden[4], gnPrio->iden[5], gnPrio->iden[6], gnPrio->iden[7]);
memcpy(&gPTPd->bmc.gmPrio, &gPTPd->bmc.portPrio, sizeof(struct gPTPPrioVec));
}
return gmFound;
}
static void updatePrioVectors(struct gPTPd* gPTPd)
{
struct gPTPHdr *gh = (struct gPTPHdr *)&gPTPd->txBuf[sizeof(struct ethhdr)];
gPTPd->bmc.portPrio.currUTCOff = 0;
gPTPd->bmc.portPrio.prio1 = GPTP_DEFAULT_CLOCK_PRIO1;
gPTPd->bmc.portPrio.clockQual.clockClass = GPTP_DEFAULT_CLOCK_CLASS;
gPTPd->bmc.portPrio.clockQual.clockAccuracy = GPTP_DEFAULT_CLOCK_ACCURACY;
gPTPd->bmc.portPrio.clockQual.offsetScaledLogVariance = GPTP_DEFAULT_OFFSET_VARIANCE;
gPTPd->bmc.portPrio.prio2 = GPTP_DEFAULT_CLOCK_PRIO2;
memcpy(&gPTPd->bmc.portPrio.iden[0], &gh->h.f.srcPortIden[0], GPTP_PORT_IDEN_LEN);
gPTPd->bmc.portPrio.stepsRem = GPTP_DEFAULT_STEPS_REMOVED;
gPTPd->bmc.portPrio.clockSrc = GPTP_CLOCK_TYPE_INT_OSC;
memcpy(&gPTPd->bmc.gmPrio, &gPTPd->bmc.portPrio, sizeof(struct gPTPPrioVec));
}