-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmd_announce_receive_sm.c
214 lines (195 loc) · 6.64 KB
/
md_announce_receive_sm.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
/*
* 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 "mind.h"
#include "mdeth.h"
#include "gptpnet.h"
#include "gptpclock.h"
#include "md_announce_receive_sm.h"
typedef enum {
INIT,
INITIALIZE,
RECV_ANNOUNCE,
REACTION,
}md_announce_receive_state_t;
struct md_announce_receive_data{
PerTimeAwareSystemGlobal *ptasg;
PerPortGlobal *ppg;
md_announce_receive_state_t state;
md_announce_receive_state_t last_state;
MDAnnounceReceiveSM *thisSM;
int domainIndex;
int portIndex;
PTPMsgAnnounce rcvdAnnounce;
md_announce_receive_stat_data_t statd;
int cmlds_mode;
};
#define PORT_OPER sm->ppg->forAllDomain->portOper
#define PTP_PORT_ENABLED sm->ppg->ptpPortEnabled
#define AS_CAPABLE sm->ppg->asCapable
#define RXANN sm->thisSM->rxAnnouncePtr
#define RCVDRXANN sm->thisSM->rcvdRxAnnounce
#define IEEE_1588_ANNOUNCE_NO_TLV_SIZE 64
static void *recAnnounce(md_announce_receive_data_t *sm)
{
MDPTPMsgAnnounce *md_announce=(MDPTPMsgAnnounce *)RXANN;
md_decompose_head((MDPTPMsgHeader *)RXANN, &sm->rcvdAnnounce.header);
if(!sm->cmlds_mode){
// 802.1AS-2020 8.1 the value of majorSdoId for gPTP domain must be 0x1
// When device is accepting message under CMLDS domain, allow values
// other than 0x1
if(sm->rcvdAnnounce.header.majorSdoId!=0x1){
UB_LOG(UBL_DEBUGV, "%s:port=%d, invalid majorSdoId on gPTP domain, ignore ANNOUNCE\n",
__func__, sm->portIndex);
return NULL;
}
}
sm->rcvdAnnounce.currentUtcOffset = ntohs(md_announce->currentUtcOffset_ns);
sm->rcvdAnnounce.grandmasterPriority1 = md_announce->grandmasterPriority1;
sm->rcvdAnnounce.grandmasterClockQuality.clockClass =
md_announce->grandmasterClockQuality.clockClass;
sm->rcvdAnnounce.grandmasterClockQuality.clockAccuracy =
md_announce->grandmasterClockQuality.clockAccuracy;
sm->rcvdAnnounce.grandmasterClockQuality.offsetScaledLogVariance =
ntohs(md_announce->grandmasterClockQuality.offsetScaledLogVariance_ns);
sm->rcvdAnnounce.grandmasterPriority2 = md_announce->grandmasterPriority2;
memcpy(&sm->rcvdAnnounce.grandmasterIdentity,
&md_announce->grandmasterIdentity, sizeof(ClockIdentity));
sm->rcvdAnnounce.stepsRemoved = ntohs(md_announce->stepsRemoved_ns);
sm->rcvdAnnounce.timeSource = md_announce->timeSource;
if(ntohs(md_announce->head.messageLength_ns) > IEEE_1588_ANNOUNCE_NO_TLV_SIZE){
sm->rcvdAnnounce.tlvType = ntohs(md_announce->tlvType_ns);
sm->rcvdAnnounce.tlvLength = ntohs(md_announce->tlvLength_ns);
// ??? In the case where the master sent inconsistent value
// where stepsRemoved does not corresponds
// to the number (N) of elements in the pathSequence, set missing path TLV to all 1
memset(&sm->rcvdAnnounce.pathSequence, 0xFF,
sizeof(ClockIdentity)*MAX_PATH_TRACE_N);
memcpy(&sm->rcvdAnnounce.pathSequence, &md_announce->pathSequence,
sm->rcvdAnnounce.tlvLength);
}
sm->statd.announce_rec_valid++;
return &sm->rcvdAnnounce;
}
static md_announce_receive_state_t allstate_condition(md_announce_receive_data_t *sm)
{
if(sm->ptasg->BEGIN || !sm->ptasg->instanceEnable ||
!PORT_OPER || !PTP_PORT_ENABLED || !AS_CAPABLE) {
sm->last_state=REACTION;
return INITIALIZE;
}
return sm->state;
}
static void *initialize_proc(md_announce_receive_data_t *sm)
{
UB_LOG(UBL_DEBUGV, "md_announce_receive:%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
return NULL;
}
static md_announce_receive_state_t initialize_condition(md_announce_receive_data_t *sm)
{
if(RCVDRXANN) return RECV_ANNOUNCE;
return INITIALIZE;
}
static void *recv_announce_proc(md_announce_receive_data_t *sm)
{
UB_LOG(UBL_DEBUGV, "md_announce_receive:%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
RCVDRXANN=false;
return recAnnounce(sm);
}
static md_announce_receive_state_t recv_announce_condition(md_announce_receive_data_t *sm)
{
if(RCVDRXANN) sm->last_state=REACTION;
return RECV_ANNOUNCE;
}
void *md_announce_receive_sm(md_announce_receive_data_t *sm, uint64_t cts64)
{
bool state_change;
void *retp=NULL;
if(!sm) return NULL;
sm->state = allstate_condition(sm);
while(true){
state_change=(sm->last_state != sm->state);
sm->last_state = sm->state;
switch(sm->state){
case INIT:
sm->state = INITIALIZE;
break;
case INITIALIZE:
if(state_change)
retp=initialize_proc(sm);
sm->state = initialize_condition(sm);
break;
case RECV_ANNOUNCE:
if(state_change){
retp=recv_announce_proc(sm);
break;
}
sm->state = recv_announce_condition(sm);
break;
case REACTION:
break;
}
if(retp) return retp;
if(sm->last_state == sm->state) break;
}
return retp;
}
void md_announce_receive_sm_init(md_announce_receive_data_t **sm,
int domainIndex, int portIndex,
PerTimeAwareSystemGlobal *ptasg,
PerPortGlobal *ppg)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d, portIndex=%d\n",
__func__, domainIndex, portIndex);
INIT_SM_DATA(md_announce_receive_data_t, MDAnnounceReceiveSM, sm);
(*sm)->ptasg = ptasg;
(*sm)->ppg = ppg;
(*sm)->domainIndex = domainIndex;
(*sm)->portIndex = portIndex;
(*sm)->cmlds_mode = gptpconf_get_intitem(CONF_CMLDS_MODE);
}
int md_announce_receive_sm_close(md_announce_receive_data_t **sm)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d, portIndex=%d\n",
__func__, (*sm)->domainIndex, (*sm)->portIndex);
CLOSE_SM_DATA(sm);
return 0;
}
void *md_announce_receive_sm_mdAnnounceRec(md_announce_receive_data_t *sm,
event_data_recv_t *edrecv, uint64_t cts64)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
RCVDRXANN=true;
sm->statd.announce_rec++;
RXANN=edrecv->recbptr;
sm->last_state=REACTION;
return md_announce_receive_sm(sm, cts64);
}
void md_announce_receive_stat_reset(md_announce_receive_data_t *sm)
{
memset(&sm->statd, 0, sizeof(md_announce_receive_stat_data_t));
}
md_announce_receive_stat_data_t *md_announce_receive_get_stat(md_announce_receive_data_t *sm)
{
return &sm->statd;
}