xref: /btstack/src/btstack_util.c (revision 5d067ab1c4e447692cc06fe2ee805514f636e938)
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"
4702bdfbf8SMatthias Ringwald #include "btstack_debug.h"
48eb886013SMatthias Ringwald #include "btstack_util.h"
4902bdfbf8SMatthias Ringwald 
50eb886013SMatthias Ringwald #include <stdio.h>
51eb886013SMatthias Ringwald #include <string.h>
52eb886013SMatthias Ringwald 
5373988a59SMatthias Ringwald /**
5473988a59SMatthias Ringwald  * @brief Compare two Bluetooth addresses
5573988a59SMatthias Ringwald  * @param a
5673988a59SMatthias Ringwald  * @param b
57969fc1c5SMilanka 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 }
135cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t * src, uint8_t * dst){
136cc7a2d78SMatthias Ringwald     reverse_bytes(src, dst, 32);
137cc7a2d78SMatthias Ringwald }
138eb886013SMatthias Ringwald 
139724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){
140724d70a2SMatthias Ringwald     reverse_bytes(src, dest, 6);
141724d70a2SMatthias Ringwald }
142724d70a2SMatthias Ringwald 
143ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b){
144ebaeb1beSMatthias Ringwald     return a < b ? a : b;
145ebaeb1beSMatthias Ringwald }
146ebaeb1beSMatthias Ringwald 
147ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b){
148ebaeb1beSMatthias Ringwald     return a > b ? a : b;
149ebaeb1beSMatthias Ringwald }
150ebaeb1beSMatthias Ringwald 
151eb886013SMatthias Ringwald char char_for_nibble(int nibble){
152eb886013SMatthias Ringwald     if (nibble < 10) return '0' + nibble;
153eb886013SMatthias Ringwald     nibble -= 10;
154eb886013SMatthias Ringwald     if (nibble < 6) return 'A' + nibble;
155eb886013SMatthias Ringwald     return '?';
156eb886013SMatthias Ringwald }
157eb886013SMatthias Ringwald 
158405d63a9SMatthias Ringwald static inline char char_for_high_nibble(int value){
159405d63a9SMatthias Ringwald     return char_for_nibble((value >> 4) & 0x0f);
160405d63a9SMatthias Ringwald }
161405d63a9SMatthias Ringwald 
162405d63a9SMatthias Ringwald static inline char char_for_low_nibble(int value){
163405d63a9SMatthias Ringwald     return char_for_nibble(value & 0x0f);
164405d63a9SMatthias Ringwald }
165405d63a9SMatthias Ringwald 
166a6efb919SMatthias Ringwald int nibble_for_char(char c){
167a6efb919SMatthias Ringwald     if (c >= '0' && c <= '9') return c - '0';
1689f507070SMatthias Ringwald     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
1699f507070SMatthias Ringwald     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
170a6efb919SMatthias Ringwald     return -1;
171a6efb919SMatthias Ringwald }
172a6efb919SMatthias Ringwald 
173eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){
174eb886013SMatthias Ringwald     if (size <= 0) return;
175eb886013SMatthias Ringwald     int i;
176eb886013SMatthias Ringwald     for (i=0; i<size;i++){
177eb886013SMatthias Ringwald         printf("%02X ", ((uint8_t *)data)[i]);
178eb886013SMatthias Ringwald     }
179eb886013SMatthias Ringwald     printf("\n");
180eb886013SMatthias Ringwald }
181eb886013SMatthias Ringwald 
1828314c363SMatthias Ringwald void log_info_hexdump(const void *data, int size){
183eb886013SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1847224be7eSMatthias Ringwald 
185405d63a9SMatthias Ringwald #define ITEMS_PER_LINE 16
186405d63a9SMatthias Ringwald // template '0x12, '
187405d63a9SMatthias Ringwald #define BYTES_PER_BYTE  6
1887224be7eSMatthias Ringwald 
189405d63a9SMatthias Ringwald     char buffer[BYTES_PER_BYTE*ITEMS_PER_LINE+1];
1907224be7eSMatthias Ringwald     int i, j;
191eb886013SMatthias Ringwald     j = 0;
192eb886013SMatthias Ringwald     for (i=0; i<size;i++){
1937224be7eSMatthias Ringwald 
1947224be7eSMatthias Ringwald         // help static analyzer proof that j stays within bounds
195405d63a9SMatthias Ringwald         if (j > BYTES_PER_BYTE * (ITEMS_PER_LINE-1)){
1967224be7eSMatthias Ringwald             j = 0;
1977224be7eSMatthias Ringwald         }
1987224be7eSMatthias Ringwald 
199eb886013SMatthias Ringwald         uint8_t byte = ((uint8_t *)data)[i];
200eb886013SMatthias Ringwald         buffer[j++] = '0';
201eb886013SMatthias Ringwald         buffer[j++] = 'x';
202405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
203405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
204eb886013SMatthias Ringwald         buffer[j++] = ',';
205eb886013SMatthias Ringwald         buffer[j++] = ' ';
2067224be7eSMatthias Ringwald 
207405d63a9SMatthias Ringwald         if (j >= BYTES_PER_BYTE * ITEMS_PER_LINE ){
208eb886013SMatthias Ringwald             buffer[j] = 0;
209eb886013SMatthias Ringwald             log_info("%s", buffer);
210eb886013SMatthias Ringwald             j = 0;
211eb886013SMatthias Ringwald         }
212eb886013SMatthias Ringwald     }
213eb886013SMatthias Ringwald     if (j != 0){
214eb886013SMatthias Ringwald         buffer[j] = 0;
215eb886013SMatthias Ringwald         log_info("%s", buffer);
216eb886013SMatthias Ringwald     }
2178314c363SMatthias Ringwald #endif
2187299c0feSMatthias Ringwald }
219eb886013SMatthias Ringwald 
2208314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key){
22102bdfbf8SMatthias Ringwald #ifdef ENABLE_LOG_INFO
22202bdfbf8SMatthias Ringwald     char buffer[16*2+1];
22302bdfbf8SMatthias Ringwald     int i;
22402bdfbf8SMatthias Ringwald     int j = 0;
22502bdfbf8SMatthias Ringwald     for (i=0; i<16;i++){
22602bdfbf8SMatthias Ringwald         uint8_t byte = key[i];
227405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
228405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
22902bdfbf8SMatthias Ringwald     }
23002bdfbf8SMatthias Ringwald     buffer[j] = 0;
23102bdfbf8SMatthias Ringwald     log_info("%-6s %s", name, buffer);
23202bdfbf8SMatthias Ringwald #endif
233eb886013SMatthias Ringwald }
234eb886013SMatthias Ringwald 
2352b604902SMatthias Ringwald // UUIDs are stored in big endian, similar to bd_addr_t
2362b604902SMatthias Ringwald 
237eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB
2382b604902SMatthias Ringwald const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */
239eb886013SMatthias Ringwald     0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
240eb886013SMatthias Ringwald 
241e1a125dfSMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){
2422b604902SMatthias Ringwald     memcpy(uuid, bluetooth_base_uuid, 16);
243f8fbdce0SMatthias Ringwald     big_endian_store_32(uuid, 0, shortUUID);
244eb886013SMatthias Ringwald }
245eb886013SMatthias Ringwald 
246e1a125dfSMatthias Ringwald int uuid_has_bluetooth_prefix(uint8_t * uuid128){
2472b604902SMatthias Ringwald     return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0;
248eb886013SMatthias Ringwald }
249eb886013SMatthias Ringwald 
250eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1];
251eb886013SMatthias Ringwald char * uuid128_to_str(uint8_t * uuid){
2527224be7eSMatthias Ringwald     int i;
2537224be7eSMatthias Ringwald     int j = 0;
2547224be7eSMatthias Ringwald     // after 4, 6, 8, and 10 bytes = XYXYXYXY-XYXY-XYXY-XYXY-XYXYXYXYXYXY, there's a dash
2557224be7eSMatthias Ringwald     const int dash_locations = (1<<3) | (1<<5) | (1<<7) | (1<<9);
2567224be7eSMatthias Ringwald     for (i=0;i<16;i++){
257405d63a9SMatthias Ringwald         uint8_t byte = uuid[i];
258405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_high_nibble(byte);
259405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_low_nibble(byte);
2607224be7eSMatthias Ringwald         if (dash_locations & (1<<i)){
2617224be7eSMatthias Ringwald             uuid128_to_str_buffer[j++] = '-';
2627224be7eSMatthias Ringwald         }
2637224be7eSMatthias Ringwald     }
264eb886013SMatthias Ringwald     return uuid128_to_str_buffer;
265eb886013SMatthias Ringwald }
266eb886013SMatthias Ringwald 
267eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3];  // 12:45:78:01:34:67\0
268eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr){
269eb886013SMatthias Ringwald     // orig code
270eb886013SMatthias 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]);
271eb886013SMatthias Ringwald     // sprintf-free code
272eb886013SMatthias Ringwald     char * p = bd_addr_to_str_buffer;
273eb886013SMatthias Ringwald     int i;
274eb886013SMatthias Ringwald     for (i = 0; i < 6 ; i++) {
275405d63a9SMatthias Ringwald         uint8_t byte = addr[i];
276405d63a9SMatthias Ringwald         *p++ = char_for_high_nibble(byte);
277405d63a9SMatthias Ringwald         *p++ = char_for_low_nibble(byte);
278eb886013SMatthias Ringwald         *p++ = ':';
279eb886013SMatthias Ringwald     }
280eb886013SMatthias Ringwald     *--p = 0;
281eb886013SMatthias Ringwald     return (char *) bd_addr_to_str_buffer;
282eb886013SMatthias Ringwald }
283eb886013SMatthias Ringwald 
284a6efb919SMatthias Ringwald static int scan_hex_byte(const char * byte_string){
285a6efb919SMatthias Ringwald     int upper_nibble = nibble_for_char(*byte_string++);
286a6efb919SMatthias Ringwald     if (upper_nibble < 0) return -1;
287a6efb919SMatthias Ringwald     int lower_nibble = nibble_for_char(*byte_string);
288a6efb919SMatthias Ringwald     if (lower_nibble < 0) return -1;
289a6efb919SMatthias Ringwald     return (upper_nibble << 4) | lower_nibble;
290a6efb919SMatthias Ringwald }
291eb886013SMatthias Ringwald 
292a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){
293a6efb919SMatthias Ringwald     uint8_t buffer[BD_ADDR_LEN];
294a6efb919SMatthias Ringwald     int result = 0;
295eb886013SMatthias Ringwald     int i;
296eb886013SMatthias Ringwald     for (i = 0; i < BD_ADDR_LEN; i++) {
297a6efb919SMatthias Ringwald         int single_byte = scan_hex_byte(addr_string);
298a6efb919SMatthias Ringwald         if (single_byte < 0) break;
299a6efb919SMatthias Ringwald         addr_string += 2;
3003e40861cSMilanka Ringwald         buffer[i] = single_byte;
301a6efb919SMatthias Ringwald         // don't check seperator after last byte
302a6efb919SMatthias Ringwald         if (i == BD_ADDR_LEN - 1) {
303a6efb919SMatthias Ringwald             result = 1;
304a6efb919SMatthias Ringwald             break;
305eb886013SMatthias Ringwald         }
306a6efb919SMatthias Ringwald         char separator = *addr_string++;
307a6efb919SMatthias Ringwald         if (separator != ':' && separator != '-' && separator != ' ') break;
308a6efb919SMatthias Ringwald     }
309a6efb919SMatthias Ringwald 
310a6efb919SMatthias Ringwald     if (result){
311a6efb919SMatthias Ringwald         bd_addr_copy(addr, buffer);
312a6efb919SMatthias Ringwald     }
313a6efb919SMatthias Ringwald 	return result;
314eb886013SMatthias Ringwald }
315*5d067ab1SMatthias Ringwald 
316*5d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char *str){
317*5d067ab1SMatthias Ringwald     uint32_t val = 0;
318*5d067ab1SMatthias Ringwald     while (1){
319*5d067ab1SMatthias Ringwald         char chr = *str;
320*5d067ab1SMatthias Ringwald         if (!chr || chr < '0' || chr > '9')
321*5d067ab1SMatthias Ringwald             return val;
322*5d067ab1SMatthias Ringwald         val = (val * 10) + (chr - '0');
323*5d067ab1SMatthias Ringwald         str++;
324*5d067ab1SMatthias Ringwald     }
325*5d067ab1SMatthias Ringwald }