Access options from a CoAP message. More...

Detailed Description

Access options from a CoAP message.

To allocate an options object, use UNICOAP_OPTIONS_ALLOC. Provide a name for the variable and an estimate for the total number of bytes needed to represent all options. If you are unsure, consider using UNICOAP_OPTIONS_ALLOC_DEFAULT.

After having allocated an options object, you can access individual options using the many accessor methods. Refer to Predefined Options for typed methods adjusted to each predefined option or Generic Option Methods for options where predefined methods are not provided.

Example:

UNICOAP_OPTIONS_ALLOC(my_options, 20);
int res = 0;
if ((res = unicoap_options_add_uri_queries_string(&my_options, "a=1&b=2")) < 0) {
printf("error: could not add Uri-Query options: %i (%s)\n", res, strerror(-res));
}
printf("error: could set Accept option: %i (%s)\n", res, strerror(-res));
}
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition: stdio.h:60
static ssize_t unicoap_options_set_accept(unicoap_options_t *options, unicoap_content_format_t format)
Sets the Accept option.
Definition: options.h:1220
static ssize_t unicoap_options_add_uri_queries_string(unicoap_options_t *options, char *queries)
Adds multiple Uri-Query options from null-terminated string.
Definition: options.h:2148
@ UNICOAP_FORMAT_TEXT
Content type text/plain; charset=utf-8
Definition: constants.h:720
#define UNICOAP_OPTIONS_ALLOC(name, capacity)
Allocates options with buffer capacity.
Definition: options.h:200

Should you need to dump all options, use unicoap_options_t::unicoap_options_dump_all.

Modules

 Generic Option Methods
 Retrieve and manipulate generic repeatable and non-repeatable options in CoAP messages.
 
 Predefined Options
 Read, set, and add options predefined by IANA.
 

Files

file  options.h
 Options header.
 

Data Structures

struct  unicoap_option_entry_t
 Helper struct for options parser. More...
 
struct  unicoap_options_t
 CoAP options container. More...
 

Option numbers

enum  unicoap_option_number_t {
  UNICOAP_OPTION_IF_MATCH = 1 , UNICOAP_OPTION_URI_HOST = 3 , UNICOAP_OPTION_ETAG = 4 , UNICOAP_OPTION_IF_NONE_MATCH = 5 ,
  UNICOAP_OPTION_OBSERVE = 6 , UNICOAP_OPTION_URI_PORT = 7 , UNICOAP_OPTION_LOCATION_PATH = 8 , UNICOAP_OPTION_OSCORE = 9 ,
  UNICOAP_OPTION_URI_PATH = 11 , UNICOAP_OPTION_CONTENT_FORMAT = 12 , UNICOAP_OPTION_MAX_AGE = 14 , UNICOAP_OPTION_URI_QUERY = 15 ,
  UNICOAP_OPTION_HOP_LIMIT = 16 , UNICOAP_OPTION_ACCEPT = 17 , UNICOAP_OPTION_Q_BLOCK1 = 19 , UNICOAP_OPTION_LOCATION_QUERY = 20 ,
  UNICOAP_OPTION_EDHOC = 21 , UNICOAP_OPTION_BLOCK2 = 23 , UNICOAP_OPTION_BLOCK1 = 27 , UNICOAP_OPTION_SIZE2 = 28 ,
  UNICOAP_OPTION_Q_BLOCK2 = 31 , UNICOAP_OPTION_PROXY_URI = 35 , UNICOAP_OPTION_PROXY_SCHEME = 39 , UNICOAP_OPTION_SIZE1 = 60 ,
  UNICOAP_OPTION_ECHO = 252 , UNICOAP_OPTION_NO_RESPONSE = 258 , UNICOAP_OPTION_REQUEST_TAG = 292 , UNICOAP_SIGNALING_CSM_OPTION_MAX_MESSAGE_SIZE = 2 ,
  UNICOAP_SIGNALING_CSM_OPTION_BLOCKWISE_TRANSFER = 4 , UNICOAP_SIGNALING_PING_PONG_OPTION_CUSTODY = 2 , UNICOAP_SIGNALING_RELEASE_OPTION_ALTERNATIVE_ADDRESS = 2 , UNICOAP_SIGNALING_RELEASE_OPTION_HOLD_OFF = 4 ,
  UNICOAP_SIGNALING_ABORT_OPTION_BAD_CSM = 2
}
 CoAP option number. More...
 

Option characteristics

static bool unicoap_option_is_critical (unicoap_option_number_t option_number)
 Determines whether the given option is considered critical. More...
 
static bool unicoap_option_is_safe_to_forward (unicoap_option_number_t option_number)
 Determines whether the given option is safe to forward. More...
 
