Loading...
Searching...
No Matches
state.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 <stdlib.h>
10#include "ztimer.h"
11#include "random.h"
12#include "event.h"
13#include "container.h"
14
15#include "net/unicoap/server.h"
16
17#include "private/packet.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
28
34
35/* MARK: - Event Scheduling */
40
72typedef struct {
79 event_t super;
80
88 ztimer_t ztimer;
89
90#if DEVELHELP || defined (DOXYGEN)
96 const char* name;
97#endif
98} unicoap_scheduled_event_t;
99
105static inline const char* unicoap_scheduled_event_name(unicoap_scheduled_event_t* event) {
106 (void)event;
107#if DEVELHELP
108 return event->name;
109#else
110 return NULL;
111#endif
112}
113
119static inline unicoap_scheduled_event_t* unicoap_scheduled_event_of_event(event_t* event) {
120 return container_of(event, unicoap_scheduled_event_t, super);
121}
122
124#define UNICOAP_CLOCK ZTIMER_MSEC
125
134typedef void (*unicoap_event_callback_t)(unicoap_scheduled_event_t* event);
135
148void unicoap_event_schedule(unicoap_scheduled_event_t* event, unicoap_event_callback_t callback,
149 uint32_t duration, const char* name);
150
160void unicoap_event_reschedule(unicoap_scheduled_event_t* event, uint32_t duration);
171void unicoap_event_cancel(unicoap_scheduled_event_t* event);
173
174/* MARK: - Common state */
179
182#define UNICOAP_HAVE_MESSAGING_STATE \
183 IS_USED(MODULE_UNICOAP_DRIVER_RFC7252_COMMON)
184/* MARK: unicoap_driver_extension_point */
185
189#define UNICOAP_HAVE_CLIENT_STATE \
190 IS_USED(MODULE_UNICOAP_CLIENT) && CONFIG_UNICOAP_CLIENT_MEMOS_CAPACITY > 0
191
199typedef struct {
206 struct {
210 unicoap_scheduled_event_t timeout;
211 } exchange;
212
220 union {
221#if UNICOAP_HAVE_MESSAGING_STATE || defined(DOXYGEN)
222
226 void* state;
227#endif
228 } messaging;
229
236
243 (void)memo;
244#if UNICOAP_HAVE_MESSAGING_STATE
245 return memo->messaging.state;
246#else
247 return NULL;
248#endif
249}
250
256static inline unicoap_memo_t* unicoap_memo_of_timeout(unicoap_scheduled_event_t* timeout) {
257 return container_of(timeout, unicoap_memo_t, exchange.timeout);
258}
259
268
269
270/* MARK: - Layered notification system */
285
296#define UNICOAP_LAYER_NOTIFICATION_ASYNC_FAILURE (~(~0U >> 1))
297
308
315 return -abs(error);
316}
317
325#define UNICOAP_LAYER_NOTIFICATION_STATE_RELEASE (0)
326
332#define UNICOAP_LAYER_NOTIFICATION_STATE_ALLOC (1)
333
345 void* arg, unicoap_proto_t proto);
346
371void unicoap_exchange_notify(void* state, unicoap_layer_notification_t type, void* arg);
372
384
385/* MARK: - Client state */
394typedef union {
397 void* _any;
398} unicoap_callback_t;
399
405static inline bool unicoap_callback_is_present(const unicoap_callback_t callback) {
406 /* Per ISO-C, union members all start at the same address, hence checking if
407 the first member is NULL is fine as long as the second member also is a pointer.
408 Both are function pointers. Global reasoning says we're fine here. */
409 return callback._any != NULL;
410}
411
415typedef struct {
418
420 unicoap_callback_t callback;
421
424
427
428#if IS_USED(MODULE_UNICOAP_CLIENT_CANCELLATION)
429 uint16_t reference_id;
430#endif
431
437
446
455
461static inline unicoap_client_memo_t* unicoap_client_memo_of_timeout(unicoap_scheduled_event_t* timeout) {
463}
464
474
479
490
501 const uint8_t* token, size_t token_length);
502
503
519
534
550
551#ifdef __cplusplus
552}
553#endif
554
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Common macros and compiler attributes/pragmas configuration.
#define container_of(PTR, TYPE, MEMBER)
Returns the container of a pointer to a member.
Definition container.h:58
int(* unicoap_response_callback_t)(const unicoap_message_t *response, const unicoap_aux_t *aux, int error, void *arg)
Response or error callback.
Definition client.h:118
unicoap_request_flags_t
Flags for enabling advanced features in client exchanges.
Definition client.h:47
#define CONFIG_UNICOAP_GENERATED_TOKEN_LENGTH
Length of tokens generated by the unicoap client API.
Definition config.h:207
uint32_t unicoap_block_option_t
Block1 and Block2 option value
Definition generated.h:1761
static unicoap_client_memo_t * unicoap_client_memo_of_timeout(unicoap_scheduled_event_t *timeout)
Returns client memo of scheduled event.
Definition state.h:461
void unicoap_event_cancel(unicoap_scheduled_event_t *event)
Cancels the event.
void unicoap_messaging_notify(void *state, unicoap_layer_notification_t type, void *arg, unicoap_proto_t proto)
Informs messaging layer of event.
void unicoap_exchange_notify(void *state, unicoap_layer_notification_t type, void *arg)
Informs exchange layer of event.
void unicoap_client_memo_free(unicoap_client_memo_t *memo, int error)
Frees memo and associated buffers.
int unicoap_layer_notification_t
A signal sent from one layer to another indicating a change to a state object.
Definition state.h:284
#define UNICOAP_LAYER_NOTIFICATION_ASYNC_FAILURE
Event indicating a failure on the layer this message is sent from.
Definition state.h:296
void unicoap_event_reschedule(unicoap_scheduled_event_t *event, uint32_t duration)
Discards the currently set timeout, and reschedules the event to be posted in duration ms.
void unicoap_event_schedule(unicoap_scheduled_event_t *event, unicoap_event_callback_t callback, uint32_t duration, const char *name)
Schedules an event on the internal unicoap queue.
static unicoap_client_memo_t * unicoap_client_memo_of_event(event_t *event)
Returns client memo of event.
Definition state.h:452
unicoap_client_memo_t * unicoap_client_memo_find_refno(int refno)
Tries to find client state object based on reference number.
static unicoap_memo_t * unicoap_memo_of_timeout(unicoap_scheduled_event_t *timeout)
Returns memo of scheduled event.
Definition state.h:256
static unicoap_memo_t * unicoap_memo_of_event(event_t *event)
Returns memo of scheduled event.
Definition state.h:265
static unicoap_scheduled_event_t * unicoap_scheduled_event_of_event(event_t *event)
Returns scheduled event subclass of event.
Definition state.h:119
static const char * unicoap_scheduled_event_name(unicoap_scheduled_event_t *event)
Name of scheduled event.
Definition state.h:105
void(* unicoap_event_callback_t)(unicoap_scheduled_event_t *event)
Scheduled event callback.
Definition state.h:134
static bool unicoap_callback_is_present(const unicoap_callback_t callback)
Determines whether the given callback is not NULL
Definition state.h:405
static unicoap_layer_notification_t unicoap_layer_notification_async_failure_from_errno(int error)
Converts error number into notification.
Definition state.h:314
void unicoap_exchange_notify_all(const unicoap_endpoint_t *endpoint, unicoap_layer_notification_t type, void *arg)
Informs exchange layer of event that applies to all exchange state objects associated with given endp...
int unicoap_client_memo_assign_refno(unicoap_client_memo_t *memo)
Assigns a refno to the given client memo.
unicoap_client_memo_t * unicoap_client_memo_create(const unicoap_endpoint_t *endpoint)
Allocates and sets up a new memo.
static unicoap_client_memo_t * unicoap_client_memo_of_super(unicoap_memo_t *memo)
Returns client memo of common memo state object.
Definition state.h:443
int unicoap_client_callback_success(unicoap_client_memo_t *memo, const unicoap_packet_t *packet, unicoap_block_option_t block)
Invokes the client callback.
static void * unicoap_memo_messaging_state(unicoap_memo_t *memo)
Returns messaging state associated with memo.
Definition state.h:242
void unicoap_client_callback_failure(unicoap_client_memo_t *memo, int error)
Invokes the client callback indicating an error occurred.
unicoap_client_memo_t * unicoap_client_memo_find_token(const unicoap_endpoint_t *endpoint, const uint8_t *token, size_t token_length)
Tries to find a client exchange memo by endpoint and token.
static int unicoap_layer_notification_async_failure_to_errno(unicoap_layer_notification_t type)
Converts notification to negative error number.
Definition state.h:303
unicoap_proto_t
Transport protocol CoAP is used over.
Definition transport.h:142
struct event event_t
event structure forward declaration
Definition event.h:132
Packet.
Common interface to the software PRNG.
Server APIs.
event structure
Definition event.h:142
Client exchange.
Definition state.h:415
unicoap_callback_t callback
Callback function registered by the client API.
Definition state.h:420
unicoap_memo_t super
Common client/server memo super struct.
Definition state.h:417
void * callback_arg
Argument to be passed to callback.
Definition state.h:423
uint8_t token[CONFIG_UNICOAP_GENERATED_TOKEN_LENGTH]
Request token generated by unicoap.
Definition state.h:426
unicoap_request_flags_t flags
Request flags from application, from the original call to unicoap_send_request_*
Definition state.h:435
A CoAP endpoint.
Definition transport.h:204
A type used to retain state spanning across a single or multiple message exchanges.
Definition state.h:199
unicoap_endpoint_t endpoint
The remote CoAP endpoint unicoap maintains an exchange with (client/server) or stores messaging state...
Definition state.h:234
void * state
Messaging state.
Definition state.h:226
unicoap_scheduled_event_t timeout
Timeout.
Definition state.h:210
union unicoap_memo_t::@101272157352214111246162252141132147115334324007 messaging
Messaging state.
A type acting as an envelope for a message and data connected, like endpoints.
Definition packet.h:45
ztimer structure
Definition ztimer.h:316
ztimer API