SatCat5
ntp_header.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>
7 #include <satcat5/ntp_header.h>
8 
10 
11 void Header::log_to(satcat5::log::LogBuffer& wr) const {
12  wr.wr_str("\r\n LI: "); wr.wr_dec((lvm & LI_MASK) >> 6);
13  wr.wr_str("\r\n VN: "); wr.wr_dec((lvm & VN_MASK) >> 3);
14  wr.wr_str("\r\n Mode: "); wr.wr_dec(lvm & MODE_MASK);
15  wr.wr_str("\r\n Stratum: "); wr.wr_dec((u32)stratum);
16  wr.wr_str("\r\n Poll: "); wr.wr_dec((s32)poll);
17  wr.wr_str("\r\n Prec: "); wr.wr_dec((s32)precision);
18  wr.wr_str("\r\n RtDelay: 0x"); wr.wr_h32(rootdelay);
19  wr.wr_str("\r\n RtDisp: 0x"); wr.wr_h32(rootdisp);
20  wr.wr_str("\r\n RefID: 0x"); wr.wr_h32(refid);
21  wr.wr_str("\r\n RefTime: 0x"); wr.wr_h64(ref);
22  wr.wr_str("\r\n OrgTime: 0x"); wr.wr_h64(org);
23  wr.wr_str("\r\n RecTime: 0x"); wr.wr_h64(rec);
24  wr.wr_str("\r\n XmtTime: 0x"); wr.wr_h64(xmt);
25 }
26 
28  if (rd->get_read_ready() >= HEADER_LEN) {
29  lvm = rd->read_u8();
30  stratum = rd->read_u8();
31  poll = rd->read_s8();
32  precision = rd->read_s8();
33  rootdelay = rd->read_u32();
34  rootdisp = rd->read_u32();
35  refid = rd->read_u32();
36  ref = rd->read_u64();
37  org = rd->read_u64();
38  rec = rd->read_u64();
39  xmt = rd->read_u64();
40  return true;
41  } else {
42  return false;
43  }
44 }
45 
47  wr->write_u8(lvm);
48  wr->write_u8(stratum);
49  wr->write_s8(poll);
50  wr->write_s8(precision);
51  wr->write_u32(rootdelay);
52  wr->write_u32(rootdisp);
53  wr->write_u32(refid);
54  wr->write_u64(ref);
55  wr->write_u64(org);
56  wr->write_u64(rec);
57  wr->write_u64(xmt);
58 }
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
u8 read_u8()
One of many functions for reading integer/floating point values, see details.
Definition: io_readable.cc:44
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
void write_u8(u8 data)
One of many functions for writing integer/floating point values, see details.
Definition: io_writeable.cc:20
Internal buffer used by the Log class.
Definition: log.h:134
void wr_h32(u32 val, unsigned nhex=8)
Write an integer (u32) in hexadecimal format.
Definition: log.cc:303
void wr_str(const char *str)
Write a null-terminated UTF-8 string.
Definition: log.cc:297
void wr_h64(u64 val, unsigned nhex=16)
Write an integer (u64) in hexadecimal format.
Definition: log.cc:310
void wr_dec(u32 val)
Legacy alias for wr_d32.
Definition: log.h:166
Diagnostic logging to UART and/or Ethernet ports.
Message headers for the Network Time Protocol (NTP / IETF RFC-5905).
Definition: ntp_header.h:20
u64 org
T1 (Client transmit time)
Definition: ntp_header.h:30
void write_to(satcat5::io::Writeable *wr) const
Write this header to a data sink.
Definition: ntp_header.cc:46
static constexpr u8 VN_MASK
Version number is always 4.
Definition: ntp_header.h:47
u64 xmt
T3 (Server transmit time)
Definition: ntp_header.h:32
static constexpr unsigned HEADER_LEN
The basic header is exactly 12 words = 48 bytes long.
Definition: ntp_header.h:35
static constexpr u8 LI_MASK
Leap second indictor (LI) for last minute of current day.
Definition: ntp_header.h:39
u64 ref
Time of last sync to parent.
Definition: ntp_header.h:29
u32 rootdelay
Round trip delay to grandmaster.
Definition: ntp_header.h:26
bool read_from(satcat5::io::Readable *rd)
Read this header from a data source.
Definition: ntp_header.cc:27
u32 rootdisp
Total dispersion to grandmaster.
Definition: ntp_header.h:27
u8 stratum
Hops to grandmaster (1-15)
Definition: ntp_header.h:23
s8 precision
Precision = 2^N seconds.
Definition: ntp_header.h:25
u64 rec
T2 (Server receive time)
Definition: ntp_header.h:31
u32 refid
Server-ID or KoD code.
Definition: ntp_header.h:28
static constexpr u8 MODE_MASK
Mode number indicates client or server role.
Definition: ntp_header.h:53
s8 poll
Interval = 2^N seconds.
Definition: ntp_header.h:24
u8 lvm
Combined LI + VN + Mode.
Definition: ntp_header.h:22