Network Component  Version 6.6
MDK-Professional Middleware for IP Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SNTP Client

SNTP Client routines deliver precise timing information over an IP network. More...

Functions

netStatus sntp_get_time (const uint8_t *ip_addr, net_sntp_client_cb_t cb_func)
 Determine current time from NTP or SNTP time server.
 
typedef void(* net_sntp_client_cb_t )(uint32_t utc_time)
 SNTP Client callback function.
 

Description

SNTP Client routines deliver precise timing information over an IP network.

Simple Network Time Protocol (SNTP) is a networking protocol for clock synchronization between computer systems over IP based, variable-latency data networks. SNTP does not require the storage of the state over extended periods of time, so that it is used mostly in memory limited embedded devices and in applications where high accuracy timing is not required.

Typedef Documentation

void(* net_sntp_client_cb_t)(uint32_t utc_time)

SNTP Client callback function.

Parameters
[in]utc_timeUTC time in seconds

Is the type definition for the SNTP callback function. Users must provide the function.

The function is called by the SNTP client when an NTP message is received from the server. The utc_time argument of the callback function signals the time, specified in UTC (Universal Time Coordinated) in seconds, elapsed since 1.1.1970. A value of 0 reports an error.

Parameter for:

Function Documentation

netStatus sntp_get_time ( const uint8_t *  ip_addr,
net_sntp_client_cb_t  cb_func 
)

Determine current time from NTP or SNTP time server.

Parameters
[in]ip_addrIP address of NTP or SNTP server.
[in]cb_funccallback function to call, when the session ends.
Returns
status code that indicates the execution status of the function as defined with netStatus.

In unicast mode, the function sntp_get_time sends a get time request to NTP server.

In broadcast mode, it opens an UDP socket for receiving broadcast NTP messages. You can use broadcast mode, if you have a broadcasting NTP server in your local network.

The argument ip_addr specifies the IP address of a local NTP server. NTP messages that come from the specified NTP server are accepted. Messages that come from a different NTP server are ignored. If ip_addr is 0.0.0.0, then NTP messages from any NTP server are accepted.

If the IP address is not specified (ip_addr is NULL), then the IP address of the NTP Server configured in Net_Config_SNTP_Client.h is used. Broadcast Mode gets also enabled here.

The argument cb_func points to a user-defined callback function called by the SNTP client when an NTP message is received from the server. Refer to net_sntp_client_cb_t.

Code Example

static void time_cback (uint32_t time);
void get_time (void) {
const uint8_t ntp_server[4] = {217,79,179,106};
if (sntp_get_time (&ntp_server[0], time_cback) == netOK) {
printf ("SNTP request sent.\n");
}
else {
printf ("Failed, SNTP not ready or bad parameters.\n");
}
}
static void time_cback (uint32_t time) {
if (time == 0) {
printf ("Error, server not responding or bad response.\n");
}
else {
printf ("%d seconds elapsed since 1.1.1970\n", time);
}
}