SatCat5
ntp_client.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Client and/or server for the Network Time Protocol (NTP)
6 
7 #pragma once
8 
9 #include <satcat5/ntp_header.h>
10 #include <satcat5/polling.h>
11 #include <satcat5/ptp_source.h>
12 #include <satcat5/udp_core.h>
13 #include <satcat5/udp_dispatch.h>
14 
15 namespace satcat5 {
16  namespace ntp {
43  class Client
44  : public satcat5::net::Protocol
45  , public satcat5::poll::Timer
46  , public satcat5::ptp::Source
47  {
48  public:
50  Client(
52  satcat5::udp::Dispatch* iface);
53  ~Client() SATCAT5_OPTIONAL_DTOR;
54 
57  void client_connect(
58  const satcat5::ip::Addr& server,
59  s8 poll_rate = satcat5::ntp::Header::TIME_1MIN);
60 
62  void client_close();
63 
65  inline bool client_ok()
66  { return m_iface.ready(); }
67 
70  void client_set_rate(s8 poll_rate);
71 
73  inline void server_start(u8 stratum)
74  { m_stratum = stratum; }
75 
77  u64 ntp_now() const;
79  u64 to_ntp(const satcat5::ptp::Time& t) const;
81  satcat5::ptp::Time to_ptp(u64 t) const;
82 
83  protected:
84  // Inherited event handlers.
85  void frame_rcvd(satcat5::io::LimitedRead& src) override;
86  void timer_event() override;
87 
88  // Internal event handlers.
89  void rcvd_reply(const satcat5::ntp::Header& msg, u64 rxtime);
90  bool send_reply(const satcat5::ntp::Header& msg, u64 rxtime);
91  bool send_query();
92 
93  // Internal state.
94  satcat5::ptp::TrackingClock* const m_refclk;
95  satcat5::udp::Address m_iface;
96  u64 m_reftime;
97  u8 m_leap;
98  u8 m_stratum;
99  s8 m_rate;
100  };
101  }
102 }
Limited read of next N bytes.
Definition: io_readable.h:255
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Client and/or server for the Network Time Protocol (NTP).
Definition: ntp_client.h:47
void client_connect(const satcat5::ip::Addr &server, s8 poll_rate=satcat5::ntp::Header::TIME_1MIN)
Enable client mode by connecting to the specified server.
Definition: ntp_client.cc:57
void timer_event() override
Child class MUST override this method.
Definition: ntp_client.cc:97
void client_close()
Close the connection to a remote server.
Definition: ntp_client.cc:63
void client_set_rate(s8 poll_rate)
Set the client polling rate.
Definition: ntp_client.cc:68
satcat5::ptp::Time to_ptp(u64 t) const
Convert NTP timestamp to PTP format.
Definition: ntp_client.cc:188
bool client_ok()
Is the client currently connected?
Definition: ntp_client.h:65
u64 to_ntp(const satcat5::ptp::Time &t) const
Convert any PTP timestamp to NTP format.
Definition: ntp_client.cc:180
u64 ntp_now() const
Get the current time in NTP format.
Definition: ntp_client.cc:176
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: ntp_client.cc:73
Client(satcat5::ptp::TrackingClock *refclk, satcat5::udp::Dispatch *iface)
Set the reference clock and network interface for this client.
Definition: ntp_client.cc:37
void server_start(u8 stratum)
Enable or disable server mode.
Definition: ntp_client.h:73
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
A source for ptp::Measurement events, usually a ptp::Client.
Definition: ptp_source.h:19
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
Generic interface to a numerically-controlled reference clock.
Definition: ptp_tracking.h:52
Implementation of "net::Address" for UDP Dispatch.
Definition: udp_core.h:72
bool ready() const override
Is this address object ready for use? Child MUST override this method.
Definition: udp_core.h:103
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Core event-processing loop for SatCat5 software.
Define the API for generating ptp::Measurement events.
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
Message headers for the Network Time Protocol (NTP / IETF RFC-5905).
Definition: ntp_header.h:20