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-2025 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.5.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  //Initialize SNTP client state
65  context->state = SNTP_CLIENT_STATE_INIT;
66 
67  //Default protocol version
68  context->version = NTP_VERSION_3;
69  //Default timeout
71 
72  //Successful initialization
73  return NO_ERROR;
74 }
75 
76 
77 /**
78  * @brief Set the NTP protocol version to be used
79  * @param[in] context Pointer to the SNTP client context
80  * @param[in] version NTP protocol version (2 or 3)
81  * @return Error code
82  **/
83 
85 {
86  //Make sure the SNTP client context is valid
87  if(context == NULL)
89 
90  //Check NTP version
92  return ERROR_INVALID_VERSION;
93 
94  //Save the protocol version to be used
95  context->version = version;
96 
97  //Successful initialization
98  return NO_ERROR;
99 }
100 
101 
102 /**
103  * @brief Set communication timeout
104  * @param[in] context Pointer to the SNTP client context
105  * @param[in] timeout Timeout value, in milliseconds
106  * @return Error code
107  **/
108 
110 {
111  //Make sure the SNTP client context is valid
112  if(context == NULL)
114 
115  //Save timeout value
116  context->timeout = timeout;
117 
118  //Successful processing
119  return NO_ERROR;
120 }
121 
122 
123 /**
124  * @brief Bind the SNTP client to a particular network interface
125  * @param[in] context Pointer to the SNTP client context
126  * @param[in] interface Network interface to be used
127  * @return Error code
128  **/
129 
131  NetInterface *interface)
132 {
133  //Make sure the SNTP client context is valid
134  if(context == NULL)
136 
137  //Explicitly associate the SNTP client with the specified interface
138  context->interface = interface;
139 
140  //Successful processing
141  return NO_ERROR;
142 }
143 
144 
145 /**
146  * @brief Specify the IP address of the NTP server
147  * @param[in] context Pointer to the SNTP client context
148  * @param[in] serverIpAddr IP address of the NTP server
149  * @param[in] serverPort Port number
150  * @return Error code
151  **/
152 
154  const IpAddr *serverIpAddr, uint16_t serverPort)
155 {
156  //Check parameters
157  if(context == NULL || serverIpAddr == NULL)
159 
160  //Save the IP address and the port number of the NTP server
161  context->serverIpAddr = *serverIpAddr;
162  context->serverPort = serverPort;
163 
164  //Close UDP socket
165  sntpClientCloseConnection(context);
166  //Revert to default state
167  context->state = SNTP_CLIENT_STATE_INIT;
168 
169  //Successful processing
170  return NO_ERROR;
171 }
172 
173 
174 /**
175  * @brief Retrieve current time from NTP server
176  * @param[in] context Pointer to the SNTP client context
177  * @param[out] timestamp Pointer to the NTP timestamp
178  * @return Error code
179  **/
180 
182  NtpTimestamp *timestamp)
183 {
184  error_t error;
185 
186  //Check parameters
187  if(context == NULL || timestamp == NULL)
189 
190  //Initialize status code
191  error = NO_ERROR;
192 
193  //Send NTP request and wait for server's response
194  while(!error)
195  {
196  //Check current state
197  if(context->state == SNTP_CLIENT_STATE_INIT)
198  {
199  //Open UDP socket
200  error = sntpClientOpenConnection(context);
201 
202  //Check status code
203  if(!error)
204  {
205  //Save current time
206  context->startTime = osGetSystemTime();
207  //Initialize retransmission timeout
209 
210  //Send the request to the designated NTP server
211  context->state = SNTP_CLIENT_STATE_SENDING;
212  }
213  }
214  else if(context->state == SNTP_CLIENT_STATE_SENDING)
215  {
216  //Send the request to the designated NTP server
217  error = sntpClientSendRequest(context);
218  }
219  else if(context->state == SNTP_CLIENT_STATE_RECEIVING)
220  {
221  //Wait for server's response
222  error = sntpClientReceiveResponse(context);
223  }
224  else if(context->state == SNTP_CLIENT_STATE_COMPLETE)
225  {
226  //Extract NTP timestamp from server's response
227  error = sntpClientParseResponse(context, timestamp);
228  //We are done
229  break;
230  }
231  else
232  {
233  //Invalid state
234  error = ERROR_WRONG_STATE;
235  }
236  }
237 
238  //Check status code
239  if(error != ERROR_WOULD_BLOCK)
240  {
241  //Close UDP socket
242  sntpClientCloseConnection(context);
243  //Revert to default state
244  context->state = SNTP_CLIENT_STATE_INIT;
245  }
246 
247  //Return status code
248  return error;
249 }
250 
251 
252 /**
253  * @brief Retrieve the kiss code from a Kiss-of-Death message
254  * @param[in] context Pointer to the SNTP client context
255  * @return Kiss code
256  **/
257 
259 {
260  uint32_t kissCode;
261 
262  //Make sure the SNTP client context is valid
263  if(context != NULL)
264  {
265  //Get kiss code
266  kissCode = context->kissCode;
267  }
268  else
269  {
270  //The SNTP client context is not valid
271  kissCode = 0;
272  }
273 
274  //Return kiss code
275  return kissCode;
276 }
277 
278 
279 /**
280  * @brief Release SNTP client context
281  * @param[in] context Pointer to the SNTP client context
282  **/
283 
285 {
286  //Make sure the SNTP client context is valid
287  if(context != NULL)
288  {
289  //Close UDP socket
290  sntpClientCloseConnection(context);
291 
292  //Clear SNTP client context
293  osMemset(context, 0, sizeof(SntpClientContext));
294  }
295 }
296 
297 #endif
error_t sntpClientInit(SntpClientContext *context)
Initialize SNTP client context.
Definition: sntp_client.c:55
@ SNTP_CLIENT_STATE_COMPLETE
Definition: sntp_client.h:86
@ ERROR_WOULD_BLOCK
Definition: error.h:96
IP network address.
Definition: ip.h:90
NtpTimestamp
Definition: ntp_common.h:179
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:258
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:99
systime_t startTime
Request start time.
Definition: sntp_client.h:103
SNTP client context.
Definition: sntp_client.h:95
#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:96
error_t
Error codes.
Definition: error.h:43
#define SNTP_CLIENT_DEFAULT_TIMEOUT
Definition: sntp_client.h:47
#define NetInterface
Definition: net.h:36
@ SNTP_CLIENT_STATE_RECEIVING
Definition: sntp_client.h:85
error_t sntpClientBindToInterface(SntpClientContext *context, NetInterface *interface)
Bind the SNTP client to a particular network interface.
Definition: sntp_client.c:130
@ NTP_VERSION_3
Definition: ntp_common.h:74
error_t sntpClientGetTimestamp(SntpClientContext *context, NtpTimestamp *timestamp)
Retrieve current time from NTP server.
Definition: sntp_client.c:181
void sntpClientDeinit(SntpClientContext *context)
Release SNTP client context.
Definition: sntp_client.c:284
error_t sntpClientReceiveResponse(SntpClientContext *context)
Wait for NTP server's response.
systime_t timeout
Timeout value.
Definition: sntp_client.h:101
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:83
error_t sntpClientSendRequest(SntpClientContext *context)
Send request to the NTP server.
uint32_t kissCode
Kiss code.
Definition: sntp_client.h:108
@ SNTP_CLIENT_STATE_SENDING
Definition: sntp_client.h:84
error_t sntpClientSetServerAddr(SntpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the IP address of the NTP server.
Definition: sntp_client.c:153
NtpVersion
NTP version numbers.
Definition: ntp_common.h:71
NtpVersion version
NTP protocol version.
Definition: sntp_client.h:97
systime_t retransmitTimeout
Retransmission timeout.
Definition: sntp_client.h:105
error_t sntpClientSetVersion(SntpClientContext *context, NtpVersion version)
Set the NTP protocol version to be used.
Definition: sntp_client.c:84
NetInterface * interface
Underlying network interface.
Definition: sntp_client.h:98
#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:75
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint16_t serverPort
NTP server port.
Definition: sntp_client.h:100
systime_t osGetSystemTime(void)
Retrieve system time.
error_t sntpClientSetTimeout(SntpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: sntp_client.c:109
Helper functions for SNTP client.