nuttx/Documentation/applications/netutils/ntpclient/index.rst
Xiang Xiao c32b683085 !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support
Every compiler supported by NuttX provides the "long long" types,
so the CONFIG_HAVE_LONG_LONG indirection is no longer useful.
Remove the option from include/nuttx/compiler.h and treat
"long long" as unconditionally available across the OS.

In addition to deleting the guard itself, this commit unconditionally
enables the long-long flavored helpers that used to be gated behind
it:

  - libs/libc/fixedmath: drop the soft-emulated b32/ub32 routines
    in lib_fixedmath.c (-261 lines) and trim the matching
    prototypes, Make.defs and CMakeLists.txt entries; keep only
    the long-long backed implementations.
  - include/sys/endian.h, include/strings.h, libs/libc/string
    /lib_ffsll.c, lib_flsll.c: always expose the 64-bit byte-swap
    and ffsll/flsll variants.
  - libs/libm/libm/lib_llround{,f,l}.c: drop the empty stubs.
  - libs/libc/stdlib (atoll, llabs, lldiv, strtoll/ull, rand48,
    strtold), libs/libc/stream (libvsprintf, libvscanf,
    libbsprintf, ultoa_invert), libs/libc/misc (crc64, crc64emac),
    libs/libc/inttypes/strtoimax, libs/libc/lzf, libs/libc/libc.csv,
    libs/libc/string (memset, vikmemcpy): remove the
    #ifdef CONFIG_HAVE_LONG_LONG branches.
  - include/{stddef.h,stdlib.h,fixedmath.h,sys/epoll.h,cxx/cstdlib,
    nuttx/audio/audio.h,nuttx/crc64.h,nuttx/lib/math.h,
    nuttx/lib/math32.h,nuttx/lib/stdbit.h}: same guard cleanup.
  - drivers/note/note_driver.c, fs/spiffs/src/spiffs.h,
    sched/irq/irq_procfs.c: drop their local guards as well.
  - Documentation/applications/netutils/ntpclient/index.rst:
    refresh the documentation snippet.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2026-05-19 16:21:28 +08:00

174 lines
6.5 KiB
ReStructuredText

