libcoap-sys 0.2.2+libcoap-4.3.1

Raw bindings to the libcoap CoAP library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// -*- mode:doc; -*-
// vim: set syntax=asciidoc,tw=0:

coap_resource(3)
=================
:doctype: manpage
:man source:   coap_resource
:man version:  @PACKAGE_VERSION@
:man manual:   libcoap Manual

NAME
----
coap_resource,
coap_resource_init,
coap_resource_unknown_init,
coap_resource_unknown_init2,
coap_resource_proxy_uri_init,
coap_resource_proxy_uri_init2,
coap_add_resource,
coap_delete_resource,
coap_resource_set_mode,
coap_resource_set_userdata,
coap_resource_get_userdata,
coap_resource_release_userdata_handler,
coap_resource_get_uri_path
- Work with CoAP resources

SYNOPSIS
--------
*#include <coap@LIBCOAP_API_VERSION@/coap.h>*

*coap_resource_t *coap_resource_init(coap_str_const_t *_uri_path_,
int _flags_);*

*coap_resource_t *coap_resource_unknown_init(coap_method_handler_t
_put_handler_);*

*coap_resource_t *coap_resource_unknown_init2(coap_method_handler_t
_put_handler_, int _flags_);*

*coap_resource_t *coap_resource_proxy_uri_init(coap_method_handler_t
_proxy_handler_, size_t _host_name_count_, const char *_host_name_list_[]);*

*coap_resource_t *coap_resource_proxy_uri_init2(coap_method_handler_t
_proxy_handler_, size_t _host_name_count_, const char *_host_name_list_[],
int flags);*

*void coap_add_resource(coap_context_t *_context_,
coap_resource_t *_resource_);*

*int coap_delete_resource(coap_context_t *_context_,
coap_resource_t *_resource_);*

*void coap_resource_set_mode(coap_resource_t *_resource_, int _mode_);*

*void coap_resource_set_userdata(coap_resource_t *_resource_, void *_data_);*

*void *coap_resource_get_userdata(coap_resource_t *_resource_);*

*void coap_resource_release_userdata_handler(coap_context_t *_context_,
coap_resource_release_userdata_handler_t _callback_);*

*coap_str_const_t *coap_resource_get_uri_path(coap_resource_t *_resource_);*

For specific (D)TLS library support, link with
*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*
or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*.   Otherwise, link with
*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support.

DESCRIPTION
-----------
CoAP Resources on a CoAP Server need to be created and updated etc. The URI in
the request packet defines the resource to work with, with possibly the Query
referring to a sub-resource.

When resources are configured on the CoAP server, the URI to match against
in the request packet is specified.

Callback Handlers are then added to the resource to handle the different
request methods. See *coap_register_request_handler*(3) for further information.

Adding Attributes allows textual information to be added to the resource
which can then be reported back to any client doing a "GET .well-known/core"
request. See *coap_add_attr*(3) for further information.

If an incoming packet request matches a resource's URI and Method, then
the appropriate callback resource handler is invoked to process the packet
which should then update a suitable response packet for returning back to the
requester.

There is support for handling incoming packets where the URI is unknown (no
specific resource has been created).
This could, for example, happen when a PUT request is trying to create a new
resource. It is the responsibility of the unknown resource callback handler
to either create a new resource for the URI or just manage things separately.

CoAP Observe (RFC 7641) is not supported for unknown resources, so a new
resource with GET handler must be created by the unknown resource callback
handle matching the URI which then can be Observable.

There is support for handling incoming proxy based requests using the Proxy-Uri
or Proxy-Scheme options.

FUNCTIONS
---------

*Function: coap_resource_init()*

The *coap_resource_init*() function returns a newly created _resource_ of
type _coap_resource_t_ * .  _uri_path_ specifies the uri string path to match
against.  Normally there is no need for the leading '/' - e.g. just
"full/path/for/resource".  _flags_ is used to define whether the
_resource_ is of type Confirmable Message or Non-Confirmable Message for
any "observe" responses.  See *coap_observe*(3).
_flags_ can be one of the following definitions ored together.

