libcoap 4.3.5-develop-4c3f4af
Loading...
Searching...
No Matches
coap_option.h
Go to the documentation of this file.
1/*
2 * coap_option.h -- helpers for handling options in CoAP PDUs
3 *
4 * Copyright (C) 2010-2013,2022-2026 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see README for terms
9 * of use.
10 */
11
16
17#ifndef COAP_OPTION_H_
18#define COAP_OPTION_H_
19
20#if defined(RIOT_VERSION)
21#include <thread.h>
22#endif /* RIOT_VERSION */
23
24#if defined(_WIN32)
25#include <stddef.h>
26#include <stdint.h>
27#endif /* _WIN32 */
28
29#ifndef NULL
30#define NULL 0
31#endif
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37typedef uint16_t coap_option_num_t;
38
39/*
40 * CoAP option numbers (be sure to update coap_option_check_critical()
41 * and coap_option_check_repeatable() when adding options).
42 */
43
44/*
45 * The C, U, and N flags indicate the properties
46 * Critical, Unsafe, and NoCacheKey, respectively.
47 * If U is set, then N has no meaning as per
48 * https://rfc-editor.org/rfc/rfc7252#section-5.10
49 * and is set to a -.
50 *
51 * Separately, R is for the options that can be repeated
52 *
53 * The least significant byte of the option is set as followed
54 * as per https://rfc-editor.org/rfc/rfc7252#section-5.4.6
55 *
56 * 0 1 2 3 4 5 6 7
57 * --+---+---+---+---+---+---+---+
58 * | NoCacheKey| U | C |
59 * --+---+---+---+---+---+---+---+
60 *
61 * https://rfc-editor.org/rfc/rfc8613#section-4 goes on to define E, I and U
62 * properties Encrypted and Integrity Protected, Integrity Protected Only, and
63 * Unprotected respectively. Integrity Protected Only is not currently used.
64 *
65 * An Option is tagged with CUNREIU with any of the letters replaced with _ if
66 * not set, or - for N if U is set (see above) for aiding understanding of the
67 * Option.
68 */
69
70typedef enum coap_code_opt_num_t {
71 COAP_OPTION_IF_MATCH = 1, /* C__RE__, opaque, 0-8 B, none, RFC7252 */
72 COAP_OPTION_URI_HOST = 3, /* CU-___U, String, 1-255 B, none, RFC7252 */
73 COAP_OPTION_ETAG = 4, /* ___RE__, opaque, 1-8 B, none, RFC7252 */
74 COAP_OPTION_IF_NONE_MATCH = 5, /* C___E__, empty, 0 B, none, RFC7252 */
75 COAP_OPTION_OBSERVE = 6, /* _U-_E_U, empty/uint,0/0-3 B, none, RFC7641 */
76 COAP_OPTION_URI_PORT = 7, /* CU-___U, uint, 0-2 B, none, RFC7252 */
77 COAP_OPTION_LOCATION_PATH = 8, /* ___RE__, String, 0-255 B, none, RFC7252 */
78 COAP_OPTION_OSCORE = 9, /* C_____U, *, 0-255 B, none, RFC8613 */
79 COAP_OPTION_URI_PATH = 11, /* CU-RE__, String, 0-255 B, none, RFC7252 */
80 COAP_OPTION_CONTENT_FORMAT= 12, /* ____E__, uint, 0-2 B, none, RFC7252 */
81 COAP_OPTION_URI_PATH_ABB = 13, /* C___E__, uint, 0-4 B, none, RFC TBD */
82 /* COAP_OPTION_MAXAGE default 60 seconds if not set */
83 COAP_OPTION_MAXAGE = 14, /* _U-_E_U, uint, 0-4 B, 60, RFC7252 */
84 COAP_OPTION_URI_QUERY = 15, /* CU-RE__, String, 1-255 B, none, RFC7252 */
85 COAP_OPTION_HOP_LIMIT = 16, /* ______U, uint, 1 B, none, RFC8768 */
86 COAP_OPTION_ACCEPT = 17, /* C___E__, uint, 0-2 B, none, RFC7252 */
87 COAP_OPTION_Q_BLOCK1 = 19, /* CU__E_U, uint, 0-3 B, none, RFC9177 */
88 COAP_OPTION_LOCATION_QUERY= 20, /* ___RE__, String, 0-255 B, none, RFC7252 */
89 COAP_OPTION_EDHOC = 21, /* C_____U, empty, 0 B, none, RFC9668 */
90 COAP_OPTION_BLOCK2 = 23, /* CU-_E_U, uint, 0-3 B, none, RFC7959 */
91 COAP_OPTION_BLOCK1 = 27, /* CU-_E_U, uint, 0-3 B, none, RFC7959 */
92 COAP_OPTION_SIZE2 = 28, /* __N_E_U, uint, 0-4 B, none, RFC7959 */
93 COAP_OPTION_Q_BLOCK2 = 31, /* CU_RE_U, uint, 0-3 B, none, RFC9177 */
94 COAP_OPTION_PROXY_URI = 35, /* CU-___U, String, 1-1034 B, none, RFC7252 */
95 COAP_OPTION_PROXY_SCHEME = 39, /* CU-___U, String, 1-255 B, none, RFC7252 */
96 COAP_OPTION_SIZE1 = 60, /* __N_E_U, uint, 0-4 B, none, RFC7252 */
97 COAP_OPTION_ECHO = 252, /* __N_E_U, opaque, 0-40 B, none, RFC9175 */
98 COAP_OPTION_NORESPONSE = 258, /* _U-_E_U, uint, 0-1 B, none, RFC7967 */
99 COAP_OPTION_RTAG = 292, /* ___RE_U, opaque, 0-8 B, none, RFC9175 */
101
102#define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
103
104#if (UINT_MAX > 65535)
105#define COAP_MAX_OPT 65535
106#else /* UINT_MAX <= 65535 */
107#define COAP_MAX_OPT 65534
108#endif /* UINT_MAX <= 65535 */
109
110/* Signaling options */
111
112/* Applies to COAP_SIGNALING_CSM (7.01) */
113typedef enum coap_sig_csm_opt_t {
114 COAP_SIG_OPT_MAX_MESSAGE_SIZE = 2, /* ____E_U, uint, 0-4 B, 1152, RFC8323 */
115 COAP_SIG_OPT_BLOCK_WISE_TRANSFER = 4, /* ____E_U, empty, 0 B, none, RFC8323 */
116 COAP_SIG_OPT_EXTENDED_TOKEN_LENGTH = 6, /* ____E_U, uint, 0-3 B, 8, RFC8974 */
118
119/* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG (7.02 / 7.03) */
121 COAP_SIG_OPT_CUSTODY = 2, /* ____E_U, empty, 0 B, none, RFC8323 */
123
124/* Applies to COAP_SIGNALING_RELEASE (7.04) */
126 COAP_SIG_OPT_ALTERNATIVE_ADDRESS = 2, /* ___RE_U, String, 1-255 B, none, RFC8323 */
127 COAP_SIG_OPT_HOLD_OFF = 4, /* ____E_U, uint, 0-3 B, none, RFC8323 */
129
130/* Applies to COAP_SIGNALING_ABORT (7.05 )*/
132 COAP_SIG_OPT_BAD_CSM_OPTION = 2, /* ____E_U, uint, 0-2 B, none, RFC8323 */
134
135/* Backward Application compatability */
136#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE COAP_SIG_OPT_MAX_MESSAGE_SIZE
137#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER COAP_SIG_OPT_BLOCK_WISE_TRANSFER
138#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH COAP_SIG_OPT_EXTENDED_TOKEN_LENGTH
139#define COAP_SIGNALING_OPTION_CUSTODY COAP_SIG_OPT_CUSTODY
140#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS COAP_SIG_OPT_ALTERNATIVE_ADDRESS
141#define COAP_SIGNALING_OPTION_HOLD_OFF COAP_SIG_OPT_HOLD_OFF
142#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION COAP_SIG_OPT_BAD_CSM_OPTION
143
148typedef uint8_t coap_opt_t;
149#define PCHAR(p) ((coap_opt_t *)(p))
150
154typedef struct {
155 uint16_t delta;
156 size_t length;
157 const uint8_t *value;
159
172size_t coap_opt_parse(const coap_opt_t *opt,
173 size_t length,
174 coap_option_t *result);
175
185size_t coap_opt_size(const coap_opt_t *opt);
186
193
200#define COAP_OPT_FILTER_SHORT 6
201
208#define COAP_OPT_FILTER_LONG 2
209
210/* Ensure that COAP_OPT_FILTER_SHORT and COAP_OPT_FILTER_LONG are set
211 * correctly. */
212#if (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16)
213#error COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be less or equal 16
214#endif /* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */
215
216/*
217 * mask contains a bit vector that indicates which fields in the long_opts[]
218 * and subsequent short_opts[] are used. The first COAP_OPT_FILTER_LONG bits
219 * correspond to the long option types that are stored in long_opts[]
220 * elements. The next COAP_OPT_FILTER_SHORT bits correspond to the short
221 * option types that are stored in short_opts[].
222 */
228
230#define COAP_OPT_ALL NULL
231
238
250
261 coap_option_num_t number);
262
273
298
317 const coap_opt_filter_t *filter);
318
336
351 coap_option_num_t number,
353
369 size_t maxlen,
370 uint16_t delta,
371 size_t length);
372
382size_t coap_opt_encode_size(uint16_t delta, size_t length);
383
399size_t coap_opt_encode(coap_opt_t *opt,
400 size_t n,
401 uint16_t delta,
402 const uint8_t *val,
403 size_t length);
404
418uint32_t coap_opt_length(const coap_opt_t *opt);
419
429const uint8_t *coap_opt_value(const coap_opt_t *opt);
430
453
470 size_t length,
471 const uint8_t *data);
472
482int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **optlist_chain);
483
484
492int coap_sort_optlist(coap_optlist_t **options);
493
505int coap_insert_optlist(coap_optlist_t **optlist_chain,
506 coap_optlist_t *optlist);
507
514void coap_delete_optlist(coap_optlist_t *optlist_chain);
515
517
532 return coap_option_filter_set(filter, type) ? 1 : -1;
533}
534
548 return coap_option_filter_unset(filter, type) ? 1 : -1;
549}
550
564 return coap_option_filter_get(filter, type);
565}
566
567#ifdef __cplusplus
568}
569#endif
570
571#endif /* COAP_OPTION_H_ */
COAP_STATIC_INLINE COAP_DEPRECATED int coap_option_getb(coap_opt_filter_t *filter, coap_option_num_t type)
Gets the corresponding bit for type in filter.
size_t coap_opt_parse(const coap_opt_t *opt, size_t length, coap_option_t *result)
Parses the option pointed to by opt into result.
Definition coap_option.c:41
COAP_STATIC_INLINE COAP_DEPRECATED int coap_option_setb(coap_opt_filter_t *filter, coap_option_num_t type)
Sets the corresponding bit for type in filter.
size_t coap_opt_size(const coap_opt_t *opt)
Returns the size of the given option, taking into account a possible option jump.
COAP_STATIC_INLINE COAP_DEPRECATED int coap_option_clrb(coap_opt_filter_t *filter, coap_option_num_t type)
Clears the corresponding bit for type in filter.
uint16_t coap_option_num_t
Definition coap_option.h:37
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_sig_ping_opt_t
@ COAP_SIG_OPT_CUSTODY
coap_sig_csm_opt_t
@ COAP_SIG_OPT_BLOCK_WISE_TRANSFER
@ COAP_SIG_OPT_EXTENDED_TOKEN_LENGTH
@ COAP_SIG_OPT_MAX_MESSAGE_SIZE
coap_sig_release_opt_t
@ COAP_SIG_OPT_ALTERNATIVE_ADDRESS
@ COAP_SIG_OPT_HOLD_OFF
coap_code_opt_num_t
Definition coap_option.h:70
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
@ COAP_OPTION_IF_NONE_MATCH
Definition coap_option.h:74
@ COAP_OPTION_ETAG
Definition coap_option.h:73
@ COAP_OPTION_EDHOC
Definition coap_option.h:89
@ COAP_OPTION_NORESPONSE
Definition coap_option.h:98
@ COAP_OPTION_MAXAGE
Definition coap_option.h:83
@ COAP_OPTION_SIZE2
Definition coap_option.h:92
@ COAP_OPTION_Q_BLOCK2
Definition coap_option.h:93
@ COAP_OPTION_PROXY_SCHEME
Definition coap_option.h:95
@ COAP_OPTION_HOP_LIMIT
Definition coap_option.h:85
@ COAP_OPTION_URI_PORT
Definition coap_option.h:76
@ COAP_OPTION_URI_HOST
Definition coap_option.h:72
@ COAP_OPTION_BLOCK2
Definition coap_option.h:90
@ COAP_OPTION_IF_MATCH
Definition coap_option.h:71
@ COAP_OPTION_SIZE1
Definition coap_option.h:96
@ COAP_OPTION_ECHO
Definition coap_option.h:97
@ COAP_OPTION_RTAG
Definition coap_option.h:99
@ COAP_OPTION_BLOCK1
Definition coap_option.h:91
@ COAP_OPTION_URI_PATH
Definition coap_option.h:79
@ COAP_OPTION_Q_BLOCK1
Definition coap_option.h:87
@ COAP_OPTION_OSCORE
Definition coap_option.h:78
@ COAP_OPTION_CONTENT_FORMAT
Definition coap_option.h:80
@ COAP_OPTION_LOCATION_QUERY
Definition coap_option.h:88
@ COAP_OPTION_URI_QUERY
Definition coap_option.h:84
@ COAP_OPTION_PROXY_URI
Definition coap_option.h:94
@ COAP_OPTION_LOCATION_PATH
Definition coap_option.h:77
@ COAP_OPTION_URI_PATH_ABB
Definition coap_option.h:81
@ COAP_OPTION_ACCEPT
Definition coap_option.h:86
coap_sig_abort_opt_t
@ COAP_SIG_OPT_BAD_CSM_OPTION
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
size_t coap_opt_encode(coap_opt_t *opt, size_t n, uint16_t delta, const uint8_t *val, size_t length)
Encodes option with given delta into opt.
coap_optlist_t * coap_new_optlist(coap_option_num_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
void coap_delete_optlist(coap_optlist_t *optlist_chain)
Removes all entries from the optlist_chain, freeing off their memory usage.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
int coap_sort_optlist(coap_optlist_t **options)
Sorts the optlist chain.
#define COAP_OPT_FILTER_SHORT
The number of option types below 256 that can be stored in an option filter.
int coap_option_filter_unset(coap_opt_filter_t *filter, coap_option_num_t number)
Clears the corresponding entry for number in filter.
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **optlist_chain)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
#define COAP_OPT_FILTER_LONG
The number of option types above 255 that can be stored in an option filter.
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
int coap_insert_optlist(coap_optlist_t **optlist_chain, coap_optlist_t *optlist)
Adds optlist to the given optlist_chain.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t number)
Checks if number is contained in filter.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t number)
Sets the corresponding entry for number in filter.
size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, uint16_t delta, size_t length)
Encodes the given delta and length values into opt.
#define COAP_DEPRECATED
Definition libcoap.h:66
#define COAP_STATIC_INLINE
Definition libcoap.h:57
uint8_t short_opts[COAP_OPT_FILTER_SHORT]
uint16_t long_opts[COAP_OPT_FILTER_LONG]
Iterator to run through PDU options.
coap_opt_t * next_option
pointer to the unparsed next option
coap_opt_filter_t filter
option filter
unsigned int bad
iterator object is ok if not set
size_t length
remaining length of PDU
unsigned int filtered
denotes whether or not filter is used
coap_option_num_t number
decoded option number
Representation of CoAP options.
const uint8_t * value
uint16_t delta
Representation of chained list of CoAP options to install.
coap_option_num_t number
the option number (no delta coding)
size_t length
the option value length
uint8_t * data
the option data
struct coap_optlist_t * next
next entry in the optlist chain
structure for CoAP PDUs