b2457602c7b57c841fbc92b614691b7644c1b21b
2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
12 #include "common/assert.h"
13 #include "common/uuid.h"
17 * Generate a random UUID according to RFC4122, section 4.4.
20 void bt_uuid_generate(bt_uuid_t uuid_out
)
30 * Generate 16 bytes of random bits.
32 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
33 uuid_out
[i
] = (uint8_t) g_rand_int(rand
);
37 * Set the two most significant bits (bits 6 and 7) of the
38 * clock_seq_hi_and_reserved to zero and one, respectively.
40 uuid_out
[8] &= ~(1 << 6);
41 uuid_out
[8] |= (1 << 7);
44 * Set the four most significant bits (bits 12 through 15) of the
45 * time_hi_and_version field to the 4-bit version number from
49 uuid_out
[6] |= (BT_UUID_VER
<< 4);
55 void bt_uuid_to_str(const bt_uuid_t uuid_in
, char *str_out
)
57 BT_ASSERT_DBG(uuid_in
);
58 BT_ASSERT_DBG(str_out
);
60 snprintf(str_out
, BT_UUID_STR_LEN
+ 1, BT_UUID_FMT
, BT_UUID_FMT_VALUES(uuid_in
));
64 int bt_uuid_from_str(const char *str_in
, bt_uuid_t uuid_out
)
69 BT_ASSERT_DBG(uuid_out
);
70 BT_ASSERT_DBG(str_in
);
72 if (strnlen(str_in
, BT_UUID_STR_LEN
+ 1) != BT_UUID_STR_LEN
) {
77 /* Scan to a temporary location in case of a partial match. */
78 if (sscanf(str_in
, BT_UUID_FMT
, BT_UUID_SCAN_VALUES(uuid_scan
)) != BT_UUID_LEN
) {
82 bt_uuid_copy(uuid_out
, uuid_scan
);
88 int bt_uuid_compare(const bt_uuid_t uuid_a
, const bt_uuid_t uuid_b
)
90 return memcmp(uuid_a
, uuid_b
, BT_UUID_LEN
);
94 void bt_uuid_copy(bt_uuid_t uuid_dest
, const bt_uuid_t uuid_src
)
98 BT_ASSERT(uuid_dest
!= uuid_src
);
100 memcpy(uuid_dest
, uuid_src
, BT_UUID_LEN
);
This page took 0.044644 seconds and 4 git commands to generate.