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.4
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  //We are done
202  break;
203  }
204  else
205  {
206  //Invalid state
207  error = ERROR_WRONG_STATE;
208  }
209  }
210 
211  //Check status code
212  if(error != ERROR_WOULD_BLOCK)
213  {
214  //Close UDP socket
215  sntpClientCloseConnection(context);
216  //Revert to default state
217  context->state = SNTP_CLIENT_STATE_INIT;
218  }
219 
220  //Return status code
221  return error;
222 }
223 
224 
225 /**
226  * @brief Retrieve the kiss code from a Kiss-of-Death message
227  * @param[in] context Pointer to the SNTP client context
228  * @return Kiss code
229  **/
230 
232 {
233  uint32_t kissCode;
234 
235  //Make sure the SNTP client context is valid
236  if(context != NULL)
237  {
238  //Get kiss code
239  kissCode = context->kissCode;
240  }
241  else
242  {
243  //The SNTP client context is not valid
244  kissCode = 0;
245  }
246 
247  //Return kiss code
248  return kissCode;
249 }
250 
251 
252 /**
253  * @brief Release SNTP client context
254  * @param[in] context Pointer to the SNTP client context
255  **/
256 
258 {
259  //Make sure the SNTP client context is valid
260  if(context != NULL)
261  {
262  //Close UDP socket
263  sntpClientCloseConnection(context);
264 
265  //Clear SNTP client context
266  osMemset(context, 0, sizeof(SntpClientContext));
267  }
268 }
269 
270 #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:231
error_t sntpClientOpenConnection(SntpClientContext *context)
Open UDP connection.
@ ERROR_WRONG_STATE
Definition: error.h:209
IpAddr serverIpAddr
NTP server address.
Definition: sntp_client.h:98
systime_t startTime
Request start time.
Definition: sntp_client.h:102
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:103
error_t sntpClientGetTimestamp(SntpClientContext *context, NtpTimestamp *timestamp)
Retrieve current time from NTP server.
Definition: sntp_client.c:154
void sntpClientDeinit(SntpClientContext *context)
Release SNTP client context.
Definition: sntp_client.c:257
error_t sntpClientReceiveResponse(SntpClientContext *context)
Wait for NTP server's response.
systime_t timeout
Timeout value.
Definition: sntp_client.h:100
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:107
@ 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:126
systime_t retransmitTimeout
Retransmission timeout.
Definition: sntp_client.h:104
NetInterface * interface
Underlying network interface.
Definition: sntp_client.h:97
#define osMemset(p, value, length)
Definition: os_port.h:135
TCP/IP stack core.
SNTP client (Simple Network Time Protocol)
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint16_t serverPort
NTP server port.
Definition: sntp_client.h:99
systime_t osGetSystemTime(void)
Retrieve system time.
error_t sntpClientSetTimeout(SntpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: sntp_client.c:82
Helper functions for SNTP client.