static bool unicoap_option_is_no_cache_key (unicoap_option_number_t option_number)
 Determines whether the given option is not intended to be part of the cache key. More...
 
static bool unicoap_option_is_cache_key (unicoap_option_number_t option_number)
 Determines whether the given option is intended to be part of the cache key. More...
 

Tools

const char * unicoap_string_from_option_number (unicoap_option_number_t number)
 Returns label of option corresponding to the given number. More...
 
ssize_t unicoap_options_t::unicoap_options_swap_storage (unicoap_options_t *options, uint8_t *destination, size_t capacity)
 Copies storage and adjusts options struct. More...
 
static uint8_t * unicoap_options_t::unicoap_options_data (const unicoap_options_t *options)
 Retrieves storage buffer. More...
 
static size_t unicoap_options_t::unicoap_options_size (const unicoap_options_t *options)
 Retrieves total size of options in buffer. More...
 

Essentials

static void unicoap_options_t::unicoap_options_init (unicoap_options_t *options, uint8_t *storage, size_t capacity)
 Initializes the given options structure. More...
 
bool unicoap_options_t::unicoap_options_contains (const unicoap_options_t *options, unicoap_option_number_t number)
 Determines whether the given options container has one or more options with the given number. More...
 
static void unicoap_options_t::unicoap_options_clear (unicoap_options_t *options)
 Removes all options. More...
 

Allocating option buffers

#define UNICOAP_OPTIONS_ALLOC(name, capacity)    _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity,)
 Allocates options with buffer capacity. More...
 
#define UNICOAP_OPTIONS_ALLOC_STATIC(name, capacity)    _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity, static)
 Statically allocates options with buffer capacity. More...
 
#define UNICOAP_OPTIONS_ALLOC_DEFAULT(name)    UNICOAP_OPTIONS_ALLOC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)
 Allocates options with default capacity. More...
 
#define UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT(name)    UNICOAP_OPTIONS_ALLOC_STATIC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)
 Statically allocates options with default capacity. More...
 

Auxiliary constants

#define EBADOPT   151
 Bad option.
 
#define EPAYLD   152
 Cannot read option, encountered payload marker.
 
#define UNICOAP_UINT24_MAX   (0xffffff)
 Largest number representable with 24 bits (3 bytes)
 
#define UNICOAP_UINT24_SIZE   (3)
 Size in bytes of number representable with 24 bits (3 bytes)
 
#define UNICOAP_UINT_MAX   (14 + 255 + 0xffff)
 Unsigned integer large enough to accommodate the maximum integer representable by CoAP option delta fields and extended length fields. More...
 

Macro Definition Documentation

◆ UNICOAP_OPTIONS_ALLOC

#define UNICOAP_OPTIONS_ALLOC (   name,
  capacity 
)     _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity,)

Allocates options with buffer capacity.

Parameters
nameName of the variable storing the options structure
capacityStorage buffer capacity in bytes

Allocates a new unicoap_options_t container and a storage buffer with the given capacity, and initializes it. No need to call unicoap_options_t::unicoap_options_init afterwards.

See UNICOAP_OPTIONS_ALLOC_STATIC for static allocation

Definition at line 200 of file options.h.

◆ UNICOAP_OPTIONS_ALLOC_DEFAULT

#define UNICOAP_OPTIONS_ALLOC_DEFAULT (   name)     UNICOAP_OPTIONS_ALLOC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)

Allocates options with default capacity.

Parameters
nameName of the variable storing the options structure

Allocates a new unicoap_options_t container and a storage buffer with CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, and initializes it. No need to call unicoap_options_t::unicoap_options_init afterwards.

See UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT for static allocation

Definition at line 229 of file options.h.

◆ UNICOAP_OPTIONS_ALLOC_STATIC

#define UNICOAP_OPTIONS_ALLOC_STATIC (   name,
  capacity 
)     _UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity, static)

Statically allocates options with buffer capacity.

Parameters
nameName of the variable storing the options structure
capacityStatic storage buffer capacity in bytes

Statically allocates a new unicoap_options_t container and a storage buffer with the given capacity, and initializes it. No need to call unicoap_options_t::unicoap_options_init afterwards.

See UNICOAP_OPTIONS_ALLOC for non-static allocation

Definition at line 215 of file options.h.

◆ UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT

#define UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT (   name)     UNICOAP_OPTIONS_ALLOC_STATIC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)

Statically allocates options with default capacity.

Parameters
nameName of the variable storing the options structure

Statically allocates a new unicoap_options_t container and a storage buffer with CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, and initializes it. No need to call unicoap_options_t::unicoap_options_init afterwards.

See UNICOAP_OPTIONS_ALLOC_DEFAULT for non-static allocation

Definition at line 243 of file options.h.

