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