SatCat5
net_echo.cc
1 // Copyright 2021 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/eth_dispatch.h>
7 #include <satcat5/log.h>
8 #include <satcat5/net_echo.h>
9 #include <satcat5/udp_dispatch.h>
10 
12 using satcat5::net::Type;
13 namespace log = satcat5::log;
14 
15 // Set verbosity level (0/1)
16 static const unsigned DEBUG_VERBOSE = 0;
17 
18 // Define each of the thin wrappers:
19 satcat5::eth::ProtoEcho::ProtoEcho(
21  satcat5::eth::MacType type_req,
22  satcat5::eth::MacType type_ack)
23  : satcat5::net::ProtoEcho(iface,
24  Type(type_req.value), Type(type_ack.value))
25 {
26  // Nothing else to initialize.
27 }
28 
29 satcat5::udp::ProtoEcho::ProtoEcho(
31  satcat5::udp::Port port)
32  : satcat5::net::ProtoEcho(iface,
33  Type(port.value), Type(port.value))
34 {
35  // Nothing else to initialize.
36 }
37 
38 // Main class definition:
39 ProtoEcho::ProtoEcho(
41  const Type& type_req,
42  const Type& type_ack)
43  : satcat5::net::Protocol(type_req)
44  , m_iface(iface)
45  , m_replytype(type_ack)
46 {
47  m_iface->add(this);
48 }
49 
50 #if SATCAT5_ALLOW_DELETION
52 {
53  m_iface->remove(this);
54 }
55 #endif
56 
58 {
59  // Attempt to open a reply object.
60  unsigned nreply = src.get_read_ready();
62 
63  if (DEBUG_VERBOSE > 0) {
64  log::Log(log::DEBUG, "ProtoEcho").write((u16)nreply);
65  }
66 
67  // If successful, copy input to output.
68  if (dst) {
69  while (nreply--)
70  dst->write_u8(src.read_u8());
71  dst->write_finalize();
72  }
73 }
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
Limited read of next N bytes.
Definition: io_readable.h:255
unsigned get_read_ready() const override
How many bytes can be read without blocking?
Definition: io_readable.cc:293
u8 read_u8()
One of many functions for reading integer/floating point values, see details.
Definition: io_readable.cc:44
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
void write_u8(u8 data)
One of many functions for writing integer/floating point values, see details.
Definition: io_writeable.cc:20
virtual bool write_finalize()
Mark end of frame and release temporary working data.
The Log class creates and formats one log message.
Definition: log.h:195
Log & write(const char *str)
Formatting methods for various data types.
Definition: log.cc:198
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
void add(satcat5::net::Protocol *proto)
Register a Protocol object.
Definition: net_dispatch.h:55
virtual satcat5::io::Writeable * open_reply(const satcat5::net::Type &type, unsigned len)=0
Open a reply to the sender of the most recent message by writing frame header(s) and returning a stre...
void remove(satcat5::net::Protocol *proto)
Unregister a Protocol object.
Definition: net_dispatch.h:59
The Echo protocol can be attached to any Dispatch object.
Definition: net_echo.h:20
satcat5::net::Type const m_replytype
Type for replies.
Definition: net_echo.h:35
~ProtoEcho() SATCAT5_OPTIONAL_DTOR
Only children can safely access constructor/destructor.
Definition: net_echo.cc:51
void frame_rcvd(satcat5::io::LimitedRead &src) override
Event handler to process incoming frames and respond.
Definition: net_echo.cc:57
satcat5::net::Dispatch *const m_iface
Registered iface.
Definition: net_echo.h:34
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Diagnostic logging to UART and/or Ethernet ports.
Generic Echo service.
EtherType field (uint16) is used a protocol-ID [1536..65535].
Definition: eth_header.h:96
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38