12#if PC_ENABLE_NTP_SERVER
23 uint32_t pc_ntp_secs, uint32_t pc_ntp_frac, uint8_t *out,
size_t out_cap)
31 uint8_t vn = (uint8_t)((req[0] >> 3) & 0x7);
33 out[0] = (uint8_t)((0u << 6) | (vn << 3) | 4u);
35 out[2] = req[2] ? req[2] : 6;
36 out[3] = (uint8_t)(-6);
42 memcpy(out + 24, req + 40, 8);
64void pc_ntp_server_udp_handler(
const uint8_t *data,
size_t len,
const struct pc_udp_peer *peer,
void *ctx)
74 uint32_t frac = (uint32_t)(((uint64_t)(
pc_millis() % 1000u) << 32) / 1000u);
88 s_ntp.stratum = stratum;
90 return pc_udp_listen(123, pc_ntp_server_udp_handler,
nullptr);
Pluggable monotonic clock for all library timing.
uint32_t pc_millis(void)
The library's monotonic time at 1000 Hz (milliseconds).
Fixed-width integer serializers into a raw uint8_t* buffer - one source of truth.
size_t pc_wr32be(uint8_t *p, uint32_t v)
Write v big-endian at p.
#define NTP_REFID_LOCL
Reference ID "LOCL" - an undisciplined local clock (RFC 5905 sec 7.3).
#define NTP_UNIX_OFFSET
Seconds between the NTP epoch (1900-01-01) and the Unix epoch (1970-01-01).
size_t pc_ntp_server_build_response(const uint8_t *req, size_t req_len, uint8_t stratum, uint32_t refid, uint32_t pc_ntp_secs, uint32_t pc_ntp_frac, uint8_t *out, size_t out_cap)
Build a server (mode 4) reply to a client NTP request. Pure - no clock, no I/O.
bool pc_ntp_server_begin(uint8_t stratum, uint32_t refid=NTP_REFID_LOCL)
Start answering NTP requests on UDP/123 from the device's own clock.
#define NTP_PACKET_LEN
One NTP packet on the wire is exactly 48 octets (no extension/auth fields).
User-facing configuration for ProtoCore.
#define PC_NTP_SERVER_STRATUM
Stratum the NTP server advertises (distance from a reference clock; 1-15).
uint32_t pc_time_now(void)
Current best time.
Multi-source time fallback matrix (PC_ENABLE_TIME_SOURCE).
bool pc_udp_listen(uint16_t port, pc_udp_handler handler, void *ctx)
Bind a UDP port and route incoming datagrams to handler.
bool pc_udp_send(const struct pc_udp_peer *peer, const uint8_t *data, size_t len)
Send a datagram back to the peer captured during the handler call.
Layer 4 (Transport) - connectionless UDP datagram service.