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 withcoap_delete_resource()COAP_ATTR_FLAGS_RELEASE_VALUEβ free off value when the attribute is deleted withcoap_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, orNULLif there is amallocfailure π (rip)coap_find_attr()returns a pointer to the first matching attribute, orNULLif the name was not found πcoap_attr_get_value()returns a pointer to the value held within the attribute. The pointer can beNULLif the value isNULL, orNULLif attribute does not exist. Yeah, two differentNULLs, 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
- ubuntu stonking: libcoap3t64 4.3.5-4ubuntu1 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms