1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
24 #include "btcore/include/uuid.h"
25 #include "osi/include/allocator.h"
27 static const size_t UUID_WELL_FORMED_STRING_LEN = 36;
28 static const size_t UUID_WELL_FORMED_STRING_LEN_WITH_NULL = 36 + 1;
30 typedef struct uuid_string_t {
34 static const bt_uuid_t empty_uuid = {{ 0 }};
36 // The base UUID is used for calculating 128-bit UUIDs from 16 and
37 // 32 bit UUIDs as described in the SDP specification.
38 static const bt_uuid_t base_uuid = {
39 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
40 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb, }};
42 static bool uuid_is_base(const bt_uuid_t *uuid);
44 uuid_string_t *uuid_string_new(void) {
45 return osi_calloc(UUID_WELL_FORMED_STRING_LEN_WITH_NULL);
48 void uuid_string_free(uuid_string_t *uuid_string) {
49 osi_free(uuid_string);
52 const char *uuid_string_data(const uuid_string_t *uuid_string) {
53 assert(uuid_string != NULL);
54 return (const char *)uuid_string->string;
57 bt_uuid_t *uuid_new(const char *uuid_string) {
58 assert(uuid_string != NULL);
60 if (strlen(uuid_string) < UUID_WELL_FORMED_STRING_LEN)
62 if (uuid_string[8] != '-' || uuid_string[13] != '-' || uuid_string[18] != '-' || uuid_string[23] != '-')
65 bt_uuid_t *uuid = osi_calloc(sizeof(bt_uuid_t));
69 const char *s = uuid_string;
70 for (size_t i = 0; i < sizeof(bt_uuid_t); ++i, s+=2) {
74 uuid->uu[i] = strtoul(buf, NULL, 16);
75 // Adjust by skipping the dashes
88 void uuid_free(bt_uuid_t *uuid) {
92 bool uuid_is_empty(const bt_uuid_t *uuid) {
93 return !uuid || !memcmp(uuid, &empty_uuid, sizeof(bt_uuid_t));
96 bool uuid_is_equal(const bt_uuid_t *first, const bt_uuid_t *second) {
97 assert(first != NULL);
98 assert(second != NULL);
99 return !memcmp(first, second, sizeof(bt_uuid_t));
102 bt_uuid_t *uuid_copy(bt_uuid_t *dest, const bt_uuid_t *src) {
103 assert(dest != NULL);
105 return (bt_uuid_t *)memcpy(dest, src, sizeof(bt_uuid_t));
108 bool uuid_128_to_16(const bt_uuid_t *uuid, uint16_t *uuid16) {
109 assert(uuid != NULL);
110 assert(uuid16 != NULL);
112 if (!uuid_is_base(uuid))
115 *uuid16 = (uuid->uu[2] << 8) + uuid->uu[3];
119 bool uuid_128_to_32(const bt_uuid_t *uuid, uint32_t *uuid32) {
120 assert(uuid != NULL);
121 assert(uuid32 != NULL);
123 if (!uuid_is_base(uuid))
126 *uuid32 = (uuid->uu[0] << 24) + (uuid->uu[1] << 16) + (uuid->uu[2] << 8) + uuid->uu[3];
130 void uuid_to_string(const bt_uuid_t *uuid, uuid_string_t *uuid_string) {
131 assert(uuid != NULL);
132 assert(uuid_string != NULL);
134 char *string = uuid_string->string;
136 for (int i = 0; i < 4; i++) {
137 string += sprintf(string, "%02x", uuid->uu[i]);
139 string += sprintf(string, "-");
140 for (int i = 4; i < 6; i++) {
141 string += sprintf(string, "%02x", uuid->uu[i]);
143 string += sprintf(string, "-");
144 for (int i = 6; i < 8; i++) {
145 string += sprintf(string, "%02x", uuid->uu[i]);
147 string += sprintf(string, "-");
148 for (int i = 8; i < 10; i++) {
149 string += sprintf(string, "%02x", uuid->uu[i]);
151 string += sprintf(string, "-");
152 for (int i = 10; i < 16; i++) {
153 string += sprintf(string, "%02x", uuid->uu[i]);
157 static bool uuid_is_base(const bt_uuid_t *uuid) {
161 for (int i = 4; i < 16; i++) {
162 if (uuid->uu[i] != base_uuid.uu[i])