1 /*
2  * Copyright (c) 2019 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
9 
10 #include <errno.h>
11 #include <zephyr/net/sntp.h>
12 #include <zephyr/posix/time.h>
13 
net_init_clock_via_sntp(void)14 int net_init_clock_via_sntp(void)
15 {
16 	struct sntp_time ts;
17 	struct timespec tspec;
18 	int res = sntp_simple(CONFIG_NET_CONFIG_SNTP_INIT_SERVER,
19 			      CONFIG_NET_CONFIG_SNTP_INIT_TIMEOUT, &ts);
20 
21 	if (res < 0) {
22 		LOG_ERR("Cannot set time using SNTP");
23 		return res;
24 	}
25 
26 	tspec.tv_sec = ts.seconds;
27 	tspec.tv_nsec = ((uint64_t)ts.fraction * (1000 * 1000 * 1000)) >> 32;
28 	res = clock_settime(CLOCK_REALTIME, &tspec);
29 
30 	return 0;
31 }
32