Zephyr API Documentation  3.6.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
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
37#define COAP_SERVICE_AUTOSTART BIT(0)
38
43struct coap_service_data {
44 int sock_fd;
45 struct coap_observer observers[CONFIG_COAP_SERVICE_OBSERVERS];
46 struct coap_pending pending[CONFIG_COAP_SERVICE_PENDING_MESSAGES];
47};
48
49struct coap_service {
50 const char *name;
51 const char *host;
52 uint16_t *port;
54 struct coap_resource *res_begin;
55 struct coap_resource *res_end;
56 struct coap_service_data *data;
57};
58
59#define __z_coap_service_define(_name, _host, _port, _flags, _res_begin, _res_end) \
60 static struct coap_service_data coap_service_data_##_name = { \
61 .sock_fd = -1, \
62 }; \
63 const STRUCT_SECTION_ITERABLE(coap_service, _name) = { \
64 .name = STRINGIFY(_name), \
65 .host = _host, \
66 .port = (uint16_t *)(_port), \
67 .flags = _flags, \
68 .res_begin = (_res_begin), \
69 .res_end = (_res_end), \
70 .data = &coap_service_data_##_name, \
71 }
72
111#define COAP_RESOURCE_DEFINE(_name, _service, ...) \
112 STRUCT_SECTION_ITERABLE_ALTERNATE(coap_resource_##_service, coap_resource, _name) \
113 = __VA_ARGS__
114
132#define COAP_SERVICE_DEFINE(_name, _host, _port, _flags) \
133 extern struct coap_resource _CONCAT(_coap_resource_##_name, _list_start)[]; \
134 extern struct coap_resource _CONCAT(_coap_resource_##_name, _list_end)[]; \
135 __z_coap_service_define(_name, _host, _port, _flags, \
136 &_CONCAT(_coap_resource_##_name, _list_start)[0], \
137 &_CONCAT(_coap_resource_##_name, _list_end)[0])
138
144#define COAP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(coap_service, _dst)
145
151#define COAP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
152
159#define COAP_SERVICE_HAS_RESOURCE(_service, _resource) \
160 ((_service)->res_begin <= _resource && _resource < (_service)->res_end)
161
167#define COAP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(coap_service, _it)
168
177#define COAP_RESOURCE_FOREACH(_service, _it) \
178 STRUCT_SECTION_FOREACH_ALTERNATE(coap_resource_##_service, coap_resource, _it)
179
188#define COAP_SERVICE_FOREACH_RESOURCE(_service, _it) \
189 for (struct coap_resource *_it = (_service)->res_begin; ({ \
190 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
191 _it < (_service)->res_end; \
192 }); _it++)
193
204int coap_service_start(const struct coap_service *service);
205
215int coap_service_stop(const struct coap_service *service);
216
227int coap_service_is_running(const struct coap_service *service);
228
241int coap_service_send(const struct coap_service *service, const struct coap_packet *cpkt,
242 const struct sockaddr *addr, socklen_t addr_len,
243 const struct coap_transmission_parameters *params);
244
257int coap_resource_send(const struct coap_resource *resource, const struct coap_packet *cpkt,
258 const struct sockaddr *addr, socklen_t addr_len,
259 const struct coap_transmission_parameters *params);
260
274int coap_resource_parse_observe(struct coap_resource *resource, const struct coap_packet *request,
275 const struct sockaddr *addr);
276
287 const struct sockaddr *addr);
288
300 const uint8_t *token, uint8_t token_len);
301
306#ifdef __cplusplus
307}
308#endif
309
310#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:168
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:281
Representation of a CoAP Packet.
Definition: coap.h:295
Represents a request awaiting for an acknowledgment (ACK).
Definition: coap.h:352
Description of CoAP resource.
Definition: coap.h:263
CoAP transmission parameters.
Definition: coap.h:340
Generic sockaddr struct.
Definition: net_ip.h:385