SatCat5
coap_endpoint.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // CoAP endpoint (i.e., client, server, or combined client+server)
6 
7 #pragma once
8 
9 #include <satcat5/ccsds_spp.h>
10 #include <satcat5/coap_connection.h>
11 #include <satcat5/udp_dispatch.h>
12 #include <satcat5/utils.h>
13 
14 namespace satcat5 {
15  namespace coap {
42  public:
44  inline satcat5::net::Dispatch* iface() const
45  { return m_iface; }
47  inline satcat5::udp::Port srcport() const
48  { return satcat5::udp::Port(m_filter.as_u16()); }
49 
51  Connection* find_token(u32 token) const;
52 
55 
59  { m_prefer = obj; }
60 
63  inline void set_filter(const satcat5::net::Type& filter)
64  { m_filter = filter; }
65 
66  protected:
69  ~Endpoint() SATCAT5_OPTIONAL_DTOR;
70 
71  friend satcat5::coap::EndpointSppFwd;
72 
101  virtual void coap_request(Connection* obj, Reader* msg) // GCOVR_EXCL_LINE
102  { obj->error_response(CODE_NOT_IMPL); } // GCOVR_EXCL_LINE
103  virtual void coap_response(Connection* obj, Reader* msg) {} // GCOVR_EXCL_LINE
104  virtual void coap_reqwait(Connection* obj, Reader* msg) {} // GCOVR_EXCL_LINE
105  virtual void coap_separate(Connection* obj, Reader* msg) {} // GCOVR_EXCL_LINE
106  virtual void coap_error(Connection* obj) {} // GCOVR_EXCL_LINE
107  virtual void coap_ping(const Reader* msg) {} // GCOVR_EXCL_LINE
108  virtual void coap_timeout(Connection* obj) {} // GCOVR_EXCL_LINE
110 
111  // Network event handling.
112  void frame_rcvd(satcat5::io::LimitedRead& src) override;
113 
114  // Internal event handling.
116  inline void add_connection(Connection* item)
117  { m_list.add(item); }
118  inline void remove_connection(Connection* item)
119  { m_list.remove(item); }
120  bool reply(u8 type, const satcat5::coap::ReadHeader* msg);
121 
122  // Internal state.
123  satcat5::net::Dispatch* const m_iface;
125  satcat5::coap::Connection* m_prefer;
126  satcat5::coap::Endpoint* m_aux_ep;
127  };
128 
132  class ManageSpp {
133  public:
136  { return &m_connection; }
137 
138  protected:
141  ManageSpp(satcat5::coap::Endpoint* coap, u16 apid);
142  ~ManageSpp() {}
143 
146  };
147 
151  : public satcat5::coap::Endpoint
152  , public satcat5::coap::ManageSpp
153  {
154  protected:
158  ~EndpointSpp() {}
159  };
160 
166  public:
169  satcat5::coap::Endpoint* backing_endpoint);
170 
171  protected:
172  void coap_request(Connection* obj, Reader* msg) override;
173  void coap_response(Connection* obj, Reader* msg) override;
174  void coap_error(Connection* obj) override;
175  void coap_ping(const Reader* msg) override;
176 
177  satcat5::coap::Endpoint* const m_endpoint;
178  };
179 
183  class ManageUdp {
184  public:
186  void bind(satcat5::udp::Port req_port = satcat5::udp::PORT_COAP);
187 
193  const satcat5::udp::Addr& dstaddr,
194  const satcat5::udp::Port& dstport = satcat5::udp::PORT_COAP,
195  const satcat5::udp::Port& srcport = satcat5::udp::PORT_NONE);
196 
199  inline satcat5::eth::Dispatch* eth() const
200  { return ip()->iface(); }
201  inline satcat5::ip::Dispatch* ip() const
202  { return udp()->iface(); }
203  inline satcat5::udp::Dispatch* udp() const
204  { return (satcat5::udp::Dispatch*)m_endpoint->iface(); }
206 
207  protected:
212  explicit ManageUdp(satcat5::coap::Endpoint* coap,
213  satcat5::udp::Port req_port = satcat5::udp::PORT_NONE);
214  ~ManageUdp() {}
215 
218  };
219 
224  : public satcat5::coap::Endpoint
225  , public satcat5::coap::ManageUdp
226  {
227  protected:
233  satcat5::udp::Port req_port = satcat5::udp::PORT_NONE);
234  ~EndpointUdp() {}
235  };
236 
241  protected:
244  satcat5::udp::Port req_port = satcat5::udp::PORT_NONE)
245  : EndpointUdp(iface, req_port), m_connection(this, iface) {}
246  ~EndpointUdpSimple() {}
247 
250  };
251 
254  template <unsigned SIZE>
256  public:
258  inline ConnectionUdp& connections(unsigned idx)
259  { return m_connections[idx]; }
260 
261  protected:
264  satcat5::udp::Port req_port = satcat5::udp::PORT_NONE)
265  : EndpointUdp(iface, req_port), m_connections(this, iface) {}
266  ~EndpointUdpStatic() {}
267 
270  };
271  }
272 }
CCSDS Space Packet Protocol.
Implemention of "net::Dispatch" API for CCSDS-SPP packets.
Definition: ccsds_spp.h:199
CoAP request/response handling for a single client-server connection.
Variant of coap::Connection for CCSDS-SPP connections.
Statically-allocated array of ConnectionUdp objects.
Variant of coap::Connection for UDP connections.
CoAP endpoint (i.e., client, server, or combined client+server).
Definition: coap_endpoint.h:41
satcat5::udp::Port srcport() const
For UDP only, query the local port number.
Definition: coap_endpoint.h:47
virtual void coap_reqwait(Connection *obj, Reader *msg)
The Child class overrides these event-handlers:
void set_connection(satcat5::coap::Connection *obj)
Set the preferred connection for outgoing requests.
Definition: coap_endpoint.h:58
virtual void coap_request(Connection *obj, Reader *msg)
The Child class overrides these event-handlers:
satcat5::coap::Connection * get_idle_connection() const
Get the first idle connection, or null if all are busy.
Endpoint(satcat5::net::Dispatch *iface)
Constructor is only accessible to the child object.
virtual void coap_separate(Connection *obj, Reader *msg)
The Child class overrides these event-handlers:
satcat5::net::Dispatch * iface() const
Fetch the associated network interface.
Definition: coap_endpoint.h:44
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
virtual void coap_error(Connection *obj)
The Child class overrides these event-handlers:
virtual void coap_ping(const Reader *msg)
The Child class overrides these event-handlers:
Connection * find_token(u32 token) const
Scan connections for a matching proxy-ID.
void set_filter(const satcat5::net::Type &filter)
Set network interface filter.
Definition: coap_endpoint.h:63
virtual void coap_timeout(Connection *obj)
The Child class overrides these event-handlers:
virtual void coap_response(Connection *obj, Reader *msg)
The Child class overrides these event-handlers:
EndpointSpp variant that forwards all requests to another Endpoint.
void coap_ping(const Reader *msg) override
The Child class overrides these event-handlers:
void coap_request(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
void coap_error(Connection *obj) override
The Child class overrides these event-handlers:
void coap_response(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
Variant of coap::Endpoint for a CCSDS-SPP client or server.
EndpointSpp(satcat5::ccsds_spp::Dispatch *iface, u16 apid)
Constructor and destructor should only be called by the child.
Variant for a UDP client or server with multiple active connections.
EndpointUdp(satcat5::udp::Dispatch *iface, satcat5::udp::Port req_port=satcat5::udp::PORT_NONE)
Constructor and destructor should only be called by the child.
Variant of EndpointUdp with a single active connection.
satcat5::coap::ConnectionUdp m_connection
Connection to the remote server.
EndpointUdpSimple(satcat5::udp::Dispatch *iface, satcat5::udp::Port req_port=satcat5::udp::PORT_NONE)
Constructor and destructor should only be called by the child.
Variant of EndpointUdp with a static array of connections.
satcat5::coap::ConnectionUdpArray< SIZE > m_connections
Connections to remote server(s).
EndpointUdpStatic(satcat5::udp::Dispatch *iface, satcat5::udp::Port req_port=satcat5::udp::PORT_NONE)
Constructor and destructor should only be called by the child.
ConnectionUdp & connections(unsigned idx)
Access an internal connection object by index.
Connection manager for CoAP endpoints using CCSDS-SPP.
satcat5::coap::ConnectionSpp * connection()
Accessor for the internal connection object.
satcat5::coap::ConnectionSpp m_connection
Point-to-point link, only one Connection is required.
ManageSpp(satcat5::coap::Endpoint *coap, u16 apid)
Constructor and destructor should only be called by the child.
Connection manager for CoAP endpoints using UDP.
satcat5::coap::ConnectionUdp * connect(const satcat5::udp::Addr &dstaddr, const satcat5::udp::Port &dstport=satcat5::udp::PORT_COAP, const satcat5::udp::Port &srcport=satcat5::udp::PORT_NONE)
Open a connection to the designated remote UDP endpoint.
ManageUdp(satcat5::coap::Endpoint *coap, satcat5::udp::Port req_port=satcat5::udp::PORT_NONE)
Constructor and destructor should only be called by the child.
satcat5::eth::Dispatch * eth() const
Pointer to the parent's network interface.
satcat5::coap::Endpoint *const m_endpoint
Pointer to the associated Endpoint.
satcat5::ip::Dispatch * ip() const
Pointer to the parent's network interface.
satcat5::udp::Dispatch * udp() const
Pointer to the parent's network interface.
void bind(satcat5::udp::Port req_port=satcat5::udp::PORT_COAP)
Begin accepting incoming requests on the designated UDP port.
Parser for CoAP message headers only.
Definition: coap_reader.h:80
Base-class for parsing CoAP message headers and options.
Definition: coap_reader.h:162
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
Limited read of next N bytes.
Definition: io_readable.h:255
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
Definition: ip_dispatch.h:43
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
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
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
void add(T *item)
Add new item to front or back, whichever is simpler.
Definition: list.h:221
void remove(T *item)
Remove the designated item from the list.
Definition: list.h:277
constexpr Code CODE_NOT_IMPL(5, 1)
< 5.00 Internal Server Error
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
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38
u16 as_u16() const
Accessors for m_value.
Definition: net_type.h:65
Miscellaneous mathematical utility functions.