Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
ntp_client_core.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_NTP_CLIENT_CORE_HPP_INCLUDED
4#define _TIME_SHIELD_NTP_CLIENT_CORE_HPP_INCLUDED
5
6#include "ntp_packet.hpp"
7#include "udp_transport.hpp"
8
9#include <cstdint>
10#include <string>
11
12namespace time_shield {
13namespace detail {
14
17 public:
19 bool query(IUdpTransport& transport,
20 const std::string& host,
21 int port,
22 int timeout_ms,
23 int& out_error_code,
24 int64_t& out_offset_us,
25 int64_t& out_delay_us,
26 int& out_stratum) noexcept {
27 out_error_code = 0;
28 out_offset_us = 0;
29 out_delay_us = 0;
30 out_stratum = -1;
31
32 uint64_t now_us = 0;
33 if (!get_now_us(now_us)) {
34 out_error_code = -1;
35 return false;
36 }
37
38 NtpPacket pkt{};
39 fill_client_packet(pkt, now_us);
40
41 NtpPacket reply{};
42 UdpRequest req;
43 req.host = host;
44 req.port = port;
45 req.send_data = &pkt;
46 req.send_size = sizeof(pkt);
47 req.recv_data = &reply;
48 req.recv_size = sizeof(reply);
49 req.timeout_ms = timeout_ms;
50
51 if (!transport.transact(req, out_error_code)) {
52 if (out_error_code == 0) {
53 out_error_code = -1;
54 }
55 return false;
56 }
57
58 uint64_t arrival_us = 0;
59 if (!get_now_us(arrival_us)) {
60 out_error_code = -1;
61 return false;
62 }
63
64 if (!parse_server_packet(reply, arrival_us, out_offset_us, out_delay_us, out_stratum, out_error_code)) {
65 if (out_error_code == 0) {
66 out_error_code = -1;
67 }
68 return false;
69 }
70
71 return true;
72 }
73
74 private:
76 static bool get_now_us(uint64_t& out) noexcept {
77 const int64_t v = time_shield::now_realtime_us();
78 if (v < 0) return false;
79 out = static_cast<uint64_t>(v);
80 return true;
81 }
82 };
83
84} // namespace detail
85} // namespace time_shield
86
87#endif // _TIME_SHIELD_NTP_CLIENT_CORE_HPP_INCLUDED
Abstract UDP transport interface for NTP queries.
Core NTP query logic that parses packets and computes offsets.
static bool get_now_us(uint64_t &out) noexcept
Read current realtime clock in microseconds.
bool query(IUdpTransport &transport, const std::string &host, int port, int timeout_ms, int &out_error_code, int64_t &out_offset_us, int64_t &out_delay_us, int &out_stratum) noexcept
Perform one NTP transaction using a UDP transport.
int64_t now_realtime_us()
Get current real time in microseconds using a platform-specific method.
static bool parse_server_packet(const NtpPacket &pkt, uint64_t arrival_us, int64_t &offset_us, int64_t &delay_us, int &stratum, int &out_error_code) noexcept
Parse server response and compute offset and delay.
static void fill_client_packet(NtpPacket &pkt, uint64_t now_us)
Fill an NTP client request packet using local time.
Main namespace for the Time Shield library.
NTP packet layout (48 bytes).
UDP request parameters for NTP transactions.
std::size_t send_size
Outgoing payload size in bytes.
int timeout_ms
Receive timeout in milliseconds.
std::string host
Target host name or IP address.
std::size_t recv_size
Receive buffer size in bytes.
const void * send_data
Pointer to outgoing payload.
void * recv_data
Pointer to receive buffer.