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