Skip to content

Commit c0cc617

Browse files
authored
Merge pull request #21633 from crasbe/pr/sntp
sys/net/sntp: migrate from xtimer to ztimer
2 parents e26321e + acf10d9 commit c0cc617

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

sys/Makefile.dep

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,8 @@ endif
134134

135135
ifneq (,$(filter sntp,$(USEMODULE)))
136136
USEMODULE += sock_udp
137-
USEMODULE += xtimer
138-
ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
139-
# requires 64bit ftimestamps
140-
USEMODULE += ztimer64_xtimer_compat
141-
endif
137+
USEMODULE += ztimer64
138+
USEMODULE += ztimer64_usec
142139
endif
143140

144141
ifneq (,$(filter sock_%,$(USEMODULE)))

sys/include/net/ntp_packet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* @brief The NTP packet module provides functionality to manipulate the NTP header
1515
* @{
1616
*
17+
* @see An implementation of Simple NTP can be found in @ref net_sntp.
18+
*
1719
* @file
1820
* @brief NTP packet definitions
1921
*
@@ -46,7 +48,7 @@ extern "C" {
4648
#define NTP_PORT (123U) /**< NTP port number */
4749

4850
/**
49-
* @brief Offset in seconds of NTP timestamp (seconds from 1990-01-01 00:00:00 UTC)
51+
* @brief Offset in seconds of NTP timestamp (seconds from 1900-01-01 00:00:00 UTC)
5052
* to UNIX timestamp (seconds from 1970-01-01 00:00:00 UTC).
5153
*/
5254
#define NTP_UNIX_OFFSET (2208988800)

sys/include/net/sntp.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
* @brief Simple Network Time Protocol (SNTP) implementation
1616
* @{
1717
*
18+
* @note The current implementation of SNTP uses @ref sys_ztimer64 with
19+
* microsecond accuracy, which can have a strong impact on
20+
* the power consumption of your device.
21+
*
1822
* @file
1923
* @brief SNTP definitions
2024
*
@@ -27,7 +31,7 @@
2731

2832
#include "net/ntp_packet.h"
2933
#include "net/sock/udp.h"
30-
#include "xtimer.h"
34+
#include "ztimer64.h"
3135

3236
#ifdef __cplusplus
3337
extern "C" {
@@ -45,7 +49,7 @@ extern "C" {
4549
int sntp_sync(sock_udp_ep_t *server, uint32_t timeout);
4650

4751
/**
48-
* @brief Get real time offset from system time as returned by @ref xtimer_now64()
52+
* @brief Get real time offset from system time as returned by @ref ztimer64_now()
4953
*
5054
* @return Real time offset in microseconds relative to 1900-01-01 00:00 UTC
5155
*/
@@ -58,7 +62,8 @@ int64_t sntp_get_offset(void);
5862
*/
5963
static inline uint64_t sntp_get_unix_usec(void)
6064
{
61-
return (uint64_t)(sntp_get_offset() - (NTP_UNIX_OFFSET * US_PER_SEC) + xtimer_now_usec64());
65+
return (uint64_t)(sntp_get_offset() - (NTP_UNIX_OFFSET * US_PER_SEC) + \
66+
ztimer64_now(ZTIMER64_USEC));
6267
}
6368

6469
#ifdef __cplusplus

sys/net/application_layer/sntp/sntp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "net/sntp.h"
2323
#include "net/ntp_packet.h"
2424
#include "net/sock/udp.h"
25-
#include "xtimer.h"
25+
#include "ztimer64.h"
2626
#include "mutex.h"
2727
#include "byteorder.h"
2828

@@ -70,7 +70,7 @@ int sntp_sync(sock_udp_ep_t *server, uint32_t timeout)
7070
mutex_lock(&_sntp_mutex);
7171
_sntp_offset = (((int64_t)byteorder_ntohl(_sntp_packet.transmit.seconds)) * US_PER_SEC) +
7272
((((int64_t)byteorder_ntohl(_sntp_packet.transmit.fraction)) * 232)
73-
/ 1000000) - xtimer_now_usec64();
73+
/ 1000000) - ztimer64_now(ZTIMER64_USEC);
7474
mutex_unlock(&_sntp_mutex);
7575
return 0;
7676
}

0 commit comments

Comments
 (0)