From 8ab9956ae5b88bf7ff4f99eda14acdd46b136732 Mon Sep 17 00:00:00 2001 From: crasbe Date: Thu, 26 Jun 2025 15:49:29 +0200 Subject: [PATCH 1/2] sys/net/sntp: migrate from xtimer to ztimer --- sys/Makefile.dep | 7 ++----- sys/include/net/sntp.h | 11 ++++++++--- sys/net/application_layer/sntp/sntp.c | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sys/Makefile.dep b/sys/Makefile.dep index b15de27334..5ae9ae3abe 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -134,11 +134,8 @@ endif ifneq (,$(filter sntp,$(USEMODULE))) USEMODULE += sock_udp - USEMODULE += xtimer - ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - # requires 64bit ftimestamps - USEMODULE += ztimer64_xtimer_compat - endif + USEMODULE += ztimer64 + USEMODULE += ztimer64_usec endif ifneq (,$(filter sock_%,$(USEMODULE))) diff --git a/sys/include/net/sntp.h b/sys/include/net/sntp.h index 226756b3d2..e6fc1ce6bc 100644 --- a/sys/include/net/sntp.h +++ b/sys/include/net/sntp.h @@ -15,6 +15,10 @@ * @brief Simple Network Time Protocol (SNTP) implementation * @{ * + * @note The current implementation of SNTP uses @ref sys_ztimer64 with + * microsecond accuracy, which can have a strong impact on + * the power consumption of your device. + * * @file * @brief SNTP definitions * @@ -27,7 +31,7 @@ #include "net/ntp_packet.h" #include "net/sock/udp.h" -#include "xtimer.h" +#include "ztimer64.h" #ifdef __cplusplus extern "C" { @@ -45,7 +49,7 @@ extern "C" { int sntp_sync(sock_udp_ep_t *server, uint32_t timeout); /** - * @brief Get real time offset from system time as returned by @ref xtimer_now64() + * @brief Get real time offset from system time as returned by @ref ztimer64_now() * * @return Real time offset in microseconds relative to 1900-01-01 00:00 UTC */ @@ -58,7 +62,8 @@ int64_t sntp_get_offset(void); */ static inline uint64_t sntp_get_unix_usec(void) { - return (uint64_t)(sntp_get_offset() - (NTP_UNIX_OFFSET * US_PER_SEC) + xtimer_now_usec64()); + return (uint64_t)(sntp_get_offset() - (NTP_UNIX_OFFSET * US_PER_SEC) + \ + ztimer64_now(ZTIMER64_USEC)); } #ifdef __cplusplus diff --git a/sys/net/application_layer/sntp/sntp.c b/sys/net/application_layer/sntp/sntp.c index ec6ee6b5b0..48a24512d1 100644 --- a/sys/net/application_layer/sntp/sntp.c +++ b/sys/net/application_layer/sntp/sntp.c @@ -22,7 +22,7 @@ #include "net/sntp.h" #include "net/ntp_packet.h" #include "net/sock/udp.h" -#include "xtimer.h" +#include "ztimer64.h" #include "mutex.h" #include "byteorder.h" @@ -70,7 +70,7 @@ int sntp_sync(sock_udp_ep_t *server, uint32_t timeout) mutex_lock(&_sntp_mutex); _sntp_offset = (((int64_t)byteorder_ntohl(_sntp_packet.transmit.seconds)) * US_PER_SEC) + ((((int64_t)byteorder_ntohl(_sntp_packet.transmit.fraction)) * 232) - / 1000000) - xtimer_now_usec64(); + / 1000000) - ztimer64_now(ZTIMER64_USEC); mutex_unlock(&_sntp_mutex); return 0; } From acf10d91b97f35c2930481264823bf37e5ee6dce Mon Sep 17 00:00:00 2001 From: crasbe Date: Thu, 21 Aug 2025 14:20:26 +0200 Subject: [PATCH 2/2] sys/net/ntp_packet: add ref to SNTP, fix time --- sys/include/net/ntp_packet.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sys/include/net/ntp_packet.h b/sys/include/net/ntp_packet.h index 99ef72f624..9bb0dc8dd8 100644 --- a/sys/include/net/ntp_packet.h +++ b/sys/include/net/ntp_packet.h @@ -14,6 +14,8 @@ * @brief The NTP packet module provides functionality to manipulate the NTP header * @{ * + * @see An implementation of Simple NTP can be found in @ref net_sntp. + * * @file * @brief NTP packet definitions * @@ -46,7 +48,7 @@ extern "C" { #define NTP_PORT (123U) /**< NTP port number */ /** - * @brief Offset in seconds of NTP timestamp (seconds from 1990-01-01 00:00:00 UTC) + * @brief Offset in seconds of NTP timestamp (seconds from 1900-01-01 00:00:00 UTC) * to UNIX timestamp (seconds from 1970-01-01 00:00:00 UTC). */ #define NTP_UNIX_OFFSET (2208988800)