◆ UNICOAP_UINT_MAX

#define UNICOAP_UINT_MAX   (14 + 255 + 0xffff)

Unsigned integer large enough to accommodate the maximum integer representable by CoAP option delta fields and extended length fields.

See also
CoAP RFC 7252, 3.1

Definition at line 390 of file options.h.

Enumeration Type Documentation

◆ unicoap_option_number_t

CoAP option number.

These option numbers are not valid in signaling messages.

Enumerator
UNICOAP_OPTION_IF_MATCH 

If-Match option

See also
RFC 7252
UNICOAP_OPTION_URI_HOST 

Uri-Host option

See also
RFC 7252
UNICOAP_OPTION_ETAG 

ETag option

See also
RFC 7252
UNICOAP_OPTION_IF_NONE_MATCH 

If-None-Match option

See also
RFC 7252
UNICOAP_OPTION_OBSERVE 

Observe option

See also
RFC 7252
UNICOAP_OPTION_URI_PORT 

Uri-Port

See also
RFC 7252
UNICOAP_OPTION_LOCATION_PATH 

Location-Path option

See also
RFC 7252
UNICOAP_OPTION_OSCORE 

OSCORE option.

Indicates that the CoAP message is an OSCORE message and that it contains a compressed COSE object.

See also
RFC 8613
UNICOAP_OPTION_URI_PATH 

Uri-Path option

See also
RFC 7252
UNICOAP_OPTION_CONTENT_FORMAT 

Content-Format option

See also
RFC 7252
UNICOAP_OPTION_MAX_AGE 

Max-Age option

See also
RFC 7252
UNICOAP_OPTION_URI_QUERY 

Uri-Query option

See also
RFC 7252

Examples: greet=yes, polite in URI coap://example.org/hello-world?greet=yes&polite

UNICOAP_OPTION_HOP_LIMIT 

Hop-Limit option

Used to prevent infinite loops when communicating over multiple proxies.

See also
RFC 8768
UNICOAP_OPTION_ACCEPT 

Accept option

See also
RFC 7252
UNICOAP_OPTION_Q_BLOCK1 

Q-Block1 option

Used for block-wise transfer supporting robust transmission in requests.

See also
RFC 9177
UNICOAP_OPTION_LOCATION_QUERY 

Location-Query option

See also
RFC 7252
UNICOAP_OPTION_EDHOC 

EDHOC option.

Used in a CoAP request to signal that the request payload conveys both an EDHOC message_3 and OSCORE protected data, combined together.

See also
draft-ietf-core-oscore-edhoc-02
UNICOAP_OPTION_BLOCK2 

Block2 option

Used to indicate the block size and number in a block-wise transfer. Used only to transfer a request body.

See also
RFC 7959
UNICOAP_OPTION_BLOCK1 

Block1 option

Used to indicate the block size and number in a block-wise transfer. Used only to transfer a response body.

See also
RFC 7959
UNICOAP_OPTION_SIZE2 

Size2 option.

Used by clients to request an estimate of a resource's total size from a server during block-wise transfer and by servers to inform clients about the size.

See also
RFC 7959
RFC 8613
UNICOAP_OPTION_Q_BLOCK2 

Q-Block2 option.

Used for block-wise transfer supporting robust transmission in responses.

See also
RFC 9177
UNICOAP_OPTION_PROXY_URI 

Proxy-Uri option

See also
RFC 7252
UNICOAP_OPTION_PROXY_SCHEME 

Proxy-Scheme option

See also
RFC 7252
UNICOAP_OPTION_SIZE1 

Size1 option.

Used by clients to give servers an estimate of the total request payload size during block-wise server and by servers to indicate the maximum acceptable payload size in a 4.13 = "Request Entity Too Large") response.

See also
RFC 7252
RFC 7959
RFC 8613
UNICOAP_OPTION_ECHO 

Echo option.

Enables a CoAP server to verify the freshness of a request or to force a client to demonstrate reachability at its claimed network address.

See also
RFC 9175
UNICOAP_OPTION_NO_RESPONSE 

suppress CoAP response

See also
RFC 7968
UNICOAP_OPTION_REQUEST_TAG 

Request-Tag option.

Allows a CoAP server to match block-wise message fragments belonging to the same request.

See also
RFC 9175
UNICOAP_SIGNALING_CSM_OPTION_MAX_MESSAGE_SIZE 

Max-Message-Size option Applies to 7.01 UNICOAP_SIGNAL_CAPABILITIES_SETTINGS message.

See also
RFC8323
UNICOAP_SIGNALING_CSM_OPTION_BLOCKWISE_TRANSFER 

Blockwise-Transfer option Applies to 7.01 UNICOAP_SIGNAL_CAPABILITIES_SETTINGS message.