[horizontal]
*COAP_RESOURCE_FLAGS_NOTIFY_NON*::
Set the notification message type to non-confirmable for any trigggered
"observe" responses with type set to confirmable every 5 packets as required by
RFC7641 section-4.5.

*COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS*::
Set the notification message type to always non-confirmable for any trigggered
"observe" responses. This should only be used if a upper layer protocol
requires it.

*COAP_RESOURCE_FLAGS_NOTIFY_CON*::
Set the notification message type to confirmable for any trigggered
"observe" responses.

*COAP_RESOURCE_FLAGS_RELEASE_URI*::
Free off the coap_str_const_t for _uri_path_ when the _resource_ is deleted.

*NOTE:* The following flags are only tested against if
*coap_mcast_per_resource*() has been called.  If *coap_mcast_per_resource*()
has not been called, then all resources have multicast support, libcoap adds
in random delays to the responses, and 4.xx / 5.xx responses are dropped.

[horizontal]
*COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT*::
This resource has support for multicast requests.

*COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_DELAYS*::
Disable libcoap library from adding in delays to multicast requests before
releasing the response back to the client.  It is then the responsibility of
the app to delay the responses for multicast requests. See
https://datatracker.ietf.org/doc/html/rfc7252#section-8.2.  However, the
pseudo resource for ".well-known/core" always has multicast support enabled.

*COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_05*::
Enable libcoap library suppression of 205 multicast responses that are empty
(overridden by RFC7969 No-Response option) for multicast requests.

*COAP_RESOURCE_FLAGS_LIB_ENA_MCAST_SUPPRESS_2_XX*::
Enable libcoap library suppressing 2.xx multicast responses (overridden by
RFC7969 No-Response option) for multicast requests.

*COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_4_XX*::
Disable libcoap library suppressing 4.xx multicast responses (overridden by
RFC7969 No-Response option) for multicast requests.

*COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_SUPPRESS_5_XX*::
Disable libcoap library suppressing 5.xx multicast responses (overridden by
RFC7969 No-Response option) for multicast requests.

*NOTE:* _uri_path_, if not 7 bit readable ASCII, binary bytes must be hex
encoded according to the rules defined in RFC3968 Section 2.1.

*Function: coap_resource_unknown_init()*

The *coap_resource_unknown_init*() function returns a newly created _resource_
of type _coap_resource_t_ *. _put_handler_ is automatically added to the
_resource_ to handle PUT requests to resources that are unknown. Additional
handlers can be added to this resource if required.

*Function: coap_resource_unknown_init2()*

The *coap_resource_unknown_init2*() function returns a newly created _resource_
of type _coap_resource_t_ *. _put_handler_ is automatically added to the
_resource_ to handle PUT requests to resources that are unknown. Additional
handlers can be added to this resource if required. _flags_ can be zero or
more COAP_RESOURCE_FLAGS MCAST definitions.

*Function: coap_resource_proxy_uri_init()*

The *coap_resource_proxy_uri_init*() function returns a newly created
_resource_ of type _coap_resource_t_ *. _proxy_handler_ is automatically added
to the _resource_ to handle PUT/POST/GET etc. requests that use the Proxy-Uri
option.  There is no need to add explicit request type handlers. One or more
names by which the proxy is known by (IP address, DNS name etc.) must be
supplied in the array defined by _host_name_list_[] which has a count of
_host_name_count_.  This is used to check whether the current endpoint is
the proxy target address, or the request has to be passed on to an upstream
server.

*Function: coap_resource_proxy_uri_init2()*

The *coap_resource_proxy_uri_init2*() function returns a newly created
_resource_ of type _coap_resource_t_ *. _proxy_handler_ is automatically added
to the _resource_ to handle PUT/POST/GET etc. requests that use the Proxy-Uri
option.  There is no need to add explicit request type handlers. One or more
names by which the proxy is known by (IP address, DNS name etc.) must be
supplied in the array defined by _host_name_list_[] which has a count of
_host_name_count_.  This is used to check whether the current endpoint is
the proxy target address, or the request has to be passed on to an upstream
server. _flags_ can be zero or more COAP_RESOURCE_FLAGS MCAST definitions.

*Function: coap_add_resource()*

