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