1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /** 39 * @title General Utility Functions 40 * 41 */ 42 43 #ifndef BTSTACK_UTIL_H 44 #define BTSTACK_UTIL_H 45 46 47 #if defined __cplusplus 48 extern "C" { 49 #endif 50 51 #include <stdint.h> 52 #include <string.h> 53 54 #include "bluetooth.h" 55 #include "btstack_defines.h" 56 #include "btstack_linked_list.h" 57 58 // hack: compilation with the android ndk causes an error as there's a reverse_64 macro 59 #ifdef reverse_64 60 #undef reverse_64 61 #endif 62 63 // will be moved to daemon/btstack_device_name_db.h 64 65 66 /** 67 * @brief The device name type 68 */ 69 #define DEVICE_NAME_LEN 248 70 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; 71 72 /* API_START */ 73 74 /** 75 * @brief Minimum function for uint32_t 76 * @param a 77 * @param b 78 * @return value 79 */ 80 uint32_t btstack_min(uint32_t a, uint32_t b); 81 82 /** 83 * @brief Maximum function for uint32_t 84 * @param a 85 * @param b 86 * @return value 87 */ 88 uint32_t btstack_max(uint32_t a, uint32_t b); 89 90 /** 91 * @brief Calculate delta between two points in time 92 * @return time_a - time_b - result > 0 if time_a is newer than time_b 93 */ 94 int32_t btstack_time_delta(uint32_t time_a, uint32_t time_b); 95 96 /** 97 * @brief Read 16/24/32 bit little endian value from buffer 98 * @param buffer 99 * @param position in buffer 100 * @return value 101 */ 102 uint16_t little_endian_read_16(const uint8_t * buffer, int position); 103 uint32_t little_endian_read_24(const uint8_t * buffer, int position); 104 uint32_t little_endian_read_32(const uint8_t * buffer, int position); 105 106 /** 107 * @brief Write 16/32 bit little endian value into buffer 108 * @param buffer 109 * @param position in buffer 110 * @param value 111 */ 112 void little_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); 113 void little_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); 114 void little_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); 115 116 /** 117 * @brief Read 16/24/32 bit big endian value from buffer 118 * @param buffer 119 * @param position in buffer 120 * @return value 121 */ 122 uint32_t big_endian_read_16(const uint8_t * buffer, int position); 123 uint32_t big_endian_read_24(const uint8_t * buffer, int position); 124 uint32_t big_endian_read_32(const uint8_t * buffer, int position); 125 126 /** 127 * @brief Write 16/32 bit big endian value into buffer 128 * @param buffer 129 * @param position in buffer 130 * @param value 131 */ 132 void big_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); 133 void big_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); 134 void big_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); 135 136 137 /** 138 * @brief Swap bytes in 16 bit integer 139 */ 140 static inline uint16_t btstack_flip_16(uint16_t value){ 141 return (uint16_t)((value & 0xffu) << 8) | (value >> 8); 142 } 143 144 /** 145 * @brief Check for big endian system 146 * @return 1 if on big endian 147 */ 148 static inline int btstack_is_big_endian(void){ 149 uint16_t sample = 0x0100; 150 return (int) *(uint8_t*) &sample; 151 } 152 153 /** 154 * @brief Check for little endian system 155 * @return 1 if on little endian 156 */ 157 static inline int btstack_is_little_endian(void){ 158 uint16_t sample = 0x0001; 159 return (int) *(uint8_t*) &sample; 160 } 161 162 /** 163 * @brief Copy from source to destination and reverse byte order 164 * @param src 165 * @param dest 166 * @param len 167 */ 168 void reverse_bytes(const uint8_t * src, uint8_t * dest, int len); 169 170 /** 171 * @brief Wrapper around reverse_bytes for common buffer sizes 172 * @param src 173 * @param dest 174 */ 175 void reverse_24 (const uint8_t * src, uint8_t * dest); 176 void reverse_48 (const uint8_t * src, uint8_t * dest); 177 void reverse_56 (const uint8_t * src, uint8_t * dest); 178 void reverse_64 (const uint8_t * src, uint8_t * dest); 179 void reverse_128(const uint8_t * src, uint8_t * dest); 180 void reverse_256(const uint8_t * src, uint8_t * dest); 181 182 void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest); 183 184 /** 185 * @brief ASCII character for 4-bit nibble 186 * @return character 187 */ 188 char char_for_nibble(int nibble); 189 190 /** 191 * @brif 4-bit nibble from ASCII character 192 * @return value 193 */ 194 int nibble_for_char(char c); 195 196 /** 197 * @brief Compare two Bluetooth addresses 198 * @param a 199 * @param b 200 * @return 0 if equal 201 */ 202 int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b); 203 204 /** 205 * @brief Copy Bluetooth address 206 * @param dest 207 * @param src 208 */ 209 void bd_addr_copy(bd_addr_t dest, const bd_addr_t src); 210 211 /** 212 * @brief Use printf to write hexdump as single line of data 213 */ 214 void printf_hexdump(const void * data, int size); 215 216 /** 217 * @brief Create human readable representation for UUID128 218 * @note uses fixed global buffer 219 * @return pointer to UUID128 string 220 */ 221 char * uuid128_to_str(const uint8_t * uuid); 222 223 /** 224 * @brief Create human readable represenationt of Bluetooth address 225 * @note uses fixed global buffer 226 * @return pointer to Bluetooth address string 227 */ 228 char * bd_addr_to_str(const bd_addr_t addr); 229 230 /** 231 * @brief Replace address placeholder '00:00:00:00:00:00' with Bluetooth address 232 * @param buffer 233 * @param size 234 * @param address 235 */ 236 void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address); 237 238 /** 239 * @brief Parse Bluetooth address 240 * @param address_string 241 * @param buffer for parsed address 242 * @return 1 if string was parsed successfully 243 */ 244 int sscanf_bd_addr(const char * addr_string, bd_addr_t addr); 245 246 /** 247 * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID 248 * @param uuid128 output buffer 249 * @param short_uuid 250 */ 251 void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid); 252 253 /** 254 * @brief Checks if UUID128 has Bluetooth base UUID prefix 255 * @param uui128 to test 256 * @return 1 if it can be expressed as UUID32 257 */ 258 int uuid_has_bluetooth_prefix(const uint8_t * uuid128); 259 260 /** 261 * @brief Parse unsigned number 262 * @param str to parse 263 * @return value 264 */ 265 uint32_t btstack_atoi(const char * str); 266 267 /** 268 * @brief Return number of digits of a uint32 number 269 * @param uint32_number 270 * @return num_digits 271 */ 272 int string_len_for_uint32(uint32_t i); 273 274 /** 275 * @brief Return number of set bits in a uint32 number 276 * @param uint32_number 277 * @return num_set_bits 278 */ 279 int count_set_bits_uint32(uint32_t x); 280 281 /** 282 * @brief Check CRC8 using ETSI TS 101 369 V6.3.0. 283 * @note Only used by RFCOMM 284 * @param data 285 * @param len 286 * @param check_sum 287 */ 288 uint8_t btstack_crc8_check(uint8_t * data, uint16_t len, uint8_t check_sum); 289 290 /** 291 * @brief Calculate CRC8 using ETSI TS 101 369 V6.3.0. 292 * @note Only used by RFCOMM 293 * @param data 294 * @param len 295 */ 296 uint8_t btstack_crc8_calc(uint8_t * data, uint16_t len); 297 298 /** 299 * @brief Get next cid 300 * @param current_cid 301 * @return next cid skiping 0 302 */ 303 uint16_t btstack_next_cid_ignoring_zero(uint16_t current_cid); 304 305 /* API_END */ 306 307 #if defined __cplusplus 308 } 309 #endif 310 311 #endif // BTSTACK_UTIL_H 312