SatCat5
eth_interface.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 // Simulated point-to-point network interface with PTP compatibility
6 
7 #pragma once
8 
10 #include <satcat5/io_readable.h>
11 #include <satcat5/io_writeable.h>
12 #include <satcat5/ptp_interface.h>
13 #include <satcat5/ptp_time.h>
14 
15 namespace satcat5 {
16  namespace test {
25  {
26  public:
29 
32 
34  inline void support_one_step(bool en)
35  { m_support_one_step = en; }
36 
37  // Required API for Precision Time Protocol (ptp::Interface)
44 
45  // Override specific new-packet and end-of-packet notifications
46  // so we can add timestamp metadata as needed.
47  void set_callback(satcat5::io::EventListener* callback) override;
48  void read_finalize() override;
49  bool write_finalize() override;
50 
52  void set_loss_rate(float rate);
53 
55  inline void set_zero_pad(unsigned len) {m_zero_pad = len;}
56 
58  inline unsigned tx_count() const { return m_tx_count; }
60  inline unsigned rx_count() const { return m_rx_count; }
61 
62  protected:
63  // Event handler for new-packet notifications.
64  void data_rcvd(satcat5::io::Readable* src) override;
65 
66  // Update internal state at start of each packet.
67  void read_begin_packet();
68 
69  // Internal state.
70  satcat5::io::Writeable* m_txpcap;
71  satcat5::io::Writeable* m_txbuff_data;
72  satcat5::io::Writeable* m_txbuff_time;
73  satcat5::io::PacketBufferHeap m_rxbuff_data;
74  satcat5::io::PacketBufferHeap m_rxbuff_time;
75  satcat5::ptp::Time m_time_rx;
76  satcat5::ptp::Time m_time_tx0;
77  satcat5::ptp::Time m_time_tx1;
78  unsigned m_tx_count;
79  unsigned m_rx_count;
80  unsigned m_zero_pad;
81  bool m_support_one_step;
82  u32 m_loss_threshold;
83 
84  // Working buffer for cloning incoming data.
85  // (Large enough for a full-size Ethernet packet.)
86  u8 m_txbuff[1600];
87  };
88  }
89 }
Ephemeral Writeable interface for a simple array.
Definition: io_writeable.h:127
Event-handler interface for newly received data.
Definition: io_readable.h:43
PacketBuffer with heap allocation (in packet mode).
Definition: posix_utils.h:128
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Wrapper class for forwarding reads to another object.
Definition: io_readable.h:299
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Generic API for network ports that support PTP.
Definition: ptp_interface.h:27
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
Simulation of a PTP-compatible Ethernet interface controller.
Definition: eth_interface.h:25
satcat5::io::Writeable * ptp_tx_write() override
Return an object suitable for writing the next PTP frame.
satcat5::ptp::Time ptp_time_now() override
Return the best available estimate of the current time.
void set_zero_pad(unsigned len)
Set minimum frame length. Runt frames are zero-padded.
Definition: eth_interface.h:55
unsigned rx_count() const
Count packets received.
Definition: eth_interface.h:60
void set_loss_rate(float rate)
Set rate for randomized drops of outgoing packets.
EthernetInterface(satcat5::io::Writeable *pcap)
Create a simulated interface, with optional packet-capture.
void connect(satcat5::test::EthernetInterface *dst)
Crosslink to specified destination object.
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
unsigned tx_count() const
Count packets sent.
Definition: eth_interface.h:58
satcat5::ptp::Time ptp_rx_timestamp() override
Return timestamp of the current incoming message.
satcat5::ptp::Time ptp_tx_timestamp() override
Return timestamp of the most recent outgoing message.
void set_callback(satcat5::io::EventListener *callback) override
Update registered callback for data_rcvd() events.
void support_one_step(bool en)
Enable or disable support for one-step timestamps.
Definition: eth_interface.h:34
bool write_finalize() override
Mark end of frame and release temporary working data.
satcat5::ptp::Time ptp_tx_start() override
Begin sending a timestamped message.
satcat5::io::Readable * ptp_rx_read() override
Return an object suitable for reading the next PTP frame.
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
"Readable" I/O interface core definitions
"Writeable" I/O interface core definitions
Miscellaneous POSIX wrappers (e.g., heap allocation, log to console...)
High-precision "Time" object for use with PTP / IEEE1588.