Loading...
Searching...
No Matches
transport.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#include "iolist.h"
12
13#include "timex.h"
14#include "uri_parser.h"
15#include "modules.h"
16
17#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
18# include "net/sock.h"
19# include "net/sock/async/event.h"
20# include "net/sock/util.h"
21# include "net/sock/udp.h"
22# include "net/sock/tcp.h"
23#endif
24
25#if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
26# include "net/sock/dtls.h"
27#endif
28
29/* MARK: unicoap_driver_extension_point */
30
31#include "net/unicoap/message.h"
32#include "net/unicoap/options.h"
33
40
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/* MARK: - CoAP URI schemes */
62#define UNICOAP_SCHEME "coap"
63
67#define UNICOAP_SCHEME_UDP "coap"
68
72#define UNICOAP_SCHEME_DTLS "coaps"
73
77#define UNICOAP_SCHEME_TCP "coap+tcp"
78
82#define UNICOAP_SCHEME_TLS "coaps+tcp"
83
87#define UNICOAP_SCHEME_WEBSOCKET "coap+ws"
88
92#define UNICOAP_SCHEME_WEBSOCKET_TLS "coaps+ws"
93
97#define UNICOAP_SCHEME_BLE_GATT "coap"
98
105#define UNICOAP_DOMAIN_BLE ".ble.arpa"
106
107/* MARK: unicoap_driver_extension_point */
109
110/* MARK: - Endpoints */
120#define UNICOAP_PROTO_UNSPECIFIED (0)
121
125#define UNICOAP_PROTO_FLAG_RELIABLE_TRANSPORT (1)
126
130typedef enum {
133
136
137 /* MARK: unicoap_driver_extension_point */
138} __attribute__((__packed__)) unicoap_proto_t;
139
151{
153}
154
171{
172 (void)proto;
173 /* If a new transport driver does not use RIOT's socket API,
174 * such as CoAP over GATT, return false here. */
175 /* MARK: unicoap_driver_extension_point */
176 return true;
177}
178
182typedef struct {
185
186 union {
187 /* union members are guaranteed to start at offset zero */
188
189#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
192
193 /* _tl_ep acts as a view on both udp_ep and dtls_ep. */
194
197
200#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
201
202 /* MARK: unicoap_driver_extension_point */
203 };
205
206#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
218{
219 return &endpoint->udp_ep;
220}
221
233{
234 return &endpoint->dtls_ep;
235}
236
237# ifndef DOXYGEN
247static inline struct _sock_tl_ep* _unicoap_endpoint_get_tl(unicoap_endpoint_t* endpoint)
248{
249 return &endpoint->_tl_ep;
250}
251# endif /* !defined(DOXYGEN) */
252#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
254
255/* MARK: - Conversions and Tools */
260
261/* TODO: Client: URI */
262
270
280 const unicoap_endpoint_t* destination);
281
289
295#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
297#else
298static inline void unicoap_print_sock_tl_ep(const void* ep)
299{
300 (void)ep;
301 assert(false);
302}
303#endif
312
317/* MARK: - Sockets */
323#if IS_USED(MODULE_UNICOAP_DRIVER_UDP) || defined(DOXYGEN)
325#else
326static inline
328{
329 return NULL;
330}
331#endif
332
333#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
334/* sock_udp_t requires an implementation which is not present if someone uses unicoap
335 * without a driver, i.e., just message APIs. In this case, unicoap_sock_support is not imported. */
336
349# if IS_USED(MODULE_UNICOAP_DRIVER_UDP) || defined(DOXYGEN)
351# else
352static inline
354{
355 (void)socket;
356 (void)local;
357 return 0;
358}
359# endif
360
369# if IS_USED(MODULE_UNICOAP_DRIVER_UDP) || defined(DOXYGEN)
371# else
372static inline
374{
375 (void)socket;
376 return 0;
377}
378# endif
379#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
382
387/* MARK: - Sockets */
393#if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
395#else
396static inline
398{
399 return NULL;
400}
401#endif
402
403#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
404/* sock_udp_t requires an implementation which is not present if someone uses unicoap
405 * without a driver, i.e., just message APIs. In this case, unicoap_sock_support is not imported. */
406
421# if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
423 sock_udp_ep_t* local);
424# else
425static inline
427 sock_udp_ep_t* local)
428{
429 (void)socket;
430 (void)base_socket;
431 (void)local;
432 return 0;
433}
434# endif
435
445# if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
447# else
448static inline
450{
451 (void)socket;
452 return 0;
453}
454# endif
455#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
457
458/* MARK: - Utils */
482
483
485
486#ifdef __cplusplus
487}
488#endif
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
struct sock_udp sock_udp_t
forward declare for async
Definition types.h:136
struct sock_dtls sock_dtls_t
forward declare for async
Definition types.h:44
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:292
sock_dtls_t * unicoap_transport_dtls_get_socket(void)
Returns the internal DTLS socket.
int unicoap_transport_dtls_add_socket(sock_dtls_t *socket, sock_udp_t *base_socket, sock_udp_ep_t *local)
Adds DTLS socket for client and server functionality.
sock_udp_t * unicoap_transport_udp_get_socket(void)
Returns the internal UDP socket.
int unicoap_transport_dtls_remove_socket(sock_dtls_t *socket)
Removes DTLS socket previously added manually.
int unicoap_transport_udp_add_socket(sock_udp_t *socket, sock_udp_ep_t *local)
Adds UDP socket for client and server functionality.
int unicoap_transport_udp_remove_socket(sock_udp_t *socket)
Removes UDP socket previously added manually.
unicoap_proto_t
Transport protocol CoAP is used over.
Definition transport.h:130
#define UNICOAP_PROTO_FLAG_RELIABLE_TRANSPORT
Protocol number flag indicating the transport is considered reliable on its own.
Definition transport.h:125
void unicoap_print_endpoint(const unicoap_endpoint_t *endpoint)
Prints the given CoAP endpoint.
const char * unicoap_string_from_proto(unicoap_proto_t proto)
Returns scheme from protocol number.
static sock_udp_ep_t * unicoap_endpoint_get_udp(unicoap_endpoint_t *endpoint)
Retrieves UDP endpoint from CoAP endpoint.
Definition transport.h:217
void unicoap_print_sock_tl_ep(const struct _sock_tl_ep *ep)
Prints the given transport layer endpoint.
static bool unicoap_transport_is_reliable(unicoap_proto_t proto)
Determines whether the given transport protocol is reliable.
Definition transport.h:150
bool unicoap_endpoint_is_equal(const unicoap_endpoint_t *source, const unicoap_endpoint_t *destination)
Compares two endpoints.
static bool unicoap_transport_uses_sock_tl_ep(unicoap_proto_t proto)
Determines whether the given transport driver uses a transport layer socket in RIOT.
Definition transport.h:170
bool unicoap_endpoint_is_multicast(const unicoap_endpoint_t *endpoint)
Determines whether the given endpoint is a multicast endpoint.
static sock_udp_ep_t * unicoap_endpoint_get_dtls(unicoap_endpoint_t *endpoint)
Retrieves DTLS endpoint from CoAP endpoint.
Definition transport.h:232
@ UNICOAP_PROTO_UDP
CoAP over UDP endpoint.
Definition transport.h:132
@ UNICOAP_PROTO_DTLS
CoAP over DTLS over UDP endpoint.
Definition transport.h:135
int socket(int domain, int type, int protocol)
Create an endpoint for communication.
iolist scatter / gather IO
CoAP Message API.
Common macros and compiler attributes/pragmas configuration.
Asynchronous sock using Event Queue definitions.
Options umbrella header.
DTLS sock definitions.
TCP sock definitions.
UDP sock definitions.
Common sock API definitions.
Common IP-based transport layer end point.
Definition sock.h:211
Auxiliary exchange information.
Definition transport.h:466
const unicoap_message_properties_t * properties
Message properties.
Definition transport.h:480
const unicoap_endpoint_t * local
The local CoAP endpoint in this exchange.
Definition transport.h:475
const unicoap_endpoint_t * remote
The remote CoAP endpoint in this exchange.
Definition transport.h:470
A CoAP endpoint.
Definition transport.h:182
sock_udp_ep_t dtls_ep
RIOT sock DTLS endpoint.
Definition transport.h:199
struct _sock_tl_ep _tl_ep
Transport layer endpoint.
Definition transport.h:191
unicoap_proto_t proto
Protocol number.
Definition transport.h:184
sock_udp_ep_t udp_ep
RIOT sock UDP endpoint.
Definition transport.h:196
Properties of a CoAP message.
Definition message.h:297
Utility library for comparing and computing timestamps.
sock utility function definitions