libcoap  4.2.1
encode.h
Go to the documentation of this file.
1 /*
2  * encode.h -- encoding and decoding of CoAP data types
3  *
4  * Copyright (C) 2010-2012 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 
10 #ifndef COAP_ENCODE_H_
11 #define COAP_ENCODE_H_
12 
13 #if (BSD >= 199103) || defined(WITH_CONTIKI) || defined(_WIN32)
14 # include <string.h>
15 #else
16 # include <strings.h>
17 #endif
18 
19 #include <stdint.h>
20 
21 #define Nn 8 /* duplicate definition of N if built on sky motes */
22 #define ENCODE_HEADER_SIZE 4
23 #define HIBIT (1 << (Nn - 1))
24 #define EMASK ((1 << ENCODE_HEADER_SIZE) - 1)
25 #define MMASK ((1 << Nn) - 1 - EMASK)
26 #define MAX_VALUE ( (1 << Nn) - (1 << ENCODE_HEADER_SIZE) ) * (1 << ((1 << ENCODE_HEADER_SIZE) - 1))
27 
28 #define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
29 
30 #ifndef HAVE_FLS
31 /* include this only if fls() is not available */
32 extern int coap_fls(unsigned int i);
33 #else
34 #define coap_fls(i) fls(i)
35 #endif
36 
37 #ifndef HAVE_FLSLL
38  /* include this only if flsll() is not available */
39 extern int coap_flsll(long long i);
40 #else
41 #define coap_flsll(i) flsll(i)
42 #endif
43 
44 /* ls and s must be integer variables */
45 #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls)
46 #define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (s = (((v + ((1<<ENCODE_HEADER_SIZE<<ls)-1)) >> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls))
47 
57 unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int length);
58 
68 uint64_t coap_decode_var_bytes8(const uint8_t *buf, unsigned int length);
69 
82 unsigned int coap_encode_var_safe(uint8_t *buf,
83  size_t length,
84  unsigned int value);
85 
98 unsigned int coap_encode_var_safe8(uint8_t *buf,
99  size_t length,
100  uint64_t value);
101 
118 coap_encode_var_bytes(uint8_t *buf, unsigned int value
119 ) {
120  return (int)coap_encode_var_safe(buf, sizeof(value), value);
121 }
122 
123 #endif /* COAP_ENCODE_H_ */
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int value)
Encodes multiple-length byte sequences.
Definition: encode.c:38
int coap_fls(unsigned int i)
Definition: encode.c:13
COAP_STATIC_INLINE COAP_DEPRECATED int coap_encode_var_bytes(uint8_t *buf, unsigned int value)
Definition: encode.h:118
unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int length)
Decodes multiple-length byte sequences.
Definition: encode.c:29
uint64_t coap_decode_var_bytes8(const uint8_t *buf, unsigned int length)
Decodes multiple-length byte sequences.
Definition: encode.c:58
#define COAP_DEPRECATED
Definition: libcoap.h:46
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t value)
Encodes multiple-length byte sequences.
Definition: encode.c:68
unsigned char uint8_t
Definition: uthash.h:79
int coap_flsll(long long i)
Definition: encode.c:19