Zephyr API Documentation 4.0.0
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
13#ifndef ZEPHYR_INCLUDE_NET_COAP_SERVICE_H_
14#define ZEPHYR_INCLUDE_NET_COAP_SERVICE_H_
15
16#include <zephyr/net/coap.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
39#define COAP_SERVICE_AUTOSTART BIT(0)
40
45struct coap_service_data {
46 int sock_fd;
47 struct coap_observer observers[CONFIG_COAP_SERVICE_OBSERVERS];
48 struct coap_pending pending[CONFIG_COAP_SERVICE_PENDING_MESSAGES];
49};
50
51struct coap_service {
52 const char *name;
53 const char *host;
54 uint16_t *port;
56 struct coap_resource *res_begin;
57 struct coap_resource *res_end;
58 struct coap_service_data *data;
59};
60
61#define __z_coap_service_define(_name, _host, _port, _flags, _res_begin, _res_end) \
62 static struct coap_service_data _CONCAT(coap_service_data_, _name) = { \
63 .sock_fd = -1, \
64 }; \
65 const STRUCT_SECTION_ITERABLE(coap_service, _name) = { \
66 .name = STRINGIFY(_name), \
67 .host = _host, \
68 .port = (uint16_t *)(_port), \
69 .flags = _flags, \
70 .res_begin = (_res_begin), \
71 .res_end = (_res_end), \
72 .data = &_CONCAT(coap_service_data_, _name), \
73 }
74
113#define COAP_RESOURCE_DEFINE(_name, _service, ...) \
114 STRUCT_SECTION_ITERABLE_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, \
115 _name) = __VA_ARGS__
116
134#define COAP_SERVICE_DEFINE(_name, _host, _port, _flags) \
135 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[]; \
136 extern struct coap_resource _CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[]; \
137 __z_coap_service_define(_name, _host, _port, _flags, \
138 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_start)[0], \
139 &_CONCAT(_CONCAT(_coap_resource_, _name), _list_end)[0])
140
146#define COAP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(coap_service, _dst)
147
153#define COAP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
154
161#define COAP_SERVICE_HAS_RESOURCE(_service, _resource) \
162 ((_service)->res_begin <= _resource && _resource < (_service)->res_end)
163
169#define COAP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(coap_service, _it)
170
179#define COAP_RESOURCE_FOREACH(_service, _it) \
180 STRUCT_SECTION_FOREACH_ALTERNATE(_CONCAT(coap_resource_, _service), coap_resource, _it)
181
190#define COAP_SERVICE_FOREACH_RESOURCE(_service, _it) \
191 for (struct coap_resource *_it = (_service)->res_begin; ({ \
192 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
193 _it < (_service)->res_end; \
194 }); _it++)
195
206int coap_service_start(const struct coap_service *service);
207
217int coap_service_stop(const struct coap_service *service);
218
229int coap_service_is_running(const struct coap_service *service);
230
243int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
244 const struct sockaddr *addr, socklen_t addr_len,
245 const struct coap_transmission_parameters *params);
246
259int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
260 const struct sockaddr *addr, socklen_t addr_len,
261 const struct coap_transmission_parameters *params);
262
276int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request,
277 const struct sockaddr *addr);
278
289 const struct sockaddr *addr);
290
302 const uint8_t *token, uint8_t token_len);
303
308#ifdef __cplusplus
309}
310#endif
311
312#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:171
flags
Definition parser.h:96
__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:388