sntp_client.h File Reference

SNTP client (Simple Network Time Protocol) More...

#include "core/net.h"
#include "core/socket.h"

Go to the source code of this file.

Data Structures

struct  SntpClientContext
 SNTP client context. More...
 

Macros

#define SNTP_CLIENT_SUPPORT   ENABLED
 
#define SNTP_CLIENT_MAX_RETRIES   3
 
#define SNTP_CLIENT_INIT_TIMEOUT   1000
 
#define SNTP_CLIENT_MAX_TIMEOUT   5000
 
#define NTP_PORT   123
 
#define NTP_MESSAGE_MAX_SIZE   68
 

Typedefs

struct {
   uint32_t   seconds
 
   uint32_t   fraction
 
NtpTimestamp
 Time representation. More...
 
struct {
   uint8_t   mode: 3
 
   uint8_t   vn: 3
 
   uint8_t   li: 2
 
   uint8_t   stratum
 
   uint8_t   poll
 
   int8_t   precision
 
   uint32_t   rootDelay
 
   uint32_t   rootDispersion
 
   uint32_t   referenceIdentifier
 
   NtpTimestamp   referenceTimestamp
 
   NtpTimestamp   originateTimestamp
 
   NtpTimestamp   receiveTimestamp
 
   NtpTimestamp   transmitTimestamp
 
NtpHeader
 NTP packet header. More...
 
struct {
   uint32_t   keyIdentifier
 
