1COAP_ATTRIBUTE(3)               libcoap Manual               COAP_ATTRIBUTE(3)
2
3
4

NAME

6       coap_attribute, coap_add_attr, coap_find_attr, coap_attr_get_value -
7       Work with CoAP attributes
8

SYNOPSIS

10       #include <coap3/coap.h>
11
12       coap_attr_t *coap_add_attr(coap_resource_t *resource, coap_str_const_t
13       *name, coap_str_const_t *value, int flags);
14
15       coap_attr_t *coap_find_attr(coap_resource_t *resource, coap_str_const_t
16       *name);
17
18       coap_str_const_t *coap_attr_get_value(coap_attr_t *attribute);
19
20       For specific (D)TLS library support, link with -lcoap-3-notls,
21       -lcoap-3-gnutls, -lcoap-3-openssl, -lcoap-3-mbedtls or
22       -lcoap-3-tinydtls. Otherwise, link with -lcoap-3 to get the default
23       (D)TLS library support.
24

DESCRIPTION

26       CoAP Resources on a CoAP Server need to be created, updated etc. The
27       URI in the request packet defines the resource to work with, with
28       possibly the Query referring to a sub-resource.
29
30       When resources are configured on the CoAP server, the URI to match
31       against is specified. Callback Handlers are then added to the resource
32       to handle the different request methods.
33
34       Adding Attributes allows textual information to be added to the
35       resource which can then be reported back to any client doing a Resource
36       Discovery using a "GET /.well-known/core" request with an optional
37       query. See "RFC6690 5. Examples" for some examples of resource
38       discovery usage. Common attribute names are rt, if, sz, ct, obs, rel,
39       anchor, rev, hreflang, media, title and type. href cannot be an
40       attribute name.
41
42       Attributes are automatically deleted when a Resource is deleted.
43

FUNCTIONS

45       Function: coap_add_attr()
46
47       The coap_add_attr() function registers a new attribute called name for
48       the resource. The value of the attribute is value which can be NULL.
49
50       flags can be zero or more of the following or’d together, which, if
51       set, defines what is to be internally freed off when the attribute is
52       deleted with coap_delete_resource().
53
54
55       COAP_ATTR_FLAGS_RELEASE_NAME    Free off name when
56                                       attribute is deleted with
57                                       coap_delete_resource().
58
59       COAP_ATTR_FLAGS_RELEASE_VALUE   Free off value when
60                                       attribute is deleted with
61                                       coap_delete_resource().
62
63
64       Function: coap_find_attr()
65
66       The coap_find_attr() function returns the attribute with the name, if
67       found, associated with resource.
68
69       Function: coap_attr_get_value()
70
71       The coap_attr_get_value() function returns the value associated with
72       the specified attribute.
73

RETURN VALUES

75       coap_add_attr() returns a pointer to the attribute that was created or
76       NULL if there is a malloc failure.
77
78       coap_find_attr() returns a pointer to the first matching attribute or
79       NULL if the name was not found.
80
81       coap_attr_get_value() returns a pointer to the value held within the
82       attribute. The pointer can be NULL if the value id NULL, or NULL if
83       attribute does not exist.
84

EXAMPLE

86       Initialize Resources
87
88           #include <coap3/coap.h>
89
90           void hnd_get_index(coap_resource_t *resource, coap_session_t *session,
91                              const coap_pdu_t *request,
92                              const coap_string_t *query,
93                              coap_pdu_t *response);
94
95           void hnd_get_time(coap_resource_t *resource, coap_session_t *session,
96                             const coap_pdu_t *request,
97                             const coap_string_t *query,
98                             coap_pdu_t *response);
99
100           void hnd_put_time(coap_resource_t *resource, coap_session_t *session,
101                             const coap_pdu_t *request,
102                             const coap_string_t *query,
103                             coap_pdu_t *response);
104
105           void hnd_delete_time(coap_resource_t *resource, coap_session_t *session,
106                                const coap_pdu_t *request,
107                                const coap_string_t *query,
108                                coap_pdu_t *response);
109
110           static void
111           init_resources(coap_context_t *ctx) {
112
113             coap_resource_t *r;
114
115             /* Create a resource to return general information */
116             r = coap_resource_init(NULL, 0);
117             coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index);
118
119             /* Document resource for '.well-known/core' request */
120             coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
121             coap_add_attr(r, coap_make_str_const("title"),
122                           coap_make_str_const("\"General Info\""), 0);
123
124             coap_add_resource(ctx, r);
125
126             /* Create a resource to return return or update time */
127             r = coap_resource_init(coap_make_str_const("time"),
128                                    COAP_RESOURCE_FLAGS_NOTIFY_CON);
129             coap_resource_set_get_observable(r, 1);
130             coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_time);
131             coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_time);
132             coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
133
134             /* Document resource for 'time' request */
135             coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
136             coap_add_attr(r, coap_make_str_const("title"),
137                           coap_make_str_const("\"Internal Clock\""), 0);
138             coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"secs\""),
139                           0);
140             coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""),
141                           0);
142
143             coap_add_resource(ctx, r);
144
145           }
146

SEE ALSO

148       coap_resource(3) and coap_handler(3)
149

FURTHER INFORMATION

151       See
152
153       "RFC7252: The Constrained Application Protocol (CoAP)"
154
155       "RFC6690: Constrained RESTful Environments (CoRE) Link Format"
156
157       for further information.
158

BUGS

160       Please report bugs on the mailing list for libcoap:
161       libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at
162       https://github.com/obgm/libcoap/issues
163

AUTHORS

165       The libcoap project <libcoap-developers@lists.sourceforge.net>
166
167
168
169coap_attribute 4.3.4              10/09/2023                 COAP_ATTRIBUTE(3)
Impressum