SatCat5
ccsds_aos.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
31 
32 #pragma once
33 
34 #include <satcat5/crc16_checksum.h>
35 #include <satcat5/net_core.h>
36 #include <satcat5/pkt_buffer.h>
37 
38 namespace satcat5 {
39  namespace ccsds_aos {
40  // Constants and conversion functions for specific fields.
41  // (Refer to 732.0-B-4 Section 4.1 for details.)
42  constexpr u16 VERSION_MASK = 0xC000; // Transfer Frame Version Number
43  constexpr u16 SCID_MASK = 0x3FC0; // Spacecraft Identifier (SCID)
44  constexpr u16 VCID_MASK = 0x003F; // Virtual Channel ID (VCID)
45  constexpr u8 REPLAY_MASK = 0x80; // Replay flag
46  constexpr u8 FRCT_EXT_MASK = 0x40; // Extended frame-count enable?
47  constexpr u8 RSVD_MASK = 0x30; // Reserved (zeros)
48  constexpr u8 FRCT_VAL_MASK = 0x0F; // Extended frame-count value
49  constexpr u16 VERSION_2 = (1 << 14);
50 
52  constexpr u32 TM_SYNC_WORD = 0x1ACFFC1D;
53  constexpr u8 TM_SYNC_BYTES[] = {0x1A, 0xCF, 0xFC, 0x1D};
54 
55  // Special IDs for virtual channels:
56  constexpr u16 VCID_DEFAULT = 0x0000; // Default channel
57  constexpr u16 VCID_IDLE = 0x003F; // Virtual channel: Only idle data (OID)
58 
62  struct Header {
63  u16 id;
64  u8 signal;
65  u32 count;
66 
68  constexpr Header()
69  : id(0), signal(0), count(0) {}
70  Header(const Header& t) = default;
71  Header& operator=(const Header& t) = default;
73  constexpr Header(u8 scid, u8 vcid)
74  : id(VERSION_2 | pack_scid(scid) | pack_vcid(vcid))
75  , signal(FRCT_EXT_MASK), count(0) {}
76 
78  static constexpr u32 pack_scid(u8 scid)
79  { return (u16(scid) << 6) & SCID_MASK; }
81  static constexpr u32 pack_vcid(u8 vcid)
82  { return u16(vcid) & VCID_MASK; }
83 
84  // Convenience accessors.
85  u16 version() const
86  { return id & VERSION_MASK; }
87  u8 scid() const
88  { return u8((id & SCID_MASK) >> 6); }
89  u8 vcid() const
90  { return u8(id & VCID_MASK); }
91  bool replay() const
92  { return !!(signal & REPLAY_MASK); }
93 
95  void write_to(satcat5::io::Writeable* wr) const;
99  Header& operator++();
100  };
101 
109  class Channel final
111  , public satcat5::net::Protocol {
112  public:
115  Channel(
116  satcat5::ccsds_aos::Dispatch* iface, // Parent interface
117  satcat5::io::Readable* src, // Outgoing data buffer
118  satcat5::io::Writeable* dst, // Incoming data buffer
119  u8 scid, u8 vcid, bool pkt); // ID and mode-select
120  ~Channel() SATCAT5_OPTIONAL_DTOR;
121 
123  void desync();
124 
126  void frame_rcvd(satcat5::io::LimitedRead& src) override;
127 
132  void set_wildcard();
133 
134  protected:
135  // Required event handlers.
136  void data_rcvd(satcat5::io::Readable* src) override;
137  void data_unlink(satcat5::io::Readable* src) override;
138  u8 idle_filler(satcat5::io::Writeable* dst, unsigned req);
139  bool read_header(satcat5::io::Readable* src);
140 
141  // Internal state.
142  enum class State {RAW, RESYNC, HEADER, DATA, SKIP};
143  satcat5::ccsds_aos::Dispatch* const m_iface;
144  satcat5::io::Readable* m_src;
145  satcat5::io::Writeable* const m_dst;
146  satcat5::io::ArrayWrite m_rx_spp;
147  satcat5::ccsds_aos::Header m_rx_next;
148  satcat5::ccsds_aos::Header m_tx_next;
149  satcat5::ccsds_aos::Channel::State m_rx_state;
150  unsigned m_rx_rem;
151  u8 m_tx_busy;
152  u8 m_tx_irem;
153  u16 m_tx_iseq;
154  u8 m_rx_tmp[6];
155  };
156 
159  class Dispatch
161  , public satcat5::net::Dispatch {
162  public:
169  Dispatch(
170  satcat5::io::Readable* src, // Receive interface
171  satcat5::io::Writeable* dst, // Transmit interface
172  u8* buff, unsigned dsize, // Rx working buffer
173  bool insert_sync = false); // Insert TM sync word?
174  ~Dispatch() SATCAT5_OPTIONAL_DTOR;
175 
178  const satcat5::ccsds_aos::Header& hdr);
179 
182  { return satcat5::io::TrafficStats::query(m_crc_rx, m_crc_tx); }
183 
184  // Other accessors.
185  inline unsigned dsize() const
186  { return m_dsize; }
187  inline void error_incr()
188  { m_crc_rx.error_incr(); }
189  inline unsigned tsize() const
190  { return m_dsize + 6;}
191  inline const satcat5::ccsds_aos::Header& rcvd_hdr() const
192  { return m_rcvd_hdr; }
193 
194  protected:
195  // Required event handlers.
196  void data_rcvd(satcat5::io::Readable* src) override;
197  void data_unlink(satcat5::io::Readable* src) override;
198  bool read_sync(satcat5::io::Readable* src);
199  bool read_data(satcat5::io::Readable* src);
200 
201  // Stub required for the Dispatch API (not supported).
203  const satcat5::net::Type& type, unsigned len) override;
204 
205  // Synchronization and parser state.
206  const unsigned m_dsize;
207  const bool m_insert;
208  u8 m_sync_state;
209 
210  // Sink and source objects for the parent interface.
211  satcat5::io::Readable* m_src;
212  satcat5::io::Writeable* const m_dst;
214  satcat5::crc16::XmodemRx m_crc_rx;
215  satcat5::crc16::XmodemTx m_crc_tx;
216 
217  // Store most recent received header parameters.
218  satcat5::ccsds_aos::Header m_rcvd_hdr;
219  };
220 
223  template<unsigned DSIZE=251>
225  public:
227  satcat5::io::Readable* src, // Input interface
228  satcat5::io::Writeable* dst, // Output interface
229  bool insert_sync = false) // Already packetized?
230  : Dispatch(src, dst, m_raw, DSIZE, insert_sync), m_raw{} {}
231  private:
232  u8 m_raw[DSIZE+6]; // Working buffer
233  };
234 
235  }
236 }
constexpr u32 TM_SYNC_WORD
Sync-word for CCSDS "TM Synchronization and Channel Coding".
Definition: ccsds_aos.h:52
CCSDS-AOS Virtual Channel.
Definition: ccsds_aos.h:111
void frame_rcvd(satcat5::io::LimitedRead &src) override
Process each received AOS frame.
Definition: ccsds_aos.cc:118
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: ccsds_aos.cc:179
Channel(satcat5::ccsds_aos::Dispatch *iface, satcat5::io::Readable *src, satcat5::io::Writeable *dst, u8 scid, u8 vcid, bool pkt)
Create a virtual channel, bound to an AOS interface.
Definition: ccsds_aos.cc:68
void data_unlink(satcat5::io::Readable *src) override
Unlink this EventListener from the designated source, because the designated Readable object is being...
Definition: ccsds_aos.cc:233
void set_wildcard()
Enable wildcard mode, ignoring incoming SCID and VCID.
Definition: ccsds_aos.cc:110
void desync()
Force resynchronization after an error.
Definition: ccsds_aos.cc:97
Implemention of "net::Dispatch" API for CCSDS-AOS protocol.
Definition: ccsds_aos.h:161
satcat5::io::TrafficStats stats()
Report packet statistics since the previous query.
Definition: ccsds_aos.h:181
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: ccsds_aos.cc:325
satcat5::io::Writeable * open_write(const satcat5::ccsds_aos::Header &hdr)
Write CCSDS-AOS frame header and get Writeable object.
Definition: ccsds_aos.cc:309
void data_unlink(satcat5::io::Readable *src) override
Unlink this EventListener from the designated source, because the designated Readable object is being...
Definition: ccsds_aos.cc:347
satcat5::io::Writeable * open_reply(const satcat5::net::Type &type, unsigned len) override
Open a reply to the sender of the most recent message by writing frame header(s) and returning a stre...
Definition: ccsds_aos.cc:307
unsigned tsize() const
< Total size = Header + Data
Definition: ccsds_aos.h:189
unsigned dsize() const
< Data field size
Definition: ccsds_aos.h:185
const satcat5::ccsds_aos::Header & rcvd_hdr() const
Most recent received header.
Definition: ccsds_aos.h:191
Dispatch with a statically-allocated working buffer.
Definition: ccsds_aos.h:224
Check and remove FCS from each incoming frame ("XMODEM" variant).
Append FCS to each outgoing frame ("XMODEM" variant).
Ephemeral Writeable interface for a simple array.
Definition: io_writeable.h:127
void error_incr()
Increment the internal error counter.
Definition: io_checksum.h:118
Event-handler interface for newly received data.
Definition: io_readable.h:43
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
Dispatch()
Constructor and destructor access restricted to children.
Definition: net_dispatch.h:68
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Inline CRC16 Checksum insertion and verification.
Generic network Dispatch API.
CCSDS-AOS transfer frame header.
Definition: ccsds_aos.h:62
void write_to(satcat5::io::Writeable *wr) const
Write this header to a stream.
Definition: ccsds_aos.cc:43
u16 version() const
< Transfer frame version number
Definition: ccsds_aos.h:85
static constexpr u32 pack_scid(u8 scid)
Convert raw SCID to the internal packed format.
Definition: ccsds_aos.h:78
u16 id
Spaceraft ID + Virtual Channel ID.
Definition: ccsds_aos.h:63
bool replay() const
< Replay flag?
Definition: ccsds_aos.h:91
u8 signal
Signaling field.
Definition: ccsds_aos.h:64
constexpr Header(u8 scid, u8 vcid)
Constructor with known SCID and VCID.
Definition: ccsds_aos.h:73
bool read_from(satcat5::io::Readable *rd)
Read from stream, populating header fields.
Definition: ccsds_aos.cc:52
static constexpr u32 pack_vcid(u8 vcid)
Convert raw VCID to the internal packed format.
Definition: ccsds_aos.h:81
constexpr Header()
Basic constructor sets frame count to zero.
Definition: ccsds_aos.h:68
u8 scid() const
< Spacecraft ID
Definition: ccsds_aos.h:87
Header & operator++()
Increment the Virtual Channel Frame Count.
Definition: ccsds_aos.cc:62
u32 count
Virtual Channel Frame Count.
Definition: ccsds_aos.h:65
u8 vcid() const
< Virtual Channel ID
Definition: ccsds_aos.h:89
Data structure for combined transmit and receive statistics.
Definition: io_counter.h:94
static TrafficStats query(satcat5::io::Counter &rx, satcat5::io::Counter &tx)
Read updated statistics from a transmit/receive pair.
Definition: io_counter.cc:58
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38