9 #include <satcat5/coap_writer.h>
16 namespace log = satcat5::log;
19 : m_uri_path(normalize_uri(uri_path))
26 #if SATCAT5_ALLOW_DELETION
38 SATCAT5_COAP_MAX_URI_PATH_LEN) == 0 &&
39 memchr(
m_uri_path,
'\0', SATCAT5_COAP_MAX_URI_PATH_LEN + 1) &&
40 memchr(other.
m_uri_path,
'\0', SATCAT5_COAP_MAX_URI_PATH_LEN + 1);
51 if (!reply.
ready()) {
return false; }
59 if (!ok || !src || !dst) {
return false; }
82 if (!reply.
ready()) {
return false; }
90 Resource* matched_resource =
nullptr;
93 if (*resource == target_resource) {
94 matched_resource = resource;
break;
100 if (!matched_resource) {
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.
satcat5::io::Writeable * open_response()
If able, accept an incoming request from a remote client.
satcat5::ip::Dispatch * ip() const
Pointer to the parent's network interface.
satcat5::udp::Dispatch * udp() const
Pointer to the parent's network interface.
Base-class for parsing CoAP message headers and options.
satcat5::util::optional< const char * > uri_path() const
Accessors for parsed options.
satcat5::util::optional< u16 > format() const
Accessors for parsed options.
Resource that echos back any incoming payload.
bool request_get(Connection *obj, Reader *msg) override
GET requests respond with the payload from the request.
Define a single CoAP resource.
virtual bool request_delete(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
virtual bool request_put(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
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.
satcat5::ip::Dispatch * ip() const
Pointer to the parent's network interface.
~Resource() SATCAT5_OPTIONAL_DTOR
Destructor is only accessible to the child object.
virtual bool request_get(Connection *obj, Reader *msg)
Event handlers for GET, POST, PUT, and DELETE queries.
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.
s8 m_priority
Priority for created log messages.
bool request_post(Connection *obj, Reader *msg) override
POST requests create a log::Log entry.
The NullResource does not implement GET, POST, PUT, or DELETE.
Manager for several Resource objects available on an Endpoint.
void coap_request(Connection *obj, Reader *msg) override
Handler for an incoming request.
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.
Message formatting for the Constrained Applications Protocol (CoAP).
bool ready() const
Is this object ready for writing? Note: This is the only safe method if m_dst is null.
bool write_finalize()
After the last option, finish with an empty message.
bool write_option(u16 id, unsigned len, const void *data)
Write option(s) one at a time, in various formats.
satcat5::io::Writeable * write_data()
After the last option, start writing message data.
bool write_header(u8 type, Code code, u16 msg_id, u64 token=0, u8 tkl=0)
Always start by writing the header, with optional token.
const u8 * buffer() const
Read-only access to the working buffer.
Abstract API for reading byte-streams and packets.
bool copy_and_finalize(satcat5::io::Writeable *dst, satcat5::io::CopyMode mode=CopyMode::PACKET)
Copy data to a Writeable object, then finalize.
unsigned copy_to(satcat5::io::Writeable *dst)
Copy data to a Writeable object, without finalizing.
virtual unsigned get_read_ready() const =0
How many bytes can be read without blocking?
Abstract API for writing byte-streams and packets.
void write_u8(u8 data)
One of many functions for writing integer/floating point values, see details.
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
The Log class creates and formats one log message.
Log & write(const char *str)
Formatting methods for various data types.
Dispatcher sorts incoming UDP messages by port index.
constexpr Code CODE_BAD_FORMAT(4, 15)
< 4.13 Request Entity Too Large
constexpr Code CODE_BAD_REQUEST(4, 0)
< 2.31 Continue
constexpr u16 FORMAT_TEXT
text/plain;charset=utf-8
constexpr Code CODE_PUT(0, 3)
< 0.02 POST request
constexpr Code CODE_POST(0, 2)
< 0.01 GET request
constexpr Code CODE_DELETE(0, 4)
< 0.03 PUT request
constexpr Code CODE_BAD_METHOD(4, 5)
< 4.04 Not Found
constexpr Code CODE_NOT_FOUND(4, 4)
< 4.03 Forbidden
constexpr Code CODE_GET(0, 1)
< 0.00 Empty message
constexpr u16 FORMAT_BYTES
application/octet-stream
constexpr Code CODE_SERVER_ERROR(5, 0)
< 4.15 Unsupported Content-Format
constexpr u16 OPTION_FORMAT
Option: Content-Format.
constexpr Code CODE_CONTENT(2, 5)
< 2.04 Changed
Message parsing for the Constrained Applications Protocol (CoAP)
CoAP resource handler definition and resource server creation.
Diagnostic logging to UART and/or Ethernet ports.
T value() const
Fetch the inner value.
T value_or(const T &t) const
Fetch the inner value if present, or the specified default.