Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / encode.h
1 /* encode.h -- encoding and decoding of CoAP data types
2  *
3  * Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 #ifndef _COAP_ENCODE_H_
10 #define _COAP_ENCODE_H_
11
12 #if (BSD >= 199103) || defined(WITH_CONTIKI)
13 # include <string.h>
14 #else
15 #ifndef WITH_ARDUINO
16 # include <strings.h>
17 #endif
18 #endif
19
20 #include "option.h"
21
22 #define Nn 8  /* duplicate definition of N if built on sky motes */
23 #define E 4
24 #define HIBIT (1 << (Nn - 1))
25 #define EMASK ((1 << E) - 1)
26 #define MMASK ((1 << Nn) - 1 - EMASK)
27 #define MAX_VALUE ( (1 << Nn) - (1 << E) ) * (1 << ((1 << E) - 1))
28
29 #define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
30
31 #ifndef HAVE_FLS
32 /* include this only if fls() is not available */
33 extern int coap_fls(unsigned int i);
34 #else
35 #define coap_fls(i) fls(i)
36 #endif
37
38 /* ls and s must be integer variables */
39 #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls)
40 #define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (s = (((v + ((1<<E<<ls)-1)) >> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls))
41
42 /**
43  * Decodes multiple-length byte sequences. buf points to an input byte
44  * sequence of length len. Returns the decoded value.
45  */
46 unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len);
47
48 /**
49  * Encodes multiple-length byte sequences. buf points to an output
50  * buffer of sufficient length to store the encoded bytes. val is
51  * the value to encode. Returns the number of bytes used to encode
52  * val or 0 on error.
53  */
54 unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val);
55
56 /**
57  * Tests whether the option definition has a type that allows variable byte encoding.
58  * Returns true when supported, false when not supported.
59  */
60 bool coap_is_var_bytes(coap_option_def_t* def);
61
62 #endif /* _COAP_ENCODE_H_ */