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 98*e57a2545SMilanka Ringwald uint32_t big_endian_read_24( const uint8_t * buffer, int pos) { 99*e57a2545SMilanka Ringwald return ( ((uint32_t)buffer[(pos)+2]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t) buffer[pos]) << 16)); 100*e57a2545SMilanka Ringwald } 101*e57a2545SMilanka Ringwald 10273988a59SMatthias Ringwald uint32_t big_endian_read_32( const uint8_t * buffer, int pos) { 10373988a59SMatthias Ringwald return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24); 10473988a59SMatthias Ringwald } 10573988a59SMatthias Ringwald 106f8fbdce0SMatthias Ringwald void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 107eb886013SMatthias Ringwald buffer[pos++] = value >> 8; 108eb886013SMatthias Ringwald buffer[pos++] = value; 109eb886013SMatthias Ringwald } 110eb886013SMatthias Ringwald 1117f535380SMilanka Ringwald void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value){ 1127f535380SMilanka Ringwald buffer[pos++] = value >> 16; 1137f535380SMilanka Ringwald buffer[pos++] = value >> 8; 1147f535380SMilanka Ringwald buffer[pos++] = value; 1157f535380SMilanka Ringwald } 1167f535380SMilanka Ringwald 117f8fbdce0SMatthias Ringwald void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ 118eb886013SMatthias Ringwald buffer[pos++] = value >> 24; 119eb886013SMatthias Ringwald buffer[pos++] = value >> 16; 120eb886013SMatthias Ringwald buffer[pos++] = value >> 8; 121eb886013SMatthias Ringwald buffer[pos++] = value; 122eb886013SMatthias Ringwald } 123eb886013SMatthias Ringwald 124eb886013SMatthias Ringwald // general swap/endianess utils 1259c80e4ccSMatthias Ringwald void reverse_bytes(const uint8_t *src, uint8_t *dst, int len){ 126eb886013SMatthias Ringwald int i; 127eb886013SMatthias Ringwald for (i = 0; i < len; i++) 128eb886013SMatthias Ringwald dst[len - 1 - i] = src[i]; 129eb886013SMatthias Ringwald } 1309c80e4ccSMatthias Ringwald void reverse_24(const uint8_t * src, uint8_t * dst){ 1319c80e4ccSMatthias Ringwald reverse_bytes(src, dst, 3); 132eb886013SMatthias Ringwald } 1339c80e4ccSMatthias Ringwald void reverse_48(const uint8_t * src, uint8_t * dst){ 1349c80e4ccSMatthias Ringwald reverse_bytes(src, dst, 6); 135bf1b35bfSMatthias Ringwald } 1369c80e4ccSMatthias Ringwald void reverse_56(const uint8_t * src, uint8_t * dst){ 1379c80e4ccSMatthias Ringwald reverse_bytes(src, dst, 7); 138eb886013SMatthias Ringwald } 1399c80e4ccSMatthias Ringwald void reverse_64(const uint8_t * src, uint8_t * dst){ 1409c80e4ccSMatthias Ringwald reverse_bytes(src, dst, 8); 141eb886013SMatthias Ringwald } 1429c80e4ccSMatthias Ringwald void reverse_128(const uint8_t * src, uint8_t * dst){ 1439c80e4ccSMatthias Ringwald reverse_bytes(src, dst, 16); 144eb886013SMatthias Ringwald } 145cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t * src, uint8_t * dst){ 146cc7a2d78SMatthias Ringwald reverse_bytes(src, dst, 32); 147cc7a2d78SMatthias Ringwald } 148eb886013SMatthias Ringwald 149724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){ 150724d70a2SMatthias Ringwald reverse_bytes(src, dest, 6); 151724d70a2SMatthias Ringwald } 152724d70a2SMatthias Ringwald 153ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b){ 154ebaeb1beSMatthias Ringwald return a < b ? a : b; 155ebaeb1beSMatthias Ringwald } 156ebaeb1beSMatthias Ringwald 157ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b){ 158ebaeb1beSMatthias Ringwald return a > b ? a : b; 159ebaeb1beSMatthias Ringwald } 160ebaeb1beSMatthias Ringwald 161eb886013SMatthias Ringwald char char_for_nibble(int nibble){ 162eb886013SMatthias Ringwald if (nibble < 10) return '0' + nibble; 163eb886013SMatthias Ringwald nibble -= 10; 164eb886013SMatthias Ringwald if (nibble < 6) return 'A' + nibble; 165eb886013SMatthias Ringwald return '?'; 166eb886013SMatthias Ringwald } 167eb886013SMatthias Ringwald 168405d63a9SMatthias Ringwald static inline char char_for_high_nibble(int value){ 169405d63a9SMatthias Ringwald return char_for_nibble((value >> 4) & 0x0f); 170405d63a9SMatthias Ringwald } 171405d63a9SMatthias Ringwald 172405d63a9SMatthias Ringwald static inline char char_for_low_nibble(int value){ 173405d63a9SMatthias Ringwald return char_for_nibble(value & 0x0f); 174405d63a9SMatthias Ringwald } 175405d63a9SMatthias Ringwald 176a6efb919SMatthias Ringwald int nibble_for_char(char c){ 177a6efb919SMatthias Ringwald if (c >= '0' && c <= '9') return c - '0'; 1789f507070SMatthias Ringwald if (c >= 'a' && c <= 'f') return c - 'a' + 10; 1799f507070SMatthias Ringwald if (c >= 'A' && c <= 'F') return c - 'A' + 10; 180a6efb919SMatthias Ringwald return -1; 181a6efb919SMatthias Ringwald } 182a6efb919SMatthias Ringwald 183eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){ 184eb886013SMatthias Ringwald if (size <= 0) return; 185eb886013SMatthias Ringwald int i; 186eb886013SMatthias Ringwald for (i=0; i<size;i++){ 187eb886013SMatthias Ringwald printf("%02X ", ((uint8_t *)data)[i]); 188eb886013SMatthias Ringwald } 189eb886013SMatthias Ringwald printf("\n"); 190eb886013SMatthias Ringwald } 191eb886013SMatthias Ringwald 1928314c363SMatthias Ringwald void log_info_hexdump(const void *data, int size){ 193eb886013SMatthias Ringwald #ifdef ENABLE_LOG_INFO 1947224be7eSMatthias Ringwald 195405d63a9SMatthias Ringwald #define ITEMS_PER_LINE 16 196405d63a9SMatthias Ringwald // template '0x12, ' 197405d63a9SMatthias Ringwald #define BYTES_PER_BYTE 6 1987224be7eSMatthias Ringwald 199405d63a9SMatthias Ringwald char buffer[BYTES_PER_BYTE*ITEMS_PER_LINE+1]; 2007224be7eSMatthias Ringwald int i, j; 201eb886013SMatthias Ringwald j = 0; 202eb886013SMatthias Ringwald for (i=0; i<size;i++){ 2037224be7eSMatthias Ringwald 2047224be7eSMatthias Ringwald // help static analyzer proof that j stays within bounds 205405d63a9SMatthias Ringwald if (j > BYTES_PER_BYTE * (ITEMS_PER_LINE-1)){ 2067224be7eSMatthias Ringwald j = 0; 2077224be7eSMatthias Ringwald } 2087224be7eSMatthias Ringwald 209eb886013SMatthias Ringwald uint8_t byte = ((uint8_t *)data)[i]; 210eb886013SMatthias Ringwald buffer[j++] = '0'; 211eb886013SMatthias Ringwald buffer[j++] = 'x'; 212405d63a9SMatthias Ringwald buffer[j++] = char_for_high_nibble(byte); 213405d63a9SMatthias Ringwald buffer[j++] = char_for_low_nibble(byte); 214eb886013SMatthias Ringwald buffer[j++] = ','; 215eb886013SMatthias Ringwald buffer[j++] = ' '; 2167224be7eSMatthias Ringwald 217405d63a9SMatthias Ringwald if (j >= BYTES_PER_BYTE * ITEMS_PER_LINE ){ 218eb886013SMatthias Ringwald buffer[j] = 0; 219eb886013SMatthias Ringwald log_info("%s", buffer); 220eb886013SMatthias Ringwald j = 0; 221eb886013SMatthias Ringwald } 222eb886013SMatthias Ringwald } 223eb886013SMatthias Ringwald if (j != 0){ 224eb886013SMatthias Ringwald buffer[j] = 0; 225eb886013SMatthias Ringwald log_info("%s", buffer); 226eb886013SMatthias Ringwald } 227d0662982SMatthias Ringwald #else 228d0662982SMatthias Ringwald UNUSED(data); 229d0662982SMatthias Ringwald UNUSED(size); 2308314c363SMatthias Ringwald #endif 2317299c0feSMatthias Ringwald } 232eb886013SMatthias Ringwald 2338314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key){ 23402bdfbf8SMatthias Ringwald #ifdef ENABLE_LOG_INFO 23502bdfbf8SMatthias Ringwald char buffer[16*2+1]; 23602bdfbf8SMatthias Ringwald int i; 23702bdfbf8SMatthias Ringwald int j = 0; 23802bdfbf8SMatthias Ringwald for (i=0; i<16;i++){ 23902bdfbf8SMatthias Ringwald uint8_t byte = key[i]; 240405d63a9SMatthias Ringwald buffer[j++] = char_for_high_nibble(byte); 241405d63a9SMatthias Ringwald buffer[j++] = char_for_low_nibble(byte); 24202bdfbf8SMatthias Ringwald } 24302bdfbf8SMatthias Ringwald buffer[j] = 0; 24402bdfbf8SMatthias Ringwald log_info("%-6s %s", name, buffer); 245d0662982SMatthias Ringwald #else 246d0662982SMatthias Ringwald UNUSED(name); 247d0662982SMatthias Ringwald UNUSED(key); 24802bdfbf8SMatthias Ringwald #endif 249eb886013SMatthias Ringwald } 250eb886013SMatthias Ringwald 2512b604902SMatthias Ringwald // UUIDs are stored in big endian, similar to bd_addr_t 2522b604902SMatthias Ringwald 253eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB 2542b604902SMatthias Ringwald const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */ 255eb886013SMatthias Ringwald 0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; 256eb886013SMatthias Ringwald 257e1a125dfSMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){ 2582b604902SMatthias Ringwald memcpy(uuid, bluetooth_base_uuid, 16); 259f8fbdce0SMatthias Ringwald big_endian_store_32(uuid, 0, shortUUID); 260eb886013SMatthias Ringwald } 261eb886013SMatthias Ringwald 262e1a125dfSMatthias Ringwald int uuid_has_bluetooth_prefix(uint8_t * uuid128){ 2632b604902SMatthias Ringwald return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0; 264eb886013SMatthias Ringwald } 265eb886013SMatthias Ringwald 266eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1]; 267eb886013SMatthias Ringwald char * uuid128_to_str(uint8_t * uuid){ 2687224be7eSMatthias Ringwald int i; 2697224be7eSMatthias Ringwald int j = 0; 2707224be7eSMatthias Ringwald // after 4, 6, 8, and 10 bytes = XYXYXYXY-XYXY-XYXY-XYXY-XYXYXYXYXYXY, there's a dash 2717224be7eSMatthias Ringwald const int dash_locations = (1<<3) | (1<<5) | (1<<7) | (1<<9); 2727224be7eSMatthias Ringwald for (i=0;i<16;i++){ 273405d63a9SMatthias Ringwald uint8_t byte = uuid[i]; 274405d63a9SMatthias Ringwald uuid128_to_str_buffer[j++] = char_for_high_nibble(byte); 275405d63a9SMatthias Ringwald uuid128_to_str_buffer[j++] = char_for_low_nibble(byte); 2767224be7eSMatthias Ringwald if (dash_locations & (1<<i)){ 2777224be7eSMatthias Ringwald uuid128_to_str_buffer[j++] = '-'; 2787224be7eSMatthias Ringwald } 2797224be7eSMatthias Ringwald } 280eb886013SMatthias Ringwald return uuid128_to_str_buffer; 281eb886013SMatthias Ringwald } 282eb886013SMatthias Ringwald 283eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3]; // 12:45:78:01:34:67\0 284eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr){ 285eb886013SMatthias Ringwald // orig code 286eb886013SMatthias 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]); 287eb886013SMatthias Ringwald // sprintf-free code 288eb886013SMatthias Ringwald char * p = bd_addr_to_str_buffer; 289eb886013SMatthias Ringwald int i; 290eb886013SMatthias Ringwald for (i = 0; i < 6 ; i++) { 291405d63a9SMatthias Ringwald uint8_t byte = addr[i]; 292405d63a9SMatthias Ringwald *p++ = char_for_high_nibble(byte); 293405d63a9SMatthias Ringwald *p++ = char_for_low_nibble(byte); 294eb886013SMatthias Ringwald *p++ = ':'; 295eb886013SMatthias Ringwald } 296eb886013SMatthias Ringwald *--p = 0; 297eb886013SMatthias Ringwald return (char *) bd_addr_to_str_buffer; 298eb886013SMatthias Ringwald } 299eb886013SMatthias Ringwald 300a6efb919SMatthias Ringwald static int scan_hex_byte(const char * byte_string){ 301a6efb919SMatthias Ringwald int upper_nibble = nibble_for_char(*byte_string++); 302a6efb919SMatthias Ringwald if (upper_nibble < 0) return -1; 303a6efb919SMatthias Ringwald int lower_nibble = nibble_for_char(*byte_string); 304a6efb919SMatthias Ringwald if (lower_nibble < 0) return -1; 305a6efb919SMatthias Ringwald return (upper_nibble << 4) | lower_nibble; 306a6efb919SMatthias Ringwald } 307eb886013SMatthias Ringwald 308a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){ 309a6efb919SMatthias Ringwald uint8_t buffer[BD_ADDR_LEN]; 310a6efb919SMatthias Ringwald int result = 0; 311eb886013SMatthias Ringwald int i; 312eb886013SMatthias Ringwald for (i = 0; i < BD_ADDR_LEN; i++) { 313a6efb919SMatthias Ringwald int single_byte = scan_hex_byte(addr_string); 314a6efb919SMatthias Ringwald if (single_byte < 0) break; 315a6efb919SMatthias Ringwald addr_string += 2; 3163e40861cSMilanka Ringwald buffer[i] = single_byte; 317a6efb919SMatthias Ringwald // don't check seperator after last byte 318a6efb919SMatthias Ringwald if (i == BD_ADDR_LEN - 1) { 319a6efb919SMatthias Ringwald result = 1; 320a6efb919SMatthias Ringwald break; 321eb886013SMatthias Ringwald } 322a6efb919SMatthias Ringwald char separator = *addr_string++; 323a6efb919SMatthias Ringwald if (separator != ':' && separator != '-' && separator != ' ') break; 324a6efb919SMatthias Ringwald } 325a6efb919SMatthias Ringwald 326a6efb919SMatthias Ringwald if (result){ 327a6efb919SMatthias Ringwald bd_addr_copy(addr, buffer); 328a6efb919SMatthias Ringwald } 329a6efb919SMatthias Ringwald return result; 330eb886013SMatthias Ringwald } 3315d067ab1SMatthias Ringwald 3325d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char *str){ 3335d067ab1SMatthias Ringwald uint32_t val = 0; 3345d067ab1SMatthias Ringwald while (1){ 3355d067ab1SMatthias Ringwald char chr = *str; 3365d067ab1SMatthias Ringwald if (!chr || chr < '0' || chr > '9') 3375d067ab1SMatthias Ringwald return val; 3385d067ab1SMatthias Ringwald val = (val * 10) + (chr - '0'); 3395d067ab1SMatthias Ringwald str++; 3405d067ab1SMatthias Ringwald } 3415d067ab1SMatthias Ringwald }