net: lib: config: Add SYS_INIT handler to set clock from SNTP

With this feature enabled (via CONFIG_NET_CONFIG_CLOCK_SNTP_INIT), an
application will automagically get correct absolute time via POSIX
functions like time(), gettimeofday(), etc.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
Paul Sokolovsky 2019-09-12 16:56:08 +03:00 committed by Jukka Rissanen
parent b422e4a8ea
commit 106a0f7306
4 changed files with 72 additions and 0 deletions

View File

@ -18,3 +18,5 @@ if(CONFIG_NET_CONFIG_SETTINGS)
bt_settings.c
)
endif()
zephyr_library_sources_ifdef(CONFIG_NET_CONFIG_CLOCK_SNTP_INIT init_clock_sntp.c)

View File

@ -195,3 +195,37 @@ config NET_CONFIG_BT_NODE
endif # NET_L2_BT
endif # NET_CONFIG_SETTINGS
config NET_CONFIG_CLOCK_SNTP_INIT
bool "Initialize system clock using SNTP on application startup"
depends on SNTP && POSIX_CLOCK
help
Perform an SNTP request over networking to get and absolute
wall clock time, and initialize system time from it, so
functions like time(), gettimeofday(), etc. returned the
correct absolute time (no just time since system startup).
Requires networking.
if NET_CONFIG_CLOCK_SNTP_INIT
config NET_CONFIG_SNTP_INIT_PRIO
int "Startup priority to init clock using SNTP"
default 97
help
Should be higher than CONFIG_NET_CONFIG_INIT_PRIO.
config NET_CONFIG_SNTP_INIT_SERVER
string "SNTP server to use for system clock init"
default ""
help
Zephyr does not provide default setting for this option. Each
application and vendor should choose a suitable setting based
on their locality, needs, and server's terms of service. See
e.g. server information at
https://support.ntp.org/bin/view/Servers/NTPPoolServers
config NET_CONFIG_SNTP_INIT_TIMEOUT
int "SNTP timeout to init system clock (ms)"
default 3000
endif # NET_CONFIG_CLOCK_SNTP_INIT

View File

@ -29,6 +29,7 @@ LOG_MODULE_REGISTER(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
#include "bt_settings.h"
extern const struct log_backend *log_backend_net_get(void);
extern int net_init_clock_via_sntp(void);
static K_SEM_DEFINE(waiter, 0, 1);
static struct k_sem counter;
@ -380,6 +381,10 @@ static int init_app(struct device *device)
NET_ERR("Network initialization failed (%d)", ret);
}
if (IS_ENABLED(CONFIG_NET_CONFIG_CLOCK_SNTP_INIT)) {
net_init_clock_via_sntp();
}
/* This is activated late as it requires the network stack to be up
* and running before syslog messages can be sent to network.
*/

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2019 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <logging/log.h>
LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
#include <errno.h>
#include <net/sntp.h>
#include <posix/time.h>
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);
if (res < 0) {
LOG_ERR("Cannot set time using SNTP");
return res;
}
tspec.tv_sec = ts.seconds;
tspec.tv_nsec = ((u64_t)ts.fraction * (1000 * 1000 * 1000)) >> 32;
res = clock_settime(CLOCK_REALTIME, &tspec);
return 0;
}