1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-15 09:33:50 +01:00

Merge pull request #21633 from crasbe/pr/sntp

sys/net/sntp: migrate from xtimer to ztimer
This commit is contained in:
Marian Buschsieweke 2025-08-21 16:23:56 +00:00 committed by GitHub
commit c0cc617c7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 11 deletions

View File

@ -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)))

View File

@ -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)

View File

@ -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

View File

@ -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;
}