Loading...
Searching...
No Matches
private.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 <stdint.h>
10
11#include "mutex.h"
12#include "bitfield.h"
13#include "architecture.h"
14#include "random.h"
15
16#include "net/unicoap.h"
17
18#include "private/state.h"
19#include "private/packet.h"
20#include "private/messaging.h"
21
31
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* MARK: - Debugging */
48#define UNICOAP_DEBUG_PREFIX "coap"
49
50#ifndef DOXYGEN
51# define _UNICOAP_NEED_HAVE "(need %" PRIuSIZE ", have %" PRIuSIZE ")"
52#endif
53
54#ifndef DOXYGEN
62# define _UNICOAP_PREFIX_DEBUG(category, ...) DEBUG(UNICOAP_DEBUG_PREFIX category ": " __VA_ARGS__)
63# define UNICOAP_DEBUG(...) _UNICOAP_PREFIX_DEBUG("", __VA_ARGS__)
64# define _OPTIONS_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".options", __VA_ARGS__)
65# define _MESSAGING_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".messaging", __VA_ARGS__)
66# define _STATE_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".state", __VA_ARGS__)
67# define _TRANSPORT_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".transport", __VA_ARGS__)
68# define _SERVER_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".server", __VA_ARGS__)
69# define API_WARNING(message) "WARNING: " message "\n"
70# define API_ERROR(message) "ERROR: " message "\n"
71# define API_MISUSE(message) "API MISUSE: " message "\n"
72# define INTERNAL_ERROR(message) "BUG: " message "\n"
73# define FIXIT(message) "+++ FIXIT: " message "\n"
74#endif
75
76#if ENABLE_DEBUG
80# define DEBUG_ENDPOINT(endpoint) unicoap_print_endpoint(endpoint)
84# define DEBUG_FLAGS(flags, role) unicoap_print_flags(flags, role)
85#else
89# define DEBUG_ENDPOINT(endpoint) (void)endpoint
93# define DEBUG_FLAGS(flags, role) \
94 (void)flags; \
95 (void)role
96#endif
97
98#ifndef DOXYGEN
99# if IS_ACTIVE(CONFIG_UNICOAP_ASSIST)
100# include <stdio.h>
101# endif
102# define unicoap_assist(...) \
103 do { \
104 if (IS_ACTIVE(CONFIG_UNICOAP_ASSIST) || IS_ACTIVE(CONFIG_UNICOAP_DEBUG_LOGGING)) { \
105 printf(__VA_ARGS__); \
106 } \
107 } while (0)
108#endif
109
115
116/* MARK: - Thread */
122#define UNICOAP_THREAD_IDENTIFIER "unicoap"
123
126
130
131/* MARK: - State and initialization */
139typedef struct {
144
145#if IS_USED(MODULE_UNICOAP_SERVER) || defined(DOXYGEN)
148#endif
149
150 /* TODO: Client and advanced server features: Exchange-layer state objects */
152
154static inline void unicoap_set_listeners(unicoap_state_t* state, unicoap_listener_t* listeners) {
155#if IS_USED(MODULE_UNICOAP_SERVER)
156 state->listeners = listeners;
157#else
158 (void)state;
159 (void)listeners;
160#endif
161}
162
165#if IS_USED(MODULE_UNICOAP_SERVER)
166 return state->listeners;
167#else
168 (void)state;
169 return NULL;
170#endif
171}
172
175
178
181
184
187
190
191/* MARK: unicoap_driver_extension_point */
193
194/* MARK: - Private Server Utils */
217int unicoap_resource_find(const unicoap_packet_t* packet, const unicoap_resource_t** resource_ptr,
218 const unicoap_listener_t** listener_ptr);
219
234 const unicoap_resource_t** resource_ptr,
235 const unicoap_message_t* request,
236 const unicoap_endpoint_t* endpoint);
237
244 unicoap_request_context_t* ctx, void* arg);
246
247/* MARK: - Other Utils */
252
258#define UNICOAP_DECL_RECEIVER_STORAGE \
259 uint8_t unicoap_receiver_buffer[CONFIG_UNICOAP_PDU_SIZE_MAX]
260
264#define UNICOAP_DECL_RECEIVER_STORAGE_EXTERN extern UNICOAP_DECL_RECEIVER_STORAGE
265
290static inline bool unicoap_transport_truncate_received(size_t* chunk_size, size_t received)
291{
292 /* cannot use sizeof() here, because we want this function to be inlinable,
293 * extern decl would affect transport.c, too */
294 if (received + *chunk_size > CONFIG_UNICOAP_PDU_SIZE_MAX) {
295 /* Limit chunk size to remaining available storage capacity */
296 _TRANSPORT_DEBUG("truncated\n");
297 _TRANSPORT_DEBUG("warning: recv storage too small, need at least %" PRIdSIZE "\n",
298 received + *chunk_size);
299 *chunk_size = CONFIG_UNICOAP_PDU_SIZE_MAX - received;
300 return true;
301 }
302 return false;
303}
304
305
306#ifndef DOXYGEN
307static inline void __debug_hex(const uint8_t* buffer, size_t size)
308{
309 for (size_t i = 0; i < size; i += 1) {
310 printf("%02X", buffer[i]);
311 }
312}
313
314# define _UNICOAP_DEBUG_HEX(bytes, size) \
315 do { \
316 if (ENABLE_DEBUG) { \
317 __debug_hex(bytes, size); \
318 } \
319 } while (0)
320#endif
321
322#ifdef __cplusplus
323}
324#endif
325
Platform-independent access to architecture details.
Bitfield operations on bitfields of arbitrary length.
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition stdio.h:57
#define CONFIG_UNICOAP_PDU_SIZE_MAX
Capacity of internal buffers.
Definition config.h:202
static unicoap_listener_t * unicoap_get_listeners(unicoap_state_t *state)
Gets listener array from state object.
Definition private.h:164
int unicoap_init_rfc7252_common(event_queue_t *queue)
Initializes the common RFC 7252 driver on the given queue.
void unicoap_state_unlock(void)
Unlocks internal state lock.
int unicoap_init_udp(event_queue_t *queue)
Initializes the CoAP over UDP driver on the given queue.
int unicoap_resource_find(const unicoap_packet_t *packet, const unicoap_resource_t **resource_ptr, const unicoap_listener_t **listener_ptr)
Tries to find a resource for the given packet.
int unicoap_init_dtls(event_queue_t *queue)
Initializes the CoAP over DTLS over UDP driver on the given queue.
void unicoap_assist_emit_diagnostic_missing_driver(unicoap_proto_t proto)
Emit a diagnostic stating that a driver is missing.
static bool unicoap_transport_truncate_received(size_t *chunk_size, size_t received)
Determines whether the chunk of the given size must be truncated to fit into a buffer of size CONFIG_...
Definition private.h:290
int unicoap_resource_handle_well_known_core(unicoap_message_t *message, const unicoap_aux_t *aux, unicoap_request_context_t *ctx, void *arg)
Default resource handler for /.well-known/core
int unicoap_deinit_dtls(event_queue_t *queue)
Deinitializes the CoAP over DTLS over UDP driver on the given queue.
void unicoap_state_lock(void)
Locks internal state lock.
int unicoap_resource_match_request_default(const unicoap_listener_t *listener, const unicoap_resource_t **resource_ptr, const unicoap_message_t *request, const unicoap_endpoint_t *endpoint)
Default request-resource matcher for listeners.
int unicoap_deinit_rfc7252_common(event_queue_t *queue)
DeiInitializes the common RFC 7252 driver on the given queue.
int unicoap_deinit_udp(event_queue_t *queue)
Deinitializes the CoAP over UDP driver on the given queue.
static void unicoap_set_listeners(unicoap_state_t *state, unicoap_listener_t *listeners)
Sets listener array on state object to listeners.
Definition private.h:154
struct unicoap_listener unicoap_listener_t
Typealias for unicoap_listener.
Definition server.h:524
struct unicoap_resource unicoap_resource_t
CoAP resource.
Definition server.h:202
unicoap_proto_t
Transport protocol CoAP is used over.
Definition transport.h:130
#define PRIdSIZE
Macro holding the format specifier to print an ssize_t variable in decimal representation.
struct PTRTAG event_queue_t
event queue structure
Messaging API.
Mutex for thread synchronization.
Packet.
Common interface to the software PRNG.
State and Memo API.
Mutex structure.
Definition mutex.h:36
Auxiliary exchange information.
Definition transport.h:466
A CoAP endpoint.
Definition transport.h:182
Generic CoAP message.
Definition message.h:72
A type acting as an envelope for a message and data connected, like endpoints.
Definition packet.h:45
Request context used to send a response to a given request.
Definition server.h:212
Container the unicoap stacks keeps state in.
Definition private.h:139
unicoap_listener_t * listeners
Groups of resources.
Definition private.h:147
mutex_t lock
Used when allocating listener, transaction, carbon copy, observer, or registration.
Definition private.h:143
Main header for unicoap