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 MATTHIAS 24 * RINGWALD 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 * btstack_util.h 40 * 41 * General utility functions 42 * 43 * Created by Matthias Ringwald on 7/23/09. 44 */ 45 46 #ifndef BTSTACK_UTIL_H 47 #define BTSTACK_UTIL_H 48 49 50 #if defined __cplusplus 51 extern "C" { 52 #endif 53 54 #include <stdint.h> 55 #include <string.h> 56 57 #include "bluetooth.h" 58 #include "btstack_defines.h" 59 #include "btstack_linked_list.h" 60 61 // hack: compilation with the android ndk causes an error as there's a reverse_64 macro 62 #ifdef reverse_64 63 #undef reverse_64 64 #endif 65 66 // will be moved to daemon/btstack_device_name_db.h 67 68 69 /** 70 * @brief The device name type 71 */ 72 #define DEVICE_NAME_LEN 248 73 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; 74 75 /* API_START */ 76 77 /** 78 * @brief Minimum function for uint32_t 79 * @param a 80 * @param b 81 * @return value 82 */ 83 uint32_t btstack_min(uint32_t a, uint32_t b); 84 85 /** 86 * @brief Maximum function for uint32_t 87 * @param a 88 * @param b 89 * @return value 90 */ 91 uint32_t btstack_max(uint32_t a, uint32_t b); 92 93 /** 94 * @brief Calculate delta between two points in time 95 * @returns time_a - time_b - result > 0 if time_a is newer than time_b 96 */ 97 int32_t btstack_time_delta(uint32_t time_a, uint32_t time_b); 98 99 /** 100 * @brief Read 16/24/32 bit little endian value from buffer 101 * @param buffer 102 * @param position in buffer 103 * @return value 104 */ 105 uint16_t little_endian_read_16(const uint8_t * buffer, int position); 106 uint32_t little_endian_read_24(const uint8_t * buffer, int position); 107 uint32_t little_endian_read_32(const uint8_t * buffer, int position); 108 109 /** 110 * @brief Write 16/32 bit little endian value into buffer 111 * @param buffer 112 * @param position in buffer 113 * @param value 114 */ 115 void little_endian_store_16(uint8_t *buffer, uint16_t position, uint16_t value); 116 void little_endian_store_24(uint8_t *buffer, uint16_t position, uint32_t value); 117 void little_endian_store_32(uint8_t *buffer, uint16_t position, uint32_t value); 118 119 /** 120 * @brief Read 16/24/32 bit big endian value from buffer 121 * @param buffer 122 * @param position in buffer 123 * @return value 124 */ 125 uint32_t big_endian_read_16( const uint8_t * buffer, int pos); 126 uint32_t big_endian_read_24( const uint8_t * buffer, int pos); 127 uint32_t big_endian_read_32( const uint8_t * buffer, int pos); 128 129 /** 130 * @brief Write 16/32 bit big endian value into buffer 131 * @param buffer 132 * @param position in buffer 133 * @param value 134 */ 135 void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); 136 void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value); 137 void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); 138 139 140 /** 141 * @brief Swap bytes in 16 bit integer 142 */ 143 static inline uint16_t btstack_flip_16(uint16_t value){ 144 return (uint16_t)((value & 0xffu) << 8) | (value >> 8); 145 } 146 147 /** 148 * @brief Check for big endian system 149 * @returns 1 if on big endian 150 */ 151 static inline int btstack_is_big_endian(void){ 152 uint16_t sample = 0x0100; 153 return (int) *(uint8_t*) &sample; 154 } 155 156 /** 157 * @brief Check for little endian system 158 * @returns 1 if on little endian 159 */ 160 static inline int btstack_is_little_endian(void){ 161 uint16_t sample = 0x0001; 162 return (int) *(uint8_t*) &sample; 163 } 164 165 /** 166 * @brief Copy from source to destination and reverse byte order 167 * @param src 168 * @param dest 169 * @param len 170 */ 171 void reverse_bytes (const uint8_t *src, uint8_t * dest, int len); 172 173 /** 174 * @brief Wrapper around reverse_bytes for common buffer sizes 175 * @param src 176 * @param dest 177 */ 178 void reverse_24 (const uint8_t *src, uint8_t * dest); 179 void reverse_48 (const uint8_t *src, uint8_t * dest); 180 void reverse_56 (const uint8_t *src, uint8_t * dest); 181 void reverse_64 (const uint8_t *src, uint8_t * dest); 182 void reverse_128(const uint8_t *src, uint8_t * dest); 183 void reverse_256(const uint8_t *src, uint8_t * dest); 184 185 void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest); 186 187 /** 188 * @brief ASCII character for 4-bit nibble 189 * @return character 190 */ 191 char char_for_nibble(int nibble); 192 193 /** 194 * @brif 4-bit nibble from ASCII character 195 * @return value 196 */ 197 int nibble_for_char(char c); 198 199 /** 200 * @brief Compare two Bluetooth addresses 201 * @param a 202 * @param b 203 * @return 0 if equal 204 */ 205 int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b); 206 207 /** 208 * @brief Copy Bluetooth address 209 * @param dest 210 * @param src 211 */ 212 void bd_addr_copy(bd_addr_t dest, const bd_addr_t src); 213 214 /** 215 * @brief Use printf to write hexdump as single line of data 216 */ 217 void printf_hexdump(const void *data, int size); 218 219 /** 220 * @brief Create human readable representation for UUID128 221 * @note uses fixed global buffer 222 * @return pointer to UUID128 string 223 */ 224 char * uuid128_to_str(const uint8_t * uuid); 225 226 /** 227 * @brief Create human readable represenationt of Bluetooth address 228 * @note uses fixed global buffer 229 * @return pointer to Bluetooth address string 230 */ 231 char * bd_addr_to_str(const bd_addr_t addr); 232 233 /** 234 * @brief Replace address placeholder '00:00:00:00:00:00' with Bluetooth address 235 * @param buffer 236 * @param size 237 * @param address 238 */ 239 void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address); 240 241 /** 242 * @brief Parse Bluetooth address 243 * @param address_string 244 * @param buffer for parsed address 245 * @return 1 if string was parsed successfully 246 */ 247 int sscanf_bd_addr(const char * addr_string, bd_addr_t addr); 248 249 /** 250 * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID 251 * @param uuid128 output buffer 252 * @param short_uuid 253 */ 254 void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid); 255 256 /** 257 * @brief Checks if UUID128 has Bluetooth base UUID prefix 258 * @param uui128 to test 259 * @return 1 if it can be expressed as UUID32 260 */ 261 int uuid_has_bluetooth_prefix(const uint8_t * uuid128); 262 263 /** 264 * @brief Parse unsigned number 265 * @param str to parse 266 * @return value 267 */ 268 uint32_t btstack_atoi(const char *str); 269 270 /** 271 * @brief Return number of digits of a uint32 number 272 * @param uint32_number 273 * @return num_digits 274 */ 275 int string_len_for_uint32(uint32_t i); 276 277 /** 278 * @brief Return number of set bits in a uint32 number 279 * @param uint32_number 280 * @return num_set_bits 281 */ 282 int count_set_bits_uint32(uint32_t x); 283 284 /** 285 * CRC8 functions using ETSI TS 101 369 V6.3.0. 286 * Only used by RFCOMM 287 */ 288 uint8_t btstack_crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum); 289 uint8_t btstack_crc8_calc(uint8_t *data, uint16_t len); 290 291 /* API_END */ 292 293 #if defined __cplusplus 294 } 295 #endif 296 297 #endif // BTSTACK_UTIL_H 298