option.h
Go to the documentation of this file.
/*
* option.h -- helpers for handling options in CoAP PDUs
*
* Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef _COAP_OPTION_H_
#define _COAP_OPTION_H_
#include "bits.h"
#include "pdu.h"
typedef unsigned char coap_opt_t;
#define PCHAR(p) ((coap_opt_t *)(p))
typedef struct {
unsigned short delta;
size_t length;
unsigned char *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_SIZE(opt) coap_opt_size(opt)
coap_opt_t *options_start(coap_pdu_t *pdu);
#define options_next(opt) \
((coap_opt_t *)((unsigned char *)(opt) + COAP_OPT_SIZE(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) */
#define COAP_OPT_FILTER_SIZE \
(((COAP_OPT_FILTER_SHORT + 1) >> 1) + COAP_OPT_FILTER_LONG) +1
typedef uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE];
#define COAP_OPT_ALL NULL
static inline void
coap_option_filter_clear(coap_opt_filter_t f) {
memset(f, 0, sizeof(coap_opt_filter_t));
}
int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type);
int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type);
int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type);
inline static int
coap_option_setb(coap_opt_filter_t filter, unsigned short type) {
return coap_option_filter_set(filter, type) ? 1 : -1;
}
inline static int
coap_option_clrb(coap_opt_filter_t filter, unsigned short type) {
return coap_option_filter_unset(filter, type) ? 1 : -1;
}
inline static int
coap_option_getb(const coap_opt_filter_t filter, unsigned short type) {
return coap_option_filter_get(filter, type);
}
typedef struct {
size_t length;
unsigned short type;
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(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(coap_pdu_t *pdu,
unsigned short type,
coap_opt_iterator_t *oi);
size_t coap_opt_setheader(coap_opt_t *opt,
size_t maxlen,
unsigned short delta,
size_t length);
size_t coap_opt_encode(coap_opt_t *opt,
size_t n,
unsigned short delta,
const unsigned char *val,
size_t length);
unsigned short coap_opt_delta(const coap_opt_t *opt);
#define COAP_OPT_DELTA(opt) coap_opt_delta(opt)
#define COAP_OPT_SETDELTA(opt,val) \
coap_opt_encode((opt), COAP_MAX_PDU_SIZE, (val), NULL, 0)
unsigned short coap_opt_length(const coap_opt_t *opt);
#define COAP_OPT_LENGTH(opt) coap_opt_length(opt)
unsigned char *coap_opt_value(coap_opt_t *opt);
#define COAP_OPT_VALUE(opt) coap_opt_value((coap_opt_t *)opt)
#endif /* _OPTION_H_ */
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: option.c:321
coap_option_filter_set
int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding entry for type in filter.
Definition: option.c:509
coap_option_t::delta
unsigned short delta
Definition: option.h:30
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: option.c:39
coap_opt_value
unsigned char * coap_opt_value(coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:286
options_start
coap_opt_t * options_start(coap_pdu_t *pdu)
Calculates the beginning of the PDU&#39;s option section.
Definition: option.c:25
coap_opt_iterator_t::length
size_t length
remaining length of PDU
Definition: option.h:254
coap_option_getb
static int coap_option_getb(const coap_opt_filter_t filter, unsigned short type)
Gets the corresponding bit for type in filter.
Definition: option.h:233
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: option.c:159
coap_opt_iterator_t::filter
coap_opt_filter_t filter
option filter
Definition: option.h:259
COAP_OPT_FILTER_SIZE
#define COAP_OPT_FILTER_SIZE
The number of elements in coap_opt_filter_t.
Definition: option.h:109
coap_option_setb
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:199
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_option_iterator_init
coap_opt_iterator_t * coap_option_iterator_init(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&#39;s option list...
Definition: option.c:120
coap_option_t::length
size_t length
Definition: option.h:31
coap_option_t
Representation of CoAP options.
Definition: option.h:29
coap_opt_iterator_t::next_option
coap_opt_t * next_option
pointer to the unparsed next option
Definition: option.h:258
coap_opt_delta
unsigned short coap_opt_delta(const coap_opt_t *opt)
Decodes the delta value of the next option.
Definition: option.c:222
coap_opt_iterator_t
Iterator to run through PDU options.
Definition: option.h:253
coap_opt_iterator_t::bad
unsigned int bad
iterator object is ok if not set
Definition: option.h:256
pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_opt_iterator_t::filtered
unsigned int filtered
denotes whether or not filter is used
Definition: option.h:257
coap_opt_iterator_t::type
unsigned short type
decoded option type
Definition: option.h:255
coap_opt_filter_t
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
coap_option_filter_clear
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:146
coap_opt_setheader
size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, unsigned short delta, size_t length)
Encodes the given delta and length values into opt.
Definition: option.c:329
bits.h
Bit vector manipulation.
coap_option_filter_get
int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type)
Checks if type is contained in filter.
Definition: option.c:519
coap_opt_t
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:25
coap_check_option
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned short type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:209
coap_option_t::value
unsigned char * value
Definition: option.h:32
coap_option_clrb
static int coap_option_clrb(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding bit for type in filter.
Definition: option.h:216
coap_opt_length
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:251
coap_opt_encode
size_t coap_opt_encode(coap_opt_t *opt, size_t n, unsigned short delta, const unsigned char *val, size_t length)
Encodes option with given delta into opt.
Definition: option.c:384
coap_option_filter_unset
int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding entry for type in filter.
Definition: option.c:514