7 #include <hal_test/eth_interface.h>
22 static constexpr
unsigned DEBUG_VERBOSE = 0;
24 EthernetInterface::EthernetInterface(
Writeable* pcap)
25 : satcat5::ptp::Interface()
33 , m_time_rx(TIME_ZERO)
34 , m_time_tx0(TIME_ZERO)
35 , m_time_tx1(TIME_ZERO)
39 , m_support_one_step(true)
50 m_txbuff_data = &dst->m_rxbuff_data;
51 m_txbuff_time = &dst->m_rxbuff_time;
57 if (DEBUG_VERBOSE > 1)
Log(DEBUG,
"EthInterface::ptp_tx_start");
58 m_time_tx0 = m_support_one_step ?
ptp_time_now() : TIME_ZERO;
69 Readable::set_callback(callback);
74 if (DEBUG_VERBOSE > 1)
Log(DEBUG,
"EthInterface::read_finalize");
81 m_time_rx = TIME_ZERO;
87 if (DEBUG_VERBOSE > 1)
Log(DEBUG,
"EthInterface::write_finalize");
91 if (m_loss_threshold == UINT32_MAX) {
94 }
else if (m_loss_threshold > 0) {
102 if (DEBUG_VERBOSE > 0)
Log(DEBUG,
"EthInterface: Dropped packet.");
103 ArrayWrite::write_abort();
109 if (ArrayWrite::write_finalize()) {
114 m_time_tx1 = (m_time_tx0 == TIME_ZERO) ?
ptp_time_now() : m_time_tx0;
115 m_time_tx0 = TIME_ZERO;
118 while (wrlen < m_zero_pad) m_txbuff[wrlen++] = 0;
134 if (desync)
Log(satcat5::log::CRITICAL,
"EthInterface: Desync");
137 if (DEBUG_VERBOSE > 0)
Log(DEBUG,
"EthInterface: Write overflow.");
147 m_loss_threshold = 0;
148 }
else if (rate < 1.0f) {
149 m_loss_threshold = (u32)(rate * (
float)UINT32_MAX);
151 m_loss_threshold = UINT32_MAX;
157 if (DEBUG_VERBOSE > 1)
Log(DEBUG,
"EthInterface::data_rcvd");
167 if (DEBUG_VERBOSE > 0)
Log(DEBUG,
"EthInterface: Received PTP.");
172 if (DEBUG_VERBOSE > 0)
Log(DEBUG,
"EthInterface: Received Non-PTP.");
173 ReadableRedirect::read_notify();
177 void EthernetInterface::read_begin_packet()
181 if (m_time_rx != TIME_ZERO)
return;
199 int errcode = clock_gettime(CLOCK_MONOTONIC, &tv);
202 const u64 SCALE = 1000000000ull / CLOCKS_PER_SEC;
203 return Time(SCALE * clock());
206 return Time(tv.tv_sec, tv.tv_nsec);
212 {
return m_time_tx1; }
214 {
return m_time_rx; }
Ephemeral Writeable interface for a simple array.
unsigned written_len() const
Report total length after write_finalize() is called.
Event-handler interface for newly received data.
const u8 * peek(unsigned nbytes) const
Peek nbytes into the circular buffer.
unsigned get_peek_ready() const
Find the longest available contiguous segment that can be requested by peek().
unsigned get_read_ready() const override
How many bytes can be read without blocking?
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
Abstract API for reading byte-streams and packets.
bool read_obj(T &t)
Templated wrapper for any object with the following method: bool read_from(satcat5::io::Readable* rd)...
virtual void set_callback(satcat5::io::EventListener *callback)
Update registered callback for data_rcvd() events.
Wrapper class for forwarding reads to another object.
Abstract API for writing byte-streams and packets.
virtual void write_bytes(unsigned nbytes, const void *src)
Write 0 or more bytes from a buffer.
virtual bool write_finalize()
Mark end of frame and release temporary working data.
void write_obj(const T &obj)
Templated wrapper for any object with the following method: void write_to(satcat5::io::Writeable* wr)...
The Log class creates and formats one log message.
bool ptp_dispatch(const u8 *peek, unsigned length)
Determine if an incoming packet is a PTP message.
void ptp_notify_now()
Notify the PTP callback object in immediate mode.
High-precision timestamp for use with PTP / IEEE1588.
Simulation of a PTP-compatible Ethernet interface controller.
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_loss_rate(float rate)
Set rate for randomized drops of outgoing packets.
void connect(satcat5::test::EthernetInterface *dst)
Crosslink to specified destination object.
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
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.
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.
Diagnostic logging to UART and/or Ethernet ports.
constexpr s8 DEBUG
Define basic priority codes for log messages.
constexpr satcat5::ptp::Time TIME_ZERO(0LL)
Common time-related constants.
Miscellaneous simulation and test helper functions.
u32 rand_u32()
Reproducible PRNG used for unit tests.