Zephyr API Documentation 4.5.0-rc1
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 /* Local address to send the reply from; NET_AF_UNSPEC means let the OS pick. */
65 struct net_sockaddr_storage current_local_addr;
66 struct net_sockaddr_storage pending_local_addr[CONFIG_COAP_SERVICE_PENDING_MESSAGES];
67 /* Cached at coap_service_start() so the hot receive path doesn't recompute it. */
68 bool secure;
69 /* Set when the socket agreed to turn on PKTINFO. Some offloaded
70 * sockets support recvfrom() but not recvmsg().
71 */
72 bool pktinfo_supported;
73};
74
75struct coap_service {
76 const char *name;
77 const char *host;
78 uint16_t *port;
80 struct coap_resource *res_begin;
81 struct coap_resource *res_end;
82 struct coap_service_data *data;
83#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
84 const sec_tag_t *sec_tag_list;
85 size_t sec_tag_list_size;
86#endif
87#if defined(CONFIG_COAP_OSCORE) || defined(__DOXYGEN__)
92 bool oscore_required;
93#endif
94};
95
96#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
97#define __z_coap_service_secure(_sec_tag_list, _sec_tag_list_size) \
98 .sec_tag_list = _sec_tag_list, \
99 .sec_tag_list_size = _sec_tag_list_size,
100#else
101#define __z_coap_service_secure(...)
102#endif
103
104#if defined(CONFIG_COAP_OSCORE)
105/* Statically define the per-service OSCORE exchange cache. */
106#define __z_coap_oscore_cache_define(_name) \
107 static struct coap_oscore_exchange _CONCAT(coap_oscore_exchange_cache_, \
108 _name)[CONFIG_COAP_OSCORE_EXCHANGE_CACHE_SIZE];
109#define __z_coap_oscore_cache_ptr(_name) (_CONCAT(coap_oscore_exchange_cache_, _name))
110#define __z_coap_service_oscore(_required) .oscore_required = (_required),
111#define __z_coap_service_oscore_data(_cache) \
112 .oscore_exchange_cache = (_cache),
113#else
114#define __z_coap_service_oscore(...)
115#define __z_coap_oscore_cache_define(_name)
116#define __z_coap_oscore_cache_ptr(_name) NULL
117#define __z_coap_service_oscore_data(...)
118#endif
119
120/* clang-format off */
121#define __z_coap_service_define(_name, _host, _port, _flags, _res_begin, _res_end, _sec_tag_list, \
122 _sec_tag_list_size, _oscore_required, _oscore_cache) \
123 static struct coap_service_data _CONCAT(coap_service_data_, _name) = { \
124 .sock_fd = -1, \
125 __z_coap_service_oscore_data(_oscore_cache) \
126 }; \
127 const STRUCT_SECTION_ITERABLE(coap_service, _name) = { \
128 .name = STRINGIFY(_name), \
129 .host = _host, \
130 .port = (uint16_t *)(_port), \
131 .flags = _flags, \
132 .res_begin = (_res_begin), \
133 .res_end = (_res_end), \
134 .data = &_CONCAT(coap_service_data_, _name), \
135 __z_coap_service_secure(_sec_tag_list, _sec_tag_list_size) \
136 __z_coap_service_oscore(_oscore_required) \
137 }
138/* clang-format on */
139
141
178#define COAP_RESOURCE_DEFINE(_name, _service, ...) \
179 STRUCT_SECTION_ITERABLE_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, \
180 _name) = __VA_ARGS__
181
199#define COAP_SERVICE_DEFINE(_name, _host, _port, _flags) \
200 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
201 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
202 __z_coap_service_define(_name, _host, _port, _flags, \
203 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
204 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], NULL, 0, \
205 false, NULL)
206
228#define COAPS_SERVICE_DEFINE(_name, _host, _port, _flags, _sec_tag_list, _sec_tag_list_size) \
229 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_ENABLE_DTLS), \
230 "DTLS is required for CoAP secure (CONFIG_NET_SOCKETS_ENABLE_DTLS)"); \
231 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
232 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
233 __z_coap_service_define(_name, _host, _port, _flags, \
234 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
235 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], \
236 _sec_tag_list, _sec_tag_list_size, false, NULL)
237
255#define COAP_SERVICE_DEFINE_OSCORE(_name, _host, _port, _flags, _oscore_required) \
256 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
257 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
258 __z_coap_oscore_cache_define(_name) __z_coap_service_define( \
259 _name, _host, _port, _flags, \
260 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
261 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], NULL, 0, \
262 (_oscore_required), __z_coap_oscore_cache_ptr(_name))
263
279#define COAPS_SERVICE_DEFINE_OSCORE(_name, _host, _port, _flags, _sec_tag_list, \
280 _sec_tag_list_size, _oscore_required) \
281 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_ENABLE_DTLS), \
282 "DTLS is required for CoAP secure (CONFIG_NET_SOCKETS_ENABLE_DTLS)"); \
283 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
284 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
285 __z_coap_oscore_cache_define(_name) \
286 __z_coap_service_define(_name, _host, _port, _flags, \
287 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
288 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], \
289 _sec_tag_list, _sec_tag_list_size, \
290 (_oscore_required), __z_coap_oscore_cache_ptr(_name))
291
297#define COAP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(coap_service, _dst)
298
304#define COAP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
305
312#define COAP_SERVICE_HAS_RESOURCE(_service, _resource) \
313 ((_service)->res_begin <= _resource && _resource < (_service)->res_end)
314
320#define COAP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(coap_service, _it)
321
330#define COAP_RESOURCE_FOREACH(_service, _it) \
331 STRUCT_SECTION_FOREACH_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, _it)
332
341#define COAP_SERVICE_FOREACH_RESOURCE(_service, _it) \
342 for (struct coap_resource *_it = (_service)->res_begin; ({ \
343 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
344 _it < (_service)->res_end; \
345 }); _it++)
346
357int coap_service_start(const struct coap_service *service);
358
368int coap_service_stop(const struct coap_service *service);
369
380int coap_service_is_running(const struct coap_service *service);
381
394int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
395 const struct net_sockaddr *addr, net_socklen_t addr_len,
396 const struct coap_transmission_parameters *params);
397
410int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
411 const struct net_sockaddr *addr, net_socklen_t addr_len,
412 const struct coap_transmission_parameters *params);
413
427int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request,
428 const struct net_sockaddr *addr);
429
440 const struct net_sockaddr *addr);
441
453 const uint8_t *token, uint8_t token_len);
454
458
459#ifdef __cplusplus
460}
461#endif
462
463#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:86
__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:336
OSCORE exchange entry.
Definition coap_oscore.h:163
Representation of a CoAP Packet.
Definition coap.h:357
Represents a request awaiting for an acknowledgment (ACK).
Definition coap.h:428
Description of CoAP resource.
Definition coap.h:316
CoAP transmission parameters.
Definition coap.h:409
Generic sockaddr struct.
Definition net_ip.h:455
Iterable sections helpers.
TLS credentials management.