See also
RFC 8323
UNICOAP_SIGNALING_PING_PONG_OPTION_CUSTODY 

Custody option Applies to 7.02 UNICOAP_SIGNAL_PING and 7.03 UNICOAP_SIGNAL_PONG message.

See also
RFC8323
UNICOAP_SIGNALING_RELEASE_OPTION_ALTERNATIVE_ADDRESS 

Alternative-Address option Applies to 7.04 UNICOAP_SIGNAL_RELEASE message.

See also
RFC 8323
UNICOAP_SIGNALING_RELEASE_OPTION_HOLD_OFF 

Hold-Off option Applies to 7.04 UNICOAP_SIGNAL_RELEASE message.

See also
RFC 8323
UNICOAP_SIGNALING_ABORT_OPTION_BAD_CSM 

Bad-CSM-Option option Applies to 7.05 UNICOAP_SIGNAL_ABORT message.

See also
RFC 8323

Definition at line 419 of file constants.h.

Function Documentation

◆ unicoap_option_is_cache_key()

static bool unicoap_option_is_cache_key ( unicoap_option_number_t  option_number)
inlinestatic

Determines whether the given option is intended to be part of the cache key.

Whether an option is considered a NoCacheKey is independent from the option's value.

Parameters
option_numberThe option number
Returns
A boolean value indicating whether options with the given number are a cache key

Definition at line 305 of file options.h.

◆ unicoap_option_is_critical()

static bool unicoap_option_is_critical ( unicoap_option_number_t  option_number)
inlinestatic

Determines whether the given option is considered critical.

Criticality is independent from the corresponding option's value. As per CoAP RFC 7252, critical options must never be silently ignored.

Parameters
option_numberThe option number
Returns
A boolean value indicating whether options with the given number are critical

Definition at line 262 of file options.h.

◆ unicoap_option_is_no_cache_key()

static bool unicoap_option_is_no_cache_key ( unicoap_option_number_t  option_number)
inlinestatic

Determines whether the given option is not intended to be part of the cache key.

Whether an option is considered NoCacheKey is independent from the option's value.

Parameters
option_numberThe option number
Returns
A boolean value indicating whether options with the given number are not a cache key

Definition at line 290 of file options.h.

◆ unicoap_option_is_safe_to_forward()

static bool unicoap_option_is_safe_to_forward ( unicoap_option_number_t  option_number)
inlinestatic

Determines whether the given option is safe to forward.

Whether an option is safe to forward is independent from the corresponding option's value.

Parameters
option_numberThe option number
Returns
A boolean value indicating whether options with the given number are safe to forward

Definition at line 275 of file options.h.

◆ unicoap_options_clear()

static void unicoap_options_clear ( unicoap_options_t options)
inline

Removes all options.

Parameters
[in,out]optionsOptions

Definition at line 167 of file options.h.

◆ unicoap_options_contains()

bool unicoap_options_contains ( const unicoap_options_t options,
unicoap_option_number_t  number 
)

Determines whether the given options container has one or more options with the given number.

Parameters
[in]optionsOptions
numberThe option number
Returns
true if options contains an options with the given number

◆ unicoap_options_data()

static uint8_t * unicoap_options_data ( const unicoap_options_t options)
inline

Retrieves storage buffer.

Parameters
[in]optionsOptions
Returns
Pointer to first byte of storage buffer, can be NULL

Definition at line 345 of file options.h.

◆ unicoap_options_init()

static void unicoap_options_init ( unicoap_options_t options,
uint8_t *  storage,
size_t  capacity 
)
inline

Initializes the given options structure.

Parameters
[in,out]optionsOptions struct to initialize
[in]storageA buffer unicoap will use to store option values in
capacityThe number of usable bytes in storage

Definition at line 139 of file options.h.

◆ unicoap_options_size()

static size_t unicoap_options_size ( const unicoap_options_t options)
inline

Retrieves total size of options in buffer.

Parameters
[in]optionsOptions
Returns
Size of options in buffer in bytes

Definition at line 357 of file options.h.

◆ unicoap_options_swap_storage()

ssize_t unicoap_options_swap_storage ( unicoap_options_t options,
uint8_t *  destination,
size_t  capacity 
)

Copies storage and adjusts options struct.

Parameters
[in,out]optionsOptions struct whose storage to swap
[in,out]destinationNew storage buffer
capacitydestination capacity
Returns
negative number on error
Return values
<tt>0</tt>on success
<tt>-ENOBUFS</tt>destination is not sufficiently big

◆ unicoap_string_from_option_number()

const char* unicoap_string_from_option_number ( unicoap_option_number_t  number)

Returns label of option corresponding to the given number.

Parameters
numberOption number
Returns
Null-terminated string label, such as Content-Format