SatCat5
eth_chat.h
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Protocol handler for a simple text-messaging protocol
6 
7 #pragma once
8 
9 #include <satcat5/ethernet.h>
10 #include <satcat5/log.h>
11 #include <satcat5/polling.h>
12 
13 namespace satcat5 {
14  namespace eth {
15  constexpr satcat5::eth::MacType
16  ETYPE_CHAT_HEARTBEAT = {0x999B},
17  ETYPE_CHAT_TEXT = {0x999C},
18  ETYPE_CHAT_DATA = {0x999D};
19 
25  class ChatProto final
26  : public satcat5::eth::Protocol
27  , public satcat5::poll::Timer
28  {
29  public:
31  ChatProto(
32  satcat5::eth::Dispatch* dispatch,
33  const char* username,
34  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
35  ~ChatProto() {}
36 
38  inline void set_callback(satcat5::net::Protocol* callback)
39  {m_callback = callback;}
40 
42  void send_heartbeat();
44  void send_text(
45  const satcat5::eth::MacAddr& dst,
46  unsigned nbytes, const char* msg);
50  void send_data(
51  const satcat5::eth::MacAddr& dst,
52  unsigned nbytes, const void* msg);
53 
55  satcat5::io::Writeable* open_reply(unsigned len);
56 
59  const satcat5::eth::MacAddr& dst, unsigned len);
60 
65 
66  protected:
67  satcat5::io::Writeable* open_inner(
68  const satcat5::eth::MacAddr& dst,
69  const satcat5::eth::MacType& typ,
70  unsigned msg_bytes);
71  void frame_rcvd(satcat5::io::LimitedRead& src) override;
72  void timer_event() override;
73 
74  const satcat5::net::Type m_reply_type;
75  const char* const m_username;
76  const unsigned m_userlen;
77  const satcat5::eth::VlanTag m_vtag;
78  satcat5::net::Protocol* m_callback;
79  };
80 
85  class LogToChat final : public satcat5::log::EventHandler {
86  public:
89  explicit LogToChat(
91  const satcat5::eth::MacAddr& addr = satcat5::eth::MACADDR_BROADCAST);
92 
94  void log_event(s8 priority, unsigned nbytes, const char* msg) override;
95 
96  private:
97  satcat5::eth::ChatProto* const m_chat;
98  const satcat5::eth::MacAddr m_addr;
99  };
100 
107  class ChatEcho final : public satcat5::net::Protocol {
108  public:
111  ~ChatEcho() SATCAT5_OPTIONAL_DTOR;
112 
113  protected:
114  void frame_rcvd(satcat5::io::LimitedRead& src) override;
115 
116  satcat5::eth::ChatProto* const m_chat;
117  };
118  }
119 }
Service for echoing ChatProto text messages.
Definition: eth_chat.h:107
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: eth_chat.cc:162
ChatEcho(satcat5::eth::ChatProto *service)
Bind to the designated ChatProto object.
Definition: eth_chat.cc:148
Protocol handler for a simple text-messaging protocol This class implements a simple as-hoc text-mess...
Definition: eth_chat.h:28
ChatProto(satcat5::eth::Dispatch *dispatch, const char *username, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Bind this handler to a specified Ethernet interface.
Definition: eth_chat.cc:19
void send_data(const satcat5::eth::MacAddr &dst, unsigned nbytes, const void *msg)
Send machine-readable data.
Definition: eth_chat.cc:69
satcat5::io::Writeable * open_reply(unsigned len)
Open a reply to the sender of the most recent message.
Definition: eth_chat.cc:80
void send_text(const satcat5::eth::MacAddr &dst, unsigned nbytes, const char *msg)
Send a human-readable text message.
Definition: eth_chat.cc:58
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: eth_chat.cc:102
void set_callback(satcat5::net::Protocol *callback)
Set callback for processing incoming messages.
Definition: eth_chat.h:38
void timer_event() override
Child class MUST override this method.
Definition: eth_chat.cc:119
satcat5::eth::MacAddr local_mac() const
Get the local device's source MAC address.
Definition: eth_chat.cc:92
void send_heartbeat()
Send a heartbeat message indicating our presence on the LAN.
Definition: eth_chat.cc:45
satcat5::eth::MacAddr reply_mac() const
Source MAC address of the most recent received message.
Definition: eth_chat.cc:97
satcat5::io::Writeable * open_text(const satcat5::eth::MacAddr &dst, unsigned len)
As "open_reply", but to any destination address.
Definition: eth_chat.cc:87
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
Service for forwarding Log events as text messages.
Definition: eth_chat.h:85
void log_event(s8 priority, unsigned nbytes, const char *msg) override
Event handler for the log::EventHandler API.
Definition: eth_chat.cc:131
LogToChat(satcat5::eth::ChatProto *dst, const satcat5::eth::MacAddr &addr=satcat5::eth::MACADDR_BROADCAST)
Bind to the designated ChatProto object.
Definition: eth_chat.cc:124
Accept incoming Ethernet packets by EtherType.
Definition: eth_protocol.h:24
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Defines the interface for accepting Log messages.
Definition: log.h:72
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Diagnostic logging to UART and/or Ethernet ports.
Core event-processing loop for SatCat5 software.
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
EtherType field (uint16) is used a protocol-ID [1536..65535].
Definition: eth_header.h:96
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38