libcoap  4.3.0rc1
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 README for terms
7  * of use.
8  */
9 
15 #ifndef COAP_OPTION_H_
16 #define COAP_OPTION_H_
17 
18 #include "bits.h"
19 
24 typedef uint8_t coap_opt_t;
25 #define PCHAR(p) ((coap_opt_t *)(p))
26 
30 typedef struct {
31  uint16_t delta;
32  size_t length;
33  const uint8_t *value;
35 
48 size_t coap_opt_parse(const coap_opt_t *opt,
49  size_t length,
50  coap_option_t *result);
51 
61 size_t coap_opt_size(const coap_opt_t *opt);
62 
75 #define COAP_OPT_FILTER_SHORT 6
76 
83 #define COAP_OPT_FILTER_LONG 2
84 
85 /* Ensure that COAP_OPT_FILTER_SHORT and COAP_OPT_FILTER_LONG are set
86  * correctly. */
87 #if (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16)
88 #error COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be less or equal 16
89 #endif /* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */
90 
91 /*
92  * mask contains a bit vector that indicates which fields in the long_opts[]
93  * and subsequent short_opts[] are used. The first COAP_OPT_FILTER_LONG bits
94  * correspond to the long option types that are stored in long_opts[]
95  * elements. The next COAP_OPT_FILTER_SHORT bits correspond to the short
96  * option types that are stored in short_opts[].
97  */
98 typedef struct coap_opt_filter_t {
99  uint16_t mask;
103 
105 #define COAP_OPT_ALL NULL
106 
114  memset(f, 0, sizeof(coap_opt_filter_t));
115 }
116 
127 int coap_option_filter_set(coap_opt_filter_t *filter, uint16_t type);
128 
139 int coap_option_filter_unset(coap_opt_filter_t *filter, uint16_t type);
140 
151 int coap_option_filter_get(coap_opt_filter_t *filter, uint16_t type);
152 
169 typedef struct {
170  size_t length;
171  uint16_t type;
172  unsigned int bad:1;
173  unsigned int filtered:1;
177 
196  const coap_opt_filter_t *filter);
197 
215 
230  uint16_t type,
231  coap_opt_iterator_t *oi);
232 
247 size_t coap_opt_setheader(coap_opt_t *opt,
248  size_t maxlen,
249  uint16_t delta,
250  size_t length);
251 
261 size_t coap_opt_encode_size(uint16_t delta, size_t length);
262 
278 size_t coap_opt_encode(coap_opt_t *opt,
279  size_t n,
280  uint16_t delta,
281  const uint8_t *val,
282  size_t length);
283 
297 uint32_t coap_opt_length(const coap_opt_t *opt);
298 
308 const uint8_t *coap_opt_value(const coap_opt_t *opt);
309 
326 typedef struct coap_optlist_t {
328  uint16_t number;
329  size_t length;
330  uint8_t *data;
332 
343  size_t length,
344  const uint8_t *data);
345 
355 int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t** optlist_chain);
356 
368 int coap_insert_optlist(coap_optlist_t **optlist_chain,
369  coap_optlist_t *optlist);
370 
377 void coap_delete_optlist(coap_optlist_t *optlist_chain);
378 
394 coap_option_setb(coap_opt_filter_t *filter, uint16_t type) {
395  return coap_option_filter_set(filter, type) ? 1 : -1;
396 }
397 
411 coap_option_clrb(coap_opt_filter_t *filter, uint16_t type) {
412  return coap_option_filter_unset(filter, type) ? 1 : -1;
413 }
414 
428 coap_option_getb(coap_opt_filter_t *filter, uint16_t type) {
429  return coap_option_filter_get(filter, type);
430 }
431 
432 #endif /* COAP_OPTION_H_ */
Bit vector manipulation.
int coap_option_filter_set(coap_opt_filter_t *filter, uint16_t type)
Sets the corresponding entry for type in filter.
Definition: option.c:486
coap_optlist_t * coap_new_optlist(uint16_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
Definition: option.c:503
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:146
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: option.c:369
COAP_STATIC_INLINE void coap_option_filter_clear(coap_opt_filter_t *f)
Clears filter f.
Definition: option.h:113
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:209
void coap_delete_optlist(coap_optlist_t *optlist_chain)
Removes all entries from the optlist_chain, freeing off their memory usage.
Definition: option.c:581
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: option.c:348
int coap_option_filter_unset(coap_opt_filter_t *filter, uint16_t type)
Clears the corresponding entry for type in filter.
Definition: option.c:491
#define COAP_OPT_FILTER_SHORT
The number of option types below 256 that can be stored in an option filter.
Definition: option.h:75
struct coap_opt_filter_t coap_opt_filter_t
int coap_option_filter_get(coap_opt_filter_t *filter, uint16_t type)
Checks if type is contained in filter.
Definition: option.c:496
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: option.c:544
#define COAP_OPT_FILTER_LONG
The number of option types above 255 that can be stored in an option filter.
Definition: option.h:83
struct coap_optlist_t coap_optlist_t
Representation of chained list of CoAP options to install.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:246
coap_opt_t * coap_check_option(coap_pdu_t *pdu, uint16_t type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
Definition: option.c:196
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: option.c:110
int coap_insert_optlist(coap_optlist_t **optlist_chain, coap_optlist_t *optlist)
Adds optlist to the given optlist_chain.
Definition: option.c:560
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: option.c:289
#define COAP_DEPRECATED
Definition: libcoap.h:46
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
const uint32_t n
Definition: murmur3.c:56
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:35
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:281
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:24
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: option.h:411
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: option.h:428
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: option.h:394
uint16_t mask
Definition: option.h:99
uint8_t short_opts[COAP_OPT_FILTER_SHORT]
Definition: option.h:101
uint16_t long_opts[COAP_OPT_FILTER_LONG]
Definition: option.h:100
Iterator to run through PDU options.
Definition: option.h:169
coap_opt_t * next_option
pointer to the unparsed next option
Definition: option.h:174
uint16_t type
decoded option type
Definition: option.h:171
coap_opt_filter_t filter
option filter
Definition: option.h:175
unsigned int bad
iterator object is ok if not set
Definition: option.h:172
size_t length
remaining length of PDU
Definition: option.h:170
unsigned int filtered
denotes whether or not filter is used
Definition: option.h:173
Representation of CoAP options.
Definition: option.h:30
const uint8_t * value
Definition: option.h:33
uint16_t delta
Definition: option.h:31
size_t length
Definition: option.h:32
Representation of chained list of CoAP options to install.
Definition: option.h:326
uint16_t number
the option number (no delta coding)
Definition: option.h:328
size_t length
the option value length
Definition: option.h:329
uint8_t * data
the option data
Definition: option.h:330
struct coap_optlist_t * next
next entry in the optlist chain
Definition: option.h:327
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287