Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
coap_service.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Basalte bv
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_NET_COAP_SERVICE_H_
14#define ZEPHYR_INCLUDE_NET_COAP_SERVICE_H_
15
16#include <zephyr/net/coap.h>
19
20#if defined(CONFIG_COAP_OSCORE)
22#endif
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
36
42
44#define COAP_SERVICE_AUTOSTART BIT(0)
45
47
49
50struct coap_service_data {
51 int sock_fd;
52 bool close_requested;
53 struct coap_observer observers[CONFIG_COAP_SERVICE_OBSERVERS];
54 struct coap_pending pending[CONFIG_COAP_SERVICE_PENDING_MESSAGES];
55#if defined(CONFIG_COAP_OSCORE) || defined(__DOXYGEN__)
62 struct coap_oscore_exchange *oscore_exchange_cache;
63#endif /* CONFIG_COAP_OSCORE */
64};
65
66struct coap_service {
67 const char *name;
68 const char *host;
69 uint16_t *port;
71 struct coap_resource *res_begin;
72 struct coap_resource *res_end;
73 struct coap_service_data *data;
74#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
75 const sec_tag_t *sec_tag_list;
76 size_t sec_tag_list_size;
77#endif
78#if defined(CONFIG_COAP_OSCORE) || defined(__DOXYGEN__)
83 bool oscore_required;
84#endif
85};
86
87#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
88#define __z_coap_service_secure(_sec_tag_list, _sec_tag_list_size) \
89 .sec_tag_list = _sec_tag_list, \
90 .sec_tag_list_size = _sec_tag_list_size,
91#else
92#define __z_coap_service_secure(...)
93#endif
94
95#if defined(CONFIG_COAP_OSCORE)
96/* Statically define the per-service OSCORE exchange cache. */
97#define __z_coap_oscore_cache_define(_name) \
98 static struct coap_oscore_exchange _CONCAT(coap_oscore_exchange_cache_, \
99 _name)[CONFIG_COAP_OSCORE_EXCHANGE_CACHE_SIZE];
100#define __z_coap_oscore_cache_ptr(_name) (_CONCAT(coap_oscore_exchange_cache_, _name))
101#define __z_coap_service_oscore(_required) .oscore_required = (_required),
102#define __z_coap_service_oscore_data(_cache) \
103 .oscore_exchange_cache = (_cache),
104#else
105#define __z_coap_service_oscore(...)
106#define __z_coap_oscore_cache_define(_name)
107#define __z_coap_oscore_cache_ptr(_name) NULL
108#define __z_coap_service_oscore_data(...)
109#endif
110
111/* clang-format off */
112#define __z_coap_service_define(_name, _host, _port, _flags, _res_begin, _res_end, _sec_tag_list, \
113 _sec_tag_list_size, _oscore_required, _oscore_cache) \
114 static struct coap_service_data _CONCAT(coap_service_data_, _name) = { \
115 .sock_fd = -1, \
116 __z_coap_service_oscore_data(_oscore_cache) \
117 }; \
118 const STRUCT_SECTION_ITERABLE(coap_service, _name) = { \
119 .name = STRINGIFY(_name), \
120 .host = _host, \
121 .port = (uint16_t *)(_port), \
122 .flags = _flags, \
123 .res_begin = (_res_begin), \
124 .res_end = (_res_end), \
125 .data = &_CONCAT(coap_service_data_, _name), \
126 __z_coap_service_secure(_sec_tag_list, _sec_tag_list_size) \
127 __z_coap_service_oscore(_oscore_required) \
128 }
129/* clang-format on */
130
132
169#define COAP_RESOURCE_DEFINE(_name, _service, ...) \
170 STRUCT_SECTION_ITERABLE_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, \
171 _name) = __VA_ARGS__
172
190#define COAP_SERVICE_DEFINE(_name, _host, _port, _flags) \
191 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
192 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
193 __z_coap_service_define(_name, _host, _port, _flags, \
194 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
195 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], NULL, 0, \
196 false, NULL)
197
219#define COAPS_SERVICE_DEFINE(_name, _host, _port, _flags, _sec_tag_list, _sec_tag_list_size) \
220 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_ENABLE_DTLS), \
221 "DTLS is required for CoAP secure (CONFIG_NET_SOCKETS_ENABLE_DTLS)"); \
222 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
223 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
224 __z_coap_service_define(_name, _host, _port, _flags, \
225 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
226 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], \
227 _sec_tag_list, _sec_tag_list_size, false, NULL)
228
246#define COAP_SERVICE_DEFINE_OSCORE(_name, _host, _port, _flags, _oscore_required) \
247 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
248 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
249 __z_coap_oscore_cache_define(_name) __z_coap_service_define( \
250 _name, _host, _port, _flags, \
251 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
252 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], NULL, 0, \
253 (_oscore_required), __z_coap_oscore_cache_ptr(_name))
254
270#define COAPS_SERVICE_DEFINE_OSCORE(_name, _host, _port, _flags, _sec_tag_list, \
271 _sec_tag_list_size, _oscore_required) \
272 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_ENABLE_DTLS), \
273 "DTLS is required for CoAP secure (CONFIG_NET_SOCKETS_ENABLE_DTLS)"); \
274 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
275 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
276 __z_coap_oscore_cache_define(_name) \
277 __z_coap_service_define(_name, _host, _port, _flags, \
278 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
279 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], \
280 _sec_tag_list, _sec_tag_list_size, \
281 (_oscore_required), __z_coap_oscore_cache_ptr(_name))
282
288#define COAP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(coap_service, _dst)
289
295#define COAP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
296
303#define COAP_SERVICE_HAS_RESOURCE(_service, _resource) \
304 ((_service)->res_begin <= _resource && _resource < (_service)->res_end)
305
311#define COAP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(coap_service, _it)
312
321#define COAP_RESOURCE_FOREACH(_service, _it) \
322 STRUCT_SECTION_FOREACH_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, _it)
323
332#define COAP_SERVICE_FOREACH_RESOURCE(_service, _it) \
333 for (struct coap_resource *_it = (_service)->res_begin; ({ \
334 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
335 _it < (_service)->res_end; \
336 }); _it++)
337
348int coap_service_start(const struct coap_service *service);
349
359int coap_service_stop(const struct coap_service *service);
360
371int coap_service_is_running(const struct coap_service *service);
372
385int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
386 const struct net_sockaddr *addr, net_socklen_t addr_len,
387 const struct coap_transmission_parameters *params);
388
401int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
402 const struct net_sockaddr *addr, net_socklen_t addr_len,
403 const struct coap_transmission_parameters *params);
404
418int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request,
419 const struct net_sockaddr *addr);
420
431 const struct net_sockaddr *addr);
432
444 const uint8_t *token, uint8_t token_len);
445
449
450#ifdef __cplusplus
451}
452#endif
453
454#endif /* ZEPHYR_INCLUDE_NET_COAP_SERVICE_H_ */
CoAP implementation for Zephyr.
CoAP OSCORE API.
int coap_service_is_running(const struct coap_service *service)
Query the provided service running state.
int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt, const struct net_sockaddr *addr, net_socklen_t addr_len, const struct coap_transmission_parameters *params)
Send a CoAP message from the provided service .
int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt, const struct net_sockaddr *addr, net_socklen_t addr_len, const struct coap_transmission_parameters *params)
Send a CoAP message from the provided resource .
int coap_service_stop(const struct coap_service *service)
Stop the provided service .
int coap_resource_remove_observer_by_addr(struct coap_resource *resource, const struct net_sockaddr *addr)
Lookup an observer by address and remove it from the resource .
int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request, const struct net_sockaddr *addr)
Parse a CoAP observe request for the provided resource .
int coap_service_start(const struct coap_service *service)
Start the provided service .
int coap_resource_remove_observer_by_token(struct coap_resource *resource, const uint8_t *token, uint8_t token_len)
Lookup an observer by token and remove it from the resource .
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:172
int sec_tag_t
Secure tag, a reference to TLS credential.
Definition tls_credentials.h:90
flags
Definition parser.h:97
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Represents a remote device that is observing a local resource.
Definition coap.h:333
OSCORE exchange entry.
Definition coap_oscore.h:163
Representation of a CoAP Packet.
Definition coap.h:354
Represents a request awaiting for an acknowledgment (ACK).
Definition coap.h:425
Description of CoAP resource.
Definition coap.h:313
CoAP transmission parameters.
Definition coap.h:406
Generic sockaddr struct.
Definition net_ip.h:455
Iterable sections helpers.
TLS credentials management.