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