Implementation of the client API of the coreSNTP library. More...
Macros | |
#define | FRACTIONS_TO_MS(fractions) ( fractions / ( SNTP_FRACTION_VALUE_PER_MICROSECOND * 1000U ) ) |
Utility to convert fractions part of SNTP timestamp to milliseconds. More... | |
Functions | |
SntpStatus_t | Sntp_Init (SntpContext_t *pContext, const SntpServerInfo_t *pTimeServers, size_t numOfServers, uint32_t serverResponseTimeoutMs, uint8_t *pNetworkBuffer, size_t bufferSize, SntpResolveDns_t resolveDnsFunc, SntpGetTime_t getSystemTimeFunc, SntpSetTime_t setSystemTimeFunc, const UdpTransportInterface_t *pTransportIntf, const SntpAuthenticationInterface_t *pAuthIntf) |
Initializes a context for SNTP client communication with SNTP/NTP servers. More... | |
static uint64_t | calculateElapsedTimeMs (const SntpTimestamp_t *pCurrentTime, const SntpTimestamp_t *pOlderTime) |
Utility to calculate the difference in milliseconds between 2 SNTP timestamps. More... | |
static SntpStatus_t | validateContext (const SntpContext_t *pContext) |
Validates the content of the SNTP context passed to the APIs to check whether it represents an initialized context. More... | |
static SntpStatus_t | sendSntpPacket (const UdpTransportInterface_t *pNetworkIntf, uint32_t timeServer, uint16_t serverPort, SntpGetTime_t getTimeFunc, const uint8_t *pPacket, uint16_t packetSize, uint32_t timeoutMs) |
Sends SNTP request packet to the passed server over the network using transport interface's send function. More... | |
static SntpStatus_t | addClientAuthentication (SntpContext_t *pContext) |
Adds client authentication data to SNTP request packet by calling the authentication interface. More... | |
SntpStatus_t | Sntp_SendTimeRequest (SntpContext_t *pContext, uint32_t randomNumber, uint32_t blockTimeMs) |
Sends a request for time from the currently configured server (in the context). If the user has provided an authentication interface, the client authentication code is appended to the request before sending over the network by calling the SntpGenerateAuthCode_t function of the SntpAuthenticationInterface_t. More... | |
static void | rotateServerForNextTimeQuery (SntpContext_t *pContext) |
Utility to update the SNTP context to rotate the server of use for subsequent time request(s). More... | |
static SntpStatus_t | receiveSntpResponse (const UdpTransportInterface_t *pTransportIntf, uint32_t timeServer, uint16_t serverPort, uint8_t *pBuffer, uint16_t responseSize) |
This function attempts to receive the SNTP response packet from a server. More... | |
static SntpStatus_t | processServerResponse (SntpContext_t *pContext, const SntpTimestamp_t *pResponseRxTime) |
Processes the response from a server by de-serializing the SNTP packet to validate the server (if an authentication interface has been configured), determine whether server has accepted or rejected the time request, and update the system clock if the server responded positively with time. More... | |
static bool | decideAboutReadRetry (const SntpTimestamp_t *pCurrentTime, const SntpTimestamp_t *pReadStartTime, const SntpTimestamp_t *pRequestTime, uint32_t responseTimeoutMs, uint32_t blockTimeMs, bool *pHasResponseTimedOut) |
Determines whether a retry attempt should be made to receive server response packet from the network depending on the timing constraints of server response timeout, responseTimeoutMs , and the block time period, blockTimeMs , passed. If neither of the time windows have expired, the function determines that the read operation can be re-tried. More... | |
SntpStatus_t | Sntp_ReceiveTimeResponse (SntpContext_t *pContext, uint32_t blockTimeMs) |
Receives a time response from the server that has been requested for time with the Sntp_SendTimeRequest API function. Once an accepted response containing time from server is received, this function calls the user-defined SntpSetTime_t function to update the system time. More... | |
const char * | Sntp_StatusToStr (SntpStatus_t status) |
Converts SntpStatus_t to its equivalent string. More... | |
Implementation of the client API of the coreSNTP library.
#define FRACTIONS_TO_MS | ( | fractions | ) | ( fractions / ( SNTP_FRACTION_VALUE_PER_MICROSECOND * 1000U ) ) |
Utility to convert fractions part of SNTP timestamp to milliseconds.
[in] | fractions | The fractions value in an SNTP timestamp. |
SntpStatus_t Sntp_Init | ( | SntpContext_t * | pContext, |
const SntpServerInfo_t * | pTimeServers, | ||
size_t | numOfServers, | ||
uint32_t | serverResponseTimeoutMs, | ||
uint8_t * | pNetworkBuffer, | ||
size_t | bufferSize, | ||
SntpResolveDns_t | resolveDnsFunc, | ||
SntpGetTime_t | getSystemTimeFunc, | ||
SntpSetTime_t | setSystemTimeFunc, | ||
const UdpTransportInterface_t * | pTransportIntf, | ||
const SntpAuthenticationInterface_t * | pAuthIntf | ||
) |
Initializes a context for SNTP client communication with SNTP/NTP servers.
[out] | pContext | The user-supplied memory for the context that will be initialized to represent an SNTP client. |
[in] | pTimeServers | The list of decreasing order of priority of time servers that should be used by the SNTP client. This list MUST stay in scope for all the time of use of the context. |
[in] | numOfServers | The number of servers in the list, pTimeServers . |
[in] | serverResponseTimeoutMs | The timeout duration (in milliseconds) for receiving server response for time requests. The same timeout value is used for each server in the pTimeServers list. |
[in] | pNetworkBuffer | The user-supplied memory that will be used for storing network data for SNTP client-server communication. The buffer MUST stay in scope for all the time of use of the context. |
[in] | bufferSize | The size of the passed buffer pNetworkBuffer . The buffer SHOULD be appropriately sized for storing an entire SNTP packet which includes both SNTP_PACKET_BASE_SIZE bytes of standard SNTP packet size, and space for authentication data, if security mechanism is used to communicate with any of the time servers configured for use. |
[in] | resolveDnsFunc | The user-defined function for DNS resolution of time server. |
[in] | getSystemTimeFunc | The user-defined function for querying system time. |
[in] | setSystemTimeFunc | The user-defined function for correcting system time for every successful time response received from a server. |
[in] | pTransportIntf | The user-defined function for performing network send/recv operations over UDP. |
[in] | pAuthIntf | The user-defined interface for generating client authentication in SNTP requests and authenticating servers in SNTP responses, if security mechanism is used in SNTP communication with server(s). If security mechanism is not used in communication with any of the configured servers (in pTimeServers ), then the SntpAuthenticationInterface_t does not need to be defined and this parameter can be NULL. |
|
static |
Utility to calculate the difference in milliseconds between 2 SNTP timestamps.
[in] | pCurrentTime | The more recent timestamp. |
[in] | pOlderTime | The older timestamp. |
pCurrentTime
represents time in NTP era 1 (i.e. time since 7 Feb 2036) and the OlderTime
represents time in NTP era 0 (i.e. time since 1st Jan 1900).
|
static |
Validates the content of the SNTP context passed to the APIs to check whether it represents an initialized context.
[in] | pContext | The SNTP context to validate. |
|
static |
Sends SNTP request packet to the passed server over the network using transport interface's send function.
packetSize
have been sent. OR[in] | pNetworkIntf | The UDP transport interface to use for sending data over the network. |
[in] | timeServer | The IPv4 address of the server to send the SNTP request packet to. |
[in] | serverPort | The port of the timeServer to send the request to. |
[in] | getTimeFunc | The function to query system time for tracking retry time period of no data transmissions. |
[in] | pPacket | The buffer containing the SNTP packet data to send over the network. |
[in] | packetSize | The size of data in the SNTP request packet. |
[in] | timeoutMs | The timeout period for retry attempts of sending SNTP request packet over the network. |
timeoutMs
duration.
|
static |
Adds client authentication data to SNTP request packet by calling the authentication interface.
[in] | pContext | The SNTP context. |
SntpStatus_t Sntp_SendTimeRequest | ( | SntpContext_t * | pContext, |
uint32_t | randomNumber, | ||
uint32_t | blockTimeMs | ||
) |
Sends a request for time from the currently configured server (in the context). If the user has provided an authentication interface, the client authentication code is appended to the request before sending over the network by calling the SntpGenerateAuthCode_t function of the SntpAuthenticationInterface_t.
[in] | pContext | The context representing an SNTPv4 client. |
[in] | randomNumber | A random number serializing the SNTP request packet to protect against spoofing attacks by attackers that are off the network path of the SNTP client-server communication. This mechanism is suggested by SNTPv4 specification in RFC 4330 Section 3. |
[in] | blockTimeMs | The maximum duration of time (in milliseconds) the function will block on attempting to send time request to the server over the network. If a zero block time value is provided, then the function will attempt to send the packet ONLY once. |
blockTimeMs
duration.
|
static |
Utility to update the SNTP context to rotate the server of use for subsequent time request(s).
|
static |
This function attempts to receive the SNTP response packet from a server.
[in] | pTransportIntf | The UDP transport interface to use for receiving data from the network. |
[in] | timeServer | The server to read the response from the network. |
[in] | serverPort | The port of the server to read the response from. |
[in,out] | pBuffer | This will be filled with the server response read from the network. |
[in] | responseSize | The size of server response to read from the network. |
|
static |
Processes the response from a server by de-serializing the SNTP packet to validate the server (if an authentication interface has been configured), determine whether server has accepted or rejected the time request, and update the system clock if the server responded positively with time.
[in] | pContext | The SNTP context representing the SNTP client. |
[in] | pResponseRxTime | The time of receiving the server response from the network. |
|
static |
Determines whether a retry attempt should be made to receive server response packet from the network depending on the timing constraints of server response timeout, responseTimeoutMs
, and the block time period, blockTimeMs
, passed. If neither of the time windows have expired, the function determines that the read operation can be re-tried.
[in] | pCurrentTime | The current time in the system used for calculating elapsed time windows. |
[in] | pReadStartTime | The time of the first read attempt in the current set of read tries occurring from the Sntp_ReceiveTimeRequest API call by the application. This time is used for calculating the elapsed time to determine whether the block time has expired. |
[in] | pRequestTime | The time of sending the SNTP request to the server for which the response is awaited. This time is used for calculating total elapsed elapsed time of waiting for server response to determine if a server response timeout has occurred. |
[in] | responseTimeoutMs | The server response timeout configuration. |
[in] | blockTimeMs | The maximum block time of waiting for server response across read tries in the current call made by application to Sntp_ReceiveTimeResponse API. |
[out] | pHasResponseTimedOut | This will be populated with state to indicate whether the wait for server response has timed out. |
SntpStatus_t Sntp_ReceiveTimeResponse | ( | SntpContext_t * | pContext, |
uint32_t | blockTimeMs | ||
) |
Receives a time response from the server that has been requested for time with the Sntp_SendTimeRequest API function. Once an accepted response containing time from server is received, this function calls the user-defined SntpSetTime_t function to update the system time.
[in] | pContext | The context representing an SNTPv4 client. |
[in] | blockTimeMs | The maximum duration of time (in milliseconds) the function will block on receiving a response from the server unless either the response is received OR a response timeout occurs. |
const char * Sntp_StatusToStr | ( | SntpStatus_t | status | ) |
Converts SntpStatus_t to its equivalent string.
[in] | status | The status to convert to a string. |