SatCat5
ptp_measurement.cc
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/log.h>
8 #include <satcat5/utils.h>
9 
14 using satcat5::ptp::Time;
17 
18 bool Measurement::done() const {
19  return (t1 != TIME_ZERO)
20  && (t2 != TIME_ZERO)
21  && (t3 != TIME_ZERO)
22  && (t4 != TIME_ZERO);
23 }
24 
26  wr.wr_str("\r\n t1"); t1.log_to(wr);
27  wr.wr_str("\r\n t2"); t2.log_to(wr);
28  wr.wr_str("\r\n t3"); t3.log_to(wr);
29  wr.wr_str("\r\n t4"); t4.log_to(wr);
30 }
31 
32 bool Measurement::match(const Header& hdr, const PortId& port) const {
33  // Follow guidelines from Section 10.2.1 and Section 10.3.1.
34  // (Caller provides either sourcePortIdentity or requestingPortIdentity.)
35  return (ref.domain == hdr.domain)
36  && (ref.sdo_id == hdr.sdo_id)
37  && (ref.seq_id == hdr.seq_id)
38  && (ref.src_port == port);
39 }
40 
42  // See also: Section 11.3.1.
43  return ((t2 - t1) + (t4 - t3)) / 2;
44 }
45 
47  // See also: Section 11.4.2.
48  return (t4 - t1) / 2;
49 }
50 
52  // See also: Section 11.2.
53  return ((t2 - t1) + (t3 - t4)) / 2;
54 }
55 
56 void Measurement::reset(const Header& hdr) {
57  ref = hdr;
58  t1 = TIME_ZERO;
59  t2 = TIME_ZERO;
60  t3 = TIME_ZERO;
61  t4 = TIME_ZERO;
62 }
63 
64 Measurement* MeasurementCache::find(const Header& hdr, const PortId& port) {
65  // Cache size is small (2-8 typ), so direct linear search is fine.
66  for (unsigned a = 0 ; a < SATCAT5_PTP_CACHE_SIZE ; ++a) {
67  if (m_buff[a].match(hdr, port)) return m_buff + a;
68  }
69  return 0; // No match
70 }
71 
73  Measurement* next = m_buff + m_next_wr;
74  m_next_wr = modulo_add_uns(m_next_wr + 1, SATCAT5_PTP_CACHE_SIZE);
75  next->reset(hdr);
76  return next;
77 }
Internal buffer used by the Log class.
Definition: log.h:134
void wr_str(const char *str)
Write a null-terminated UTF-8 string.
Definition: log.cc:297
Searchable cache of recent Measurement objects.
satcat5::ptp::Measurement * find(const satcat5::ptp::Header &hdr, const satcat5::ptp::PortId &port)
Find the first matching measurement in the cache.
satcat5::ptp::Measurement * push(const satcat5::ptp::Header &hdr)
Create a new measurement, overwriting the oldest.
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
void log_to(satcat5::log::LogBuffer &wr) const
User-readable format for logging.
Definition: ptp_time.cc:83
Diagnostic logging to UART and/or Ethernet ports.
Define the data structure for a two-way time transfer handshake.
constexpr satcat5::ptp::Time TIME_ZERO(0LL)
Common time-related constants.
Struct representing the PTP header used for all message types.
Definition: ptp_header.h:46
satcat5::ptp::PortId src_port
sourcePortIdentity
Definition: ptp_header.h:56
u16 sdo_id
majorSdoId + minorSdoId
Definition: ptp_header.h:52
u8 domain
domainNumber
Definition: ptp_header.h:51
u16 seq_id
sequenceId
Definition: ptp_header.h:57
Timestamps and metadata for a two-way time-transfer handshake.
satcat5::ptp::Time t1
Timestamp T1 (A to B / Tx)
bool match(const satcat5::ptp::Header &hdr, const satcat5::ptp::PortId &port) const
Check if an incoming message matches this exchange.
void log_to(satcat5::log::LogBuffer &wr) const
Write all four timestamps to the log.
void reset(const satcat5::ptp::Header &hdr)
Reset this data structure for immediate reuse.
satcat5::ptp::Time mean_link_delay() const
Calculate PTP "meanLinkDelay".
satcat5::ptp::Header ref
Reference header is copied from the initiating PTP message (i.e., SYNC or PDELAY_REQ) and used to mat...
satcat5::ptp::Time mean_path_delay() const
Calculate PTP "meanPathDelay".
satcat5::ptp::Time t2
Timestamp T2 (A to B / Rx)
satcat5::ptp::Time t4
Timestamp T4 (B to A / Rx)
satcat5::ptp::Time t3
Timestamp T3 (B to A / Tx)
satcat5::ptp::Time offset_from_master() const
Calculate PTP "offsetFromMaster".
Struct used for sourcePortIdentity and requestingPortIdentity.
Definition: ptp_header.h:17
Miscellaneous mathematical utility functions.
constexpr unsigned modulo_add_uns(unsigned sum, unsigned m)
Modulo addition function.
Definition: utils.h:182