xref: /btstack/src/btstack_util.h (revision 7dc86dfd3569d69491d87d64749fd45afb46c67a)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  btstack_util.h
40  *
41  *  General utility functions
42  *
43  *  Created by Matthias Ringwald on 7/23/09.
44  */
45 
46 #ifndef __BTSTACK_UTIL_H
47 #define __BTSTACK_UTIL_H
48 
49 
50 #if defined __cplusplus
51 extern "C" {
52 #endif
53 
54 #include <stdint.h>
55 #include <string.h>
56 
57 #include "bluetooth.h"
58 #include "btstack_defines.h"
59 #include "btstack_linked_list.h"
60 
61 // hack: compilation with the android ndk causes an error as there's a reverse_64 macro
62 #ifdef reverse_64
63 #undef reverse_64
64 #endif
65 
66 // will be moved to daemon/btstack_device_name_db.h
67 
68 
69 /**
70  * @brief The device name type
71  */
72 #define DEVICE_NAME_LEN 248
73 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
74 
75 /* API_START */
76 
77 /**
78  * @brief Minimum function for uint32_t
79  * @param a
80  * @param b
81  * @return value
82  */
83 uint32_t btstack_min(uint32_t a, uint32_t b);
84 
85 /**
86  * @brief Maximum function for uint32_t
87  * @param a
88  * @param b
89  * @return value
90  */
91 uint32_t btstack_max(uint32_t a, uint32_t b);
92 
93 
94 /**
95  * @brief Read 16/24/32 bit little endian value from buffer
96  * @param buffer
97  * @param position in buffer
98  * @return value
99  */
100 uint16_t little_endian_read_16(const uint8_t * buffer, int position);
101 uint32_t little_endian_read_24(const uint8_t * buffer, int position);
102 uint32_t little_endian_read_32(const uint8_t * buffer, int position);
103 
104 /**
105  * @brief Write 16/32 bit little endian value into buffer
106  * @param buffer
107  * @param position in buffer
108  * @param value
109  */
110 void little_endian_store_16(uint8_t *buffer, uint16_t position, uint16_t value);
111 void little_endian_store_32(uint8_t *buffer, uint16_t position, uint32_t value);
112 
113 /**
114  * @brief Read 16/24/32 bit big endian value from buffer
115  * @param buffer
116  * @param position in buffer
117  * @return value
118  */
119 uint32_t big_endian_read_16( const uint8_t * buffer, int pos);
120 uint32_t big_endian_read_24( const uint8_t * buffer, int pos);
121 uint32_t big_endian_read_32( const uint8_t * buffer, int pos);
122 
123 /**
124  * @brief Write 16/32 bit big endian value into buffer
125  * @param buffer
126  * @param position in buffer
127  * @param value
128  */
129 void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
130 void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value);
131 void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
132 
133 
134 /**
135  * @brief Swap bytes in 16 bit integer
136  */
137 static inline uint16_t btstack_flip_16(uint16_t value){
138     return (uint16_t)((value & 0xff) << 8) | (value >> 8);
139 }
140 
141 /**
142  * @brief Check for big endian system
143  * @returns 1 if on big endian
144  */
145 static inline int btstack_is_big_endian(void){
146 	uint16_t sample = 0x0100;
147 	return *(uint8_t*) &sample;
148 }
149 
150 /**
151  * @brief Check for little endian system
152  * @returns 1 if on little endian
153  */
154 static inline int btstack_is_little_endian(void){
155 	uint16_t sample = 0x0001;
156 	return *(uint8_t*) &sample;
157 }
158 
159 /**
160  * @brief Copy from source to destination and reverse byte order
161  * @param src
162  * @param dest
163  * @param len
164  */
165 void reverse_bytes  (const uint8_t *src, uint8_t * dest, int len);
166 
167 /**
168  * @brief Wrapper around reverse_bytes for common buffer sizes
169  * @param src
170  * @param dest
171  */
172 void reverse_24 (const uint8_t *src, uint8_t * dest);
173 void reverse_48 (const uint8_t *src, uint8_t * dest);
174 void reverse_56 (const uint8_t *src, uint8_t * dest);
175 void reverse_64 (const uint8_t *src, uint8_t * dest);
176 void reverse_128(const uint8_t *src, uint8_t * dest);
177 void reverse_256(const uint8_t *src, uint8_t * dest);
178 
179 void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest);
180 
181 /**
182  * @brief ASCII character for 4-bit nibble
183  * @return character
184  */
185 char char_for_nibble(int nibble);
186 
187 /**
188  * @brif 4-bit nibble from ASCII character
189  * @return value
190  */
191 int nibble_for_char(char c);
192 
193 /**
194  * @brief Compare two Bluetooth addresses
195  * @param a
196  * @param b
197  * @return true if equal
198  */
199 int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b);
200 
201 /**
202  * @brief Copy Bluetooth address
203  * @param dest
204  * @param src
205  */
206 void bd_addr_copy(bd_addr_t dest, const bd_addr_t src);
207 
208 /**
209  * @brief Use printf to write hexdump as single line of data
210  */
211 void printf_hexdump(const void *data, int size);
212 
213 /**
214  * @brief Create human readable representation for UUID128
215  * @note uses fixed global buffer
216  * @return pointer to UUID128 string
217  */
218 char * uuid128_to_str(const uint8_t * uuid);
219 
220 /**
221  * @brief Create human readable represenationt of Bluetooth address
222  * @note uses fixed global buffer
223  * @return pointer to Bluetooth address string
224  */
225 char * bd_addr_to_str(const bd_addr_t addr);
226 
227 /**
228  * @brief Parse Bluetooth address
229  * @param address_string
230  * @param buffer for parsed address
231  * @return 1 if string was parsed successfully
232  */
233 int sscanf_bd_addr(const char * addr_string, bd_addr_t addr);
234 
235 /**
236  * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID
237  * @param uuid128 output buffer
238  * @param short_uuid
239  */
240 void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid);
241 
242 /**
243  * @brief Checks if UUID128 has Bluetooth base UUID prefix
244  * @param uui128 to test
245  * @return 1 if it can be expressed as UUID32
246  */
247 int  uuid_has_bluetooth_prefix(const uint8_t * uuid128);
248 
249 /**
250  * @brief Parse unsigned number
251  * @param str to parse
252  * @return value
253  */
254 uint32_t btstack_atoi(const char *str);
255 
256 /**
257  * @brief Return number of digits of a uint32 number
258  * @param uint32_number
259  * @return num_digits
260  */
261 int string_len_for_uint32(uint32_t i);
262 
263 /**
264  * @brief Return number of set bits in a uint32 number
265  * @param uint32_number
266  * @return num_set_bits
267  */
268 int count_set_bits_uint32(uint32_t x);
269 
270 /**
271  * CRC8 functions using ETSI TS 101 369 V6.3.0.
272  * Only used by RFCOMM
273  */
274 uint8_t btstack_crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
275 uint8_t btstack_crc8_calc(uint8_t *data, uint16_t len);
276 
277 /* API_END */
278 
279 #if defined __cplusplus
280 }
281 #endif
282 
283 #endif // __BTSTACK_UTIL_H
284