mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-25 16:27:15 +02:00
IDF release/v4.0 08219f3cf
This commit is contained in:
@ -12,8 +12,8 @@
|
||||
* @brief Helpers for handling options in CoAP PDUs
|
||||
*/
|
||||
|
||||
#ifndef _COAP_OPTION_H_
|
||||
#define _COAP_OPTION_H_
|
||||
#ifndef COAP_OPTION_H_
|
||||
#define COAP_OPTION_H_
|
||||
|
||||
#include "bits.h"
|
||||
#include "pdu.h"
|
||||
@ -22,14 +22,16 @@
|
||||
* 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 unsigned char coap_opt_t;
|
||||
typedef uint8_t coap_opt_t;
|
||||
#define PCHAR(p) ((coap_opt_t *)(p))
|
||||
|
||||
/** Representation of CoAP options. */
|
||||
/**
|
||||
* Representation of CoAP options.
|
||||
*/
|
||||
typedef struct {
|
||||
unsigned short delta;
|
||||
uint16_t delta;
|
||||
size_t length;
|
||||
unsigned char *value;
|
||||
const uint8_t *value;
|
||||
} coap_option_t;
|
||||
|
||||
/**
|
||||
@ -59,27 +61,9 @@ size_t coap_opt_parse(const coap_opt_t *opt,
|
||||
*/
|
||||
size_t coap_opt_size(const coap_opt_t *opt);
|
||||
|
||||
/** @deprecated { Use coap_opt_size() instead. } */
|
||||
#define COAP_OPT_SIZE(opt) coap_opt_size(opt)
|
||||
|
||||
/**
|
||||
* Calculates the beginning of the PDU's option section.
|
||||
*
|
||||
* @param pdu The PDU containing the options.
|
||||
* @return A pointer to the first option if available, or @c NULL otherwise.
|
||||
*/
|
||||
coap_opt_t *options_start(coap_pdu_t *pdu);
|
||||
|
||||
/**
|
||||
* Interprets @p opt as pointer to a CoAP option and advances to
|
||||
* the next byte past this option.
|
||||
* @hideinitializer
|
||||
*/
|
||||
#define options_next(opt) \
|
||||
((coap_opt_t *)((unsigned char *)(opt) + COAP_OPT_SIZE(opt)))
|
||||
|
||||
/**
|
||||
* @defgroup opt_filter Option Filters
|
||||
* API functions for access option filters
|
||||
* @{
|
||||
*/
|
||||
|
||||
@ -106,7 +90,7 @@ coap_opt_t *options_start(coap_pdu_t *pdu);
|
||||
#endif /* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */
|
||||
|
||||
/** The number of elements in coap_opt_filter_t. */
|
||||
#define COAP_OPT_FILTER_SIZE \
|
||||
#define COAP_OPT_FILTER_SIZE \
|
||||
(((COAP_OPT_FILTER_SHORT + 1) >> 1) + COAP_OPT_FILTER_LONG) +1
|
||||
|
||||
/**
|
||||
@ -142,7 +126,7 @@ typedef uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE];
|
||||
*
|
||||
* @param f The filter to clear.
|
||||
*/
|
||||
static inline void
|
||||
COAP_STATIC_INLINE void
|
||||
coap_option_filter_clear(coap_opt_filter_t f) {
|
||||
memset(f, 0, sizeof(coap_opt_filter_t));
|
||||
}
|
||||
@ -157,7 +141,7 @@ coap_option_filter_clear(coap_opt_filter_t f) {
|
||||
*
|
||||
* @return @c 1 if bit was set, @c 0 otherwise.
|
||||
*/
|
||||
int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type);
|
||||
int coap_option_filter_set(coap_opt_filter_t filter, uint16_t type);
|
||||
|
||||
/**
|
||||
* Clears the corresponding entry for @p type in @p filter. This
|
||||
@ -169,7 +153,7 @@ int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type);
|
||||
*
|
||||
* @return @c 1 if bit was set, @c 0 otherwise.
|
||||
*/
|
||||
int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type);
|
||||
int coap_option_filter_unset(coap_opt_filter_t filter, uint16_t type);
|
||||
|
||||
/**
|
||||
* Checks if @p type is contained in @p filter. This function returns
|
||||
@ -181,7 +165,7 @@ int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type);
|
||||
*
|
||||
* @return @c 1 if @p type was found, @c 0 otherwise, or @c -1 on error.
|
||||
*/
|
||||
int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type);
|
||||
int coap_option_filter_get(coap_opt_filter_t filter, uint16_t type);
|
||||
|
||||
/**
|
||||
* Sets the corresponding bit for @p type in @p filter. This function returns @c
|
||||
@ -195,8 +179,8 @@ int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type);
|
||||
*
|
||||
* @return @c 1 if bit was set, @c -1 otherwise.
|
||||
*/
|
||||
inline static int
|
||||
coap_option_setb(coap_opt_filter_t filter, unsigned short type) {
|
||||
COAP_STATIC_INLINE int
|
||||
coap_option_setb(coap_opt_filter_t filter, uint16_t type) {
|
||||
return coap_option_filter_set(filter, type) ? 1 : -1;
|
||||
}
|
||||
|
||||
@ -212,8 +196,8 @@ coap_option_setb(coap_opt_filter_t filter, unsigned short type) {
|
||||
*
|
||||
* @return @c 1 if bit was set, @c -1 otherwise.
|
||||
*/
|
||||
inline static int
|
||||
coap_option_clrb(coap_opt_filter_t filter, unsigned short type) {
|
||||
COAP_STATIC_INLINE int
|
||||
coap_option_clrb(coap_opt_filter_t filter, uint16_t type) {
|
||||
return coap_option_filter_unset(filter, type) ? 1 : -1;
|
||||
}
|
||||
|
||||
@ -229,8 +213,8 @@ coap_option_clrb(coap_opt_filter_t filter, unsigned short type) {
|
||||
*
|
||||
* @return @c 1 if bit was set, @c 0 if not, @c -1 on error.
|
||||
*/
|
||||
inline static int
|
||||
coap_option_getb(const coap_opt_filter_t filter, unsigned short type) {
|
||||
COAP_STATIC_INLINE int
|
||||
coap_option_getb(coap_opt_filter_t filter, uint16_t type) {
|
||||
return coap_option_filter_get(filter, type);
|
||||
}
|
||||
|
||||
@ -252,7 +236,7 @@ coap_option_getb(const coap_opt_filter_t filter, unsigned short type) {
|
||||
*/
|
||||
typedef struct {
|
||||
size_t length; /**< remaining length of PDU */
|
||||
unsigned short type; /**< decoded option type */
|
||||
uint16_t type; /**< decoded option type */
|
||||
unsigned int bad:1; /**< iterator object is ok if not set */
|
||||
unsigned int filtered:1; /**< denotes whether or not filter is used */
|
||||
coap_opt_t *next_option; /**< pointer to the unparsed next option */
|
||||
@ -275,7 +259,7 @@ typedef struct {
|
||||
*
|
||||
* @return The iterator object @p oi on success, @c NULL otherwise.
|
||||
*/
|
||||
coap_opt_iterator_t *coap_option_iterator_init(coap_pdu_t *pdu,
|
||||
coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *pdu,
|
||||
coap_opt_iterator_t *oi,
|
||||
const coap_opt_filter_t filter);
|
||||
|
||||
@ -311,7 +295,7 @@ coap_opt_t *coap_option_next(coap_opt_iterator_t *oi);
|
||||
* not found.
|
||||
*/
|
||||
coap_opt_t *coap_check_option(coap_pdu_t *pdu,
|
||||
unsigned short type,
|
||||
uint16_t type,
|
||||
coap_opt_iterator_t *oi);
|
||||
|
||||
/**
|
||||
@ -330,9 +314,20 @@ coap_opt_t *coap_check_option(coap_pdu_t *pdu,
|
||||
*/
|
||||
size_t coap_opt_setheader(coap_opt_t *opt,
|
||||
size_t maxlen,
|
||||
unsigned short delta,
|
||||
uint16_t delta,
|
||||
size_t length);
|
||||
|
||||
/**
|
||||
* 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 coap_opt_encode_size(uint16_t delta, size_t length);
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -350,8 +345,8 @@ size_t coap_opt_setheader(coap_opt_t *opt,
|
||||
*/
|
||||
size_t coap_opt_encode(coap_opt_t *opt,
|
||||
size_t n,
|
||||
unsigned short delta,
|
||||
const unsigned char *val,
|
||||
uint16_t delta,
|
||||
const uint8_t *val,
|
||||
size_t length);
|
||||
|
||||
/**
|
||||
@ -364,14 +359,7 @@ size_t coap_opt_encode(coap_opt_t *opt,
|
||||
*
|
||||
* @return The number of bytes read or @c 0 on error.
|
||||
*/
|
||||
unsigned short coap_opt_delta(const coap_opt_t *opt);
|
||||
|
||||
/** @deprecated { Use coap_opt_delta() instead. } */
|
||||
#define COAP_OPT_DELTA(opt) coap_opt_delta(opt)
|
||||
|
||||
/** @deprecated { Use coap_opt_encode() instead. } */
|
||||
#define COAP_OPT_SETDELTA(opt,val) \
|
||||
coap_opt_encode((opt), COAP_MAX_PDU_SIZE, (val), NULL, 0)
|
||||
uint16_t coap_opt_delta(const coap_opt_t *opt);
|
||||
|
||||
/**
|
||||
* Returns the length of the given option. @p opt must point to an option jump
|
||||
@ -386,10 +374,7 @@ unsigned short coap_opt_delta(const coap_opt_t *opt);
|
||||
*
|
||||
* @return The option's length or @c 0 when undefined.
|
||||
*/
|
||||
unsigned short coap_opt_length(const coap_opt_t *opt);
|
||||
|
||||
/** @deprecated { Use coap_opt_length() instead. } */
|
||||
#define COAP_OPT_LENGTH(opt) coap_opt_length(opt)
|
||||
uint16_t coap_opt_length(const coap_opt_t *opt);
|
||||
|
||||
/**
|
||||
* Returns a pointer to the value of the given option. @p opt must point to an
|
||||
@ -400,11 +385,77 @@ unsigned short coap_opt_length(const coap_opt_t *opt);
|
||||
*
|
||||
* @return A pointer to the option value or @c NULL on error.
|
||||
*/
|
||||
unsigned char *coap_opt_value(coap_opt_t *opt);
|
||||
|
||||
/** @deprecated { Use coap_opt_value() instead. } */
|
||||
#define COAP_OPT_VALUE(opt) coap_opt_value((coap_opt_t *)opt)
|
||||
const uint8_t *coap_opt_value(const coap_opt_t *opt);
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif /* _OPTION_H_ */
|
||||
/**
|
||||
* 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 {
|
||||
struct coap_optlist_t *next; /**< next entry in the optlist chain */
|
||||
uint16_t number; /**< the option number (no delta coding) */
|
||||
size_t length; /**< the option value length */
|
||||
uint8_t *data; /**< the option data */
|
||||
} coap_optlist_t;
|
||||
|
||||
/**
|
||||
* Create a new optlist entry.
|
||||
*
|
||||
* @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 *coap_new_optlist(uint16_t number,
|
||||
size_t length,
|
||||
const uint8_t *data);
|
||||
|
||||
/**
|
||||
* 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 coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t** optlist_chain);
|
||||
|
||||
/**
|
||||
* 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 coap_insert_optlist(coap_optlist_t **optlist_chain,
|
||||
coap_optlist_t *optlist);
|
||||
|
||||
/**
|
||||
* 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 coap_delete_optlist(coap_optlist_t *optlist_chain);
|
||||
|
||||
#endif /* COAP_OPTION_H_ */
|
||||
|
Reference in New Issue
Block a user