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
/*
* coap_option.h -- helpers for handling options in CoAP PDUs
*
* Copyright (C) 2010-2013,2022 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_option.h
* @brief Helpers for handling options in CoAP PDUs
*/
typedef uint16_t coap_option_num_t;
/**
* Use byte-oriented access methods here because sliding a complex struct
* coap_opt_t over the data buffer may cause bus error on certain platforms.
*/
typedef uint8_t coap_opt_t;
/**
* Representation of CoAP options.
*/
typedef struct coap_option_t;
/**
* Parses the option pointed to by @p opt into @p result. This function returns
* the number of bytes that have been parsed, or @c 0 on error. An error is
* signaled when illegal delta or length values are encountered or when option
* parsing would result in reading past the option (i.e. beyond opt + length).
*
* @param opt The beginning of the option to parse.
* @param length The maximum length of @p opt.
* @param result A pointer to the coap_option_t structure that is filled with
* actual values iff coap_opt_parse() > 0.
* @return The number of bytes parsed or @c 0 on error.
*/
size_t ;
/**
* Returns the size of the given option, taking into account a possible option
* jump.
*
* @param opt An option jump or the beginning of the option.
* @return The number of bytes between @p opt and the end of the option
* starting at @p opt. In case of an error, this function returns
* @c 0 as options need at least one byte storage space.
*/
size_t ;
/**
* @ingroup application_api
* @defgroup opt_filter Option Filters
* API for access option filters
* @{
*/
/**
* The number of option types below 256 that can be stored in an
* option filter. COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be
* at most 16. Each coap_option_filter_t object reserves
* ((COAP_OPT_FILTER_SHORT + 1) / 2) * 2 bytes for short options.
*/
/**
* The number of option types above 255 that can be stored in an
* option filter. COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be
* at most 16. Each coap_option_filter_t object reserves
* COAP_OPT_FILTER_LONG * 2 bytes for short options.
*/
/* Ensure that COAP_OPT_FILTER_SHORT and COAP_OPT_FILTER_LONG are set
* correctly. */
/* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */
/*
* mask contains a bit vector that indicates which fields in the long_opts[]
* and subsequent short_opts[] are used. The first COAP_OPT_FILTER_LONG bits
* correspond to the long option types that are stored in long_opts[]
* elements. The next COAP_OPT_FILTER_SHORT bits correspond to the short
* option types that are stored in short_opts[].
*/
typedef struct coap_opt_filter_t coap_opt_filter_t;
/** Pre-defined filter that includes all options. */
/**
* Clears filter @p filter.
*
* @param filter The filter to clear.
*/
void
;
/**
* Sets the corresponding entry for @p number in @p filter. This
* function returns @c 1 if bit was set or @c 0 on error (i.e. when
* the given number does not fit in the filter).
*
* @param filter The filter object to change.
* @param number The option number for which the bit should be set.
*
* @return @c 1 if bit was set, @c 0 otherwise.
*/
int ;
/**
* Clears the corresponding entry for @p number in @p filter. This
* function returns @c 1 if bit was set or @c 0 on error (i.e. when
* the given number does not fit in the filter).
*
* @param filter The filter object to change.
* @param number The option number that should be cleared from the filter.
*
* @return @c 1 if bit was set, @c 0 otherwise.
*/
int ;
/**
* Checks if @p number is contained in @p filter. This function returns
* @c 1 if found, @c 0 if not, or @c -1 on error (i.e. when the given
* number does not fit in the filter).
*
* @param filter The filter object to search.
* @param number The option number to search for.
*
* @return @c 1 if @p number was found, @c 0 otherwise, or @c -1 on error.
*/
int ;
/**
* Iterator to run through PDU options. This object must be
* initialized with coap_option_iterator_init(). Call
* coap_option_next() to walk through the list of options until
* coap_option_next() returns @c NULL.
*
* @code
* coap_opt_t *option;
* coap_opt_iterator_t opt_iter;
* coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
*
* while ((option = coap_option_next(&opt_iter))) {
* ... do something with option ...
* }
* @endcode
*/
typedef struct coap_opt_iterator_t;
/**
* Initializes the given option iterator @p oi to point to the beginning of the
* @p pdu's option list. This function returns @p oi on success, @c NULL
* otherwise (i.e. when no options exist). Note that a length check on the
* option list must be performed before coap_option_iterator_init() is called.
*
* @param pdu The PDU the options of which should be walked through.
* @param oi An iterator object that will be initilized.
* @param filter An optional option number filter.
* With @p number != @c COAP_OPT_ALL, coap_option_next()
* will return only options matching this bitmask.
* Fence-post options @c 14, @c 28, @c 42, ... are always
* skipped.
*
* @return The iterator object @p oi on success, @c NULL otherwise.
*/
coap_opt_iterator_t *;
/**
* Updates the iterator @p oi to point to the next option. This function returns
* a pointer to that option or @c NULL if no more options exist. The contents of
* @p oi will be updated. In particular, @c oi->n specifies the current option's
* ordinal number (counted from @c 1), @c oi->number is the option's number
* value, and @c oi->option points to the beginning of the current option
* itself. When * advanced past the last option, @c oi->option will be @c NULL.
*
* Note that options are skipped whose corresponding bits in the filter
* specified with coap_option_iterator_init() are @c 0. Options with numbers
* that do not fit in this filter hence will always be returned.
*
* @param oi The option iterator to update.
*
* @return The next option or @c NULL if no more options exist.
*/
coap_opt_t *;
/**
* Retrieves the first option of number @p number from @p pdu. @p oi must
* point to a coap_opt_iterator_t object that will be initialized by this
* function to filter only options with number @p number. This function returns
* the first option with this number, or @c NULL if not found.
*
* @param pdu The PDU to parse for options.
* @param number The option number to search for.
* @param oi An iterator object to use.
*
* @return A pointer to the first option of number @p number, or @c NULL if
* not found.
*/
coap_opt_t *;
/**
* Encodes the given delta and length values into @p opt. This function returns
* the number of bytes that were required to encode @p delta and @p length or @c
* 0 on error. Note that the result indicates by how many bytes @p opt must be
* advanced to encode the option value.
*
* @param opt The option buffer space where @p delta and @p length are
* written.
* @param maxlen The maximum length of @p opt.
* @param delta The actual delta value to encode.
* @param length The actual length value to encode.
*
* @return The number of bytes used or @c 0 on error.
*/
size_t ;
/**
* Compute storage bytes needed for an option with given @p delta and
* @p length
*
* @param delta The option delta.
* @param length The option length.
*
* @return The number of bytes required to encode this option.
*/
size_t ;
/**
* Encodes option with given @p delta into @p opt. This function returns the
* number of bytes written to @p opt or @c 0 on error. This happens especially
* when @p opt does not provide sufficient space to store the option value,
* delta, and option jumps when required.
*
* @param opt The option buffer space where @p val is written.
* @param n Maximum length of @p opt.
* @param delta The option delta.
* @param val The option value to copy into @p opt.
* @param length The actual length of @p val.
*
* @return The number of bytes that have been written to @p opt or @c 0 on
* error. The return value will always be less than @p n.
*/
size_t ;
/**
* Returns the length of the given option. @p opt must point to an option jump
* or the beginning of the option. This function returns @c 0 when @p opt is not
* an option or the actual length of @p opt (which can be @c 0 as well).
*
* @note {The rationale for using @c 0 in case of an error is that in most
* contexts, the result of this function is used to skip the next
* coap_opt_length() bytes.}
*
* @param opt The option whose length should be returned.
*
* @return The option's length or @c 0 when undefined.
*/
uint32_t ;
/**
* Returns a pointer to the value of the given option. @p opt must point to an
* option jump or the beginning of the option. This function returns @c NULL if
* @p opt is not a valid option.
*
* @param opt The option whose value should be returned.
*
* @return A pointer to the option value or @c NULL on error.
*/
const uint8_t *;
/**
* Representation of chained list of CoAP options to install.
*
* @code
* coap_optlist_t *optlist_chain = NULL;
* coap_pdu_t *pdu = coap_new_pdu(session);
*
* ... other set up code ...
* coap_insert_optlist(&optlist_chain, coap_new_optlist(COAP_OPTION_OBSERVE,
* COAP_OBSERVE_ESTABLISH, NULL));
*
* coap_add_optlist_pdu(pdu, &optlist_chain);
* ... other code ...
* coap_delete_optlist(optlist_chain);
* @endcode
*/
typedef struct coap_optlist_t coap_optlist_t;
/**
* Create a new optlist entry.
*
* Note: Where possible, the option data needs to be stripped of leading zeros
* (big endian) to reduce the amount of data needed in the PDU, as well as in
* some cases the maximum data size of an opton can be exceeded if not stripped
* and hence be illegal. This is done by using coap_encode_var_safe() or
* coap_encode_var_safe8().
*
* @param number The option number (COAP_OPTION_*)
* @param length The option length
* @param data The option value data
*
* @return A pointer to the new optlist entry, or @c NULL if error
*/
coap_optlist_t *;
/**
* The current optlist of @p optlist_chain is first sorted (as per RFC7272
* ordering requirements) and then added to the @p pdu.
*
* @param pdu The pdu to add the options to from the chain list
* @param optlist_chain The chained list of optlist to add to the pdu
*
* @return @c 1 if succesful or @c 0 if failure;
*/
int ;
/**
* Adds @p optlist to the given @p optlist_chain. The optlist_chain variable
* be set to NULL before the initial call to coap_insert_optlist().
* The optlist_chain will need to be deleted using coap_delete_optlist()
* when no longer required.
*
* @param optlist_chain The chain to add optlist to
* @param optlist The optlist to add to the queue
*
* @return @c 1 if successful, @c 0 otherwise.
*/
int ;
/**
* Removes all entries from the @p optlist_chain, freeing off their
* memory usage.
*
* @param optlist_chain The optlist chain to remove all the entries from
*/
void ;
/** @} */
/**
* Sets the corresponding bit for @p type in @p filter. This function returns @c
* 1 if bit was set or @c -1 on error (i.e. when the given type does not fit in
* the filter).
*
* @deprecated Use coap_option_filter_set() instead.
*
* @param filter The filter object to change.
* @param type The type for which the bit should be set.
*
* @return @c 1 if bit was set, @c -1 otherwise.
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
/**
* Clears the corresponding bit for @p type in @p filter. This function returns
* @c 1 if bit was cleared or @c -1 on error (i.e. when the given type does not
* fit in the filter).
*
* @deprecated Use coap_option_filter_unset() instead.
*
* @param filter The filter object to change.
* @param type The type for which the bit should be cleared.
*
* @return @c 1 if bit was set, @c -1 otherwise.
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
/**
* Gets the corresponding bit for @p type in @p filter. This function returns @c
* 1 if the bit is set @c 0 if not, or @c -1 on error (i.e. when the given type
* does not fit in the filter).
*
* @deprecated Use coap_option_filter_get() instead.
*
* @param filter The filter object to read bit from.
* @param type The type for which the bit should be read.
*
* @return @c 1 if bit was set, @c 0 if not, @c -1 on error.
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
/* COAP_OPTION_H_ */