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 23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24*2fca4dadSMilanka Ringwald * GMBH 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 38fe5a6c4eSMilanka Ringwald /** 39fe5a6c4eSMilanka Ringwald * @title General Utility Functions 40eb886013SMatthias Ringwald * 41eb886013SMatthias Ringwald */ 42eb886013SMatthias Ringwald 4380e33422SMatthias Ringwald #ifndef BTSTACK_UTIL_H 4480e33422SMatthias Ringwald #define BTSTACK_UTIL_H 45eb886013SMatthias Ringwald 46eb886013SMatthias Ringwald 47eb886013SMatthias Ringwald #if defined __cplusplus 48eb886013SMatthias Ringwald extern "C" { 49eb886013SMatthias Ringwald #endif 50eb886013SMatthias Ringwald 51eb886013SMatthias Ringwald #include <stdint.h> 52cbabbca4SMatthias Ringwald #include <string.h> 53cbabbca4SMatthias Ringwald 548974fcd6SMatthias Ringwald #include "bluetooth.h" 558974fcd6SMatthias Ringwald #include "btstack_defines.h" 568d471f5cSMatthias Ringwald #include "btstack_linked_list.h" 57eb886013SMatthias Ringwald 589c80e4ccSMatthias Ringwald // hack: compilation with the android ndk causes an error as there's a reverse_64 macro 599c80e4ccSMatthias Ringwald #ifdef reverse_64 609c80e4ccSMatthias Ringwald #undef reverse_64 6173988a59SMatthias Ringwald #endif 6273988a59SMatthias Ringwald 638974fcd6SMatthias Ringwald // will be moved to daemon/btstack_device_name_db.h 64eb886013SMatthias Ringwald 65f54850a9SMatthias Ringwald 66eb886013SMatthias Ringwald /** 67eb886013SMatthias Ringwald * @brief The device name type 68eb886013SMatthias Ringwald */ 69eb886013SMatthias Ringwald #define DEVICE_NAME_LEN 248 70eb886013SMatthias Ringwald typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; 71eb886013SMatthias Ringwald 72f54850a9SMatthias Ringwald /* API_START */ 73caed94dfSMatthias Ringwald 74ebaeb1beSMatthias Ringwald /** 75ebaeb1beSMatthias Ringwald * @brief Minimum function for uint32_t 76ebaeb1beSMatthias Ringwald * @param a 77ebaeb1beSMatthias Ringwald * @param b 78ebaeb1beSMatthias Ringwald * @return value 79ebaeb1beSMatthias Ringwald */ 80ebaeb1beSMatthias Ringwald uint32_t btstack_min(uint32_t a, uint32_t b); 81ebaeb1beSMatthias Ringwald 82ebaeb1beSMatthias Ringwald /** 83ebaeb1beSMatthias Ringwald * @brief Maximum function for uint32_t 84ebaeb1beSMatthias Ringwald * @param a 85ebaeb1beSMatthias Ringwald * @param b 86ebaeb1beSMatthias Ringwald * @return value 87ebaeb1beSMatthias Ringwald */ 88ebaeb1beSMatthias Ringwald uint32_t btstack_max(uint32_t a, uint32_t b); 89ebaeb1beSMatthias Ringwald 9068af3967SMatthias Ringwald /** 9168af3967SMatthias Ringwald * @brief Calculate delta between two points in time 926b65794dSMilanka Ringwald * @return time_a - time_b - result > 0 if time_a is newer than time_b 9368af3967SMatthias Ringwald */ 9468af3967SMatthias Ringwald int32_t btstack_time_delta(uint32_t time_a, uint32_t time_b); 95cbabbca4SMatthias Ringwald 9673988a59SMatthias Ringwald /** 9773988a59SMatthias Ringwald * @brief Read 16/24/32 bit little endian value from buffer 9873988a59SMatthias Ringwald * @param buffer 9973988a59SMatthias Ringwald * @param position in buffer 10073988a59SMatthias Ringwald * @return value 10173988a59SMatthias Ringwald */ 10273988a59SMatthias Ringwald uint16_t little_endian_read_16(const uint8_t * buffer, int position); 10373988a59SMatthias Ringwald uint32_t little_endian_read_24(const uint8_t * buffer, int position); 10473988a59SMatthias Ringwald uint32_t little_endian_read_32(const uint8_t * buffer, int position); 105eb886013SMatthias Ringwald 10673988a59SMatthias Ringwald /** 10773988a59SMatthias Ringwald * @brief Write 16/32 bit little endian value into buffer 10873988a59SMatthias Ringwald * @param buffer 10973988a59SMatthias Ringwald * @param position in buffer 11073988a59SMatthias Ringwald * @param value 11173988a59SMatthias Ringwald */ 11273988a59SMatthias Ringwald void little_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); 113adbdd27aSMilanka Ringwald void little_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); 11473988a59SMatthias Ringwald void little_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); 11573988a59SMatthias Ringwald 11673988a59SMatthias Ringwald /** 11773988a59SMatthias Ringwald * @brief Read 16/24/32 bit big endian value from buffer 11873988a59SMatthias Ringwald * @param buffer 11973988a59SMatthias Ringwald * @param position in buffer 12073988a59SMatthias Ringwald * @return value 12173988a59SMatthias Ringwald */ 12241f9be70SMatthias Ringwald uint32_t big_endian_read_16(const uint8_t * buffer, int position); 12341f9be70SMatthias Ringwald uint32_t big_endian_read_24(const uint8_t * buffer, int position); 12441f9be70SMatthias Ringwald uint32_t big_endian_read_32(const uint8_t * buffer, int position); 12573988a59SMatthias Ringwald 12673988a59SMatthias Ringwald /** 12773988a59SMatthias Ringwald * @brief Write 16/32 bit big endian value into buffer 12873988a59SMatthias Ringwald * @param buffer 12973988a59SMatthias Ringwald * @param position in buffer 13073988a59SMatthias Ringwald * @param value 13173988a59SMatthias Ringwald */ 13241f9be70SMatthias Ringwald void big_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); 13341f9be70SMatthias Ringwald void big_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); 13441f9be70SMatthias Ringwald void big_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); 135eb886013SMatthias Ringwald 1365d86890cSMatthias Ringwald 1375d86890cSMatthias Ringwald /** 1385d86890cSMatthias Ringwald * @brief Swap bytes in 16 bit integer 1395d86890cSMatthias Ringwald */ 1405d86890cSMatthias Ringwald static inline uint16_t btstack_flip_16(uint16_t value){ 1415ebc8227SMatthias Ringwald return (uint16_t)((value & 0xffu) << 8) | (value >> 8); 1425d86890cSMatthias Ringwald } 1435d86890cSMatthias Ringwald 1445d86890cSMatthias Ringwald /** 1455d86890cSMatthias Ringwald * @brief Check for big endian system 1466b65794dSMilanka Ringwald * @return 1 if on big endian 1475d86890cSMatthias Ringwald */ 1485d86890cSMatthias Ringwald static inline int btstack_is_big_endian(void){ 1495d86890cSMatthias Ringwald uint16_t sample = 0x0100; 1505ebc8227SMatthias Ringwald return (int) *(uint8_t*) &sample; 1515d86890cSMatthias Ringwald } 1525d86890cSMatthias Ringwald 1535d86890cSMatthias Ringwald /** 1545d86890cSMatthias Ringwald * @brief Check for little endian system 1556b65794dSMilanka Ringwald * @return 1 if on little endian 1565d86890cSMatthias Ringwald */ 1575d86890cSMatthias Ringwald static inline int btstack_is_little_endian(void){ 1585d86890cSMatthias Ringwald uint16_t sample = 0x0001; 1595ebc8227SMatthias Ringwald return (int) *(uint8_t*) &sample; 1605d86890cSMatthias Ringwald } 1615d86890cSMatthias Ringwald 162caed94dfSMatthias Ringwald /** 163caed94dfSMatthias Ringwald * @brief Copy from source to destination and reverse byte order 1649c80e4ccSMatthias Ringwald * @param src 1659c80e4ccSMatthias Ringwald * @param dest 1669c80e4ccSMatthias Ringwald * @param len 167caed94dfSMatthias Ringwald */ 1689c80e4ccSMatthias Ringwald void reverse_bytes(const uint8_t * src, uint8_t * dest, int len); 1699c80e4ccSMatthias Ringwald 1709c80e4ccSMatthias Ringwald /** 1719c80e4ccSMatthias Ringwald * @brief Wrapper around reverse_bytes for common buffer sizes 1729c80e4ccSMatthias Ringwald * @param src 1739c80e4ccSMatthias Ringwald * @param dest 1749c80e4ccSMatthias Ringwald */ 1759c80e4ccSMatthias Ringwald void reverse_24 (const uint8_t * src, uint8_t * dest); 1769c80e4ccSMatthias Ringwald void reverse_48 (const uint8_t * src, uint8_t * dest); 1779c80e4ccSMatthias Ringwald void reverse_56 (const uint8_t * src, uint8_t * dest); 1789c80e4ccSMatthias Ringwald void reverse_64 (const uint8_t * src, uint8_t * dest); 1799c80e4ccSMatthias Ringwald void reverse_128(const uint8_t * src, uint8_t * dest); 180cc7a2d78SMatthias Ringwald void reverse_256(const uint8_t * src, uint8_t * dest); 1819c80e4ccSMatthias Ringwald 182724d70a2SMatthias Ringwald void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest); 183caed94dfSMatthias Ringwald 184caed94dfSMatthias Ringwald /** 185877f40f3SMatthias Ringwald * @brief ASCII character for 4-bit nibble 186877f40f3SMatthias Ringwald * @return character 187caed94dfSMatthias Ringwald */ 188eb886013SMatthias Ringwald char char_for_nibble(int nibble); 189eb886013SMatthias Ringwald 190caed94dfSMatthias Ringwald /** 191877f40f3SMatthias Ringwald * @brif 4-bit nibble from ASCII character 192877f40f3SMatthias Ringwald * @return value 193877f40f3SMatthias Ringwald */ 194877f40f3SMatthias Ringwald int nibble_for_char(char c); 195877f40f3SMatthias Ringwald 196877f40f3SMatthias Ringwald /** 197caed94dfSMatthias Ringwald * @brief Compare two Bluetooth addresses 198caed94dfSMatthias Ringwald * @param a 199caed94dfSMatthias Ringwald * @param b 20053c654feSMilanka Ringwald * @return 0 if equal 201caed94dfSMatthias Ringwald */ 2025222912bSMatthias Ringwald int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b); 203caed94dfSMatthias Ringwald 204caed94dfSMatthias Ringwald /** 205caed94dfSMatthias Ringwald * @brief Copy Bluetooth address 20673988a59SMatthias Ringwald * @param dest 207caed94dfSMatthias Ringwald * @param src 208caed94dfSMatthias Ringwald */ 2095222912bSMatthias Ringwald void bd_addr_copy(bd_addr_t dest, const bd_addr_t src); 210caed94dfSMatthias Ringwald 211caed94dfSMatthias Ringwald /** 212caed94dfSMatthias Ringwald * @brief Use printf to write hexdump as single line of data 213caed94dfSMatthias Ringwald */ 214caed94dfSMatthias Ringwald void printf_hexdump(const void * data, int size); 215caed94dfSMatthias Ringwald 216caed94dfSMatthias Ringwald /** 217caed94dfSMatthias Ringwald * @brief Create human readable representation for UUID128 218caed94dfSMatthias Ringwald * @note uses fixed global buffer 219caed94dfSMatthias Ringwald * @return pointer to UUID128 string 220caed94dfSMatthias Ringwald */ 2215222912bSMatthias Ringwald char * uuid128_to_str(const uint8_t * uuid); 222caed94dfSMatthias Ringwald 223caed94dfSMatthias Ringwald /** 224caed94dfSMatthias Ringwald * @brief Create human readable represenationt of Bluetooth address 225caed94dfSMatthias Ringwald * @note uses fixed global buffer 226caed94dfSMatthias Ringwald * @return pointer to Bluetooth address string 227caed94dfSMatthias Ringwald */ 2285222912bSMatthias Ringwald char * bd_addr_to_str(const bd_addr_t addr); 229caed94dfSMatthias Ringwald 230caed94dfSMatthias Ringwald /** 2313c9da642SMatthias Ringwald * @brief Replace address placeholder '00:00:00:00:00:00' with Bluetooth address 2323c9da642SMatthias Ringwald * @param buffer 2333c9da642SMatthias Ringwald * @param size 2343c9da642SMatthias Ringwald * @param address 2353c9da642SMatthias Ringwald */ 2363c9da642SMatthias Ringwald void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address); 2373c9da642SMatthias Ringwald 2383c9da642SMatthias Ringwald /** 239caed94dfSMatthias Ringwald * @brief Parse Bluetooth address 240caed94dfSMatthias Ringwald * @param address_string 241caed94dfSMatthias Ringwald * @param buffer for parsed address 242caed94dfSMatthias Ringwald * @return 1 if string was parsed successfully 243caed94dfSMatthias Ringwald */ 244a6efb919SMatthias Ringwald int sscanf_bd_addr(const char * addr_string, bd_addr_t addr); 245caed94dfSMatthias Ringwald 24673988a59SMatthias Ringwald /** 24773988a59SMatthias Ringwald * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID 24873988a59SMatthias Ringwald * @param uuid128 output buffer 24973988a59SMatthias Ringwald * @param short_uuid 25073988a59SMatthias Ringwald */ 25173988a59SMatthias Ringwald void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid); 252eb886013SMatthias Ringwald 25373988a59SMatthias Ringwald /** 25473988a59SMatthias Ringwald * @brief Checks if UUID128 has Bluetooth base UUID prefix 25573988a59SMatthias Ringwald * @param uui128 to test 25673988a59SMatthias Ringwald * @return 1 if it can be expressed as UUID32 25773988a59SMatthias Ringwald */ 2585222912bSMatthias Ringwald int uuid_has_bluetooth_prefix(const uint8_t * uuid128); 259eb886013SMatthias Ringwald 2605d067ab1SMatthias Ringwald /** 2615d067ab1SMatthias Ringwald * @brief Parse unsigned number 2625d067ab1SMatthias Ringwald * @param str to parse 2635d067ab1SMatthias Ringwald * @return value 2645d067ab1SMatthias Ringwald */ 2655d067ab1SMatthias Ringwald uint32_t btstack_atoi(const char * str); 2665d067ab1SMatthias Ringwald 2671f41c2c9SMatthias Ringwald /** 268d1207cd8SMilanka Ringwald * @brief Return number of digits of a uint32 number 269d1207cd8SMilanka Ringwald * @param uint32_number 270d1207cd8SMilanka Ringwald * @return num_digits 271d1207cd8SMilanka Ringwald */ 272d1207cd8SMilanka Ringwald int string_len_for_uint32(uint32_t i); 273d1207cd8SMilanka Ringwald 274d1207cd8SMilanka Ringwald /** 275d1207cd8SMilanka Ringwald * @brief Return number of set bits in a uint32 number 276d1207cd8SMilanka Ringwald * @param uint32_number 277d1207cd8SMilanka Ringwald * @return num_set_bits 278d1207cd8SMilanka Ringwald */ 279d1207cd8SMilanka Ringwald int count_set_bits_uint32(uint32_t x); 280d1207cd8SMilanka Ringwald 281d1207cd8SMilanka Ringwald /** 282a73d0b9dSMilanka Ringwald * @brief Check CRC8 using ETSI TS 101 369 V6.3.0. 283a73d0b9dSMilanka Ringwald * @note Only used by RFCOMM 284a73d0b9dSMilanka Ringwald * @param data 285a73d0b9dSMilanka Ringwald * @param len 286a73d0b9dSMilanka Ringwald * @param check_sum 2871f41c2c9SMatthias Ringwald */ 28863dd1c76SMatthias Ringwald uint8_t btstack_crc8_check(uint8_t * data, uint16_t len, uint8_t check_sum); 289a73d0b9dSMilanka Ringwald 290a73d0b9dSMilanka Ringwald /** 291a73d0b9dSMilanka Ringwald * @brief Calculate CRC8 using ETSI TS 101 369 V6.3.0. 292a73d0b9dSMilanka Ringwald * @note Only used by RFCOMM 293a73d0b9dSMilanka Ringwald * @param data 294a73d0b9dSMilanka Ringwald * @param len 295a73d0b9dSMilanka Ringwald */ 29663dd1c76SMatthias Ringwald uint8_t btstack_crc8_calc(uint8_t * data, uint16_t len); 2971f41c2c9SMatthias Ringwald 298a73d0b9dSMilanka Ringwald /** 299a73d0b9dSMilanka Ringwald * @brief Get next cid 300a73d0b9dSMilanka Ringwald * @param current_cid 301a73d0b9dSMilanka Ringwald * @return next cid skiping 0 302a73d0b9dSMilanka Ringwald */ 303a73d0b9dSMilanka Ringwald uint16_t btstack_next_cid_ignoring_zero(uint16_t current_cid); 304a73d0b9dSMilanka Ringwald 305f54850a9SMatthias Ringwald /* API_END */ 3065d067ab1SMatthias Ringwald 307eb886013SMatthias Ringwald #if defined __cplusplus 308eb886013SMatthias Ringwald } 309eb886013SMatthias Ringwald #endif 310eb886013SMatthias Ringwald 31180e33422SMatthias Ringwald #endif // BTSTACK_UTIL_H 312