Crate sntpc

Crate sntpc 

Source
Expand description

Rust SNTP client implementation

§Overview

This crate provides an async-first SNTP client for sending requests to NTP servers and processing responses to extract accurate timestamps.

Supported protocol version: SNTPv4 (RFC 4330)

§Quick Start

Add to your Cargo.toml:

[dependencies]
sntpc = "0.8"

For common usage patterns, choose a network adapter:

  • sntpc-net-std - Standard library UDP sockets
  • sntpc-net-tokio - Tokio async runtime
  • sntpc-net-embassy - Embassy embedded runtime

§Features

  • std - Standard library support (includes StdTimestampGen)
  • sync - Synchronous API in sync module (default is async)
  • utils - OS-specific utilities for system time sync ⚠️ Unstable API
  • log - Debug logging via log crate
  • defmt - Debug logging via defmt (mutually exclusive with log)

Warning: log and defmt are mutually exclusive features. If both are enabled, defmt takes priority.

§Architecture

The library is designed to work in both std and no_std environments through two key traits:

For std environments, StdTimestampGen is provided.

§API Approaches

  • get_time - Complete request/response in a single call (suitable for most cases)
  • sntp_send_request and sntp_process_response - Split send/receive workflow (useful when the TCP/IP stack requires polling or has custom timing requirements)

§Examples

See the examples directory for complete examples:

  • simple-request - Basic synchronous usage
  • tokio - Async with tokio runtime
  • embassy-net - Embedded async with embassy
  • smoltcp-request - Custom no_std networking
  • And more…

Refer to individual function documentation for minimal code examples

Modules§

sync
Synchronous interface for the SNTP client

Structs§

NtpContext
SNTP client context that contains of objects that may be required for client’s operation
NtpResult
SNTP request result representation
SendRequestResult
Preserve SNTP request sending operation result required during receiving and processing state
StdTimestampGen
Standard library timestamp generator wrapper type that relies on std::time to provide timestamps during SNTP client operations

Enums§

Error
The error type for SNTP client Errors originate on network layer or during processing response from a NTP server

Traits§

NtpTimestampGenerator
A trait encapsulating timestamp generator’s operations
NtpUdpSocket
A trait encapsulating UDP socket interface required for SNTP client operations

Functions§

fraction_to_microseconds
Convert second fraction value to microseconds value
fraction_to_milliseconds
Convert second fraction value to milliseconds value
fraction_to_nanoseconds
Convert second fraction value to nanoseconds value
fraction_to_picoseconds
Convert second fraction value to picoseconds value
get_time
Retrieves the current time from an NTP server.
sntp_process_response
Processes the response from an NTP server.
sntp_send_request
Sends an SNTP request to an NTP server.

Type Aliases§

Result
SNTP library result type