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