1 /* uuid.c - Bluetooth UUID handling */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <string.h>
10 #include <errno.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/printk.h>
13 
14 #include <zephyr/bluetooth/uuid.h>
15 
16 #define UUID_16_BASE_OFFSET 12
17 
18 /* TODO: Decide whether to continue using BLE format or switch to RFC 4122 */
19 
20 /* Base UUID : 0000[0000]-0000-1000-8000-00805F9B34FB
21  * 0x2800    : 0000[2800]-0000-1000-8000-00805F9B34FB
22  *  little endian 0x2800 : [00 28] -> no swapping required
23  *  big endian 0x2800    : [28 00] -> swapping required
24  */
25 static const struct bt_uuid_128 uuid128_base = {
26 	.uuid = { BT_UUID_TYPE_128 },
27 	.val = { BT_UUID_128_ENCODE(
28 		0x00000000, 0x0000, 0x1000, 0x8000, 0x00805F9B34FB) }
29 };
30 
uuid_to_uuid128(const struct bt_uuid * src,struct bt_uuid_128 * dst)31 static void uuid_to_uuid128(const struct bt_uuid *src, struct bt_uuid_128 *dst)
32 {
33 	switch (src->type) {
34 	case BT_UUID_TYPE_16:
35 		*dst = uuid128_base;
36 		sys_put_le16(BT_UUID_16(src)->val,
37 			     &dst->val[UUID_16_BASE_OFFSET]);
38 		return;
39 	case BT_UUID_TYPE_32:
40 		*dst = uuid128_base;
41 		sys_put_le32(BT_UUID_32(src)->val,
42 			     &dst->val[UUID_16_BASE_OFFSET]);
43 		return;
44 	case BT_UUID_TYPE_128:
45 		memcpy(dst, src, sizeof(*dst));
46 		return;
47 	}
48 }
49 
uuid128_cmp(const struct bt_uuid * u1,const struct bt_uuid * u2)50 static int uuid128_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2)
51 {
52 	struct bt_uuid_128 uuid1, uuid2;
53 
54 	uuid_to_uuid128(u1, &uuid1);
55 	uuid_to_uuid128(u2, &uuid2);
56 
57 	return memcmp(uuid1.val, uuid2.val, 16);
58 }
59 
bt_uuid_cmp(const struct bt_uuid * u1,const struct bt_uuid * u2)60 int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2)
61 {
62 	/* Convert to 128 bit if types don't match */
63 	if (u1->type != u2->type) {
64 		return uuid128_cmp(u1, u2);
65 	}
66 
67 	switch (u1->type) {
68 	case BT_UUID_TYPE_16:
69 		return (int)BT_UUID_16(u1)->val - (int)BT_UUID_16(u2)->val;
70 	case BT_UUID_TYPE_32:
71 		return (int)BT_UUID_32(u1)->val - (int)BT_UUID_32(u2)->val;
72 	case BT_UUID_TYPE_128:
73 		return memcmp(BT_UUID_128(u1)->val, BT_UUID_128(u2)->val, 16);
74 	}
75 
76 	return -EINVAL;
77 }
78 
bt_uuid_create(struct bt_uuid * uuid,const uint8_t * data,uint8_t data_len)79 bool bt_uuid_create(struct bt_uuid *uuid, const uint8_t *data, uint8_t data_len)
80 {
81 	/* Copy UUID from packet data/internal variable to internal bt_uuid */
82 	switch (data_len) {
83 	case BT_UUID_SIZE_16:
84 		uuid->type = BT_UUID_TYPE_16;
85 		BT_UUID_16(uuid)->val = sys_get_le16(data);
86 		break;
87 	case BT_UUID_SIZE_32:
88 		uuid->type = BT_UUID_TYPE_32;
89 		BT_UUID_32(uuid)->val = sys_get_le32(data);
90 		break;
91 	case BT_UUID_SIZE_128:
92 		uuid->type = BT_UUID_TYPE_128;
93 		memcpy(&BT_UUID_128(uuid)->val, data, 16);
94 		break;
95 	default:
96 		return false;
97 	}
98 	return true;
99 }
100 
bt_uuid_to_str(const struct bt_uuid * uuid,char * str,size_t len)101 void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len)
102 {
103 	uint32_t tmp1, tmp5;
104 	uint16_t tmp0, tmp2, tmp3, tmp4;
105 
106 	switch (uuid->type) {
107 	case BT_UUID_TYPE_16:
108 		snprintk(str, len, "%04x", BT_UUID_16(uuid)->val);
109 		break;
110 	case BT_UUID_TYPE_32:
111 		snprintk(str, len, "%08x", BT_UUID_32(uuid)->val);
112 		break;
113 	case BT_UUID_TYPE_128:
114 		memcpy(&tmp0, &BT_UUID_128(uuid)->val[0], sizeof(tmp0));
115 		memcpy(&tmp1, &BT_UUID_128(uuid)->val[2], sizeof(tmp1));
116 		memcpy(&tmp2, &BT_UUID_128(uuid)->val[6], sizeof(tmp2));
117 		memcpy(&tmp3, &BT_UUID_128(uuid)->val[8], sizeof(tmp3));
118 		memcpy(&tmp4, &BT_UUID_128(uuid)->val[10], sizeof(tmp4));
119 		memcpy(&tmp5, &BT_UUID_128(uuid)->val[12], sizeof(tmp5));
120 
121 		snprintk(str, len, "%08x-%04x-%04x-%04x-%08x%04x",
122 			 sys_le32_to_cpu(tmp5), sys_le16_to_cpu(tmp4),
123 			 sys_le16_to_cpu(tmp3), sys_le16_to_cpu(tmp2),
124 			 sys_le32_to_cpu(tmp1), sys_le16_to_cpu(tmp0));
125 		break;
126 	default:
127 		(void)memset(str, 0, len);
128 		return;
129 	}
130 }
131