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 uint32_t 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 Calculate delta between two uint16_t points in time 98 * @return time_a - time_b - result > 0 if time_a is newer than time_b 99 */ 100 int16_t btstack_time16_delta(uint16_t time_a, uint16_t time_b); 101 102 /** 103 * @brief Read 16/24/32 bit little endian value from buffer 104 * @param buffer 105 * @param position in buffer 106 * @return value 107 */ 108 uint16_t little_endian_read_16(const uint8_t * buffer, int position); 109 uint32_t little_endian_read_24(const uint8_t * buffer, int position); 110 uint32_t little_endian_read_32(const uint8_t * buffer, int position); 111 112 /** 113 * @brief Write 16/32 bit little endian value into buffer 114 * @param buffer 115 * @param position in buffer 116 * @param value 117 */ 118 void little_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); 119 void little_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); 120 void little_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); 121 122 /** 123 * @brief Read 16/24/32 bit big endian value from buffer 124 * @param buffer 125 * @param position in buffer 126 * @return value 127 */ 128 uint32_t big_endian_read_16(const uint8_t * buffer, int position); 129 uint32_t big_endian_read_24(const uint8_t * buffer, int position); 130 uint32_t big_endian_read_32(const uint8_t * buffer, int position); 131 132 /** 133 * @brief Write 16/32 bit big endian value into buffer 134 * @param buffer 135 * @param position in buffer 136 * @param value 137 */ 138 void big_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); 139 void big_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); 140 void big_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); 141 142 143 /** 144 * @brief Swap bytes in 16 bit integer 145 */ 146 static inline uint16_t btstack_flip_16(uint16_t value){ 147 return (uint16_t)((value & 0xffu) << 8) | (value >> 8); 148 } 149 150 /** 151 * @brief Check for big endian system 152 * @return 1 if on big endian 153 */ 154 static inline int btstack_is_big_endian(void){ 155 uint16_t sample = 0x0100; 156 return (int) *(uint8_t*) &sample; 157 } 158 159 /** 160 * @brief Check for little endian system 161 * @return 1 if on little endian 162 */ 163 static inline int btstack_is_little_endian(void){ 164 uint16_t sample = 0x0001; 165 return (int) *(uint8_t*) &sample; 166 } 167 168 /** 169 * @brief Copy from source to destination and reverse byte order 170 * @param src 171 * @param dest 172 * @param len 173 */ 174 void reverse_bytes(const uint8_t * src, uint8_t * dest, int len); 175 176 /** 177 * @brief Wrapper around reverse_bytes for common buffer sizes 178 * @param src 179 * @param dest 180 */ 181 void reverse_24 (const uint8_t * src, uint8_t * dest); 182 void reverse_48 (const uint8_t * src, uint8_t * dest); 183 void reverse_56 (const uint8_t * src, uint8_t * dest); 184 void reverse_64 (const uint8_t * src, uint8_t * dest); 185 void reverse_128(const uint8_t * src, uint8_t * dest); 186 void reverse_256(const uint8_t * src, uint8_t * dest); 187 188 void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest); 189 190 /** 191 * @brief Check if all bytes in buffer are zero 192 * @param buffer 193 * @param size 194 * @return true if all bytes is buffer are zero 195 */ 196 bool btstack_is_null(const uint8_t * buffer, uint16_t size); 197 198 /** 199 * @brief Check if all bytes in a bd_addr_t are zero 200 * @param addr 201 * @return true if all bytes in addr are zero 202 */ 203 bool btstack_is_null_bd_addr( const bd_addr_t addr ); 204 205 /** 206 * @brief ASCII character for 4-bit nibble 207 * @return character 208 */ 209 char char_for_nibble(uint8_t nibble); 210 211 /** 212 * @brif 4-bit nibble from ASCII character 213 * @return value 214 */ 215 int nibble_for_char(char c); 216 217 /** 218 * @brief Compare two Bluetooth addresses 219 * @param a 220 * @param b 221 * @return 0 if equal 222 */ 223 int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b); 224 225 /** 226 * @brief Copy Bluetooth address 227 * @param dest 228 * @param src 229 */ 230 void bd_addr_copy(bd_addr_t dest, const bd_addr_t src); 231 232 /** 233 * @brief Use printf to write hexdump as single line of data 234 */ 235 void printf_hexdump(const void * data, int size); 236 237 /** 238 * @brief Create human readable representation for UUID128 239 * @note uses fixed global buffer 240 * @return pointer to UUID128 string 241 */ 242 char * uuid128_to_str(const uint8_t * uuid); 243 244 /** 245 * @brief Create human readable represenationt of Bluetooth address 246 * @note uses fixed global buffer 247 * @param delimiter 248 * @return pointer to Bluetooth address string 249 */ 250 char * bd_addr_to_str_with_delimiter(const bd_addr_t addr, char delimiter); 251 252 /** 253 * @brief Create human readable represenationt of Bluetooth address 254 * @note uses fixed global buffer 255 * @return pointer to Bluetooth address string 256 */ 257 char * bd_addr_to_str(const bd_addr_t addr); 258 259 /** 260 * @brief Replace address placeholder '00:00:00:00:00:00' with Bluetooth address 261 * @param buffer 262 * @param size 263 * @param address 264 */ 265 void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address); 266 267 /** 268 * @brief Parse Bluetooth address 269 * @param address_string 270 * @param buffer for parsed address 271 * @return 1 if string was parsed successfully 272 */ 273 int sscanf_bd_addr(const char * addr_string, bd_addr_t addr); 274 275 /** 276 * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID 277 * @param uuid128 output buffer 278 * @param short_uuid 279 */ 280 void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid); 281 282 /** 283 * @brief Checks if UUID128 has Bluetooth base UUID prefix 284 * @param uui128 to test 285 * @return true if it can be expressed as UUID32 286 */ 287 bool uuid_has_bluetooth_prefix(const uint8_t * uuid128); 288 289 /** 290 * @brief Parse unsigned number 291 * @param str to parse 292 * @return value 293 */ 294 uint32_t btstack_atoi(const char * str); 295 296 /** 297 * @brief Return number of digits of a uint32 number 298 * @param uint32_number 299 * @return num_digits 300 */ 301 int string_len_for_uint32(uint32_t i); 302 303 /** 304 * @brief Return number of set bits in a uint32 number 305 * @param uint32_number 306 * @return num_set_bits 307 */ 308 int count_set_bits_uint32(uint32_t x); 309 310 /** 311 * @brief Check CRC8 using ETSI TS 101 369 V6.3.0. 312 * @note Only used by RFCOMM 313 * @param data 314 * @param len 315 * @param check_sum 316 */ 317 uint8_t btstack_crc8_check(uint8_t * data, uint16_t len, uint8_t check_sum); 318 319 /** 320 * @brief Calculate CRC8 using ETSI TS 101 369 V6.3.0. 321 * @note Only used by RFCOMM 322 * @param data 323 * @param len 324 */ 325 uint8_t btstack_crc8_calc(uint8_t * data, uint16_t len); 326 327 328 /** 329 * @brief Calculate the initial CRC32 value using ISO 3309 (HDLC), polynomial (normal) 0x04c11db7 330 * @note Used by OTS Service. 331 * 332 * @return The initial crc value. 333 */ 334 uint32_t btstack_crc32_init(void); 335 336 /** 337 * @brief Update the CRC32 value with new data. 338 * 339 * @param crc The current crc value. 340 * @param data Pointer to a buffer of \a data_len bytes. 341 * @param data_len Number of bytes in the \a data buffer. 342 * @return The updated crc value. 343 */ 344 uint32_t btstack_crc32_update(uint32_t crc, const uint8_t * data, uint32_t data_len); 345 346 /** 347 * @brief Calculate the final CRC32 value. 348 * 349 * @param crc The current crc value. 350 * @return The final crc value. 351 */ 352 uint32_t btstack_crc32_finalize(uint32_t crc); 353 354 /** 355 * @brief Get next cid 356 * @param current_cid 357 * @return next cid skiping 0 358 */ 359 uint16_t btstack_next_cid_ignoring_zero(uint16_t current_cid); 360 361 /** 362 * @brief Copy string (up to dst_size-1 characters) from src into dst buffer with terminating '\0' 363 * @note replaces strncpy + dst[dst_size-1] = '\0' 364 * @param dst 365 * @param dst_size 366 * @param src 367 * @retun bytes_copied including trailing 0 368 */ 369 uint16_t btstack_strcpy(char * dst, uint16_t dst_size, const char * src); 370 371 /** 372 * @brief Append src string to string in dst buffer with terminating '\0' 373 * @note max total string length will be dst_size-1 characters 374 * @param dst 375 * @param dst_size 376 * @param src 377 */ 378 void btstack_strcat(char * dst, uint16_t dst_size, const char * src); 379 380 /** 381 * @brief Calculated the number of characters that would get printed 382 * @note same as calling snprintf without a buffer 383 * @param format 384 * @param argsq 385 * @return number of characters, or negative value on error 386 */ 387 int btstack_printf_strlen(const char * format, ...) 388 #ifdef __GNUC__ 389 __attribute__ ((format (__printf__, 1, 2))) 390 #endif 391 ; 392 393 394 /** 395 * @brief Format string into buffer with '\0' and assert it is large enough 396 * @note same as calling snprintf and assert that the string was not truncated 397 * @param buffer 398 * @param size of buffer 399 * @param format 400 * @param argsq 401 * @return number of characters 402 */ 403 uint16_t btstack_snprintf_assert_complete(char * buffer, size_t size, const char * format, ...) 404 #ifdef __GNUC__ 405 __attribute__ ((format (__printf__, 3, 4))) 406 #endif 407 ; 408 409 /** 410 * @brief Format string into buffer, truncated if necessary. Output string is '\0' terminated 411 * @note similar to calling snprintf but returns the length of the output string 412 * @param buffer 413 * @param size of buffer 414 * @param format 415 * @param argsq 416 * @return number of characters 417 */ 418 uint16_t btstack_snprintf_best_effort(char * buffer, size_t size, const char * format, ...) 419 #ifdef __GNUC__ 420 __attribute__ ((format (__printf__, 3, 4))) 421 #endif 422 ; 423 424 /** 425 * Returns the number of leading 0-bits in x, starting at the most significant bit position. 426 * If x is 0, the result is undefined. 427 * @note maps to __builtin_clz for gcc and clang 428 * @param value 429 * @return number of leading 0-bits 430 */ 431 uint8_t btstack_clz(uint32_t value); 432 433 /** 434 * @brief Copy chunk of data into a virtual buffer backed by a physical buffer. 435 * Used to provide chunk of data of larger buffer that is constructed on the fly, e.g. serializing data struct 436 * 437 * For example, copy field2 to buffer, with buffer_offset = 11 438 * 439 * field1 field2 field3 field4 field5 filed6 440 * struct: -------|-------|----------|-------------|-------|-------------- 441 * buffer: ------------------ 442 * result: ----| 443 * 444 * When also copying field3 and field4 to buffer, with buffer_offset = 11 445 * 446 * field1 field2 field3 field4 field5 filed6 447 * struct: -------|-------|----------|-------------|-------|-------------- 448 * buffer: ------------------ 449 * result: ----|----------|-- 450 * 451 * @param field_data 452 * @param field_len 453 * @param field_offset position of field in complete data block 454 * @param buffer_data 455 * @param buffer_len 456 * @param buffer_offset position of buffer in complete data block 457 * @return bytes_copied number of bytes actually stored in buffer 458 */ 459 uint16_t btstack_virtual_memcpy( 460 const uint8_t * field_data, uint16_t field_len, uint16_t field_offset, 461 uint8_t * buffer, uint16_t buffer_size, uint16_t buffer_offset); 462 463 464 /* API_END */ 465 466 #if defined __cplusplus 467 } 468 #endif 469 470 #endif // BTSTACK_UTIL_H 471