xref: /btstack/src/btstack_util.c (revision e501bae08349e058caa4648e0af3dd01cbd89d20)
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 
38*e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_util.c"
39ab2c6ae4SMatthias Ringwald 
40eb886013SMatthias Ringwald /*
41eb886013SMatthias Ringwald  *  btstack_util.c
42eb886013SMatthias Ringwald  *
43eb886013SMatthias Ringwald  *  General utility functions
44eb886013SMatthias Ringwald  *
45eb886013SMatthias Ringwald  *  Created by Matthias Ringwald on 7/23/09.
46eb886013SMatthias Ringwald  */
47eb886013SMatthias Ringwald 
487907f069SMatthias Ringwald #include "btstack_config.h"
4902bdfbf8SMatthias Ringwald #include "btstack_debug.h"
50eb886013SMatthias Ringwald #include "btstack_util.h"
5102bdfbf8SMatthias Ringwald 
52eb886013SMatthias Ringwald #include <stdio.h>
53eb886013SMatthias Ringwald #include <string.h>
54eb886013SMatthias Ringwald 
5573988a59SMatthias Ringwald /**
5673988a59SMatthias Ringwald  * @brief Compare two Bluetooth addresses
5773988a59SMatthias Ringwald  * @param a
5873988a59SMatthias Ringwald  * @param b
59969fc1c5SMilanka Ringwald  * @return 0 if equal
6073988a59SMatthias Ringwald  */
615222912bSMatthias Ringwald int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b){
6273988a59SMatthias Ringwald     return memcmp(a,b, BD_ADDR_LEN);
6373988a59SMatthias Ringwald }
6473988a59SMatthias Ringwald 
6573988a59SMatthias Ringwald /**
6673988a59SMatthias Ringwald  * @brief Copy Bluetooth address
6773988a59SMatthias Ringwald  * @param dest
6873988a59SMatthias Ringwald  * @param src
6973988a59SMatthias Ringwald  */
705222912bSMatthias Ringwald void bd_addr_copy(bd_addr_t dest, const bd_addr_t src){
7173988a59SMatthias Ringwald     memcpy(dest,src,BD_ADDR_LEN);
7273988a59SMatthias Ringwald }
7373988a59SMatthias Ringwald 
7473988a59SMatthias Ringwald uint16_t little_endian_read_16(const uint8_t * buffer, int pos){
75b0920f25SMilanka Ringwald     return (uint16_t)(((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8));
7673988a59SMatthias Ringwald }
7773988a59SMatthias Ringwald uint32_t little_endian_read_24(const uint8_t * buffer, int pos){
7873988a59SMatthias Ringwald     return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16);
7973988a59SMatthias Ringwald }
8073988a59SMatthias Ringwald uint32_t little_endian_read_32(const uint8_t * buffer, int pos){
8173988a59SMatthias Ringwald     return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3]) << 24);
8273988a59SMatthias Ringwald }
8373988a59SMatthias Ringwald 
84f8fbdce0SMatthias Ringwald void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
85b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)value;
86b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 8);
87eb886013SMatthias Ringwald }
88eb886013SMatthias Ringwald 
89adbdd27aSMilanka Ringwald void little_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value){
90adbdd27aSMilanka Ringwald     buffer[pos++] = (uint8_t)(value);
91adbdd27aSMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 8);
92adbdd27aSMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 16);
93adbdd27aSMilanka Ringwald }
94adbdd27aSMilanka Ringwald 
95f8fbdce0SMatthias Ringwald void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
96b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value);
97b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 8);
98b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 16);
99b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 24);
100eb886013SMatthias Ringwald }
101eb886013SMatthias Ringwald 
10273988a59SMatthias Ringwald uint32_t big_endian_read_16( const uint8_t * buffer, int pos) {
103b0920f25SMilanka Ringwald     return (uint16_t)(((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos   ]) << 8));
10473988a59SMatthias Ringwald }
10573988a59SMatthias Ringwald 
106e57a2545SMilanka Ringwald uint32_t big_endian_read_24( const uint8_t * buffer, int pos) {
107e57a2545SMilanka Ringwald     return ( ((uint32_t)buffer[(pos)+2]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t) buffer[pos]) << 16));
108e57a2545SMilanka Ringwald }
109e57a2545SMilanka Ringwald 
11073988a59SMatthias Ringwald uint32_t big_endian_read_32( const uint8_t * buffer, int pos) {
11173988a59SMatthias Ringwald     return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24);
11273988a59SMatthias Ringwald }
11373988a59SMatthias Ringwald 
114f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
115b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 8);
116b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value);
117eb886013SMatthias Ringwald }
118eb886013SMatthias Ringwald 
1197f535380SMilanka Ringwald void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value){
120b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 16);
121b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 8);
122b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value);
1237f535380SMilanka Ringwald }
1247f535380SMilanka Ringwald 
125f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
126b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 24);
127b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 16);
128b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value >> 8);
129b0920f25SMilanka Ringwald     buffer[pos++] = (uint8_t)(value);
130eb886013SMatthias Ringwald }
131eb886013SMatthias Ringwald 
132eb886013SMatthias Ringwald // general swap/endianess utils
1339c80e4ccSMatthias Ringwald void reverse_bytes(const uint8_t *src, uint8_t *dst, int len){
134eb886013SMatthias Ringwald     int i;
135eb886013SMatthias Ringwald     for (i = 0; i < len; i++)
136eb886013SMatthias Ringwald         dst[len - 1 - i] = src[i];
137eb886013SMatthias Ringwald }
1389c80e4ccSMatthias Ringwald void reverse_24(const uint8_t * src, uint8_t * dst){
1399c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 3);
140eb886013SMatthias Ringwald }
1419c80e4ccSMatthias Ringwald void reverse_48(const uint8_t * src, uint8_t * dst){
1429c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 6);
143bf1b35bfSMatthias Ringwald }
1449c80e4ccSMatthias Ringwald void reverse_56(const uint8_t * src, uint8_t * dst){
1459c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 7);
146eb886013SMatthias Ringwald }
1479c80e4ccSMatthias Ringwald void reverse_64(const uint8_t * src, uint8_t * dst){
1489c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 8);
149eb886013SMatthias Ringwald }
1509c80e4ccSMatthias Ringwald void reverse_128(const uint8_t * src, uint8_t * dst){
1519c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 16);
152eb886013SMatthias Ringwald }
153cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t * src, uint8_t * dst){
154cc7a2d78SMatthias Ringwald     reverse_bytes(src, dst, 32);
155cc7a2d78SMatthias Ringwald }
156eb886013SMatthias Ringwald 
157724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){
158724d70a2SMatthias Ringwald     reverse_bytes(src, dest, 6);
159724d70a2SMatthias Ringwald }
160724d70a2SMatthias Ringwald 
161ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b){
162ebaeb1beSMatthias Ringwald     return a < b ? a : b;
163ebaeb1beSMatthias Ringwald }
164ebaeb1beSMatthias Ringwald 
165ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b){
166ebaeb1beSMatthias Ringwald     return a > b ? a : b;
167ebaeb1beSMatthias Ringwald }
168ebaeb1beSMatthias Ringwald 
169c1c58647SMatthias Ringwald static const char * char_to_nibble = "0123456789ABCDEF";
170c1c58647SMatthias Ringwald 
171eb886013SMatthias Ringwald char char_for_nibble(int nibble){
172c1c58647SMatthias Ringwald     if (nibble < 16){
173c1c58647SMatthias Ringwald         return char_to_nibble[nibble];
174c1c58647SMatthias Ringwald     } else {
175eb886013SMatthias Ringwald         return '?';
176eb886013SMatthias Ringwald     }
177c1c58647SMatthias Ringwald }
178eb886013SMatthias Ringwald 
179405d63a9SMatthias Ringwald static inline char char_for_high_nibble(int value){
180405d63a9SMatthias Ringwald     return char_for_nibble((value >> 4) & 0x0f);
181405d63a9SMatthias Ringwald }
182405d63a9SMatthias Ringwald 
183405d63a9SMatthias Ringwald static inline char char_for_low_nibble(int value){
184405d63a9SMatthias Ringwald     return char_for_nibble(value & 0x0f);
185405d63a9SMatthias Ringwald }
186405d63a9SMatthias Ringwald 
187c1c58647SMatthias Ringwald 
188a6efb919SMatthias Ringwald int nibble_for_char(char c){
189a6efb919SMatthias Ringwald     if (c >= '0' && c <= '9') return c - '0';
1909f507070SMatthias Ringwald     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
1919f507070SMatthias Ringwald     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
192a6efb919SMatthias Ringwald     return -1;
193a6efb919SMatthias Ringwald }
194a6efb919SMatthias Ringwald 
195eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){
196c1c58647SMatthias Ringwald     char buffer[4];
197c1c58647SMatthias Ringwald     buffer[2] = ' ';
198c1c58647SMatthias Ringwald     buffer[3] =  0;
1999dbfa930SMatthias Ringwald     const uint8_t * ptr = (const uint8_t *) data;
200c1c58647SMatthias Ringwald     while (size > 0){
2019dbfa930SMatthias Ringwald         uint8_t byte = *ptr++;
202c1c58647SMatthias Ringwald         buffer[0] = char_for_high_nibble(byte);
203c1c58647SMatthias Ringwald         buffer[1] = char_for_low_nibble(byte);
204c1c58647SMatthias Ringwald         printf("%s", buffer);
205c1c58647SMatthias Ringwald         size--;
206eb886013SMatthias Ringwald     }
207eb886013SMatthias Ringwald     printf("\n");
208eb886013SMatthias Ringwald }
209eb886013SMatthias Ringwald 
210cb42147aSMatthias Ringwald #if defined(ENABLE_LOG_INFO) || defined(ENABLE_LOG_DEBUG)
211cb42147aSMatthias Ringwald static void log_hexdump(int level, const void * data, int size){
212405d63a9SMatthias Ringwald #define ITEMS_PER_LINE 16
213405d63a9SMatthias Ringwald // template '0x12, '
214405d63a9SMatthias Ringwald #define BYTES_PER_BYTE  6
215405d63a9SMatthias Ringwald     char buffer[BYTES_PER_BYTE*ITEMS_PER_LINE+1];
2167224be7eSMatthias Ringwald     int i, j;
217eb886013SMatthias Ringwald     j = 0;
218eb886013SMatthias Ringwald     for (i=0; i<size;i++){
2197224be7eSMatthias Ringwald 
2207224be7eSMatthias Ringwald         // help static analyzer proof that j stays within bounds
221405d63a9SMatthias Ringwald         if (j > BYTES_PER_BYTE * (ITEMS_PER_LINE-1)){
2227224be7eSMatthias Ringwald             j = 0;
2237224be7eSMatthias Ringwald         }
2247224be7eSMatthias Ringwald 
225eb886013SMatthias Ringwald         uint8_t byte = ((uint8_t *)data)[i];
226eb886013SMatthias Ringwald         buffer[j++] = '0';
227eb886013SMatthias Ringwald         buffer[j++] = 'x';
228405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
229405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
230eb886013SMatthias Ringwald         buffer[j++] = ',';
231eb886013SMatthias Ringwald         buffer[j++] = ' ';
2327224be7eSMatthias Ringwald 
233405d63a9SMatthias Ringwald         if (j >= BYTES_PER_BYTE * ITEMS_PER_LINE ){
234eb886013SMatthias Ringwald             buffer[j] = 0;
235cb42147aSMatthias Ringwald             HCI_DUMP_LOG(level, "%s", buffer);
236eb886013SMatthias Ringwald             j = 0;
237eb886013SMatthias Ringwald         }
238eb886013SMatthias Ringwald     }
239eb886013SMatthias Ringwald     if (j != 0){
240eb886013SMatthias Ringwald         buffer[j] = 0;
241cb42147aSMatthias Ringwald         HCI_DUMP_LOG(level, "%s", buffer);
242eb886013SMatthias Ringwald     }
243cb42147aSMatthias Ringwald }
244cb42147aSMatthias Ringwald #endif
245cb42147aSMatthias Ringwald 
246cb42147aSMatthias Ringwald void log_debug_hexdump(const void *data, int size){
247e950fa96SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG
248fa087deaSMatthias Ringwald     log_hexdump(HCI_DUMP_LOG_LEVEL_DEBUG, data, size);
249cb42147aSMatthias Ringwald #else
2504bd79111SMatthias Ringwald     UNUSED(data);   // ok: no code
2514bd79111SMatthias Ringwald     UNUSED(size);   // ok: no code
252cb42147aSMatthias Ringwald #endif
253cb42147aSMatthias Ringwald }
254cb42147aSMatthias Ringwald 
255cb42147aSMatthias Ringwald void log_info_hexdump(const void *data, int size){
256cb42147aSMatthias Ringwald #ifdef ENABLE_LOG_INFO
257fa087deaSMatthias Ringwald     log_hexdump(HCI_DUMP_LOG_LEVEL_INFO, data, size);
258d0662982SMatthias Ringwald #else
2594bd79111SMatthias Ringwald     UNUSED(data);   // ok: no code
2604bd79111SMatthias Ringwald     UNUSED(size);   // ok: no code
2618314c363SMatthias Ringwald #endif
2627299c0feSMatthias Ringwald }
263eb886013SMatthias Ringwald 
2648314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key){
26502bdfbf8SMatthias Ringwald #ifdef ENABLE_LOG_INFO
26602bdfbf8SMatthias Ringwald     char buffer[16*2+1];
26702bdfbf8SMatthias Ringwald     int i;
26802bdfbf8SMatthias Ringwald     int j = 0;
26902bdfbf8SMatthias Ringwald     for (i=0; i<16;i++){
27002bdfbf8SMatthias Ringwald         uint8_t byte = key[i];
271405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
272405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
27302bdfbf8SMatthias Ringwald     }
27402bdfbf8SMatthias Ringwald     buffer[j] = 0;
27502bdfbf8SMatthias Ringwald     log_info("%-6s %s", name, buffer);
276d0662982SMatthias Ringwald #else
277d0662982SMatthias Ringwald     UNUSED(name);
278cb42147aSMatthias Ringwald     (void)key;
27902bdfbf8SMatthias Ringwald #endif
280eb886013SMatthias Ringwald }
281eb886013SMatthias Ringwald 
2822b604902SMatthias Ringwald // UUIDs are stored in big endian, similar to bd_addr_t
2832b604902SMatthias Ringwald 
284eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB
2852b604902SMatthias Ringwald const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */
286eb886013SMatthias Ringwald     0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
287eb886013SMatthias Ringwald 
288e1a125dfSMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){
2892b604902SMatthias Ringwald     memcpy(uuid, bluetooth_base_uuid, 16);
290f8fbdce0SMatthias Ringwald     big_endian_store_32(uuid, 0, shortUUID);
291eb886013SMatthias Ringwald }
292eb886013SMatthias Ringwald 
2935222912bSMatthias Ringwald int uuid_has_bluetooth_prefix(const uint8_t * uuid128){
2942b604902SMatthias Ringwald     return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0;
295eb886013SMatthias Ringwald }
296eb886013SMatthias Ringwald 
297eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1];
2985222912bSMatthias Ringwald char * uuid128_to_str(const uint8_t * uuid){
2997224be7eSMatthias Ringwald     int i;
3007224be7eSMatthias Ringwald     int j = 0;
3017224be7eSMatthias Ringwald     // after 4, 6, 8, and 10 bytes = XYXYXYXY-XYXY-XYXY-XYXY-XYXYXYXYXYXY, there's a dash
3027224be7eSMatthias Ringwald     const int dash_locations = (1<<3) | (1<<5) | (1<<7) | (1<<9);
3037224be7eSMatthias Ringwald     for (i=0;i<16;i++){
304405d63a9SMatthias Ringwald         uint8_t byte = uuid[i];
305405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_high_nibble(byte);
306405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_low_nibble(byte);
3077224be7eSMatthias Ringwald         if (dash_locations & (1<<i)){
3087224be7eSMatthias Ringwald             uuid128_to_str_buffer[j++] = '-';
3097224be7eSMatthias Ringwald         }
3107224be7eSMatthias Ringwald     }
311eb886013SMatthias Ringwald     return uuid128_to_str_buffer;
312eb886013SMatthias Ringwald }
313eb886013SMatthias Ringwald 
314eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3];  // 12:45:78:01:34:67\0
3155222912bSMatthias Ringwald char * bd_addr_to_str(const bd_addr_t addr){
316eb886013SMatthias Ringwald     // orig code
317eb886013SMatthias 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]);
318eb886013SMatthias Ringwald     // sprintf-free code
319eb886013SMatthias Ringwald     char * p = bd_addr_to_str_buffer;
320eb886013SMatthias Ringwald     int i;
321eb886013SMatthias Ringwald     for (i = 0; i < 6 ; i++) {
322405d63a9SMatthias Ringwald         uint8_t byte = addr[i];
323405d63a9SMatthias Ringwald         *p++ = char_for_high_nibble(byte);
324405d63a9SMatthias Ringwald         *p++ = char_for_low_nibble(byte);
325eb886013SMatthias Ringwald         *p++ = ':';
326eb886013SMatthias Ringwald     }
327eb886013SMatthias Ringwald     *--p = 0;
328eb886013SMatthias Ringwald     return (char *) bd_addr_to_str_buffer;
329eb886013SMatthias Ringwald }
330eb886013SMatthias Ringwald 
331a6efb919SMatthias Ringwald static int scan_hex_byte(const char * byte_string){
332a6efb919SMatthias Ringwald     int upper_nibble = nibble_for_char(*byte_string++);
333a6efb919SMatthias Ringwald     if (upper_nibble < 0) return -1;
334a6efb919SMatthias Ringwald     int lower_nibble = nibble_for_char(*byte_string);
335a6efb919SMatthias Ringwald     if (lower_nibble < 0) return -1;
336a6efb919SMatthias Ringwald     return (upper_nibble << 4) | lower_nibble;
337a6efb919SMatthias Ringwald }
338eb886013SMatthias Ringwald 
339a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){
340a6efb919SMatthias Ringwald     uint8_t buffer[BD_ADDR_LEN];
341a6efb919SMatthias Ringwald     int result = 0;
342eb886013SMatthias Ringwald     int i;
343eb886013SMatthias Ringwald     for (i = 0; i < BD_ADDR_LEN; i++) {
344a6efb919SMatthias Ringwald         int single_byte = scan_hex_byte(addr_string);
345a6efb919SMatthias Ringwald         if (single_byte < 0) break;
346a6efb919SMatthias Ringwald         addr_string += 2;
347b0920f25SMilanka Ringwald         buffer[i] = (uint8_t)single_byte;
348a6efb919SMatthias Ringwald         // don't check seperator after last byte
349a6efb919SMatthias Ringwald         if (i == BD_ADDR_LEN - 1) {
350a6efb919SMatthias Ringwald             result = 1;
351a6efb919SMatthias Ringwald             break;
352eb886013SMatthias Ringwald         }
353cd2e416cSMatthias Ringwald         // skip supported separators
354cd2e416cSMatthias Ringwald         char next_char = *addr_string;
355cd2e416cSMatthias Ringwald         if (next_char == ':' || next_char == '-' || next_char == ' ') {
356cd2e416cSMatthias Ringwald             addr_string++;
357cd2e416cSMatthias Ringwald         }
358a6efb919SMatthias Ringwald     }
359a6efb919SMatthias Ringwald 
360a6efb919SMatthias Ringwald     if (result){
361a6efb919SMatthias Ringwald         bd_addr_copy(addr, buffer);
362a6efb919SMatthias Ringwald     }
363a6efb919SMatthias Ringwald 	return result;
364eb886013SMatthias Ringwald }
3655d067ab1SMatthias Ringwald 
3665d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char *str){
3675d067ab1SMatthias Ringwald     uint32_t val = 0;
3685d067ab1SMatthias Ringwald     while (1){
3695d067ab1SMatthias Ringwald         char chr = *str;
3705d067ab1SMatthias Ringwald         if (!chr || chr < '0' || chr > '9')
3715d067ab1SMatthias Ringwald             return val;
372b0920f25SMilanka Ringwald         val = (val * 10) + (uint8_t)(chr - '0');
3735d067ab1SMatthias Ringwald         str++;
3745d067ab1SMatthias Ringwald     }
3755d067ab1SMatthias Ringwald }
3761f41c2c9SMatthias Ringwald 
377d1207cd8SMilanka Ringwald int string_len_for_uint32(uint32_t i){
378d1207cd8SMilanka Ringwald     if (i <         10) return 1;
379d1207cd8SMilanka Ringwald     if (i <        100) return 2;
380d1207cd8SMilanka Ringwald     if (i <       1000) return 3;
381d1207cd8SMilanka Ringwald     if (i <      10000) return 4;
382d1207cd8SMilanka Ringwald     if (i <     100000) return 5;
383d1207cd8SMilanka Ringwald     if (i <    1000000) return 6;
384d1207cd8SMilanka Ringwald     if (i <   10000000) return 7;
385d1207cd8SMilanka Ringwald     if (i <  100000000) return 8;
386d1207cd8SMilanka Ringwald     if (i < 1000000000) return 9;
387d1207cd8SMilanka Ringwald     return 10;
388d1207cd8SMilanka Ringwald }
389d1207cd8SMilanka Ringwald 
390d1207cd8SMilanka Ringwald int count_set_bits_uint32(uint32_t x){
391d1207cd8SMilanka Ringwald     x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
392d1207cd8SMilanka Ringwald     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
393d1207cd8SMilanka Ringwald     x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
394d1207cd8SMilanka Ringwald     x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
395d1207cd8SMilanka Ringwald     x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
396d1207cd8SMilanka Ringwald     return x;
397d1207cd8SMilanka Ringwald }
3981f41c2c9SMatthias Ringwald 
3991f41c2c9SMatthias Ringwald /*
4001f41c2c9SMatthias Ringwald  * CRC (reversed crc) lookup table as calculated by the table generator in ETSI TS 101 369 V6.3.0.
4011f41c2c9SMatthias Ringwald  */
4021f41c2c9SMatthias Ringwald 
4031f41c2c9SMatthias Ringwald #define CRC8_INIT  0xFF          // Initial FCS value
4041f41c2c9SMatthias Ringwald #define CRC8_OK    0xCF          // Good final FCS value
4051f41c2c9SMatthias Ringwald 
4061f41c2c9SMatthias Ringwald static const uint8_t crc8table[256] = {    /* reversed, 8-bit, poly=0x07 */
4071f41c2c9SMatthias Ringwald     0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
4081f41c2c9SMatthias Ringwald     0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
4091f41c2c9SMatthias Ringwald     0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
4101f41c2c9SMatthias Ringwald     0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
4111f41c2c9SMatthias Ringwald     0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
4121f41c2c9SMatthias Ringwald     0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
4131f41c2c9SMatthias Ringwald     0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
4141f41c2c9SMatthias Ringwald     0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
4151f41c2c9SMatthias Ringwald     0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
4161f41c2c9SMatthias Ringwald     0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
4171f41c2c9SMatthias Ringwald     0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
4181f41c2c9SMatthias Ringwald     0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
4191f41c2c9SMatthias Ringwald     0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
4201f41c2c9SMatthias Ringwald     0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
4211f41c2c9SMatthias Ringwald     0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
4221f41c2c9SMatthias Ringwald     0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
4231f41c2c9SMatthias Ringwald };
4241f41c2c9SMatthias Ringwald 
4251f41c2c9SMatthias Ringwald /*-----------------------------------------------------------------------------------*/
4261f41c2c9SMatthias Ringwald static uint8_t crc8(uint8_t *data, uint16_t len){
4271f41c2c9SMatthias Ringwald     uint16_t count;
4281f41c2c9SMatthias Ringwald     uint8_t crc = CRC8_INIT;
4291f41c2c9SMatthias Ringwald     for (count = 0; count < len; count++){
4301f41c2c9SMatthias Ringwald         crc = crc8table[crc ^ data[count]];
4311f41c2c9SMatthias Ringwald     }
4321f41c2c9SMatthias Ringwald     return crc;
4331f41c2c9SMatthias Ringwald }
4341f41c2c9SMatthias Ringwald 
4351f41c2c9SMatthias Ringwald /*-----------------------------------------------------------------------------------*/
43663dd1c76SMatthias Ringwald uint8_t btstack_crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum){
4371f41c2c9SMatthias Ringwald     uint8_t crc;
4381f41c2c9SMatthias Ringwald     crc = crc8(data, len);
4391f41c2c9SMatthias Ringwald     crc = crc8table[crc ^ check_sum];
4401f41c2c9SMatthias Ringwald     if (crc == CRC8_OK){
4411f41c2c9SMatthias Ringwald         return 0;               /* Valid */
4421f41c2c9SMatthias Ringwald     } else {
4431f41c2c9SMatthias Ringwald         return 1;               /* Failed */
4441f41c2c9SMatthias Ringwald     }
4451f41c2c9SMatthias Ringwald }
4461f41c2c9SMatthias Ringwald 
4471f41c2c9SMatthias Ringwald /*-----------------------------------------------------------------------------------*/
44863dd1c76SMatthias Ringwald uint8_t btstack_crc8_calc(uint8_t *data, uint16_t len){
4491f41c2c9SMatthias Ringwald     /* Ones complement */
4501f41c2c9SMatthias Ringwald     return 0xFF - crc8(data, len);
4511f41c2c9SMatthias Ringwald }
452