xref: /btstack/src/btstack_util.c (revision 5222912b1f9c3566e39bfc7510c3fa3878d25676)
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 
38ab2c6ae4SMatthias 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  */
61*5222912bSMatthias 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  */
70*5222912bSMatthias 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){
7573988a59SMatthias Ringwald     return ((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){
85eb886013SMatthias Ringwald     buffer[pos++] = value;
86eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
87eb886013SMatthias Ringwald }
88eb886013SMatthias Ringwald 
89f8fbdce0SMatthias Ringwald void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
90eb886013SMatthias Ringwald     buffer[pos++] = value;
91eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
92eb886013SMatthias Ringwald     buffer[pos++] = value >> 16;
93eb886013SMatthias Ringwald     buffer[pos++] = value >> 24;
94eb886013SMatthias Ringwald }
95eb886013SMatthias Ringwald 
9673988a59SMatthias Ringwald uint32_t big_endian_read_16( const uint8_t * buffer, int pos) {
9773988a59SMatthias Ringwald     return ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos   ]) << 8);
9873988a59SMatthias Ringwald }
9973988a59SMatthias Ringwald 
100e57a2545SMilanka Ringwald uint32_t big_endian_read_24( const uint8_t * buffer, int pos) {
101e57a2545SMilanka Ringwald     return ( ((uint32_t)buffer[(pos)+2]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t) buffer[pos]) << 16));
102e57a2545SMilanka Ringwald }
103e57a2545SMilanka Ringwald 
10473988a59SMatthias Ringwald uint32_t big_endian_read_32( const uint8_t * buffer, int pos) {
10573988a59SMatthias Ringwald     return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24);
10673988a59SMatthias Ringwald }
10773988a59SMatthias Ringwald 
108f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
109eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
110eb886013SMatthias Ringwald     buffer[pos++] = value;
111eb886013SMatthias Ringwald }
112eb886013SMatthias Ringwald 
1137f535380SMilanka Ringwald void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value){
1147f535380SMilanka Ringwald     buffer[pos++] = value >> 16;
1157f535380SMilanka Ringwald     buffer[pos++] = value >> 8;
1167f535380SMilanka Ringwald     buffer[pos++] = value;
1177f535380SMilanka Ringwald }
1187f535380SMilanka Ringwald 
119f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
120eb886013SMatthias Ringwald     buffer[pos++] = value >> 24;
121eb886013SMatthias Ringwald     buffer[pos++] = value >> 16;
122eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
123eb886013SMatthias Ringwald     buffer[pos++] = value;
124eb886013SMatthias Ringwald }
125eb886013SMatthias Ringwald 
126eb886013SMatthias Ringwald // general swap/endianess utils
1279c80e4ccSMatthias Ringwald void reverse_bytes(const uint8_t *src, uint8_t *dst, int len){
128eb886013SMatthias Ringwald     int i;
129eb886013SMatthias Ringwald     for (i = 0; i < len; i++)
130eb886013SMatthias Ringwald         dst[len - 1 - i] = src[i];
131eb886013SMatthias Ringwald }
1329c80e4ccSMatthias Ringwald void reverse_24(const uint8_t * src, uint8_t * dst){
1339c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 3);
134eb886013SMatthias Ringwald }
1359c80e4ccSMatthias Ringwald void reverse_48(const uint8_t * src, uint8_t * dst){
1369c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 6);
137bf1b35bfSMatthias Ringwald }
1389c80e4ccSMatthias Ringwald void reverse_56(const uint8_t * src, uint8_t * dst){
1399c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 7);
140eb886013SMatthias Ringwald }
1419c80e4ccSMatthias Ringwald void reverse_64(const uint8_t * src, uint8_t * dst){
1429c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 8);
143eb886013SMatthias Ringwald }
1449c80e4ccSMatthias Ringwald void reverse_128(const uint8_t * src, uint8_t * dst){
1459c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 16);
146eb886013SMatthias Ringwald }
147cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t * src, uint8_t * dst){
148cc7a2d78SMatthias Ringwald     reverse_bytes(src, dst, 32);
149cc7a2d78SMatthias Ringwald }
150eb886013SMatthias Ringwald 
151724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){
152724d70a2SMatthias Ringwald     reverse_bytes(src, dest, 6);
153724d70a2SMatthias Ringwald }
154724d70a2SMatthias Ringwald 
155ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b){
156ebaeb1beSMatthias Ringwald     return a < b ? a : b;
157ebaeb1beSMatthias Ringwald }
158ebaeb1beSMatthias Ringwald 
159ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b){
160ebaeb1beSMatthias Ringwald     return a > b ? a : b;
161ebaeb1beSMatthias Ringwald }
162ebaeb1beSMatthias Ringwald 
163eb886013SMatthias Ringwald char char_for_nibble(int nibble){
164eb886013SMatthias Ringwald     if (nibble < 10) return '0' + nibble;
165eb886013SMatthias Ringwald     nibble -= 10;
166eb886013SMatthias Ringwald     if (nibble < 6) return 'A' + nibble;
167eb886013SMatthias Ringwald     return '?';
168eb886013SMatthias Ringwald }
169eb886013SMatthias Ringwald 
170405d63a9SMatthias Ringwald static inline char char_for_high_nibble(int value){
171405d63a9SMatthias Ringwald     return char_for_nibble((value >> 4) & 0x0f);
172405d63a9SMatthias Ringwald }
173405d63a9SMatthias Ringwald 
174405d63a9SMatthias Ringwald static inline char char_for_low_nibble(int value){
175405d63a9SMatthias Ringwald     return char_for_nibble(value & 0x0f);
176405d63a9SMatthias Ringwald }
177405d63a9SMatthias Ringwald 
178a6efb919SMatthias Ringwald int nibble_for_char(char c){
179a6efb919SMatthias Ringwald     if (c >= '0' && c <= '9') return c - '0';
1809f507070SMatthias Ringwald     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
1819f507070SMatthias Ringwald     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
182a6efb919SMatthias Ringwald     return -1;
183a6efb919SMatthias Ringwald }
184a6efb919SMatthias Ringwald 
185eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){
186eb886013SMatthias Ringwald     if (size <= 0) return;
187eb886013SMatthias Ringwald     int i;
188eb886013SMatthias Ringwald     for (i=0; i<size;i++){
189eb886013SMatthias Ringwald         printf("%02X ", ((uint8_t *)data)[i]);
190eb886013SMatthias Ringwald     }
191eb886013SMatthias Ringwald     printf("\n");
192eb886013SMatthias Ringwald }
193eb886013SMatthias Ringwald 
1948314c363SMatthias Ringwald void log_info_hexdump(const void *data, int size){
195eb886013SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1967224be7eSMatthias Ringwald 
197405d63a9SMatthias Ringwald #define ITEMS_PER_LINE 16
198405d63a9SMatthias Ringwald // template '0x12, '
199405d63a9SMatthias Ringwald #define BYTES_PER_BYTE  6
2007224be7eSMatthias Ringwald 
201405d63a9SMatthias Ringwald     char buffer[BYTES_PER_BYTE*ITEMS_PER_LINE+1];
2027224be7eSMatthias Ringwald     int i, j;
203eb886013SMatthias Ringwald     j = 0;
204eb886013SMatthias Ringwald     for (i=0; i<size;i++){
2057224be7eSMatthias Ringwald 
2067224be7eSMatthias Ringwald         // help static analyzer proof that j stays within bounds
207405d63a9SMatthias Ringwald         if (j > BYTES_PER_BYTE * (ITEMS_PER_LINE-1)){
2087224be7eSMatthias Ringwald             j = 0;
2097224be7eSMatthias Ringwald         }
2107224be7eSMatthias Ringwald 
211eb886013SMatthias Ringwald         uint8_t byte = ((uint8_t *)data)[i];
212eb886013SMatthias Ringwald         buffer[j++] = '0';
213eb886013SMatthias Ringwald         buffer[j++] = 'x';
214405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
215405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
216eb886013SMatthias Ringwald         buffer[j++] = ',';
217eb886013SMatthias Ringwald         buffer[j++] = ' ';
2187224be7eSMatthias Ringwald 
219405d63a9SMatthias Ringwald         if (j >= BYTES_PER_BYTE * ITEMS_PER_LINE ){
220eb886013SMatthias Ringwald             buffer[j] = 0;
221eb886013SMatthias Ringwald             log_info("%s", buffer);
222eb886013SMatthias Ringwald             j = 0;
223eb886013SMatthias Ringwald         }
224eb886013SMatthias Ringwald     }
225eb886013SMatthias Ringwald     if (j != 0){
226eb886013SMatthias Ringwald         buffer[j] = 0;
227eb886013SMatthias Ringwald         log_info("%s", buffer);
228eb886013SMatthias Ringwald     }
229d0662982SMatthias Ringwald #else
230d0662982SMatthias Ringwald     UNUSED(data);
231d0662982SMatthias Ringwald     UNUSED(size);
2328314c363SMatthias Ringwald #endif
2337299c0feSMatthias Ringwald }
234eb886013SMatthias Ringwald 
2358314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key){
23602bdfbf8SMatthias Ringwald #ifdef ENABLE_LOG_INFO
23702bdfbf8SMatthias Ringwald     char buffer[16*2+1];
23802bdfbf8SMatthias Ringwald     int i;
23902bdfbf8SMatthias Ringwald     int j = 0;
24002bdfbf8SMatthias Ringwald     for (i=0; i<16;i++){
24102bdfbf8SMatthias Ringwald         uint8_t byte = key[i];
242405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
243405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
24402bdfbf8SMatthias Ringwald     }
24502bdfbf8SMatthias Ringwald     buffer[j] = 0;
24602bdfbf8SMatthias Ringwald     log_info("%-6s %s", name, buffer);
247d0662982SMatthias Ringwald #else
248d0662982SMatthias Ringwald     UNUSED(name);
249d0662982SMatthias Ringwald     UNUSED(key);
25002bdfbf8SMatthias Ringwald #endif
251eb886013SMatthias Ringwald }
252eb886013SMatthias Ringwald 
2532b604902SMatthias Ringwald // UUIDs are stored in big endian, similar to bd_addr_t
2542b604902SMatthias Ringwald 
255eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB
2562b604902SMatthias Ringwald const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */
257eb886013SMatthias Ringwald     0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
258eb886013SMatthias Ringwald 
259e1a125dfSMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){
2602b604902SMatthias Ringwald     memcpy(uuid, bluetooth_base_uuid, 16);
261f8fbdce0SMatthias Ringwald     big_endian_store_32(uuid, 0, shortUUID);
262eb886013SMatthias Ringwald }
263eb886013SMatthias Ringwald 
264*5222912bSMatthias Ringwald int uuid_has_bluetooth_prefix(const uint8_t * uuid128){
2652b604902SMatthias Ringwald     return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0;
266eb886013SMatthias Ringwald }
267eb886013SMatthias Ringwald 
268eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1];
269*5222912bSMatthias Ringwald char * uuid128_to_str(const uint8_t * uuid){
2707224be7eSMatthias Ringwald     int i;
2717224be7eSMatthias Ringwald     int j = 0;
2727224be7eSMatthias Ringwald     // after 4, 6, 8, and 10 bytes = XYXYXYXY-XYXY-XYXY-XYXY-XYXYXYXYXYXY, there's a dash
2737224be7eSMatthias Ringwald     const int dash_locations = (1<<3) | (1<<5) | (1<<7) | (1<<9);
2747224be7eSMatthias Ringwald     for (i=0;i<16;i++){
275405d63a9SMatthias Ringwald         uint8_t byte = uuid[i];
276405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_high_nibble(byte);
277405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_low_nibble(byte);
2787224be7eSMatthias Ringwald         if (dash_locations & (1<<i)){
2797224be7eSMatthias Ringwald             uuid128_to_str_buffer[j++] = '-';
2807224be7eSMatthias Ringwald         }
2817224be7eSMatthias Ringwald     }
282eb886013SMatthias Ringwald     return uuid128_to_str_buffer;
283eb886013SMatthias Ringwald }
284eb886013SMatthias Ringwald 
285eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3];  // 12:45:78:01:34:67\0
286*5222912bSMatthias Ringwald char * bd_addr_to_str(const bd_addr_t addr){
287eb886013SMatthias Ringwald     // orig code
288eb886013SMatthias 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]);
289eb886013SMatthias Ringwald     // sprintf-free code
290eb886013SMatthias Ringwald     char * p = bd_addr_to_str_buffer;
291eb886013SMatthias Ringwald     int i;
292eb886013SMatthias Ringwald     for (i = 0; i < 6 ; i++) {
293405d63a9SMatthias Ringwald         uint8_t byte = addr[i];
294405d63a9SMatthias Ringwald         *p++ = char_for_high_nibble(byte);
295405d63a9SMatthias Ringwald         *p++ = char_for_low_nibble(byte);
296eb886013SMatthias Ringwald         *p++ = ':';
297eb886013SMatthias Ringwald     }
298eb886013SMatthias Ringwald     *--p = 0;
299eb886013SMatthias Ringwald     return (char *) bd_addr_to_str_buffer;
300eb886013SMatthias Ringwald }
301eb886013SMatthias Ringwald 
302a6efb919SMatthias Ringwald static int scan_hex_byte(const char * byte_string){
303a6efb919SMatthias Ringwald     int upper_nibble = nibble_for_char(*byte_string++);
304a6efb919SMatthias Ringwald     if (upper_nibble < 0) return -1;
305a6efb919SMatthias Ringwald     int lower_nibble = nibble_for_char(*byte_string);
306a6efb919SMatthias Ringwald     if (lower_nibble < 0) return -1;
307a6efb919SMatthias Ringwald     return (upper_nibble << 4) | lower_nibble;
308a6efb919SMatthias Ringwald }
309eb886013SMatthias Ringwald 
310a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){
311a6efb919SMatthias Ringwald     uint8_t buffer[BD_ADDR_LEN];
312a6efb919SMatthias Ringwald     int result = 0;
313eb886013SMatthias Ringwald     int i;
314eb886013SMatthias Ringwald     for (i = 0; i < BD_ADDR_LEN; i++) {
315a6efb919SMatthias Ringwald         int single_byte = scan_hex_byte(addr_string);
316a6efb919SMatthias Ringwald         if (single_byte < 0) break;
317a6efb919SMatthias Ringwald         addr_string += 2;
3183e40861cSMilanka Ringwald         buffer[i] = single_byte;
319a6efb919SMatthias Ringwald         // don't check seperator after last byte
320a6efb919SMatthias Ringwald         if (i == BD_ADDR_LEN - 1) {
321a6efb919SMatthias Ringwald             result = 1;
322a6efb919SMatthias Ringwald             break;
323eb886013SMatthias Ringwald         }
324a6efb919SMatthias Ringwald         char separator = *addr_string++;
325a6efb919SMatthias Ringwald         if (separator != ':' && separator != '-' && separator != ' ') break;
326a6efb919SMatthias Ringwald     }
327a6efb919SMatthias Ringwald 
328a6efb919SMatthias Ringwald     if (result){
329a6efb919SMatthias Ringwald         bd_addr_copy(addr, buffer);
330a6efb919SMatthias Ringwald     }
331a6efb919SMatthias Ringwald 	return result;
332eb886013SMatthias Ringwald }
3335d067ab1SMatthias Ringwald 
3345d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char *str){
3355d067ab1SMatthias Ringwald     uint32_t val = 0;
3365d067ab1SMatthias Ringwald     while (1){
3375d067ab1SMatthias Ringwald         char chr = *str;
3385d067ab1SMatthias Ringwald         if (!chr || chr < '0' || chr > '9')
3395d067ab1SMatthias Ringwald             return val;
3405d067ab1SMatthias Ringwald         val = (val * 10) + (chr - '0');
3415d067ab1SMatthias Ringwald         str++;
3425d067ab1SMatthias Ringwald     }
3435d067ab1SMatthias Ringwald }