========================
``ntpclient`` NTP client
========================
The NTP (Network Time Protocol) client is a network utility that synchronizes
the system clock with time servers over the Internet. This implementation
provides a minimal but functional NTP client for NuttX.
What is NTP?
============
The Network Time Protocol (NTP) is a networking protocol designed to
synchronize clocks of computer systems over packet-switched, variable-latency
data networks. NTP is one of the oldest Internet protocols still in use,
originally designed by David L. Mills of the University of Delaware.
Key features of NTP:
- **High Precision**: NTP can achieve sub-millisecond accuracy on local area
networks and typically 10-100 millisecond accuracy over the Internet
- **Robust Algorithm**: Uses sophisticated algorithms to filter out network
jitter and select the best time sources
- **Hierarchical Structure**: Uses a stratum system where stratum 0 devices
are atomic clocks, stratum 1 servers sync with stratum 0, and so on
- **Fault Tolerance**: Can handle multiple time sources and automatically
switch between them
NTP Protocol Overview
=====================
NTP uses UDP port 123 and follows a client-server model. The protocol
exchanges timestamps to calculate:
- **Offset**: The difference between the client's clock and the server's clock
- **Delay**: The round-trip network delay
- **Dispersion**: The maximum error due to clock frequency tolerance
The NTP packet format (version 3) includes:
- **Leap Indicator**: Warns of an impending leap second
- **Version Number**: NTP version (3 in this implementation)
- **Mode**: Client, server, broadcast, etc.
- **Stratum**: Clock level (0-15)
- **Poll Interval**: Maximum interval between successive messages
- **Precision**: Clock precision
- **Root Delay/Dispersion**: Total delay and dispersion to the reference clock
- **Reference Identifier**: Identifies the reference source
- **Reference Timestamp**: Time when the system clock was last set
- **Originate Timestamp**: Time when the request departed the client
- **Receive Timestamp**: Time when the request arrived at the server
- **Transmit Timestamp**: Time when the reply departed the server
Implementation Details
======================
The NuttX NTP client implementation consists of several key components:
Source Code Structure
---------------------
**ntpclient.c** - Main implementation file containing:
- **Daemon Management**: Functions to start, stop, and manage the NTP daemon
- **Time Synchronization**: Core algorithms for calculating clock offset and delay
- **Network Communication**: UDP socket handling and NTP packet exchange
- **Sample Collection**: Gathering multiple time samples for statistical filtering
**ntpv3.h** - NTP version 3 packet format definitions:
- **ntp_datagram_s**: Complete NTP packet structure
- **ntp_timestamp_s**: 64-bit NTP timestamp format
- **Protocol Constants**: NTP version, modes, and stratum definitions
Key Functions
-------------
- **ntpc_start_with_list()**: Starts the NTP daemon with a list of servers
- **ntpc_start()**: Starts the NTP daemon with default configuration
- **ntpc_stop()**: Stops the running NTP daemon
- **ntpc_status()**: Retrieves current synchronization status and samples
- **ntpc_daemon()**: Main daemon loop that:
- Connects to configured NTP servers
- Sends NTP requests and processes responses
- Calculates clock offset and delay
- Applies time corrections to the system clock
- Continues polling at configured intervals
- **ntpc_get_ntp_sample()**: Performs a single NTP transaction:
- Creates UDP socket to NTP server
- Sends NTP request packet with current timestamp
- Receives and validates NTP response
- Calculates offset and delay using NTP algorithms
- **ntpc_calculate_offset()**: Implements the NTP clock filter algorithm
- Uses four timestamps, calculates offset and delay
- Applies statistical filtering to reduce jitter
- **ntpc_settime()**: Applies time correction to system clock:
- Uses calculated offset to adjust system time
- Handles both positive and negative time adjustments
- Maintains monotonic clock consistency
Configuration Options
=====================
The NTP client can be configured through Kconfig options:
- **CONFIG_NETUTILS_NTPCLIENT_SERVER**: List of NTP server hostnames
- **CONFIG_NETUTILS_NTPCLIENT_PORTNO**: NTP server port (default: 123)
- **CONFIG_NETUTILS_NTPCLIENT_STACKSIZE**: Daemon task stack size
- **CONFIG_NETUTILS_NTPCLIENT_SERVERPRIO**: Daemon task priority
- **CONFIG_NETUTILS_NTPCLIENT_STAY_ON**: Keep polling continuously
- **CONFIG_NETUTILS_NTPCLIENT_POLLDELAYSEC**: Polling interval in seconds
- **CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES**: Number of samples for filtering
- **CONFIG_NETUTILS_NTPCLIENT_TIMEOUT_MS**: Network timeout in milliseconds
Usage
=====
The NTP client is typically used through the system commands:
.. note:: The NTP client functionality requires enabling the :code:`SYSTEM_NTPC` option in your configuration.
Make sure to select this option in menuconfig or your Kconfig fragment before building.
- **ntpcstart**: Start the NTP daemon
- **ntpcstop**: Stop the NTP daemon
- **ntpcstatus**: Display synchronization status
Example workflow:
1. Configure network connectivity
2. Start NTP client: ``ntpcstart``
3. Check status: ``ntpcstatus``
4. Verify time: ``date`` command
5. Stop when needed: ``ntpcstop``
The client will automatically:
- Connect to configured NTP servers
- Exchange time information
- Calculate and apply clock corrections
- Continue periodic synchronization
Limitations
===========
This is a minimal NTP client implementation with some limitations:
- **No Authentication**: Does not support NTP authentication (MD5/SHA1)
- **Basic Filtering**: Uses simple statistical filtering, not full NTP algorithms
- **Single Reference**: Does not implement full NTP reference clock selection
- **No Leap Seconds**: Does not handle leap second announcements
- **Limited Error Handling**: Basic error recovery and retry mechanisms
Despite these limitations, the implementation provides sufficient accuracy
for most embedded applications requiring network time synchronization.
Dependencies
============
The NTP client requires:
- **CONFIG_NET**: Network support
- **CONFIG_NET_UDP**: UDP protocol support
- **CONFIG_NET_SOCKOPTS**: Socket options support
- **CONFIG_LIBC_NETDB**: DNS resolution (recommended)
For best results, ensure:
- Stable network connectivity
- Access to reliable NTP servers
- Sufficient system resources for daemon operation