1c87c5fbaSopenharmony_ci/*
2c87c5fbaSopenharmony_ci * coap_resource.h -- generic resource handling
3c87c5fbaSopenharmony_ci *
4c87c5fbaSopenharmony_ci * Copyright (C) 2010,2011,2014-2023 Olaf Bergmann <bergmann@tzi.org>
5c87c5fbaSopenharmony_ci *
6c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
7c87c5fbaSopenharmony_ci *
8c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see README for terms
9c87c5fbaSopenharmony_ci * of use.
10c87c5fbaSopenharmony_ci */
11c87c5fbaSopenharmony_ci
12c87c5fbaSopenharmony_ci/**
13c87c5fbaSopenharmony_ci * @file coap_resource.h
14c87c5fbaSopenharmony_ci * @brief Generic resource handling
15c87c5fbaSopenharmony_ci */
16c87c5fbaSopenharmony_ci
17c87c5fbaSopenharmony_ci#ifndef COAP_RESOURCE_H_
18c87c5fbaSopenharmony_ci#define COAP_RESOURCE_H_
19c87c5fbaSopenharmony_ci
20c87c5fbaSopenharmony_ci#ifndef COAP_RESOURCE_CHECK_TIME
21c87c5fbaSopenharmony_ci/** The interval in seconds to check if resources have changed. */
22c87c5fbaSopenharmony_ci#define COAP_RESOURCE_CHECK_TIME 2
23c87c5fbaSopenharmony_ci#endif /* COAP_RESOURCE_CHECK_TIME */
24c87c5fbaSopenharmony_ci
25c87c5fbaSopenharmony_ci/**
26c87c5fbaSopenharmony_ci * @ingroup application_api
27c87c5fbaSopenharmony_ci * @defgroup coap_resource Resource Configuraton
28c87c5fbaSopenharmony_ci * API for setting up resources
29c87c5fbaSopenharmony_ci * @{
30c87c5fbaSopenharmony_ci */
31c87c5fbaSopenharmony_ci
32c87c5fbaSopenharmony_ci/**
33c87c5fbaSopenharmony_ci * Definition of message handler function
34c87c5fbaSopenharmony_ci */
35c87c5fbaSopenharmony_citypedef void (*coap_method_handler_t)
36c87c5fbaSopenharmony_ci(coap_resource_t *,
37c87c5fbaSopenharmony_ci coap_session_t *,
38c87c5fbaSopenharmony_ci const coap_pdu_t * /* request */,
39c87c5fbaSopenharmony_ci const coap_string_t * /* query string */,
40c87c5fbaSopenharmony_ci coap_pdu_t * /* response */);
41c87c5fbaSopenharmony_ci
42c87c5fbaSopenharmony_ci#define COAP_ATTR_FLAGS_RELEASE_NAME  0x1
43c87c5fbaSopenharmony_ci#define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
44c87c5fbaSopenharmony_ci
45c87c5fbaSopenharmony_ci/** The URI passed to coap_resource_init() is free'd by coap_delete_resource(). */
46c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
47c87c5fbaSopenharmony_ci
48c87c5fbaSopenharmony_ci/**
49c87c5fbaSopenharmony_ci * Observe Notifications will be sent non-confirmable by default. RFC 7641
50c87c5fbaSopenharmony_ci * Section 4.5
51c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7641#section-4.5
52c87c5fbaSopenharmony_ci * Libcoap will always send every fifth packet as confirmable.
53c87c5fbaSopenharmony_ci */
54c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_NOTIFY_NON  0x0
55c87c5fbaSopenharmony_ci
56c87c5fbaSopenharmony_ci/**
57c87c5fbaSopenharmony_ci * Observe Notifications will be sent confirmable. RFC 7641 Section 4.5
58c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7641#section-4.5
59c87c5fbaSopenharmony_ci */
60c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_NOTIFY_CON  0x2
61c87c5fbaSopenharmony_ci
62c87c5fbaSopenharmony_ci/**
63c87c5fbaSopenharmony_ci * Observe Notifications will always be sent non-confirmable. This is in
64c87c5fbaSopenharmony_ci * violation of RFC 7641 Section 4.5
65c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7641#section-4.5
66c87c5fbaSopenharmony_ci * but required by the DOTS signal channel protocol which needs to operate in
67c87c5fbaSopenharmony_ci * lossy DDoS attack environments.
68c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc8782#section-4.4.2.1
69c87c5fbaSopenharmony_ci */
70c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS  0x4
71c87c5fbaSopenharmony_ci
72c87c5fbaSopenharmony_ci/**
73c87c5fbaSopenharmony_ci * This resource has support for multicast requests.
74c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7252#section-11.3
75c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7390#section-2.8
76c87c5fbaSopenharmony_ci * https://datatracker.ietf.org/doc/html/draft-ietf-core-groupcomm-bis-06.txt#section-3.6
77c87c5fbaSopenharmony_ci * Note: ".well-known/core" always supports multicast.
78c87c5fbaSopenharmony_ci * Note: Only tested if coap_mcast_per_resource() has been called.
79c87c5fbaSopenharmony_ci */
80c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT 0x8
81c87c5fbaSopenharmony_ci
82c87c5fbaSopenharmony_ci/**
83c87c5fbaSopenharmony_ci * Disable libcoap library from adding in delays to multicast requests before
84c87c5fbaSopenharmony_ci * releasing the response back to the client.  It is then the responsibility of
85c87c5fbaSopenharmony_ci * the app to delay the responses for multicast requests.
86c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7252#section-8.2
87c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7390#section-2.8
88c87c5fbaSopenharmony_ci * https://datatracker.ietf.org/doc/html/draft-ietf-core-groupcomm-bis-06.txt#section-3.6
89c87c5fbaSopenharmony_ci * Note: Only tested if coap_mcast_per_resource() has been called.
90c87c5fbaSopenharmony_ci */
91c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_DELAYS 0x10
92c87c5fbaSopenharmony_ci
93c87c5fbaSopenharmony_ci/**
94c87c5fbaSopenharmony_ci * Enable libcoap library suppression of 205 multicast responses that are empty
95c87c5fbaSopenharmony_ci * (overridden by RFC7969 No-Response option) for multicast requests.
96c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7390#section-2.7
97c87c5fbaSopenharmony_ci * https://datatracker.ietf.org/doc/html/draft-ietf-core-groupcomm-bis-06.txt#section-3.1.2
98c87c5fbaSopenharmony_ci * Note: Only tested if coap_mcast_per_resource() has been called.
99c87c5fbaSopenharmony_ci */
100c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_05 0x20
101c87c5fbaSopenharmony_ci
102c87c5fbaSopenharmony_ci/**
103c87c5fbaSopenharmony_ci * Enable libcoap library suppressing 2.xx multicast responses (overridden by
104c87c5fbaSopenharmony_ci * RFC7969 No-Response option) for multicast requests.
105c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7390#section-2.7
106c87c5fbaSopenharmony_ci * https://datatracker.ietf.org/doc/html/draft-ietf-core-groupcomm-bis-06.txt#section-3.1.2
107c87c5fbaSopenharmony_ci * Note: Only tested if coap_mcast_per_resource() has been called.
108c87c5fbaSopenharmony_ci */
109c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_XX 0x40
110c87c5fbaSopenharmony_ci
111c87c5fbaSopenharmony_ci/**
112c87c5fbaSopenharmony_ci * Disable libcoap library suppressing 4.xx multicast responses (overridden by
113c87c5fbaSopenharmony_ci * RFC7969 No-Response option) for multicast requests.
114c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7390#section-2.7
115c87c5fbaSopenharmony_ci * https://datatracker.ietf.org/doc/html/draft-ietf-core-groupcomm-bis-06.txt#section-3.1.2
116c87c5fbaSopenharmony_ci * Note: Only tested if coap_mcast_per_resource() has been called.
117c87c5fbaSopenharmony_ci */
118c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_4_XX 0x80
119c87c5fbaSopenharmony_ci
120c87c5fbaSopenharmony_ci/**
121c87c5fbaSopenharmony_ci * Disable libcoap library suppressing 5.xx multicast responses (overridden by
122c87c5fbaSopenharmony_ci * RFC7969 No-Response option) for multicast requests.
123c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7390#section-2.7
124c87c5fbaSopenharmony_ci * https://datatracker.ietf.org/doc/html/draft-ietf-core-groupcomm-bis-06.txt#section-3.1.2
125c87c5fbaSopenharmony_ci * Note: Only tested if coap_mcast_per_resource() has been called.
126c87c5fbaSopenharmony_ci */
127c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_5_XX 0x100
128c87c5fbaSopenharmony_ci
129c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_MCAST_LIST \
130c87c5fbaSopenharmony_ci  (COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT | \
131c87c5fbaSopenharmony_ci   COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_DELAYS | \
132c87c5fbaSopenharmony_ci   COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_05 | \
133c87c5fbaSopenharmony_ci   COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_XX | \
134c87c5fbaSopenharmony_ci   COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_4_XX | \
135c87c5fbaSopenharmony_ci   COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_5_XX)
136c87c5fbaSopenharmony_ci
137c87c5fbaSopenharmony_ci/**
138c87c5fbaSopenharmony_ci * Force all large traffic to this resource to be presented as a single body
139c87c5fbaSopenharmony_ci * to the request handler.
140c87c5fbaSopenharmony_ci */
141c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_FORCE_SINGLE_BODY 0x200
142c87c5fbaSopenharmony_ci
143c87c5fbaSopenharmony_ci/**
144c87c5fbaSopenharmony_ci * Define this resource as an OSCORE enabled access only.
145c87c5fbaSopenharmony_ci */
146c87c5fbaSopenharmony_ci#define COAP_RESOURCE_FLAGS_OSCORE_ONLY 0x400
147c87c5fbaSopenharmony_ci
148c87c5fbaSopenharmony_ci/**
149c87c5fbaSopenharmony_ci * Creates a new resource object and initializes the link field to the string
150c87c5fbaSopenharmony_ci * @p uri_path. This function returns the new coap_resource_t object.
151c87c5fbaSopenharmony_ci *
152c87c5fbaSopenharmony_ci * If the string is going to be freed off by coap_delete_resource() when
153c87c5fbaSopenharmony_ci * COAP_RESOURCE_FLAGS_RELEASE_URI is set in @p flags, then either the 's'
154c87c5fbaSopenharmony_ci * variable of coap_str_const_t has to point to constant text, or point to data
155c87c5fbaSopenharmony_ci * within the allocated coap_str_const_t parameter.
156c87c5fbaSopenharmony_ci *
157c87c5fbaSopenharmony_ci * @param uri_path The string URI path of the new resource. The leading '/' is
158c87c5fbaSopenharmony_ci *                 not normally required - e.g. just "full/path/for/resource".
159c87c5fbaSopenharmony_ci * @param flags    Flags for memory management, observe handling and multicast
160c87c5fbaSopenharmony_ci *                 support, comprising of zero or more COAP_RESOURCE_FLAGS_*
161c87c5fbaSopenharmony_ci *                 ored together.
162c87c5fbaSopenharmony_ci *
163c87c5fbaSopenharmony_ci *                 If flags does not have COAP_RESOURCE_FLAGS_NOTIFY_CON
164c87c5fbaSopenharmony_ci *                 set, then COAP_RESOURCE_FLAGS_NOTIFY_NON is assumed and
165c87c5fbaSopenharmony_ci *                 observe notifications will be sent as non-confirmable,
166c87c5fbaSopenharmony_ci *                 except that every 5th notification will be confirmable.
167c87c5fbaSopenharmony_ci *
168c87c5fbaSopenharmony_ci *                 If COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS is set,
169c87c5fbaSopenharmony_ci *                 observe notifications will always be sent
170c87c5fbaSopenharmony_ci *                 non-confirmable.
171c87c5fbaSopenharmony_ci *
172c87c5fbaSopenharmony_ci * @return         A pointer to the new object or @c NULL on error.
173c87c5fbaSopenharmony_ci */
174c87c5fbaSopenharmony_cicoap_resource_t *coap_resource_init(coap_str_const_t *uri_path,
175c87c5fbaSopenharmony_ci                                    int flags);
176c87c5fbaSopenharmony_ci
177c87c5fbaSopenharmony_ci/**
178c87c5fbaSopenharmony_ci * Creates a new resource object for the unknown resource handler with support
179c87c5fbaSopenharmony_ci * for PUT.
180c87c5fbaSopenharmony_ci *
181c87c5fbaSopenharmony_ci * In the same way that additional handlers can be added to the resource
182c87c5fbaSopenharmony_ci * created by coap_resource_init() by using coap_register_request_handler(),
183c87c5fbaSopenharmony_ci * POST, GET, DELETE etc. handlers can be added to this resource. It is the
184c87c5fbaSopenharmony_ci * responsibility of the application to manage the unknown resources by either
185c87c5fbaSopenharmony_ci * creating new resources with coap_resource_init() (which should have a
186c87c5fbaSopenharmony_ci * DELETE handler specified for the resource removal) or by maintaining an
187c87c5fbaSopenharmony_ci * active resource list.
188c87c5fbaSopenharmony_ci *
189c87c5fbaSopenharmony_ci * Note: There can only be one unknown resource handler per context - attaching
190c87c5fbaSopenharmony_ci *       a new one overrides the previous definition.
191c87c5fbaSopenharmony_ci *
192c87c5fbaSopenharmony_ci * Note: It is not possible to observe the unknown resource with a GET request
193c87c5fbaSopenharmony_ci *       - a separate resource needs to be created by the PUT (or POST)
194c87c5fbaSopenharmony_ci *       handler, and make that resource observable.
195c87c5fbaSopenharmony_ci *
196c87c5fbaSopenharmony_ci * This function returns the new coap_resource_t object.
197c87c5fbaSopenharmony_ci *
198c87c5fbaSopenharmony_ci * @param put_handler The PUT handler to register with @p resource for
199c87c5fbaSopenharmony_ci *                    unknown Uri-Path.
200c87c5fbaSopenharmony_ci *
201c87c5fbaSopenharmony_ci * @return       A pointer to the new object or @c NULL on error.
202c87c5fbaSopenharmony_ci */
203c87c5fbaSopenharmony_cicoap_resource_t *coap_resource_unknown_init(coap_method_handler_t put_handler);
204c87c5fbaSopenharmony_ci
205c87c5fbaSopenharmony_ci/**
206c87c5fbaSopenharmony_ci * Creates a new resource object for the unknown resource handler with support
207c87c5fbaSopenharmony_ci * for PUT and configurable control over multicast requests packets.
208c87c5fbaSopenharmony_ci *
209c87c5fbaSopenharmony_ci * In the same way that additional handlers can be added to the resource
210c87c5fbaSopenharmony_ci * created by coap_resource_init() by using coap_register_request_handler(),
211c87c5fbaSopenharmony_ci * POST, GET, DELETE etc. handlers can be added to this resource. It is the
212c87c5fbaSopenharmony_ci * responsibility of the application to manage the unknown resources by either
213c87c5fbaSopenharmony_ci * creating new resources with coap_resource_init() (which should have a
214c87c5fbaSopenharmony_ci * DELETE handler specified for the resource removal) or by maintaining an
215c87c5fbaSopenharmony_ci * active resource list.
216c87c5fbaSopenharmony_ci *
217c87c5fbaSopenharmony_ci * Note: There can only be one unknown resource handler per context - attaching
218c87c5fbaSopenharmony_ci *       a new one overrides the previous definition.
219c87c5fbaSopenharmony_ci *
220c87c5fbaSopenharmony_ci * Note: It is not possible to observe the unknown resource with a GET request
221c87c5fbaSopenharmony_ci *       - a separate resource needs to be created by the PUT (or POST)
222c87c5fbaSopenharmony_ci *       handler, and make that resource observable.
223c87c5fbaSopenharmony_ci *
224c87c5fbaSopenharmony_ci * This function returns the new coap_resource_t object.
225c87c5fbaSopenharmony_ci *
226c87c5fbaSopenharmony_ci * @param put_handler The PUT handler to register with @p resource for
227c87c5fbaSopenharmony_ci *                    unknown Uri-Path.
228c87c5fbaSopenharmony_ci * @param flags Zero or more COAP_RESOURCE_FLAGS_*MCAST* ored together to
229c87c5fbaSopenharmony_ci *              indicate what to do with multicast packets.
230c87c5fbaSopenharmony_ci *
231c87c5fbaSopenharmony_ci * @return       A pointer to the new object or @c NULL on error.
232c87c5fbaSopenharmony_ci */
233c87c5fbaSopenharmony_cicoap_resource_t *coap_resource_unknown_init2(coap_method_handler_t put_handler,
234c87c5fbaSopenharmony_ci                                             int flags);
235c87c5fbaSopenharmony_ci
236c87c5fbaSopenharmony_ci/**
237c87c5fbaSopenharmony_ci * Creates a new resource object for handling proxy URIs.
238c87c5fbaSopenharmony_ci * This function returns the new coap_resource_t object.
239c87c5fbaSopenharmony_ci *
240c87c5fbaSopenharmony_ci * Note: There can only be one proxy resource handler per context - attaching
241c87c5fbaSopenharmony_ci *       a new one overrides the previous definition.
242c87c5fbaSopenharmony_ci *
243c87c5fbaSopenharmony_ci * @param handler The PUT/POST/GET etc. handler that handles all request types.
244c87c5fbaSopenharmony_ci * @param host_name_count The number of provided host_name_list entries. A
245c87c5fbaSopenharmony_ci *                        minimum of 1 must be provided.
246c87c5fbaSopenharmony_ci * @param host_name_list Array of depth host_name_count names that this proxy
247c87c5fbaSopenharmony_ci *                       is known by.
248c87c5fbaSopenharmony_ci *
249c87c5fbaSopenharmony_ci * @return         A pointer to the new object or @c NULL on error.
250c87c5fbaSopenharmony_ci */
251c87c5fbaSopenharmony_cicoap_resource_t *coap_resource_proxy_uri_init(coap_method_handler_t handler,
252c87c5fbaSopenharmony_ci                                              size_t host_name_count, const char *host_name_list[]);
253c87c5fbaSopenharmony_ci
254c87c5fbaSopenharmony_ci/**
255c87c5fbaSopenharmony_ci * Creates a new resource object for handling proxy URIs with configurable
256c87c5fbaSopenharmony_ci * control over multicast request packets.
257c87c5fbaSopenharmony_ci * This function returns the new coap_resource_t object.
258c87c5fbaSopenharmony_ci *
259c87c5fbaSopenharmony_ci * Note: There can only be one proxy resource handler per context - attaching
260c87c5fbaSopenharmony_ci *       a new one overrides the previous definition.
261c87c5fbaSopenharmony_ci *
262c87c5fbaSopenharmony_ci * @param handler The PUT/POST/GET etc. handler that handles all request types.
263c87c5fbaSopenharmony_ci * @param host_name_count The number of provided host_name_list entries. A
264c87c5fbaSopenharmony_ci *                        minimum of 1 must be provided.
265c87c5fbaSopenharmony_ci * @param host_name_list Array of depth host_name_count names that this proxy
266c87c5fbaSopenharmony_ci *                       is known by.
267c87c5fbaSopenharmony_ci * @param flags Zero or more COAP_RESOURCE_FLAGS_*MCAST* ored together to
268c87c5fbaSopenharmony_ci *              indicate what to do with multicast packets.
269c87c5fbaSopenharmony_ci *
270c87c5fbaSopenharmony_ci * @return         A pointer to the new object or @c NULL on error.
271c87c5fbaSopenharmony_ci */
272c87c5fbaSopenharmony_cicoap_resource_t *coap_resource_proxy_uri_init2(coap_method_handler_t handler,
273c87c5fbaSopenharmony_ci                                               size_t host_name_count,
274c87c5fbaSopenharmony_ci                                               const char *host_name_list[],
275c87c5fbaSopenharmony_ci                                               int flags);
276c87c5fbaSopenharmony_ci
277c87c5fbaSopenharmony_ci/**
278c87c5fbaSopenharmony_ci * Returns the resource identified by the unique string @p uri_path. If no
279c87c5fbaSopenharmony_ci * resource was found, this function returns @c NULL.
280c87c5fbaSopenharmony_ci *
281c87c5fbaSopenharmony_ci * @param context  The context to look for this resource.
282c87c5fbaSopenharmony_ci * @param uri_path  The unique string uri of the resource.
283c87c5fbaSopenharmony_ci *
284c87c5fbaSopenharmony_ci * @return         A pointer to the resource or @c NULL if not found.
285c87c5fbaSopenharmony_ci */
286c87c5fbaSopenharmony_cicoap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context,
287c87c5fbaSopenharmony_ci                                                 coap_str_const_t *uri_path);
288c87c5fbaSopenharmony_ci
289c87c5fbaSopenharmony_ci/**
290c87c5fbaSopenharmony_ci * Get the uri_path from a @p resource.
291c87c5fbaSopenharmony_ci *
292c87c5fbaSopenharmony_ci * @param resource The CoAP resource to check.
293c87c5fbaSopenharmony_ci *
294c87c5fbaSopenharmony_ci * @return         The uri_path if it exists or @c NULL otherwise.
295c87c5fbaSopenharmony_ci */
296c87c5fbaSopenharmony_cicoap_str_const_t *coap_resource_get_uri_path(coap_resource_t *resource);
297c87c5fbaSopenharmony_ci
298c87c5fbaSopenharmony_ci/**
299c87c5fbaSopenharmony_ci * Sets the notification message type of resource @p resource to given
300c87c5fbaSopenharmony_ci * @p mode
301c87c5fbaSopenharmony_ci
302c87c5fbaSopenharmony_ci * @param resource The resource to update.
303c87c5fbaSopenharmony_ci * @param mode     Must be one of @c COAP_RESOURCE_FLAGS_NOTIFY_NON
304c87c5fbaSopenharmony_ci *                 or @c COAP_RESOURCE_FLAGS_NOTIFY_CON.
305c87c5fbaSopenharmony_ci */
306c87c5fbaSopenharmony_civoid coap_resource_set_mode(coap_resource_t *resource, int mode);
307c87c5fbaSopenharmony_ci
308c87c5fbaSopenharmony_ci/**
309c87c5fbaSopenharmony_ci * Sets the user_data. The user_data is exclusively used by the library-user
310c87c5fbaSopenharmony_ci * and can be used as user defined context in the handler functions.
311c87c5fbaSopenharmony_ci *
312c87c5fbaSopenharmony_ci * @param resource Resource to attach the data to
313c87c5fbaSopenharmony_ci * @param data     Data to attach to the user_data field. This pointer is
314c87c5fbaSopenharmony_ci *                 only used for storage, the data remains under user control
315c87c5fbaSopenharmony_ci */
316c87c5fbaSopenharmony_civoid coap_resource_set_userdata(coap_resource_t *resource, void *data);
317c87c5fbaSopenharmony_ci
318c87c5fbaSopenharmony_ci/**
319c87c5fbaSopenharmony_ci * Gets the user_data. The user_data is exclusively used by the library-user
320c87c5fbaSopenharmony_ci * and can be used as context in the handler functions.
321c87c5fbaSopenharmony_ci *
322c87c5fbaSopenharmony_ci * @param resource Resource to retrieve the user_data from
323c87c5fbaSopenharmony_ci *
324c87c5fbaSopenharmony_ci * @return        The user_data pointer
325c87c5fbaSopenharmony_ci */
326c87c5fbaSopenharmony_civoid *coap_resource_get_userdata(coap_resource_t *resource);
327c87c5fbaSopenharmony_ci
328c87c5fbaSopenharmony_ci/**
329c87c5fbaSopenharmony_ci * Definition of release resource user_data callback function
330c87c5fbaSopenharmony_ci */
331c87c5fbaSopenharmony_citypedef void (*coap_resource_release_userdata_handler_t)(void *user_data);
332c87c5fbaSopenharmony_ci
333c87c5fbaSopenharmony_ci/**
334c87c5fbaSopenharmony_ci * Defines the context wide callback to use to when the resource is deleted
335c87c5fbaSopenharmony_ci * to release the data held in the resource's user_data.
336c87c5fbaSopenharmony_ci *
337c87c5fbaSopenharmony_ci * @param context  The context to associate the release callback with
338c87c5fbaSopenharmony_ci * @param callback The callback to invoke when the resource is deleted or NULL
339c87c5fbaSopenharmony_ci *
340c87c5fbaSopenharmony_ci */
341c87c5fbaSopenharmony_civoid coap_resource_release_userdata_handler(coap_context_t *context,
342c87c5fbaSopenharmony_ci                                            coap_resource_release_userdata_handler_t callback);
343c87c5fbaSopenharmony_ci
344c87c5fbaSopenharmony_ci/**
345c87c5fbaSopenharmony_ci * Registers the given @p resource for @p context. The resource must have been
346c87c5fbaSopenharmony_ci * created by coap_resource_init() or coap_resource_unknown_init(), the
347c87c5fbaSopenharmony_ci * storage allocated for the resource will be released by coap_delete_resource().
348c87c5fbaSopenharmony_ci *
349c87c5fbaSopenharmony_ci * @param context  The context to use.
350c87c5fbaSopenharmony_ci * @param resource The resource to store.
351c87c5fbaSopenharmony_ci */
352c87c5fbaSopenharmony_civoid coap_add_resource(coap_context_t *context, coap_resource_t *resource);
353c87c5fbaSopenharmony_ci
354c87c5fbaSopenharmony_ci/**
355c87c5fbaSopenharmony_ci * Deletes a resource identified by @p resource. The storage allocated for that
356c87c5fbaSopenharmony_ci * resource is freed, and removed from the context.
357c87c5fbaSopenharmony_ci *
358c87c5fbaSopenharmony_ci * @param context  This parameter is ignored, but kept for backward
359c87c5fbaSopenharmony_ci *                 compatibility.
360c87c5fbaSopenharmony_ci * @param resource The resource to delete.
361c87c5fbaSopenharmony_ci *
362c87c5fbaSopenharmony_ci * @return         @c 1 if the resource was found (and destroyed),
363c87c5fbaSopenharmony_ci *                 @c 0 otherwise.
364c87c5fbaSopenharmony_ci */
365c87c5fbaSopenharmony_ciint coap_delete_resource(coap_context_t *context, coap_resource_t *resource);
366c87c5fbaSopenharmony_ci
367c87c5fbaSopenharmony_ci/**
368c87c5fbaSopenharmony_ci * Registers the specified @p handler as message handler for the request type
369c87c5fbaSopenharmony_ci * @p method
370c87c5fbaSopenharmony_ci *
371c87c5fbaSopenharmony_ci * @deprecated use coap_register_request_handler() instead.
372c87c5fbaSopenharmony_ci *
373c87c5fbaSopenharmony_ci * @param resource The resource for which the handler shall be registered.
374c87c5fbaSopenharmony_ci * @param method   The CoAP request method to handle.
375c87c5fbaSopenharmony_ci * @param handler  The handler to register with @p resource.
376c87c5fbaSopenharmony_ci */
377c87c5fbaSopenharmony_civoid coap_register_handler(coap_resource_t *resource,
378c87c5fbaSopenharmony_ci                           coap_request_t method,
379c87c5fbaSopenharmony_ci                           coap_method_handler_t handler);
380c87c5fbaSopenharmony_ci
381c87c5fbaSopenharmony_ci/**
382c87c5fbaSopenharmony_ci * Registers the specified @p handler as message handler for the request type
383c87c5fbaSopenharmony_ci * @p method
384c87c5fbaSopenharmony_ci *
385c87c5fbaSopenharmony_ci * @param resource The resource for which the handler shall be registered.
386c87c5fbaSopenharmony_ci * @param method   The CoAP request method to handle.
387c87c5fbaSopenharmony_ci * @param handler  The handler to register with @p resource.
388c87c5fbaSopenharmony_ci */
389c87c5fbaSopenharmony_civoid coap_register_request_handler(coap_resource_t *resource,
390c87c5fbaSopenharmony_ci                                   coap_request_t method,
391c87c5fbaSopenharmony_ci                                   coap_method_handler_t handler);
392c87c5fbaSopenharmony_ci
393c87c5fbaSopenharmony_ci/**
394c87c5fbaSopenharmony_ci * Registers a new attribute with the given @p resource. As the
395c87c5fbaSopenharmony_ci * attribute's coap_str_const_ fields will point to @p name and @p value the
396c87c5fbaSopenharmony_ci * caller must ensure that these pointers are valid during the
397c87c5fbaSopenharmony_ci * attribute's lifetime.
398c87c5fbaSopenharmony_ci
399c87c5fbaSopenharmony_ci * If the @p name and/or @p value string is going to be freed off at attribute
400c87c5fbaSopenharmony_ci * removal time by the setting of COAP_ATTR_FLAGS_RELEASE_NAME or
401c87c5fbaSopenharmony_ci * COAP_ATTR_FLAGS_RELEASE_VALUE in @p flags, then either the 's'
402c87c5fbaSopenharmony_ci * variable of coap_str_const_t has to point to constant text, or point to data
403c87c5fbaSopenharmony_ci * within the allocated coap_str_const_t parameter.
404c87c5fbaSopenharmony_ci *
405c87c5fbaSopenharmony_ci * @param resource The resource to register the attribute with.
406c87c5fbaSopenharmony_ci * @param name     The attribute's name as a string.
407c87c5fbaSopenharmony_ci * @param value    The attribute's value as a string or @c NULL if none.
408c87c5fbaSopenharmony_ci * @param flags    Flags for memory management (in particular release of
409c87c5fbaSopenharmony_ci *                 memory). Possible values:@n
410c87c5fbaSopenharmony_ci *
411c87c5fbaSopenharmony_ci *                 COAP_ATTR_FLAGS_RELEASE_NAME
412c87c5fbaSopenharmony_ci *                  If this flag is set, the name passed to
413c87c5fbaSopenharmony_ci *                  coap_add_attr_release() is free'd
414c87c5fbaSopenharmony_ci *                  when the attribute is deleted@n
415c87c5fbaSopenharmony_ci *
416c87c5fbaSopenharmony_ci *                 COAP_ATTR_FLAGS_RELEASE_VALUE
417c87c5fbaSopenharmony_ci *                  If this flag is set, the value passed to
418c87c5fbaSopenharmony_ci *                  coap_add_attr_release() is free'd
419c87c5fbaSopenharmony_ci *                  when the attribute is deleted@n
420c87c5fbaSopenharmony_ci *
421c87c5fbaSopenharmony_ci * @return         A pointer to the new attribute or @c NULL on error.
422c87c5fbaSopenharmony_ci */
423c87c5fbaSopenharmony_cicoap_attr_t *coap_add_attr(coap_resource_t *resource,
424c87c5fbaSopenharmony_ci                           coap_str_const_t *name,
425c87c5fbaSopenharmony_ci                           coap_str_const_t *value,
426c87c5fbaSopenharmony_ci                           int flags);
427c87c5fbaSopenharmony_ci
428c87c5fbaSopenharmony_ci/**
429c87c5fbaSopenharmony_ci * Returns @p resource's coap_attr_t object with given @p name if found, @c NULL
430c87c5fbaSopenharmony_ci * otherwise.
431c87c5fbaSopenharmony_ci *
432c87c5fbaSopenharmony_ci * @param resource The resource to search for attribute @p name.
433c87c5fbaSopenharmony_ci * @param name     Name of the requested attribute as a string.
434c87c5fbaSopenharmony_ci * @return         The first attribute with specified @p name or @c NULL if none
435c87c5fbaSopenharmony_ci *                 was found.
436c87c5fbaSopenharmony_ci */
437c87c5fbaSopenharmony_cicoap_attr_t *coap_find_attr(coap_resource_t *resource,
438c87c5fbaSopenharmony_ci                            coap_str_const_t *name);
439c87c5fbaSopenharmony_ci
440c87c5fbaSopenharmony_ci/**
441c87c5fbaSopenharmony_ci * Returns @p attribute's value.
442c87c5fbaSopenharmony_ci *
443c87c5fbaSopenharmony_ci * @param attribute Pointer to attribute.
444c87c5fbaSopenharmony_ci *
445c87c5fbaSopenharmony_ci * @return Attribute's value or @c NULL.
446c87c5fbaSopenharmony_ci */
447c87c5fbaSopenharmony_cicoap_str_const_t *coap_attr_get_value(coap_attr_t *attribute);
448c87c5fbaSopenharmony_ci
449c87c5fbaSopenharmony_ci/**
450c87c5fbaSopenharmony_ci * Status word to encode the result of conditional print or copy operations such
451c87c5fbaSopenharmony_ci * as coap_print_link(). The lower 28 bits of coap_print_status_t are used to
452c87c5fbaSopenharmony_ci * encode the number of characters that has actually been printed, bits 28 to 31
453c87c5fbaSopenharmony_ci * encode the status.  When COAP_PRINT_STATUS_ERROR is set, an error occurred
454c87c5fbaSopenharmony_ci * during output. In this case, the other bits are undefined.
455c87c5fbaSopenharmony_ci * COAP_PRINT_STATUS_TRUNC indicates that the output is truncated, i.e. the
456c87c5fbaSopenharmony_ci * printing would have exceeded the current buffer.
457c87c5fbaSopenharmony_ci */
458c87c5fbaSopenharmony_citypedef unsigned int coap_print_status_t;
459c87c5fbaSopenharmony_ci
460c87c5fbaSopenharmony_ci#define COAP_PRINT_STATUS_MASK  0xF0000000u
461c87c5fbaSopenharmony_ci#define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
462c87c5fbaSopenharmony_ci#define COAP_PRINT_STATUS_ERROR 0x80000000u
463c87c5fbaSopenharmony_ci#define COAP_PRINT_STATUS_TRUNC 0x40000000u
464c87c5fbaSopenharmony_ci
465c87c5fbaSopenharmony_ci/**
466c87c5fbaSopenharmony_ci * Writes a description of this resource in link-format to given text buffer. @p
467c87c5fbaSopenharmony_ci * len must be initialized to the maximum length of @p buf and will be set to
468c87c5fbaSopenharmony_ci * the number of characters actually written if successful. This function
469c87c5fbaSopenharmony_ci * returns @c 1 on success or @c 0 on error.
470c87c5fbaSopenharmony_ci *
471c87c5fbaSopenharmony_ci * @param resource The resource to describe.
472c87c5fbaSopenharmony_ci * @param buf      The output buffer to write the description to.
473c87c5fbaSopenharmony_ci * @param len      Must be initialized to the length of @p buf and
474c87c5fbaSopenharmony_ci *                 will be set to the length of the printed link description.
475c87c5fbaSopenharmony_ci * @param offset   The offset within the resource description where to
476c87c5fbaSopenharmony_ci *                 start writing into @p buf. This is useful for dealing
477c87c5fbaSopenharmony_ci *                 with the Block2 option. @p offset is updated during
478c87c5fbaSopenharmony_ci *                 output as it is consumed.
479c87c5fbaSopenharmony_ci *
480c87c5fbaSopenharmony_ci * @return If COAP_PRINT_STATUS_ERROR is set, an error occured. Otherwise,
481c87c5fbaSopenharmony_ci *         the lower 28 bits will indicate the number of characters that
482c87c5fbaSopenharmony_ci *         have actually been output into @p buffer. The flag
483c87c5fbaSopenharmony_ci *         COAP_PRINT_STATUS_TRUNC indicates that the output has been
484c87c5fbaSopenharmony_ci *         truncated.
485c87c5fbaSopenharmony_ci */
486c87c5fbaSopenharmony_cicoap_print_status_t coap_print_link(const coap_resource_t *resource,
487c87c5fbaSopenharmony_ci                                    unsigned char *buf,
488c87c5fbaSopenharmony_ci                                    size_t *len,
489c87c5fbaSopenharmony_ci                                    size_t *offset);
490c87c5fbaSopenharmony_ci
491c87c5fbaSopenharmony_ci/** @} */
492c87c5fbaSopenharmony_ci
493c87c5fbaSopenharmony_ci/**
494c87c5fbaSopenharmony_ci * Returns the resource identified by the unique string @p uri_path. If no
495c87c5fbaSopenharmony_ci * resource was found, this function returns @c NULL.
496c87c5fbaSopenharmony_ci *
497c87c5fbaSopenharmony_ci * @param context  The context to look for this resource.
498c87c5fbaSopenharmony_ci * @param uri_path  The unique string uri of the resource.
499c87c5fbaSopenharmony_ci *
500c87c5fbaSopenharmony_ci * @return         A pointer to the resource or @c NULL if not found.
501c87c5fbaSopenharmony_ci */
502c87c5fbaSopenharmony_cicoap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context,
503c87c5fbaSopenharmony_ci                                                 coap_str_const_t *uri_path);
504c87c5fbaSopenharmony_ci
505c87c5fbaSopenharmony_ci/**
506c87c5fbaSopenharmony_ci * @deprecated use coap_resource_notify_observers() instead.
507c87c5fbaSopenharmony_ci */
508c87c5fbaSopenharmony_ciCOAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r,
509c87c5fbaSopenharmony_ci                                            const coap_string_t *query);
510c87c5fbaSopenharmony_ci
511c87c5fbaSopenharmony_ci#endif /* COAP_RESOURCE_H_ */
512