1COAP_ATTRIBUTE(3) libcoap Manual COAP_ATTRIBUTE(3)
2
3
4
6 coap_attribute, coap_add_attr, coap_find_attr - Work with CoAP
7 attributes
8
10 #include <coap2/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 Link with -lcoap-2, -lcoap-2-gnutls, -lcoap-2-openssl or
19 -lcoap-2-tinydtls depending on your (D)TLS library type.
20
22 CoAP Resources on a CoAP Server need to be created, updated etc. The
23 URI in the request packet defines the resource to work with, with
24 possibly the Query referring to a sub-resource. When resources are
25 configured on the CoAP server, the URI to match against is specified.
26 Callback Handlers are then added to the resource to handle the
27 different request methods. Adding Attributes allows textual information
28 to be added to the resource which can then be reported back to any
29 client doing a "GET .well-known/core" request.
30
31 Attributes are automatically freed when a Resource is deleted.
32
33 The coap_add_attr() function registers a new attribute called name for
34 the resource. The value of the attribute is value.
35
36 flags can be one or more of the following, which, if set, defines what
37 is to be internally freed off when the attribute is deleted with
38 coap_delete_resource().
39
40
41 COAP_ATTR_FLAGS_RELEASE_NAME Free off name when
42 attribute is deleted with
43 coap_delete_resource().
44
45 COAP_ATTR_FLAGS_RELEASE_VALUE Free off value when
46 attribute is deleted with
47 coap_delete_resource().
48
49
50 The coap_find_attr() function returns the attribute with the name, if
51 found, associated with resource.
52
54 coap_add_attr() function return a pointer to the attribute that was
55 created or NULL if there is a malloc failure.
56
57 coap_find_attr() function returns a pointer to the first matching
58 attribute or NULL if the name was not found.
59
61 Initialize Resources
62
63 #include <coap2/coap.h>
64
65 static void
66 init_resources(coap_context_t *ctx) {
67
68 coap_resource_t *r;
69
70 /* Create a resource to return general information */
71 r = coap_resource_init(NULL, 0);
72 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index);
73
74 /* Document resource for .well-known/core request */
75 coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
76 coap_add_attr(r, coap_make_str_const("title"),
77 coap_make_str_const("\"General Info\""), 0);
78
79 coap_add_resource(ctx, r);
80
81 /* Create a resource to return return or update time */
82 r = coap_resource_init(coap_make_str_const("time"),
83 COAP_RESOURCE_FLAGS_NOTIFY_CON);
84 coap_resource_set_get_observable(r, 1);
85 coap_register_handler(r, COAP_REQUEST_GET, hnd_get_time);
86 coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_time);
87 coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
88
89 /* Document resource for .well-known/core request */
90 coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
91 coap_add_attr(r, coap_make_str_const("title"),
92 coap_make_str_const("\"Internal Clock\""), 0);
93 coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"secs\""),
94 0);
95 coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""),
96 0);
97
98 coap_add_resource(ctx, r);
99
100 }
101
103 coap_resource(3) and coap_handler(3)
104
106 See RFC7252 ‘The Constrained Application Protocol (CoAP)’ for further
107 information.
108
110 Please report bugs on the mailing list for libcoap:
111 libcoap-developers@lists.sourceforge.net
112
114 The libcoap project <libcoap-developers@lists.sourceforge.net>
115
116
117
118coap_attribute 4.2.1 07/28/2020 COAP_ATTRIBUTE(3)