-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathport_sync_sync_receive_sm.c
210 lines (191 loc) · 6.79 KB
/
port_sync_sync_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
/*
* 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 "port_sync_sync_receive_sm.h"
#define RCVD_MDSYNC sm->thisSM->rcvdMDSync
#define RCVD_MDSYNC_PTR sm->thisSM->rcvdMDSyncPtr
#define PORT_OPER sm->ppg->forAllDomain->portOper
#define PTP_PORT_ENABLED sm->ppg->ptpPortEnabled
#define AS_CAPABLE sm->ppg->asCapable
#define ASYMMETRY_MEASUREMENT_MODE sm->ppg->forAllDomain->asymmetryMeasurementMode
typedef enum {
INIT,
DISCARD,
RECEIVED_SYNC,
REACTION,
}port_sync_sync_receive_state_t;
struct port_sync_sync_receive_data{
PerTimeAwareSystemGlobal *ptasg;
PerPortGlobal *ppg;
port_sync_sync_receive_state_t state;
port_sync_sync_receive_state_t last_state;
PortSyncSyncReceiveSM *thisSM;
int domainIndex;
int portIndex;
PortSyncSync portSyncSync;
};
static void setPSSyncPSSR(port_sync_sync_receive_data_t *sm, double rateRatio, uint64_t cts64)
{
uint64_t interval;
sm->portSyncSync.localPortNumber = sm->ppg->thisPort;
sm->portSyncSync.localPortIndex = sm->ppg->thisPortIndex;
sm->portSyncSync.local_ppg = sm->ppg;
sm->portSyncSync.domainNumber = RCVD_MDSYNC_PTR->domainNumber;
sm->portSyncSync.followUpCorrectionField = RCVD_MDSYNC_PTR->followUpCorrectionField;
sm->portSyncSync.sourcePortIdentity = RCVD_MDSYNC_PTR->sourcePortIdentity;
sm->portSyncSync.logMessageInterval = RCVD_MDSYNC_PTR->logMessageInterval;
sm->portSyncSync.preciseOriginTimestamp = RCVD_MDSYNC_PTR->preciseOriginTimestamp;
sm->portSyncSync.upstreamTxTime = RCVD_MDSYNC_PTR->upstreamTxTime;
sm->portSyncSync.syncReceiptTimeoutTime.nsec =
cts64 + sm->ppg->syncReceiptTimeoutTimeInterval.nsec;
sm->portSyncSync.rateRatio = rateRatio;
sm->portSyncSync.gmTimeBaseIndicator = RCVD_MDSYNC_PTR->gmTimeBaseIndicator;
sm->portSyncSync.lastGmPhaseChange = RCVD_MDSYNC_PTR->lastGmPhaseChange;
sm->portSyncSync.lastGmFreqChange = RCVD_MDSYNC_PTR->lastGmFreqChange;
sm->portSyncSync.lastSyncSeqID = RCVD_MDSYNC_PTR->seqid;
/* calculate syncNextSendTimeoutTime
* upstreamTxTime may be used as an alternative, but since allows certain
* level of tolerence due to nature of the network medium and software
* processing, use relative to current time for simplicity.
*
* There is no need to align this timeoutTime to a specific timeslot since
* this should be the time when we received the Sync message fro the master.
*
* AVNU standards allow a variance of interval +50%/-10% (e.g. for 125msec
* allowed timer values are 112.5msec to 187.5msec).
*/
interval = LOG_TO_NSEC(sm->portSyncSync.logMessageInterval);
sm->portSyncSync.syncNextSendTimeoutTime.nsec = cts64 + interval + (interval/2);
}
static void *received_sync(port_sync_sync_receive_data_t *sm, uint64_t cts64)
{
double rateRatio;
int a;
RCVD_MDSYNC = false;
rateRatio = RCVD_MDSYNC_PTR->rateRatio;
rateRatio += (sm->ppg->forAllDomain->neighborRateRatio - 1.0);
a = RCVD_MDSYNC_PTR->logMessageInterval;
sm->ppg->syncReceiptTimeoutTimeInterval.nsec =
sm->ppg->syncReceiptTimeout * LOG_TO_NSEC(a);
setPSSyncPSSR(sm, rateRatio, cts64);
sm->ptasg->parentLogSyncInterval = RCVD_MDSYNC_PTR->logMessageInterval;
return &sm->portSyncSync;
}
static port_sync_sync_receive_state_t allstate_condition(port_sync_sync_receive_data_t *sm)
{
if(sm->ptasg->BEGIN || !sm->ptasg->instanceEnable ||
(RCVD_MDSYNC && (!PORT_OPER || !PTP_PORT_ENABLED || !AS_CAPABLE))){
sm->last_state=REACTION;
return DISCARD;
}
return sm->state;
}
static void *discard_proc(port_sync_sync_receive_data_t *sm)
{
UB_LOG(UBL_DEBUGV, "port_sync_sync_receive:%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
RCVD_MDSYNC = false;
return NULL;
}
static port_sync_sync_receive_state_t discard_condition(port_sync_sync_receive_data_t *sm)
{
if(RCVD_MDSYNC && PORT_OPER && PTP_PORT_ENABLED && AS_CAPABLE)
return RECEIVED_SYNC;
return DISCARD;
}
static void *received_sync_proc(port_sync_sync_receive_data_t *sm, uint64_t cts64)
{
UB_LOG(UBL_DEBUGV, "port_sync_sync_receive:%s:domainIndex=%d, portIndex=%d\n",
__func__, sm->domainIndex, sm->portIndex);
//UB_LOG(UBL_DEBUG,"%s:txPSSyncPSSR\n", __func__);
return received_sync(sm, cts64);
}
static port_sync_sync_receive_state_t received_sync_condition(port_sync_sync_receive_data_t *sm)
{
if(RCVD_MDSYNC && PORT_OPER && PTP_PORT_ENABLED && AS_CAPABLE &&
!ASYMMETRY_MEASUREMENT_MODE) return REACTION;
return RECEIVED_SYNC;
}
void *port_sync_sync_receive_sm(port_sync_sync_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 = DISCARD;
break;
case DISCARD:
if(state_change){
retp=discard_proc(sm);
sm->state = RECEIVED_SYNC;
}else{
sm->state = discard_condition(sm);
}
break;
case RECEIVED_SYNC:
if(state_change)
retp=received_sync_proc(sm, cts64);
sm->state = received_sync_condition(sm);
break;
case REACTION:
break;
}
if(retp) return retp;
if(sm->last_state == sm->state) break;
}
return retp;
}
void port_sync_sync_receive_sm_init(port_sync_sync_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(port_sync_sync_receive_data_t, PortSyncSyncReceiveSM, sm);
(*sm)->ptasg = ptasg;
(*sm)->ppg = ppg;
(*sm)->domainIndex = domainIndex;
(*sm)->portIndex = portIndex;
}
int port_sync_sync_receive_sm_close(port_sync_sync_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 *port_sync_sync_receive_sm_recvMDSync(port_sync_sync_receive_data_t *sm,
MDSyncReceive *txMDSyncReceivePtr, uint64_t cts64)
{
RCVD_MDSYNC = true;
RCVD_MDSYNC_PTR = txMDSyncReceivePtr;
sm->last_state = REACTION;
return port_sync_sync_receive_sm(sm, cts64);
}