sntp_client.h
Go to the documentation of this file.
1 /**
2  * @file sntp_client.h
3  * @brief SNTP client (Simple Network Time Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _SNTP_CLIENT_H
32 #define _SNTP_CLIENT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "sntp/ntp_common.h"
37 
38 //SNTP client support
39 #ifndef SNTP_CLIENT_SUPPORT
40  #define SNTP_CLIENT_SUPPORT ENABLED
41 #elif (SNTP_CLIENT_SUPPORT != ENABLED && SNTP_CLIENT_SUPPORT != DISABLED)
42  #error SNTP_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //Default timeout
46 #ifndef SNTP_CLIENT_DEFAULT_TIMEOUT
47  #define SNTP_CLIENT_DEFAULT_TIMEOUT 30000
48 #elif (SNTP_CLIENT_DEFAULT_TIMEOUT < 1000)
49  #error SNTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
50 #endif
51 
52 //Initial retransmission timeout
53 #ifndef SNTP_CLIENT_INIT_RETRANSMIT_TIMEOUT
54  #define SNTP_CLIENT_INIT_RETRANSMIT_TIMEOUT 2000
55 #elif (SNTP_CLIENT_INIT_RETRANSMIT_TIMEOUT < 1000)
56  #error SNTP_CLIENT_INIT_RETRANSMIT_TIMEOUT parameter is not valid
57 #endif
58 
59 //Maximum retransmission timeout
60 #ifndef SNTP_CLIENT_MAX_RETRANSMIT_TIMEOUT
61  #define SNTP_CLIENT_MAX_RETRANSMIT_TIMEOUT 15000
62 #elif (SNTP_CLIENT_MAX_RETRANSMIT_TIMEOUT < 1000)
63  #error SNTP_CLIENT_MAX_RETRANSMIT_TIMEOUT parameter is not valid
64 #endif
65 
66 //Application specific context
67 #ifndef SNTP_CLIENT_PRIVATE_CONTEXT
68  #define SNTP_CLIENT_PRIVATE_CONTEXT
69 #endif
70 
71 //C++ guard
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 
77 /**
78  * @brief SNTP client states
79  **/
80 
81 typedef enum
82 {
88 
89 
90 /**
91  * @brief SNTP client context
92  **/
93 
94 typedef struct
95 {
96  SntpClientState state; ///<SNTP client state
97  NetInterface *interface; ///<Underlying network interface
98  IpAddr serverIpAddr; ///<NTP server address
99  uint16_t serverPort; ///<NTP server port
100  systime_t timeout; ///<Timeout value
101  Socket *socket; ///<Underlying socket
102  systime_t startTime; ///<Request start time
103  systime_t retransmitStartTime; ///<Time at which the last request was sent
104  systime_t retransmitTimeout; ///<Retransmission timeout
105  uint8_t message[NTP_MAX_MSG_SIZE]; ///<Buffer that holds the NTP request/response
106  size_t messageLen; ///<Length of the NTP message, in bytes
107  uint32_t kissCode; ///<Kiss code
108  SNTP_CLIENT_PRIVATE_CONTEXT ///<Application specific context
110 
111 
112 //SNTP client related functions
114 
116 
118  NetInterface *interface);
119 
121  const IpAddr *serverIpAddr, uint16_t serverPort);
122 
124  NtpTimestamp *timestamp);
125 
126 uint32_t sntpClientGetKissCode(SntpClientContext *context);
127 
128 void sntpClientDeinit(SntpClientContext *context);
129 
130 //C++ guard
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif
uint8_t message[]
Definition: chap.h:154
error_t
Error codes.
Definition: error.h:43
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
Definitions common to NTP client and server.
#define NTP_MAX_MSG_SIZE
Definition: ntp_common.h:40
NtpTimestamp
Definition: ntp_common.h:137
uint32_t systime_t
System time.
uint32_t sntpClientGetKissCode(SntpClientContext *context)
Retrieve the kiss code from a Kiss-of-Death message.
Definition: sntp_client.c:233
#define SNTP_CLIENT_PRIVATE_CONTEXT
Definition: sntp_client.h:68
void sntpClientDeinit(SntpClientContext *context)
Release SNTP client context.
Definition: sntp_client.c:259
error_t sntpClientBindToInterface(SntpClientContext *context, NetInterface *interface)
Bind the SNTP client to a particular network interface.
Definition: sntp_client.c:103
error_t sntpClientInit(SntpClientContext *context)
Initialize SNTP client context.
Definition: sntp_client.c:55
error_t sntpClientSetTimeout(SntpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: sntp_client.c:82
error_t sntpClientSetServerAddr(SntpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the IP address of the NTP server.
Definition: sntp_client.c:126
error_t sntpClientGetTimestamp(SntpClientContext *context, NtpTimestamp *timestamp)
Retrieve current time from NTP server.
Definition: sntp_client.c:154
SntpClientState
SNTP client states.
Definition: sntp_client.h:82
@ SNTP_CLIENT_STATE_COMPLETE
Definition: sntp_client.h:86
@ SNTP_CLIENT_STATE_INIT
Definition: sntp_client.h:83
@ SNTP_CLIENT_STATE_SENDING
Definition: sntp_client.h:84
@ SNTP_CLIENT_STATE_RECEIVING
Definition: sntp_client.h:85
#define Socket
Definition: socket.h:36
IP network address.
Definition: ip.h:79
SNTP client context.
Definition: sntp_client.h:95
uint32_t kissCode
Kiss code.
Definition: sntp_client.h:107
systime_t retransmitTimeout
Retransmission timeout.
Definition: sntp_client.h:104
size_t messageLen
Length of the NTP message, in bytes.
Definition: sntp_client.h:106
SntpClientState state
SNTP client state.
Definition: sntp_client.h:96
IpAddr serverIpAddr
NTP server address.
Definition: sntp_client.h:98
uint16_t serverPort
NTP server port.
Definition: sntp_client.h:99
systime_t timeout
Timeout value.
Definition: sntp_client.h:100
systime_t retransmitStartTime
Time at which the last request was sent.
Definition: sntp_client.h:103
Socket * socket
Underlying socket.
Definition: sntp_client.h:101
systime_t startTime
Request start time.
Definition: sntp_client.h:102
NetInterface * interface
Underlying network interface.
Definition: sntp_client.h:97