SatCat5
udp_keep_alive.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/udp_dispatch.h>
7 #include <satcat5/udp_keep_alive.h>
8 
10 
11 KeepAlive::KeepAlive(
13  satcat5::udp::Port port,
14  const char* label)
15  : Protocol(satcat5::net::Type(port.value))
16  , m_addr(iface)
17  , m_label(label)
18 {
19  m_addr.iface()->add(this);
20  m_addr.connect(satcat5::ip::ADDR_BROADCAST, port, port);
21 }
22 
23 KeepAlive::~KeepAlive() {
24  m_addr.iface()->remove(this);
25 }
26 
28  const satcat5::ip::Addr& dstaddr,
29  const satcat5::eth::VlanTag& vtag)
30 {
31  auto dstport = m_addr.dstport();
32  m_addr.connect(dstaddr, dstport, dstport, vtag);
33 }
34 
35 void KeepAlive::send_now(const char* msg) {
36  unsigned len = msg ? strlen(msg) : 0;
37  auto wr = m_addr.open_write(len);
38  if (wr) {
39  if (msg) wr->write_str(msg);
40  wr->write_finalize();
41  }
42 }
43 
45  // Discard all incoming packets with no further action.
46 }
47 
49  send_now(m_label);
50 }
Limited read of next N bytes.
Definition: io_readable.h:255
void write_str(const char *str)
Write the contents of a null-terminated string.
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
satcat5::net::Dispatch * iface() const override
Fetch a pointer to the underlying interface.
Definition: udp_core.cc:62
satcat5::io::Writeable * open_write(unsigned len) override
Open a new frame to the designated address and type.
Definition: udp_core.cc:71
void connect(const satcat5::udp::Addr &dstaddr, const satcat5::eth::MacAddr &dstmac, const satcat5::udp::Port &dstport, const satcat5::udp::Port &srcport=satcat5::udp::PORT_NONE, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Manual address resolution (user supplies IP + MAC).
Definition: udp_core.cc:30
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Send and receive keep-alive messages.
void timer_event() override
Child class MUST override this method.
void send_now(const char *msg)
Immediately send a keep-alive, with an optional message.
void connect(const satcat5::ip::Addr &dstaddr, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Connect to a specific destination address.
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119