Loading...
Searching...
No Matches
coap_option.h
Go to the documentation of this file.
/*
* coap_option.h -- helpers for handling options in CoAP PDUs
*
* Copyright (C) 2010-2013,2022-2023 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.
*/
#ifndef COAP_OPTION_H_
#define COAP_OPTION_H_
typedef uint16_t coap_option_num_t;
typedef uint8_t coap_opt_t;
#define PCHAR(p) ((coap_opt_t *)(p))
typedef struct {
uint16_t delta;
size_t length;
const uint8_t *value;
} coap_option_t;
size_t coap_opt_parse(const coap_opt_t *opt,
size_t length,
coap_option_t *result);
size_t coap_opt_size(const coap_opt_t *opt);
#define COAP_OPT_FILTER_SHORT 6
#define COAP_OPT_FILTER_LONG 2
/* Ensure that COAP_OPT_FILTER_SHORT and COAP_OPT_FILTER_LONG are set
* correctly. */
#if (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16)
#error COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be less or equal 16
#endif /* (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 {
uint16_t mask;
uint16_t long_opts[COAP_OPT_FILTER_LONG];
uint8_t short_opts[COAP_OPT_FILTER_SHORT];
} coap_opt_filter_t;
#define COAP_OPT_ALL NULL
void coap_option_filter_clear(coap_opt_filter_t *filter);
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t number);
int coap_option_filter_unset(coap_opt_filter_t *filter,
coap_option_num_t number);
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t number);
typedef struct {
size_t length;
coap_option_num_t number;
unsigned int bad:1;
unsigned int filtered:1;
coap_opt_t *next_option;
coap_opt_filter_t filter;
} coap_opt_iterator_t;
coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *pdu,
coap_opt_iterator_t *oi,
const coap_opt_filter_t *filter);
coap_opt_t *coap_option_next(coap_opt_iterator_t *oi);
coap_opt_t *coap_check_option(const coap_pdu_t *pdu,
coap_option_num_t number,
coap_opt_iterator_t *oi);
size_t coap_opt_setheader(coap_opt_t *opt,
size_t maxlen,
uint16_t delta,
size_t length);
size_t coap_opt_encode_size(uint16_t delta, size_t length);
size_t coap_opt_encode(coap_opt_t *opt,
size_t n,
uint16_t delta,
const uint8_t *val,
size_t length);
uint32_t coap_opt_length(const coap_opt_t *opt);
const uint8_t *coap_opt_value(const coap_opt_t *opt);
typedef struct coap_optlist_t {
struct coap_optlist_t *next;
uint16_t number;
size_t length;
uint8_t *data;
} coap_optlist_t;
coap_optlist_t *coap_new_optlist(uint16_t number,
size_t length,
const uint8_t *data);
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **optlist_chain);
int coap_insert_optlist(coap_optlist_t **optlist_chain,
coap_optlist_t *optlist);
void coap_delete_optlist(coap_optlist_t *optlist_chain);
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_option_setb(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_set(filter, type) ? 1 : -1;
}
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_option_clrb(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_unset(filter, type) ? 1 : -1;
}
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_option_getb(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_get(filter, type);
}
#endif /* COAP_OPTION_H_ */
coap_opt_parse
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_opt_size
size_t coap_opt_size(const coap_opt_t *opt)
Returns the size of the given option, taking into account a possible option jump.
Definition coap_option.c:284
coap_option_num_t
uint16_t coap_option_num_t
Definition coap_option.h:20
coap_opt_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition coap_option.h:26
coap_option_clrb
COAP_STATIC_INLINE COAP_DEPRECATED int coap_option_clrb(coap_opt_filter_t *filter, uint16_t type)
Clears the corresponding bit for type in filter.
Definition coap_option.h:415
coap_option_getb
COAP_STATIC_INLINE COAP_DEPRECATED int coap_option_getb(coap_opt_filter_t *filter, uint16_t type)
Gets the corresponding bit for type in filter.
Definition coap_option.h:431
coap_option_setb
COAP_STATIC_INLINE COAP_DEPRECATED int coap_option_setb(coap_opt_filter_t *filter, uint16_t type)
Sets the corresponding bit for type in filter.
Definition coap_option.h:399
coap_option_next
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition coap_option.c:153
coap_new_optlist
coap_optlist_t * coap_new_optlist(uint16_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
Definition coap_option.c:511
coap_opt_encode
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.
Definition coap_option.c:372
coap_opt_length
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition coap_option.c:212
coap_option_iterator_init
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.
Definition coap_option.c:117
coap_delete_optlist
void coap_delete_optlist(coap_optlist_t *optlist_chain)
Removes all entries from the optlist_chain, freeing off their memory usage.
Definition coap_option.c:592
coap_opt_encode_size
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
Definition coap_option.c:351
COAP_OPT_FILTER_SHORT
#define COAP_OPT_FILTER_SHORT
The number of option types below 256 that can be stored in an option filter.
Definition coap_option.h:78
coap_option_filter_unset
int coap_option_filter_unset(coap_opt_filter_t *filter, coap_option_num_t number)
Clears the corresponding entry for number in filter.
Definition coap_option.c:501
coap_add_optlist_pdu
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 ...
Definition coap_option.c:551
COAP_OPT_FILTER_LONG
#define COAP_OPT_FILTER_LONG
The number of option types above 255 that can be stored in an option filter.
Definition coap_option.h:86
coap_option_filter_clear
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
Definition coap_option.c:491
coap_check_option
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.
Definition coap_option.c:199
coap_insert_optlist
int coap_insert_optlist(coap_optlist_t **optlist_chain, coap_optlist_t *optlist)
Adds optlist to the given optlist_chain.
Definition coap_option.c:571
coap_opt_value
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition coap_option.c:249
coap_option_filter_get
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t number)
Checks if number is contained in filter.
Definition coap_option.c:506
coap_option_filter_set
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t number)
Sets the corresponding entry for number in filter.
Definition coap_option.c:496
coap_opt_setheader
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.
Definition coap_option.c:292
COAP_DEPRECATED
#define COAP_DEPRECATED
Definition libcoap.h:61
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition libcoap.h:53
coap_opt_filter_t
Definition coap_option.h:101
coap_opt_filter_t::mask
uint16_t mask
Definition coap_option.h:102
coap_opt_filter_t::short_opts
uint8_t short_opts[COAP_OPT_FILTER_SHORT]
Definition coap_option.h:104
coap_opt_filter_t::long_opts
uint16_t long_opts[COAP_OPT_FILTER_LONG]
Definition coap_option.h:103
coap_opt_iterator_t
Iterator to run through PDU options.
Definition coap_option.h:168
coap_opt_iterator_t::next_option
coap_opt_t * next_option
pointer to the unparsed next option
Definition coap_option.h:173
coap_opt_iterator_t::filter
coap_opt_filter_t filter
option filter
Definition coap_option.h:174
coap_opt_iterator_t::bad
unsigned int bad
iterator object is ok if not set
Definition coap_option.h:171
coap_opt_iterator_t::length
size_t length
remaining length of PDU
Definition coap_option.h:169
coap_opt_iterator_t::filtered
unsigned int filtered
denotes whether or not filter is used
Definition coap_option.h:172
coap_opt_iterator_t::number
coap_option_num_t number
decoded option number
Definition coap_option.h:170
coap_option_t
Representation of CoAP options.
Definition coap_option.h:32
coap_option_t::value
const uint8_t * value
Definition coap_option.h:35
coap_option_t::delta
uint16_t delta
Definition coap_option.h:33
coap_option_t::length
size_t length
Definition coap_option.h:34
coap_optlist_t
Representation of chained list of CoAP options to install.
Definition coap_option.h:325
coap_optlist_t::number
uint16_t number
the option number (no delta coding)
Definition coap_option.h:327
coap_optlist_t::length
size_t length
the option value length
Definition coap_option.h:328
coap_optlist_t::data
uint8_t * data
the option data
Definition coap_option.h:329
coap_optlist_t::next
struct coap_optlist_t * next
next entry in the optlist chain
Definition coap_option.h:326
coap_pdu_t
structure for CoAP PDUs
Definition coap_pdu_internal.h:119