xref: /btstack/src/btstack_util.h (revision f8fbdce0c5067e7e7edd3a29934b1f9b79c8ff2d)
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 
56 /**
57  * @brief hci connection handle type
58  */
59 typedef uint16_t hci_con_handle_t;
60 
61 /**
62  * @brief Length of a bluetooth device address.
63  */
64 #define BD_ADDR_LEN 6
65 typedef uint8_t bd_addr_t[BD_ADDR_LEN];
66 
67 /**
68  * @brief link key and its type
69  */
70 #define LINK_KEY_LEN 16
71 #define LINK_KEY_STR_LEN (LINK_KEY_LEN*2)
72 typedef uint8_t link_key_t[LINK_KEY_LEN];
73 
74 typedef enum {
75 	COMBINATION_KEY = 0,	// standard pairing
76 	LOCAL_UNIT_KEY,			// ?
77 	REMOTE_UNIT_KEY,		// ?
78 	DEBUG_COMBINATION_KEY,	// SSP with debug
79 	UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192, // SSP Simple Pairing
80 	AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192,	 // SSP Passkey, Number confirm, OOB
81 	CHANGED_COMBINATION_KEY,							 // Link key changed using Change Connection Lnk Key
82 	UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256, // SSP Simpe Pairing
83 	AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256,   // SSP Passkey, Number confirm, OOB
84 } link_key_type_t;
85 
86 /**
87  * @brief 128 bit key used with AES128 in Security Manager
88  */
89 typedef uint8_t sm_key_t[16];
90 
91 /**
92  * @brief The device name type
93  */
94 #define DEVICE_NAME_LEN 248
95 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
96 
97 // packet handler
98 typedef void (*btstack_packet_handler_t) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
99 
100 // helper for BT little endian format
101 #define little_endian_read_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8))
102 #define little_endian_read_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16))
103 #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)
104 
105 // helper for SDP big endian format
106 #define big_endian_read_16( buffer, pos) ( ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos   ]) << 8))
107 #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)
108 
109 // HCI CMD OGF/OCF
110 #define READ_CMD_OGF(buffer) (buffer[1] >> 2)
111 #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0])
112 
113 // check if command complete event for given command
114 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && little_endian_read_16(event,3) == cmd.opcode)
115 #define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && little_endian_read_16(event,4) == cmd.opcode)
116 
117 // Code+Len=2, Pkts+Opcode=3; total=5
118 #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5
119 
120 // ACL Packet
121 #define READ_ACL_CONNECTION_HANDLE( buffer ) ( little_endian_read_16(buffer,0) & 0x0fff)
122 #define READ_ACL_FLAGS( buffer )      ( buffer[1] >> 4 )
123 #define READ_ACL_LENGTH( buffer )     (little_endian_read_16(buffer, 2))
124 
125 // L2CAP Packet
126 #define READ_L2CAP_LENGTH(buffer)     ( little_endian_read_16(buffer, 4))
127 #define READ_L2CAP_CHANNEL_ID(buffer) ( little_endian_read_16(buffer, 6))
128 
129 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
130 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
131 void bt_flip_addr(bd_addr_t dest, bd_addr_t src);
132 
133 void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
134 void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
135 
136 // hack: compilation with the android ndk causes an error as there's a swap64 macro
137 #ifdef swap64
138 #undef swap64
139 #endif
140 
141 void swapX  (const uint8_t *src, uint8_t * dst, int len);
142 void swap24 (const uint8_t *src, uint8_t * dst);
143 void swap48 (const uint8_t *src, uint8_t * dst);
144 void swap56 (const uint8_t *src, uint8_t * dst);
145 void swap64 (const uint8_t *src, uint8_t * dst);
146 void swap128(const uint8_t *src, uint8_t * dst);
147 
148 char char_for_nibble(int nibble);
149 
150 void printf_hexdump(const void *data, int size);
151 void hexdump(const void *data, int size);
152 void hexdumpf(const void *data, int size);
153 char * uuid128_to_str(uint8_t * uuid);
154 void printUUID128(uint8_t *uuid);
155 void log_key(const char * name, sm_key_t key);
156 
157 // @deprecated please use more convenient bd_addr_to_str
158 void print_bd_addr( bd_addr_t addr);
159 
160 char * bd_addr_to_str(bd_addr_t addr);
161 char * link_key_to_str(link_key_t link_key);
162 char *link_key_type_to_str(link_key_type_t link_key);
163 
164 void sdp_normalize_uuid(uint8_t *uuid, uint32_t shortUUID);
165 int  sdp_has_blueooth_base_uuid(uint8_t * uuid128);
166 
167 int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr);
168 int sscan_link_key(char * addr_string, link_key_t link_key);
169 
170 uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
171 uint8_t crc8_calc(uint8_t *data, uint16_t len);
172 
173 #define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN)
174 #define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN)
175 
176 int is_authenticated_link_key(link_key_type_t link_key_type);
177 
178 #if defined __cplusplus
179 }
180 #endif
181 
182 #endif // __BTSTACK_UTIL_H
183