Skip to content

Commit c2f2471

Browse files
committed
net: wg: Support RTC with native_sim board
If running wg in native-sim, use the host clock to get the current time. This helps to have a proper handshake when connecting even after restarting the zephyr.exe process. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent c71c299 commit c2f2471

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

subsys/net/lib/wireguard/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ zephyr_library_sources(crypto/refc/poly1305-donna.c)
1313
zephyr_library_sources(crypto/refc/blake2s.c)
1414
zephyr_library_sources(crypto/refc/chacha20poly1305.c)
1515
zephyr_library_sources(crypto/refc/x25519.c)
16+
17+
zephyr_library_sources_ifdef(CONFIG_BOARD_NATIVE_SIM ns_rtc.c)

subsys/net/lib/wireguard/ns_rtc.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/* Get time from host system when running on native_posix board */
8+
9+
#include <zephyr/logging/log.h>
10+
LOG_MODULE_DECLARE(wireguard, CONFIG_WIREGUARD_LOG_LEVEL);
11+
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/net/net_core.h>
14+
#include <zephyr/net/wireguard.h>
15+
16+
#include "native_rtc.h"
17+
18+
int wireguard_get_current_time(uint64_t *seconds, uint32_t *nanoseconds)
19+
{
20+
uint64_t timeus;
21+
22+
timeus = native_rtc_gettime_us(RTC_CLOCK_PSEUDOHOSTREALTIME);
23+
24+
*seconds = timeus / USEC_PER_SEC;
25+
*nanoseconds = (timeus % USEC_PER_SEC) * NSEC_PER_USEC;
26+
27+
NET_DBG("Current time: %llu.%09u", *seconds, *nanoseconds);
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)