1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_attribute(3) 5c87c5fbaSopenharmony_ci================= 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_attribute 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_attribute, 14c87c5fbaSopenharmony_cicoap_add_attr, 15c87c5fbaSopenharmony_cicoap_find_attr, 16c87c5fbaSopenharmony_cicoap_attr_get_value 17c87c5fbaSopenharmony_ci- Work with CoAP attributes 18c87c5fbaSopenharmony_ci 19c87c5fbaSopenharmony_ciSYNOPSIS 20c87c5fbaSopenharmony_ci-------- 21c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 22c87c5fbaSopenharmony_ci 23c87c5fbaSopenharmony_ci*coap_attr_t *coap_add_attr(coap_resource_t *_resource_, 24c87c5fbaSopenharmony_cicoap_str_const_t *_name_, coap_str_const_t *_value_, int _flags_);* 25c87c5fbaSopenharmony_ci 26c87c5fbaSopenharmony_ci*coap_attr_t *coap_find_attr(coap_resource_t *_resource_, 27c87c5fbaSopenharmony_cicoap_str_const_t *_name_);* 28c87c5fbaSopenharmony_ci 29c87c5fbaSopenharmony_ci*coap_str_const_t *coap_attr_get_value(coap_attr_t *_attribute_);* 30c87c5fbaSopenharmony_ci 31c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 32c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 33c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 34c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 35c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ciDESCRIPTION 38c87c5fbaSopenharmony_ci----------- 39c87c5fbaSopenharmony_ciCoAP Resources on a CoAP Server need to be created, updated etc. The URI in 40c87c5fbaSopenharmony_cithe request packet defines the resource to work with, with possibly the Query 41c87c5fbaSopenharmony_cireferring to a sub-resource. 42c87c5fbaSopenharmony_ci 43c87c5fbaSopenharmony_ciWhen resources are configured on the CoAP server, the URI to match against 44c87c5fbaSopenharmony_ciis specified. 45c87c5fbaSopenharmony_ciCallback Handlers are then added to the resource to handle the different 46c87c5fbaSopenharmony_cirequest methods. 47c87c5fbaSopenharmony_ci 48c87c5fbaSopenharmony_ciAdding Attributes allows textual information to be added to the resource 49c87c5fbaSopenharmony_ciwhich can then be reported back to any client doing a Resource Discovery using 50c87c5fbaSopenharmony_cia "GET /.well-known/core" request with an optional query. 51c87c5fbaSopenharmony_ciSee "https://rfc-editor.org/rfc/rfc6690#section-5[RFC6690 5. Examples]" for 52c87c5fbaSopenharmony_cisome examples of 53c87c5fbaSopenharmony_ciresource discovery usage. Common attribute names are rt, if, sz, ct, obs, rel, 54c87c5fbaSopenharmony_cianchor, rev, hreflang, media, title and type. href cannot be an attribute name. 55c87c5fbaSopenharmony_ci 56c87c5fbaSopenharmony_ciAttributes are automatically deleted when a Resource is deleted. 57c87c5fbaSopenharmony_ci 58c87c5fbaSopenharmony_ciFUNCTIONS 59c87c5fbaSopenharmony_ci--------- 60c87c5fbaSopenharmony_ci 61c87c5fbaSopenharmony_ci*Function: coap_add_attr()* 62c87c5fbaSopenharmony_ci 63c87c5fbaSopenharmony_ciThe *coap_add_attr*() function 64c87c5fbaSopenharmony_ciregisters a new attribute called _name_ for the _resource_. 65c87c5fbaSopenharmony_ciThe value of the attribute is _value_ which can be NULL. 66c87c5fbaSopenharmony_ci 67c87c5fbaSopenharmony_ci_flags_ can be zero or more of the following or'd together, which, if set, 68c87c5fbaSopenharmony_cidefines what is to be internally freed off when the attribute is deleted with 69c87c5fbaSopenharmony_ci*coap_delete_resource*(). 70c87c5fbaSopenharmony_ci 71c87c5fbaSopenharmony_ci[horizontal] 72c87c5fbaSopenharmony_ci*COAP_ATTR_FLAGS_RELEASE_NAME*:: 73c87c5fbaSopenharmony_ciFree off _name_ when attribute is deleted with *coap_delete_resource*(). 74c87c5fbaSopenharmony_ci 75c87c5fbaSopenharmony_ci*COAP_ATTR_FLAGS_RELEASE_VALUE*:: 76c87c5fbaSopenharmony_ciFree off _value_ when attribute is deleted with *coap_delete_resource*(). 77c87c5fbaSopenharmony_ci 78c87c5fbaSopenharmony_ci*Function: coap_find_attr()* 79c87c5fbaSopenharmony_ci 80c87c5fbaSopenharmony_ciThe *coap_find_attr*() function returns the attribute with the _name_, 81c87c5fbaSopenharmony_ciif found, associated with _resource_. 82c87c5fbaSopenharmony_ci 83c87c5fbaSopenharmony_ci*Function: coap_attr_get_value()* 84c87c5fbaSopenharmony_ci 85c87c5fbaSopenharmony_ciThe *coap_attr_get_value*() function returns the _value_ associated with the 86c87c5fbaSopenharmony_cispecified _attribute_. 87c87c5fbaSopenharmony_ci 88c87c5fbaSopenharmony_ciRETURN VALUES 89c87c5fbaSopenharmony_ci------------- 90c87c5fbaSopenharmony_ci*coap_add_attr*() returns a pointer to 91c87c5fbaSopenharmony_cithe attribute that was created or NULL if there is a malloc failure. 92c87c5fbaSopenharmony_ci 93c87c5fbaSopenharmony_ci*coap_find_attr*() returns a pointer to the first matching 94c87c5fbaSopenharmony_ciattribute or NULL if the _name_ was not found. 95c87c5fbaSopenharmony_ci 96c87c5fbaSopenharmony_ci*coap_attr_get_value*() returns a pointer to the value held within 97c87c5fbaSopenharmony_cithe attribute. The pointer can be NULL if the _value_ id NULL, or NULL if 98c87c5fbaSopenharmony_ci_attribute_ does not exist. 99c87c5fbaSopenharmony_ci 100c87c5fbaSopenharmony_ciEXAMPLE 101c87c5fbaSopenharmony_ci------- 102c87c5fbaSopenharmony_ci*Initialize Resources* 103c87c5fbaSopenharmony_ci 104c87c5fbaSopenharmony_ci[source, c] 105c87c5fbaSopenharmony_ci---- 106c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 107c87c5fbaSopenharmony_ci 108c87c5fbaSopenharmony_civoid hnd_get_index(coap_resource_t *resource, coap_session_t *session, 109c87c5fbaSopenharmony_ci const coap_pdu_t *request, 110c87c5fbaSopenharmony_ci const coap_string_t *query, 111c87c5fbaSopenharmony_ci coap_pdu_t *response); 112c87c5fbaSopenharmony_ci 113c87c5fbaSopenharmony_civoid hnd_get_time(coap_resource_t *resource, coap_session_t *session, 114c87c5fbaSopenharmony_ci const coap_pdu_t *request, 115c87c5fbaSopenharmony_ci const coap_string_t *query, 116c87c5fbaSopenharmony_ci coap_pdu_t *response); 117c87c5fbaSopenharmony_ci 118c87c5fbaSopenharmony_civoid hnd_put_time(coap_resource_t *resource, coap_session_t *session, 119c87c5fbaSopenharmony_ci const coap_pdu_t *request, 120c87c5fbaSopenharmony_ci const coap_string_t *query, 121c87c5fbaSopenharmony_ci coap_pdu_t *response); 122c87c5fbaSopenharmony_ci 123c87c5fbaSopenharmony_civoid hnd_delete_time(coap_resource_t *resource, coap_session_t *session, 124c87c5fbaSopenharmony_ci const coap_pdu_t *request, 125c87c5fbaSopenharmony_ci const coap_string_t *query, 126c87c5fbaSopenharmony_ci coap_pdu_t *response); 127c87c5fbaSopenharmony_ci 128c87c5fbaSopenharmony_cistatic void 129c87c5fbaSopenharmony_ciinit_resources(coap_context_t *ctx) { 130c87c5fbaSopenharmony_ci 131c87c5fbaSopenharmony_ci coap_resource_t *r; 132c87c5fbaSopenharmony_ci 133c87c5fbaSopenharmony_ci /* Create a resource to return general information */ 134c87c5fbaSopenharmony_ci r = coap_resource_init(NULL, 0); 135c87c5fbaSopenharmony_ci coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index); 136c87c5fbaSopenharmony_ci 137c87c5fbaSopenharmony_ci /* Document resource for '.well-known/core' request */ 138c87c5fbaSopenharmony_ci coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); 139c87c5fbaSopenharmony_ci coap_add_attr(r, coap_make_str_const("title"), 140c87c5fbaSopenharmony_ci coap_make_str_const("\"General Info\""), 0); 141c87c5fbaSopenharmony_ci 142c87c5fbaSopenharmony_ci coap_add_resource(ctx, r); 143c87c5fbaSopenharmony_ci 144c87c5fbaSopenharmony_ci /* Create a resource to return return or update time */ 145c87c5fbaSopenharmony_ci r = coap_resource_init(coap_make_str_const("time"), 146c87c5fbaSopenharmony_ci COAP_RESOURCE_FLAGS_NOTIFY_CON); 147c87c5fbaSopenharmony_ci coap_resource_set_get_observable(r, 1); 148c87c5fbaSopenharmony_ci coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_time); 149c87c5fbaSopenharmony_ci coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_time); 150c87c5fbaSopenharmony_ci coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_time); 151c87c5fbaSopenharmony_ci 152c87c5fbaSopenharmony_ci /* Document resource for 'time' request */ 153c87c5fbaSopenharmony_ci coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); 154c87c5fbaSopenharmony_ci coap_add_attr(r, coap_make_str_const("title"), 155c87c5fbaSopenharmony_ci coap_make_str_const("\"Internal Clock\""), 0); 156c87c5fbaSopenharmony_ci coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"secs\""), 157c87c5fbaSopenharmony_ci 0); 158c87c5fbaSopenharmony_ci coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""), 159c87c5fbaSopenharmony_ci 0); 160c87c5fbaSopenharmony_ci 161c87c5fbaSopenharmony_ci coap_add_resource(ctx, r); 162c87c5fbaSopenharmony_ci 163c87c5fbaSopenharmony_ci} 164c87c5fbaSopenharmony_ci---- 165c87c5fbaSopenharmony_ci 166c87c5fbaSopenharmony_ciSEE ALSO 167c87c5fbaSopenharmony_ci-------- 168c87c5fbaSopenharmony_ci*coap_resource*(3) and *coap_handler*(3) 169c87c5fbaSopenharmony_ci 170c87c5fbaSopenharmony_ciFURTHER INFORMATION 171c87c5fbaSopenharmony_ci------------------- 172c87c5fbaSopenharmony_ciSee 173c87c5fbaSopenharmony_ci 174c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 175c87c5fbaSopenharmony_ci 176c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc6690[RFC6690: Constrained RESTful Environments (CoRE) Link Format]" 177c87c5fbaSopenharmony_ci 178c87c5fbaSopenharmony_cifor further information. 179c87c5fbaSopenharmony_ci 180c87c5fbaSopenharmony_ciBUGS 181c87c5fbaSopenharmony_ci---- 182c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 183c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 184c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 185c87c5fbaSopenharmony_ci 186c87c5fbaSopenharmony_ciAUTHORS 187c87c5fbaSopenharmony_ci------- 188c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 189