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 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; 29 30 typedef struct uuid_string_t { 31 char string[0]; 32 } uuid_string_t; 33 34 static 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. 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, }}; 41 42 static bool uuid_is_base(const bt_uuid_t *uuid); 43 44 uuid_string_t *uuid_string_new(void) { 45 return osi_calloc(UUID_WELL_FORMED_STRING_LEN_WITH_NULL); 46 } 47 48 void uuid_string_free(uuid_string_t *uuid_string) { 49 osi_free(uuid_string); 50 } 51 52 const char *uuid_string_data(const uuid_string_t *uuid_string) { 53 assert(uuid_string != NULL); 54 return (const char *)uuid_string->string; 55 } 56 57 bt_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 67 const char *s = uuid_string; 68 for (size_t i = 0; i < sizeof(bt_uuid_t); ++i, s+=2) { 69 char buf[3] = {0}; 70 buf[0] = s[0]; 71 buf[1] = s[1]; 72 uuid->uu[i] = strtoul(buf, NULL, 16); 73 // Adjust by skipping the dashes 74 switch(i) { 75 case 3: 76 case 5: 77 case 7: 78 case 9: 79 s++; 80 break; 81 } 82 } 83 return uuid; 84 } 85 86 void uuid_free(bt_uuid_t *uuid) { 87 osi_free(uuid); 88 } 89 90 bool uuid_is_empty(const bt_uuid_t *uuid) { 91 return !uuid || !memcmp(uuid, &empty_uuid, sizeof(bt_uuid_t)); 92 } 93 94 bool uuid_is_equal(const bt_uuid_t *first, const bt_uuid_t *second) { 95 assert(first != NULL); 96 assert(second != NULL); 97 return !memcmp(first, second, sizeof(bt_uuid_t)); 98 } 99 100 bt_uuid_t *uuid_copy(bt_uuid_t *dest, const bt_uuid_t *src) { 101 assert(dest != NULL); 102 assert(src != NULL); 103 return (bt_uuid_t *)memcpy(dest, src, sizeof(bt_uuid_t)); 104 } 105 106 bool uuid_128_to_16(const bt_uuid_t *uuid, uint16_t *uuid16) { 107 assert(uuid != NULL); 108 assert(uuid16 != NULL); 109 110 if (!uuid_is_base(uuid)) 111 return false; 112 113 *uuid16 = (uuid->uu[2] << 8) + uuid->uu[3]; 114 return true; 115 } 116 117 bool uuid_128_to_32(const bt_uuid_t *uuid, uint32_t *uuid32) { 118 assert(uuid != NULL); 119 assert(uuid32 != NULL); 120 121 if (!uuid_is_base(uuid)) 122 return false; 123 124 *uuid32 = (uuid->uu[0] << 24) + (uuid->uu[1] << 16) + (uuid->uu[2] << 8) + uuid->uu[3]; 125 return true; 126 } 127 128 void uuid_to_string(const bt_uuid_t *uuid, uuid_string_t *uuid_string) { 129 assert(uuid != NULL); 130 assert(uuid_string != NULL); 131 132 char *string = uuid_string->string; 133 134 for (int i = 0; i < 4; i++) { 135 string += sprintf(string, "%02x", uuid->uu[i]); 136 } 137 string += sprintf(string, "-"); 138 for (int i = 4; i < 6; i++) { 139 string += sprintf(string, "%02x", uuid->uu[i]); 140 } 141 string += sprintf(string, "-"); 142 for (int i = 6; i < 8; i++) { 143 string += sprintf(string, "%02x", uuid->uu[i]); 144 } 145 string += sprintf(string, "-"); 146 for (int i = 8; i < 10; i++) { 147 string += sprintf(string, "%02x", uuid->uu[i]); 148 } 149 string += sprintf(string, "-"); 150 for (int i = 10; i < 16; i++) { 151 string += sprintf(string, "%02x", uuid->uu[i]); 152 } 153 } 154 155 static bool uuid_is_base(const bt_uuid_t *uuid) { 156 if (!uuid) 157 return false; 158 159 for (int i = 4; i < 16; i++) { 160 if (uuid->uu[i] != base_uuid.uu[i]) 161 return false; 162 } 163 return true; 164 } 165