1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 /* */
28 /*	ntpClient.h */
29 /* */
30 
31 #ifndef __NTPCLIENT_H__
32 
33 #define __NTPCLIENT_H__
34 
35 #define NTP_PORT    123
36 
37 typedef uint32_t           quint32;
38 typedef int32_t            qint32;
39 typedef uint8_t            quint8;
40 typedef int8_t             qint8;
41 
42 typedef union _SNtpFlags   SNtpFlags;
43 
44 #ifdef _MSC_VER
45     #define __attribute__( x )
46 #endif
47 
48 /**
49  * 64-bit NTP timestamp.
50  */
51 struct __attribute__( ( __packed__ ) ) _SNtpTimestamp
52 {
53     /** Number of seconds passed since Jan 1 1900, in big-endian format. */
54     quint32 seconds;
55 
56     /** Fractional time part, in <tt>1/0xFFFFFFFF</tt>s of a second. */
57     quint32 fraction;
58 };
59 
60 typedef struct _SNtpTimestamp SNtpTimestamp;
61 
62 /**
63  * Mandatory part of an NTP packet
64  */
65 struct SNtpPacket
66 {
67     /** Flags. */
68     unsigned char flags; /* value 0xDB : mode 3 (client), version 3, leap indicator unknown 3 */
69 
70     /** Stratum of the clock. */
71     quint8 stratum; /* value 0 : unspecified */
72 
73     /** Maximum interval between successive messages, in log2 seconds. Note that the value is signed. */
74     qint8 poll; /* 10 means 1 << 10 = 1024 seconds */
75 
76     /** Precision of the clock, in log2 seconds. Note that the value is signed. */
77     qint8 precision; /* 0xFA = 250 = 0.015625 seconds */
78 
79     /** Round trip time to the primary reference source, in NTP short format. */
80     qint32 rootDelay; /* 0x5D2E = 23854 or (23854/65535)= 0.3640 sec */
81 
82     /** Nominal error relative to the primary reference source. */
83     qint32 rootDispersion; /* 0x0008 CAC8 = 8.7912  seconds */
84 
85     /** Reference identifier (either a 4 character string or an IP address). */
86     qint8 referenceID[ 4 ]; /* or just 0000 */
87 
88     /** The time at which the clock was last set or corrected. */
89     SNtpTimestamp referenceTimestamp; /* Current time */
90 
91     /** The time at which the request departed the client for the server. */
92     SNtpTimestamp originateTimestamp; /* Keep 0 */
93 
94     /** The time at which the request arrived at the server. */
95     SNtpTimestamp receiveTimestamp; /* Keep 0 */
96 
97     /** The time at which the reply departed the server for client. */
98     SNtpTimestamp transmitTimestamp;
99 };
100 
101 /* Add this number to get secs since 1-1-1900 */
102 #define TIME1970    2208988800UL
103 
104 #endif /* __NTPCLIENT_H__ */
105