SatCat5
pkt_buffer.h
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Multipurpose circular buffer
6 //
7 
8 #pragma once
9 
10 #include <satcat5/io_core.h>
11 #include <satcat5/io_counter.h>
12 #include <satcat5/types.h>
13 
14 // Default size is large enough for one full-size Ethernet frame + metadata.
15 #ifndef SATCAT5_DEFAULT_PKTBUFF
16 #define SATCAT5_DEFAULT_PKTBUFF 1600 // Buffer size in bytes
17 #endif
18 
19 namespace satcat5 {
20  namespace io {
63  : public satcat5::io::Writeable
64  , public satcat5::io::Readable
65  , public satcat5::io::Counter
66  {
67  public:
70  constexpr PacketBuffer(void* buff, unsigned nbytes, unsigned max_pkt=0)
71  : m_buff(static_cast<u8*>(buff) + 2*max_pkt)
72  , m_buff_size((buff && nbytes >= 2*max_pkt) ? (nbytes - 2*max_pkt) : 0)
73  , m_buff_rdidx(0)
74  , m_buff_rdcount(0)
75  , m_pkt_lbuff(static_cast<u16*>(buff))
76  , m_pkt_maxct(max_pkt)
77  , m_pkt_rdidx(0)
78  , m_total_bytes(0)
79  , m_total_frames(0)
80  , m_next_wrpos(0)
81  , m_next_wrlen(0)
82  , m_shared_rdavail(0)
84  {} // No further initialization required
85 
88  void clear();
89 
90  // Implement the io::Countable API.
91  unsigned byte_count(bool reset = true) override;
92  unsigned frame_count(bool reset = true) override;
93 
95  u8 get_percent_full() const;
96 
98  unsigned get_write_partial() const;
99 
100  // io::Writeable function implementations.
101  unsigned get_write_space() const override;
102  void write_bytes(unsigned nbytes, const void* src) override;
103  void write_abort() override;
104  bool write_finalize() override;
105 
112  void zcw_write(unsigned nbytes);
113  unsigned zcw_maxlen() const;
114  u8* zcw_start();
115 
116  // io::Readable function implementations.
117  unsigned get_read_ready() const override;
118  bool read_bytes(unsigned nbytes, void* dst) override;
119  bool read_consume(unsigned nbytes) override;
120  void read_finalize() override;
121 
128  const u8* peek(unsigned nbytes) const;
129 
132  unsigned get_peek_ready() const;
133 
137  inline u8* get_buff_dtor() const {return (u8*)m_pkt_lbuff;}
138  inline unsigned get_buff_size() const {return m_buff_size;}
140 
141  protected:
142  // Internal functions required for Writeable and Readable APIs.
143  void write_next(u8 data) override;
144  void write_overflow() override;
145  u8 read_next() override;
146  bool can_read_internal(unsigned nbytes) const;
147  void consume_internal(unsigned nbytes);
148 
149  private:
152  u8* const m_buff; // Pointer to backing array
153  const unsigned m_buff_size; // Size of backing array
154  unsigned m_buff_rdidx; // Current read position
155  unsigned m_buff_rdcount; // Count consumed bytes
157 
160  u16* const m_pkt_lbuff; // Pointer to backing array
161  const unsigned m_pkt_maxct; // Size of backing array
162  unsigned m_pkt_rdidx; // Current read position
164 
167  unsigned m_total_bytes;
168  unsigned m_total_frames;
170 
173  unsigned m_next_wrpos; // Base position for writes
174  unsigned m_next_wrlen; // Working packet length
176 
179  volatile unsigned m_shared_rdavail;
180  volatile unsigned m_shared_pktcount;
182  };
183 
197  template<unsigned SIZE = SATCAT5_DEFAULT_PKTBUFF>
199  public:
202  explicit PacketBufferStatic(unsigned max_pkt = 32)
203  : PacketBuffer(m_raw, SIZE, max_pkt), m_raw{} {}
204  private:
205  u8 m_raw[SIZE];
206  };
207 
222  template<unsigned SIZE = SATCAT5_DEFAULT_PKTBUFF>
224  public:
227  : PacketBuffer(m_raw, SIZE, 0), m_raw{} {}
228  private:
229  u8 m_raw[SIZE];
230  };
231  }
232 }
Define a generic API for reporting traffic statistics.
Definition: io_counter.h:26
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
Definition: pkt_buffer.h:66
const u8 * peek(unsigned nbytes) const
Peek nbytes into the circular buffer.
Definition: pkt_buffer.cc:248
void clear()
Reset buffer contents.
Definition: pkt_buffer.cc:22
unsigned m_next_wrpos
Working state for writes (write domain)
Definition: pkt_buffer.h:173
unsigned frame_count(bool reset=true) override
Frames transferred since the last query.
Definition: pkt_buffer.cc:37
unsigned m_total_bytes
Cumulative total-bytes and total-frames statistics counters.
Definition: pkt_buffer.h:167
unsigned byte_count(bool reset=true) override
Bytes transferred since the last query.
Definition: pkt_buffer.cc:33
unsigned get_write_partial() const
Get number of bytes in a partial packet, or -1 on overflow.
Definition: pkt_buffer.cc:69
unsigned get_peek_ready() const
Find the longest available contiguous segment that can be requested by peek().
Definition: pkt_buffer.cc:242
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
Definition: pkt_buffer.cc:93
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: pkt_buffer.cc:105
unsigned m_pkt_rdidx
Store packet lengths in an auxiliary buffer (read domain)
Definition: pkt_buffer.h:162
u8 read_next() override
Read the next byte from the underlying buffer or device.
Definition: pkt_buffer.cc:206
bool read_bytes(unsigned nbytes, void *dst) override
Read 0 or more bytes into a buffer.
Definition: pkt_buffer.cc:187
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: pkt_buffer.cc:52
volatile unsigned m_shared_pktcount
Shared state is constant except for cross-domain events.
Definition: pkt_buffer.h:180
void zcw_write(unsigned nbytes)
Zero-copy write (zcw) mode, required for UART interface.
Definition: pkt_buffer.cc:164
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
Definition: pkt_buffer.cc:73
void write_overflow() override
Optional error handling for write overflow.
Definition: pkt_buffer.cc:101
volatile unsigned m_shared_rdavail
Shared state is constant except for cross-domain events.
Definition: pkt_buffer.h:179
u8 *const m_buff
State for the main circular buffer (read domain)
Definition: pkt_buffer.h:152
constexpr PacketBuffer(void *buff, unsigned nbytes, unsigned max_pkt=0)
Configure this object and link to the underlying working memory.
Definition: pkt_buffer.h:70
unsigned m_total_frames
Cumulative total-bytes and total-frames statistics counters.
Definition: pkt_buffer.h:168
u8 * get_buff_dtor() const
Accessor for children that need to delete underlying buffer.
Definition: pkt_buffer.h:137
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Definition: pkt_buffer.cc:177
bool read_consume(unsigned nbytes) override
Read and discard 0 or more bytes.
Definition: pkt_buffer.cc:256
const unsigned m_pkt_maxct
Store packet lengths in an auxiliary buffer (read domain)
Definition: pkt_buffer.h:161
unsigned get_buff_size() const
Accessor for children that need to delete underlying buffer.
Definition: pkt_buffer.h:138
unsigned m_buff_rdcount
State for the main circular buffer (read domain)
Definition: pkt_buffer.h:155
unsigned m_buff_rdidx
State for the main circular buffer (read domain)
Definition: pkt_buffer.h:154
unsigned zcw_maxlen() const
Max contiguous write length (ZCW).
Definition: pkt_buffer.cc:142
unsigned m_next_wrlen
Working state for writes (write domain)
Definition: pkt_buffer.h:174
void write_abort() override
If possible, abort the current partially-written packet.
Definition: pkt_buffer.cc:89
const unsigned m_buff_size
State for the main circular buffer (read domain)
Definition: pkt_buffer.h:153
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
Definition: pkt_buffer.cc:266
u8 get_percent_full() const
Get overall buffer occupancy as percentage full (0-100%).
Definition: pkt_buffer.cc:41
u8 * zcw_start()
Pointer to a contiguous buffer (ZCW).
Definition: pkt_buffer.cc:154
u16 *const m_pkt_lbuff
Store packet lengths in an auxiliary buffer (read domain)
Definition: pkt_buffer.h:160
Packet buffer with statically-allocated working memory.
Definition: pkt_buffer.h:198
PacketBufferStatic(unsigned max_pkt=32)
Link the parent object to the statically allocated buffer.
Definition: pkt_buffer.h:202
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Stream buffer with statically-allocated working memory.
Definition: pkt_buffer.h:223
StreamBufferStatic()
Link the parent object to the statically allocated buffer.
Definition: pkt_buffer.h:226
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
I/O interface core definitions.
Inline byte and packet counters for statistics and diagnostics.
Basic type aliases and prototypes used throughout SatCat5.