1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! [`NtpTimestampGenerator`] implementation backed by [`embassy_time`] for the [`sntpc`] SNTP client library.
//!
//! This crate provides an [`EmbassyTimestampGenerator`] that uses the Embassy async runtime's
//! monotonic clock to produce timestamps suitable for SNTP round-trip time measurements in
//! embedded `no_std` environments.
//!
//! # Design Rationale
//!
//! The timestamp generator is separated into its own crate to:
//! - **Independent versioning**: Update `embassy-time` without requiring `sntpc` core updates
//! - **Version flexibility**: Works with `embassy-time` 0.5.x (`>=0.5, <0.6`)
//! - **Embedded focus**: Minimal dependencies suitable for `no_std` embedded systems
//! - **Clean separation**: Core SNTP protocol logic remains independent of the async runtime
//!
//! # Important
//!
//! [`EmbassyTimestampGenerator`] provides **monotonic** timestamps based on [`embassy_time::Instant`],
//! not wall-clock time. This is enough for SNTP request/response delay calculations, where
//! the client measures the elapsed time between sending a request and receiving a response.
//! The actual wall-clock offset is computed by the `sntpc` library using the server's timestamps.
//!
//! # Example
//!
//! ```ignore
//! use sntpc::{get_time, NtpContext};
//! use sntpc_time_embassy::EmbassyTimestampGenerator;
//!
//! // Create an NtpContext with the Embassy timestamp generator
//! let ntp_context = NtpContext::new(EmbassyTimestampGenerator::default());
//!
//! // Use with an Embassy UDP socket adapter
//! let result = get_time(server_addr, &socket, ntp_context).await;
//! ```
//!
//! For complete examples, see the [sntpc examples](https://github.com/vpetrigo/sntpc/tree/master/examples/embassy-net).
use Instant;
use NtpTimestampGenerator;
/// Monotonic timestamp generator backed by [`embassy_time::Instant`].
///
/// This type implements [`NtpTimestampGenerator`] by capturing the current monotonic
/// time via [`Instant::now`] on each call to [`init`](NtpTimestampGenerator::init),
/// then reporting the elapsed seconds and sub-second microseconds.
///
/// It does **not** provide wall-clock time. Instead, it provides a monotonic timestamp
/// source suitable for SNTP round-trip delay calculations, where only the relative
/// elapsed time between request and response matters.
///
/// # Example
///
/// ```ignore
/// use sntpc::NtpContext;
/// use sntpc_time_embassy::EmbassyTimestampGenerator;
///
/// let ntp_context = NtpContext::new(EmbassyTimestampGenerator::default());
/// ```