The *coap_add_resource*() function registers the given _resource_ with the
_context_. The _resource_ must have been created by *coap_resource_init*(),
*coap_resource_unknown_init*(), *coap_resource_unknown_init2*(),
*coap_resource_proxy_uri_init*() or *coap_resource_proxy_uri_init2*(). The
storage allocated for the _resource_ will be released by
*coap_delete_resource*().

As the _uri_path_ of the resource has to be unique across all of the resources
associated with a _context_, *coap_add_resource*() will delete any previous
_resource_ with the same _uri_path_ before adding in the new _resource_.

*Function: coap_delete_resource()*

The *coap_delete_resource*() function deletes a resource identified by
_resource_. The _context_ parameter is ignored. The storage allocated for that
_resource_ is freed, along with any attributes associated with the _resource_.

*Function: coap_resource_set_mode()*

The *coap_resource_set_mode*() changes the notification message type of
_resource_ to the given _mode_ which must be one of
COAP_RESOURCE_FLAGS_NOTIFY_NON, COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS or
COAP_RESOURCE_FLAGS_NOTIFY_CON.

*Function: coap_resource_set_userdata()*

The *coap_resource_set_userdata*() function allows a pointer to user _data_
to be associated with a _resource_ that can accessed in any callback that
includes _resource_ as a parameter.

*NOTE:* _data_ must point to a static, or allocated, block of memory.

*Function: coap_resource_get_userdata()*

The *coap_resource_get_userdata*() function obtains the user data pointer
from the _resource_ that had previously been set up by
*coap_resource_set_userdata*().

*Function: coap_resource_release_userdata_handler()*

The *coap_resource_release_userdata_handler*() function defines the _context_
wide _callback_ handler to call to release the allocated user data that has
been added to the resource using *coap_resource_set_userdata*() when the
resource is deleted. _callback_ can be NULL (which is the default) if nothing
needs to be freed off.

*Function: coap_resource_get_uri_path()*

The *coap_resource_get_uri_path*() function is used to obtain the UriPath of
the _resource_ definion.

RETURN VALUES
-------------
The *coap_resource_init*(), *coap_resource_unknown_init*(),
*coap_resource_unknown_init2*(), *coap_resource_proxy_uri_init*() and
*coap_resource_proxy_uri_init2*() functions return a newly created resource
or NULL if there is a malloc failure.

The *coap_delete_resource*() function return 0 on failure (_resource_ not
found), 1 on success.

The *coap_resource_get_userdata*() function returns the value previously set
by the *coap_resource_set_userdata*() function or NULL.

The *coap_resource_get_uri_path*() function returns the uri_path or NULL if
there was a failure.

EXAMPLES
--------
*Fixed Resources Set Up*

[source, c]
----
#include <coap@LIBCOAP_API_VERSION@/coap.h>

#define INDEX "This is an example server using libcoap\n"

static 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) {
  unsigned char buf[3];
  /* Remove (void) definition if variable is used */
  (void)resource;
  (void)session;
  (void)request;
  (void)query;

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);

  coap_add_option(response,
                  COAP_OPTION_CONTENT_TYPE,
                  coap_encode_var_safe(buf, sizeof(buf),
                                       COAP_MEDIATYPE_TEXT_PLAIN),
                  buf);

  coap_add_option(response,
                  COAP_OPTION_MAXAGE,
                  coap_encode_var_safe(buf, sizeof(buf), 0x2ffff), buf);

  coap_add_data(response, strlen(INDEX), (const uint8_t *)INDEX);

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
}

static 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) {
  /* Remove (void) definition if variable is used */
  (void)resource;
  (void)session;
  (void)request;
  (void)query;

  /* .. code .. */

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}

static 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) {
  /* Remove (void) definition if variable is used */
  (void)resource;
  (void)session;
  (void)request;
  (void)query;
  (void)response;

  /* .. code .. */

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
}

static 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) {
  /* Remove (void) definition if variable is used */
  (void)resource;
  (void)session;
  (void)request;
  (void)query;
  (void)response;

  /* .. code .. */

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
}

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);

}
----

*Dynamic Resources Set Up*

[source, c]
----
#include <coap@LIBCOAP_API_VERSION@/coap.h>

