-
Notifications
You must be signed in to change notification settings - Fork 5
/
SNTP_SERV.scl
417 lines (353 loc) · 16.3 KB
/
SNTP_SERV.scl
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
(*
08/2013
Author: GER I S WEST IA APPS
Simple Network Time Protocol - RFC 4330
SNTP - Server implementation using UDP with OUC (Open User Communication)
This Fb answers NTP Client requests. The internal clock is used as source.
Be sure to set the clock to UTC!
Parameter:
Startup: Use System Flag "First Run"
ReferenceIdentifier: Set to LOCL (uncalibrated local Clock) if not Set. If a external Reference (recommended)
set to '1' for DCF77 or '2' for GPS
LastTimeSet: Set only if GPS or DCF77 time source is used. Update every time the internal clock is synchronized
OUC_Con_ID: Connection reference used bei connection blocks. Default is 0FFF. Change if 0FFF is allready used.
*)
FUNCTION_BLOCK "SNTP_SERV"
TITLE = 'SNTP Server'
AUTHOR : AV
FAMILY : NET
VERSION : '0.1'
//Answers NTP requests
VAR_INPUT
Startup : Bool; // Detect first run
ReferenceIdentifier : BYTE := 0; // 0=LOCL (uncalibrated local Clock); 1=DCF; 2=GPS
LastTimeSet : DT := DT#1990-01-01-00:00:00; // Should be set when Sytem time is set by DCF or GPS
LastTimeSetStuff AT LastTimeSet : STRUCT
Year : BYTE;
Month : BYTE;
Day : BYTE;
Hour : BYTE;
Minute : BYTE;
Second : BYTE;
MilliSecond : WORD;
END_STRUCT;
OUC_Con_ID : WORD := W#16#1; // ID must be unique
// Valid Values for Local Device ID
// B#16#01 (IF1) bei WinAC RTX, IM151-8 PN/DP CPU, S7-1200
// B#16#02 CPU31x-2 PN/DP, CPU314C-2 PN/DP und IM154-8 CPU
// B#16#03 CPU319-3 PN/DP, CPU315T-3 PN/DP, CPU317T-3 PN/DP, CPU317TF-3 PN/DP
// B#16#05 CPU412-2 PN, CPU414-3 PN/DP, CPU416-3 PN/DP und CPU41x-5H PN/DP (Rack 0)
// B#16#06 (IF2) bei WinAC RTX
// B#16#0B (IF3) bei WinAC RTX
// B#16#0F (IF4) bei WinAC RTX
// B#16#15 CPU 41x-5H PN/DP (Rack 1)
LocDevID : BYTE := 0;
END_VAR
VAR_OUTPUT
ERROR : BOOL; // ERROR bit
STATUS : DWORD;
END_VAR
VAR
Send_Buf : Struct // Data to Client
Flags : Array[0..3] of Byte;
Root_Delay : DWord;
Root_Dispersion : DWord;
Reference_identifier : ARRAY [0..3] of Char;
Reference_TimeStamp_Sec : DWORD;
Reference_TimeSTamp_Frac : DWORD;
Originate_TimeStamp_Sec : DWORD;
Originate_TimeStamp_Frac : DWORD;
Receive_TimeStamp_Sec : DWORD;
Receive_TimeSTamp_Frac : DWORD;
Transmit_TimeStamp_Sec : DWORD;
Transmit_TimeStamp_Frac : DWORD;
END_STRUCT;
Receive_Buf : Struct // Date from Client
Flags : Array[0..3] of Byte;
Root_Delay : DWord;
Root_Dispersion : DWord;
Reference_identifier : DWord;
Reference_TimeStamp_Sec : DWord;
Reference_TimeSTamp_Frac : DWord;
Originate_TimeStamp_Sec : DWord;
Originate_TimeSTamp_Frac : DWord;
Receive_TimeStamp_Sec : DWord;
Receive_TimeSTamp_Frac : DWord;
Transmit_TimeStamp_Sec : DWord;
Transmit_TimeSTamp_Frac : DWord;
KeyIdentifier : DWord;
MessageDigest : Array[0..15] of Byte;
END_STRUCT;
TURCV_Instance : TURCV;
RecSenderData : TADDR_Par; // Client Address Data
RecBusy : Bool;
RecError : Bool;
RecNdr : Bool;
RecLen : INT; //UINT
RecStatus : WORD;
TCON_Instance : TCON;
ConReq : Bool;
ConPar : TCON_PAR;
ConDone : Bool;
ConBusy : Bool;
ConError : Bool;
ConStatus : Word;
State : Int; // 0 = Startup; 1 = Wait for Client request; 2 = Send Time to Client
TUSEND_Instance : TUSEND;
SendReq : Bool;
SendDone : Bool;
SendBusy : Bool;
SendError : Bool;
SendStatus : Word;
SendData : TADDR_PAR;
RecReq : Bool;
SendReqDone : BOOL;
LastStartup: BOOL;
END_VAR
VAR_TEMP
SysTime : DT; // System Time
SysTimeStuff AT SysTime : STRUCT
Year : BYTE;
Month : BYTE;
Day : BYTE;
Hour : BYTE;
Minute : BYTE;
Second : BYTE;
MilliSecond : WORD;
END_STRUCT;
YEAR : INT;
MONTH : INT;
DAY : INT;
HOUR : INT;
MINUTE : INT;
SECOND : INT;
MILLISECOND : INT;
WDAY : INT; // WeekDay [1..7] 1=Sunday
ret : Int; // Function Return value
tmp : WORD; // UINT Temporaray Variable
xms : DWORD;
NTPTime_frac : DWORD;
NTPTime_sec : DWORD;
END_VAR
BEGIN
// Init everything on first run
IF Startup AND NOT LastStartup THEN
ConReq := FALSE;
ConDone := FALSE;
ConBusy := FALSE ;
ConError := FALSE;
ConStatus := 0;
RecReq := FALSE;
RecBusy := FALSE;
RecError := FALSE;
RecNdr := FALSE;
RecStatus := 0;
SendReq := FALSE;
SendBusy := FALSE;
SendError := FALSE;
SendDone := FALSE;
RecStatus := 0;
SendReqDone := FALSE;
STATUS := 0;
// Set connection parameters
ConPar.BLOCK_LENGTH := 64;
ConPar.ID := OUC_Con_ID; // Highest Connection ID used, change if already in use
ConPar.ACTIVE_EST := FALSE;
ConPar.CONNECTION_TYPE := 19; // 19: UDP -> SNTP uses UDP
ConPar.LOCAL_DEVICE_ID := 1; // S7-1200 1: local IE Interface
ConPar.LOCAL_TSAP_ID_LEN := 2;
ConPar.REM_SUBNET_ID_LEN := 0;
ConPar.REM_STADDR_LEN := 0;
ConPar.REM_TSAP_ID_LEN := 0;
ConPar.LOCAL_TSAP_ID[1] := 0; // Port No High Byte
ConPar.LOCAL_TSAP_ID[2] := 123; // Port No Low Byte -> SNTP uses Port 123 (7Bh)
State := 0;
END_IF;
//Check Local Device ID
IF LocDevID = B#16#01 OR LocDevID = B#16#02 OR LocDevID = B#16#03 OR LocDevID = B#16#05
OR LocDevID = B#16#06 OR LocDevID = B#16#0B OR LocDevID = B#16#0F OR LocDevID = B#16#15 THEN
ConPar.LOCAL_DEVICE_ID := LocDevID;
STATUS := STATUS AND W#2#01111111;
ELSE
ERROR:= TRUE;
STATUS := STATUS OR W#2#10000000;
RETURN;
END_IF;
IF State = 0 THEN // Initiate connection
IF Startup = False THEN ConReq := True; END_IF;
TCON_Instance(REQ:=ConReq, ID:=OUC_Con_ID, CONNECT:= ConPar);
ConBusy := TCON_Instance.Busy;
ConDone := TCON_Instance.Done;
ConError := TCON_Instance.Error;
ConStatus := TCON_Instance.Status;
IF ConDone = true AND ConError = false THEN
State := 1; //Change State to TCON OK
END_IF;
END_IF;
IF State = 1 THEN // Wait for NTP-Client request
IF RecBusy = FALSE THEN
RecReq := True;
END_IF;
TURCV_Instance(ID:=OUC_Con_ID, EN_R := RecReq, LEN := 68, DATA := Receive_Buf, ADDR:=RecSenderData);
RecBusy := TURCV_Instance.BUSY;
RecError := TURCV_Instance.ERROR;
RecNdr := TURCV_Instance.NDR;
RecLen := TURCV_Instance.RCVD_LEN;
RecStatus := TURCV_Instance.STATUS;
// Check if new data arrived
IF RecNdr = TRUE AND RecError = FALSE THEN
RecReq := FALSE;
// Check received length and MODE ( 3 = Client) |LI | VN |MODE |
IF RecLen = 48 AND ((Receive_Buf.Flags[0] AND BYTE#2#00000011) = BYTE#2#00000011) THEN
State := 2;
END_IF;
END_IF;
END_IF;
// Send time to client
IF State = 2 THEN
IF SendDone = TRUE OR SendError = TRUE THEN
SendReq := FALSE;
SendDone := FALSE;
SendError := FALSE;
SendReqDone := False;
State := 1;
RETURN;
END_IF;
IF SendReqDone = False THEN
// Set Address and port for client
SendData.REM_IP_ADDR[1] := RecSenderData.REM_IP_ADDR[1];
SendData.REM_IP_ADDR[2] := RecSenderData.REM_IP_ADDR[2];
SendData.REM_IP_ADDR[3] := RecSenderData.REM_IP_ADDR[3];
SendData.REM_IP_ADDR[4] := RecSenderData.REM_IP_ADDR[4];
SendData.REM_PORT_NR := RecSenderData.REM_PORT_NR;
// Set NTP Data
Send_Buf.Root_Delay := 0;
Send_Buf.Root_Dispersion := 0;
// Read System Time
// Asume invalid time if year < 2010
ret := READ_CLK(CDT := SysTime);
//IF ret <> 0 OR SysTime.YEAR < 2010 THEN
IF ret <> 0 OR (BCD_TO_INT(SysTimeStuff.YEAR) < 10) THEN
Send_Buf.Flags[1] := 0; // Stratum
ELSE
Send_Buf.Flags[1] := 1; // Stratum
END_IF;
// Calculate NTP Time as secs since 1900-1-1
NTPTime_sec := DINT_TO_DWORD((DATE_TO_DINT(DT_DATE(SysTime)) * 86400) + (TOD_TO_DINT(DT_TOD(SysTime)) / 1000) + DINT#631152000 + DINT#2208988800);
// Calculate fractional part of NTP - Timestamp
// Eliminate Weekday to get ms
xms := DINT_TO_DWORD(BCD_TO_DINT(SHR(IN := SysTimeStuff.MILLISECOND AND W#16#FFF0, N := 4)));
// Convert to pico seconds wit ms precision ms * 2^32 / 1000
NTPTime_frac := DINT_TO_DWORD((DWORD_TO_DINT(xms) * DINT#65536) / DINT#1000);
NTPTime_frac := SHL(IN := NTPTime_frac, N := 16);
// Fill buffer with Reiceive and Transmit Timestamp
Send_Buf.Receive_TimeStamp_sec := NTPTime_sec;
Send_Buf.Receive_TimeStamp_frac := NTPTime_frac;
Send_Buf.Transmit_TimeStamp_sec := NTPTime_sec;
Send_Buf.Transmit_TimeStamp_frac := NTPTime_frac;
// Set Reference Timestamp to actual time if no other information available
IF LastTimeSet = DT#1990-01-01-00:00:00 THEN
Send_Buf.Reference_TimeStamp_sec := NTPTime_sec;
Send_Buf.Reference_TimeStamp_frac := NTPTime_frac;
ELSE
// Calculate Reference Timestamp
// Calculate NTP Time as secs since 1900-1-1
Send_Buf.Reference_TimeStamp_sec := DINT_TO_DWORD((DATE_TO_DINT(DT_DATE(LastTimeSet)) * 86400) + (TOD_TO_DINT(DT_TOD(LastTimeSet)) / 1000) + DINT#631152000 + DINT#2208988800);
// Calculate fractional part of NTP - Timestamp
// Eliminate Weekday to get ms
xms := DINT_TO_DWORD(BCD_TO_DINT(SHR(IN := LastTimeSetStuff.MILLISECOND AND W#16#FFF0, N := 4)));
// Convert to pico seconds wit ms precision ms * 2^32 / 1000
Send_Buf.Reference_TimeStamp_frac := DINT_TO_DWORD((DWORD_TO_DINT(xms) * DINT#65536) / DINT#1000);
Send_Buf.Reference_TimeStamp_frac := SHL(IN := Send_Buf.Reference_TimeStamp_frac, N := 16);
END_IF;
// Fill Send Buffer according to RFC 4330
Send_Buf.Flags[0] := Byte#2#00011100; // Mode - No Warning, NTP Version 3, Server
Send_Buf.Flags[2] := Receive_Buf.Flags[2]; // Poll - Peer polling intervall copied from request
Send_Buf.Flags[3] := Byte#16#FA; // Clock Precision -6 =-0,015625s
Send_Buf.Root_Delay := 0; // Root Delay
Send_Buf.Root_Delay := 0; // Root Dispersion
//Set reference identifier according to RFC 4330, only LOCL, DCF and GPS used
IF ReferenceIdentifier = 1 THEN
Send_Buf.Reference_identifier[0] := 'D';
Send_Buf.Reference_identifier[1] := 'C';
Send_Buf.Reference_identifier[2] := 'F';
ELSIF ReferenceIdentifier = 2 THEN
Send_Buf.Reference_identifier[0] := 'G';
Send_Buf.Reference_identifier[1] := 'P';
Send_Buf.Reference_identifier[2] := 'S';
ELSE
Send_Buf.Reference_identifier[0] := 'L';
Send_Buf.Reference_identifier[1] := 'O';
Send_Buf.Reference_identifier[2] := 'C';
Send_Buf.Reference_identifier[3] := 'L';
END_IF;
// Copy received originate Timestamp to Transmit Timestamp
Send_Buf.Originate_TimeStamp_Sec := Receive_Buf.Transmit_TimeStamp_Sec;
Send_Buf.Originate_TimeSTamp_Frac := Receive_Buf.Transmit_TimeSTamp_Frac;
SendReq := TRUE; // Send
SendReqDone := TRUE;
END_IF;
END_IF;
TUSEND_Instance(REQ:=SendReq,ID:=OUC_Con_ID, LEN:= 48, DATA:=Send_Buf, ADDR:=SendData);
SendDone := TUSEND_Instance.DONE;
SendBusy := TUSEND_Instance.BUSY;
SendError := TUSEND_Instance.ERROR;
SendStatus := TUSEND_Instance.STATUS;
SendReq := FALSE;
//set output values
IF ConError OR SendError OR RecError THEN
ERROR := 1;
IF ConError THEN
STATUS:=(STATUS AND 16#10000);
STATUS:= (STATUS OR ConStatus);
ELSIF SendError THEN
STATUS:=(STATUS AND 16#20000);
STATUS:= (STATUS OR SendStatus);
ELSIF RecError THEN
STATUS:=(STATUS AND 16#30000);
STATUS:= (STATUS OR RecStatus);
END_IF;
ELSE
ERROR:= 0;
STATUS:= 16#0;
END_IF;
LastStartup := Startup;
(*
NTP Packet Header - Details see RFC 4330
1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN |Mode | Stratum | Poll | Precision |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Delay |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Dispersion |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reference Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Reference Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Originate Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Receive Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Transmit Timestamp (64) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Key Identifier (optional) (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| |
| Message Digest (optional) (128) |
| |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*)
END_FUNCTION_BLOCK