libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
option.h
Go to the documentation of this file.
1 /*
2  * option.h -- helpers for handling options in CoAP PDUs
3  *
4  * Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see
7  * README for terms of use.
8  */
9 
15 #ifndef _OPTION_H_
16 #define _OPTION_H_
17 
18 #include "bits.h"
19 #include "pdu.h"
20 
26 typedef unsigned char coap_opt_t;
27 #define PCHAR(p) ((coap_opt_t *)(p))
28 
30 typedef struct {
31  unsigned short delta;
32  size_t length;
33  unsigned char *value;
35 
49 size_t coap_opt_parse(const coap_opt_t *opt, size_t length,
50  coap_option_t *result);
51 
62 size_t coap_opt_size(const coap_opt_t *opt);
63 
65 #define COAP_OPT_SIZE(opt) coap_opt_size(opt)
66 
74 
80 #define options_next(opt) \
81  ((coap_opt_t *)((unsigned char *)(opt) + COAP_OPT_SIZE(opt)))
82 
93 typedef unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3) + 1];
94 
96 #define COAP_OPT_ALL NULL
97 
103 static inline void
105  memset(f, 0, sizeof(coap_opt_filter_t));
106 }
107 
118 inline static int
119 coap_option_setb(coap_opt_filter_t filter, unsigned short type) {
120  return bits_setb((uint8_t *)filter, sizeof(coap_opt_filter_t), type);
121 }
122 
133 inline static int
134 coap_option_clrb(coap_opt_filter_t filter, unsigned short type) {
135  return bits_clrb((uint8_t *)filter, sizeof(coap_opt_filter_t), type);
136 }
137 
148 inline static int
149 coap_option_getb(const coap_opt_filter_t filter, unsigned short type) {
150  return bits_getb((uint8_t *)filter, sizeof(coap_opt_filter_t), type);
151 }
152 
169 typedef struct {
170  size_t length;
171  unsigned short type;
172  unsigned int bad:1;
173  unsigned int filtered:1;
177 
196  coap_opt_iterator_t *oi, const coap_opt_filter_t filter);
197 
218 
234  unsigned char type,
235  coap_opt_iterator_t *oi);
236 
251 size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen,
252  unsigned short delta, size_t length);
253 
268 size_t coap_opt_encode(coap_opt_t *opt, size_t n, unsigned short delta,
269  const unsigned char *val, size_t length);
270 
280 unsigned short coap_opt_delta(const coap_opt_t *opt);
281 
283 #define COAP_OPT_DELTA(opt) coap_opt_delta(opt)
284 
286 #define COAP_OPT_SETDELTA(opt,val) \
287  coap_opt_encode((opt), COAP_MAX_PDU_SIZE, (val), NULL, 0)
288 
302 unsigned short coap_opt_length(const coap_opt_t *opt);
303 
305 #define COAP_OPT_LENGTH(opt) coap_opt_length(opt)
306 
315 unsigned char *coap_opt_value(coap_opt_t *opt);
316 
318 #define COAP_OPT_VALUE(opt) coap_opt_value((coap_opt_t *)opt)
319 
322 #endif /* _OPTION_H_ */
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:319
unsigned short delta
Definition: option.h:31
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:38
unsigned char * coap_opt_value(coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:284
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned char type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:207
coap_opt_t * options_start(coap_pdu_t *pdu)
Calculates the beginning of the PDU's option section.
Definition: option.c:24
size_t length
remaining length of PDU
Definition: option.h:170
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:149
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:157
coap_opt_filter_t filter
option filter
Definition: option.h:175
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:119
static int bits_setb(uint8_t *vec, size_t size, uint8_t bit)
Sets the bit bit in bit-vector vec.
Definition: bits.h:33
Header structure for CoAP PDUs.
Definition: pdu.h:206
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...
Definition: option.c:118
size_t length
Definition: option.h:32
Representation of CoAP options.
Definition: option.h:30
coap_opt_t * next_option
pointer to the unparsed next option
Definition: option.h:174
unsigned short coap_opt_delta(const coap_opt_t *opt)
Decodes the delta value of the next option.
Definition: option.c:220
Iterator to run through PDU options.
Definition: option.h:169
unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3)+1]
Fixed-size bit-vector we use for option filtering.
Definition: option.h:93
unsigned short type
decoded option type
Definition: option.h:171
static int bits_getb(const uint8_t *vec, size_t size, uint8_t bit)
Gets the status of bit bit from bit-vector vec.
Definition: bits.h:72
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:104
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:327
bit vector manipulation
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:26
static int bits_clrb(uint8_t *vec, size_t size, uint8_t bit)
Clears the bit bit from bit-vector vec.
Definition: bits.h:53
#define COAP_MAX_OPT
the highest option number we know
Definition: pdu.h:84
unsigned char * value
Definition: option.h:33
static int coap_option_clrb(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding bit for type in filter.
Definition: option.h:134
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:249
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:382