sntp_client.c
Go to the documentation of this file.
1 /**
2  * @file sntp_client.c
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-2026 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  * @section Description
28  *
29  * The Simple Network Time Protocol is used to synchronize computer clocks
30  * in the Internet. Refer to RFC 4330 for more details
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.6.0
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL SNTP_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/net.h"
41 #include "sntp/sntp_client.h"
42 #include "sntp/sntp_client_misc.h"
43 #include "debug.h"
44 
45 //Check TCP/IP stack configuration
46 #if (SNTP_CLIENT_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief Initialize SNTP client context
51  * @param[in] context Pointer to the SNTP client context
52  * @return Error code
53  **/
54 
56 {
57  //Make sure the SNTP client context is valid
58  if(context == NULL)
60 
61  //Clear SNTP client context
62  osMemset(context, 0, sizeof(SntpClientContext));
63 
64  //Attach TCP/IP stack context
65  context->netContext = netGetDefaultContext();
66 
67  //Initialize SNTP client state
68  context->state = SNTP_CLIENT_STATE_INIT;
69 
70  //Default protocol version
71  context->version = NTP_VERSION_3;
72  //Default timeout
74 
75  //Successful initialization
76  return NO_ERROR;
77 }
78 
79 
80 /**
81  * @brief Set the NTP protocol version to be used
82  * @param[in] context Pointer to the SNTP client context
83  * @param[in] version NTP protocol version (2 or 3)
84  * @return Error code
85  **/
86 
88 {
89  //Make sure the SNTP client context is valid
90  if(context == NULL)
92 
93  //Check NTP version
95  return ERROR_INVALID_VERSION;
96 
97  //Save the protocol version to be used
98  context->version = version;
99 
100  //Successful initialization
101  return NO_ERROR;
102 }
103 
104 
105 /**
106  * @brief Set communication timeout
107  * @param[in] context Pointer to the SNTP client context
108  * @param[in] timeout Timeout value, in milliseconds
109  * @return Error code
110  **/
111 
113 {
114  //Make sure the SNTP client context is valid
115  if(context == NULL)
117 
118  //Save timeout value
119  context->timeout = timeout;
120 
121  //Successful processing
122  return NO_ERROR;
123 }
124 
125 
126 /**
127  * @brief Bind the SNTP client to a particular network interface
128  * @param[in] context Pointer to the SNTP client context
129  * @param[in] interface Network interface to be used
130  * @return Error code
131  **/
132 
134  NetInterface *interface)
135 {
136  //Make sure the SNTP client context is valid
137  if(context == NULL)
139 
140  //Explicitly associate the SNTP client with the specified interface
141  context->interface = interface;
142 
143  //Successful processing
144  return NO_ERROR;
145 }
146 
147 
148 /**
149  * @brief Specify the IP address of the NTP server
150  * @param[in] context Pointer to the SNTP client context
151  * @param[in] serverIpAddr IP address of the NTP server
152  * @param[in] serverPort Port number
153  * @return Error code
154  **/
155 
157  const IpAddr *serverIpAddr, uint16_t serverPort)
158 {
159  //Check parameters
160  if(context == NULL || serverIpAddr == NULL)
162 
163  //Save the IP address and the port number of the NTP server
164  context->serverIpAddr = *serverIpAddr;
165  context->serverPort = serverPort;
166 
167  //Close UDP socket
168  sntpClientCloseConnection(context);
169  //Revert to default state
170  context->state = SNTP_CLIENT_STATE_INIT;
171 
172  //Successful processing
173  return NO_ERROR;
174 }
175 
176 
177 /**
178  * @brief Retrieve current time from NTP server
179  * @param[in] context Pointer to the SNTP client context
180  * @param[out] timestamp Pointer to the NTP timestamp
181  * @return Error code
182  **/
183 
185  NtpTimestamp *timestamp)
186 {
187  error_t error;
188 
189  //Check parameters
190  if(context == NULL || timestamp == NULL)
192 
193  //Initialize status code
194  error = NO_ERROR;
195 
196  //Send NTP request and wait for server's response
197  while(!error)
198  {
199  //Check current state
200  if(context->state == SNTP_CLIENT_STATE_INIT)
201  {
202  //Open UDP socket
203  error = sntpClientOpenConnection(context);
204 
205  //Check status code
206  if(!error)
207  {
208  //Save current time
209  context->startTime = osGetSystemTime();
210  //Initialize retransmission timeout
212 
213  //Send the request to the designated NTP server
214  context->state = SNTP_CLIENT_STATE_SENDING;
215  }
216  }
217  else if(context->state == SNTP_CLIENT_STATE_SENDING)
218  {
219  //Send the request to the designated NTP server
220  error = sntpClientSendRequest(context);
221  }
222  else if(context->state == SNTP_CLIENT_STATE_RECEIVING)
223  {
224  //Wait for server's response
225  error = sntpClientReceiveResponse(context);
226  }
227  else if(context->state == SNTP_CLIENT_STATE_COMPLETE)
228  {
229  //Extract NTP timestamp from server's response
230  error = sntpClientParseResponse(context, timestamp);
231  //We are done
232  break;
233  }
234  else
235  {
236  //Invalid state
237  error = ERROR_WRONG_STATE;
238  }
239  }
240 
241  //Check status code
242  if(error != ERROR_WOULD_BLOCK)
243  {
244  //Close UDP socket
245  sntpClientCloseConnection(context);
246  //Revert to default state
247  context->state = SNTP_CLIENT_STATE_INIT;
248  }
249 
250  //Return status code
251  return error;
252 }
253 
254 
255 /**
256  * @brief Retrieve the kiss code from a Kiss-of-Death message
257  * @param[in] context Pointer to the SNTP client context
258  * @return Kiss code
259  **/
260 
262 {
263  uint32_t kissCode;
264 
265  //Make sure the SNTP client context is valid
266  if(context != NULL)
267  {
268  //Get kiss code
269  kissCode = context->kissCode;
270  }
271  else
272  {
273  //The SNTP client context is not valid
274  kissCode = 0;
275  }
276 
277  //Return kiss code
278  return kissCode;
279 }
280 
281 
282 /**
283  * @brief Release SNTP client context
284  * @param[in] context Pointer to the SNTP client context
285  **/
286 
288 {
289  //Make sure the SNTP client context is valid
290  if(context != NULL)
291  {
292  //Close UDP socket
293  sntpClientCloseConnection(context);
294 
295  //Clear SNTP client context
296  osMemset(context, 0, sizeof(SntpClientContext));
297  }
298 }
299 
300 #endif
error_t sntpClientInit(SntpClientContext *context)
Initialize SNTP client context.
Definition: sntp_client.c:55
@ SNTP_CLIENT_STATE_COMPLETE
Definition: sntp_client.h:93
@ ERROR_WOULD_BLOCK
Definition: error.h:96
IP network address.
Definition: ip.h:90
NtpTimestamp
Definition: ntp_common.h:185
void sntpClientCloseConnection(SntpClientContext *context)
Close UDP connection.
uint32_t sntpClientGetKissCode(SntpClientContext *context)
Retrieve the kiss code from a Kiss-of-Death message.
Definition: sntp_client.c:261
uint8_t version
Definition: coap_common.h:177
@ ERROR_INVALID_VERSION
Definition: error.h:118
error_t sntpClientOpenConnection(SntpClientContext *context)
Open UDP connection.
@ ERROR_WRONG_STATE
Definition: error.h:210
IpAddr serverIpAddr
NTP server address.
Definition: sntp_client.h:107
systime_t startTime
Request start time.
Definition: sntp_client.h:111
SNTP client context.
Definition: sntp_client.h:102
#define SNTP_CLIENT_INIT_RETRANSMIT_TIMEOUT
Definition: sntp_client.h:54
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
SntpClientState state
SNTP client state.
Definition: sntp_client.h:103
error_t
Error codes.
Definition: error.h:43
#define SNTP_CLIENT_DEFAULT_TIMEOUT
Definition: sntp_client.h:47
NetContext * netContext
TCP/IP stack context.
Definition: sntp_client.h:105
#define NetInterface
Definition: net.h:40
@ SNTP_CLIENT_STATE_RECEIVING
Definition: sntp_client.h:92
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:527
error_t sntpClientBindToInterface(SntpClientContext *context, NetInterface *interface)
Bind the SNTP client to a particular network interface.
Definition: sntp_client.c:133
@ NTP_VERSION_3
Definition: ntp_common.h:80
error_t sntpClientGetTimestamp(SntpClientContext *context, NtpTimestamp *timestamp)
Retrieve current time from NTP server.
Definition: sntp_client.c:184
void sntpClientDeinit(SntpClientContext *context)
Release SNTP client context.
Definition: sntp_client.c:287
error_t sntpClientReceiveResponse(SntpClientContext *context)
Wait for NTP server's response.
systime_t timeout
Timeout value.
Definition: sntp_client.h:109
error_t sntpClientParseResponse(SntpClientContext *context, NtpTimestamp *timestamp)
Parse NTP server's response.
uint32_t systime_t
System time.
@ SNTP_CLIENT_STATE_INIT
Definition: sntp_client.h:90
error_t sntpClientSendRequest(SntpClientContext *context)
Send request to the NTP server.
uint32_t kissCode
Kiss code.
Definition: sntp_client.h:116
@ SNTP_CLIENT_STATE_SENDING
Definition: sntp_client.h:91
error_t sntpClientSetServerAddr(SntpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the IP address of the NTP server.
Definition: sntp_client.c:156
NtpVersion
NTP version numbers.
Definition: ntp_common.h:77
NtpVersion version
NTP protocol version.
Definition: sntp_client.h:104
systime_t retransmitTimeout
Retransmission timeout.
Definition: sntp_client.h:113
error_t sntpClientSetVersion(SntpClientContext *context, NtpVersion version)
Set the NTP protocol version to be used.
Definition: sntp_client.c:87
NetInterface * interface
Underlying network interface.
Definition: sntp_client.h:106
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
SNTP client (Simple Network Time Protocol)
@ NTP_VERSION_4
Definition: ntp_common.h:81
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint16_t serverPort
NTP server port.
Definition: sntp_client.h:108
systime_t osGetSystemTime(void)
Retrieve system time.
error_t sntpClientSetTimeout(SntpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: sntp_client.c:112
Helper functions for SNTP client.