-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathclock_slave_sync_sm.c
211 lines (189 loc) · 6.8 KB
/
clock_slave_sync_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
/*
* 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 "clock_slave_sync_sm.h"
typedef enum {
INIT,
INITIALIZING,
SEND_SYNC_INDICATION,
REACTION,
}clock_slave_sync_state_t;
struct clock_slave_sync_data{
PerTimeAwareSystemGlobal *ptasg;
clock_slave_sync_state_t state;
clock_slave_sync_state_t last_state;
ClockSlaveSyncSM *thisSM;
int domainIndex;
};
#define RCVD_PSSYNC sm->thisSM->rcvdPSSync
#define RCVD_PSSYNC_PTR sm->thisSM->rcvdPSSyncPtr
#define RCVD_LOCAL_CLOCK_TICK sm->thisSM->rcvdLocalClockTick
static void updateSlaveTime(clock_slave_sync_data_t *sm)
{
int64_t ts64;
struct timespec ts;
UB_LOG(UBL_DEBUGV, "clock_slave_sync:%s:domainIndex=%d\n", __func__, sm->domainIndex);
// ??? regardless of sm->ptasg->gmPresent, do this way
ts64=gptpclock_getts64(sm->ptasg->thisClockIndex, sm->ptasg->domainNumber);
UB_NSEC2TS(ts64, ts);
sm->ptasg->clockSlaveTime.seconds.lsb=ts.tv_sec;
sm->ptasg->clockSlaveTime.fractionalNanoseconds.msb=ts.tv_nsec;
}
static clock_slave_sync_state_t allstate_condition(clock_slave_sync_data_t *sm)
{
if(sm->ptasg->BEGIN || !sm->ptasg->instanceEnable ) {
return INITIALIZING;
}
return sm->state;
}
static void *initializing_proc(clock_slave_sync_data_t *sm)
{
UB_LOG(UBL_DEBUGV, "clock_slave_sync:%s:domainIndex=%d\n", __func__, sm->domainIndex);
RCVD_PSSYNC = false;
return NULL;
}
static clock_slave_sync_state_t initializing_condition(clock_slave_sync_data_t *sm)
{
if(RCVD_PSSYNC || RCVD_LOCAL_CLOCK_TICK) return SEND_SYNC_INDICATION;
return INITIALIZING;
}
static void *send_sync_indication_proc(clock_slave_sync_data_t *sm)
{
uint64_t nsec;
void *smret=NULL;
UB_LOG(UBL_DEBUGV, "clock_slave_sync:%s:domainIndex=%d\n", __func__, sm->domainIndex);
// localPortNumber==0 means, sync by ClockMasterSyncSend,
// and don't need to issue syncReciptTime event
if (RCVD_PSSYNC && RCVD_PSSYNC_PTR->localPortNumber ) {
nsec=((RCVD_PSSYNC_PTR->preciseOriginTimestamp.seconds.lsb * UB_SEC_NS) +
RCVD_PSSYNC_PTR->preciseOriginTimestamp.nanoseconds) +
RCVD_PSSYNC_PTR->followUpCorrectionField.nsec;
if(RCVD_PSSYNC_PTR->local_ppg)
nsec += RCVD_PSSYNC_PTR->local_ppg->forAllDomain->neighborPropDelay.nsec *
(RCVD_PSSYNC_PTR->rateRatio /
RCVD_PSSYNC_PTR->local_ppg->forAllDomain->neighborRateRatio) +
RCVD_PSSYNC_PTR->local_ppg->forAllDomain->delayAsymmetry.nsec;
sm->ptasg->lastSyncSeqID = RCVD_PSSYNC_PTR->lastSyncSeqID;
sm->ptasg->syncReceiptTime.seconds.lsb = nsec / UB_SEC_NS;
sm->ptasg->syncReceiptTime.fractionalNanoseconds.msb = nsec % UB_SEC_NS;
sm->ptasg->syncReceiptLocalTime.nsec = RCVD_PSSYNC_PTR->upstreamTxTime.nsec;
if(RCVD_PSSYNC_PTR->local_ppg)
sm->ptasg->syncReceiptLocalTime.nsec +=
RCVD_PSSYNC_PTR->local_ppg->forAllDomain->neighborPropDelay.nsec /
RCVD_PSSYNC_PTR->local_ppg->forAllDomain->neighborRateRatio +
RCVD_PSSYNC_PTR->local_ppg->forAllDomain->delayAsymmetry.nsec /
RCVD_PSSYNC_PTR->rateRatio;
sm->ptasg->gmTimeBaseIndicator = RCVD_PSSYNC_PTR->gmTimeBaseIndicator;
sm->ptasg->lastGmPhaseChange = RCVD_PSSYNC_PTR->lastGmPhaseChange;
sm->ptasg->lastGmFreqChange = RCVD_PSSYNC_PTR->lastGmFreqChange;
/* 'static port config' and 'this is not GM'
Since BMCS is not performed, port_announce port_state_selection SMs
will not be able to get GM information.
For static port slave mode, consider the peer clockId as the GM.
*/
if(gptpconf_get_intitem(CONF_STATIC_PORT_STATE_SLAVE_PORT)>0){
if(memcmp(sm->ptasg->gmIdentity, RCVD_PSSYNC_PTR->sourcePortIdentity.clockIdentity,
sizeof(ClockIdentity))!=0){
memcpy(sm->ptasg->gmIdentity, RCVD_PSSYNC_PTR->sourcePortIdentity.clockIdentity,
sizeof(ClockIdentity));
gptpclock_set_gmchange(sm->ptasg->domainNumber, sm->ptasg->gmIdentity);
}
}
// this may need IPC notice
//invokeApplicationInterfaceFunction (ClockTargetPhaseDiscontinuity.result);
UB_LOG(UBL_DEBUGV, "clock_slave_sync: syncReceiptTime\n");
smret = &sm->ptasg->syncReceiptTime;
}
if(RCVD_LOCAL_CLOCK_TICK) updateSlaveTime(sm);
RCVD_PSSYNC = false;
RCVD_LOCAL_CLOCK_TICK = false;
return smret;
}
static clock_slave_sync_state_t send_sync_indication_condition(clock_slave_sync_data_t *sm)
{
if(RCVD_PSSYNC || RCVD_LOCAL_CLOCK_TICK) {
sm->last_state = REACTION;
}
return SEND_SYNC_INDICATION;
}
void *clock_slave_sync_sm(clock_slave_sync_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 = INITIALIZING;
break;
case INITIALIZING:
if(state_change)
retp=initializing_proc(sm);
sm->state = initializing_condition(sm);
break;
case SEND_SYNC_INDICATION:
if(state_change)
retp=send_sync_indication_proc(sm);
sm->state = send_sync_indication_condition(sm);
break;
case REACTION:
break;
}
if(retp) return retp;
if(sm->last_state == sm->state) break;
}
return retp;
}
void clock_slave_sync_sm_init(clock_slave_sync_data_t **sm,
int domainIndex,
PerTimeAwareSystemGlobal *ptasg)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d\n", __func__, domainIndex);
INIT_SM_DATA(clock_slave_sync_data_t, ClockSlaveSyncSM, sm);
(*sm)->ptasg = ptasg;
(*sm)->domainIndex = domainIndex;
}
int clock_slave_sync_sm_close(clock_slave_sync_data_t **sm)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d\n", __func__, (*sm)->domainIndex);
CLOSE_SM_DATA(sm);
return 0;
}
void *clock_slave_sync_sm_loclClockUpdate(clock_slave_sync_data_t *sm, uint64_t cts64)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d\n", __func__, sm->domainIndex);
RCVD_LOCAL_CLOCK_TICK = true;
return clock_slave_sync_sm(sm, cts64);
}
void *clock_slave_sync_sm_portSyncSync(clock_slave_sync_data_t *sm,
PortSyncSync *portSyncSync, uint64_t cts64)
{
UB_LOG(UBL_DEBUGV, "%s:domainIndex=%d\n", __func__, sm->domainIndex);
RCVD_PSSYNC = true;
RCVD_PSSYNC_PTR = portSyncSync;
return clock_slave_sync_sm(sm, cts64);
}