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-2020 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 1.9.8
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 //C++ guard
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 
72 /**
73  * @brief SNTP client states
74  **/
75 
76 typedef enum
77 {
83 
84 
85 /**
86  * @brief SNTP client context
87  **/
88 
89 typedef struct
90 {
91  SntpClientState state; ///<SNTP client state
92  NetInterface *interface; ///<Underlying network interface
93  IpAddr serverIpAddr; ///<NTP server address
94  uint16_t serverPort; ///<NTP server port
95  systime_t timeout; ///<Timeout value
96  Socket *socket; ///<Underlying socket
97  systime_t startTime; ///<Request start time
98  systime_t retransmitStartTime; ///<Time at which the last request was sent
99  systime_t retransmitTimeout; ///<Retransmission timeout
100  uint8_t message[NTP_MAX_MSG_SIZE]; ///<Buffer that holds the NTP request/response
101  size_t messageLen; ///<Length of the NTP message, in bytes
102  uint32_t kissCode; ///<Kiss code
104 
105 
106 //SNTP client related functions
108 
110 
112  NetInterface *interface);
113 
115  const IpAddr *serverIpAddr, uint16_t serverPort);
116 
118  NtpTimestamp *timestamp);
119 
120 uint32_t sntpClientGetKissCode(SntpClientContext *context);
121 
122 void sntpClientDeinit(SntpClientContext *context);
123 
124 //C++ guard
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif
size_t messageLen
Length of the NTP message, in bytes.
Definition: sntp_client.h:101
error_t sntpClientSetTimeout(SntpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: sntp_client.c:82
IP network address.
Definition: ip.h:78
systime_t retransmitStartTime
Time at which the last request was sent.
Definition: sntp_client.h:98
error_t sntpClientInit(SntpClientContext *context)
Initialize SNTP client context.
Definition: sntp_client.c:55
Socket * socket
Underlying socket.
Definition: sntp_client.h:96
IpAddr serverIpAddr
NTP server address.
Definition: sntp_client.h:93
systime_t startTime
Request start time.
Definition: sntp_client.h:97
SNTP client context.
Definition: sntp_client.h:89
__start_packed struct @190 NtpTimestamp
NTP timestamp representation.
uint32_t sntpClientGetKissCode(SntpClientContext *context)
Retrieve the kiss code from a Kiss-of-Death message.
Definition: sntp_client.c:233
SntpClientState state
SNTP client state.
Definition: sntp_client.h:91
error_t
Error codes.
Definition: error.h:42
error_t sntpClientBindToInterface(SntpClientContext *context, NetInterface *interface)
Bind the SNTP client to a particular network interface.
Definition: sntp_client.c:103
#define NetInterface
Definition: net.h:36
#define NTP_MAX_MSG_SIZE
Definition: ntp_common.h:40
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 timeout
Timeout value.
Definition: sntp_client.h:95
error_t sntpClientGetTimestamp(SntpClientContext *context, NtpTimestamp *timestamp)
Retrieve current time from NTP server.
Definition: sntp_client.c:154
uint32_t kissCode
Kiss code.
Definition: sntp_client.h:102
#define Socket
Definition: socket.h:36
SntpClientState
SNTP client states.
Definition: sntp_client.h:76
uint8_t message[]
Definition: chap.h:152
Definitions common to NTP client and server.
systime_t retransmitTimeout
Retransmission timeout.
Definition: sntp_client.h:99
NetInterface * interface
Underlying network interface.
Definition: sntp_client.h:92
TCP/IP stack core.
uint32_t systime_t
Definition: compiler_port.h:46
void sntpClientDeinit(SntpClientContext *context)
Release SNTP client context.
Definition: sntp_client.c:259
uint16_t serverPort
NTP server port.
Definition: sntp_client.h:94