b9b304e69a2b81f285fb2aa64dbe654f75586305
[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 #define Nn 8  /* duplicate definition of N if built on sky motes */
21 #define E 4
22 #define HIBIT (1 << (Nn - 1))
23 #define EMASK ((1 << E) - 1)
24 #define MMASK ((1 << Nn) - 1 - EMASK)
25 #define MAX_VALUE ( (1 << Nn) - (1 << E) ) * (1 << ((1 << E) - 1))
26
27 #define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
28
29 #ifndef HAVE_FLS
30 /* include this only if fls() is not available */
31 extern int coap_fls(unsigned int i);
32 #else
33 #define coap_fls(i) fls(i)
34 #endif
35
36 /* ls and s must be integer variables */
37 #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls)
38 #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))
39
40 /**
41  * Decodes multiple-length byte sequences. buf points to an input byte
42  * sequence of length len. Returns the decoded value.
43  */
44 unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len);
45
46 /**
47  * Encodes multiple-length byte sequences. buf points to an output
48  * buffer of sufficient length to store the encoded bytes. val is
49  * the value to encode. Returns the number of bytes used to encode
50  * val or 0 on error.
51  */
52 unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val);
53
54 #endif /* _COAP_ENCODE_H_ */