   uint8_t   messageDigest [16]
 
NtpAuthData
 Authentication data. More...
 

Enumerations

enum  NtpLeapIndicator { NTP_LI_NO_WARNING = 0, NTP_LI_LAST_MIN_HAS_61_SECS = 1, NTP_LI_LAST_MIN_HAS_59_SECS = 2, NTP_LI_ALARM_CONDITION = 3 }
 Leap indicator. More...
 
enum  NtpVersion { NTP_VERSION_1 = 1, NTP_VERSION_2 = 2, NTP_VERSION_3 = 3, NTP_VERSION_4 = 4 }
 NTP version number. More...
 
enum  NtpMode {
  NTP_MODE_SYMMETRIC_ACTIVE = 1, NTP_MODE_SYMMETRIC_PASSIVE = 2, NTP_MODE_CLIENT = 3, NTP_MODE_SERVER = 4,
  NTP_MODE_BROADCAST = 5
}
 Protocol mode. More...
 

Functions

error_t sntpClientGetTimestamp (NetInterface *interface, const IpAddr *serverIpAddr, NtpTimestamp *timestamp)
 Retrieve current time from NTP server using SNTP protocol. More...
 
error_t sntpSendRequest (SntpClientContext *context)
 Send NTP request using SNTP protocol. More...
 
error_t sntpWaitForResponse (SntpClientContext *context, systime_t timeout)
 Wait for a valid response from the NTP server. More...
 
error_t sntpParseResponse (SntpClientContext *context, const NtpHeader *message, size_t length)
 Parse NTP server response. More...
 
void sntpDumpMessage (const NtpHeader *message, size_t length)
 Dump NTP message for debugging purpose. More...
 
void sntpDumpTimestamp (const NtpTimestamp *timestamp)
 Dump NTP timestamp. More...
 

Detailed Description

SNTP client (Simple Network Time Protocol)

License

Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
1.9.0

Definition in file sntp_client.h.

Macro Definition Documentation

◆ NTP_MESSAGE_MAX_SIZE

#define NTP_MESSAGE_MAX_SIZE   68

Definition at line 67 of file sntp_client.h.

◆ NTP_PORT

#define NTP_PORT   123

Definition at line 65 of file sntp_client.h.

◆ SNTP_CLIENT_INIT_TIMEOUT

#define SNTP_CLIENT_INIT_TIMEOUT   1000

Definition at line 52 of file sntp_client.h.

◆ SNTP_CLIENT_MAX_RETRIES

#define SNTP_CLIENT_MAX_RETRIES   3

Definition at line 45 of file sntp_client.h.

◆ SNTP_CLIENT_MAX_TIMEOUT

#define SNTP_CLIENT_MAX_TIMEOUT   5000

Definition at line 59 of file sntp_client.h.

◆ SNTP_CLIENT_SUPPORT

#define SNTP_CLIENT_SUPPORT   ENABLED

Definition at line 38 of file sntp_client.h.

Typedef Documentation

◆ NtpAuthData

typedef { ... } NtpAuthData

Authentication data.

◆ NtpHeader

typedef { ... } NtpHeader

NTP packet header.

◆ NtpTimestamp

typedef { ... } NtpTimestamp

Time representation.

Enumeration Type Documentation

◆ NtpLeapIndicator

Leap indicator.

Enumerator
NTP_LI_NO_WARNING 
NTP_LI_LAST_MIN_HAS_61_SECS 
NTP_LI_LAST_MIN_HAS_59_SECS 
NTP_LI_ALARM_CONDITION 

Definition at line 79 of file sntp_client.h.

◆ NtpMode

enum NtpMode

Protocol mode.

Enumerator
NTP_MODE_SYMMETRIC_ACTIVE 
NTP_MODE_SYMMETRIC_PASSIVE 
NTP_MODE_CLIENT 
NTP_MODE_SERVER 
NTP_MODE_BROADCAST 

Definition at line 105 of file sntp_client.h.

◆ NtpVersion

enum NtpVersion

NTP version number.

Enumerator
NTP_VERSION_1 
NTP_VERSION_2 
NTP_VERSION_3 
NTP_VERSION_4 

Definition at line 92 of file sntp_client.h.

Function Documentation

◆ sntpClientGetTimestamp()

error_t sntpClientGetTimestamp ( NetInterface interface,
const IpAddr serverIpAddr,
NtpTimestamp timestamp 
)

Retrieve current time from NTP server using SNTP protocol.

Parameters
[in]interfaceUnderlying network interface (optional parameter)
[in]serverIpAddrIP address of the NTP server
[out]timestampCurrent time
Returns
Error code

Definition at line 55 of file sntp_client.c.

◆ sntpDumpMessage()

void sntpDumpMessage ( const NtpHeader message,
size_t  length 
)

Dump NTP message for debugging purpose.

Parameters
[in]messagePointer to the NTP message
[in]lengthLength of the NTP message

Definition at line 280 of file sntp_client.c.

◆ sntpDumpTimestamp()

void sntpDumpTimestamp ( const NtpTimestamp timestamp)

Dump NTP timestamp.

Parameters
[in]timestampPointer to the NTP timestamp

Definition at line 345 of file sntp_client.c.

◆ sntpParseResponse()

error_t sntpParseResponse ( SntpClientContext context,
const NtpHeader message,
size_t  length 
)

Parse NTP server response.

Parameters
[in]contextPointer to the SNTP client context
[in]messageNTP response message to parse
[in]lengthLength of the incoming NTP message
Returns
Error code

Definition at line 238 of file sntp_client.c.

◆ sntpSendRequest()

error_t sntpSendRequest ( SntpClientContext context)

Send NTP request using SNTP protocol.

Parameters
[in]contextSNTP client context
Returns
Error code

Definition at line 142 of file sntp_client.c.

◆ sntpWaitForResponse()

error_t sntpWaitForResponse ( SntpClientContext context,
systime_t  timeout 
)

Wait for a valid response from the NTP server.

Parameters
[in]contextPointer to the SNTP client context
[in]timeoutMaximum time period to wait
Returns
Error code

Definition at line 186 of file sntp_client.c.

Variable Documentation

◆ fraction

uint32_t fraction

Definition at line 128 of file sntp_client.h.

◆ keyIdentifier

uint32_t keyIdentifier

Definition at line 166 of file sntp_client.h.

◆ li

uint8_t li

Definition at line 145 of file sntp_client.h.

◆ messageDigest

uint8_t messageDigest[16]

Definition at line 167 of file sntp_client.h.

◆ mode

uint8_t mode

Definition at line 143 of file sntp_client.h.

◆ originateTimestamp

NtpTimestamp originateTimestamp

Definition at line 154 of file sntp_client.h.

◆ poll

uint8_t poll

Definition at line 148 of file sntp_client.h.

◆ precision

int8_t precision

Definition at line 149 of file sntp_client.h.

◆ receiveTimestamp

NtpTimestamp receiveTimestamp

Definition at line 155 of file sntp_client.h.

◆ referenceIdentifier

uint32_t referenceIdentifier

Definition at line 152 of file sntp_client.h.

◆ referenceTimestamp

NtpTimestamp referenceTimestamp

Definition at line 153 of file sntp_client.h.

◆ rootDelay

uint32_t rootDelay

Definition at line 150 of file sntp_client.h.

◆ rootDispersion

uint32_t rootDispersion

Definition at line 151 of file sntp_client.h.

◆ seconds

uint32_t seconds

Definition at line 127 of file sntp_client.h.

◆ stratum

uint8_t stratum

Definition at line 147 of file sntp_client.h.

◆ transmitTimestamp

NtpTimestamp transmitTimestamp

Definition at line 156 of file sntp_client.h.

◆ vn

uint8_t vn

Definition at line 144 of file sntp_client.h.