1 /* Copyright (c) 2019 Intel Corporation
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <stdint.h>
6 #include <zephyr/ztest.h>
7 #include <zephyr/bluetooth/uuid.h>
8 
9 static struct bt_uuid_128 le_128 = BT_UUID_INIT_128(
10 	0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
11 	0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00);
12 
13 ZTEST_SUITE(bt_uuid_create, NULL, NULL, NULL, NULL, NULL);
14 
ZTEST(bt_uuid_create,test_uuid_create)15 ZTEST(bt_uuid_create, test_uuid_create)
16 {
17 	uint8_t le16[] = { 0x01, 0x00 };
18 	uint8_t be16[] = { 0x00, 0x01 };
19 	union {
20 		struct bt_uuid uuid;
21 		struct bt_uuid_16 u16;
22 		struct bt_uuid_128 u128;
23 	} u;
24 
25 	/* Create UUID from LE 16 bit byte array */
26 	zassert_true(bt_uuid_create(&u.uuid, le16, sizeof(le16)),
27 		     "Unable create UUID");
28 
29 	/* Compare UUID 16 bits */
30 	zassert_true(bt_uuid_cmp(&u.uuid, BT_UUID_DECLARE_16(0x0001)) == 0,
31 		     "Test UUIDs don't match");
32 
33 	/* Compare UUID 128 bits */
34 	zassert_true(bt_uuid_cmp(&u.uuid, &le_128.uuid) == 0,
35 		     "Test UUIDs don't match");
36 
37 	/* Compare swapped UUID 16 bits */
38 	zassert_false(bt_uuid_cmp(&u.uuid, BT_UUID_DECLARE_16(0x0100)) == 0,
39 		     "Test UUIDs match");
40 
41 	/* Create UUID from BE 16 bit byte array */
42 	zassert_true(bt_uuid_create(&u.uuid, be16, sizeof(be16)),
43 		     "Unable create UUID");
44 
45 	/* Compare UUID 16 bits */
46 	zassert_false(bt_uuid_cmp(&u.uuid, BT_UUID_DECLARE_16(0x0001)) == 0,
47 		     "Test UUIDs match");
48 
49 	/* Compare UUID 128 bits */
50 	zassert_false(bt_uuid_cmp(&u.uuid, &le_128.uuid) == 0,
51 		     "Test UUIDs match");
52 
53 	/* Compare swapped UUID 16 bits */
54 	zassert_true(bt_uuid_cmp(&u.uuid, BT_UUID_DECLARE_16(0x0100)) == 0,
55 		     "Test UUIDs don't match");
56 }
57