COAP_ATTRIBUTE(3)

πŸ’… COAP_ATTRIBUTE(3) β€” like, the libcoap manual, but make it make sense πŸŽ€

okay bestie, sit down, hydrate, and let's talk about CoAP attributes ✨

This page covers coap_attribute, coap_add_attr, coap_find_attr, and coap_attr_get_value β€” literally the whole "work with CoAP attributes" friend group 🫢


🌸 NAME 🌸

coap_attribute, coap_add_attr, coap_find_attr, coap_attr_get_value β€” work with CoAP attributes, periodt πŸ’…


⭐ SYNOPSIS ⭐

#include <coap3/coap.h>

coap_attr_t *coap_add_attr(coap_resource_t *resource, coap_str_const_t
*name, coap_str_const_t *value, int flags);

coap_attr_t *coap_find_attr(coap_resource_t *resource, coap_str_const_t
*name);

coap_str_const_t *coap_attr_get_value(coap_attr_t *attribute);

And for your specific (D)TLS library support, you link against one of these besties:

  • -lcoap-3-notls
  • -lcoap-3-gnutls
  • -lcoap-3-openssl
  • -lcoap-3-mbedtls
  • -lcoap-3-wolfssl
  • -lcoap-3-tinydtls

Otherwise (no cap), just link with -lcoap-3 and you get the default (D)TLS library support 🧚


πŸ¦‹ DESCRIPTION πŸ¦‹

So. CoAP Resources on a CoAP Server need to be created, updated, etc. β€” the whole lifecycle, iykyk. The URI in the request packet is what defines which resource you're working with, and the Query might be pointing at a sub-resource πŸ‘€

When resources get configured on the CoAP server, you specify the URI to match against. Then Callback Handlers get added to the resource to handle the different request methods. Easy πŸ’–

Now here's the fun part: adding attributes lets you slap textual information onto the resource, which can then be reported back to any client doing a Resource Discovery with a "GET /.well-known/core" request (with an optional query, obviously). See "RFC6690 5. Examples" for some examples of resource discovery usage 🌈

Common attribute names are: rt, if, sz, ct, obs, rel, anchor, rev, hreflang, media, title and type.

🚨 Important bestie note: href cannot be an attribute name. That's the rule, don't fight it πŸ’€

And the glow-up: Attributes are automatically deleted when a Resource is deleted. Cleanup? Handled. Slay ✨


πŸ‘‘ FUNCTIONS πŸ‘‘

πŸ’œ Function: coap_add_attr()

The coap_add_attr() function registers a new attribute called name for the resource. The value of the attribute is value, which can be NULL 🫢

flags can be zero or more of the following, or'd together, which β€” if set β€” defines what is to be internally freed off when the attribute is deleted with coap_delete_resource():

  • COAP_ATTR_FLAGS_RELEASE_NAME β€” free off name when the attribute is deleted with coap_delete_resource()
  • COAP_ATTR_FLAGS_RELEASE_VALUE β€” free off value when the attribute is deleted with coap_delete_resource()

πŸŽ€ Function: coap_find_attr()

The coap_find_attr() function returns the attribute with the name, if found, associated with resource. Detective work, but make it C πŸ”

🌸 Function: coap_attr_get_value()

The coap_attr_get_value() function returns the value associated with the specified attribute. That's it, that's the function πŸ’…


🌟 RETURN VALUES 🌟

  • coap_add_attr() returns a pointer to the attribute that was created, or NULL if there is a malloc failure 😭 (rip)
  • coap_find_attr() returns a pointer to the first matching attribute, or NULL if the name was not found πŸ™ˆ
  • coap_attr_get_value() returns a pointer to the value held within the attribute. The pointer can be NULL if the value is NULL, or NULL if attribute does not exist. Yeah, two different NULLs, keep up bestie πŸ’€

✨ EXAMPLE ✨

Initialize Resources β€” the main character code block 🎬

#include <coap3/coap.h>

void hnd_get_index(coap_resource_t *resource, coap_session_t *session,
                   const coap_pdu_t *request,
                   const coap_string_t *query,
                   coap_pdu_t *response);

void hnd_get_time(coap_resource_t *resource, coap_session_t *session,
                  const coap_pdu_t *request,
                  const coap_string_t *query,
                  coap_pdu_t *response);

void hnd_put_time(coap_resource_t *resource, coap_session_t *session,
                  const coap_pdu_t *request,
                  const coap_string_t *query,
                  coap_pdu_t *response);

void hnd_delete_time(coap_resource_t *resource, coap_session_t *session,
                     const coap_pdu_t *request,
                     const coap_string_t *query,
                     coap_pdu_t *response);

static void
init_resources(coap_context_t *ctx) {

  coap_resource_t *r;

  /* Create a resource to return general information */
  r = coap_resource_init(NULL, 0);
  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index);

  /* Document resource for '.well-known/core' request */
  coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
  coap_add_attr(r, coap_make_str_const("title"),
                coap_make_str_const("\"General Info\""), 0);

  coap_add_resource(ctx, r);

  /* Create a resource to return return or update time */
  r = coap_resource_init(coap_make_str_const("time"),
                         COAP_RESOURCE_FLAGS_NOTIFY_CON);
  coap_resource_set_get_observable(r, 1);
  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_time);
  coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_time);
  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);

  /* Document resource for 'time' request */
  coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
  coap_add_attr(r, coap_make_str_const("title"),
                coap_make_str_const("\"Internal Clock\""), 0);
  coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"secs\""),
                0);
  coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""),
                0);

  coap_add_resource(ctx, r);

}

πŸ”₯ SEE ALSO πŸ”₯

Go read coap_resource(3) and coap_handler(3) next, bestie β€” they're literally the supporting cast 🫢


🌈 FURTHER INFORMATION 🌈

See:

  • "RFC7252: The Constrained Application Protocol (CoAP)"
  • "RFC6690: Constrained RESTful Environments (CoRE) Link Format"

for further information. Required reading, no skipping πŸ’–


πŸ’€ BUGS πŸ’€

Found something cursed? Please raise an issue on GitHub at https://github.com/obgm/libcoap/issues to report any bugs πŸ™ˆ

Got a fix? Please raise a Pull Request at https://github.com/obgm/libcoap/pulls for any fixes β€” be the change you want to see in the source tree πŸ‘‘


πŸ“ AUTHORS πŸ“

The libcoap project <libcoap-developers@lists.sourceforge.net> πŸ’œ


okay that's the whole page, you ate that πŸŽ€ this was coap_attribute 4.3.5, dated 08/24/2026 β€” from your fave libcoap manual, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from