xref: /btstack/src/btstack_util.c (revision 969fc1c5012c6dba7780d5c32045aeb3530e5f29)
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 
5273988a59SMatthias Ringwald 
5373988a59SMatthias Ringwald /**
5473988a59SMatthias Ringwald  * @brief Compare two Bluetooth addresses
5573988a59SMatthias Ringwald  * @param a
5673988a59SMatthias Ringwald  * @param b
57*969fc1c5SMilanka Ringwald  * @return 0 if equal
5873988a59SMatthias Ringwald  */
5973988a59SMatthias Ringwald int bd_addr_cmp(bd_addr_t a, bd_addr_t b){
6073988a59SMatthias Ringwald     return memcmp(a,b, BD_ADDR_LEN);
6173988a59SMatthias Ringwald }
6273988a59SMatthias Ringwald 
6373988a59SMatthias Ringwald /**
6473988a59SMatthias Ringwald  * @brief Copy Bluetooth address
6573988a59SMatthias Ringwald  * @param dest
6673988a59SMatthias Ringwald  * @param src
6773988a59SMatthias Ringwald  */
6873988a59SMatthias Ringwald void bd_addr_copy(bd_addr_t dest, bd_addr_t src){
6973988a59SMatthias Ringwald     memcpy(dest,src,BD_ADDR_LEN);
7073988a59SMatthias Ringwald }
7173988a59SMatthias Ringwald 
7273988a59SMatthias Ringwald uint16_t little_endian_read_16(const uint8_t * buffer, int pos){
7373988a59SMatthias Ringwald     return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8);
7473988a59SMatthias Ringwald }
7573988a59SMatthias Ringwald uint32_t little_endian_read_24(const uint8_t * buffer, int pos){
7673988a59SMatthias Ringwald     return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16);
7773988a59SMatthias Ringwald }
7873988a59SMatthias Ringwald uint32_t little_endian_read_32(const uint8_t * buffer, int pos){
7973988a59SMatthias Ringwald     return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3]) << 24);
8073988a59SMatthias Ringwald }
8173988a59SMatthias Ringwald 
82f8fbdce0SMatthias Ringwald void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
83eb886013SMatthias Ringwald     buffer[pos++] = value;
84eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
85eb886013SMatthias Ringwald }
86eb886013SMatthias Ringwald 
87f8fbdce0SMatthias Ringwald void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
88eb886013SMatthias Ringwald     buffer[pos++] = value;
89eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
90eb886013SMatthias Ringwald     buffer[pos++] = value >> 16;
91eb886013SMatthias Ringwald     buffer[pos++] = value >> 24;
92eb886013SMatthias Ringwald }
93eb886013SMatthias Ringwald 
9473988a59SMatthias Ringwald uint32_t big_endian_read_16( const uint8_t * buffer, int pos) {
9573988a59SMatthias Ringwald     return ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos   ]) << 8);
9673988a59SMatthias Ringwald }
9773988a59SMatthias Ringwald 
9873988a59SMatthias Ringwald uint32_t big_endian_read_32( const uint8_t * buffer, int pos) {
9973988a59SMatthias Ringwald     return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24);
10073988a59SMatthias Ringwald }
10173988a59SMatthias Ringwald 
102f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
103eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
104eb886013SMatthias Ringwald     buffer[pos++] = value;
105eb886013SMatthias Ringwald }
106eb886013SMatthias Ringwald 
107f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
108eb886013SMatthias Ringwald     buffer[pos++] = value >> 24;
109eb886013SMatthias Ringwald     buffer[pos++] = value >> 16;
110eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
111eb886013SMatthias Ringwald     buffer[pos++] = value;
112eb886013SMatthias Ringwald }
113eb886013SMatthias Ringwald 
114eb886013SMatthias Ringwald // general swap/endianess utils
1159c80e4ccSMatthias Ringwald void reverse_bytes(const uint8_t *src, uint8_t *dst, int len){
116eb886013SMatthias Ringwald     int i;
117eb886013SMatthias Ringwald     for (i = 0; i < len; i++)
118eb886013SMatthias Ringwald         dst[len - 1 - i] = src[i];
119eb886013SMatthias Ringwald }
1209c80e4ccSMatthias Ringwald void reverse_24(const uint8_t * src, uint8_t * dst){
1219c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 3);
122eb886013SMatthias Ringwald }
1239c80e4ccSMatthias Ringwald void reverse_48(const uint8_t * src, uint8_t * dst){
1249c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 6);
125bf1b35bfSMatthias Ringwald }
1269c80e4ccSMatthias Ringwald void reverse_56(const uint8_t * src, uint8_t * dst){
1279c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 7);
128eb886013SMatthias Ringwald }
1299c80e4ccSMatthias Ringwald void reverse_64(const uint8_t * src, uint8_t * dst){
1309c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 8);
131eb886013SMatthias Ringwald }
1329c80e4ccSMatthias Ringwald void reverse_128(const uint8_t * src, uint8_t * dst){
1339c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 16);
134eb886013SMatthias Ringwald }
135eb886013SMatthias Ringwald 
136724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){
137724d70a2SMatthias Ringwald     reverse_bytes(src, dest, 6);
138724d70a2SMatthias Ringwald }
139724d70a2SMatthias Ringwald 
140ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b){
141ebaeb1beSMatthias Ringwald     return a < b ? a : b;
142ebaeb1beSMatthias Ringwald }
143ebaeb1beSMatthias Ringwald 
144ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b){
145ebaeb1beSMatthias Ringwald     return a > b ? a : b;
146ebaeb1beSMatthias Ringwald }
147ebaeb1beSMatthias Ringwald 
148eb886013SMatthias Ringwald char char_for_nibble(int nibble){
149eb886013SMatthias Ringwald     if (nibble < 10) return '0' + nibble;
150eb886013SMatthias Ringwald     nibble -= 10;
151eb886013SMatthias Ringwald     if (nibble < 6) return 'A' + nibble;
152eb886013SMatthias Ringwald     return '?';
153eb886013SMatthias Ringwald }
154eb886013SMatthias Ringwald 
155a6efb919SMatthias Ringwald int nibble_for_char(char c){
156a6efb919SMatthias Ringwald     if (c >= '0' && c <= '9') return c - '0';
1579f507070SMatthias Ringwald     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
1589f507070SMatthias Ringwald     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
159a6efb919SMatthias Ringwald     return -1;
160a6efb919SMatthias Ringwald }
161a6efb919SMatthias Ringwald 
162eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){
163eb886013SMatthias Ringwald     if (size <= 0) return;
164eb886013SMatthias Ringwald     int i;
165eb886013SMatthias Ringwald     for (i=0; i<size;i++){
166eb886013SMatthias Ringwald         printf("%02X ", ((uint8_t *)data)[i]);
167eb886013SMatthias Ringwald     }
168eb886013SMatthias Ringwald     printf("\n");
169eb886013SMatthias Ringwald }
170eb886013SMatthias Ringwald 
171caed94dfSMatthias Ringwald //  void log_info_hexdump(..){
172caed94dfSMatthias Ringwald //
173caed94dfSMatthias Ringwald //  }
174caed94dfSMatthias Ringwald 
1758314c363SMatthias Ringwald void log_info_hexdump(const void *data, int size){
176eb886013SMatthias Ringwald #ifdef ENABLE_LOG_INFO
177eb886013SMatthias Ringwald     char buffer[6*16+1];
178eb886013SMatthias Ringwald     int i, j;
179eb886013SMatthias Ringwald 
180eb886013SMatthias Ringwald     uint8_t low = 0x0F;
181eb886013SMatthias Ringwald     uint8_t high = 0xF0;
182eb886013SMatthias Ringwald     j = 0;
183eb886013SMatthias Ringwald     for (i=0; i<size;i++){
184eb886013SMatthias Ringwald         uint8_t byte = ((uint8_t *)data)[i];
185eb886013SMatthias Ringwald         buffer[j++] = '0';
186eb886013SMatthias Ringwald         buffer[j++] = 'x';
187eb886013SMatthias Ringwald         buffer[j++] = char_for_nibble((byte & high) >> 4);
188eb886013SMatthias Ringwald         buffer[j++] = char_for_nibble(byte & low);
189eb886013SMatthias Ringwald         buffer[j++] = ',';
190eb886013SMatthias Ringwald         buffer[j++] = ' ';
191eb886013SMatthias Ringwald         if (j >= 6*16 ){
192eb886013SMatthias Ringwald             buffer[j] = 0;
193eb886013SMatthias Ringwald             log_info("%s", buffer);
194eb886013SMatthias Ringwald             j = 0;
195eb886013SMatthias Ringwald         }
196eb886013SMatthias Ringwald     }
197eb886013SMatthias Ringwald     if (j != 0){
198eb886013SMatthias Ringwald         buffer[j] = 0;
199eb886013SMatthias Ringwald         log_info("%s", buffer);
200eb886013SMatthias Ringwald     }
2018314c363SMatthias Ringwald #endif
2027299c0feSMatthias Ringwald }
203eb886013SMatthias Ringwald 
2048314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key){
2058314c363SMatthias Ringwald     // log_info("%-6s ", name);
2068314c363SMatthias Ringwald     // hexdump(key, 16);
207eb886013SMatthias Ringwald }
208eb886013SMatthias Ringwald 
2092b604902SMatthias Ringwald // UUIDs are stored in big endian, similar to bd_addr_t
2102b604902SMatthias Ringwald 
211eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB
2122b604902SMatthias Ringwald const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */
213eb886013SMatthias Ringwald     0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
214eb886013SMatthias Ringwald 
215e1a125dfSMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){
2162b604902SMatthias Ringwald     memcpy(uuid, bluetooth_base_uuid, 16);
217f8fbdce0SMatthias Ringwald     big_endian_store_32(uuid, 0, shortUUID);
218eb886013SMatthias Ringwald }
219eb886013SMatthias Ringwald 
220e1a125dfSMatthias Ringwald int uuid_has_bluetooth_prefix(uint8_t * uuid128){
2212b604902SMatthias Ringwald     return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0;
222eb886013SMatthias Ringwald }
223eb886013SMatthias Ringwald 
224eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1];
225eb886013SMatthias Ringwald char * uuid128_to_str(uint8_t * uuid){
226eb886013SMatthias Ringwald     sprintf(uuid128_to_str_buffer, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
227eb886013SMatthias Ringwald            uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
228eb886013SMatthias Ringwald            uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
229eb886013SMatthias Ringwald     return uuid128_to_str_buffer;
230eb886013SMatthias Ringwald }
231eb886013SMatthias Ringwald 
232eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3];  // 12:45:78:01:34:67\0
233eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr){
234eb886013SMatthias Ringwald     // orig code
235eb886013SMatthias 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]);
236eb886013SMatthias Ringwald     // sprintf-free code
237eb886013SMatthias Ringwald     char * p = bd_addr_to_str_buffer;
238eb886013SMatthias Ringwald     int i;
239eb886013SMatthias Ringwald     for (i = 0; i < 6 ; i++) {
240eb886013SMatthias Ringwald         *p++ = char_for_nibble((addr[i] >> 4) & 0x0F);
241eb886013SMatthias Ringwald         *p++ = char_for_nibble((addr[i] >> 0) & 0x0F);
242eb886013SMatthias Ringwald         *p++ = ':';
243eb886013SMatthias Ringwald     }
244eb886013SMatthias Ringwald     *--p = 0;
245eb886013SMatthias Ringwald     return (char *) bd_addr_to_str_buffer;
246eb886013SMatthias Ringwald }
247eb886013SMatthias Ringwald 
248a6efb919SMatthias Ringwald static int scan_hex_byte(const char * byte_string){
249a6efb919SMatthias Ringwald     int upper_nibble = nibble_for_char(*byte_string++);
250a6efb919SMatthias Ringwald     if (upper_nibble < 0) return -1;
251a6efb919SMatthias Ringwald     int lower_nibble = nibble_for_char(*byte_string);
252a6efb919SMatthias Ringwald     if (lower_nibble < 0) return -1;
253a6efb919SMatthias Ringwald     return (upper_nibble << 4) | lower_nibble;
254a6efb919SMatthias Ringwald }
255eb886013SMatthias Ringwald 
256a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){
257a6efb919SMatthias Ringwald     uint8_t buffer[BD_ADDR_LEN];
258a6efb919SMatthias Ringwald     int result = 0;
259eb886013SMatthias Ringwald     int i;
260eb886013SMatthias Ringwald     for (i = 0; i < BD_ADDR_LEN; i++) {
261a6efb919SMatthias Ringwald         int single_byte = scan_hex_byte(addr_string);
262a6efb919SMatthias Ringwald         if (single_byte < 0) break;
263a6efb919SMatthias Ringwald         addr_string += 2;
2643e40861cSMilanka Ringwald         buffer[i] = single_byte;
265a6efb919SMatthias Ringwald         // don't check seperator after last byte
266a6efb919SMatthias Ringwald         if (i == BD_ADDR_LEN - 1) {
267a6efb919SMatthias Ringwald             result = 1;
268a6efb919SMatthias Ringwald             break;
269eb886013SMatthias Ringwald         }
270a6efb919SMatthias Ringwald         char separator = *addr_string++;
271a6efb919SMatthias Ringwald         if (separator != ':' && separator != '-' && separator != ' ') break;
272a6efb919SMatthias Ringwald     }
273a6efb919SMatthias Ringwald 
274a6efb919SMatthias Ringwald     if (result){
275a6efb919SMatthias Ringwald         bd_addr_copy(addr, buffer);
276a6efb919SMatthias Ringwald     }
277a6efb919SMatthias Ringwald 	return result;
278eb886013SMatthias Ringwald }
279