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-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  * @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.4.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 timeout
69 
70  //Successful initialization
71  return NO_ERROR;
72 }
73 
74 
75 /**
76  * @brief Set communication timeout
77  * @param[in] context Pointer to the SNTP client context
78  * @param[in] timeout Timeout value, in milliseconds
79  * @return Error code
80  **/
81 
83 {
84  //Make sure the SNTP client context is valid
85  if(context == NULL)
87 
88  //Save timeout value
89  context->timeout = timeout;
90 
91  //Successful processing
92  return NO_ERROR;
93 }
94 
95 
96 /**
97  * @brief Bind the SNTP client to a particular network interface
98  * @param[in] context Pointer to the SNTP client context
99  * @param[in] interface Network interface to be used
100  * @return Error code
101  **/
102 
104  NetInterface *interface)
105 {
106  //Make sure the SNTP client context is valid
107  if(context == NULL)
109 
110  //Explicitly associate the SNTP client with the specified interface
111  context->interface = interface;
112 
113  //Successful processing
114  return NO_ERROR;
115 }
116 
117 
118 /**
119  * @brief Specify the IP address of the NTP server
120  * @param[in] context Pointer to the SNTP client context
121  * @param[in] serverIpAddr IP address of the NTP server
122  * @param[in] serverPort Port number
123  * @return Error code
124  **/
125 
127  const IpAddr *serverIpAddr, uint16_t serverPort)
128 {
129  //Check parameters
130  if(context == NULL || serverIpAddr == NULL)
132 
133  //Save the IP address and the port number of the NTP server
134  context->serverIpAddr = *serverIpAddr;
135  context->serverPort = serverPort;
136 
137  //Close UDP socket
138  sntpClientCloseConnection(context);
139  //Revert to default state
140  context->state = SNTP_CLIENT_STATE_INIT;
141 
142  //Successful processing
143  return NO_ERROR;
144 }
145 
146 
147 /**
148  * @brief Retrieve current time from NTP server
149  * @param[in] context Pointer to the SNTP client context
150  * @param[out] timestamp Pointer to the NTP timestamp
151  * @return Error code
152  **/
153 
155  NtpTimestamp *timestamp)
156 {
157  error_t error;
158 
159  //Check parameters
160  if(context == NULL || timestamp == NULL)
162 
163  //Initialize status code
164  error = NO_ERROR;
165 
166  //Send NTP request and wait for server's response
167  while(!error)
168  {
169  //Check current state
170  if(context->state == SNTP_CLIENT_STATE_INIT)
171  {
172  //Open UDP socket
173  error = sntpClientOpenConnection(context);
174 
175  //Check status code
176  if(!error)
177  {
178  //Save current time
179  context->startTime = osGetSystemTime();
180  //Initialize retransmission timeout
182 
183  //Send the request to the designated NTP server
184  context->state = SNTP_CLIENT_STATE_SENDING;
185  }
186  }
187  else if(context->state == SNTP_CLIENT_STATE_SENDING)
188  {
189  //Send the request to the designated NTP server
190  error = sntpClientSendRequest(context);
191  }
192  else if(context->state == SNTP_CLIENT_STATE_RECEIVING)
193  {
194  //Wait for server's response
195  error = sntpClientReceiveResponse(context);
196  }
197  else if(context->state == SNTP_CLIENT_STATE_COMPLETE)
198  {
199  //Extract NTP timestamp from server's response
200  error = sntpClientParseResponse(context, timestamp);
201 
202  //Revert to default state
203  context->state = SNTP_CLIENT_STATE_INIT;
204  break;
205  }
206  else
207  {
208  //Invalid state
209  error = ERROR_WRONG_STATE;
210  }
211  }
212 
213  //Check status code
214  if(error != ERROR_WOULD_BLOCK)
215  {
216  //Close UDP socket
217  sntpClientCloseConnection(context);
218  //Revert to default state
219  context->state = SNTP_CLIENT_STATE_INIT;
220  }
221 
222  //Return status code
223  return error;
224 }
225 
226 
227 /**
228  * @brief Retrieve the kiss code from a Kiss-of-Death message
229  * @param[in] context Pointer to the SNTP client context
230  * @return Kiss code
231  **/
232 
234 {
235  uint32_t kissCode;
236 
237  //Make sure the SNTP client context is valid
238  if(context != NULL)
239  {
240  //Get kiss code
241  kissCode = context->kissCode;
242  }
243  else
244  {
245  //The SNTP client context is not valid
246  kissCode = 0;
247  }
248 
249  //Return kiss code
250  return kissCode;
251 }
252 
253 
254 /**
255  * @brief Release SNTP client context
256  * @param[in] context Pointer to the SNTP client context
257  **/
258 
260 {
261  //Make sure the SNTP client context is valid
262  if(context != NULL)
263  {
264  //Close UDP socket
265  sntpClientCloseConnection(context);
266 
267  //Clear SNTP client context
268  osMemset(context, 0, sizeof(SntpClientContext));
269  }
270 }
271 
272 #endif
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ ERROR_WOULD_BLOCK
Definition: error.h:96
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_WRONG_STATE
Definition: error.h:209
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
NtpTimestamp
Definition: ntp_common.h:137
#define osMemset(p, value, length)
Definition: os_port.h:135
systime_t osGetSystemTime(void)
Retrieve system time.
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
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
SNTP client (Simple Network Time Protocol)
#define SNTP_CLIENT_INIT_RETRANSMIT_TIMEOUT
Definition: sntp_client.h:54
#define SNTP_CLIENT_DEFAULT_TIMEOUT
Definition: sntp_client.h:47
@ 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
error_t sntpClientReceiveResponse(SntpClientContext *context)
Wait for NTP server's response.
error_t sntpClientSendRequest(SntpClientContext *context)
Send request to the NTP server.
error_t sntpClientOpenConnection(SntpClientContext *context)
Open UDP connection.
error_t sntpClientParseResponse(SntpClientContext *context, NtpTimestamp *timestamp)
Parse NTP server's response.
void sntpClientCloseConnection(SntpClientContext *context)
Close UDP connection.
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
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 startTime
Request start time.
Definition: sntp_client.h:102
NetInterface * interface
Underlying network interface.
Definition: sntp_client.h:97