2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include "common/assert.h"
29 #include "common/uuid.h"
33 * Generate a random UUID according to RFC4122, section 4.4.
36 void bt_uuid_generate(bt_uuid_t uuid_out
)
46 * Generate 16 bytes of random bits.
48 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
49 uuid_out
[i
] = (uint8_t) g_rand_int(rand
);
53 * Set the two most significant bits (bits 6 and 7) of the
54 * clock_seq_hi_and_reserved to zero and one, respectively.
56 uuid_out
[8] &= ~(1 << 6);
57 uuid_out
[8] |= (1 << 7);
60 * Set the four most significant bits (bits 12 through 15) of the
61 * time_hi_and_version field to the 4-bit version number from
65 uuid_out
[6] |= (BT_UUID_VER
<< 4);
71 void bt_uuid_to_str(const bt_uuid_t uuid_in
, char *str_out
)
76 sprintf(str_out
, BT_UUID_FMT
, BT_UUID_FMT_VALUES(uuid_in
));
80 int bt_uuid_from_str(const char *str_in
, bt_uuid_t uuid_out
)
88 if (strnlen(str_in
, BT_UUID_STR_LEN
+ 1) != BT_UUID_STR_LEN
) {
93 /* Scan to a temporary location in case of a partial match. */
94 if (sscanf(str_in
, BT_UUID_FMT
, BT_UUID_SCAN_VALUES(uuid_scan
)) != BT_UUID_LEN
) {
98 bt_uuid_copy(uuid_out
, uuid_scan
);
104 int bt_uuid_compare(const bt_uuid_t uuid_a
, const bt_uuid_t uuid_b
)
106 return memcmp(uuid_a
, uuid_b
, BT_UUID_LEN
);
110 void bt_uuid_copy(bt_uuid_t uuid_dest
, const bt_uuid_t uuid_src
)
112 BT_ASSERT(uuid_dest
);
114 BT_ASSERT(uuid_dest
!= uuid_src
);
116 memcpy(uuid_dest
, uuid_src
, BT_UUID_LEN
);
This page took 0.030505 seconds and 4 git commands to generate.