11 #include "coap_config.h" 13 #if defined(HAVE_ASSERT_H) && !defined(assert) 27 if (pdu && pdu->
hdr &&
29 < (
unsigned char *)pdu->
hdr + pdu->
length)) {
45 #define ADVANCE_OPT(o,e,step) if ((e) < step) { \ 46 debug("cannot advance opt past end\n"); \ 50 (o) = ((unsigned char *)(o)) + step; \ 56 result->
delta = (*opt & 0xf0) >> 4;
57 result->
length = *opt & 0x0f;
59 switch(result->
delta) {
62 debug(
"ignored reserved option delta 15\n");
70 result->
delta = ((*opt & 0xff) << 8) + 269;
71 if (result->
delta < 269) {
72 debug(
"delta too large\n");
78 result->
delta += *opt & 0xff;
87 debug(
"found reserved option length 15\n");
94 result->
length = ((*opt & 0xff) << 8) + 269;
98 result->
length += *opt & 0xff;
108 result->
value = (
unsigned char *)opt;
109 if (length < result->length) {
110 debug(
"invalid option length\n");
116 return (opt + result->
length) - opt_start;
180 assert(optsize <= oi->length);
225 n = (*opt++ & 0xf0) >> 4;
229 warn(
"coap_opt_delta: illegal option delta\n");
238 n = ((*opt++ & 0xff) << 8) + 269;
252 unsigned short length;
254 length = *opt & 0x0f;
255 switch (*opt & 0xf0) {
257 debug(
"illegal option delta\n");
271 debug(
"illegal option length\n");
274 length = (*opt++ << 8) + 269;
289 switch (*opt & 0xf0) {
291 debug(
"illegal option delta\n");
303 switch (*opt & 0x0f) {
305 debug(
"illegal option length\n");
317 return (
unsigned char *)opt + ofs;
330 unsigned short delta,
size_t length) {
340 }
else if (delta < 270) {
342 debug(
"insufficient space to encode option delta %d\n", delta);
347 opt[++skip] = delta - 13;
350 debug(
"insufficient space to encode option delta %d\n", delta);
355 opt[++skip] = ((delta - 269) >> 8) & 0xff;
356 opt[++skip] = (delta - 269) & 0xff;
360 opt[0] |= length & 0x0f;
361 }
else if (length < 270) {
362 if (maxlen < skip + 2) {
363 debug(
"insufficient space to encode option length %zu\n", length);
368 opt[++skip] = length - 13;
370 if (maxlen < skip + 3) {
371 debug(
"insufficient space to encode option delta %d\n", delta);
376 opt[++skip] = ((length - 269) >> 8) & 0xff;
377 opt[++skip] = (length - 269) & 0xff;
385 const unsigned char *val,
size_t length) {
392 debug(
"coap_opt_encode: cannot set option header\n");
399 if (maxlen < length) {
400 debug(
"coap_opt_encode: option too large for buffer\n");
405 memcpy(opt, val, length);
414 #define LONG_MASK ((1 << COAP_OPT_FILTER_LONG) - 1) 416 (~LONG_MASK & ((1 << (COAP_OPT_FILTER_LONG + COAP_OPT_FILTER_SHORT)) - 1)) 454 uint16_t nr, mask = 0;
461 if (((of->
mask & nr) > 0) && (of->
long_opts[index] == type)) {
475 if (((of->
mask & nr) > 0) && (of->
short_opts[index] == (type & 0xff))) {
503 of->
mask |= 1 << (index - 1);
int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding entry for type in filter.
#define COAP_OPT_FILTER_LONG
The number of option types above 255 that can be stored in an option filter.
unsigned char * coap_opt_value(coap_opt_t *opt)
Returns a pointer to the value of the given option.
unsigned short length
PDU length (including header, options, data)
size_t length
remaining length of PDU
filter_op_t
Operation specifiers for coap_filter_op().
static int coap_option_getb(const coap_opt_filter_t filter, unsigned short type)
Gets the corresponding bit for type in filter.
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
static int coap_option_filter_op(coap_opt_filter_t filter, unsigned short type, enum filter_op_t op)
Applies op on filter with respect to type.
coap_opt_filter_t filter
option filter
int coap_fls(unsigned int i)
Helpers for handling options in CoAP PDUs.
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
static int opt_finished(coap_opt_iterator_t *oi)
#define COAP_OPT_FILTER_SHORT
The number of option types below 256 that can be stored in an option filter.
Header structure for CoAP PDUs.
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's option list...
Representation of CoAP options.
coap_opt_t * next_option
pointer to the unparsed next option
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.
unsigned short coap_opt_delta(const coap_opt_t *opt)
Decodes the delta value of the next option.
Iterator to run through PDU options.
unsigned int bad
iterator object is ok if not set
unsigned int filtered
denotes whether or not filter is used
unsigned short type
decoded option type
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
#define COAP_PAYLOAD_START
#define ADVANCE_OPT(o, e, step)
unsigned int token_length
uint16_t long_opts[COAP_OPT_FILTER_LONG]
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.
uint8_t short_opts[COAP_OPT_FILTER_SHORT]
int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type)
Checks if type is contained in filter.
coap_opt_t * options_start(coap_pdu_t *pdu)
Calculates the beginning of the PDU's option section.
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
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.
size_t coap_opt_size(const coap_opt_t *opt)
Returns the size of the given option, taking into account a possible option jump. ...
static int is_long_option(unsigned short type)
Returns true iff type denotes an option type larger than 255.
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
size_t coap_opt_encode(coap_opt_t *opt, size_t maxlen, unsigned short delta, const unsigned char *val, size_t length)
Encodes option with given delta into opt.
int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding entry for type in filter.