SatCat5
coap_resource.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
36 
37 #pragma once
38 
39 #include <satcat5/coap_connection.h>
40 #include <satcat5/coap_endpoint.h>
41 #include <satcat5/utils.h>
42 
43 namespace satcat5 {
44  namespace coap {
47  class Resource {
48  protected:
52  constexpr explicit Resource(const char* uri_path)
53  : m_uri_path(normalize_uri(uri_path))
54  , m_server(nullptr)
55  , m_next(nullptr) {}
56 
60  Resource(ResourceServer* server, const char* uri_path);
61 
63  ~Resource() SATCAT5_OPTIONAL_DTOR;
64 
66  static constexpr const char* normalize_uri(const char* uri)
67  { return (uri[0] == '/') ? (uri + 1) : (uri); }
68 
69  public:
72  bool operator==(const Resource& other) const;
73  inline bool operator!=(const Resource& other) const
74  { return !(*this == other); }
76 
79  satcat5::ip::Dispatch* ip() const;
80  satcat5::udp::Dispatch* udp() const;
82 
89  virtual bool request_get(Connection* obj, Reader* msg)
90  { return obj->error_response(CODE_BAD_METHOD); }
91  virtual bool request_post(Connection* obj, Reader* msg)
92  { return obj->error_response(CODE_BAD_METHOD); }
93  virtual bool request_put(Connection* obj, Reader* msg)
94  { return obj->error_response(CODE_BAD_METHOD); }
95  virtual bool request_delete(Connection* obj, Reader* msg)
96  { return obj->error_response(CODE_BAD_METHOD); }
98 
99  protected:
101  const char* const m_uri_path;
102 
105 
106  private:
107  // Member variables
109  Resource* m_next; // Linked list next node
110  };
111 
113  class ResourceEcho : public Resource {
114  public:
116  constexpr explicit ResourceEcho(const char* uri_path)
117  : Resource(uri_path) {}
119  ResourceEcho(ResourceServer* server, const char* uri_path)
120  : Resource(server, uri_path) {}
121 
123  bool request_get(Connection* obj, Reader* msg) override;
124  };
125 
128  class ResourceError final : public Resource {
129  public:
131  constexpr explicit ResourceError(const char* uri_path, Code errcode)
132  : Resource(uri_path), m_errcode(errcode) {}
134  ResourceError(ResourceServer* server, const char* uri_path, Code errcode)
135  : Resource(server, uri_path), m_errcode(errcode) {}
136 
139  bool request_get(Connection* obj, Reader* msg) override
140  { return obj->error_response(m_errcode); }
141  bool request_post(Connection* obj, Reader* msg) override
142  { return obj->error_response(m_errcode); }
143  bool request_put(Connection* obj, Reader* msg) override
144  { return obj->error_response(m_errcode); }
145  bool request_delete(Connection* obj, Reader* msg) override
146  { return obj->error_response(m_errcode); }
148 
149  protected:
151  };
152 
154  class ResourceLog : public Resource {
155  public:
157  constexpr ResourceLog(const char* uri_path, s8 priority)
158  : Resource(uri_path), m_priority(priority) {}
160  ResourceLog(ResourceServer* server, const char* uri_path, s8 priority)
161  : Resource(server, uri_path), m_priority(priority) {}
162 
164  bool request_post(Connection* obj, Reader* msg) override;
165 
166  protected:
168  };
169 
172  class ResourceNull : public Resource {
173  public:
174  explicit ResourceNull(const char* uri)
175  : Resource(uri) {}
176  ResourceNull(ResourceServer* server, const char* uri)
177  : Resource(server, uri) {}
178  };
179 
184  public:
187  satcat5::udp::Port port = satcat5::udp::PORT_COAP)
188  : satcat5::coap::EndpointUdp(udp, port), m_connection(this, udp) {}
189 
191  inline void add_resource(Resource* resource)
192  { m_resources.add(resource); }
193 
195  inline void remove_resource(Resource* resource)
196  { m_resources.remove(resource); }
197 
198  protected:
199  // Member variables
202 
204  void coap_request(Connection* obj, Reader* msg) override;
205  };
206  }
207 }
CoAP request/response handling for a single client-server connection.
bool error_response(satcat5::coap::Code code, const char *why=0)
If able, return an error in response to an incoming request from a remote client.
Variant of coap::Connection for UDP connections.
Variant for a UDP client or server with multiple active connections.
satcat5::udp::Dispatch * udp() const
Pointer to the parent's network interface.
Base-class for parsing CoAP message headers and options.
Definition: coap_reader.h:162
Resource that echos back any incoming payload.
bool request_get(Connection *obj, Reader *msg) override
GET requests respond with the payload from the request.
constexpr ResourceEcho(const char *uri_path)
Constructor (user calls add_resource).
ResourceEcho(ResourceServer *server, const char *uri_path)
Constructor (parent class calls add_resource).
Resource that returns a fixed status code for all requests.
constexpr ResourceError(const char *uri_path, Code errcode)
Constructor (user calls add_resource).
const satcat5::coap::Code m_errcode
All request types respond with the specified error code.
bool request_delete(Connection *obj, Reader *msg) override
All request types respond with the specified error code.
bool request_put(Connection *obj, Reader *msg) override
All request types respond with the specified error code.
bool request_post(Connection *obj, Reader *msg) override
All request types respond with the specified error code.
ResourceError(ResourceServer *server, const char *uri_path, Code errcode)
Constructor (parent class calls add_resource).
bool request_get(Connection *obj, Reader *msg) override
All request types respond with the specified error code.
Define a single CoAP resource.
Definition: coap_resource.h:47
virtual bool request_delete(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
Definition: coap_resource.h:95
virtual bool request_put(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
Definition: coap_resource.h:93
const char *const m_uri_path
URI-Path for this resource.
bool operator==(const Resource &other) const
Comparison operators for comparing two URI-paths.
virtual bool request_post(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
Definition: coap_resource.h:91
static constexpr const char * normalize_uri(const char *uri)
Remove leading slash from a user-provided path.
Definition: coap_resource.h:66
satcat5::ip::Dispatch * ip() const
Pointer to the parent's network interface.
~Resource() SATCAT5_OPTIONAL_DTOR
Destructor is only accessible to the child object.
constexpr Resource(const char *uri_path)
Simple constructor sets the URI path for the resource.
Definition: coap_resource.h:52
bool operator!=(const Resource &other) const
Comparison operators for comparing two URI-paths.
Definition: coap_resource.h:73
virtual bool request_get(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
Definition: coap_resource.h:89
ResourceServer *const m_server
Optional pointer to the server object.
satcat5::udp::Dispatch * udp() const
Pointer to the parent's network interface.
Resource that creates a log::Log entry.
constexpr ResourceLog(const char *uri_path, s8 priority)
Constructor (user calls add_resource).
s8 m_priority
Priority for created log messages.
bool request_post(Connection *obj, Reader *msg) override
POST requests create a log::Log entry.
ResourceLog(ResourceServer *server, const char *uri_path, s8 priority)
Constructor (parent class calls add_resource).
The NullResource does not implement GET, POST, PUT, or DELETE.
Manager for several Resource objects available on an Endpoint.
ResourceServer(satcat5::udp::Dispatch *udp, satcat5::udp::Port port=satcat5::udp::PORT_COAP)
Constructor. By default, bind this server to port 5683.
void coap_request(Connection *obj, Reader *msg) override
Handler for an incoming request.
satcat5::coap::ConnectionUdp m_connection
Single UDP connection.
void remove_resource(Resource *resource)
Remove a Resource from the linked list.
satcat5::util::List< Resource > m_resources
List of resources.
void add_resource(Resource *resource)
Add a Resource to the linked list.
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
Definition: ip_dispatch.h:43
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Helper functions for manipulating singly-linked lists.
Definition: list.h:52
Templated linked-list class.
Definition: list.h:210
constexpr Code CODE_BAD_METHOD(4, 5)
< 4.04 Not Found
CoAP message header CODE field (Section 12.1).
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
Miscellaneous mathematical utility functions.