Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2026 Carl Seifert
3 * SPDX-FileCopyrightText: 2024-2026 TU Dresden
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
9#include <stdbool.h>
10#include <stdint.h>
11
12#include "xfa.h"
13#include "ztimer.h"
14#include "event.h"
15
17#include "net/unicoap/message.h"
20
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
36
37/* MARK: - Specifying and inspecting resource paths */
53typedef struct {
71 const char** _components;
73
74/* Ensure components array is not stored in-place as this will make the resource struct variable
75 * in length, screwing XFA resource definitions where resources are assumed to be equal in size. */
76static_assert(sizeof(unicoap_pathspec_t) == sizeof(char*),
77 "unicoap_pathspec_t is of unexpected size. Please file a bug report.");
78
89
90#define UNICOAP_PATH(...) \
91 ((unicoap_pathspec_t) { ._components = (const char*[]) { __VA_ARGS__, \
92 _UNICOAP_TRY_CHECK_PATH_COMPONENTS(__VA_ARGS__) ? NULL : NULL } })
93
95#define UNICOAP_PATH_ROOT \
96 ((unicoap_pathspec_t) { ._components = NULL })
97
99#define UNICOAP_PATH_RESOURCE_DISCOVERY UNICOAP_PATH(".well-known", "core")
100
106static inline bool unicoap_path_is_root(const unicoap_pathspec_t* path) {
107 assert(path);
108 return path->_components == NULL;
109}
110
122
135
147 const unicoap_options_t* options, bool match_subtree);
148
168 const char* string, size_t string_length, bool match_subtree);
169
185ssize_t unicoap_path_stringify(const unicoap_pathspec_t* path, char* buffer, size_t capacity);
186
195
196#ifndef DOXYGEN
197/* Forward declaration */
198struct unicoap_resource;
199#endif
200
203
204/* MARK: - Reacting to CoAP requests */
212typedef struct {
217
224 void* _packet;
226
247 unicoap_request_context_t* ctx, void* arg);
248
259
267#define UNICOAP_IGNORING_REQUEST (-2042)
268
288
298
299/* MARK: - Registering CoAP resources */
310typedef enum {
321
330
331 /* TODO: Advanced features */
333
340
349typedef uint8_t unicoap_proto_set_t;
350
357
366typedef uint8_t unicoap_method_set_t;
367
374
427
435
436/* MARK: - Resource discovery */
464ssize_t unicoap_resource_core_link_format_build(char* buffer, size_t capacity,
465 unicoap_proto_t proto);
466
484
496typedef ssize_t (*unicoap_link_encoder_t)(const unicoap_resource_t* resource, char* buffer,
497 size_t capacity, unicoap_link_encoder_ctx_t* context);
498
512ssize_t unicoap_resource_encode_link(const unicoap_resource_t* resource, char* buffer,
513 size_t capacity, unicoap_link_encoder_ctx_t* context);
515
516/* MARK: - Matching requests and resources */
525
537typedef int (*unicoap_request_matcher_t)(const unicoap_listener_t* listener,
538 const unicoap_resource_t** resource,
539 const unicoap_message_t* request,
540 const unicoap_endpoint_t* endpoint);
588
605
616
638 const char* path, size_t length) {
639 return unicoap_path_matches_string(&resource->path, path, length,
640 (resource->flags & UNICOAP_RESOURCE_FLAG_MATCH_SUBTREE) != 0);
641}
642
654 const unicoap_options_t* options) {
655 return unicoap_path_matches_options(&resource->path, options,
656 (resource->flags & UNICOAP_RESOURCE_FLAG_MATCH_SUBTREE) != 0);
657}
658
659
660/* MARK: - Matching methods and protocols */
670#define UNICOAP_METHOD_FLAG(method) (1 << (method))
671
691#define UNICOAP_METHODS(first_method, ...) UNICOAP_BITFIELD(first_method, __VA_ARGS__)
692
696#define UNICOAP_METHODS_ALL (0xff)
697
704{
705 return (methods & UNICOAP_METHOD_FLAG(method)) != 0;
706}
707
713#define UNICOAP_PROTOCOL_FLAG(proto) (1 << (proto))
714
726#define UNICOAP_PROTOCOLS(first_proto, ...) UNICOAP_BITFIELD(first_proto, __VA_ARGS__)
727
731#define UNICOAP_PROTOCOLS_ALLOW_ALL (0)
732
736#define UNICOAP_PROTOCOLS_ALLOW_NONE (1)
737
745static inline bool unicoap_match_proto(unicoap_proto_set_t protocols, unicoap_proto_t proto)
746{
747 return protocols == UNICOAP_PROTOCOLS_ALLOW_ALL ||
748 (protocols & UNICOAP_PROTOCOL_FLAG(proto)) != 0;
749}
750
752
757/* MARK: - Declaring a CoAP resource at compile time */
772#if IS_USED(MODULE_UNICOAP_SERVER_RESOURCE_DECLARATIONS) || defined(DOXYGEN)
773# define UNICOAP_RESOURCE(name) \
774 XFA_CONST(unicoap_resource_t, unicoap_resources_xfa, 0) CONCAT(unicoap_resource_, name) =
775#else
776# define UNICOAP_RESOURCE(name) \
777 static_assert(false, \
778 "The unicoap_server_resource_declarations module is missing, resource cannot be registered"); \
779 unicoap_resource_t CONCAT(unicoap_resource_, name) =
780#endif
783
784#ifndef DOXYGEN
785extern uint8_t unicoap_receiver_buffer[CONFIG_UNICOAP_PDU_SIZE_MAX];
786#endif
787
788#ifdef __cplusplus
789}
790#endif
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Constants used in CoAP such as option numbers and message codes.
#define CONFIG_UNICOAP_PDU_SIZE_MAX
Capacity of internal buffers.
Definition config.h:202
unicoap_status_t
CoAP response status codes.
Definition constants.h:212
unicoap_method_t
CoAP request method codes (0.xx range)
Definition constants.h:138
unicoap_content_format_t
Content-Format option values
Definition constants.h:708
bool unicoap_path_is_equal(const unicoap_pathspec_t *lhs, const unicoap_pathspec_t *rhs)
Compares two path objects.
void unicoap_listener_register(unicoap_listener_t *listener)
Makes unicoap listen for resources contained in listener.
bool unicoap_response_is_optional(unicoap_options_t *options, unicoap_status_t status)
Determines if the client is interested in a response.
static bool unicoap_resource_match_method(unicoap_method_set_t methods, unicoap_method_t method)
Returns whether the method is allowed according to the given methods.
Definition server.h:703
void unicoap_print_path(const unicoap_pathspec_t *path)
Prints given path object as serialized path.
int(* unicoap_request_handler_t)(unicoap_message_t *request, const unicoap_aux_t *aux, unicoap_request_context_t *ctx, void *arg)
Resource request handler.
Definition server.h:246
#define UNICOAP_METHOD_FLAG(method)
A bit set at the position dictated by method.
Definition server.h:670
static bool unicoap_match_proto(unicoap_proto_set_t protocols, unicoap_proto_t proto)
Returns whether the given proto is allowed according to the protocols argument.
Definition server.h:745
struct unicoap_listener unicoap_listener_t
Typealias for unicoap_listener.
Definition server.h:524
unicoap_status_t unicoap_response_status_from_errno(int _errno)
Maps a given errno to a CoAP response status code.
ssize_t unicoap_resource_core_link_format_build(char *buffer, size_t capacity, unicoap_proto_t proto)
Builds a string in Constrained RESTful Environments (CoRE) Link Format.
unicoap_resource_flags_t
Flags for enabling advanced features in server exchanges.
Definition server.h:310
bool unicoap_path_matches_string(const unicoap_pathspec_t *path, const char *string, size_t string_length, bool match_subtree)
Compares UTF-8 string path against path object.
int unicoap_send_response(unicoap_message_t *response, unicoap_request_context_t *context)
Sends a response to a request identifier by the given context.
uint8_t unicoap_method_set_t
Allowed methods bit field.
Definition server.h:366
static bool unicoap_resource_match_path_options(const unicoap_resource_t *resource, const unicoap_options_t *options)
Determines whether the complete Uri-Path matches the resources path.
Definition server.h:653
void unicoap_print_protocols(unicoap_proto_set_t protocols)
Prints protocols bitfield.
ssize_t unicoap_resource_encode_link(const unicoap_resource_t *resource, char *buffer, size_t capacity, unicoap_link_encoder_ctx_t *context)
Encodes given resource in Constrained RESTful Environments (CoRE) Link Format.
ssize_t unicoap_path_stringify(const unicoap_pathspec_t *path, char *buffer, size_t capacity)
Writes path into buffer, with path components separated by /
size_t unicoap_path_component_count(const unicoap_pathspec_t *path)
Counts path components in path.
ssize_t(* unicoap_link_encoder_t)(const unicoap_resource_t *resource, char *buffer, size_t capacity, unicoap_link_encoder_ctx_t *context)
Handler function to write a resource link.
Definition server.h:496
void unicoap_print_methods(unicoap_method_set_t methods)
Prints methods bitfield.
static bool unicoap_path_is_root(const unicoap_pathspec_t *path)
Determines whether the given path is the root path.
Definition server.h:106
uint8_t unicoap_proto_set_t
Allowed protocols bit field.
Definition server.h:349
int(* unicoap_request_matcher_t)(const unicoap_listener_t *listener, const unicoap_resource_t **resource, const unicoap_message_t *request, const unicoap_endpoint_t *endpoint)
Handler function for the request matcher strategy.
Definition server.h:537
void unicoap_print_resource_flags(unicoap_resource_flags_t flags)
Prints resource flags.
static bool unicoap_resource_match_path_string(const unicoap_resource_t *resource, const char *path, size_t length)
Determines whether the complete Uri-Path matches the resources path.
Definition server.h:637
void unicoap_print_resource(const unicoap_resource_t *resource)
Prints CoAP resource definition properties.
bool unicoap_path_matches_options(const unicoap_pathspec_t *path, const unicoap_options_t *options, bool match_subtree)
Compares Uri-Path options against path object.
struct unicoap_resource unicoap_resource_t
CoAP resource.
Definition server.h:202
#define UNICOAP_PROTOCOLS_ALLOW_ALL
unicoap_proto_set_t value indicating all protocols are allowed
Definition server.h:731
int unicoap_listener_deregister(unicoap_listener_t *listener)
Removes listener from unicoap.
#define UNICOAP_PROTOCOL_FLAG(proto)
A bit set at the position dictated by proto.
Definition server.h:713
@ UNICOAP_RESOURCE_FLAG_RELIABLE
Sets the type of the message to confirmable (CON), if an unreliable transport is used.
Definition server.h:320
@ UNICOAP_RESOURCE_FLAG_MATCH_SUBTREE
Makes this resource match paths located in the subtree of the given path.
Definition server.h:329
unicoap_proto_t
Transport protocol CoAP is used over.
Definition transport.h:130
CoAP Message API.
Auxiliary exchange information.
Definition transport.h:466
A CoAP endpoint.
Definition transport.h:182
A modular collection of resources for a server.
Definition server.h:544
const unicoap_resource_t * resources
Reference to contiguous array of resource belonging to this listener.
Definition server.h:548
unicoap_proto_set_t protocols
Allowed protocols for the resources grouped together by this resource.
Definition server.h:586
unicoap_link_encoder_t link_encoder
Encoder for Constrained RESTful Environments (CoRE) Link Format.
Definition server.h:569
size_t resource_count
The number of resources belonging in this listener.
Definition server.h:553
unicoap_listener_t * next
Next listener in linked list.
Definition server.h:574
unicoap_request_matcher_t request_matcher
Function that picks a suitable request handler from a request.
Definition server.h:564
Generic CoAP message.
Definition message.h:72
CoAP options container.
Definition base.h:97
An immutable path object.
Definition server.h:53
Request context used to send a response to a given request.
Definition server.h:212
const unicoap_resource_t * resource
Resource this request was directed to.
Definition server.h:216
A type representing a CoAP resource.
Definition server.h:383
unicoap_method_set_t methods
Allowed request methods for this resource.
Definition server.h:416
unicoap_proto_set_t protocols
Allowed transport protocols this resource can be reached over.
Definition server.h:425
unicoap_request_handler_t handler
Request handler callback.
Definition server.h:397
void * handler_arg
Opaque optional argument for the request handler (nullable)
Definition server.h:402
const unicoap_pathspec_t path
Resource path.
Definition server.h:389
unicoap_resource_flags_t flags
Flags for modifying resource behavior.
Definition server.h:409
CoAP transport protocol abstraction layer.
Utility macros for unicoap
Cross File Arrays.
ztimer API