xref: /btstack/src/btstack_util.c (revision 73988a59ceac89ff830d69617eb8c69d4fdb6564)
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.c
40  *
41  *  General utility functions
42  *
43  *  Created by Matthias Ringwald on 7/23/09.
44  */
45 
46 #include "btstack_config.h"
47 #include "btstack_util.h"
48 #include <stdio.h>
49 #include <string.h>
50 #include "btstack_debug.h"
51 
52 
53 /**
54  * @brief Compare two Bluetooth addresses
55  * @param a
56  * @param b
57  * @return true if equal
58  */
59 int bd_addr_cmp(bd_addr_t a, bd_addr_t b){
60     return memcmp(a,b, BD_ADDR_LEN);
61 }
62 
63 /**
64  * @brief Copy Bluetooth address
65  * @param dest
66  * @param src
67  */
68 void bd_addr_copy(bd_addr_t dest, bd_addr_t src){
69     memcpy(dest,src,BD_ADDR_LEN);
70 }
71 
72 uint16_t little_endian_read_16(const uint8_t * buffer, int pos){
73     return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8);
74 }
75 uint32_t little_endian_read_24(const uint8_t * buffer, int pos){
76     return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16);
77 }
78 uint32_t little_endian_read_32(const uint8_t * buffer, int pos){
79     return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3]) << 24);
80 }
81 
82 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
83     buffer[pos++] = value;
84     buffer[pos++] = value >> 8;
85 }
86 
87 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
88     buffer[pos++] = value;
89     buffer[pos++] = value >> 8;
90     buffer[pos++] = value >> 16;
91     buffer[pos++] = value >> 24;
92 }
93 
94 uint32_t big_endian_read_16( const uint8_t * buffer, int pos) {
95     return ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos   ]) << 8);
96 }
97 
98 uint32_t big_endian_read_32( const uint8_t * buffer, int pos) {
99     return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24);
100 }
101 
102 void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
103     buffer[pos++] = value >> 8;
104     buffer[pos++] = value;
105 }
106 
107 void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
108     buffer[pos++] = value >> 24;
109     buffer[pos++] = value >> 16;
110     buffer[pos++] = value >> 8;
111     buffer[pos++] = value;
112 }
113 
114 
115 void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
116     dest[0] = src[5];
117     dest[1] = src[4];
118     dest[2] = src[3];
119     dest[3] = src[2];
120     dest[4] = src[1];
121     dest[5] = src[0];
122 }
123 
124 // general swap/endianess utils
125 void swapX(const uint8_t *src, uint8_t *dst, int len){
126     int i;
127     for (i = 0; i < len; i++)
128         dst[len - 1 - i] = src[i];
129 }
130 void swap24(const uint8_t * src, uint8_t * dst){
131     swapX(src, dst, 3);
132 }
133 void swap48(const uint8_t * src, uint8_t * dst){
134     swapX(src, dst, 6);
135 }
136 void swap56(const uint8_t * src, uint8_t * dst){
137     swapX(src, dst, 7);
138 }
139 void swap64(const uint8_t * src, uint8_t * dst){
140     swapX(src, dst, 8);
141 }
142 void swap128(const uint8_t * src, uint8_t * dst){
143     swapX(src, dst, 16);
144 }
145 
146 char char_for_nibble(int nibble){
147     if (nibble < 10) return '0' + nibble;
148     nibble -= 10;
149     if (nibble < 6) return 'A' + nibble;
150     return '?';
151 }
152 
153 void printf_hexdump(const void *data, int size){
154     if (size <= 0) return;
155     int i;
156     for (i=0; i<size;i++){
157         printf("%02X ", ((uint8_t *)data)[i]);
158     }
159     printf("\n");
160 }
161 
162 void hexdumpf(const void *data, int size){
163     char buffer[6*16+1];
164     int i, j;
165 
166     uint8_t low = 0x0F;
167     uint8_t high = 0xF0;
168     j = 0;
169     for (i=0; i<size;i++){
170         uint8_t byte = ((uint8_t *)data)[i];
171         buffer[j++] = '0';
172         buffer[j++] = 'x';
173         buffer[j++] = char_for_nibble((byte & high) >> 4);
174         buffer[j++] = char_for_nibble(byte & low);
175         buffer[j++] = ',';
176         buffer[j++] = ' ';
177         if (j >= 6*16 ){
178             buffer[j] = 0;
179             printf("%s\n", buffer);
180             j = 0;
181         }
182     }
183     if (j != 0){
184         buffer[j] = 0;
185         printf("%s\n", buffer);
186     }
187 }
188 
189 //  void log_info_hexdump(..){
190 //
191 //  }
192 
193 void hexdump(const void *data, int size){
194 #ifdef ENABLE_LOG_INFO
195     char buffer[6*16+1];
196     int i, j;
197 
198     uint8_t low = 0x0F;
199     uint8_t high = 0xF0;
200     j = 0;
201     for (i=0; i<size;i++){
202         uint8_t byte = ((uint8_t *)data)[i];
203         buffer[j++] = '0';
204         buffer[j++] = 'x';
205         buffer[j++] = char_for_nibble((byte & high) >> 4);
206         buffer[j++] = char_for_nibble(byte & low);
207         buffer[j++] = ',';
208         buffer[j++] = ' ';
209         if (j >= 6*16 ){
210             buffer[j] = 0;
211             log_info("%s", buffer);
212             j = 0;
213         }
214     }
215     if (j != 0){
216         buffer[j] = 0;
217         log_info("%s", buffer);
218     }
219 #endif
220 }
221 
222 void log_key(const char * name, sm_key_t key){
223     log_info("%-6s ", name);
224     hexdump(key, 16);
225 }
226 
227 // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB
228 const uint8_t sdp_bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */
229     0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
230 
231 void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){
232     memcpy(uuid, sdp_bluetooth_base_uuid, 16);
233     big_endian_store_32(uuid, 0, shortUUID);
234 }
235 
236 int uuid_has_bluetooth_prefix(uint8_t * uuid128){
237     return memcmp(&uuid128[4], &sdp_bluetooth_base_uuid[4], 12) == 0;
238 }
239 
240 static char uuid128_to_str_buffer[32+4+1];
241 char * uuid128_to_str(uint8_t * uuid){
242     sprintf(uuid128_to_str_buffer, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
243            uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
244            uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
245     return uuid128_to_str_buffer;
246 }
247 
248 static char bd_addr_to_str_buffer[6*3];  // 12:45:78:01:34:67\0
249 char * bd_addr_to_str(bd_addr_t addr){
250     // orig code
251     // sprintf(bd_addr_to_str_buffer, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
252     // sprintf-free code
253     char * p = bd_addr_to_str_buffer;
254     int i;
255     for (i = 0; i < 6 ; i++) {
256         *p++ = char_for_nibble((addr[i] >> 4) & 0x0F);
257         *p++ = char_for_nibble((addr[i] >> 0) & 0x0F);
258         *p++ = ':';
259     }
260     *--p = 0;
261     return (char *) bd_addr_to_str_buffer;
262 }
263 
264 int sscanf_bd_addr(uint8_t * addr_string, bd_addr_t addr){
265 	unsigned int bd_addr_buffer[BD_ADDR_LEN];  //for sscanf, integer needed
266 	// reset result buffer
267     memset(bd_addr_buffer, 0, sizeof(bd_addr_buffer));
268 
269 	// parse
270     int result = sscanf( (char *) addr_string, "%2x:%2x:%2x:%2x:%2x:%2x", &bd_addr_buffer[0], &bd_addr_buffer[1], &bd_addr_buffer[2],
271 						&bd_addr_buffer[3], &bd_addr_buffer[4], &bd_addr_buffer[5]);
272 
273     if (result != BD_ADDR_LEN) return 0;
274 
275 	// store
276     int i;
277 	for (i = 0; i < BD_ADDR_LEN; i++) {
278 		addr[i] = (uint8_t) bd_addr_buffer[i];
279 	}
280 	return 1;
281 }
282