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#if IS_USED(MODULE_UNICOAP_DRIVER_SLIPMUX) || defined(DOXYGEN)
30# include "slipdev.h"
31#endif
32
33/* MARK: unicoap_driver_extension_point */
34
35#include "net/unicoap/message.h"
36#include "net/unicoap/options.h"
37
44
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/* MARK: - CoAP URI schemes */
66#define UNICOAP_SCHEME "coap"
67
71#define UNICOAP_SCHEME_UDP "coap"
72
76#define UNICOAP_SCHEME_DTLS "coaps"
77
81#define UNICOAP_SCHEME_TCP "coap+tcp"
82
86#define UNICOAP_SCHEME_TLS "coaps+tcp"
87
91#define UNICOAP_SCHEME_WEBSOCKET "coap+ws"
92
96#define UNICOAP_SCHEME_WEBSOCKET_TLS "coaps+ws"
97
101#define UNICOAP_SCHEME_BLE_GATT "coap"
102
106#define UNICOAP_SCHEME_SLIPMUX "coap+uart"
107
114#define UNICOAP_DOMAIN_BLE ".ble.arpa"
115
116/* MARK: unicoap_driver_extension_point */
118
119/* MARK: - Endpoints */
129#define UNICOAP_PROTO_UNSPECIFIED (0)
130
134#define UNICOAP_PROTO_FLAG_RELIABLE_TRANSPORT (1)
135
141typedef enum {
144
147
150
151 /* MARK: unicoap_driver_extension_point */
152} __attribute__((__packed__)) unicoap_proto_t;
153
165{
167}
168
185{
186 switch (proto) {
189 return true;
191 return false;
192 /* If a new transport driver does not use RIOT's socket API,
193 * such as CoAP over GATT, return false here. */
194 /* MARK: unicoap_driver_extension_point */
195 default:
196 return false;
197 }
198}
199
203typedef struct {
206
207 union {
208 /* union members are guaranteed to start at offset zero */
209
210#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
213
214 /* _tl_ep acts as a view on both udp_ep and dtls_ep. */
215
218
221#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
222#if IS_USED(MODULE_UNICOAP_DRIVER_SLIPMUX) || defined(DOXYGEN)
223 slipdev_t *slipmux_ep;
224#endif /* IS_USED(MODULE_UNICOAP_DRIVER_SLIPMUX) || defined(DOXYGEN) */
225 /* MARK: unicoap_driver_extension_point */
226 };
228
229#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
241{
242 return &endpoint->udp_ep;
243}
244
256{
257 return &endpoint->dtls_ep;
258}
259
260# ifndef DOXYGEN
270static inline struct _sock_tl_ep* _unicoap_endpoint_get_tl(unicoap_endpoint_t* endpoint)
271{
272 return &endpoint->_tl_ep;
273}
274# endif /* !defined(DOXYGEN) */
275#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
277
278/* MARK: - Conversions and Tools */
283
284/* TODO: Client: URI */
285
293
303 const unicoap_endpoint_t* destination);
304
312
318#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
320#else
321static inline void unicoap_print_sock_tl_ep(const void* ep)
322{
323 (void)ep;
324 assert(false);
325}
326#endif
335
340/* MARK: - Sockets */
346#if IS_USED(MODULE_UNICOAP_DRIVER_UDP) || defined(DOXYGEN)
348#else
349static inline
351{
352 return NULL;
353}
354#endif
355
356#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
357/* sock_udp_t requires an implementation which is not present if someone uses unicoap
358 * without a driver, i.e., just message APIs. In this case, unicoap_sock_support is not imported. */
359
372# if IS_USED(MODULE_UNICOAP_DRIVER_UDP) || defined(DOXYGEN)
374# else
375static inline
377{
378 (void)socket;
379 (void)local;
380 return 0;
381}
382# endif
383
392# if IS_USED(MODULE_UNICOAP_DRIVER_UDP) || defined(DOXYGEN)
394# else
395static inline
397{
398 (void)socket;
399 return 0;
400}
401# endif
402#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
405
410/* MARK: - Sockets */
416#if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
418#else
419static inline
421{
422 return NULL;
423}
424#endif
425
426#if IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN)
427/* sock_udp_t requires an implementation which is not present if someone uses unicoap
428 * without a driver, i.e., just message APIs. In this case, unicoap_sock_support is not imported. */
429
444# if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
446 sock_udp_ep_t* local);
447# else
448static inline
450 sock_udp_ep_t* local)
451{
452 (void)socket;
453 (void)base_socket;
454 (void)local;
455 return 0;
456}
457# endif
458
468# if IS_USED(MODULE_UNICOAP_DRIVER_DTLS) || defined(DOXYGEN)
470# else
471static inline
473{
474 (void)socket;
475 return 0;
476}
477# endif
478#endif /* IS_USED(MODULE_UNICOAP_SOCK_SUPPORT) || defined(DOXYGEN) */
480
481/* MARK: - Utils */
505
506
508
509#ifdef __cplusplus
510}
511#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:141
#define UNICOAP_PROTO_FLAG_RELIABLE_TRANSPORT
Protocol number flag indicating the transport is considered reliable on its own.
Definition transport.h:134
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:240
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:164
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:184
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:255
@ UNICOAP_PROTO_UDP
CoAP over UDP endpoint.
Definition transport.h:143
@ UNICOAP_PROTO_SLIPMUX
CoAP over Slipmux endpoint.
Definition transport.h:149
@ UNICOAP_PROTO_DTLS
CoAP over DTLS over UDP endpoint.
Definition transport.h:146
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.
SLIP device definitions.
DTLS sock definitions.
TCP sock definitions.
UDP sock definitions.
Common sock API definitions.
Common IP-based transport layer end point.
Definition sock.h:211
Device descriptor for slipdev.
Definition slipdev.h:162
Auxiliary exchange information.
Definition transport.h:489
const unicoap_message_properties_t * properties
Message properties.
Definition transport.h:503
const unicoap_endpoint_t * local
The local CoAP endpoint in this exchange.
Definition transport.h:498
const unicoap_endpoint_t * remote
The remote CoAP endpoint in this exchange.
Definition transport.h:493
A CoAP endpoint.
Definition transport.h:203
sock_udp_ep_t dtls_ep
RIOT sock DTLS endpoint.
Definition transport.h:220
struct _sock_tl_ep _tl_ep
Transport layer endpoint.
Definition transport.h:212
unicoap_proto_t proto
Protocol number.
Definition transport.h:205
sock_udp_ep_t udp_ep
RIOT sock UDP endpoint.
Definition transport.h:217
Properties of a CoAP message.
Definition message.h:297
Utility library for comparing and computing timestamps.
sock utility function definitions