-   Notifications  
You must be signed in to change notification settings  - Fork 8.2k
 
net: sntp: implement SNTP server library #96040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Open  
  lfelten wants to merge 1 commit into zephyrproject-rtos:main       Choose a base branch                                                 from lfelten:sntp_server                
 base: main
Could not load branches
    Branch not found: {{ refName }} 
     Loading  
 Could not load tags
   Nothing to show
        Loading  
 Are you sure you want to change the base?
 Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated. 
    +437   −17      
     Open  
 Changes from all commits
 Commits 
  File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright (c) 2025 Lothar Felten <lothar.felten@gmail.com> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|   |  ||
| /** | ||
| * @file | ||
| * @brief SNTP (Simple Network Time Protocol, RFC5905) Server | ||
| */ | ||
|   |  ||
| #ifndef ZEPHYR_INCLUDE_NET_SNTP_SERVER_H_ | ||
| #define ZEPHYR_INCLUDE_NET_SNTP_SERVER_H_ | ||
|   |  ||
| #include <zephyr/posix/time.h> | ||
|   |  ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|   |  ||
| /** | ||
| * @brief Simple Network Time Protocol Server API | ||
| * @defgroup sntp SNTP | ||
| * @since 4.3 | ||
| * @version 0.1.0 | ||
| * @ingroup networking | ||
| * @{ | ||
| */ | ||
|   |  ||
| /** | ||
| * @brief Set SNTP time source id and stratum | ||
| * @param refid the reference ID (RFC5905, Fig.12) | ||
| * @param stratum indicating the stratum of the clock source | ||
| * @param precision an exponent of two, where the resulting value is the precision | ||
| * of the system clock in seconds. | ||
| * | ||
| * @return 0 if ok, <0 if error. | ||
| */ | ||
| int sntp_server_clock_source(unsigned char *refid, unsigned char stratum, char precision); | ||
|   |  ||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|   |  ||
| /** | ||
| * @} | ||
| */ | ||
|   |  ||
| #endif /* ZEPHYR_INCLUDE_NET_SNTP_SERVER_H_ */ | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|   |  ||
| cmake_minimum_required(VERSION 3.20.0) | ||
| find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
| project(sntp_server) | ||
|   |  ||
| FILE(GLOB app_sources src/*.c) | ||
| target_sources(app PRIVATE ${app_sources}) | ||
|   |  ||
| include(${ZEPHYR_BASE}/samples/net/common/common.cmake) | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Copyright (c) 2025 Lothar Felten <lothar.felten@gmail.com> | ||
|   |  ||
| source "Kconfig.zephyr" | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| .. zephyr:code-sample:: sntp-server | ||
| :name: SNTP server | ||
| :relevant-api: bsd_sockets sntp | ||
|   |  ||
| SNTP to get the current time from the host. | ||
|   |  ||
| Overview | ||
| ******** | ||
|   |  ||
| This sample is a simple SNTP server implementation. | ||
| The demo will query a SNTP server and set the system time and date. | ||
| When the local clock is set, the system will respond to SNTP queries | ||
| on port 123, the default SNTP port. | ||
| You can query the server using a SNTP client, such as ntpdig or sntp. | ||
|   |  ||
| This demo assumes that the platform of choice has networking support, | ||
| some adjustments to the configuration may be needed. | ||
|   |  ||
| Building and Running | ||
| ******************** | ||
|   |  ||
| See the `net-tools`_ project for more details. | ||
|   |  ||
| This sample can be built and executed on QEMU or native_sim board as | ||
| described in :ref:`networking_with_qemu`. | ||
|   |  ||
| .. _`net-tools`: https://github.com/zephyrproject-rtos/net-tools | ||
|   |  ||
| Wi-Fi | ||
| ===== | ||
|   |  ||
| The IPv4 Wi-Fi support can be enabled in the sample with | ||
| :ref:`Wi-Fi snippet <snippet-wifi-ipv4>`. | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Copyright (c) 2025 Embeint Inc | ||
|   |  ||
| CONFIG_ETH_NATIVE_TAP=n | ||
| CONFIG_NET_DRIVERS=y | ||
| CONFIG_NET_SOCKETS=y | ||
| CONFIG_NET_SOCKETS_OFFLOAD=y | ||
| CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y | ||
| CONFIG_HEAP_MEM_POOL_SIZE=4096 | ||
|   |  ||
| # IPv6 DAD requires lower level network interface access, below exposed socket-level access | ||
| CONFIG_NET_IPV6_DAD=n | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # General config | ||
| CONFIG_REQUIRES_FULL_LIBC=y | ||
| CONFIG_POSIX_API=y | ||
|   |  ||
| # Networking config | ||
| CONFIG_NETWORKING=y | ||
| CONFIG_NET_IPV4=y | ||
| CONFIG_NET_IPV6=y | ||
| CONFIG_NET_UDP=y | ||
| CONFIG_NET_SHELL=y | ||
|   |  ||
| # Sockets | ||
| CONFIG_NET_SOCKETS=y | ||
| CONFIG_NET_SOCKETS_SERVICE=y | ||
|   |  ||
| # Network driver config | ||
| CONFIG_TEST_RANDOM_GENERATOR=y | ||
|   |  ||
| # Network address config | ||
| CONFIG_NET_CONFIG_SETTINGS=y | ||
| CONFIG_NET_CONFIG_NEED_IPV4=y | ||
| CONFIG_NET_DHCPV4=y | ||
| CONFIG_NET_CONFIG_NEED_IPV6=y | ||
|   |  ||
| # SNTP | ||
| CONFIG_SNTP=y | ||
| CONFIG_DNS_RESOLVER=y | ||
| CONFIG_SNTP_CLIENT=y | ||
| CONFIG_SNTP_SERVER=y | ||
|   |  ||
| CONFIG_MAIN_STACK_SIZE=2048 | ||
| # Network debug config | ||
| CONFIG_LOG=y | ||
| CONFIG_NET_LOG=y | ||
| CONFIG_SNTP_LOG_LEVEL_DBG=y | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| sample: | ||
| description: SNTP server sample | ||
| name: sntp_server | ||
| common: | ||
| harness: net | ||
| tags: net | ||
| tests: | ||
| sample.net.sockets.sntp_server: | ||
| platform_allow: | ||
| - qemu_x86 | ||
| - native_sim | ||
| sample.net.sockets.sntp_server.native: | ||
| platform_allow: | ||
| - native_sim | ||
| integration_platforms: | ||
| - native_sim | ||
| extra_configs: | ||
| - CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=y | ||
| extra_args: | ||
| - EXTRA_CONF_FILE="overlay-nsos.conf" | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) 2025 Lothar Felten <lothar.felten@gmail.com> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|   |  ||
| #include <zephyr/logging/log.h> | ||
| LOG_MODULE_REGISTER(sntp_demo, LOG_LEVEL_DBG); | ||
|   |  ||
| #include <zephyr/net/sntp_server.h> | ||
| #include <zephyr/net/sntp.h> | ||
| #include "net_sample_common.h" | ||
|   |  ||
| int main(void) | ||
| { | ||
| struct sntp_time t; | ||
| struct timespec tp; | ||
| int ret; | ||
|   |  ||
| LOG_INF("waiting for network"); | ||
| wait_for_network(); | ||
| ret = sntp_simple("ptbtime1.ptb.de", SYS_FOREVER_MS, &t); | ||
| if (ret < 0) { | ||
| LOG_ERR("SNTP client error (%d)", ret); | ||
| return 0; | ||
| } | ||
| tp.tv_sec = t.seconds; | ||
| tp.tv_nsec = t.fraction / 1000; | ||
|   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, wrong handling of the fractional part, I think  |  ||
| ret = sys_clock_settime(SYS_CLOCK_REALTIME, &tp); | ||
| if (ret < 0) { | ||
| LOG_ERR("SNTP unable to set system time (%d)", ret); | ||
| return 0; | ||
| } | ||
|   |  ||
| sntp_server_clock_source("XDEV", 2, -6); | ||
| return 0; | ||
| } | ||
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,13 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|   |  ||
| zephyr_sources( | ||
| zephyr_sources_ifdef(CONFIG_SNTP | ||
| sntp.c | ||
| ) | ||
|   |  ||
| zephyr_sources_ifdef(CONFIG_SNTP_CLIENT | ||
| sntp_simple.c | ||
| ) | ||
|   |  ||
| zephyr_sources_ifdef(CONFIG_SNTP_SERVER | ||
| sntp_server.c | ||
| ) | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|      |  @@ -5,10 +5,17 @@ menuconfig SNTP | |||||
| bool "SNTP (Simple Network Time Protocol)" | ||||||
| depends on NET_SOCKETS | ||||||
| help | ||||||
| Enable SNTP client library | ||||||
| Enable SNTP support library | ||||||
|   |  ||||||
| if SNTP | ||||||
|   |  ||||||
| config SNTP_CLIENT | ||||||
| bool "SNTP client support" | ||||||
| help | ||||||
| This option enables SNTP client support | ||||||
|   |  ||||||
| if SNTP_CLIENT | ||||||
|   |  ||||||
| config SNTP_UNCERTAINTY | ||||||
| bool "Calculate SNTP uncertainty and set uptime timestamp" | ||||||
| help | ||||||
|      |  @@ -19,10 +26,31 @@ config SNTP_UNCERTAINTY | |||||
| Enabling this option uses more resources (memory) and is not normally | ||||||
| needed. | ||||||
|   |  ||||||
| endif # SNTP client | ||||||
|   |  ||||||
| config SNTP_SERVER | ||||||
| bool "SNTP client support" | ||||||
|   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.  Suggested change   
  |  ||||||
| help | ||||||
| This option enables SNTP server support | ||||||
|   |  ||||||
| if SNTP_SERVER | ||||||
|   |  ||||||
| config SNTP_SERVER_STACK_SIZE | ||||||
| int "Stack size of the SNTP server thread" | ||||||
| default 1024 | ||||||
|   |  ||||||
| config SNTP_SERVER_THREAD_PRIORITY | ||||||
| int "SNTP server thread priority" | ||||||
| default NUM_PREEMPT_PRIORITIES | ||||||
| help | ||||||
| Priority of SNTP server thread. | ||||||
|   |  ||||||
| endif # SNTP server | ||||||
|   |  ||||||
| module = SNTP | ||||||
| module-dep = NET_LOG | ||||||
| module-str = Log level for SNTP | ||||||
| module-help = Enable debug message of SNTP client library. | ||||||
| module-help = Enable debug message of SNTP library. | ||||||
| source "subsys/net/Kconfig.template.log_config.net" | ||||||
|   |  ||||||
| endif # SNTP | ||||||
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
       Oops, something went wrong.  
  Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The address of the server should be set via kconfig option. The server sample can have its own Kconfig file where the server name can be stored (see examples how to do this in other network samples). Note that the server name should be empty by default so that user is forced to set it according to his/hers network setup.