General Utility Functions API
#include "src/btstack_util.h"
btstack_min
uint32_t btstack_min(uint32_t a, uint32_t b);
Minimum function for uint32_t
Parameters
- a
- b
Returns
- uint32_t: value
btstack_max
uint32_t btstack_max(uint32_t a, uint32_t b);
Maximum function for uint32_t
Parameters
- a
- b
Returns
- uint32_t: value
btstack_time_delta
int32_t btstack_time_delta(uint32_t time_a, uint32_t time_b);
Calculate delta between two uint32_t points in time
Returns
- int32_t: time_a - time_b - result > 0 if time_a is newer than time_b
btstack_time16_delta
int16_t btstack_time16_delta(uint16_t time_a, uint16_t time_b);
Calculate delta between two uint16_t points in time
Returns
- int16_t: time_a - time_b - result > 0 if time_a is newer than time_b
little_endian_read_08
uint8_t little_endian_read_08(const uint8_t * buffer, int position);
Read 08/16/24/32 bit little endian value from buffer
Parameters
- buffer
- position: in buffer
Returns
- uint8_t: value
little_endian_read_16
uint16_t little_endian_read_16(const uint8_t * buffer, int position);
Returns
- uint16_t
little_endian_read_24
uint32_t little_endian_read_24(const uint8_t * buffer, int position);
Returns
- uint32_t
little_endian_read_32
uint32_t little_endian_read_32(const uint8_t * buffer, int position);
Returns
- uint32_t
little_endian_store_08
void little_endian_store_08(uint8_t * buffer, uint16_t position, uint8_t value);
Write 08/16/32 bit little endian value into buffer
Parameters
- buffer
- position: in buffer
- value
little_endian_store_16
void little_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value);
little_endian_store_24
void little_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value);
little_endian_store_32
void little_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value);
big_endian_read_08
uint32_t big_endian_read_08(const uint8_t* buffer, int position);
Read 08/16/24/32 bit big endian value from buffer
Parameters
- buffer
- position: in buffer
Returns
- uint32_t: value
big_endian_read_16
uint32_t big_endian_read_16(const uint8_t * buffer, int position);
Returns
- uint32_t
big_endian_read_24
uint32_t big_endian_read_24(const uint8_t * buffer, int position);
Returns
- uint32_t
big_endian_read_32
uint32_t big_endian_read_32(const uint8_t * buffer, int position);
Returns
- uint32_t
big_endian_store_08
void big_endian_store_08(uint8_t * buffer, uint16_t position, uint8_t value);
Write 08/16/32 bit big endian value into buffer
Parameters
- buffer
- position: in buffer
- value
big_endian_store_16
void big_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value);
big_endian_store_24
void big_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value);
big_endian_store_32
void big_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value);
btstack_flip_16
static inline uint16_t btstack_flip_16(uint16_t value);
Swap bytes in 16 bit integer
Returns
- uint16_t
btstack_is_big_endian
static inline int btstack_is_big_endian(void);
Check for big endian system
Returns
- int: 1 if on big endian
btstack_is_little_endian
static inline int btstack_is_little_endian(void);
Check for little endian system
Returns
- int: 1 if on little endian
reverse_bytes
void reverse_bytes(const uint8_t * src, uint8_t * dest, int len);
Copy from source to destination and reverse byte order
Parameters
- src
- dest
- len
reverse_24
void reverse_24 (const uint8_t * src, uint8_t * dest);
Wrapper around reverse_bytes for common buffer sizes
Parameters
- src
- dest
reverse_48
void reverse_48 (const uint8_t * src, uint8_t * dest);
reverse_56
void reverse_56 (const uint8_t * src, uint8_t * dest);
reverse_64
void reverse_64 (const uint8_t * src, uint8_t * dest);
reverse_128
void reverse_128(const uint8_t * src, uint8_t * dest);
reverse_256
void reverse_256(const uint8_t * src, uint8_t * dest);
reverse_bd_addr
void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest);
btstack_is_null
bool btstack_is_null(const uint8_t * buffer, uint16_t size);
Check if all bytes in buffer are zero
Parameters
- buffer
- size
Returns
- bool: true if all bytes is buffer are zero
btstack_is_null_bd_addr
bool btstack_is_null_bd_addr( const bd_addr_t addr );
Check if all bytes in a bd_addr_t are zero
Parameters
- addr
Returns
- bool: true if all bytes in addr are zero
char_for_nibble
char char_for_nibble(uint8_t nibble);
ASCII character for 4-bit nibble
Returns
- char: character
nibble_for_char
int nibble_for_char(char c);
@brif: 4-bit nibble from ASCII character
Returns
- int: value
bd_addr_cmp
int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b);
Compare two Bluetooth addresses
Parameters
- a
- b
Returns
- int: 0 if equal
bd_addr_copy
void bd_addr_copy(bd_addr_t dest, const bd_addr_t src);
Copy Bluetooth address
Parameters
- dest
- src
printf_hexdump
void printf_hexdump(const void * data, int size);
Use printf to write hexdump as single line of data
uuid128_to_str
char * uuid128_to_str(const uint8_t * uuid);
Create human readable representation for UUID128
Note: uses fixed global buffer
Returns
- char *: pointer to UUID128 string
bd_addr_to_str_with_delimiter
char * bd_addr_to_str_with_delimiter(const bd_addr_t addr, char delimiter);
Create human readable represenationt of Bluetooth address
Note: uses fixed global buffer
Parameters
- delimiter
Returns
- char *: pointer to Bluetooth address string
bd_addr_to_str
char * bd_addr_to_str(const bd_addr_t addr);
Create human readable represenationt of Bluetooth address
Note: uses fixed global buffer
Returns
- char *: pointer to Bluetooth address string
btstack_replace_bd_addr_placeholder
void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address);
Replace address placeholder '00:00:00:00:00:00' with Bluetooth address
Parameters
- buffer
- size
- address
sscanf_bd_addr
int sscanf_bd_addr(const char * addr_string, bd_addr_t addr);
Parse Bluetooth address
Parameters
- address_string
- buffer: for parsed address
Returns
- int: 1 if string was parsed successfully
uuid_add_bluetooth_prefix
void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid);
Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID
Parameters
- uuid128: output buffer
- short_uuid
uuid_has_bluetooth_prefix
bool uuid_has_bluetooth_prefix(const uint8_t * uuid128);
Checks if UUID128 has Bluetooth base UUID prefix
Parameters
- uui128: to test
Returns
- bool: true if it can be expressed as UUID32
btstack_atoi
uint32_t btstack_atoi(const char * str);
Parse an unsigned decimal number from a NUL-terminated string.
Parameters
- str: NUL-terminated string to parse.
Returns
- uint32_t: value
btstack_atoi_n
uint32_t btstack_atoi_n(const char * str, size_t len);
Parse an unsigned decimal number from a length-delimited string.
Parameters
- str: Byte sequence to parse; it does not need to be NUL-terminated.
- len: Maximum number of bytes to parse.
Returns
- uint32_t: value
string_len_for_uint32
int string_len_for_uint32(uint32_t i);
Return number of digits of a uint32 number
Parameters
- uint32_number
Returns
- int: num_digits
count_set_bits_uint32
int count_set_bits_uint32(uint32_t x);
Return number of set bits in a uint32 number
Parameters
- uint32_number
Returns
- int: num_set_bits
btstack_crc8_check
uint8_t btstack_crc8_check(const uint8_t* data, uint16_t len, uint8_t check_sum);
Check CRC8 using ETSI TS 101 369 V6.3.0.
Note: Only used by RFCOMM
Parameters
- data
- len
- check_sum
Returns
- uint8_t
btstack_crc8_calc
uint8_t btstack_crc8_calc(const uint8_t* data, uint16_t len);
Calculate CRC8 using ETSI TS 101 369 V6.3.0.
Note: Only used by RFCOMM
Parameters
- data
- len
Returns
- uint8_t
btstack_crc32_init
uint32_t btstack_crc32_init(void);
Calculate the initial CRC32 value using ISO 3309 (HDLC), polynomial (normal) 0x04c11db7
Note: Used by OTS Service.
Returns
- uint32_t: The initial crc value.
btstack_crc32_update
uint32_t btstack_crc32_update(uint32_t crc, const uint8_t * data, uint32_t data_len);
Update the CRC32 value with new data.
Parameters
- crc: The current crc value.
- data: Pointer to a buffer of \a data_len bytes.
- data_len: Number of bytes in the \a data buffer.
Returns
- uint32_t: The updated crc value.
btstack_crc32_finalize
uint32_t btstack_crc32_finalize(uint32_t crc);
Calculate the final CRC32 value.
Parameters
- crc: The current crc value.
Returns
- uint32_t: The final crc value.
btstack_next_cid_ignoring_zero
uint16_t btstack_next_cid_ignoring_zero(uint16_t current_cid);
Get next cid
Parameters
- current_cid
Returns
- uint16_t: next cid skiping 0
btstack_strcpy
uint16_t btstack_strcpy(char * dst, uint16_t dst_size, const char * src);
Copy string (up to dst_size-1 characters) from src into dst buffer with terminating '\0'
Note: replaces strncpy + dst[dst_size-1] = '\0'
@retun: bytes_copied including trailing 0
Parameters
- dst
- dst_size
- src
Returns
- uint16_t
btstack_strcat
void btstack_strcat(char * dst, uint16_t dst_size, const char * src);
Append src string to string in dst buffer with terminating '\0'
Note: max total string length will be dst_size-1 characters
Parameters
- dst
- dst_size
- src
btstack_printf_strlen
int btstack_printf_strlen(const char * format, ...)
#ifdef __GNUC__
__attribute__ ((format (__printf__, 1, 2)))
#endif
;
Calculated the number of characters that would get printed
Note: same as calling snprintf without a buffer
Parameters
- format
- argsq
Returns
- int: number of characters, or negative value on error
btstack_snprintf_assert_complete
uint16_t btstack_snprintf_assert_complete(char * buffer, size_t size, const char * format, ...)
#ifdef __GNUC__
__attribute__ ((format (__printf__, 3, 4)))
#endif
;
Format string into buffer with '\0' and assert it is large enough
Note: same as calling snprintf and assert that the string was not truncated
Parameters
- buffer
- size: of buffer
- format
- argsq
Returns
- uint16_t: number of characters
btstack_snprintf_best_effort
uint16_t btstack_snprintf_best_effort(char * buffer, size_t size, const char * format, ...)
#ifdef __GNUC__
__attribute__ ((format (__printf__, 3, 4)))
#endif
;
Format string into buffer, truncated if necessary. Output string is '\0' terminated
Note: similar to calling snprintf but returns the length of the output string
Parameters
- buffer
- size: of buffer
- format
- argsq
Returns
- uint16_t: number of characters
btstack_clz
uint8_t btstack_clz(uint32_t value);
Returns the number of leading 0-bits in x, starting at the most significant bit position.
If x is 0, the result is undefined.
Note: maps to __builtin_clz for gcc and clang
Parameters
- value
Returns
- uint8_t: number of leading 0-bits
btstack_virtual_memcpy
uint16_t btstack_virtual_memcpy(
const uint8_t * field_data, uint16_t field_len, uint16_t field_offset,
uint8_t * buffer, uint16_t buffer_size, uint16_t buffer_offset);
Copy chunk of data into a virtual buffer backed by a physical buffer. Used to provide chunk of data of larger buffer that is constructed on the fly, e.g. serializing data struct
For example, copy field2 to buffer, with buffer_offset = 11
field1 field2 field3 field4 field5 filed6
struct: -------|-------|----------|-------------|-------|-------------- buffer: ------------------ result: ----|
When also copying field3 and field4 to buffer, with buffer_offset = 11
field1 field2 field3 field4 field5 filed6
struct: -------|-------|----------|-------------|-------|-------------- buffer: ------------------ result: ----|----------|--
Parameters
- field_data
- field_len
- field_offset: position of field in complete data block
- buffer_data
- buffer_len
- buffer_offset: position of buffer in complete data block
Returns
- uint16_t: bytes_copied number of bytes actually stored in buffer
btstack_bytes_to_hex
void btstack_bytes_to_hex(char * dst, const uint8_t * src_data, uint16_t src_size);
Convert bytes to hex string
Parameters
- dst: buffer for hex string, needs to be twice as large as src_size + 1
- src_data
- src_size
btstack_hex_to_bytes
bool btstack_hex_to_bytes(uint8_t * dst, uint16_t dst_size, const char * src);
Convert hex string to bytes
Parameters
- dst
- dst_size
- src
Returns
- bool: true if conversion was successful