1c87c5fbaSopenharmony_ci// -*- mode:doc; -*-
2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0
3c87c5fbaSopenharmony_ci
4c87c5fbaSopenharmony_cicoap_resource(3)
5c87c5fbaSopenharmony_ci=================
6c87c5fbaSopenharmony_ci:doctype: manpage
7c87c5fbaSopenharmony_ci:man source:   coap_resource
8c87c5fbaSopenharmony_ci:man version:  @PACKAGE_VERSION@
9c87c5fbaSopenharmony_ci:man manual:   libcoap Manual
10c87c5fbaSopenharmony_ci
11c87c5fbaSopenharmony_ciNAME
12c87c5fbaSopenharmony_ci----
13c87c5fbaSopenharmony_cicoap_resource,
14c87c5fbaSopenharmony_cicoap_resource_init,
15c87c5fbaSopenharmony_cicoap_resource_unknown_init,
16c87c5fbaSopenharmony_cicoap_resource_unknown_init2,
17c87c5fbaSopenharmony_cicoap_resource_proxy_uri_init,
18c87c5fbaSopenharmony_cicoap_resource_proxy_uri_init2,
19c87c5fbaSopenharmony_cicoap_add_resource,
20c87c5fbaSopenharmony_cicoap_delete_resource,
21c87c5fbaSopenharmony_cicoap_resource_set_mode,
22c87c5fbaSopenharmony_cicoap_resource_set_userdata,
23c87c5fbaSopenharmony_cicoap_resource_get_userdata,
24c87c5fbaSopenharmony_cicoap_resource_release_userdata_handler,
25c87c5fbaSopenharmony_cicoap_resource_get_uri_path
26c87c5fbaSopenharmony_ci- Work with CoAP resources
27c87c5fbaSopenharmony_ci
28c87c5fbaSopenharmony_ciSYNOPSIS
29c87c5fbaSopenharmony_ci--------
30c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>*
31c87c5fbaSopenharmony_ci
32c87c5fbaSopenharmony_ci*coap_resource_t *coap_resource_init(coap_str_const_t *_uri_path_,
33c87c5fbaSopenharmony_ciint _flags_);*
34c87c5fbaSopenharmony_ci
35c87c5fbaSopenharmony_ci*coap_resource_t *coap_resource_unknown_init(coap_method_handler_t
36c87c5fbaSopenharmony_ci_put_handler_);*
37c87c5fbaSopenharmony_ci
38c87c5fbaSopenharmony_ci*coap_resource_t *coap_resource_unknown_init2(coap_method_handler_t
39c87c5fbaSopenharmony_ci_put_handler_, int _flags_);*
40c87c5fbaSopenharmony_ci
41c87c5fbaSopenharmony_ci*coap_resource_t *coap_resource_proxy_uri_init(coap_method_handler_t
42c87c5fbaSopenharmony_ci_proxy_handler_, size_t _host_name_count_, const char *_host_name_list_[]);*
43c87c5fbaSopenharmony_ci
44c87c5fbaSopenharmony_ci*coap_resource_t *coap_resource_proxy_uri_init2(coap_method_handler_t
45c87c5fbaSopenharmony_ci_proxy_handler_, size_t _host_name_count_, const char *_host_name_list_[],
46c87c5fbaSopenharmony_ciint _flags_);*
47c87c5fbaSopenharmony_ci
48c87c5fbaSopenharmony_ci*void coap_add_resource(coap_context_t *_context_,
49c87c5fbaSopenharmony_cicoap_resource_t *_resource_);*
50c87c5fbaSopenharmony_ci
51c87c5fbaSopenharmony_ci*int coap_delete_resource(coap_context_t *_context_,
52c87c5fbaSopenharmony_cicoap_resource_t *_resource_);*
53c87c5fbaSopenharmony_ci
54c87c5fbaSopenharmony_ci*void coap_resource_set_mode(coap_resource_t *_resource_, int _mode_);*
55c87c5fbaSopenharmony_ci
56c87c5fbaSopenharmony_ci*void coap_resource_set_userdata(coap_resource_t *_resource_, void *_data_);*
57c87c5fbaSopenharmony_ci
58c87c5fbaSopenharmony_ci*void *coap_resource_get_userdata(coap_resource_t *_resource_);*
59c87c5fbaSopenharmony_ci
60c87c5fbaSopenharmony_ci*void coap_resource_release_userdata_handler(coap_context_t *_context_,
61c87c5fbaSopenharmony_cicoap_resource_release_userdata_handler_t _callback_);*
62c87c5fbaSopenharmony_ci
63c87c5fbaSopenharmony_ci*coap_str_const_t *coap_resource_get_uri_path(coap_resource_t *_resource_);*
64c87c5fbaSopenharmony_ci
65c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with
66c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
67c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*
68c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*.   Otherwise, link with
69c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support.
70c87c5fbaSopenharmony_ci
71c87c5fbaSopenharmony_ciDESCRIPTION
72c87c5fbaSopenharmony_ci-----------
73c87c5fbaSopenharmony_ciCoAP Resources on a CoAP Server need to be created, updated and deleted as
74c87c5fbaSopenharmony_ciappropriate. The URI in
75c87c5fbaSopenharmony_cithe request packet defines the resource to work with, with possibly the Query
76c87c5fbaSopenharmony_cior data in the request referring to a sub-resource.
77c87c5fbaSopenharmony_ci
78c87c5fbaSopenharmony_ciWhen resources are configured on the CoAP server, the URI to match against
79c87c5fbaSopenharmony_ciin the request packet is specified.
80c87c5fbaSopenharmony_ci
81c87c5fbaSopenharmony_ciCallback Handlers are then added to the resource to handle the different
82c87c5fbaSopenharmony_cirequest methods. See *coap_register_request_handler*(3) for further information.
83c87c5fbaSopenharmony_ci
84c87c5fbaSopenharmony_ciAdding Attributes allows textual information to be added to the resource
85c87c5fbaSopenharmony_ciwhich can then be reported back to any client doing a "GET .well-known/core"
86c87c5fbaSopenharmony_cirequest. See *coap_add_attr*(3) for further information.
87c87c5fbaSopenharmony_ci
88c87c5fbaSopenharmony_ciIf an incoming packet request matches a resource's URI and Method, then
89c87c5fbaSopenharmony_cithe appropriate callback resource handler is invoked to process the packet
90c87c5fbaSopenharmony_ciwhich should then update a suitable response packet for returning back to the
91c87c5fbaSopenharmony_cirequester.
92c87c5fbaSopenharmony_ci
93c87c5fbaSopenharmony_ciThere is support for handling incoming packets where the URI is unknown (no
94c87c5fbaSopenharmony_cispecific resource has been created).
95c87c5fbaSopenharmony_ciThis could, for example, happen when a PUT request is trying to create a new
96c87c5fbaSopenharmony_ciresource. It is the responsibility of the unknown resource callback handler
97c87c5fbaSopenharmony_cito either create a new resource for the URI or just manage things separately.
98c87c5fbaSopenharmony_ci
99c87c5fbaSopenharmony_ciCoAP Observe (https://rfc-editor.org/rfc/rfc7641[RFC7641]) is not supported
100c87c5fbaSopenharmony_cifor unknown resources, so a new
101c87c5fbaSopenharmony_ciresource with GET handler must be created by the unknown resource callback
102c87c5fbaSopenharmony_cihandle matching the URI which then can be Observable.
103c87c5fbaSopenharmony_ci
104c87c5fbaSopenharmony_ciThere is support for handling incoming proxy based requests using the Proxy-Uri
105c87c5fbaSopenharmony_cior Proxy-Scheme options.
106c87c5fbaSopenharmony_ci
107c87c5fbaSopenharmony_ciFUNCTIONS
108c87c5fbaSopenharmony_ci---------
109c87c5fbaSopenharmony_ci
110c87c5fbaSopenharmony_ci*Function: coap_resource_init()*
111c87c5fbaSopenharmony_ci
112c87c5fbaSopenharmony_ciThe *coap_resource_init*() function returns a newly created _resource_ of
113c87c5fbaSopenharmony_citype _coap_resource_t_ * .  _uri_path_ specifies the uri string path to match
114c87c5fbaSopenharmony_ciagainst.  Normally there is no need for the leading '/' - e.g. just
115c87c5fbaSopenharmony_ci"full/path/for/resource".
116c87c5fbaSopenharmony_ci_flags_ can be zero or more of the following definitions or'ed together.
117c87c5fbaSopenharmony_ci
118c87c5fbaSopenharmony_ci[horizontal]
119c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_NOTIFY_NON*::
120c87c5fbaSopenharmony_ciSet the notification message type to non-confirmable for any trigggered
121c87c5fbaSopenharmony_ci"observe" responses with type set to confirmable every 5 packets as required by
122c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7641#section-4.5[RFC7641 4.5. Transmission]".
123c87c5fbaSopenharmony_ciSee *coap_observe*(3). +
124c87c5fbaSopenharmony_ci*NOTE:* This flag is ignored if COAP_RESOURCE_FLAGS_NOTIFY_CON is set.
125c87c5fbaSopenharmony_ci
126c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS*::
127c87c5fbaSopenharmony_ciSet the notification message type to always non-confirmable for any trigggered
128c87c5fbaSopenharmony_ci"observe" responses. This should only be used if a upper layer protocol
129c87c5fbaSopenharmony_cirequires it. See *coap_observe*(3). +
130c87c5fbaSopenharmony_ci*NOTE:* This flag is ignored if COAP_RESOURCE_FLAGS_NOTIFY_CON is set.
131c87c5fbaSopenharmony_ci
132c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_NOTIFY_CON*::
133c87c5fbaSopenharmony_ciSet the notification message type to confirmable for any trigggered
134c87c5fbaSopenharmony_ci"observe" responses. See *coap_observe*(3). +
135c87c5fbaSopenharmony_ci*NOTE:* COAP_RESOURCE_FLAGS_NOTIFY_NON is assumed if
136c87c5fbaSopenharmony_ciCOAP_RESOURCE_FLAGS_NOTIFY_CON is not set.
137c87c5fbaSopenharmony_ci
138c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_RELEASE_URI*::
139c87c5fbaSopenharmony_ciFree off the coap_str_const_t for _uri_path_ when the _resource_ is deleted.
140c87c5fbaSopenharmony_ci
141c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_FORCE_SINGLE_BODY*::
142c87c5fbaSopenharmony_ciForce all large traffic to this resource to be presented as a single body
143c87c5fbaSopenharmony_cito the request handler.
144c87c5fbaSopenharmony_ci
145c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_OSCORE_ONLY*::
146c87c5fbaSopenharmony_ciDefine this resource as an OSCORE enabled access only.
147c87c5fbaSopenharmony_ci
148c87c5fbaSopenharmony_ci*NOTE:* The following flags are only tested against if
149c87c5fbaSopenharmony_ci*coap_mcast_per_resource*() has been called.  If *coap_mcast_per_resource*()
150c87c5fbaSopenharmony_cihas not been called, then all resources have multicast support, libcoap adds
151c87c5fbaSopenharmony_ciin random delays to the responses, and 4.xx / 5.xx responses are dropped.
152c87c5fbaSopenharmony_ci
153c87c5fbaSopenharmony_ci*NOTE:* The pseudo resource for ".well-known/core" always has multicast
154c87c5fbaSopenharmony_cisupport enabled and is not configurable.
155c87c5fbaSopenharmony_ci
156c87c5fbaSopenharmony_ci[horizontal]
157c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT*::
158c87c5fbaSopenharmony_ciThis resource has support for multicast requests.
159c87c5fbaSopenharmony_ci
160c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_DELAYS*::
161c87c5fbaSopenharmony_ciDisable libcoap library from adding in delays to multicast requests before
162c87c5fbaSopenharmony_cisending the response back to the client.  It is then the responsibility of
163c87c5fbaSopenharmony_cithe app to delay the responses for multicast requests. See
164c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252#section-8.2[RFC7252 8.2. Request/Response
165c87c5fbaSopenharmony_ciLayer]".
166c87c5fbaSopenharmony_ci However, the pseudo resource for ".well-known/core" always has multicast
167c87c5fbaSopenharmony_cisupport enabled.
168c87c5fbaSopenharmony_ci
169c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_05*::
170c87c5fbaSopenharmony_ciEnable libcoap library suppression of 205 multicast responses that are empty
171c87c5fbaSopenharmony_ci(overridden by https://rfc-editor.org/rfc/rfc7967[RFC7967] No-Response option)
172c87c5fbaSopenharmony_cifor multicast requests.
173c87c5fbaSopenharmony_ci
174c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_XX*::
175c87c5fbaSopenharmony_ciEnable libcoap library suppressing 2.xx multicast responses (overridden by
176c87c5fbaSopenharmony_cihttps://rfc-editor.org/rfc/rfc7967[RFC7967] No-Response option) for multicast
177c87c5fbaSopenharmony_cirequests.
178c87c5fbaSopenharmony_ci
179c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_4_XX*::
180c87c5fbaSopenharmony_ciDisable libcoap library suppressing 4.xx multicast responses (overridden by
181c87c5fbaSopenharmony_cihttps://rfc-editor.org/rfc/rfc7967[RFC7967] No-Response option) for multicast
182c87c5fbaSopenharmony_cirequests.
183c87c5fbaSopenharmony_ci
184c87c5fbaSopenharmony_ci*COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_5_XX*::
185c87c5fbaSopenharmony_ciDisable libcoap library suppressing 5.xx multicast responses (overridden by
186c87c5fbaSopenharmony_cihttps://rfc-editor.org/rfc/rfc7967[RFC7967] No-Response option) for multicast
187c87c5fbaSopenharmony_cirequests.
188c87c5fbaSopenharmony_ci
189c87c5fbaSopenharmony_ci*NOTE:* _uri_path_, if not 7 bit readable ASCII, binary bytes must be hex
190c87c5fbaSopenharmony_ciencoded according to the rules defined in
191c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc3986#section-2.1[RFC3986 2.1. Percent-Encoding]".
192c87c5fbaSopenharmony_ci
193c87c5fbaSopenharmony_ci*Function: coap_resource_unknown_init()*
194c87c5fbaSopenharmony_ci
195c87c5fbaSopenharmony_ciThe *coap_resource_unknown_init*() function returns a newly created _resource_
196c87c5fbaSopenharmony_ciof type _coap_resource_t_ *. _put_handler_ is automatically added to the
197c87c5fbaSopenharmony_ci_resource_ to handle PUT requests to resources that are unknown. Additional
198c87c5fbaSopenharmony_cihandlers can be added to this resource if required.
199c87c5fbaSopenharmony_ci
200c87c5fbaSopenharmony_ci*Function: coap_resource_unknown_init2()*
201c87c5fbaSopenharmony_ci
202c87c5fbaSopenharmony_ciThe *coap_resource_unknown_init2*() function returns a newly created _resource_
203c87c5fbaSopenharmony_ciof type _coap_resource_t_ *. _put_handler_ is automatically added to the
204c87c5fbaSopenharmony_ci_resource_ to handle PUT requests to resources that are unknown. Additional
205c87c5fbaSopenharmony_cihandlers can be added to this resource if required. _flags_ can be zero or
206c87c5fbaSopenharmony_cimore of the COAP_RESOURCE_FLAGS MCAST definitions.
207c87c5fbaSopenharmony_ci
208c87c5fbaSopenharmony_ci*Function: coap_resource_proxy_uri_init()*
209c87c5fbaSopenharmony_ci
210c87c5fbaSopenharmony_ciThe *coap_resource_proxy_uri_init*() function returns a newly created
211c87c5fbaSopenharmony_ci_resource_ of type _coap_resource_t_ *. _proxy_handler_ is automatically added
212c87c5fbaSopenharmony_cito the _resource_ to handle PUT/POST/GET etc. requests that use the Proxy-Uri
213c87c5fbaSopenharmony_cioption.  There is no need to add explicit request type handlers. One or more
214c87c5fbaSopenharmony_cinames by which the proxy is known by (IP address, DNS name etc.) must be
215c87c5fbaSopenharmony_cisupplied in the array defined by _host_name_list_[] which has a count of
216c87c5fbaSopenharmony_ci_host_name_count_.  This is used to check whether the current endpoint is
217c87c5fbaSopenharmony_cithe proxy target address, or the request has to be passed on to an upstream
218c87c5fbaSopenharmony_ciserver.
219c87c5fbaSopenharmony_ci
220c87c5fbaSopenharmony_ci*Function: coap_resource_proxy_uri_init2()*
221c87c5fbaSopenharmony_ci
222c87c5fbaSopenharmony_ciThe *coap_resource_proxy_uri_init2*() function returns a newly created
223c87c5fbaSopenharmony_ci_resource_ of type _coap_resource_t_ *. _proxy_handler_ is automatically added
224c87c5fbaSopenharmony_cito the _resource_ to handle PUT/POST/GET etc. requests that use the Proxy-Uri
225c87c5fbaSopenharmony_cioption.  There is no need to add explicit request type handlers. One or more
226c87c5fbaSopenharmony_cinames by which the proxy is known by (IP address, DNS name etc.) must be
227c87c5fbaSopenharmony_cisupplied in the array defined by _host_name_list_[] which has a count of
228c87c5fbaSopenharmony_ci_host_name_count_.  This is used to check whether the current endpoint is
229c87c5fbaSopenharmony_cithe proxy target address, or the request has to be passed on to an upstream
230c87c5fbaSopenharmony_ciserver. _flags_ can be zero or more COAP_RESOURCE_FLAGS MCAST definitions.
231c87c5fbaSopenharmony_ci
232c87c5fbaSopenharmony_ci*Function: coap_add_resource()*
233c87c5fbaSopenharmony_ci
234c87c5fbaSopenharmony_ciThe *coap_add_resource*() function registers the given _resource_ with the
235c87c5fbaSopenharmony_ci_context_. The _resource_ must have been created by *coap_resource_init*(),
236c87c5fbaSopenharmony_ci*coap_resource_unknown_init*(), *coap_resource_unknown_init2*(),
237c87c5fbaSopenharmony_ci*coap_resource_proxy_uri_init*() or *coap_resource_proxy_uri_init2*(). The
238c87c5fbaSopenharmony_cistorage allocated for the _resource_ will be released by
239c87c5fbaSopenharmony_ci*coap_delete_resource*().
240c87c5fbaSopenharmony_ci
241c87c5fbaSopenharmony_ciAs the _uri_path_ of the resource has to be unique across all of the resources
242c87c5fbaSopenharmony_ciassociated with a _context_, *coap_add_resource*() will delete any previous
243c87c5fbaSopenharmony_ci_resource_ with the same _uri_path_ before adding in the new _resource_.
244c87c5fbaSopenharmony_ci
245c87c5fbaSopenharmony_ci*Function: coap_delete_resource()*
246c87c5fbaSopenharmony_ci
247c87c5fbaSopenharmony_ciThe *coap_delete_resource*() function deletes the resource identified by
248c87c5fbaSopenharmony_ci_resource_. The _context_ parameter is ignored. The storage allocated for that
249c87c5fbaSopenharmony_ci_resource_ is freed, along with any attributes associated with the _resource_.
250c87c5fbaSopenharmony_ci
251c87c5fbaSopenharmony_ci*Function: coap_resource_set_mode()*
252c87c5fbaSopenharmony_ci
253c87c5fbaSopenharmony_ciThe *coap_resource_set_mode*() changes the unsolicited notification message
254c87c5fbaSopenharmony_citype of _resource_ to the given _mode_ which must be one of
255c87c5fbaSopenharmony_ciCOAP_RESOURCE_FLAGS_NOTIFY_NON, COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS or
256c87c5fbaSopenharmony_ciCOAP_RESOURCE_FLAGS_NOTIFY_CON.
257c87c5fbaSopenharmony_ci
258c87c5fbaSopenharmony_ci*Function: coap_resource_set_userdata()*
259c87c5fbaSopenharmony_ci
260c87c5fbaSopenharmony_ciThe *coap_resource_set_userdata*() function allows a pointer to user _data_
261c87c5fbaSopenharmony_cito be associated with a _resource_ that can accessed in any callback that
262c87c5fbaSopenharmony_ciincludes _resource_ as a parameter.
263c87c5fbaSopenharmony_ci
264c87c5fbaSopenharmony_ci*NOTE:* _data_ must point to a static, or allocated, block of memory.
265c87c5fbaSopenharmony_ci
266c87c5fbaSopenharmony_ci*Function: coap_resource_get_userdata()*
267c87c5fbaSopenharmony_ci
268c87c5fbaSopenharmony_ciThe *coap_resource_get_userdata*() function obtains the user data pointer
269c87c5fbaSopenharmony_cifrom the _resource_ that had previously been set up by
270c87c5fbaSopenharmony_ci*coap_resource_set_userdata*().
271c87c5fbaSopenharmony_ci
272c87c5fbaSopenharmony_ci*Function: coap_resource_release_userdata_handler()*
273c87c5fbaSopenharmony_ci
274c87c5fbaSopenharmony_ciThe *coap_resource_release_userdata_handler*() function defines the _context_
275c87c5fbaSopenharmony_ciwide _callback_ handler to call to release the allocated user data that has
276c87c5fbaSopenharmony_cibeen added to the resource using *coap_resource_set_userdata*() when the
277c87c5fbaSopenharmony_ciresource is deleted. _callback_ can be NULL (which is the default) if nothing
278c87c5fbaSopenharmony_cineeds to be freed off.
279c87c5fbaSopenharmony_ci
280c87c5fbaSopenharmony_ci*Function: coap_resource_get_uri_path()*
281c87c5fbaSopenharmony_ci
282c87c5fbaSopenharmony_ciThe *coap_resource_get_uri_path*() function is used to obtain the UriPath of
283c87c5fbaSopenharmony_cithe _resource_ definion.
284c87c5fbaSopenharmony_ci
285c87c5fbaSopenharmony_ciRETURN VALUES
286c87c5fbaSopenharmony_ci-------------
287c87c5fbaSopenharmony_ci*coap_resource_init*(), *coap_resource_unknown_init*(),
288c87c5fbaSopenharmony_ci*coap_resource_unknown_init2*(), *coap_resource_proxy_uri_init*() and
289c87c5fbaSopenharmony_ci*coap_resource_proxy_uri_init2*() return a newly created resource
290c87c5fbaSopenharmony_cior NULL if there is a malloc failure.
291c87c5fbaSopenharmony_ci
292c87c5fbaSopenharmony_ci*coap_delete_resource*() returns 0 on failure (_resource_ not
293c87c5fbaSopenharmony_cifound), 1 on success.
294c87c5fbaSopenharmony_ci
295c87c5fbaSopenharmony_ci*coap_resource_get_userdata*() returns the value previously set
296c87c5fbaSopenharmony_ciby the *coap_resource_set_userdata*() function or NULL.
297c87c5fbaSopenharmony_ci
298c87c5fbaSopenharmony_ci*coap_resource_get_uri_path*() returns the uri_path or NULL if
299c87c5fbaSopenharmony_cithere was a failure.
300c87c5fbaSopenharmony_ci
301c87c5fbaSopenharmony_ciEXAMPLES
302c87c5fbaSopenharmony_ci--------
303c87c5fbaSopenharmony_ci*Fixed Resources Set Up*
304c87c5fbaSopenharmony_ci
305c87c5fbaSopenharmony_ci[source, c]
306c87c5fbaSopenharmony_ci----
307c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h>
308c87c5fbaSopenharmony_ci
309c87c5fbaSopenharmony_ci#define INDEX "This is an example server using libcoap\n"
310c87c5fbaSopenharmony_ci
311c87c5fbaSopenharmony_cistatic void
312c87c5fbaSopenharmony_cihnd_get_index(coap_resource_t *resource, coap_session_t *session,
313c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
314c87c5fbaSopenharmony_ci  unsigned char buf[3];
315c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
316c87c5fbaSopenharmony_ci  (void)resource;
317c87c5fbaSopenharmony_ci  (void)session;
318c87c5fbaSopenharmony_ci  (void)request;
319c87c5fbaSopenharmony_ci  (void)query;
320c87c5fbaSopenharmony_ci
321c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
322c87c5fbaSopenharmony_ci
323c87c5fbaSopenharmony_ci  coap_add_option(response,
324c87c5fbaSopenharmony_ci                  COAP_OPTION_CONTENT_TYPE,
325c87c5fbaSopenharmony_ci                  coap_encode_var_safe(buf, sizeof(buf),
326c87c5fbaSopenharmony_ci                                       COAP_MEDIATYPE_TEXT_PLAIN),
327c87c5fbaSopenharmony_ci                  buf);
328c87c5fbaSopenharmony_ci
329c87c5fbaSopenharmony_ci  coap_add_option(response,
330c87c5fbaSopenharmony_ci                  COAP_OPTION_MAXAGE,
331c87c5fbaSopenharmony_ci                  coap_encode_var_safe(buf, sizeof(buf), 0x2ffff), buf);
332c87c5fbaSopenharmony_ci
333c87c5fbaSopenharmony_ci  coap_add_data(response, strlen(INDEX), (const uint8_t *)INDEX);
334c87c5fbaSopenharmony_ci
335c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
336c87c5fbaSopenharmony_ci}
337c87c5fbaSopenharmony_ci
338c87c5fbaSopenharmony_cistatic void
339c87c5fbaSopenharmony_cihnd_delete_time(coap_resource_t *resource, coap_session_t *session,
340c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
341c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
342c87c5fbaSopenharmony_ci  (void)resource;
343c87c5fbaSopenharmony_ci  (void)session;
344c87c5fbaSopenharmony_ci  (void)request;
345c87c5fbaSopenharmony_ci  (void)query;
346c87c5fbaSopenharmony_ci
347c87c5fbaSopenharmony_ci  /* .. code .. */
348c87c5fbaSopenharmony_ci
349c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
350c87c5fbaSopenharmony_ci}
351c87c5fbaSopenharmony_ci
352c87c5fbaSopenharmony_cistatic void
353c87c5fbaSopenharmony_cihnd_get_time(coap_resource_t *resource, coap_session_t *session,
354c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
355c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
356c87c5fbaSopenharmony_ci  (void)resource;
357c87c5fbaSopenharmony_ci  (void)session;
358c87c5fbaSopenharmony_ci  (void)request;
359c87c5fbaSopenharmony_ci  (void)query;
360c87c5fbaSopenharmony_ci  (void)response;
361c87c5fbaSopenharmony_ci
362c87c5fbaSopenharmony_ci  /* .. code .. */
363c87c5fbaSopenharmony_ci
364c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
365c87c5fbaSopenharmony_ci}
366c87c5fbaSopenharmony_ci
367c87c5fbaSopenharmony_cistatic void
368c87c5fbaSopenharmony_cihnd_put_time(coap_resource_t *resource, coap_session_t *session,
369c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
370c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
371c87c5fbaSopenharmony_ci  (void)resource;
372c87c5fbaSopenharmony_ci  (void)session;
373c87c5fbaSopenharmony_ci  (void)request;
374c87c5fbaSopenharmony_ci  (void)query;
375c87c5fbaSopenharmony_ci  (void)response;
376c87c5fbaSopenharmony_ci
377c87c5fbaSopenharmony_ci  /* .. code .. */
378c87c5fbaSopenharmony_ci
379c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
380c87c5fbaSopenharmony_ci}
381c87c5fbaSopenharmony_ci
382c87c5fbaSopenharmony_cistatic void
383c87c5fbaSopenharmony_ciinit_resources(coap_context_t *ctx) {
384c87c5fbaSopenharmony_ci
385c87c5fbaSopenharmony_ci  coap_resource_t *r;
386c87c5fbaSopenharmony_ci
387c87c5fbaSopenharmony_ci  /* Create a resource to return general information */
388c87c5fbaSopenharmony_ci  r = coap_resource_init(NULL, 0);
389c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index);
390c87c5fbaSopenharmony_ci
391c87c5fbaSopenharmony_ci  /* Document resource for '.well-known/core' request */
392c87c5fbaSopenharmony_ci  coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
393c87c5fbaSopenharmony_ci  coap_add_attr(r, coap_make_str_const("title"),
394c87c5fbaSopenharmony_ci                coap_make_str_const("\"General Info\""), 0);
395c87c5fbaSopenharmony_ci
396c87c5fbaSopenharmony_ci  coap_add_resource(ctx, r);
397c87c5fbaSopenharmony_ci
398c87c5fbaSopenharmony_ci  /* Create a resource to return return or update time */
399c87c5fbaSopenharmony_ci  r = coap_resource_init(coap_make_str_const("time"),
400c87c5fbaSopenharmony_ci                         COAP_RESOURCE_FLAGS_NOTIFY_CON);
401c87c5fbaSopenharmony_ci  coap_resource_set_get_observable(r, 1);
402c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_time);
403c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_time);
404c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
405c87c5fbaSopenharmony_ci
406c87c5fbaSopenharmony_ci  /* Document resource for 'time' request */
407c87c5fbaSopenharmony_ci  coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
408c87c5fbaSopenharmony_ci  coap_add_attr(r, coap_make_str_const("title"),
409c87c5fbaSopenharmony_ci                coap_make_str_const("\"Internal Clock\""), 0);
410c87c5fbaSopenharmony_ci  coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"secs\""),
411c87c5fbaSopenharmony_ci                0);
412c87c5fbaSopenharmony_ci  coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""),
413c87c5fbaSopenharmony_ci                0);
414c87c5fbaSopenharmony_ci
415c87c5fbaSopenharmony_ci  coap_add_resource(ctx, r);
416c87c5fbaSopenharmony_ci
417c87c5fbaSopenharmony_ci}
418c87c5fbaSopenharmony_ci----
419c87c5fbaSopenharmony_ci
420c87c5fbaSopenharmony_ci*Dynamic Resources Set Up*
421c87c5fbaSopenharmony_ci
422c87c5fbaSopenharmony_ci[source, c]
423c87c5fbaSopenharmony_ci----
424c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h>
425c87c5fbaSopenharmony_ci
426c87c5fbaSopenharmony_ci/* Regular DELETE handler - used by resources created by the
427c87c5fbaSopenharmony_ci * Unknown Resource PUT handler */
428c87c5fbaSopenharmony_ci
429c87c5fbaSopenharmony_cistatic void
430c87c5fbaSopenharmony_cihnd_delete(coap_resource_t *resource, coap_session_t *session,
431c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
432c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
433c87c5fbaSopenharmony_ci  (void)session;
434c87c5fbaSopenharmony_ci  (void)request;
435c87c5fbaSopenharmony_ci  (void)query;
436c87c5fbaSopenharmony_ci  (void)response;
437c87c5fbaSopenharmony_ci
438c87c5fbaSopenharmony_ci  /* .. code .. */
439c87c5fbaSopenharmony_ci
440c87c5fbaSopenharmony_ci  /* Dynamic resource no longer required - delete it */
441c87c5fbaSopenharmony_ci  coap_delete_resource(NULL, resource);
442c87c5fbaSopenharmony_ci
443c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
444c87c5fbaSopenharmony_ci}
445c87c5fbaSopenharmony_ci
446c87c5fbaSopenharmony_ci/* Regular GET handler - used by resources created by the
447c87c5fbaSopenharmony_ci * Unknown Resource PUT handler */
448c87c5fbaSopenharmony_ci
449c87c5fbaSopenharmony_cistatic void
450c87c5fbaSopenharmony_cihnd_get(coap_resource_t *resource, coap_session_t *session,
451c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
452c87c5fbaSopenharmony_ci
453c87c5fbaSopenharmony_ci  coap_str_const_t *get_uri_path;
454c87c5fbaSopenharmony_ci
455c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
456c87c5fbaSopenharmony_ci  (void)resource;
457c87c5fbaSopenharmony_ci  (void)session;
458c87c5fbaSopenharmony_ci  (void)request;
459c87c5fbaSopenharmony_ci  (void)query;
460c87c5fbaSopenharmony_ci
461c87c5fbaSopenharmony_ci  /*
462c87c5fbaSopenharmony_ci   * Get the specific resource being requested to determine what the response is
463c87c5fbaSopenharmony_ci   * The uri_path string is a const pointer
464c87c5fbaSopenharmony_ci   */
465c87c5fbaSopenharmony_ci
466c87c5fbaSopenharmony_ci  get_uri_path = coap_resource_get_uri_path(resource);
467c87c5fbaSopenharmony_ci
468c87c5fbaSopenharmony_ci  /* .. code .. */
469c87c5fbaSopenharmony_ci
470c87c5fbaSopenharmony_ci  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
471c87c5fbaSopenharmony_ci}
472c87c5fbaSopenharmony_ci
473c87c5fbaSopenharmony_ci/* Regular PUT handler - used by resources created by the
474c87c5fbaSopenharmony_ci * Unknown Resource PUT handler */
475c87c5fbaSopenharmony_ci
476c87c5fbaSopenharmony_cistatic void
477c87c5fbaSopenharmony_cihnd_put(coap_resource_t *resource, coap_session_t *session,
478c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
479c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
480c87c5fbaSopenharmony_ci  (void)resource;
481c87c5fbaSopenharmony_ci  (void)session;
482c87c5fbaSopenharmony_ci  (void)query;
483c87c5fbaSopenharmony_ci
484c87c5fbaSopenharmony_ci  coap_string_t *put_uri_path;
485c87c5fbaSopenharmony_ci  size_t length;
486c87c5fbaSopenharmony_ci  const uint8_t *data;
487c87c5fbaSopenharmony_ci  size_t offset;
488c87c5fbaSopenharmony_ci  size_t total;
489c87c5fbaSopenharmony_ci  int new_resource = 0;
490c87c5fbaSopenharmony_ci
491c87c5fbaSopenharmony_ci  /* get the uri_path */
492c87c5fbaSopenharmony_ci  put_uri_path = coap_get_uri_path(request);
493c87c5fbaSopenharmony_ci  if (!put_uri_path) {
494c87c5fbaSopenharmony_ci    coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
495c87c5fbaSopenharmony_ci    return;
496c87c5fbaSopenharmony_ci  }
497c87c5fbaSopenharmony_ci  coap_get_data_large(request, &length, &data, &offset, &total);
498c87c5fbaSopenharmony_ci
499c87c5fbaSopenharmony_ci  /* .. code .. */
500c87c5fbaSopenharmony_ci
501c87c5fbaSopenharmony_ci  /* Need to do this as coap_get_uri_path() created it */
502c87c5fbaSopenharmony_ci  coap_delete_string(put_uri_path);
503c87c5fbaSopenharmony_ci
504c87c5fbaSopenharmony_ci  if (length + offset < total)
505c87c5fbaSopenharmony_ci    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
506c87c5fbaSopenharmony_ci  else if (new_resource)
507c87c5fbaSopenharmony_ci    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
508c87c5fbaSopenharmony_ci  else
509c87c5fbaSopenharmony_ci    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
510c87c5fbaSopenharmony_ci}
511c87c5fbaSopenharmony_ci
512c87c5fbaSopenharmony_cistatic int
513c87c5fbaSopenharmony_cicheck_url_fn(coap_string_t *uri_path, uint8_t code) {
514c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
515c87c5fbaSopenharmony_ci  (void)uri_path;
516c87c5fbaSopenharmony_ci  (void)code;
517c87c5fbaSopenharmony_ci
518c87c5fbaSopenharmony_ci  /* Code to determine whether the uri is valid or not */
519c87c5fbaSopenharmony_ci
520c87c5fbaSopenharmony_ci  return 1;
521c87c5fbaSopenharmony_ci}
522c87c5fbaSopenharmony_ci
523c87c5fbaSopenharmony_ci/* Unknown Resource PUT handler */
524c87c5fbaSopenharmony_ci
525c87c5fbaSopenharmony_cistatic void
526c87c5fbaSopenharmony_cihnd_put_unknown(coap_resource_t *resource, coap_session_t *session,
527c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
528c87c5fbaSopenharmony_ci  /* Remove (void) definition if variable is used */
529c87c5fbaSopenharmony_ci  (void)resource;
530c87c5fbaSopenharmony_ci  coap_pdu_code_t req_code = coap_pdu_get_code(request);
531c87c5fbaSopenharmony_ci
532c87c5fbaSopenharmony_ci  coap_resource_t *r;
533c87c5fbaSopenharmony_ci  coap_string_t *uri_path;
534c87c5fbaSopenharmony_ci
535c87c5fbaSopenharmony_ci  /* get the uri_path - which will get used by coap_resource_init() */
536c87c5fbaSopenharmony_ci  uri_path = coap_get_uri_path(request);
537c87c5fbaSopenharmony_ci  if (!uri_path) {
538c87c5fbaSopenharmony_ci    coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
539c87c5fbaSopenharmony_ci    return;
540c87c5fbaSopenharmony_ci  }
541c87c5fbaSopenharmony_ci
542c87c5fbaSopenharmony_ci  /* Check if new URI Path is valid */
543c87c5fbaSopenharmony_ci  if (!check_url_fn (uri_path, req_code)) {
544c87c5fbaSopenharmony_ci    coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
545c87c5fbaSopenharmony_ci    coap_delete_string(uri_path);
546c87c5fbaSopenharmony_ci    return;
547c87c5fbaSopenharmony_ci  }
548c87c5fbaSopenharmony_ci
549c87c5fbaSopenharmony_ci  /*
550c87c5fbaSopenharmony_ci   * Create a resource to handle the new URI
551c87c5fbaSopenharmony_ci   * uri_path will get deleted when the resource is removed
552c87c5fbaSopenharmony_ci   */
553c87c5fbaSopenharmony_ci  r = coap_resource_init((coap_str_const_t*)uri_path,
554c87c5fbaSopenharmony_ci        COAP_RESOURCE_FLAGS_RELEASE_URI | COAP_RESOURCE_FLAGS_NOTIFY_NON);
555c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put);
556c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete);
557c87c5fbaSopenharmony_ci  /* We possibly want to Observe the GETs */
558c87c5fbaSopenharmony_ci  coap_resource_set_get_observable(r, 1);
559c87c5fbaSopenharmony_ci  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get);
560c87c5fbaSopenharmony_ci  coap_add_resource(coap_session_get_context(session), r);
561c87c5fbaSopenharmony_ci
562c87c5fbaSopenharmony_ci  /* Do the PUT for this first call */
563c87c5fbaSopenharmony_ci  hnd_put(r, session, request, query, response);
564c87c5fbaSopenharmony_ci
565c87c5fbaSopenharmony_ci  return;
566c87c5fbaSopenharmony_ci
567c87c5fbaSopenharmony_ci}
568c87c5fbaSopenharmony_ci
569c87c5fbaSopenharmony_ci/* Initialize single Unknown Resource PUT handler */
570c87c5fbaSopenharmony_ci
571c87c5fbaSopenharmony_cistatic void
572c87c5fbaSopenharmony_ciinit_resources(coap_context_t *ctx) {
573c87c5fbaSopenharmony_ci
574c87c5fbaSopenharmony_ci  coap_resource_t *r;
575c87c5fbaSopenharmony_ci
576c87c5fbaSopenharmony_ci  /* Create a resource to handle PUTs to unknown URIs */
577c87c5fbaSopenharmony_ci  r = coap_resource_unknown_init2(hnd_put_unknown, 0);
578c87c5fbaSopenharmony_ci  /*
579c87c5fbaSopenharmony_ci   * Additional handlers can be added - for example
580c87c5fbaSopenharmony_ci   *  coap_register_request_handler(r, COAP_REQUEST_POST, hnd_post_unknown);
581c87c5fbaSopenharmony_ci   *  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_unknown);
582c87c5fbaSopenharmony_ci   *  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_unknown);
583c87c5fbaSopenharmony_ci   */
584c87c5fbaSopenharmony_ci  coap_add_resource(ctx, r);
585c87c5fbaSopenharmony_ci
586c87c5fbaSopenharmony_ci}
587c87c5fbaSopenharmony_ci----
588c87c5fbaSopenharmony_ci
589c87c5fbaSopenharmony_ciSEE ALSO
590c87c5fbaSopenharmony_ci--------
591c87c5fbaSopenharmony_ci*coap_attribute*(3), *coap_context*(3), *coap_handler*(3) and *coap_observe*(3)
592c87c5fbaSopenharmony_ci
593c87c5fbaSopenharmony_ciFURTHER INFORMATION
594c87c5fbaSopenharmony_ci-------------------
595c87c5fbaSopenharmony_ciSee
596c87c5fbaSopenharmony_ci
597c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]"
598c87c5fbaSopenharmony_ci
599c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc3986[RFC3986: Uniform Resource Identifier (URI): Generic Syntax]"
600c87c5fbaSopenharmony_ci
601c87c5fbaSopenharmony_cifor further information.
602c87c5fbaSopenharmony_ci
603c87c5fbaSopenharmony_ciBUGS
604c87c5fbaSopenharmony_ci----
605c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap:
606c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at
607c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues
608c87c5fbaSopenharmony_ci
609c87c5fbaSopenharmony_ciAUTHORS
610c87c5fbaSopenharmony_ci-------
611c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net>
612