SatCat5
ptp_dispatch.h
1 // Copyright 2023-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Dispatch API for incoming L2 and L3 PTP messages.
6 
7 #pragma once
8 
9 #include <satcat5/eth_header.h>
10 #include <satcat5/ip_core.h>
11 #include <satcat5/ip_dispatch.h>
12 #include <satcat5/polling.h>
13 #include <satcat5/ptp_header.h>
14 #include <satcat5/ptp_interface.h>
15 #include <satcat5/ptp_time.h>
16 #include <satcat5/udp_core.h>
17 
18 namespace satcat5 {
19  namespace ptp {
21  enum class DispatchTo {
22  BROADCAST_L2, // L2/Ethernet broadcast
23  BROADCAST_L3, // L3/UDP broadcast
24  REPLY, // To current reply address
25  STORED, // To stored unicast address
26  };
27 
47  public:
49  explicit Dispatch(
52  ~Dispatch() SATCAT5_OPTIONAL_DTOR;
53 
54  // Network interface accessors:
55  inline satcat5::ip::Dispatch* iface() const
56  { return m_ip; } // IP interface
57  inline satcat5::igmp::Client* igmp() const
58  { return m_ip->igmp(); }
59  inline satcat5::eth::MacAddr macaddr() const
60  { return m_ip->macaddr(); } // Source MAC address
61 
63  inline void ptp_callback(satcat5::ptp::Client* client)
64  { m_callback = client; }
65 
70  { return m_iface->ptp_time_now(); }
72  { return m_iface->ptp_tx_start(); }
74  { return m_iface->ptp_tx_timestamp(); }
76  { return m_iface->ptp_rx_timestamp(); }
78 
82  satcat5::ptp::DispatchTo where, unsigned num_bytes, u8 ptp_msg_type);
83 
86  void store_reply_addr(); // Reply to most recent sender.
87  void store_addr( // Send to a specific L2/L3 address.
88  const satcat5::eth::MacAddr& mac,
89  const satcat5::ip::Addr& ip = satcat5::ip::ADDR_NONE,
90  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
92 
93  protected:
94  // Incoming message notification.
95  void poll_demand() override;
96 
97  // Helper functions for assigning header parameters
98  satcat5::eth::Header get_dst_eth(satcat5::ptp::DispatchTo addr) const;
99  satcat5::udp::Port get_dst_port(u8 ptp_msg_type) const;
100  satcat5::ip::Addr get_dst_ip(u8 ptp_msg_type, satcat5::ptp::DispatchTo addr) const;
101 
102  // Pointers to interfaces and event handlers.
103  satcat5::ptp::Interface* const m_iface;
104  satcat5::ip::Dispatch* const m_ip;
105  satcat5::ptp::Client* m_callback;
106 
107  // Internal configuration.
108  satcat5::eth::MacAddr m_reply_mac;
109  satcat5::eth::MacAddr m_stored_mac;
110  satcat5::eth::VlanTag m_reply_vtag;
111  satcat5::eth::VlanTag m_stored_vtag;
112  satcat5::ip::Addr m_reply_ip;
113  satcat5::ip::Addr m_stored_ip;
114  };
115  }
116 }
Host/Client for the Internet Group Management Protocol (IGMP).
Definition: igmp_client.h:108
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
Definition: ip_dispatch.h:43
An "OnDemand" object is polled only on request.
Definition: polling.h:131
Client for the IEEE 1588-2019 Precision Time Protocol (PTP)
Definition: ptp_client.h:67
Dispatch and formatting for L2 and L3 PTP messages.
Definition: ptp_dispatch.h:46
void ptp_callback(satcat5::ptp::Client *client)
Set the callback object for incoming messages.
Definition: ptp_dispatch.h:63
satcat5::ptp::Time ptp_tx_start()
Accessors for one-step and two-step timestamps.
Definition: ptp_dispatch.h:71
satcat5::ptp::Time ptp_tx_timestamp()
Accessors for one-step and two-step timestamps.
Definition: ptp_dispatch.h:73
void store_addr(const satcat5::eth::MacAddr &mac, const satcat5::ip::Addr &ip=satcat5::ip::ADDR_NONE, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Set the address for use with DispatchTo::STORED.
Definition: ptp_dispatch.cc:83
satcat5::ptp::Time ptp_rx_timestamp()
Accessors for one-step and two-step timestamps.
Definition: ptp_dispatch.h:75
Dispatch(satcat5::ptp::Interface *iface, satcat5::ip::Dispatch *ip)
Attach this object to the network port and IP stack.
Definition: ptp_dispatch.cc:19
void store_reply_addr()
Set the address for use with DispatchTo::STORED.
Definition: ptp_dispatch.cc:77
satcat5::io::Writeable * ptp_send(satcat5::ptp::DispatchTo where, unsigned num_bytes, u8 ptp_msg_type)
Send a PTP message to the designated address(es).
Definition: ptp_dispatch.cc:41
void poll_demand() override
Deferred event handler, called after request().
Definition: ptp_dispatch.cc:91
satcat5::ptp::Time ptp_time_now()
Accessors for one-step and two-step timestamps.
Definition: ptp_dispatch.h:69
Generic API for network ports that support PTP.
Definition: ptp_interface.h:27
virtual satcat5::ptp::Time ptp_rx_timestamp()=0
Return timestamp of the current incoming message.
virtual satcat5::ptp::Time ptp_tx_timestamp()=0
Return timestamp of the most recent outgoing message.
virtual satcat5::ptp::Time ptp_time_now()=0
Return the best available estimate of the current time.
virtual satcat5::ptp::Time ptp_tx_start()=0
Begin sending a timestamped message.
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
Type definitions for Ethernet frames and protocol handlers.
Core event-processing loop for SatCat5 software.
High-precision "Time" object for use with PTP / IEEE1588.
An Ethernet frame header.
Definition: eth_header.h:167
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119