12 #ifndef SATCAT5_BUFFCOPY_BATCH
13 #define SATCAT5_BUFFCOPY_BATCH 32
32 #if SATCAT5_ALLOW_DELETION
55 u16 Readable::read_u16() {
65 u32 Readable::read_u24() {
76 u32 Readable::read_u32() {
88 u64 Readable::read_u48() {
102 u64 Readable::read_u64() {
118 s8 Readable::read_s8()
119 {
return reinterpret<u8, s8>(
read_u8()); }
121 s16 Readable::read_s16()
122 {
return reinterpret<u16, s16>(read_u16()); }
124 s32 Readable::read_s24()
125 {
return (reinterpret<u32, s32>(read_u24()) << 8) >> 8; }
127 s32 Readable::read_s32()
128 {
return reinterpret<u32, s32>(read_u32()); }
130 s64 Readable::read_s48()
131 {
return (reinterpret<u64, s64>(read_u48()) << 16) >> 16; }
133 s64 Readable::read_s64()
134 {
return reinterpret<u64, s64>(read_u64()); }
136 float Readable::read_f32()
137 {
return reinterpret<u32, float>(read_u32()); }
139 double Readable::read_f64()
140 {
return reinterpret<u64, double>(read_u64()); }
142 u16 Readable::read_u16l()
143 {
return __builtin_bswap16(read_u16()); }
145 u32 Readable::read_u24l()
146 {
return __builtin_bswap32(read_u24()) >> 8; }
148 u32 Readable::read_u32l()
149 {
return __builtin_bswap32(read_u32()); }
151 u64 Readable::read_u48l()
152 {
return __builtin_bswap64(read_u48()) >> 16; }
154 u64 Readable::read_u64l()
155 {
return __builtin_bswap64(read_u64()); }
157 s16 Readable::read_s16l()
158 {
return reinterpret<u16, s16>(read_u16l()); }
160 s32 Readable::read_s24l()
161 {
return reinterpret<u32, s32>(__builtin_bswap32(read_u24())) >> 8; }
163 s32 Readable::read_s32l()
164 {
return reinterpret<u32, s32>(read_u32l()); }
166 s64 Readable::read_s48l()
167 {
return reinterpret<u64, s64>(__builtin_bswap64(read_u48())) >> 16; }
169 s64 Readable::read_s64l()
170 {
return reinterpret<u64, s64>(read_u64l()); }
172 float Readable::read_f32l()
173 {
return reinterpret<u32, float>(read_u32l()); }
175 double Readable::read_f64l()
176 {
return reinterpret<u64, double>(read_u64l()); }
184 if (nwrite+1 < dst_size) dst[nwrite++] = (char)tmp;
191 u8* dst_u8 = (u8*)dst;
216 u8 buff[SATCAT5_BUFFCOPY_BATCH];
222 if (max_wr == 0)
break;
224 unsigned batch = min_unsigned(max_wr, SATCAT5_BUFFCOPY_BATCH);
229 if (batch == max_rd)
break;
238 bool rdf =
false, wrf =
false;
239 if (mode == CopyMode::STREAM) {
242 }
else if (mode == CopyMode::PACKET) {
245 }
else if (mode == CopyMode::ALWAYS) {
274 return m_len - m_rdidx;
278 m_len = m_src ? len : 0;
283 return m_src[m_rdidx++];
291 : m_src(src), m_rem(src->get_read_ready()) {}
297 if (nbytes <= m_rem) {
308 if (nbytes <= m_rem) {
340 {
return m_src && m_src->
read_bytes(nbytes, dst); }
Ephemeral Readable interface for a simple array.
unsigned get_read_ready() const override
How many bytes can be read without blocking?
u8 read_next() override
Read the next byte from the underlying buffer or device.
void read_reset(unsigned len)
Reset read position to the start of the backing array, and set the readable length to the specified v...
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
Event-handler interface for newly received data.
virtual void data_unlink(satcat5::io::Readable *src)
Unlink this EventListener from the designated source, because the designated Readable object is being...
virtual void data_rcvd(satcat5::io::Readable *src)=0
The data_rcvd() callback is polled whenever data is available.
Limited read of next N bytes.
bool read_consume(unsigned nbytes) override
Read and discard 0 or more bytes.
constexpr LimitedRead(satcat5::io::Readable *src, unsigned maxrd)
Explicitly set maximum read length in bytes.
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_read_ready() const override
How many bytes can be read without blocking?
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
Readable object that never produces any data.
u8 read_next() override
Read the next byte from the underlying buffer or device.
unsigned get_read_ready() const override
How many bytes can be read without blocking?
An EventListener object that immediately discards all received data.
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Abstract API for reading byte-streams and packets.
virtual void read_underflow()
Optional error handling for read underflow.
void poll_demand()
Event handler for on-demand polling.
unsigned read_str(unsigned dst_size, char *dst)
Safely read a null-terminated input string.
u8 read_u8()
One of many functions for reading integer/floating point values, see details.
virtual bool read_bytes(unsigned nbytes, void *dst)
Read 0 or more bytes into a buffer.
void read_notify()
Attempt notification by calling m_callback->data_rcvd().
virtual void read_finalize()
Consume any remaining bytes in this frame, if applicable.
virtual bool read_consume(unsigned nbytes)
Read and discard 0 or more bytes.
virtual u8 read_next()=0
Read the next byte from the underlying buffer or device.
bool copy_and_finalize(satcat5::io::Writeable *dst, satcat5::io::CopyMode mode=CopyMode::PACKET)
Copy data to a Writeable object, then finalize.
virtual void set_callback(satcat5::io::EventListener *callback)
Update registered callback for data_rcvd() events.
unsigned copy_to(satcat5::io::Writeable *dst)
Copy data to a Writeable object, without finalizing.
satcat5::io::EventListener * m_callback
Pointer to the callback object, or NULL.
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
Wrapper class for forwarding reads to another object.
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
bool read_bytes(unsigned nbytes, void *dst) override
Read 0 or more bytes into a buffer.
unsigned get_read_ready() const override
How many bytes can be read without blocking?
u8 read_next() override
Read the next byte from the underlying buffer or device.
void read_underflow() override
Optional error handling for read underflow.
void set_callback(satcat5::io::EventListener *callback) override
Update registered callback for data_rcvd() events.
bool read_consume(unsigned nbytes) override
Read and discard 0 or more bytes.
Abstract API for writing byte-streams and packets.
virtual unsigned get_write_space() const =0
How many bytes can be written without blocking?
virtual void write_bytes(unsigned nbytes, const void *src)
Write 0 or more bytes from a buffer.
virtual bool write_finalize()
Mark end of frame and release temporary working data.
void request_poll()
Call this method to request polling at a later time.
"Readable" I/O interface core definitions
CopyMode
Specify read and write behavior for Readable::copy_and_finalize().
satcat5::io::NullRead null_read
Global instances of the basic NullRead and NullSink objects.
satcat5::io::NullSink null_sink
Global instances of the basic NullRead and NullSink objects.
"Writeable" I/O interface core definitions
Miscellaneous mathematical utility functions.
constexpr unsigned min_unsigned(unsigned a, unsigned b)
Min and max functions.
T2 reinterpret(T1 x)
In-place byte-for-byte format conversion, aka "type-punning".