Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
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#ifdef __cplusplus
21extern "C" {
22#endif
23
40#define COAP_SERVICE_AUTOSTART BIT(0)
41
46struct coap_service_data {
47 int sock_fd;
48 struct coap_observer observers[CONFIG_COAP_SERVICE_OBSERVERS];
49 struct coap_pending pending[CONFIG_COAP_SERVICE_PENDING_MESSAGES];
50};
51
52struct coap_service {
53 const char *name;
54 const char *host;
55 uint16_t *port;
57 struct coap_resource *res_begin;
58 struct coap_resource *res_end;
59 struct coap_service_data *data;
60#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
61 const sec_tag_t *sec_tag_list;
62 size_t sec_tag_list_size;
63#endif
64};
65
66#if defined(CONFIG_NET_SOCKETS_ENABLE_DTLS)
67#define __z_coap_service_secure(_sec_tag_list, _sec_tag_list_size) \
68 .sec_tag_list = _sec_tag_list, \
69 .sec_tag_list_size = _sec_tag_list_size,
70#else
71#define __z_coap_service_secure(...)
72#endif
73
74#define __z_coap_service_define(_name, _host, _port, _flags, _res_begin, _res_end, \
75 _sec_tag_list, _sec_tag_list_size) \
76 static struct coap_service_data _CONCAT(coap_service_data_, _name) = { \
77 .sock_fd = -1, \
78 }; \
79 const STRUCT_SECTION_ITERABLE(coap_service, _name) = { \
80 .name = STRINGIFY(_name), \
81 .host = _host, \
82 .port = (uint16_t *)(_port), \
83 .flags = _flags, \
84 .res_begin = (_res_begin), \
85 .res_end = (_res_end), \
86 .data = &_CONCAT(coap_service_data_, _name), \
87 __z_coap_service_secure(_sec_tag_list, _sec_tag_list_size) \
88 }
89
128#define COAP_RESOURCE_DEFINE(_name, _service, ...) \
129 STRUCT_SECTION_ITERABLE_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, \
130 _name) = __VA_ARGS__
131
149#define COAP_SERVICE_DEFINE(_name, _host, _port, _flags) \
150 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
151 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
152 __z_coap_service_define(_name, _host, _port, _flags, \
153 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
154 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], \
155 NULL, 0)
156
178#define COAPS_SERVICE_DEFINE(_name, _host, _port, _flags, _sec_tag_list, _sec_tag_list_size) \
179 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_ENABLE_DTLS), \
180 "DTLS is required for CoAP secure (CONFIG_NET_SOCKETS_ENABLE_DTLS)"); \
181 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
182 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
183 __z_coap_service_define(_name, _host, _port, _flags, \
184 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
185 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0], \
186 _sec_tag_list, _sec_tag_list_size)
187
193#define COAP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(coap_service, _dst)
194
200#define COAP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
201
208#define COAP_SERVICE_HAS_RESOURCE(_service, _resource) \
209 ((_service)->res_begin <= _resource && _resource < (_service)->res_end)
210
216#define COAP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(coap_service, _it)
217
226#define COAP_RESOURCE_FOREACH(_service, _it) \
227 STRUCT_SECTION_FOREACH_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, _it)
228
237#define COAP_SERVICE_FOREACH_RESOURCE(_service, _it) \
238 for (struct coap_resource *_it = (_service)->res_begin; ({ \
239 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
240 _it < (_service)->res_end; \
241 }); _it++)
242
253int coap_service_start(const struct coap_service *service);
254
264int coap_service_stop(const struct coap_service *service);
265
276int coap_service_is_running(const struct coap_service *service);
277
290int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
291 const struct sockaddr *addr, socklen_t addr_len,
292 const struct coap_transmission_parameters *params);
293
306int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
307 const struct sockaddr *addr, socklen_t addr_len,
308 const struct coap_transmission_parameters *params);
309
323int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request,
324 const struct sockaddr *addr);
325
336 const struct sockaddr *addr);
337
349 const uint8_t *token, uint8_t token_len);
350
355#ifdef __cplusplus
356}
357#endif
358
359#endif /* ZEPHYR_INCLUDE_NET_COAP_SERVICE_H_ */
CoAP implementation for Zephyr.
int coap_service_is_running(const struct coap_service *service)
Query the provided service running state.
int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request, const struct sockaddr *addr)
Parse a CoAP observe request for the provided resource .
int coap_service_stop(const struct coap_service *service)
Stop the provided service .
int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt, const struct sockaddr *addr, socklen_t addr_len, const struct coap_transmission_parameters *params)
Send a CoAP message from the provided resource .
int coap_resource_remove_observer_by_addr(struct coap_resource *resource, const struct sockaddr *addr)
Lookup an observer by address and remove it from the resource .
int coap_service_start(const struct coap_service *service)
Start the provided service .
int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt, const struct sockaddr *addr, socklen_t addr_len, const struct coap_transmission_parameters *params)
Send a CoAP message from 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 .
size_t 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:78
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:298
Representation of a CoAP Packet.
Definition coap.h:312
Represents a request awaiting for an acknowledgment (ACK).
Definition coap.h:376
Description of CoAP resource.
Definition coap.h:280
CoAP transmission parameters.
Definition coap.h:357
Generic sockaddr struct.
Definition net_ip.h:408
TLS credentials management.