Select one of the symbols to view example projects that use it.
 
Outline
#define BTSTACK_UTIL_H
#include <stdint.h>
#include <string.h>
#include "bluetooth.h"
#include "btstack_defines.h"
#include "btstack_linked_list.h"
#define DEVICE_NAME_LEN
device_name_t
btstack_min(uint32_t, uint32_t);
btstack_max(uint32_t, uint32_t);
btstack_time_delta(uint32_t, uint32_t);
btstack_time16_delta(uint16_t, uint16_t);
little_endian_read_16(const uint8_t *, int);
little_endian_read_24(const uint8_t *, int);
little_endian_read_32(const uint8_t *, int);
little_endian_store_16(uint8_t *, uint16_t, uint16_t);
little_endian_store_24(uint8_t *, uint16_t, uint32_t);
little_endian_store_32(uint8_t *, uint16_t, uint32_t);
big_endian_read_16(const uint8_t *, int);
big_endian_read_24(const uint8_t *, int);
big_endian_read_32(const uint8_t *, int);
big_endian_store_16(uint8_t *, uint16_t, uint16_t);
big_endian_store_24(uint8_t *, uint16_t, uint32_t);
big_endian_store_32(uint8_t *, uint16_t, uint32_t);
btstack_flip_16(uint16_t)
btstack_is_big_endian()
btstack_is_little_endian()
reverse_bytes(const uint8_t *, uint8_t *, int);
reverse_24(const uint8_t *, uint8_t *);
reverse_48(const uint8_t *, uint8_t *);
reverse_56(const uint8_t *, uint8_t *);
reverse_64(const uint8_t *, uint8_t *);
reverse_128(const uint8_t *, uint8_t *);
reverse_256(const uint8_t *, uint8_t *);
reverse_bd_addr(const uint8_t *, uint8_t *);
btstack_is_null(const uint8_t *, uint16_t);
btstack_is_null_bd_addr(const uint8_t *);
char_for_nibble(uint8_t);
nibble_for_char(char);
bd_addr_cmp(const uint8_t *, const uint8_t *);
bd_addr_copy(uint8_t *, const uint8_t *);
printf_hexdump(const void *, int);
uuid128_to_str(const uint8_t *);
bd_addr_to_str_with_delimiter(const uint8_t *, char);
bd_addr_to_str(const uint8_t *);
btstack_replace_bd_addr_placeholder(uint8_t *, uint16_t, const uint8_t *);
sscanf_bd_addr(const char *, uint8_t *);
uuid_add_bluetooth_prefix(uint8_t *, uint32_t);
uuid_has_bluetooth_prefix(const uint8_t *);
btstack_atoi(const char *);
string_len_for_uint32(uint32_t);
count_set_bits_uint32(uint32_t);
btstack_crc8_check(uint8_t *, uint16_t, uint8_t);
btstack_crc8_calc(uint8_t *, uint16_t);
btstack_crc32_init();
btstack_crc32_update(uint32_t, const uint8_t *, uint32_t);
btstack_crc32_finalize(uint32_t);
btstack_next_cid_ignoring_zero(uint16_t);
btstack_strcpy(char *, uint16_t, const char *);
btstack_strcat(char *, uint16_t, const char *);
btstack_clz(uint32_t);
btstack_virtual_memcpy(const uint8_t *, uint16_t, uint16_t, uint8_t *, uint16_t, uint16_t);
Files
loading...
SourceVuRaspberry Pi Pico SDK and ExamplesBluetooth LE Stacksrc/btstack_util.h
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * *//* ... */ /** * @title General Utility Functions * *//* ... */ #ifndef BTSTACK_UTIL_H #define BTSTACK_UTIL_H #if defined __cplusplus extern "C" { #endif #include <stdint.h> #include <string.h> #include "bluetooth.h" #include "btstack_defines.h" #include "btstack_linked_list.h" 5 includes // hack: compilation with the android ndk causes an error as there's a reverse_64 macro #ifdef reverse_64 #undef reverse_64 #endif // will be moved to daemon/btstack_device_name_db.h /** * @brief The device name type *//* ... */ #define DEVICE_NAME_LEN 248 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; /* API_START */ /** * @brief Minimum function for uint32_t * @param a * @param b * @return value *//* ... */ uint32_t btstack_min(uint32_t a, uint32_t b); /** * @brief Maximum function for uint32_t * @param a * @param b * @return value *//* ... */ uint32_t btstack_max(uint32_t a, uint32_t b); /** * @brief Calculate delta between two uint32_t points in time * @return time_a - time_b - result > 0 if time_a is newer than time_b *//* ... */ int32_t btstack_time_delta(uint32_t time_a, uint32_t time_b); /** * @brief Calculate delta between two uint16_t points in time * @return time_a - time_b - result > 0 if time_a is newer than time_b *//* ... */ int16_t btstack_time16_delta(uint16_t time_a, uint16_t time_b); /** * @brief Read 16/24/32 bit little endian value from buffer * @param buffer * @param position in buffer * @return value *//* ... */ uint16_t little_endian_read_16(const uint8_t * buffer, int position); uint32_t little_endian_read_24(const uint8_t * buffer, int position); uint32_t little_endian_read_32(const uint8_t * buffer, int position); /** * @brief Write 16/32 bit little endian value into buffer * @param buffer * @param position in buffer * @param value *//* ... */ void little_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); void little_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); void little_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); /** * @brief Read 16/24/32 bit big endian value from buffer * @param buffer * @param position in buffer * @return value *//* ... */ uint32_t big_endian_read_16(const uint8_t * buffer, int position); uint32_t big_endian_read_24(const uint8_t * buffer, int position); uint32_t big_endian_read_32(const uint8_t * buffer, int position); /** * @brief Write 16/32 bit big endian value into buffer * @param buffer * @param position in buffer * @param value *//* ... */ void big_endian_store_16(uint8_t * buffer, uint16_t position, uint16_t value); void big_endian_store_24(uint8_t * buffer, uint16_t position, uint32_t value); void big_endian_store_32(uint8_t * buffer, uint16_t position, uint32_t value); /** * @brief Swap bytes in 16 bit integer *//* ... */ static inline uint16_t btstack_flip_16(uint16_t value){ return (uint16_t)((value & 0xffu) << 8) | (value >> 8); }{ ... } /** * @brief Check for big endian system * @return 1 if on big endian *//* ... */ static inline int btstack_is_big_endian(void){ uint16_t sample = 0x0100; return (int) *(uint8_t*) &sample; }{ ... } /** * @brief Check for little endian system * @return 1 if on little endian *//* ... */ static inline int btstack_is_little_endian(void){ uint16_t sample = 0x0001; return (int) *(uint8_t*) &sample; }{ ... } /** * @brief Copy from source to destination and reverse byte order * @param src * @param dest * @param len *//* ... */ void reverse_bytes(const uint8_t * src, uint8_t * dest, int len); /** * @brief Wrapper around reverse_bytes for common buffer sizes * @param src * @param dest *//* ... */ void reverse_24 (const uint8_t * src, uint8_t * dest); void reverse_48 (const uint8_t * src, uint8_t * dest); void reverse_56 (const uint8_t * src, uint8_t * dest); void reverse_64 (const uint8_t * src, uint8_t * dest); void reverse_128(const uint8_t * src, uint8_t * dest); void reverse_256(const uint8_t * src, uint8_t * dest); void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest); /** * @brief Check if all bytes in buffer are zero * @param buffer * @param size * @return true if all bytes is buffer are zero *//* ... */ bool btstack_is_null(const uint8_t * buffer, uint16_t size); /** * @brief Check if all bytes in a bd_addr_t are zero * @param addr * @return true if all bytes in addr are zero *//* ... */ bool btstack_is_null_bd_addr( const bd_addr_t addr ); /** * @brief ASCII character for 4-bit nibble * @return character *//* ... */ char char_for_nibble(uint8_t nibble); /** * @brif 4-bit nibble from ASCII character * @return value *//* ... */ int nibble_for_char(char c); /** * @brief Compare two Bluetooth addresses * @param a * @param b * @return 0 if equal *//* ... */ int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b); /** * @brief Copy Bluetooth address * @param dest * @param src *//* ... */ void bd_addr_copy(bd_addr_t dest, const bd_addr_t src); /** * @brief Use printf to write hexdump as single line of data *//* ... */ void printf_hexdump(const void * data, int size); /** * @brief Create human readable representation for UUID128 * @note uses fixed global buffer * @return pointer to UUID128 string *//* ... */ char * uuid128_to_str(const uint8_t * uuid); /** * @brief Create human readable represenationt of Bluetooth address * @note uses fixed global buffer * @param delimiter * @return pointer to Bluetooth address string *//* ... */ char * bd_addr_to_str_with_delimiter(const bd_addr_t addr, char delimiter); /** * @brief Create human readable represenationt of Bluetooth address * @note uses fixed global buffer * @return pointer to Bluetooth address string *//* ... */ char * bd_addr_to_str(const bd_addr_t addr); /** * @brief Replace address placeholder '00:00:00:00:00:00' with Bluetooth address * @param buffer * @param size * @param address *//* ... */ void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address); /** * @brief Parse Bluetooth address * @param address_string * @param buffer for parsed address * @return 1 if string was parsed successfully *//* ... */ int sscanf_bd_addr(const char * addr_string, bd_addr_t addr); /** * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID * @param uuid128 output buffer * @param short_uuid *//* ... */ void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid); /** * @brief Checks if UUID128 has Bluetooth base UUID prefix * @param uui128 to test * @return true if it can be expressed as UUID32 *//* ... */ bool uuid_has_bluetooth_prefix(const uint8_t * uuid128); /** * @brief Parse unsigned number * @param str to parse * @return value *//* ... */ uint32_t btstack_atoi(const char * str); /** * @brief Return number of digits of a uint32 number * @param uint32_number * @return num_digits *//* ... */ int string_len_for_uint32(uint32_t i); /** * @brief Return number of set bits in a uint32 number * @param uint32_number * @return num_set_bits *//* ... */ int count_set_bits_uint32(uint32_t x); /** * @brief Check CRC8 using ETSI TS 101 369 V6.3.0. * @note Only used by RFCOMM * @param data * @param len * @param check_sum *//* ... */ uint8_t btstack_crc8_check(uint8_t * data, uint16_t len, uint8_t check_sum); /** * @brief Calculate CRC8 using ETSI TS 101 369 V6.3.0. * @note Only used by RFCOMM * @param data * @param len *//* ... */ uint8_t btstack_crc8_calc(uint8_t * data, uint16_t len); /** * @brief Calculate the initial CRC32 value using ISO 3309 (HDLC), polynomial (normal) 0x04c11db7 * @note Used by OTS Service. * * @return The initial crc value. *//* ... */ uint32_t btstack_crc32_init(void); /** * @brief Update the CRC32 value with new data. * * @param crc The current crc value. * @param data Pointer to a buffer of \a data_len bytes. * @param data_len Number of bytes in the \a data buffer. * @return The updated crc value. *//* ... */ uint32_t btstack_crc32_update(uint32_t crc, const uint8_t * data, uint32_t data_len); /** * @brief Calculate the final CRC32 value. * * @param crc The current crc value. * @return The final crc value. *//* ... */ uint32_t btstack_crc32_finalize(uint32_t crc); /** * @brief Get next cid * @param current_cid * @return next cid skiping 0 *//* ... */ uint16_t btstack_next_cid_ignoring_zero(uint16_t current_cid); /** * @brief Copy string (up to dst_size-1 characters) from src into dst buffer with terminating '\0' * @note replaces strncpy + dst[dst_size-1] = '\0' * @param dst * @param dst_size * @param src * @retun bytes_copied including trailing 0 *//* ... */ uint16_t btstack_strcpy(char * dst, uint16_t dst_size, const char * src); /** * @brief Append src string to string in dst buffer with terminating '\0' * @note max total string length will be dst_size-1 characters * @param dst * @param dst_size * @param src *//* ... */ void btstack_strcat(char * dst, uint16_t dst_size, const char * src); /** * Returns the number of leading 0-bits in x, starting at the most significant bit position. * If x is 0, the result is undefined. * @note maps to __builtin_clz for gcc and clang * @param value * @return number of leading 0-bits *//* ... */ uint8_t btstack_clz(uint32_t value); /** * @brief Copy chunk of data into a virtual buffer backed by a physical buffer. * Used to provide chunk of data of larger buffer that is constructed on the fly, e.g. serializing data struct * * For example, copy field2 to buffer, with buffer_offset = 11 * * field1 field2 field3 field4 field5 filed6 * struct: -------|-------|----------|-------------|-------|-------------- * buffer: ------------------ * result: ----| * * When also copying field3 and field4 to buffer, with buffer_offset = 11 * * field1 field2 field3 field4 field5 filed6 * struct: -------|-------|----------|-------------|-------|-------------- * buffer: ------------------ * result: ----|----------|-- * * @param field_data * @param field_len * @param field_offset position of field in complete data block * @param buffer_data * @param buffer_len * @param buffer_offset position of buffer in complete data block * @return bytes_copied number of bytes actually stored in buffer *//* ... */ uint16_t btstack_virtual_memcpy( const uint8_t * field_data, uint16_t field_len, uint16_t field_offset, uint8_t * buffer, uint16_t buffer_size, uint16_t buffer_offset); /* API_END */ #if defined __cplusplus }extern "C" { ... } #endif /* ... */ #endif // BTSTACK_UTIL_H
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.