ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
util.h
Go to the documentation of this file.
1 /*
2  *
3  * BlueZ - Bluetooth protocol stack for Linux
4  *
5  * Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
6  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <alloca.h>
27 #include <byteswap.h>
28 #include <string.h>
29 
30 #if __BYTE_ORDER == __LITTLE_ENDIAN
31 #define le16_to_cpu(val) (val)
32 #define le32_to_cpu(val) (val)
33 #define le64_to_cpu(val) (val)
34 #define cpu_to_le16(val) (val)
35 #define cpu_to_le32(val) (val)
36 #define cpu_to_le64(val) (val)
37 #define be16_to_cpu(val) bswap_16(val)
38 #define be32_to_cpu(val) bswap_32(val)
39 #define be64_to_cpu(val) bswap_64(val)
40 #define cpu_to_be16(val) bswap_16(val)
41 #define cpu_to_be32(val) bswap_32(val)
42 #define cpu_to_be64(val) bswap_64(val)
43 #elif __BYTE_ORDER == __BIG_ENDIAN
44 #define le16_to_cpu(val) bswap_16(val)
45 #define le32_to_cpu(val) bswap_32(val)
46 #define le64_to_cpu(val) bswap_64(val)
47 #define cpu_to_le16(val) bswap_16(val)
48 #define cpu_to_le32(val) bswap_32(val)
49 #define cpu_to_le64(val) bswap_64(val)
50 #define be16_to_cpu(val) (val)
51 #define be32_to_cpu(val) (val)
52 #define be64_to_cpu(val) (val)
53 #define cpu_to_be16(val) (val)
54 #define cpu_to_be32(val) (val)
55 #define cpu_to_be64(val) (val)
56 #else
57 #error "Unknown byte order"
58 #endif
59 
60 #define get_unaligned(ptr) \
61 __extension__ ({ \
62  struct __attribute__((packed)) { \
63  __typeof__(*(ptr)) __v; \
64  } *__p = (__typeof__(__p)) (ptr); \
65  __p->__v; \
66 })
67 
68 #define put_unaligned(val, ptr) \
69 do { \
70  struct __attribute__((packed)) { \
71  __typeof__(*(ptr)) __v; \
72  } *__p = (__typeof__(__p)) (ptr); \
73  __p->__v = (val); \
74 } while (0)
75 
76 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
77 #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
78 
79 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
80 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
81 
82 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
83 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
84 #define malloc0(n) (calloc((n), 1))
85 
86 typedef void (*util_debug_func_t)(const char *str, void *user_data);
87 
88 void util_debug(util_debug_func_t function, void *user_data,
89  const char *format, ...)
90  __attribute__((format(printf, 3, 4)));
91 
92 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
93  util_debug_func_t function, void *user_data);
94 
95 unsigned char util_get_dt(const char *parent, const char *name);
96 
97 uint8_t util_get_uid(unsigned int *bitmap, uint8_t max);
98 void util_clear_uid(unsigned int *bitmap, uint8_t id);
99 
100 static inline uint16_t get_le16(const void *ptr)
101 {
102  return le16_to_cpu(get_unaligned((const uint16_t *) ptr));
103 }
104 
105 static inline uint16_t get_be16(const void *ptr)
106 {
107  return be16_to_cpu(get_unaligned((const uint16_t *) ptr));
108 }
109 
110 static inline uint32_t get_le32(const void *ptr)
111 {
112  return le32_to_cpu(get_unaligned((const uint32_t *) ptr));
113 }
114 
115 static inline uint32_t get_be32(const void *ptr)
116 {
117  return be32_to_cpu(get_unaligned((const uint32_t *) ptr));
118 }
119 
120 static inline uint64_t get_le64(const void *ptr)
121 {
122  return le64_to_cpu(get_unaligned((const uint64_t *) ptr));
123 }
124 
125 static inline uint64_t get_be64(const void *ptr)
126 {
127  return be64_to_cpu(get_unaligned((const uint64_t *) ptr));
128 }
129 
130 static inline void put_le16(uint16_t val, void *dst)
131 {
132  put_unaligned(cpu_to_le16(val), (uint16_t *) dst);
133 }
134 
135 static inline void put_be16(uint16_t val, const void *ptr)
136 {
137  put_unaligned(cpu_to_be16(val), (uint16_t *) ptr);
138 }
139 
140 static inline void put_le32(uint32_t val, void *dst)
141 {
142  put_unaligned(cpu_to_le32(val), (uint32_t *) dst);
143 }
144 
145 static inline void put_be32(uint32_t val, void *dst)
146 {
147  put_unaligned(cpu_to_be32(val), (uint32_t *) dst);
148 }
149 
150 static inline void put_le64(uint64_t val, void *dst)
151 {
152  put_unaligned(cpu_to_le64(val), (uint64_t *) dst);
153 }
154 
155 static inline void put_be64(uint64_t val, void *dst)
156 {
157  put_unaligned(cpu_to_be64(val), (uint64_t *) dst);
158 }
#define get_unaligned(ptr)
Definition: util.h:60
void(* util_debug_func_t)(const char *str, void *user_data)
Definition: util.h:86
void util_clear_uid(unsigned int *bitmap, uint8_t id)
Definition: util.c:172
#define be16_to_cpu(val)
Definition: util.h:37
#define cpu_to_le16(val)
Definition: util.h:34
#define le32_to_cpu(val)
Definition: util.h:32
static uint32_t get_be32(const void *ptr)
Definition: util.h:115
#define le64_to_cpu(val)
Definition: util.h:33
void util_debug(util_debug_func_t function, void *user_data, const char *format,...) __attribute__((format(printf
#define be32_to_cpu(val)
Definition: util.h:38
static uint64_t get_le64(const void *ptr)
Definition: util.h:120
static void put_be32(uint32_t val, void *dst)
Definition: util.h:145
#define le16_to_cpu(val)
Definition: util.h:31
static void put_be16(uint16_t val, const void *ptr)
Definition: util.h:135
uint8_t util_get_uid(unsigned int *bitmap, uint8_t max)
Definition: util.c:152
static void put_le16(uint16_t val, void *dst)
Definition: util.h:130
#define cpu_to_be64(val)
Definition: util.h:42
static void put_be64(uint64_t val, void *dst)
Definition: util.h:155
static uint32_t get_le32(const void *ptr)
Definition: util.h:110
unsigned char util_get_dt(const char *parent, const char *name)
Definition: util.c:130
static void put_le32(uint32_t val, void *dst)
Definition: util.h:140
#define cpu_to_le32(val)
Definition: util.h:35
static uint16_t get_be16(const void *ptr)
Definition: util.h:105
static void put_le64(uint64_t val, void *dst)
Definition: util.h:150
#define cpu_to_le64(val)
Definition: util.h:36
void void util_hexdump(const char dir, const unsigned char *buf, size_t len, util_debug_func_t function, void *user_data)
Definition: util.c:81
#define put_unaligned(val, ptr)
Definition: util.h:68
#define cpu_to_be32(val)
Definition: util.h:41
#define be64_to_cpu(val)
Definition: util.h:39
#define cpu_to_be16(val)
Definition: util.h:40
static uint16_t get_le16(const void *ptr)
Definition: util.h:100
static uint64_t get_be64(const void *ptr)
Definition: util.h:125