xref: /btstack/src/btstack_util.c (revision 7f535380ac9b3f2a4bef0c23ad133caacf42b257)
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 
107*7f535380SMilanka Ringwald void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value){
108*7f535380SMilanka Ringwald     buffer[pos++] = value >> 16;
109*7f535380SMilanka Ringwald     buffer[pos++] = value >> 8;
110*7f535380SMilanka Ringwald     buffer[pos++] = value;
111*7f535380SMilanka Ringwald }
112*7f535380SMilanka Ringwald 
113f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
114eb886013SMatthias Ringwald     buffer[pos++] = value >> 24;
115eb886013SMatthias Ringwald     buffer[pos++] = value >> 16;
116eb886013SMatthias Ringwald     buffer[pos++] = value >> 8;
117eb886013SMatthias Ringwald     buffer[pos++] = value;
118eb886013SMatthias Ringwald }
119eb886013SMatthias Ringwald 
120eb886013SMatthias Ringwald // general swap/endianess utils
1219c80e4ccSMatthias Ringwald void reverse_bytes(const uint8_t *src, uint8_t *dst, int len){
122eb886013SMatthias Ringwald     int i;
123eb886013SMatthias Ringwald     for (i = 0; i < len; i++)
124eb886013SMatthias Ringwald         dst[len - 1 - i] = src[i];
125eb886013SMatthias Ringwald }
1269c80e4ccSMatthias Ringwald void reverse_24(const uint8_t * src, uint8_t * dst){
1279c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 3);
128eb886013SMatthias Ringwald }
1299c80e4ccSMatthias Ringwald void reverse_48(const uint8_t * src, uint8_t * dst){
1309c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 6);
131bf1b35bfSMatthias Ringwald }
1329c80e4ccSMatthias Ringwald void reverse_56(const uint8_t * src, uint8_t * dst){
1339c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 7);
134eb886013SMatthias Ringwald }
1359c80e4ccSMatthias Ringwald void reverse_64(const uint8_t * src, uint8_t * dst){
1369c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 8);
137eb886013SMatthias Ringwald }
1389c80e4ccSMatthias Ringwald void reverse_128(const uint8_t * src, uint8_t * dst){
1399c80e4ccSMatthias Ringwald     reverse_bytes(src, dst, 16);
140eb886013SMatthias Ringwald }
141cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t * src, uint8_t * dst){
142cc7a2d78SMatthias Ringwald     reverse_bytes(src, dst, 32);
143cc7a2d78SMatthias Ringwald }
144eb886013SMatthias Ringwald 
145724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){
146724d70a2SMatthias Ringwald     reverse_bytes(src, dest, 6);
147724d70a2SMatthias Ringwald }
148724d70a2SMatthias Ringwald 
149ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b){
150ebaeb1beSMatthias Ringwald     return a < b ? a : b;
151ebaeb1beSMatthias Ringwald }
152ebaeb1beSMatthias Ringwald 
153ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b){
154ebaeb1beSMatthias Ringwald     return a > b ? a : b;
155ebaeb1beSMatthias Ringwald }
156ebaeb1beSMatthias Ringwald 
157eb886013SMatthias Ringwald char char_for_nibble(int nibble){
158eb886013SMatthias Ringwald     if (nibble < 10) return '0' + nibble;
159eb886013SMatthias Ringwald     nibble -= 10;
160eb886013SMatthias Ringwald     if (nibble < 6) return 'A' + nibble;
161eb886013SMatthias Ringwald     return '?';
162eb886013SMatthias Ringwald }
163eb886013SMatthias Ringwald 
164405d63a9SMatthias Ringwald static inline char char_for_high_nibble(int value){
165405d63a9SMatthias Ringwald     return char_for_nibble((value >> 4) & 0x0f);
166405d63a9SMatthias Ringwald }
167405d63a9SMatthias Ringwald 
168405d63a9SMatthias Ringwald static inline char char_for_low_nibble(int value){
169405d63a9SMatthias Ringwald     return char_for_nibble(value & 0x0f);
170405d63a9SMatthias Ringwald }
171405d63a9SMatthias Ringwald 
172a6efb919SMatthias Ringwald int nibble_for_char(char c){
173a6efb919SMatthias Ringwald     if (c >= '0' && c <= '9') return c - '0';
1749f507070SMatthias Ringwald     if (c >= 'a' && c <= 'f') return c - 'a' + 10;
1759f507070SMatthias Ringwald     if (c >= 'A' && c <= 'F') return c - 'A' + 10;
176a6efb919SMatthias Ringwald     return -1;
177a6efb919SMatthias Ringwald }
178a6efb919SMatthias Ringwald 
179eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){
180eb886013SMatthias Ringwald     if (size <= 0) return;
181eb886013SMatthias Ringwald     int i;
182eb886013SMatthias Ringwald     for (i=0; i<size;i++){
183eb886013SMatthias Ringwald         printf("%02X ", ((uint8_t *)data)[i]);
184eb886013SMatthias Ringwald     }
185eb886013SMatthias Ringwald     printf("\n");
186eb886013SMatthias Ringwald }
187eb886013SMatthias Ringwald 
1888314c363SMatthias Ringwald void log_info_hexdump(const void *data, int size){
189eb886013SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1907224be7eSMatthias Ringwald 
191405d63a9SMatthias Ringwald #define ITEMS_PER_LINE 16
192405d63a9SMatthias Ringwald // template '0x12, '
193405d63a9SMatthias Ringwald #define BYTES_PER_BYTE  6
1947224be7eSMatthias Ringwald 
195405d63a9SMatthias Ringwald     char buffer[BYTES_PER_BYTE*ITEMS_PER_LINE+1];
1967224be7eSMatthias Ringwald     int i, j;
197eb886013SMatthias Ringwald     j = 0;
198eb886013SMatthias Ringwald     for (i=0; i<size;i++){
1997224be7eSMatthias Ringwald 
2007224be7eSMatthias Ringwald         // help static analyzer proof that j stays within bounds
201405d63a9SMatthias Ringwald         if (j > BYTES_PER_BYTE * (ITEMS_PER_LINE-1)){
2027224be7eSMatthias Ringwald             j = 0;
2037224be7eSMatthias Ringwald         }
2047224be7eSMatthias Ringwald 
205eb886013SMatthias Ringwald         uint8_t byte = ((uint8_t *)data)[i];
206eb886013SMatthias Ringwald         buffer[j++] = '0';
207eb886013SMatthias Ringwald         buffer[j++] = 'x';
208405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
209405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
210eb886013SMatthias Ringwald         buffer[j++] = ',';
211eb886013SMatthias Ringwald         buffer[j++] = ' ';
2127224be7eSMatthias Ringwald 
213405d63a9SMatthias Ringwald         if (j >= BYTES_PER_BYTE * ITEMS_PER_LINE ){
214eb886013SMatthias Ringwald             buffer[j] = 0;
215eb886013SMatthias Ringwald             log_info("%s", buffer);
216eb886013SMatthias Ringwald             j = 0;
217eb886013SMatthias Ringwald         }
218eb886013SMatthias Ringwald     }
219eb886013SMatthias Ringwald     if (j != 0){
220eb886013SMatthias Ringwald         buffer[j] = 0;
221eb886013SMatthias Ringwald         log_info("%s", buffer);
222eb886013SMatthias Ringwald     }
223d0662982SMatthias Ringwald #else
224d0662982SMatthias Ringwald     UNUSED(data);
225d0662982SMatthias Ringwald     UNUSED(size);
2268314c363SMatthias Ringwald #endif
2277299c0feSMatthias Ringwald }
228eb886013SMatthias Ringwald 
2298314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key){
23002bdfbf8SMatthias Ringwald #ifdef ENABLE_LOG_INFO
23102bdfbf8SMatthias Ringwald     char buffer[16*2+1];
23202bdfbf8SMatthias Ringwald     int i;
23302bdfbf8SMatthias Ringwald     int j = 0;
23402bdfbf8SMatthias Ringwald     for (i=0; i<16;i++){
23502bdfbf8SMatthias Ringwald         uint8_t byte = key[i];
236405d63a9SMatthias Ringwald         buffer[j++] = char_for_high_nibble(byte);
237405d63a9SMatthias Ringwald         buffer[j++] = char_for_low_nibble(byte);
23802bdfbf8SMatthias Ringwald     }
23902bdfbf8SMatthias Ringwald     buffer[j] = 0;
24002bdfbf8SMatthias Ringwald     log_info("%-6s %s", name, buffer);
241d0662982SMatthias Ringwald #else
242d0662982SMatthias Ringwald     UNUSED(name);
243d0662982SMatthias Ringwald     UNUSED(key);
24402bdfbf8SMatthias Ringwald #endif
245eb886013SMatthias Ringwald }
246eb886013SMatthias Ringwald 
2472b604902SMatthias Ringwald // UUIDs are stored in big endian, similar to bd_addr_t
2482b604902SMatthias Ringwald 
249eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB
2502b604902SMatthias Ringwald const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */
251eb886013SMatthias Ringwald     0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
252eb886013SMatthias Ringwald 
253e1a125dfSMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){
2542b604902SMatthias Ringwald     memcpy(uuid, bluetooth_base_uuid, 16);
255f8fbdce0SMatthias Ringwald     big_endian_store_32(uuid, 0, shortUUID);
256eb886013SMatthias Ringwald }
257eb886013SMatthias Ringwald 
258e1a125dfSMatthias Ringwald int uuid_has_bluetooth_prefix(uint8_t * uuid128){
2592b604902SMatthias Ringwald     return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0;
260eb886013SMatthias Ringwald }
261eb886013SMatthias Ringwald 
262eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1];
263eb886013SMatthias Ringwald char * uuid128_to_str(uint8_t * uuid){
2647224be7eSMatthias Ringwald     int i;
2657224be7eSMatthias Ringwald     int j = 0;
2667224be7eSMatthias Ringwald     // after 4, 6, 8, and 10 bytes = XYXYXYXY-XYXY-XYXY-XYXY-XYXYXYXYXYXY, there's a dash
2677224be7eSMatthias Ringwald     const int dash_locations = (1<<3) | (1<<5) | (1<<7) | (1<<9);
2687224be7eSMatthias Ringwald     for (i=0;i<16;i++){
269405d63a9SMatthias Ringwald         uint8_t byte = uuid[i];
270405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_high_nibble(byte);
271405d63a9SMatthias Ringwald         uuid128_to_str_buffer[j++] = char_for_low_nibble(byte);
2727224be7eSMatthias Ringwald         if (dash_locations & (1<<i)){
2737224be7eSMatthias Ringwald             uuid128_to_str_buffer[j++] = '-';
2747224be7eSMatthias Ringwald         }
2757224be7eSMatthias Ringwald     }
276eb886013SMatthias Ringwald     return uuid128_to_str_buffer;
277eb886013SMatthias Ringwald }
278eb886013SMatthias Ringwald 
279eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3];  // 12:45:78:01:34:67\0
280eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr){
281eb886013SMatthias Ringwald     // orig code
282eb886013SMatthias 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]);
283eb886013SMatthias Ringwald     // sprintf-free code
284eb886013SMatthias Ringwald     char * p = bd_addr_to_str_buffer;
285eb886013SMatthias Ringwald     int i;
286eb886013SMatthias Ringwald     for (i = 0; i < 6 ; i++) {
287405d63a9SMatthias Ringwald         uint8_t byte = addr[i];
288405d63a9SMatthias Ringwald         *p++ = char_for_high_nibble(byte);
289405d63a9SMatthias Ringwald         *p++ = char_for_low_nibble(byte);
290eb886013SMatthias Ringwald         *p++ = ':';
291eb886013SMatthias Ringwald     }
292eb886013SMatthias Ringwald     *--p = 0;
293eb886013SMatthias Ringwald     return (char *) bd_addr_to_str_buffer;
294eb886013SMatthias Ringwald }
295eb886013SMatthias Ringwald 
296a6efb919SMatthias Ringwald static int scan_hex_byte(const char * byte_string){
297a6efb919SMatthias Ringwald     int upper_nibble = nibble_for_char(*byte_string++);
298a6efb919SMatthias Ringwald     if (upper_nibble < 0) return -1;
299a6efb919SMatthias Ringwald     int lower_nibble = nibble_for_char(*byte_string);
300a6efb919SMatthias Ringwald     if (lower_nibble < 0) return -1;
301a6efb919SMatthias Ringwald     return (upper_nibble << 4) | lower_nibble;
302a6efb919SMatthias Ringwald }
303eb886013SMatthias Ringwald 
304a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){
305a6efb919SMatthias Ringwald     uint8_t buffer[BD_ADDR_LEN];
306a6efb919SMatthias Ringwald     int result = 0;
307eb886013SMatthias Ringwald     int i;
308eb886013SMatthias Ringwald     for (i = 0; i < BD_ADDR_LEN; i++) {
309a6efb919SMatthias Ringwald         int single_byte = scan_hex_byte(addr_string);
310a6efb919SMatthias Ringwald         if (single_byte < 0) break;
311a6efb919SMatthias Ringwald         addr_string += 2;
3123e40861cSMilanka Ringwald         buffer[i] = single_byte;
313a6efb919SMatthias Ringwald         // don't check seperator after last byte
314a6efb919SMatthias Ringwald         if (i == BD_ADDR_LEN - 1) {
315a6efb919SMatthias Ringwald             result = 1;
316a6efb919SMatthias Ringwald             break;
317eb886013SMatthias Ringwald         }
318a6efb919SMatthias Ringwald         char separator = *addr_string++;
319a6efb919SMatthias Ringwald         if (separator != ':' && separator != '-' && separator != ' ') break;
320a6efb919SMatthias Ringwald     }
321a6efb919SMatthias Ringwald 
322a6efb919SMatthias Ringwald     if (result){
323a6efb919SMatthias Ringwald         bd_addr_copy(addr, buffer);
324a6efb919SMatthias Ringwald     }
325a6efb919SMatthias Ringwald 	return result;
326eb886013SMatthias Ringwald }
3275d067ab1SMatthias Ringwald 
3285d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char *str){
3295d067ab1SMatthias Ringwald     uint32_t val = 0;
3305d067ab1SMatthias Ringwald     while (1){
3315d067ab1SMatthias Ringwald         char chr = *str;
3325d067ab1SMatthias Ringwald         if (!chr || chr < '0' || chr > '9')
3335d067ab1SMatthias Ringwald             return val;
3345d067ab1SMatthias Ringwald         val = (val * 10) + (chr - '0');
3355d067ab1SMatthias Ringwald         str++;
3365d067ab1SMatthias Ringwald     }
3375d067ab1SMatthias Ringwald }