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
= Upgrade from 4.2.1 to 4.3.0

== Summary

When compiling 4.2.1 based code with a 4.3.0 environment, this will initially
throw up many errors as the API has been updated to make future coding simpler,
adds more functionality and adds more rigorous coding checks.  Updating your
code with the following steps will significantly reduce the reported issues.

The examples are now also named with the (D)TLS library type as a suffix.
E.g. coap-client is now coap-client-openssl.

== Include directory changes

Because of the API changes, the libcoap's include file directory has changed from `coap2/` to `coap3/`. Also, there is now no need to define additional include paths to the compiler options such as `-I include/coap3`.

=== Update coap2 to coap3

Example
----
4.2.1
  #include <coap2/coap.h>
4.3.0
  #include <coap3/coap.h>
----
No other libcoap include files need to be included in your application.

== Call-back handler updates

Infrequently used parameters (which can easily be recreated) have been removed
and others have been made const.  These call-back handlers are those
registered with the `coap_register_*()` functions as follows:

=== coap_register_handler()

The definition of `coap_method_handler_t` has been updated, so all the
functions registered by `coap_register_handler()` need to be updated. Any
application functions called by these functions may need to include `const` in
their calling parameters.

Example
----
4.2.1
static void
  hnd_get_time(coap_context_t *context,
               coap_resource_t *resource,
               coap_session_t *session,
               coap_pdu_t *request,
               coap_binary_t *token,
               coap_string_t *query,
               coap_pdu_t *response) {
4.3.0
  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) {
----
If `context` or `token` need to be recreated, this is done by
----
  coap_context_t *context = coap_session_get_context(session);
  coap_bin_const_t rcvd_token = coap_pdu_get_token(request);
----

=== coap_register_response_handler()

The definition of `coap_response_handler_t` has been updated, so all the
functions registered by `coap_register_response_handler()` need to be updated.
Any application functions called by these functions may need to include `const`
in their calling parameters.  There is a new handler function exit code
`COAP_RESPONSE_FAIL` (if the response is not liked and needs to be rejected
with a `RST` packet) or `COAP_RESPONSE_OK`. Note that `coap_tid_t` has been
replaced with `coap_mid_t` to reflect the parameter is the message id.

Example
----
4.2.1
  static void
  message_handler(struct coap_context_t *context,
                  coap_session_t *session,
                  coap_pdu_t *sent,
                  coap_pdu_t *received,
                  const coap_tid_t id) {
4.3.0
  static coap_response_t
  message_handler(coap_session_t *session,
                  const coap_pdu_t *sent,
                  const coap_pdu_t *received,
                  const coap_mid_t mid) {
----
If `context` needs to be recreated, this is done by
----
  coap_context_t *context = coap_session_get_context(session);
----

=== coap_register_nack_handler()

The definition of `coap_nack_handler_t` has been updated, so all the functions
registered by `coap_register_nack_handler()` need to be updated. Any
application functions called by these functions may need to include `const` in
their calling parameters. Note that `coap_tid_t` has been replaced with
`coap_mid_t` to reflect the parameter is the message id.

Example
----
4.2.1
  static void
  nack_handler(coap_context_t *context,
               coap_session_t *session,
               coap_pdu_t *sent,
               coap_nack_reason_t reason,
               const coap_tid_t id) {
4.3.0
  static void
  nack_handler(coap_session_t *session,
               const coap_pdu_t *sent,
               const coap_nack_reason_t reason,
               const coap_mid_t mid) {
----
If `context` needs to be recreated, this is done by
----
  coap_context_t *context = coap_session_get_context(session);
----

=== coap_register_event_handler()

The definition of `coap_event_handler_t` been updated, so all the functions
registered by `coap_register_event_handler()` need to be updated. Any
application functions called by these functions may need to include `const` in
their calling parameters.

Example
----
4.2.1
  static int
  event_handler(coap_context_t *context,
                coap_event_t event,
                struct coap_session_t *session) {
4.3.0
  static int
  event_handler(coap_session_t *session,
                const coap_event_t event) {
----
Note the reversed order of the parameters. If `context` needs to be
recreated, this is done by
----
  coap_context_t *context = coap_session_get_context(session);
----

=== coap_register_ping_handler()

The definition of `coap_ping_handler_t` been updated, so all the functions
registered by `coap_register_ping_handler()` need to be updated. Any
application functions called by these functions may need to include `const` in
their calling parameters. Note that `coap_tid_t` has been replaced with
`coap_mid_t` to reflect the parameter is the message id.

Example

----
4.2.1
  void
  ping_handler(coap_context_t *context,
               coap_session_t *session,
               coap_pdu_t *received,
               const coap_tid_t id);
4.3.0
  void
  ping_handler(coap_session_t *session,
               const coap_pdu_t *received,
               const coap_mid_t mid);
----
If `context` needs to be recreated, this is done by
----
  coap_context_t *context = coap_session_get_context(session);
----

=== coap_register_pong_handler()

The definition of `coap_pong_handler_t` been updated, so all the functions
registered by `coap_register_pong_handler()` need to be updated. Any
application functions called by these functions may need to include `const` in
their calling parameters. Note that `coap_tid_t` has been replaced with
`coap_mid_t` to reflect the parameter is the message id.

Example
----
4.2.1
  void
  pong_handler(coap_context_t *context,
               coap_session_t *session,
               coap_pdu_t *received,
               const coap_tid_t id);
4.3.0
  void
  pong_handler(coap_session_t *session,
               const coap_pdu_t *received,
               const coap_mid_t mid);
----
If `context` needs to be recreated, this is done by
----
  coap_context_t *context = coap_session_get_context(session);
----

== libcoap structures no longer directly accessible

Many of the structures internally used by libcoap are no longer exposed to
applications. Additional functions of the form `coap_X_get_Y()` and
`coap_X_set_Y()` where `X` is the structure type and `Y` is the variable. Below
is a non exhaustive set of examples,

=== coap_pdu_t code variable


Example
----
4.2.1
  if (received->code ==
4.3.0
  coap_pdu_code_t rcvd_code = coap_pdu_get_code(received);
  ...
  if (rcvd_code ==
----

Example
----
4.2.1
  response->code = COAP_RESPONSE_CODE(404);
4.3.0
  coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND);
----
Note that more descriptive names are now supported for the response codes, but
the old form can still be used.

=== coap_pdu_t type variable


Example
----
4.2.1
  if (received->type ==
4.3.0
  coap_pdu_code_t rcvd_type = coap_pdu_get_type(received);
  ...
  if (rcvd_type ==
----

Example
----
4.2.1
  request->type = COAP_MESSAGE_NON;
4.3.0
  coap_pdu_set_type(request, COAP_MESSAGE_NON);
----

=== coap_pdu_t token variable


Example
----
4.2.1
  static inline int
  check_token(coap_pdu_t *received) {
    return received->token_length == the_token.length &&
      memcmp(received->token, the_token.s, the_token.length) == 0;
  }
4.3.0
  static inline int
  check_token(const coap_pdu_t *received) {
    coap_bin_const_t rcvd_token = coap_pdu_get_token(received);

    return rcvd_token.length == the_token.length &&
      memcmp(rcvd_token.s, the_token.s, the_token.length) == 0;
  }
----

=== coap_session_t context variable


Example
----
4.2.1
  if (session->context ==
4.3.0
  coap_context_t context = coap_session_get_context(session);
  ...
  if (context ==
----

=== coap_session_t proto variable


Example
----
4.2.1
  if (session->proto ==
4.3.0
  coap_proto_t proto = coap_session_get_proto(session);
  ...
  if (proto ==
----


== Functions with changed parameters

Some functions have had the parameters updated.  Below are some of the more common ones.

=== coap_pdu_init()

The definition of the second parameter has changed from `coap_request_t` to
`coap_pdu_code_t`.

Example
----
4.2.1
  pdu = coap_pdu_init(msgtype,
                      COAP_REQUEST_GET,
                      coap_new_message_id(session),
                      coap_session_max_pdu_size(session));
4.3.0
  pdu = coap_pdu_init(msgtype,
                      COAP_REQUEST_CODE_GET,
                      coap_new_message_id(session),
                      coap_session_max_pdu_size(session));
----
Note that the second parameter (`COAP_REQUEST_CODE_GET`) goes further than
just request codes and includes the possibility of response codes (e.g.
`COAP_RESPONSE_CODE_CREATED`) from the `enum coap_pdu_code_t`. Hence the
addition of `_CODE` in the parameter value.

=== coap_get_data()

The definition of the third parameter has been changed to be `const`

Example
----
4.2.1
  uint8_t *data;
  ...
  ret = coap_get_data(pdu, &length, &data);
4.3.0
  const uint8_t *data;
  ...
  ret = coap_get_data(pdu, &length, &data);
----

== Large Data Handling

Splitting up large data transmission into blocks (RFC7959) can now all be
handled by internally by libcoap, removing the need for applications to know
anything about how to work with blocks, or need to do any block packet loss
recovery. In simple terms, `coap_context_set_block_mode()` must be called,
`coap_add_data()` (or `coap_add_data_blocked_response()`) is replaced by
`coap_add_data_large_response()` or `coap_add_data_large_request()`, and
`coap_get_data_large()` used instead of `coap_get_data()`.  See man page
`coap_block(3)` for further information.

There are 3 ways of handling the block transfers

=== Application does all the work

This is how things were done in 4.2.1.  The application recognizes the next
block request coming in and then generates the next block response (including
setting up the PDU if client).  To continue using this method,
`coap_context_set_block_mode()` must not be called and none of the `_large`
functions used.

=== Application sees individual blocks

By calling `coap_context_set_block_mode(context, COAP_BLOCK_USE_LIBCOAP)` and
using the `_large` functions, all the existing code that builds the next block
response is no longer needed (and must be removed to prevent packet
request/response duplication) as libcoap does this for the application.

By calling `coap_get_data_large()`, the application can determine if this is
the first block or not (using `offset` value), whether the first block is all
the data (`offset` = `0`, `length` = `total`) and whether this is the last
block (`offset` + `length` = `total`).  It is the responsibility of the
application to re-assemble the individual blocks into a single body of data.

If this is the request handler in a server, the server still needs to return a
`2.31 (Continue)` response code if the received data is not for the final block,
otherwise a `2.01 (Created)` or `2.04 (Changed)` should be returned.

=== Application only sees all the data

By calling `coap_context_set_block_mode(context,
COAP_BLOCK_USE_LIBCOAP|COAP_BLOCK_SINGLE_BODY)` and using the `_large`
functions, all the existing code that builds the next block response is no
longer needed (and must be removed to prevent request/response packet
duplication) as libcoap does this for the application.

`coap_get_data_large()` will only return the entire body of data (`offset`
always `0`, `length` = `total`) and there is no need to re-assemble individual
blocks into a large body of data.

In RAM constrained environments, option 2 may be the preferred method.

== Observe Handling

In the server's request handler's call-back, there is no longer any need to
check whether this is an Observe call (or Observe triggered requesting
additional response call) and add in the Observe Option into the response pdu.
This is now added by libcoap, and trying to add in the Observe Option for the
second time in the call-back handler will throw up a Informational warning.

For the client, there is a new function `coap_cancel_observe()` that can be
called to cancel an observation on the server. To use it,
`coap_context_set_block_mode()` has to be called prior to sending the initial
request containing the Observe option.

== Unused function parameters

`UNUSED_PARAM` has been replaced with `COAP_UNUSED`. If `COAP_UNUSED` is used,
then the definition for `UNUSED_PARAM` can be removed.

== CoAP Message IDs

`coap_tid_t` has been replaced with `coap_mid_t`, as well as `COAP_TID_INVALID`
has been replaced with `COAP_MID_INVALID`. This is so that the Message ID aligns
with the definition in RFC7252.

== Async Support

The `async` support has been re-written to simplify usage, and allows the
underlying libcoap to do the main management / work. A primary change is to
register the async request with a defined delay time before triggering - which
if set to 0 is an infinite time and the delay time subsequently updated if
required.  Consequently the `coap_async_*()` functions now have different
parameters.