8 #include <satcat5/pkt_buffer.h>
17 static const unsigned DEBUG_SAFE_ZCW = 0;
20 static const char* LBL_PKT =
"PKT";
22 void PacketBuffer::clear() {
49 return (u8)satcat5::util::max_unsigned(wr_pct, pkt_pct);
75 const u8* src_u8 = (
const u8*)src;
80 memcpy(
m_buff + wridx, src_u8, wrap);
81 memcpy(
m_buff, src_u8 + wrap, nbytes - wrap);
83 memcpy(
m_buff + wridx, src_u8, nbytes);
114 if ((next_len == 0) || (next_len > wrmax)) {
148 return min_unsigned(max_write, max_wrap);
166 if (DEBUG_SAFE_ZCW) {
168 if (nbytes > max_safe) {
189 u8* dst_u8 = (u8*)dst;
190 if (can_read_internal(nbytes)) {
194 memcpy(dst_u8 + wrap,
m_buff, nbytes - wrap);
198 consume_internal(nbytes);
213 bool PacketBuffer::can_read_internal(
unsigned nbytes)
const {
223 void PacketBuffer::consume_internal(
unsigned nbytes) {
245 return min_unsigned(max_read, max_wrap);
257 if (can_read_internal(nbytes)) {
258 consume_internal(nbytes);
273 if (nrem) consume_internal(nrem);
The PacketBuffer class is a wrapper for a circular buffer, with optional logic to support retention o...
const u8 * peek(unsigned nbytes) const
Peek nbytes into the circular buffer.
void clear()
Reset buffer contents.
unsigned m_next_wrpos
Working state for writes (write domain)
unsigned frame_count(bool reset=true) override
Frames transferred since the last query.
unsigned m_total_bytes
Cumulative total-bytes and total-frames statistics counters.
unsigned byte_count(bool reset=true) override
Bytes transferred since the last query.
unsigned get_write_partial() const
Get number of bytes in a partial packet, or -1 on overflow.
unsigned get_peek_ready() const
Find the longest available contiguous segment that can be requested by peek().
void write_next(u8 data) override
Write the next byte to the underlying buffer or device.
bool write_finalize() override
Mark end of frame and release temporary working data.
unsigned m_pkt_rdidx
Store packet lengths in an auxiliary buffer (read domain)
u8 read_next() override
Read the next byte from the underlying buffer or device.
bool read_bytes(unsigned nbytes, void *dst) override
Read 0 or more bytes into a buffer.
unsigned get_write_space() const override
How many bytes can be written without blocking?
volatile unsigned m_shared_pktcount
Shared state is constant except for cross-domain events.
void zcw_write(unsigned nbytes)
Zero-copy write (zcw) mode, required for UART interface.
void write_bytes(unsigned nbytes, const void *src) override
Write 0 or more bytes from a buffer.
void write_overflow() override
Optional error handling for write overflow.
volatile unsigned m_shared_rdavail
Shared state is constant except for cross-domain events.
u8 *const m_buff
State for the main circular buffer (read domain)
unsigned m_total_frames
Cumulative total-bytes and total-frames statistics counters.
unsigned get_read_ready() const override
How many bytes can be read without blocking?
bool read_consume(unsigned nbytes) override
Read and discard 0 or more bytes.
const unsigned m_pkt_maxct
Store packet lengths in an auxiliary buffer (read domain)
unsigned m_buff_rdcount
State for the main circular buffer (read domain)
unsigned m_buff_rdidx
State for the main circular buffer (read domain)
unsigned zcw_maxlen() const
Max contiguous write length (ZCW).
unsigned m_next_wrlen
Working state for writes (write domain)
void write_abort() override
If possible, abort the current partially-written packet.
const unsigned m_buff_size
State for the main circular buffer (read domain)
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
u8 get_percent_full() const
Get overall buffer occupancy as percentage full (0-100%).
u8 * zcw_start()
Pointer to a contiguous buffer (ZCW).
u16 *const m_pkt_lbuff
Store packet lengths in an auxiliary buffer (read domain)
virtual void read_underflow()
Optional error handling for read underflow.
void request_poll()
Call this method to request polling at a later time.
Platform-agnostic API for interrupt management.
Miscellaneous mathematical utility functions.
constexpr unsigned min_unsigned(unsigned a, unsigned b)
Min and max functions.
constexpr unsigned modulo_add_uns(unsigned sum, unsigned m)
Modulo addition function.