/* Regular DELETE handler - used by resources created by the
 * Unknown Resource PUT handler */

static void
hnd_delete(coap_resource_t *resource, coap_session_t *session,
const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
  /* Remove (void) definition if variable is used */
  (void)session;
  (void)request;
  (void)query;
  (void)response;

  /* .. code .. */

  /* Dynamic resource no longer required - delete it */
  coap_delete_resource(NULL, resource);

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_DELETED);
}

/* Regular GET handler - used by resources created by the
 * Unknown Resource PUT handler */

static void
hnd_get(coap_resource_t *resource, coap_session_t *session,
const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {

  coap_str_const_t *get_uri_path;

  /* Remove (void) definition if variable is used */
  (void)resource;
  (void)session;
  (void)request;
  (void)query;

  /*
   * Get the specific resouce being requested to determine what the response is
   * The uri_path string is a const pointer
   */

  get_uri_path = coap_resource_get_uri_path(resource);

  /* .. code .. */

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
}

/* Regular PUT handler - used by resources created by the
 * Unknown Resource PUT handler */

static void
hnd_put(coap_resource_t *resource, coap_session_t *session,
const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
  /* Remove (void) definition if variable is used */
  (void)resource;
  (void)session;
  (void)query;

  coap_string_t *put_uri_path;
  size_t length;
  const uint8_t *data;
  size_t offset;
  size_t total;
  int new_resource = 0;

  /* get the uri_path */
  put_uri_path = coap_get_uri_path(request);
  if (!put_uri_path) {
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
    return;
  }
  coap_get_data_large(request, &length, &data, &offset, &total);

  /* .. code .. */

  /* Need to do this as coap_get_uri_path() created it */
  coap_delete_string(put_uri_path);

  if (length + offset < total)
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
  else if (new_resource)
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
  else
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
}

static int
check_url_fn(coap_string_t *uri_path, uint8_t code) {
  /* Remove (void) definition if variable is used */
  (void)uri_path;
  (void)code;

  /* Code to determine whether the uri is valid or not */

  return 1;
}

/* Unknown Resource PUT handler */

static void
hnd_put_unknown(coap_resource_t *resource, coap_session_t *session,
const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
  /* Remove (void) definition if variable is used */
  (void)resource;
  coap_pdu_code_t req_code = coap_pdu_get_code(request);

  coap_resource_t *r;
  coap_string_t *uri_path;

  /* get the uri_path - which will get used by coap_resource_init() */
  uri_path = coap_get_uri_path(request);
  if (!uri_path) {
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
    return;
  }

  /* Check if new URI Path is valid */
  if (!check_url_fn (uri_path, req_code)) {
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
    coap_delete_string(uri_path);
    return;
  }

  /*
   * Create a resource to handle the new URI
   * uri_path will get deleted when the resource is removed
   */
  r = coap_resource_init((coap_str_const_t*)uri_path,
        COAP_RESOURCE_FLAGS_RELEASE_URI | COAP_RESOURCE_FLAGS_NOTIFY_NON);
  coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put);
  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete);
  /* We possibly want to Observe the GETs */
  coap_resource_set_get_observable(r, 1);
  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get);
  coap_add_resource(coap_session_get_context(session), r);

  /* Do the PUT for this first call */
  hnd_put(r, session, request, query, response);

  return;

}

/* Initialize single Unknown Resource PUT handler */

static void
init_resources(coap_context_t *ctx) {

  coap_resource_t *r;

  /* Create a resource to handle PUTs to unknown URIs */
  r = coap_resource_unknown_init2(hnd_put_unknown, 0);
  /*
   * Additional handlers can be added - for example
   *  coap_register_request_handler(r, COAP_REQUEST_POST, hnd_post_unknown);
   *  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_unknown);
   *  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_unknown);
   */
  coap_add_resource(ctx, r);

}
----

SEE ALSO
--------
*coap_attribute*(3), *coap_context*(3), *coap_handler*(3) and *coap_observe*(3)

FURTHER INFORMATION
-------------------
See "RFC7252: The Constrained Application Protocol (CoAP)" for further
information.

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

AUTHORS
-------
The libcoap project <libcoap-developers@lists.sourceforge.net>