SatCat5
pkt_combiner.cc
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/pkt_combiner.h>
7 
9 
10 PacketCombiner::PacketCombiner(satcat5::io::Writeable* dst,
11  u8* buff, unsigned buffbytes)
12  : WriteableRedirect(&m_buff)
13  , m_dst(dst)
14  , m_buff(buff, buffbytes, 0)
15  , m_timeout(0)
16 {
17  m_buff.set_callback(this);
18 }
19 
21  m_dst->write_finalize();
22 
23  // Reset timer
24  if (m_timeout) {
25  timer_stop();
26  timer_once(m_timeout);
27  }
28 }
29 
30 void PacketCombiner::set_timeout(unsigned msec) {
31  m_timeout = msec;
32  if (m_timeout) {
33  timer_once(m_timeout);
34  } else {
35  timer_stop();
36  }
37 }
38 
40  // Copy as many bytes as possible to the buffer
41  bool done = false;
42  while (!done) {
43  src->copy_to(m_dst);
44 
45  // If buffer is full, send to dst and set done flag
46  if (m_dst->get_write_space() == 0) {
47  send_now();
48  done = true;
49  }
50 
51  // If end of frame, check for another, else set done flag
52  if (src->get_read_ready() == 0) {
53  src->read_finalize();
54  if (!done && src->get_read_ready() == 0) { done = true; }
55  }
56  }
57 }
58 
60  send_now();
61 }
The PacketCombiner class combines packets written to it into a single packet, and forwards it to a Wr...
Definition: pkt_combiner.h:28
void set_timeout(unsigned msec)
Set a timeout for automatic data forwarding after a period, if the buffer is not yet full.
Definition: pkt_combiner.cc:30
void send_now()
Send the buffer now.
Definition: pkt_combiner.cc:20
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: pkt_combiner.cc:39
void timer_event() override
Child class MUST override this method.
Definition: pkt_combiner.cc:59
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
virtual void read_finalize()
Consume any remaining bytes in this frame, if applicable.
Definition: io_readable.cc:270
virtual void set_callback(satcat5::io::EventListener *callback)
Update registered callback for data_rcvd() events.
Definition: io_readable.cc:39
unsigned copy_to(satcat5::io::Writeable *dst)
Copy data to a Writeable object, without finalizing.
Definition: io_readable.cc:214
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
virtual unsigned get_write_space() const =0
How many bytes can be written without blocking?
virtual bool write_finalize()
Mark end of frame and release temporary working data.
Wrapper class for forwarding writes to another object.
Definition: io_writeable.h:218
void timer_stop()
Stop all future notifications.
Definition: polling.cc:326
void timer_once(unsigned msec)
Configure a one-time notification after X milliseconds.
Definition: polling.cc:316