SatCat5
ptp_tlv.cc
1 // Copyright 2024 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/ptp_client.h>
7 #include <satcat5/ptp_tlv.h>
8 
12 
13 bool TlvHeader::match(const TlvHeader& other) const
14 {
15  return (type == other.type)
16  && (org_id == other.org_id)
17  && (org_sub == other.org_sub);
18 }
19 
21 {
22  // See IEEE 1588-2019, Section 14.2.2, Table 52.
23  if (type < 0x0008) return false; // 0x0000 - 0x0007
24  if (type < 0x000A) return true; // 0x0008 - 0x0009
25  if (type < 0x4000) return false; // 0x000A - 0x3FFF
26  if (type < 0x8000) return true; // 0x4000 - 0x7FFF
27  return false; // 0x8000 - 0xFFFF
28 }
29 
31 {
32  wr->write_u16(type);
33  if (org_id || org_sub) {
34  wr->write_u16(length + 6);
35  wr->write_u24(org_id);
36  wr->write_u24(org_sub);
37  } else {
38  wr->write_u16(length);
39  }
40 }
41 
43 {
44  // Reset all fields.
45  *this = TLV_HEADER_NONE;
46  // Read and sanity-check the basic header.
47  if (rd->get_read_ready() < 4) return false;
48  type = rd->read_u16();
49  length = rd->read_u16();
50  if (rd->get_read_ready() < length) return false;
51  // Is this tlvType a valid organization extension?
52  bool type_org =
53  type == TLVTYPE_ORG_EXT ||
56  if (type_org && length < 6) return false;
57  // Read the organization sub-header, if applicable.
58  if (type_org) {
59  org_id = rd->read_u24();
60  org_sub = rd->read_u24();
61  length -= 6;
62  }
63  return true;
64 }
65 
67  : m_client(client)
68  , m_next(0)
69 {
70  if (m_client) m_client->m_tlv_list.add(this);
71 }
72 
73 TlvHandler::~TlvHandler()
74 {
75  if (m_client) m_client->m_tlv_list.remove(this);
76 }
77 
79  const satcat5::ptp::Header& hdr,
80  const satcat5::ptp::TlvHeader& tlv,
82 {
83  // Default handler does nothing. (No tags to read.)
84  return false;
85 }
86 
88  const satcat5::ptp::Header& hdr,
90 {
91  // Default handler does nothing. (No tags to write.)
92  return 0;
93 }
94 
96 {
97  // Default handler does nothing. (No need to adjust timestamps.)
98 }
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Client for the IEEE 1588-2019 Precision Time Protocol (PTP)
Definition: ptp_client.h:67
Users should derive custom TLV objects from this base class.
Definition: ptp_tlv.h:111
satcat5::ptp::Client *const m_client
Pointer to the associated PTP client.
Definition: ptp_tlv.h:141
virtual unsigned tlv_send(const satcat5::ptp::Header &hdr, satcat5::io::Writeable *wr)
Child class SHOULD override this method to append outgoing TLV(s).
Definition: ptp_tlv.cc:87
virtual bool tlv_rcvd(const satcat5::ptp::Header &hdr, const satcat5::ptp::TlvHeader &tlv, satcat5::io::LimitedRead &rd)
Child class SHOULD override this method to read incoming TLV(s).
Definition: ptp_tlv.cc:78
TlvHandler(satcat5::ptp::Client *client)
Only children should create or destroy the base class.
Definition: ptp_tlv.cc:66
virtual void tlv_meas(satcat5::ptp::Measurement &meas)
Child class MAY override this method to read or modify each complete two-way handshake event.
Definition: ptp_tlv.cc:95
void add(T *item)
Add new item to front or back, whichever is simpler.
Definition: list.h:221
void remove(T *item)
Remove the designated item from the list.
Definition: list.h:277
TLV metadata for the IEEE 1588-2019 Precision Time Protocol (PTP).
constexpr u16 TLVTYPE_ORG_EXT
Define some key tlvType values.
Definition: ptp_tlv.h:58
constexpr u16 TLVTYPE_ORG_EXT_P
Define some key tlvType values.
Definition: ptp_tlv.h:61
constexpr u16 TLVTYPE_ORG_EXT_NP
Define some key tlvType values.
Definition: ptp_tlv.h:62
Struct representing the PTP header used for all message types.
Definition: ptp_header.h:46
Timestamps and metadata for a two-way time-transfer handshake.
Data structure for identifying TLV headers.
Definition: ptp_tlv.h:70
bool propagate() const
When attached to ANNOUNCE messages, certain TLVs are required to propagate across boundary clocks,...
Definition: ptp_tlv.cc:20
void write_to(satcat5::io::Writeable *wr) const
I/O functions read or write the TLV header only.
Definition: ptp_tlv.cc:30
u16 type
Access to the raw header fields Note: This "length" field always reflects the user data length,...
Definition: ptp_tlv.h:74
bool read_from(satcat5::io::Readable *rd)
I/O functions read or write the TLV header only.
Definition: ptp_tlv.cc:42