net: config: use dhcpv4 option for sntp
Allow the use of the NTP server address, set by dhcpv4 option, by the net_init_clock_via_sntp function. Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
This commit is contained in:
parent
3be6557078
commit
bcf90edd1f
|
@ -234,6 +234,14 @@ config NET_CONFIG_SNTP_INIT_SERVER
|
|||
e.g. server information at
|
||||
https://support.ntp.org/bin/view/Servers/NTPPoolServers
|
||||
|
||||
config NET_CONFIG_SNTP_INIT_SERVER_USE_DHCPV4_OPTION
|
||||
bool "SNTP server to use for system clock init is set using DHCPv4 option"
|
||||
default y
|
||||
depends on NET_DHCPV4_OPTION_NTP_SERVER
|
||||
help
|
||||
If this option is set, then the SNTP server to use for system
|
||||
clock init can be set using DHCPv4 option.
|
||||
|
||||
config NET_CONFIG_SNTP_INIT_TIMEOUT
|
||||
int "SNTP timeout to init system clock (ms)"
|
||||
default 3000
|
||||
|
|
|
@ -11,12 +11,30 @@ LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
|
|||
#include <zephyr/net/sntp.h>
|
||||
#include <zephyr/posix/time.h>
|
||||
|
||||
static int sntp_init_helper(struct sntp_time *tm)
|
||||
{
|
||||
#ifdef CONFIG_NET_CONFIG_SNTP_INIT_SERVER_USE_DHCPV4_OPTION
|
||||
struct net_if *iface = net_if_get_default();
|
||||
|
||||
if (!net_ipv4_is_addr_unspecified(&iface->config.dhcpv4.ntp_addr)) {
|
||||
struct sockaddr_in sntp_addr = {0};
|
||||
|
||||
sntp_addr.sin_family = AF_INET;
|
||||
sntp_addr.sin_addr.s_addr = iface->config.dhcpv4.ntp_addr.s_addr;
|
||||
return sntp_simple_addr((struct sockaddr *)&sntp_addr, sizeof(sntp_addr),
|
||||
CONFIG_NET_CONFIG_SNTP_INIT_TIMEOUT, tm);
|
||||
}
|
||||
LOG_INF("SNTP address not set by DHCPv4, using Kconfig defaults");
|
||||
#endif /* NET_CONFIG_SNTP_INIT_SERVER_USE_DHCPV4_OPTION */
|
||||
return sntp_simple(CONFIG_NET_CONFIG_SNTP_INIT_SERVER,
|
||||
CONFIG_NET_CONFIG_SNTP_INIT_TIMEOUT, tm);
|
||||
}
|
||||
|
||||
int net_init_clock_via_sntp(void)
|
||||
{
|
||||
struct sntp_time ts;
|
||||
struct timespec tspec;
|
||||
int res = sntp_simple(CONFIG_NET_CONFIG_SNTP_INIT_SERVER,
|
||||
CONFIG_NET_CONFIG_SNTP_INIT_TIMEOUT, &ts);
|
||||
int res = sntp_init_helper(&ts);
|
||||
|
||||
if (res < 0) {
|
||||
LOG_ERR("Cannot set time using SNTP");
|
||||
|
|
Loading…
Reference in a new issue