1/****************************************************************************** 2 * 3 * Copyright (C) 2014 Google, Inc. 4 * 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: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 ******************************************************************************/ 18 19#include <base/logging.h> 20#include <stdio.h> 21#include <stdlib.h> 22#include <string.h> 23 24#include "btcore/include/uuid.h" 25#include "osi/include/allocator.h" 26 27static const size_t UUID_WELL_FORMED_STRING_LEN = 36; 28static const size_t UUID_WELL_FORMED_STRING_LEN_WITH_NULL = 36 + 1; 29 30typedef struct uuid_string_t { char string[0]; } uuid_string_t; 31 32static const bt_uuid_t empty_uuid = {{0}}; 33 34// The base UUID is used for calculating 128-bit UUIDs from 16 and 35// 32 bit UUIDs as described in the SDP specification. 36static const bt_uuid_t base_uuid = {{ 37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 38 0x5f, 0x9b, 0x34, 0xfb, 39}}; 40 41static bool uuid_is_base(const bt_uuid_t* uuid); 42 43uuid_string_t* uuid_string_new(void) { 44 return static_cast<uuid_string_t*>( 45 osi_calloc(UUID_WELL_FORMED_STRING_LEN_WITH_NULL)); 46} 47 48void uuid_string_free(uuid_string_t* uuid_string) { osi_free(uuid_string); } 49 50const char* uuid_string_data(const uuid_string_t* uuid_string) { 51 CHECK(uuid_string != NULL); 52 return (const char*)uuid_string->string; 53} 54 55bt_uuid_t* uuid_new(const char* uuid_string) { 56 CHECK(uuid_string != NULL); 57 58 if (strlen(uuid_string) < UUID_WELL_FORMED_STRING_LEN) return NULL; 59 if (uuid_string[8] != '-' || uuid_string[13] != '-' || 60 uuid_string[18] != '-' || uuid_string[23] != '-') 61 return NULL; 62 63 bt_uuid_t* uuid = static_cast<bt_uuid_t*>(osi_calloc(sizeof(bt_uuid_t))); 64 65 const char* s = uuid_string; 66 for (size_t i = 0; i < sizeof(bt_uuid_t); ++i, s += 2) { 67 char buf[3] = {0}; 68 buf[0] = s[0]; 69 buf[1] = s[1]; 70 uuid->uu[i] = strtoul(buf, NULL, 16); 71 // Adjust by skipping the dashes 72 switch (i) { 73 case 3: 74 case 5: 75 case 7: 76 case 9: 77 s++; 78 break; 79 } 80 } 81 return uuid; 82} 83 84void uuid_free(bt_uuid_t* uuid) { osi_free(uuid); } 85 86bool uuid_is_empty(const bt_uuid_t* uuid) { 87 return !uuid || !memcmp(uuid, &empty_uuid, sizeof(bt_uuid_t)); 88} 89 90bool uuid_is_equal(const bt_uuid_t* first, const bt_uuid_t* second) { 91 CHECK(first != NULL); 92 CHECK(second != NULL); 93 return !memcmp(first, second, sizeof(bt_uuid_t)); 94} 95 96bt_uuid_t* uuid_copy(bt_uuid_t* dest, const bt_uuid_t* src) { 97 CHECK(dest != NULL); 98 CHECK(src != NULL); 99 return (bt_uuid_t*)memcpy(dest, src, sizeof(bt_uuid_t)); 100} 101 102bool uuid_128_to_16(const bt_uuid_t* uuid, uint16_t* uuid16) { 103 CHECK(uuid != NULL); 104 CHECK(uuid16 != NULL); 105 106 if (!uuid_is_base(uuid)) return false; 107 108 *uuid16 = (uuid->uu[2] << 8) + uuid->uu[3]; 109 return true; 110} 111 112bool uuid_128_to_32(const bt_uuid_t* uuid, uint32_t* uuid32) { 113 CHECK(uuid != NULL); 114 CHECK(uuid32 != NULL); 115 116 if (!uuid_is_base(uuid)) return false; 117 118 *uuid32 = (uuid->uu[0] << 24) + (uuid->uu[1] << 16) + (uuid->uu[2] << 8) + 119 uuid->uu[3]; 120 return true; 121} 122 123void uuid_to_string(const bt_uuid_t* uuid, uuid_string_t* uuid_string) { 124 CHECK(uuid != NULL); 125 CHECK(uuid_string != NULL); 126 127 char* string = uuid_string->string; 128 char* end = string + UUID_WELL_FORMED_STRING_LEN_WITH_NULL; 129 130 // XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX 131 for (int i = 0; i < 4; i++) { 132 string += snprintf(string, end - string, "%02x", uuid->uu[i]); 133 } 134 *string = '-'; 135 ++string; 136 for (int i = 4; i < 6; i++) { 137 string += snprintf(string, end - string, "%02x", uuid->uu[i]); 138 } 139 *string = '-'; 140 ++string; 141 for (int i = 6; i < 8; i++) { 142 string += snprintf(string, end - string, "%02x", uuid->uu[i]); 143 } 144 *string = '-'; 145 ++string; 146 for (int i = 8; i < 10; i++) { 147 string += snprintf(string, end - string, "%02x", uuid->uu[i]); 148 } 149 *string = '-'; 150 ++string; 151 for (int i = 10; i < 16; i++) { 152 string += snprintf(string, end - string, "%02x", uuid->uu[i]); 153 } 154} 155 156static bool uuid_is_base(const bt_uuid_t* uuid) { 157 if (!uuid) return false; 158 159 for (int i = 4; i < 16; i++) { 160 if (uuid->uu[i] != base_uuid.uu[i]) return false; 161 } 162 return true; 163} 164