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#include <alloca.h>
11
12#include "mutex.h"
13#include "bitfield.h"
14#include "architecture.h"
15#include "random.h"
16
17#include "net/unicoap.h"
18
19#include "private/state.h"
20#include "private/packet.h"
21#include "private/messaging.h"
22
32
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/* MARK: - Debugging */
49#define UNICOAP_DEBUG_PREFIX "coap"
50
51#ifndef DOXYGEN
52# define _UNICOAP_NEED_HAVE "(need %" PRIuSIZE ", have %" PRIuSIZE ")"
53#endif
54
55#ifndef DOXYGEN
63# define _UNICOAP_PREFIX_DEBUG(category, ...) DEBUG(UNICOAP_DEBUG_PREFIX category ": " __VA_ARGS__)
64# define UNICOAP_DEBUG(...) _UNICOAP_PREFIX_DEBUG("", __VA_ARGS__)
65# define _OPTIONS_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".options", __VA_ARGS__)
66# define _MESSAGING_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".messaging", __VA_ARGS__)
67# define _STATE_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".state", __VA_ARGS__)
68# define _STATE_EVENT_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".state.sched", __VA_ARGS__)
69# define _STATE_NOTIF_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".state.notif", __VA_ARGS__)
70# define _TRANSPORT_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".transport", __VA_ARGS__)
71# define _SERVER_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".server", __VA_ARGS__)
72# define _CLIENT_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".client", __VA_ARGS__)
73# define _URI_DEBUG(...) _UNICOAP_PREFIX_DEBUG(".uri", __VA_ARGS__)
74# define API_WARNING(message) "WARNING: " message "\n"
75# define API_ERROR(message) "ERROR: " message "\n"
76# define API_MISUSE(message) "API MISUSE: " message "\n"
77# define INTERNAL_ERROR(message) "BUG: " message "\n"
78# define FIXIT(message) "+++ FIXIT: " message "\n"
79#endif
80
81#if ENABLE_DEBUG
85# define DEBUG_ENDPOINT(endpoint) unicoap_print_endpoint(endpoint)
89# define DEBUG_FLAGS(flags, role) unicoap_print_flags(flags, role)
90#else
94# define DEBUG_ENDPOINT(endpoint) (void)endpoint
98# define DEBUG_FLAGS(flags, role) \
99 (void)flags; \
100 (void)role
101#endif
102
103#ifndef DOXYGEN
104# if IS_ACTIVE(CONFIG_UNICOAP_ASSIST)
105# include <stdio.h>
106# endif
107# define unicoap_assist(...) \
108 do { \
109 if (IS_ACTIVE(CONFIG_UNICOAP_ASSIST) || IS_ACTIVE(CONFIG_UNICOAP_DEBUG_LOGGING)) { \
110 printf(__VA_ARGS__); \
111 } \
112 } while (0)
113#endif
114
120
121/* MARK: - Thread */
127#define UNICOAP_THREAD_IDENTIFIER "unicoap"
128
131
134
138
139/* MARK: - State and initialization */
147typedef struct {
152
153#if IS_USED(MODULE_UNICOAP_SERVER) || defined(DOXYGEN)
156#endif
157
158#if UNICOAP_HAVE_CLIENT_STATE
161#endif
162
163 /* TODO: Client and advanced server features: Exchange-layer state objects */
165
167static inline void unicoap_set_listeners(unicoap_state_t* state, unicoap_listener_t* listeners) {
168#if IS_USED(MODULE_UNICOAP_SERVER)
169 state->listeners = listeners;
170#else
171 (void)state;
172 (void)listeners;
173#endif
174}
175
178#if IS_USED(MODULE_UNICOAP_SERVER)
179 return state->listeners;
180#else
181 (void)state;
182 return NULL;
183#endif
184}
185
188
191
194
197
200
203
206
209
210/* MARK: unicoap_driver_extension_point */
212
213/* MARK: - Private Server Utils */
236int unicoap_resource_find(const unicoap_packet_t* packet, const unicoap_resource_t** resource_ptr,
237 const unicoap_listener_t** listener_ptr);
238
253 const unicoap_resource_t** resource_ptr,
254 const unicoap_message_t* request,
255 const unicoap_endpoint_t* endpoint);
256
263 unicoap_request_context_t* ctx, void* arg);
265
266/* MARK: - Private message and option helpers */
271#ifndef DOXYGEN
272# define _UNICOAP_OPTIONS_ALLOCA(_buf, _name, capacity, _static) \
273 uint8_t* _buf = alloca(capacity); \
274 unicoap_options_t* _name = alloca(sizeof(unicoap_options_t)); \
275 memset(_name, 0, sizeof(unicoap_options_t)); \
276 (_name)->entries->data = _buf; \
277 (_name)->storage_capacity = capacity;
278#endif
279
293#define UNICOAP_OPTIONS_ALLOCA(name, capacity) \
294 _UNICOAP_OPTIONS_ALLOCA(_CONCAT3(name, _storage, __LINE__), name, capacity,)
295
296
297/* MARK: - Other Utils */
302
308#define UNICOAP_DECL_RECEIVER_STORAGE \
309 uint8_t unicoap_receiver_buffer[CONFIG_UNICOAP_PDU_SIZE_MAX]
310
314#define UNICOAP_DECL_RECEIVER_STORAGE_EXTERN extern UNICOAP_DECL_RECEIVER_STORAGE
315
340static inline bool unicoap_transport_truncate_received(size_t* chunk_size, size_t received)
341{
342 /* cannot use sizeof() here, because we want this function to be inlinable,
343 * extern decl would affect transport.c, too */
344 if (received + *chunk_size > CONFIG_UNICOAP_PDU_SIZE_MAX) {
345 /* Limit chunk size to remaining available storage capacity */
346 _TRANSPORT_DEBUG("truncated\n");
347 _TRANSPORT_DEBUG("warning: recv storage too small, need at least %" PRIdSIZE "\n",
348 received + *chunk_size);
349 *chunk_size = CONFIG_UNICOAP_PDU_SIZE_MAX - received;
350 return true;
351 }
352 return false;
353}
354
355
356#ifndef DOXYGEN
357static inline void __debug_hex(const uint8_t* buffer, size_t size)
358{
359 for (size_t i = 0; i < size; i += 1) {
360 printf("%02X", buffer[i]);
361 }
362}
363
364# define _UNICOAP_DEBUG_HEX(bytes, size) \
365 do { \
366 if (ENABLE_DEBUG) { \
367 __debug_hex(bytes, size); \
368 } \
369 } while (0)
370#endif
371
372#ifdef __cplusplus
373}
374#endif
375
Platform-independent access to architecture details.
Bitfield operations on bitfields of arbitrary length.
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:138
#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:228
#define CONFIG_UNICOAP_CLIENT_MEMOS_CAPACITY
Maximum number of pending requests that can be started using the client API.
Definition config.h:249
static unicoap_listener_t * unicoap_get_listeners(unicoap_state_t *state)
Gets listener array from state object.
Definition private.h:177
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:340
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.
int unicoap_init_slipmux(event_queue_t *queue)
Initializes the CoAP over Slipmux driver on the given queue.
int unicoap_deinit_slipmux(event_queue_t *queue)
Deinitializes the CoAP over Slipmux 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:167
kernel_pid_t _unicoap_pid
PID of unicoap thread.
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:142
#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:808
Client exchange.
Definition state.h:415
A CoAP endpoint.
Definition transport.h:204
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:147
unicoap_listener_t * listeners
Groups of resources.
Definition private.h:155
mutex_t lock
Used when allocating listener, transaction, carbon copy, observer, or registration.
Definition private.h:151
Main header for unicoap