xref: /btstack/src/btstack_util.h (revision 5d067ab1c4e447692cc06fe2ee805514f636e938)
1eb886013SMatthias Ringwald /*
2eb886013SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3eb886013SMatthias Ringwald  *
4eb886013SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5eb886013SMatthias Ringwald  * modification, are permitted provided that the following conditions
6eb886013SMatthias Ringwald  * are met:
7eb886013SMatthias Ringwald  *
8eb886013SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9eb886013SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10eb886013SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11eb886013SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12eb886013SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13eb886013SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14eb886013SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15eb886013SMatthias Ringwald  *    from this software without specific prior written permission.
16eb886013SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17eb886013SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18eb886013SMatthias Ringwald  *    monetary gain.
19eb886013SMatthias Ringwald  *
20eb886013SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21eb886013SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22eb886013SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23eb886013SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24eb886013SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25eb886013SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26eb886013SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27eb886013SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28eb886013SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29eb886013SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30eb886013SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31eb886013SMatthias Ringwald  * SUCH DAMAGE.
32eb886013SMatthias Ringwald  *
33eb886013SMatthias Ringwald  * Please inquire about commercial licensing options at
34eb886013SMatthias Ringwald  * [email protected]
35eb886013SMatthias Ringwald  *
36eb886013SMatthias Ringwald  */
37eb886013SMatthias Ringwald 
38eb886013SMatthias Ringwald /*
39eb886013SMatthias Ringwald  *  btstack_util.h
40eb886013SMatthias Ringwald  *
41eb886013SMatthias Ringwald  *  General utility functions
42eb886013SMatthias Ringwald  *
43eb886013SMatthias Ringwald  *  Created by Matthias Ringwald on 7/23/09.
44eb886013SMatthias Ringwald  */
45eb886013SMatthias Ringwald 
46bf1b35bfSMatthias Ringwald #ifndef __BTSTACK_UTIL_H
47bf1b35bfSMatthias Ringwald #define __BTSTACK_UTIL_H
48eb886013SMatthias Ringwald 
49eb886013SMatthias Ringwald 
50eb886013SMatthias Ringwald #if defined __cplusplus
51eb886013SMatthias Ringwald extern "C" {
52eb886013SMatthias Ringwald #endif
53eb886013SMatthias Ringwald 
54eb886013SMatthias Ringwald #include <stdint.h>
55cbabbca4SMatthias Ringwald #include <string.h>
56cbabbca4SMatthias Ringwald 
578974fcd6SMatthias Ringwald #include "bluetooth.h"
588974fcd6SMatthias Ringwald #include "btstack_defines.h"
598d471f5cSMatthias Ringwald #include "btstack_linked_list.h"
60eb886013SMatthias Ringwald 
619c80e4ccSMatthias Ringwald // hack: compilation with the android ndk causes an error as there's a reverse_64 macro
629c80e4ccSMatthias Ringwald #ifdef reverse_64
639c80e4ccSMatthias Ringwald #undef reverse_64
6473988a59SMatthias Ringwald #endif
6573988a59SMatthias Ringwald 
668974fcd6SMatthias Ringwald // will be moved to daemon/btstack_device_name_db.h
67eb886013SMatthias Ringwald 
68eb886013SMatthias Ringwald /**
69eb886013SMatthias Ringwald  * @brief The device name type
70eb886013SMatthias Ringwald  */
71eb886013SMatthias Ringwald #define DEVICE_NAME_LEN 248
72eb886013SMatthias Ringwald typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
73eb886013SMatthias Ringwald 
74caed94dfSMatthias Ringwald 
75ebaeb1beSMatthias Ringwald /**
76ebaeb1beSMatthias Ringwald  * @brief Minimum function for uint32_t
77ebaeb1beSMatthias Ringwald  * @param a
78ebaeb1beSMatthias Ringwald  * @param b
79ebaeb1beSMatthias Ringwald  * @return value
80ebaeb1beSMatthias Ringwald  */
81ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b);
82ebaeb1beSMatthias Ringwald 
83ebaeb1beSMatthias Ringwald /**
84ebaeb1beSMatthias Ringwald  * @brief Maximum function for uint32_t
85ebaeb1beSMatthias Ringwald  * @param a
86ebaeb1beSMatthias Ringwald  * @param b
87ebaeb1beSMatthias Ringwald  * @return value
88ebaeb1beSMatthias Ringwald  */
89ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b);
90ebaeb1beSMatthias Ringwald 
91cbabbca4SMatthias Ringwald 
9273988a59SMatthias Ringwald /**
9373988a59SMatthias Ringwald  * @brief Read 16/24/32 bit little endian value from buffer
9473988a59SMatthias Ringwald  * @param buffer
9573988a59SMatthias Ringwald  * @param position in buffer
9673988a59SMatthias Ringwald  * @return value
9773988a59SMatthias Ringwald  */
9873988a59SMatthias Ringwald uint16_t little_endian_read_16(const uint8_t * buffer, int position);
9973988a59SMatthias Ringwald uint32_t little_endian_read_24(const uint8_t * buffer, int position);
10073988a59SMatthias Ringwald uint32_t little_endian_read_32(const uint8_t * buffer, int position);
101eb886013SMatthias Ringwald 
10273988a59SMatthias Ringwald /**
10373988a59SMatthias Ringwald  * @brief Write 16/32 bit little endian value into buffer
10473988a59SMatthias Ringwald  * @param buffer
10573988a59SMatthias Ringwald  * @param position in buffer
10673988a59SMatthias Ringwald  * @param value
10773988a59SMatthias Ringwald  */
10873988a59SMatthias Ringwald void little_endian_store_16(uint8_t *buffer, uint16_t position, uint16_t value);
10973988a59SMatthias Ringwald void little_endian_store_32(uint8_t *buffer, uint16_t position, uint32_t value);
11073988a59SMatthias Ringwald 
11173988a59SMatthias Ringwald /**
11273988a59SMatthias Ringwald  * @brief Read 16/24/32 bit big endian value from buffer
11373988a59SMatthias Ringwald  * @param buffer
11473988a59SMatthias Ringwald  * @param position in buffer
11573988a59SMatthias Ringwald  * @return value
11673988a59SMatthias Ringwald  */
11773988a59SMatthias Ringwald uint32_t big_endian_read_16( const uint8_t * buffer, int pos);
11873988a59SMatthias Ringwald uint32_t big_endian_read_32( const uint8_t * buffer, int pos);
11973988a59SMatthias Ringwald 
12073988a59SMatthias Ringwald /**
12173988a59SMatthias Ringwald  * @brief Write 16/32 bit big endian value into buffer
12273988a59SMatthias Ringwald  * @param buffer
12373988a59SMatthias Ringwald  * @param position in buffer
12473988a59SMatthias Ringwald  * @param value
12573988a59SMatthias Ringwald  */
126f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
127f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
128eb886013SMatthias Ringwald 
129caed94dfSMatthias Ringwald /**
130caed94dfSMatthias Ringwald  * @brief Copy from source to destination and reverse byte order
1319c80e4ccSMatthias Ringwald  * @param src
1329c80e4ccSMatthias Ringwald  * @param dest
1339c80e4ccSMatthias Ringwald  * @param len
134caed94dfSMatthias Ringwald  */
1359c80e4ccSMatthias Ringwald void reverse_bytes  (const uint8_t *src, uint8_t * dest, int len);
1369c80e4ccSMatthias Ringwald 
1379c80e4ccSMatthias Ringwald /**
1389c80e4ccSMatthias Ringwald  * @brief Wrapper around reverse_bytes for common buffer sizes
1399c80e4ccSMatthias Ringwald  * @param src
1409c80e4ccSMatthias Ringwald  * @param dest
1419c80e4ccSMatthias Ringwald  */
1429c80e4ccSMatthias Ringwald void reverse_24 (const uint8_t *src, uint8_t * dest);
1439c80e4ccSMatthias Ringwald void reverse_48 (const uint8_t *src, uint8_t * dest);
1449c80e4ccSMatthias Ringwald void reverse_56 (const uint8_t *src, uint8_t * dest);
1459c80e4ccSMatthias Ringwald void reverse_64 (const uint8_t *src, uint8_t * dest);
1469c80e4ccSMatthias Ringwald void reverse_128(const uint8_t *src, uint8_t * dest);
147cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t *src, uint8_t * dest);
1489c80e4ccSMatthias Ringwald 
149724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest);
150caed94dfSMatthias Ringwald 
151caed94dfSMatthias Ringwald /**
152877f40f3SMatthias Ringwald  * @brief ASCII character for 4-bit nibble
153877f40f3SMatthias Ringwald  * @return character
154caed94dfSMatthias Ringwald  */
155eb886013SMatthias Ringwald char char_for_nibble(int nibble);
156eb886013SMatthias Ringwald 
157caed94dfSMatthias Ringwald /**
158877f40f3SMatthias Ringwald  * @brif 4-bit nibble from ASCII character
159877f40f3SMatthias Ringwald  * @return value
160877f40f3SMatthias Ringwald  */
161877f40f3SMatthias Ringwald int nibble_for_char(char c);
162877f40f3SMatthias Ringwald 
163877f40f3SMatthias Ringwald /**
164caed94dfSMatthias Ringwald  * @brief Compare two Bluetooth addresses
165caed94dfSMatthias Ringwald  * @param a
166caed94dfSMatthias Ringwald  * @param b
167caed94dfSMatthias Ringwald  * @return true if equal
168caed94dfSMatthias Ringwald  */
16973988a59SMatthias Ringwald int bd_addr_cmp(bd_addr_t a, bd_addr_t b);
170caed94dfSMatthias Ringwald 
171caed94dfSMatthias Ringwald /**
172caed94dfSMatthias Ringwald  * @brief Copy Bluetooth address
17373988a59SMatthias Ringwald  * @param dest
174caed94dfSMatthias Ringwald  * @param src
175caed94dfSMatthias Ringwald  */
17673988a59SMatthias Ringwald void bd_addr_copy(bd_addr_t dest, bd_addr_t src);
177caed94dfSMatthias Ringwald 
178caed94dfSMatthias Ringwald /**
179caed94dfSMatthias Ringwald  * @brief Use printf to write hexdump as single line of data
180caed94dfSMatthias Ringwald  */
181caed94dfSMatthias Ringwald void printf_hexdump(const void *data, int size);
182caed94dfSMatthias Ringwald 
183caed94dfSMatthias Ringwald /**
184caed94dfSMatthias Ringwald  * @brief Create human readable representation for UUID128
185caed94dfSMatthias Ringwald  * @note uses fixed global buffer
186caed94dfSMatthias Ringwald  * @return pointer to UUID128 string
187caed94dfSMatthias Ringwald  */
188caed94dfSMatthias Ringwald char * uuid128_to_str(uint8_t * uuid);
189caed94dfSMatthias Ringwald 
190caed94dfSMatthias Ringwald /**
191caed94dfSMatthias Ringwald  * @brief Create human readable represenationt of Bluetooth address
192caed94dfSMatthias Ringwald  * @note uses fixed global buffer
193caed94dfSMatthias Ringwald  * @return pointer to Bluetooth address string
194caed94dfSMatthias Ringwald  */
195eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr);
196caed94dfSMatthias Ringwald 
197caed94dfSMatthias Ringwald /**
198caed94dfSMatthias Ringwald  * @brief Parse Bluetooth address
199caed94dfSMatthias Ringwald  * @param address_string
200caed94dfSMatthias Ringwald  * @param buffer for parsed address
201caed94dfSMatthias Ringwald  * @return 1 if string was parsed successfully
202caed94dfSMatthias Ringwald  */
203a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr);
204caed94dfSMatthias Ringwald 
20573988a59SMatthias Ringwald /**
20673988a59SMatthias Ringwald  * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID
20773988a59SMatthias Ringwald  * @param uuid128 output buffer
20873988a59SMatthias Ringwald  * @param short_uuid
20973988a59SMatthias Ringwald  */
21073988a59SMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid);
211eb886013SMatthias Ringwald 
21273988a59SMatthias Ringwald /**
21373988a59SMatthias Ringwald  * @brief Checks if UUID128 has Bluetooth base UUID prefix
21473988a59SMatthias Ringwald  * @param uui128 to test
21573988a59SMatthias Ringwald  * @return 1 if it can be expressed as UUID32
21673988a59SMatthias Ringwald  */
217e1a125dfSMatthias Ringwald int  uuid_has_bluetooth_prefix(uint8_t * uuid128);
218eb886013SMatthias Ringwald 
219*5d067ab1SMatthias Ringwald /**
220*5d067ab1SMatthias Ringwald  * @brief Parse unsigned number
221*5d067ab1SMatthias Ringwald  * @param str to parse
222*5d067ab1SMatthias Ringwald  * @return value
223*5d067ab1SMatthias Ringwald  */
224*5d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char *str);
225*5d067ab1SMatthias Ringwald 
226*5d067ab1SMatthias Ringwald 
227eb886013SMatthias Ringwald #if defined __cplusplus
228eb886013SMatthias Ringwald }
229eb886013SMatthias Ringwald #endif
230eb886013SMatthias Ringwald 
231bf1b35bfSMatthias Ringwald #endif // __BTSTACK_UTIL_H
232