libcoap  4.1.2
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 #include "pdu.h"
20 
25 typedef unsigned char coap_opt_t;
26 #define PCHAR(p) ((coap_opt_t *)(p))
27 
29 typedef struct {
30  unsigned short delta;
31  size_t length;
32  unsigned char *value;
34 
47 size_t coap_opt_parse(const coap_opt_t *opt,
48  size_t length,
49  coap_option_t *result);
50 
60 size_t coap_opt_size(const coap_opt_t *opt);
61 
63 #define COAP_OPT_SIZE(opt) coap_opt_size(opt)
64 
72 
78 #define options_next(opt) \
79  ((coap_opt_t *)((unsigned char *)(opt) + COAP_OPT_SIZE(opt)))
80 
92 #define COAP_OPT_FILTER_SHORT 6
93 
100 #define COAP_OPT_FILTER_LONG 2
101 
102 /* Ensure that COAP_OPT_FILTER_SHORT and COAP_OPT_FILTER_LONG are set
103  * correctly. */
104 #if (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16)
105 #error COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be less or equal 16
106 #endif /* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */
107 
109 #define COAP_OPT_FILTER_SIZE \
110  (((COAP_OPT_FILTER_SHORT + 1) >> 1) + COAP_OPT_FILTER_LONG) +1
111 
136 
138 #define COAP_OPT_ALL NULL
139 
145 static inline void
147  memset(f, 0, sizeof(coap_opt_filter_t));
148 }
149 
160 int coap_option_filter_set(coap_opt_filter_t filter, unsigned short type);
161 
172 int coap_option_filter_unset(coap_opt_filter_t filter, unsigned short type);
173 
184 int coap_option_filter_get(const coap_opt_filter_t filter, unsigned short type);
185 
198 inline static int
199 coap_option_setb(coap_opt_filter_t filter, unsigned short type) {
200  return coap_option_filter_set(filter, type) ? 1 : -1;
201 }
202 
215 inline static int
216 coap_option_clrb(coap_opt_filter_t filter, unsigned short type) {
217  return coap_option_filter_unset(filter, type) ? 1 : -1;
218 }
219 
232 inline static int
233 coap_option_getb(const coap_opt_filter_t filter, unsigned short type) {
234  return coap_option_filter_get(filter, type);
235 }
236 
253 typedef struct {
254  size_t length;
255  unsigned short type;
256  unsigned int bad:1;
257  unsigned int filtered:1;
261 
280  const coap_opt_filter_t filter);
281 
299 
314  unsigned short type,
315  coap_opt_iterator_t *oi);
316 
331 size_t coap_opt_setheader(coap_opt_t *opt,
332  size_t maxlen,
333  unsigned short delta,
334  size_t length);
335 
351 size_t coap_opt_encode(coap_opt_t *opt,
352  size_t n,
353  unsigned short delta,
354  const unsigned char *val,
355  size_t length);
356 
367 unsigned short coap_opt_delta(const coap_opt_t *opt);
368 
370 #define COAP_OPT_DELTA(opt) coap_opt_delta(opt)
371 
373 #define COAP_OPT_SETDELTA(opt,val) \
374  coap_opt_encode((opt), COAP_MAX_PDU_SIZE, (val), NULL, 0)
375 
389 unsigned short coap_opt_length(const coap_opt_t *opt);
390 
392 #define COAP_OPT_LENGTH(opt) coap_opt_length(opt)
393 
403 unsigned char *coap_opt_value(coap_opt_t *opt);
404 
406 #define COAP_OPT_VALUE(opt) coap_opt_value((coap_opt_t *)opt)
407 
410 #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:321
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
unsigned short delta
Definition: option.h:30
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
unsigned char * coap_opt_value(coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:286
coap_opt_t * options_start(coap_pdu_t *pdu)
Calculates the beginning of the PDU&#39;s option section.
Definition: option.c:25
size_t length
remaining length of PDU
Definition: option.h:254
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_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_filter_t filter
option filter
Definition: option.h:259
#define COAP_OPT_FILTER_SIZE
The number of elements in coap_opt_filter_t.
Definition: option.h:109
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
Header structure for CoAP PDUs.
Definition: pdu.h:227
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
size_t length
Definition: option.h:31
Representation of CoAP options.
Definition: option.h:29
coap_opt_t * next_option
pointer to the unparsed next option
Definition: option.h:258
unsigned short coap_opt_delta(const coap_opt_t *opt)
Decodes the delta value of the next option.
Definition: option.c:222
Iterator to run through PDU options.
Definition: option.h:253
Pre-defined constants that reflect defaults for CoAP.
unsigned short type
decoded option type
Definition: option.h:255
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:146
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
Bit vector manipulation.
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
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_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
unsigned char * value
Definition: option.h:32
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
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:251
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
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