SatCat5
net_socket.cc
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/net_socket.h>
7 #include <satcat5/utils.h>
8 
11 
12 SocketCore::SocketCore(
14  u8* txbuff, unsigned txbytes, unsigned txpkt,
15  u8* rxbuff, unsigned rxbytes, unsigned rxpkt)
16  : satcat5::io::BufferedIO(
17  txbuff, txbytes, txpkt,
18  rxbuff, rxbytes, rxpkt)
19  , satcat5::net::Protocol(satcat5::net::TYPE_NONE)
20  , m_addr_ptr(addr)
21 {
22  m_addr_ptr->iface()->add(this);
23 }
24 
25 #if SATCAT5_ALLOW_DELETION
27  m_addr_ptr->iface()->remove(this);
28 }
29 #endif
30 
32  m_addr_ptr->close(); // Unbind Tx
33  m_filter = satcat5::net::TYPE_NONE; // Unbind Rx
34 }
35 
36 bool SocketCore::ready_tx() const {
37  return m_tx.get_buff_size() && m_addr_ptr->ready();
38 }
39 
40 bool SocketCore::ready_rx() const {
41  return m_rx.get_buff_size() && m_filter.bound();
42 }
43 
45  // Data is ready in the transmit buffer.
46  unsigned rem = m_tx.get_read_ready();
48  if (wr) {
49  // Copy to the parent interface in blocks, then finalize.
50  while (rem) {
51  unsigned plen = m_tx.get_peek_ready();
52  const u8* peek = m_tx.peek(plen);
53  wr->write_bytes(plen, peek);
54  rem -= plen;
55  }
57  wr->write_finalize();
58  }
59 }
60 
62  // Drop packet if there's not enough room in the Rx buffer.
63  unsigned rem = src.get_read_ready();
64  if (m_rx.get_write_space() < rem) return;
65 
66  // New data is ready, copy it to the receive buffer.
67  // (Use ZCW method to reduce unnecessary overhead.)
68  while (rem) {
69  unsigned zlen = satcat5::util::min_unsigned(rem, m_rx.zcw_maxlen());
70  u8* zptr = m_rx.zcw_start();
71  src.read_bytes(zlen, zptr);
72  m_rx.zcw_write(zlen);
73  rem -= zlen;
74  }
76 }
satcat5::io::PacketBuffer m_tx
Transmit data (user writes, child reads)
Definition: io_buffer.h:57
satcat5::io::PacketBuffer m_rx
Receive data (user reads, child writes)
Definition: io_buffer.h:60
Limited read of next N bytes.
Definition: io_readable.h:255
bool read_bytes(unsigned nbytes, void *dst) override
Read 0 or more bytes into a buffer.
Definition: io_readable.cc:296
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Definition: io_readable.cc:293
const u8 * peek(unsigned nbytes) const
Peek nbytes into the circular buffer.
Definition: pkt_buffer.cc:248
unsigned get_peek_ready() const
Find the longest available contiguous segment that can be requested by peek().
Definition: pkt_buffer.cc:242
bool write_finalize() override
Mark end of frame and release temporary working data.
Definition: pkt_buffer.cc:105
unsigned get_write_space() const override
How many bytes can be written without blocking?
Definition: pkt_buffer.cc:52
void zcw_write(unsigned nbytes)
Zero-copy write (zcw) mode, required for UART interface.
Definition: pkt_buffer.cc:164
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Definition: pkt_buffer.cc:177
unsigned get_buff_size() const
Accessor for children that need to delete underlying buffer.
Definition: pkt_buffer.h:138
unsigned zcw_maxlen() const
Max contiguous write length (ZCW).
Definition: pkt_buffer.cc:142
void read_finalize() override
Consume any remaining bytes in this frame, if applicable.
Definition: pkt_buffer.cc:266
u8 * zcw_start()
Pointer to a contiguous buffer (ZCW).
Definition: pkt_buffer.cc:154
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
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.
Defines a generic API for sending data to a specific destination, such as a MAC address,...
Definition: net_address.h:30
virtual bool ready() const =0
Is this address object ready for use? Child MUST override this method.
virtual satcat5::net::Dispatch * iface() const =0
Fetch a pointer to the underlying interface.
virtual satcat5::io::Writeable * open_write(unsigned len)=0
Open a new frame to the designated address and type.
virtual void close()=0
Close any open connections and revert to idle.
void add(satcat5::net::Protocol *proto)
Register a Protocol object.
Definition: net_dispatch.h:55
void remove(satcat5::net::Protocol *proto)
Unregister a Protocol object.
Definition: net_dispatch.h:59
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
satcat5::net::Type m_filter
Incoming packet filter.
Definition: net_protocol.h:52
Buffered I/O for sending and receiving messages over a network.
Definition: net_socket.h:24
bool ready_tx() const
Ready to transmit data?
Definition: net_socket.cc:36
satcat5::net::Address *const m_addr_ptr
Generic handler for a specific protocol and address.
Definition: net_socket.h:57
~SocketCore() SATCAT5_OPTIONAL_DTOR
Constructor and destructor are only accessible to child class.
Definition: net_socket.cc:26
void frame_rcvd(satcat5::io::LimitedRead &src) override
Required event handlers.
Definition: net_socket.cc:61
bool ready_rx() const
Ready to receive data?
Definition: net_socket.cc:40
void close()
Close any open connections.
Definition: net_socket.cc:31
void data_rcvd(satcat5::io::Readable *src) override
Required event handlers.
Definition: net_socket.cc:44
Buffered wrappers for generic network communication.
constexpr satcat5::net::Type TYPE_NONE
The TYPE_NONE mask blocks all incoming frames.
Definition: net_type.h:97
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38
bool bound() const
Is this Type actively filtering or is it TYPE_NONE?
Definition: net_type.h:79
Miscellaneous mathematical utility functions.