zephyr/subsys/net/lib/config/init_clock_sntp.c
Gerard Marull-Paretas 5113c1418d subsystems: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 12:07:35 +02:00

32 lines
689 B
C

/*
* Copyright (c) 2019 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
#include <errno.h>
#include <zephyr/net/sntp.h>
#include <zephyr/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 = ((uint64_t)ts.fraction * (1000 * 1000 * 1000)) >> 32;
res = clock_settime(CLOCK_REALTIME, &tspec);
return 0;
}