/src/libcoap/include/coap3/coap_resource_internal.h
Line | Count | Source |
1 | | /* |
2 | | * coap_resource_internal.h -- generic resource handling |
3 | | * |
4 | | * Copyright (C) 2010,2011,2014-2026 Olaf Bergmann <bergmann@tzi.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | * |
8 | | * This file is part of the CoAP library libcoap. Please see README for terms |
9 | | * of use. |
10 | | */ |
11 | | |
12 | | /** |
13 | | * @file coap_resource_internal.h |
14 | | * @brief Generic resource internal handling |
15 | | */ |
16 | | |
17 | | #ifndef COAP_RESOURCE_INTERNAL_H_ |
18 | | #define COAP_RESOURCE_INTERNAL_H_ |
19 | | |
20 | | #include "coap_threadsafe_internal.h" |
21 | | #include "coap_uthash_internal.h" |
22 | | |
23 | | #ifdef __cplusplus |
24 | | extern "C" { |
25 | | #endif |
26 | | |
27 | | #if COAP_SERVER_SUPPORT |
28 | | /** |
29 | | * @ingroup internal_api |
30 | | * @defgroup coap_resource_internal Resources |
31 | | * Internal API for handling resources |
32 | | * @{ |
33 | | */ |
34 | | |
35 | | /** |
36 | | * Limits the number of subscribers for each resource that this server support. |
37 | | * Zero means there is no maximum. |
38 | | */ |
39 | | #ifndef COAP_RESOURCE_MAX_SUBSCRIBER |
40 | | #define COAP_RESOURCE_MAX_SUBSCRIBER 0 |
41 | | #endif /* COAP_RESOURCE_MAX_SUBSCRIBER */ |
42 | | |
43 | | /** |
44 | | * Abstraction of attribute associated with a resource. |
45 | | */ |
46 | | struct coap_attr_t { |
47 | | struct coap_attr_t *next; /**< Pointer to next in chain or NULL */ |
48 | | coap_str_const_t *name; /**< Name of the attribute */ |
49 | | coap_str_const_t *value; /**< Value of the attribute (can be NULL) */ |
50 | | int flags; |
51 | | }; |
52 | | |
53 | | /** |
54 | | * Abstraction of resource that can be attached to coap_context_t. |
55 | | * The key is uri_path. |
56 | | */ |
57 | | struct coap_resource_t { |
58 | | unsigned int dirty:1; /**< set to 1 if resource has changed */ |
59 | | unsigned int partiallydirty:1; /**< set to 1 if some subscribers have not yet |
60 | | * been notified of the last change */ |
61 | | unsigned int observable:1; /**< can be observed */ |
62 | | unsigned int cacheable:1; /**< can be cached */ |
63 | | unsigned int is_unknown:1; /**< resource created for unknown handler */ |
64 | | unsigned int is_proxy_uri:1; /**< resource created for proxy URI handler */ |
65 | | unsigned int is_reverse_proxy:1; /**< resource created for reverse proxy URI handler */ |
66 | | unsigned int list_being_traversed:1; /**< resource subscriber list being traversed */ |
67 | | unsigned int is_dynamic:1; /**< create unknown resource dynamically */ |
68 | | |
69 | | uint32_t ref; /**< Resource reference count */ |
70 | | /** |
71 | | * Used to store handlers for the seven coap methods @c GET, @c POST, @c PUT, |
72 | | * @c DELETE, @c FETCH, @c PATCH and @c IPATCH. |
73 | | * coap_dispatch() will pass incoming requests to handle_request() and then |
74 | | * to the handler that corresponds to its request method or generate a 4.05 |
75 | | * response if no handler is available. |
76 | | */ |
77 | | coap_method_handler_t handler[7]; |
78 | | |
79 | | UT_hash_handle hh; |
80 | | |
81 | | coap_attr_t *link_attr; /**< attributes to be included with the link format */ |
82 | | coap_subscription_t *subscribers; /**< list of observers for this resource */ |
83 | | |
84 | | /** |
85 | | * Request URI Path for this resource. This field will point into static |
86 | | * or allocated memory which must remain there for the duration of the |
87 | | * resource. |
88 | | */ |
89 | | coap_str_const_t *uri_path; /**< the key used for hash lookup for this |
90 | | resource */ |
91 | | int flags; /**< zero or more COAP_RESOURCE_FLAGS_* or'd together */ |
92 | | |
93 | | /** |
94 | | * The next value for the Observe option. This field must be increased each |
95 | | * time the resource changes. Only the lower 24 bits are sent. |
96 | | */ |
97 | | unsigned int observe; |
98 | | |
99 | | /** |
100 | | * Pointer back to the context that 'owns' this resource. |
101 | | */ |
102 | | coap_context_t *context; |
103 | | |
104 | | /** |
105 | | * Count of valid names this host is known by (proxy support) |
106 | | */ |
107 | | size_t proxy_name_count; |
108 | | |
109 | | /** |
110 | | * Array valid names this host is known by (proxy support) |
111 | | */ |
112 | | coap_str_const_t **proxy_name_list; |
113 | | |
114 | | /** |
115 | | * This pointer is under user control. It can be used to store context for |
116 | | * the coap handler. |
117 | | */ |
118 | | void *user_data; |
119 | | #if COAP_THREAD_SAFE |
120 | | coap_lock_t lock; |
121 | | #endif /* COAP_THREAD_SAFE */ |
122 | | |
123 | | }; |
124 | | |
125 | | typedef enum coap_deleting_resource_t { |
126 | | COAP_DELETING_RESOURCE, |
127 | | COAP_NOT_DELETING_RESOURCE, |
128 | | COAP_DELETING_RESOURCE_ON_EXIT |
129 | | } coap_deleting_resource_t; |
130 | | |
131 | | /** |
132 | | * Registers the given @p resource for @p context. The resource must have been |
133 | | * created by coap_resource_init() or coap_resource_unknown_init(), the |
134 | | * storage allocated for the resource will be released by coap_delete_resource_lkd(). |
135 | | * |
136 | | * Note: This function must be called in the locked state. |
137 | | * |
138 | | * @param context The context to use. |
139 | | * @param resource The resource to store. |
140 | | */ |
141 | | void coap_add_resource_lkd(coap_context_t *context, coap_resource_t *resource); |
142 | | |
143 | | /** |
144 | | * Deletes a resource identified by @p resource. The storage allocated for that |
145 | | * resource is freed, and removed from the context. |
146 | | * |
147 | | * Note: This function must be called in the locked state. |
148 | | * |
149 | | * @param resource The resource to delete. |
150 | | * |
151 | | * @return @c 1 if the resource was found (and destroyed), |
152 | | * @c 0 otherwise. |
153 | | */ |
154 | | int coap_delete_resource_lkd(coap_resource_t *resource); |
155 | | |
156 | | /** |
157 | | * Decrement reference counter on a resource. |
158 | | * Note that the resource storage may be deleted as a result and should not be |
159 | | * used after this call. |
160 | | * |
161 | | * Note: This function must be called in the locked state. |
162 | | * |
163 | | * @param resource The CoAP resource. |
164 | | */ |
165 | | COAP_STATIC_INLINE int |
166 | 2.74k | coap_resource_release_lkd(coap_resource_t *resource) { |
167 | 2.74k | return coap_delete_resource_lkd(resource); |
168 | 2.74k | } Unexecuted instantiation: oscore_decrypt_target.c:coap_resource_release_lkd Unexecuted instantiation: coap_address.c:coap_resource_release_lkd Unexecuted instantiation: coap_debug.c:coap_resource_release_lkd Unexecuted instantiation: coap_encode.c:coap_resource_release_lkd Unexecuted instantiation: coap_mem.c:coap_resource_release_lkd coap_net.c:coap_resource_release_lkd Line | Count | Source | 166 | 291 | coap_resource_release_lkd(coap_resource_t *resource) { | 167 | 291 | return coap_delete_resource_lkd(resource); | 168 | 291 | } |
Unexecuted instantiation: coap_netif.c:coap_resource_release_lkd Unexecuted instantiation: coap_openssl.c:coap_resource_release_lkd Unexecuted instantiation: coap_option.c:coap_resource_release_lkd Unexecuted instantiation: coap_oscore.c:coap_resource_release_lkd Unexecuted instantiation: coap_pdu.c:coap_resource_release_lkd Unexecuted instantiation: coap_proxy.c:coap_resource_release_lkd Unexecuted instantiation: coap_prng.c:coap_resource_release_lkd coap_resource.c:coap_resource_release_lkd Line | Count | Source | 166 | 2.45k | coap_resource_release_lkd(coap_resource_t *resource) { | 167 | 2.45k | return coap_delete_resource_lkd(resource); | 168 | 2.45k | } |
Unexecuted instantiation: coap_session.c:coap_resource_release_lkd Unexecuted instantiation: coap_str.c:coap_resource_release_lkd Unexecuted instantiation: coap_strm_posix.c:coap_resource_release_lkd Unexecuted instantiation: coap_subscribe.c:coap_resource_release_lkd Unexecuted instantiation: coap_time.c:coap_resource_release_lkd Unexecuted instantiation: coap_uri.c:coap_resource_release_lkd Unexecuted instantiation: coap_ws.c:coap_resource_release_lkd Unexecuted instantiation: oscore.c:coap_resource_release_lkd Unexecuted instantiation: oscore_cbor.c:coap_resource_release_lkd Unexecuted instantiation: oscore_context.c:coap_resource_release_lkd Unexecuted instantiation: oscore_cose.c:coap_resource_release_lkd Unexecuted instantiation: oscore_crypto.c:coap_resource_release_lkd Unexecuted instantiation: coap_async.c:coap_resource_release_lkd Unexecuted instantiation: coap_block.c:coap_resource_release_lkd Unexecuted instantiation: coap_cache.c:coap_resource_release_lkd Unexecuted instantiation: coap_dgrm_posix.c:coap_resource_release_lkd Unexecuted instantiation: coap_dtls.c:coap_resource_release_lkd Unexecuted instantiation: coap_io.c:coap_resource_release_lkd Unexecuted instantiation: coap_io_posix.c:coap_resource_release_lkd Unexecuted instantiation: coap_layers.c:coap_resource_release_lkd Unexecuted instantiation: pdu_parse_udp_target.c:coap_resource_release_lkd Unexecuted instantiation: dtls_define_target.c:coap_resource_release_lkd Unexecuted instantiation: uri_extended_target.c:coap_resource_release_lkd Unexecuted instantiation: ws_frame_target.c:coap_resource_release_lkd Unexecuted instantiation: coap_event.c:coap_resource_release_lkd Unexecuted instantiation: async_target.c:coap_resource_release_lkd Unexecuted instantiation: coap_fuzz_helper.c:coap_resource_release_lkd Unexecuted instantiation: block_target.c:coap_resource_release_lkd Unexecuted instantiation: split_uri_target.c:coap_resource_release_lkd Unexecuted instantiation: pdu_parse_tcp_target.c:coap_resource_release_lkd Unexecuted instantiation: persist_target.c:coap_resource_release_lkd Unexecuted instantiation: observe_target.c:coap_resource_release_lkd Unexecuted instantiation: pdu_parse_ws_target.c:coap_resource_release_lkd Unexecuted instantiation: cache_key_target.c:coap_resource_release_lkd Unexecuted instantiation: proxy_target.c:coap_resource_release_lkd Unexecuted instantiation: network_message_target.c:coap_resource_release_lkd Unexecuted instantiation: oscore_conf_parse_target.c:coap_resource_release_lkd Unexecuted instantiation: get_asn1_tag_target.c:coap_resource_release_lkd Unexecuted instantiation: coap_asn1.c:coap_resource_release_lkd Unexecuted instantiation: block_check_target.c:coap_resource_release_lkd |
169 | | |
170 | | /** |
171 | | * Deletes all resources from given @p context and frees their storage. |
172 | | * |
173 | | * @param context The CoAP context with the resources to be deleted. |
174 | | */ |
175 | | void coap_delete_all_resources(coap_context_t *context); |
176 | | |
177 | | /** |
178 | | * Increment reference counter on a resource. |
179 | | * |
180 | | * Note: This function must be called in the locked state. |
181 | | * |
182 | | * @param resource The CoAP resource. |
183 | | */ |
184 | | void coap_resource_reference_lkd(coap_resource_t *resource); |
185 | | |
186 | | #define RESOURCES_ADD(r, obj) \ |
187 | 2.50k | HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj)) |
188 | | |
189 | | #define RESOURCES_DELETE(r, obj) \ |
190 | 2.50k | HASH_DELETE(hh, (r), (obj)) |
191 | | |
192 | | #define RESOURCES_ITER(r,tmp) \ |
193 | 3.71k | coap_resource_t *tmp, *rtmp; \ |
194 | 3.71k | HASH_ITER(hh, (r), tmp, rtmp) |
195 | | |
196 | | #define RESOURCE_ITER_SAFE(e, el, rtmp) \ |
197 | 8.08k | for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp)) |
198 | | |
199 | 12.2k | #define RESOURCES_FIND(r, k, res) { \ |
200 | 12.2k | HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \ |
201 | 12.2k | } |
202 | | |
203 | | /** |
204 | | * Returns the resource identified by the unique string @p uri_path. If no |
205 | | * resource was found, this function returns @c NULL. |
206 | | * |
207 | | * Note: This function must be called in the locked state. |
208 | | * |
209 | | * @param context The context to look for this resource. |
210 | | * @param uri_path The unique string uri of the resource. |
211 | | * |
212 | | * @return A pointer to the resource or @c NULL if not found. |
213 | | */ |
214 | | coap_resource_t *coap_get_resource_from_uri_path_lkd(coap_context_t *context, |
215 | | coap_str_const_t *uri_path); |
216 | | |
217 | | /** |
218 | | * Deletes an attribute. |
219 | | * Note: This is for internal use only, as it is not deleted from its chain. |
220 | | * |
221 | | * @param attr Pointer to a previously created attribute. |
222 | | * |
223 | | */ |
224 | | void coap_delete_attr(coap_attr_t *attr); |
225 | | |
226 | | /** |
227 | | * Prints the names of all known resources for @p context to @p buf. This function |
228 | | * sets @p buflen to the number of bytes actually written and returns |
229 | | * @c COAP_PRINT_STATUS_ERROR on error. On error, the value in @p buflen is undefined. |
230 | | * Otherwise, the lower 28 bits are set to the number of bytes that have actually |
231 | | * been written. COAP_PRINT_STATUS_TRUNC is set when the output has been truncated. |
232 | | * |
233 | | * Note: This function must be called in the locked state. |
234 | | * |
235 | | * @param context The context with the resource map. |
236 | | * @param buf The buffer to write the result. |
237 | | * @param buflen Must be initialized to the maximum length of @p buf and will be |
238 | | * set to the length of the well-known response on return. |
239 | | * @param offset The offset in bytes where the output shall start and is |
240 | | * shifted accordingly with the characters that have been |
241 | | * processed. This parameter is used to support the block |
242 | | * option. |
243 | | * @param query_filter A filter query according to <a href="http://tools.ietf.org/html/draft-ietf-core-link-format-11#section-4.1">Link Format</a> |
244 | | * |
245 | | * @return COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are |
246 | | * set to the number of bytes that have actually been written to |
247 | | * @p buf. COAP_PRINT_STATUS_TRUNC is set when the output has been |
248 | | * truncated. |
249 | | */ |
250 | | coap_print_status_t coap_print_wellknown_lkd(coap_context_t *context, |
251 | | unsigned char *buf, |
252 | | size_t *buflen, |
253 | | size_t offset, |
254 | | const coap_string_t *query_filter); |
255 | | |
256 | | /** |
257 | | * Check to see if a dynamic resource needs to be added. |
258 | | * |
259 | | * @param session Session potentially adding a new dynamic resource. |
260 | | * @param pdu PDU that is asking for an unknown resource. |
261 | | * |
262 | | * @return Created resource or NULL is resource eneration not applicable. |
263 | | */ |
264 | | coap_resource_t *coap_add_dynamic_resource(coap_session_t *session, |
265 | | coap_pdu_t *pdu); |
266 | | |
267 | | /** @} */ |
268 | | |
269 | | #endif /* COAP_SERVER_SUPPORT */ |
270 | | |
271 | | #ifdef __cplusplus |
272 | | } |
273 | | #endif |
274 | | |
275 | | #endif /* COAP_RESOURCE_INTERNAL_H_ */ |