xref: /btstack/src/btstack_util.h (revision caed94df78f9730dc4c5dc4307e9a50fed8eddeb)
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>
558974fcd6SMatthias Ringwald #include "bluetooth.h"
568974fcd6SMatthias Ringwald #include "btstack_defines.h"
578d471f5cSMatthias Ringwald #include "btstack_linked_list.h"
58eb886013SMatthias Ringwald 
598974fcd6SMatthias Ringwald // will be moved to daemon/btstack_device_name_db.h
60eb886013SMatthias Ringwald 
61eb886013SMatthias Ringwald /**
62eb886013SMatthias Ringwald  * @brief The device name type
63eb886013SMatthias Ringwald  */
64eb886013SMatthias Ringwald #define DEVICE_NAME_LEN 248
65eb886013SMatthias Ringwald typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
66eb886013SMatthias Ringwald 
678d471f5cSMatthias Ringwald 
68*caed94dfSMatthias Ringwald // helper for little endian format
69f8fbdce0SMatthias Ringwald #define little_endian_read_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8))
70f8fbdce0SMatthias Ringwald #define little_endian_read_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16))
71f8fbdce0SMatthias Ringwald #define little_endian_read_32( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3])) << 24)
72eb886013SMatthias Ringwald 
73f8fbdce0SMatthias Ringwald void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
74f8fbdce0SMatthias Ringwald void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
75*caed94dfSMatthias Ringwald 
76*caed94dfSMatthias Ringwald // helper for big endian format
77*caed94dfSMatthias Ringwald #define big_endian_read_16( buffer, pos) ( ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos   ]) << 8))
78*caed94dfSMatthias Ringwald #define bit_endian_read_32( buffer, pos) ( ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos])) << 24)
79eb886013SMatthias Ringwald 
80f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
81f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
82eb886013SMatthias Ringwald 
83eb886013SMatthias Ringwald // hack: compilation with the android ndk causes an error as there's a swap64 macro
84eb886013SMatthias Ringwald #ifdef swap64
85eb886013SMatthias Ringwald #undef swap64
86eb886013SMatthias Ringwald #endif
87eb886013SMatthias Ringwald 
88*caed94dfSMatthias Ringwald /**
89*caed94dfSMatthias Ringwald  * @brief Copy from source to destination and reverse byte order
90*caed94dfSMatthias Ringwald  */
91eb886013SMatthias Ringwald void swapX  (const uint8_t *src, uint8_t * dst, int len);
92bf1b35bfSMatthias Ringwald void swap24 (const uint8_t *src, uint8_t * dst);
93bf1b35bfSMatthias Ringwald void swap48 (const uint8_t *src, uint8_t * dst);
94bf1b35bfSMatthias Ringwald void swap56 (const uint8_t *src, uint8_t * dst);
95bf1b35bfSMatthias Ringwald void swap64 (const uint8_t *src, uint8_t * dst);
96bf1b35bfSMatthias Ringwald void swap128(const uint8_t *src, uint8_t * dst);
97eb886013SMatthias Ringwald 
98*caed94dfSMatthias Ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src);
99*caed94dfSMatthias Ringwald 
100*caed94dfSMatthias Ringwald /**
101*caed94dfSMatthias Ringwald  * @brief 4-bit nibble
102*caed94dfSMatthias Ringwald  * @return ASCII character for 4-bit nibble
103*caed94dfSMatthias Ringwald  */
104eb886013SMatthias Ringwald char char_for_nibble(int nibble);
105eb886013SMatthias Ringwald 
106*caed94dfSMatthias Ringwald /**
107*caed94dfSMatthias Ringwald  * @brief Compare two Bluetooth addresses
108*caed94dfSMatthias Ringwald  * @param a
109*caed94dfSMatthias Ringwald  * @param b
110*caed94dfSMatthias Ringwald  * @return true if equal
111*caed94dfSMatthias Ringwald  */
112*caed94dfSMatthias Ringwald #define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN)
113*caed94dfSMatthias Ringwald 
114*caed94dfSMatthias Ringwald /**
115*caed94dfSMatthias Ringwald  * @brief Copy Bluetooth address
116*caed94dfSMatthias Ringwald  * @param dest
117*caed94dfSMatthias Ringwald  * @param src
118*caed94dfSMatthias Ringwald  */
119*caed94dfSMatthias Ringwald #define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN)
120*caed94dfSMatthias Ringwald 
121*caed94dfSMatthias Ringwald /**
122*caed94dfSMatthias Ringwald  * CRC8 functions using ETSI TS 101 369 V6.3.0.
123*caed94dfSMatthias Ringwald  * Only needed by RFCOMM
124*caed94dfSMatthias Ringwald  */
125*caed94dfSMatthias Ringwald uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
126*caed94dfSMatthias Ringwald uint8_t crc8_calc(uint8_t *data, uint16_t len);
127*caed94dfSMatthias Ringwald 
128eb886013SMatthias Ringwald 
129eb886013SMatthias Ringwald // @deprecated please use more convenient bd_addr_to_str
130eb886013SMatthias Ringwald void print_bd_addr( bd_addr_t addr);
131eb886013SMatthias Ringwald 
132*caed94dfSMatthias Ringwald /**
133*caed94dfSMatthias Ringwald  * @brief Use printf to write hexdump as single line of data
134*caed94dfSMatthias Ringwald  */
135*caed94dfSMatthias Ringwald void printf_hexdump(const void *data, int size);
136*caed94dfSMatthias Ringwald 
137*caed94dfSMatthias Ringwald // move to btstack_debug.h
138*caed94dfSMatthias Ringwald // void log_info_hexdump(..) either log or hci_dump or off
139*caed94dfSMatthias Ringwald void log_key(const char * name, sm_key_t key);
140*caed94dfSMatthias Ringwald 
141*caed94dfSMatthias Ringwald //
142*caed94dfSMatthias Ringwald void hexdump(const void *data, int size);
143*caed94dfSMatthias Ringwald void hexdumpf(const void *data, int size);
144*caed94dfSMatthias Ringwald 
145*caed94dfSMatthias Ringwald /**
146*caed94dfSMatthias Ringwald  * @brief Create human readable representation for UUID128
147*caed94dfSMatthias Ringwald  * @note uses fixed global buffer
148*caed94dfSMatthias Ringwald  * @return pointer to UUID128 string
149*caed94dfSMatthias Ringwald  */
150*caed94dfSMatthias Ringwald char * uuid128_to_str(uint8_t * uuid);
151*caed94dfSMatthias Ringwald 
152*caed94dfSMatthias Ringwald /**
153*caed94dfSMatthias Ringwald  * @brief Print UUID128
154*caed94dfSMatthias Ringwald  * @note uses fixed global buffer
155*caed94dfSMatthias Ringwald  */
156*caed94dfSMatthias Ringwald void printUUID128(uint8_t *uuid);
157*caed94dfSMatthias Ringwald 
158*caed94dfSMatthias Ringwald /**
159*caed94dfSMatthias Ringwald  * @brief Create human readable represenationt of Bluetooth address
160*caed94dfSMatthias Ringwald  * @note uses fixed global buffer
161*caed94dfSMatthias Ringwald  * @return pointer to Bluetooth address string
162*caed94dfSMatthias Ringwald  */
163eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr);
164*caed94dfSMatthias Ringwald 
165*caed94dfSMatthias Ringwald /**
166*caed94dfSMatthias Ringwald  * @brief Parse Bluetooth address
167*caed94dfSMatthias Ringwald  * @param address_string
168*caed94dfSMatthias Ringwald  * @param buffer for parsed address
169*caed94dfSMatthias Ringwald  * @return 1 if string was parsed successfully
170*caed94dfSMatthias Ringwald  */
171*caed94dfSMatthias Ringwald int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr);
172*caed94dfSMatthias Ringwald 
173eb886013SMatthias Ringwald 
174eb886013SMatthias Ringwald void sdp_normalize_uuid(uint8_t *uuid, uint32_t shortUUID);
175eb886013SMatthias Ringwald int  sdp_has_blueooth_base_uuid(uint8_t * uuid128);
176eb886013SMatthias Ringwald 
177eb886013SMatthias Ringwald #if defined __cplusplus
178eb886013SMatthias Ringwald }
179eb886013SMatthias Ringwald #endif
180eb886013SMatthias Ringwald 
181bf1b35bfSMatthias Ringwald #endif // __BTSTACK_UTIL_H
182