xref: /btstack/src/btstack_util.h (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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  * @brief The device name type
70  */
71 #define DEVICE_NAME_LEN 248
72 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
73 
74 
75 /**
76  * @brief Minimum function for uint32_t
77  * @param a
78  * @param b
79  * @return value
80  */
81 uint32_t btstack_min(uint32_t a, uint32_t b);
82 
83 /**
84  * @brief Maximum function for uint32_t
85  * @param a
86  * @param b
87  * @return value
88  */
89 uint32_t btstack_max(uint32_t a, uint32_t b);
90 
91 
92 /**
93  * @brief Read 16/24/32 bit little endian value from buffer
94  * @param buffer
95  * @param position in buffer
96  * @return value
97  */
98 uint16_t little_endian_read_16(const uint8_t * buffer, int position);
99 uint32_t little_endian_read_24(const uint8_t * buffer, int position);
100 uint32_t little_endian_read_32(const uint8_t * buffer, int position);
101 
102 /**
103  * @brief Write 16/32 bit little endian value into buffer
104  * @param buffer
105  * @param position in buffer
106  * @param value
107  */
108 void little_endian_store_16(uint8_t *buffer, uint16_t position, uint16_t value);
109 void little_endian_store_32(uint8_t *buffer, uint16_t position, uint32_t value);
110 
111 /**
112  * @brief Read 16/24/32 bit big endian value from buffer
113  * @param buffer
114  * @param position in buffer
115  * @return value
116  */
117 uint32_t big_endian_read_16( const uint8_t * buffer, int pos);
118 uint32_t big_endian_read_32( const uint8_t * buffer, int pos);
119 
120 /**
121  * @brief Write 16/32 bit big endian value into buffer
122  * @param buffer
123  * @param position in buffer
124  * @param value
125  */
126 void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
127 void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
128 
129 /**
130  * @brief Copy from source to destination and reverse byte order
131  * @param src
132  * @param dest
133  * @param len
134  */
135 void reverse_bytes  (const uint8_t *src, uint8_t * dest, int len);
136 
137 /**
138  * @brief Wrapper around reverse_bytes for common buffer sizes
139  * @param src
140  * @param dest
141  */
142 void reverse_24 (const uint8_t *src, uint8_t * dest);
143 void reverse_48 (const uint8_t *src, uint8_t * dest);
144 void reverse_56 (const uint8_t *src, uint8_t * dest);
145 void reverse_64 (const uint8_t *src, uint8_t * dest);
146 void reverse_128(const uint8_t *src, uint8_t * dest);
147 void reverse_256(const uint8_t *src, uint8_t * dest);
148 
149 void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest);
150 
151 /**
152  * @brief ASCII character for 4-bit nibble
153  * @return character
154  */
155 char char_for_nibble(int nibble);
156 
157 /**
158  * @brif 4-bit nibble from ASCII character
159  * @return value
160  */
161 int nibble_for_char(char c);
162 
163 /**
164  * @brief Compare two Bluetooth addresses
165  * @param a
166  * @param b
167  * @return true if equal
168  */
169 int bd_addr_cmp(bd_addr_t a, bd_addr_t b);
170 
171 /**
172  * @brief Copy Bluetooth address
173  * @param dest
174  * @param src
175  */
176 void bd_addr_copy(bd_addr_t dest, bd_addr_t src);
177 
178 /**
179  * @brief Use printf to write hexdump as single line of data
180  */
181 void printf_hexdump(const void *data, int size);
182 
183 /**
184  * @brief Create human readable representation for UUID128
185  * @note uses fixed global buffer
186  * @return pointer to UUID128 string
187  */
188 char * uuid128_to_str(uint8_t * uuid);
189 
190 /**
191  * @brief Create human readable represenationt of Bluetooth address
192  * @note uses fixed global buffer
193  * @return pointer to Bluetooth address string
194  */
195 char * bd_addr_to_str(bd_addr_t addr);
196 
197 /**
198  * @brief Parse Bluetooth address
199  * @param address_string
200  * @param buffer for parsed address
201  * @return 1 if string was parsed successfully
202  */
203 int sscanf_bd_addr(const char * addr_string, bd_addr_t addr);
204 
205 /**
206  * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID
207  * @param uuid128 output buffer
208  * @param short_uuid
209  */
210 void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid);
211 
212 /**
213  * @brief Checks if UUID128 has Bluetooth base UUID prefix
214  * @param uui128 to test
215  * @return 1 if it can be expressed as UUID32
216  */
217 int  uuid_has_bluetooth_prefix(uint8_t * uuid128);
218 
219 #if defined __cplusplus
220 }
221 #endif
222 
223 #endif // __BTSTACK_UTIL_H
224